python - How to convert text file with two columns to fasta format -
i kind of confused bit of code. have testfile.txt
sclsc1_3349_ss1g_09805t0 ttgcgatctatgccgacgttcca sclsc1_8695_ss1g_14118t0 atggtttcggc sclsc1_12154_ss1g_05183t0 atggtttcggc sclsc1_317_ss1g_00317t0 atggtttcggc sclsc1_10094_ss1g_03122t0 atggtttcggc
i want convert file format (fasta
) below:
>sclsc1_3349_ss1g_09805t0 ttgcgatctatgccgacgttcca >sclsc1_8695_ss1g_14118t0 atggtttcggc >sclsc1_12154_ss1g_05183t0 atggtttcggc >sclsc1_317_ss1g_00317t0 atggtttcggc >sclsc1_10094_ss1g_03122t0 atggtttcggc
here python code (run like: python mycode.py testfile.txt outputfile.txt
, not output result wanted. can please me correct code? thanks!
import sys #file input fileinput = open(sys.argv[1], "r") #file output fileoutput = open(sys.argv[2], "w") #seq count count = 1 ; #loop through each line in input file print "converting fasta..." strline in fileinput: #strip endline character each input line strline = strline.rstrip("\n") #output header fileoutput.write("> " + str(count) + "\n") fileoutput.write(strline + "\n") count = count + 1 print ("done.") #close input , output file fileinput.close() fileoutput.close()
as on linux os, here short , fast awk one-liner:
awk '{ printf ">%s\n%s\n",$1,$2 }' testfile.txt > outputfile.txt
the outputfile.txt
contents:
>sclsc1_3349_ss1g_09805t0 ttgcgatctatgccgacgttcca >sclsc1_8695_ss1g_14118t0 atggtttcggc >sclsc1_12154_ss1g_05183t0 atggtttcggc >sclsc1_317_ss1g_00317t0 atggtttcggc >sclsc1_10094_ss1g_03122t0 atggtttcggc
Comments
Post a Comment