如何将两列文本文件转换成fasta格式

2024-10-03 15:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这段代码有点困惑。我有我的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

我想将此文件转换为以下格式(fasta):

^{pr2}$

这是我的python代码(运行方式如下:python mycode.py testfile.txt outputfile.txt,但它没有按我的要求输出结果。有人能帮我纠正这个代码吗?谢谢!在

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 the input file
print "Converting to FASTA..."
for strLine in fileInput:

    #Strip the endline character from each input line
    strLine = strLine.rstrip("\n")

    #Output the header
    fileOutput.write("> " + str(count) + "\n")
    fileOutput.write(strLine + "\n")

    count = count + 1
print ("Done.")

#Close the input and output file
fileInput.close()
fileOutput.close()

Tags: the代码txtinputcountsysopenfile
2条回答

正如您在Linux操作系统上一样,这里有一个简短而快速的awk一行代码:

awk '{ printf ">%s\n%s\n",$1,$2 }' testfile.txt > outputfile.txt

outputfile.txt内容:

^{pr2}$
import sys
inp = open('Dataset.csv', "r")
outp = open('Book1.txt', "w")


print ("Convertion")
for a in inp:
    a = a.rstrip("\n")

    outp.write("> " + strLine[0:6] + "\n")
    outp.write(strLine[11:-4] + "\n")



print ("Done")

inp.close()
outp.close()

相关问题 更多 >