I have a FASTA file with three defined elements in the "description" line.
The first element, defined as dato[0]
, is the one that has to carry out with the condition and the third element, defined as dato[2]
, is the one that I want to sum.
The FASTA description line is like this:
PIN4 HOIAQKS02C4SWQ 1761
PIN1 HOIAQKS02D3JZ3 572
And I want to sum the values (dato[2]
) that carry out the condition dato[0] == PIN1
in one row and the condition dato[0] == PIN4
in another.
I am using the following code:
from Bio import SeqIO
secuencias=SeqIO.parse("/Users/imac/Desktop/Pruebas_UniFrac/otu1_alpin1+4.fasta", "fasta")
PIN_records=list(SeqIO.parse("/Users/imac/Desktop/Pruebas_UniFrac/otu1_alpin1+4.fasta", "fasta")
archivo1=open("/Users/imac/Desktop/Pruebas_UniFrac/pruebaalpin1+4_fin.fasta", "w")
archivo2=open("/Users/imac/Desktop/Pruebas_UniFrac/pruebaalpin1+4_seqsotus.fasta", "w")
archivo3=open("/Users/imac/Desktop/Pruebas_UniFrac/pruebaalpin1+4_sumas.fasta", "w")
x = 0
y = x+1
for linea in secuencias:
dato = linea.description.split(" ")
seqs = str(linea.seq)
if dato[0] != "PIN1":
if dato[0] != "PIN4":
if dato[0] == "consensus":
archivo1.write("hacia arriba OTU" + str(y) + "\n" + "x" + "\n" + "x" + "\n")
archivo2.write(">" + "OTU" + str(y) + "\n" + seqs + "\n")
archivo3.write("fin del OTU" + "\n")
y = y+1
else:
archivo1.write(str(dato[0]) + "," + str(dato[2]) + "\n")
#num = int(dato[2])
#archivo3.write("PIN4=" + str(sum(dato[2])) + "\n")
#archivo3.write("PIN4=%d\n" % sum(dato[2]))
archivo3.write("PIN4={}\n".format(sum(dato[2])))
else:
archivo1.write(str(dato[0]) + "," + str(dato[2]) + "\n")
#num = int(dato[2])
#archivo3.write("PIN1=" + str(sum(dato[2])) + "\n")
#archivo3.write("PIN1=%d\n" % sum(dato[2]))
archivo3.write("PIN1={}\n".format(sum(dato[2])))
archivo1.close()
archivo2.close()
archivo3.close()
And when I do that, I get this error message:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
How can I do that?
After following posterior comments, I have introduced changes in my code, but I can't get it working properly and I do not know how to fix it.
With this code, I get the following error:
File "./lectura_msaout_pruebaalpin1+4_final.py", line 16
archivo1=open("/Users/imac/Desktop/Pruebas_UniFrac/pruebaalpin1+4_fin.fasta", "w")
^
SyntaxError: invalid syntax