Skip to content

Instantly share code, notes, and snippets.

@crazyhottommy
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyhottommy/a59df92b48e6ad3f4630 to your computer and use it in GitHub Desktop.
Save crazyhottommy/a59df92b48e6ad3f4630 to your computer and use it in GitHub Desktop.
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.
@xingpel
Copy link

xingpel commented Jul 29, 2014

it seems somewhere messed up at the "\n", some sequences got multiple lines while the others are not.

@crazyhottommy
Copy link
Author

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