Created
July 31, 2014 15:58
-
-
Save xingpel/4e69ca25338f957955a9 to your computer and use it in GitHub Desktop.
Add annotation for fasta from a dictionary
This file contains 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 ("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 ("output.txt", "w") | |
with open ("data.fasta", "r") as fasta: | |
for line in fasta: | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment