I have the following code:
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()
As you can see, I am currently writing two files. I really only need the second file. Does anyone have any suggestions to combine both "subscripts" into one?
The input file "HPV16_CG.aln.fas" looks like this:
>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
I really appreciate all the help/suggestions to help me improve this!
pep8 myfile.py
andpylint myfile.py
andpyflakes myfile.py
(and there may be others) in order to have cleaner code. \$\endgroup\$