Last active
August 29, 2015 14:04
-
-
Save crazyhottommy/a59df92b48e6ad3f4630 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open ("C:/Users/Tang Ming/Desktop/anotation.txt", "r") as annotation: | |
anotation_dict = {} | |
for line in annotation: | |
line = line.split() | |
if line: #test whether it is an empty line | |
anotation_dict[line[0]]=line[1:] | |
else: | |
continue | |
# really should not parse the fasta file by myself. there are | |
# many parsers already there. you can use module from Biopython | |
ofile = open ("C:/Users/Tang Ming/Desktop/output.txt", "w") | |
with open ("C:/Users/Tang Ming/Desktop/my_fasta.txt", "r") as myfasta: | |
for line in myfasta: | |
if line.startswith (">"): | |
line = line[1:] # skip the ">" character | |
line = line.split() | |
if line[0] in anotation_dict: | |
new_line = ">" + str(line[0]) + " " + " ".join(anotation_dict[line[0]]) | |
ofile.write ( new_line + "\n") | |
else: | |
ofile.write ( ">"+ "".join(line) + "\n") | |
else: | |
ofile.write(line +"\n") | |
ofile.close() # always remember to close the file. |
yes, the last line should add a new line "\n", I though I caught that and made changes. anyways, I updated the code. Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it seems somewhere messed up at the "\n", some sequences got multiple lines while the others are not.