How to make a summation under a condition in Biopython
Question: 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.

Thanks a lot

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 
    


Answer: Your code has two main issues.

  1. You're trying to call `sum()` on string data.
  2. You're trying to format a numeric value as a string.

## Fixing summation

You want to sum an iterable of numeric values, as summing is undefined for
string values. You can convert string values to an integer by calling `int()`
on each value (use the `map()` function to do this).

### Example:

    
    
    >>> sum(["1", "2", "3"])
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> sum([1, 2, 3])
    6
    map(int, ["1", "2", "3"])
    [1, 2, 3]
    >>> sum(map(int, ["1", "2", "3"]))
    6
    

### Application to your code

Do you really want to sum the single digits of `dato[2]`? It'd look like this:

    
    
    >>> dato = ['PIN4', 'HOIAQKS02C4SWQ', '1761']
    >>> sum(map(int, dato[2]))  # 1 + 7 + 6 + 1
    15
    

## Fixing the string formatting

You can't append an integer to a string (see [String and integer
concatenation](http://stackoverflow.com/questions/2847386/string-and-integer-
concatenation)).

The solution is to either convert the integer to a string before
concatenating, or to format the integer within a string. In your case, the
solutions look like this:

  1. Convert to string:
    
        archivo3.write("PIN1=" + str(dato_2_sum) + "\n")
    

  2. Use string formatting:
    
        archivo3.write("PIN1=%d\n" % dato_2_sum)
    

  3. Use newstyle formatting:
    
        archivo3.write("PIN1={}\n".format(dato_2_sum)
    

