我有以下代码:
from Bio import AlignIO
import itertools
out=open("test.csv","a")
align = AlignIO.read("HPV16_CG.aln.fas", "fasta")
n=0
def SNP(line):
result=[]
result.append(str(n+1))
result.append(line[0])
result.append(align[y].id.rsplit("|")[3])
result.append(x)
return result
while n<len(align[0]):
line = align[:,n]
y=0
for x in line:
if line[0]!=x:
print >> out, ','.join(map(str,SNP(line)))
y=y+1
else:
y=y+1
y=0
n=n+1
out.close()
f=open("test.csv","rU")
out=open("test_2.csv","a")
lines=f.read().split()
for key, group in itertools.groupby(lines, lambda line: line.partition(',')[0]):
print >>out, ','.join(group)
out.close()
f.close()
如您所见,我目前正在编写两个文件。我真的只需要第二个文件。有没有人有任何建议将两个“下标”合并为一个?
输入文件“HPV16_CG.aln.fas”如下所示:
>gi|333031|lcl|HPV16REF.1| Alpha-9 - Human Papillomavirus 16, complete genome.
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG
>gi|333031|gb|K02718.1|PPH16 Human papillomavirus type 16 (HPV16), complete genome
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG
>gi|196170262|gb|FJ006723.1| Human papillomavirus type 16, complete genome
ACTACAATAATTCATGTATAAAACTAAGGGCGTAACCGAAATCGGTTGAACCGAAACCGG
我非常感谢所有帮助/建议来帮助我改进这一点!