Counters in files

1

I have created a function that will be part of a small working script with files but I can not get some characters that I am identifying in this function. By the error messages of the shell I deduce that it is a matter of variables that are not being correctly identified. This is what I have at the moment:

import os
os.chdir() #direccion del directorio de ficheros

def numero_bases(fichero):
    A = 0
    T = 0
    C = 0
    G = 0
    with open(fichero) as f:
        lineas = f.readlines()
        for l in lineas:
            if not l.startswith('>'): #esto lo he puesto para que no lea en lineas que empiezan por ('>') que son el encabezado de los registros del fichero, pero no estoy seguro si es la mejor forma para ello.
                for base in l:
                    if base  == 'A':
                        A += 1
                    elif base == 'T':
                        T += 1
                    elif base == 'G':
                        G += 1
                    elif base == 'C':
                        C += 1
        return A,T,C,G

while True:
    fichero = input('Introduzca nombre del fichero FASTA(q para salir):\n')
    if fichero == 'q':
        break
    print(numero_bases(fichero), A, T,C,G)

What I am getting is an error that the variables A, T, C, G do not exist, but sometimes I get, after modifying the site of the variables, a result of 0, that is to say that it is not counting .

Any idea what could be wrong?

    
asked by Steve Jade 20.10.2017 в 13:39
source

1 answer

1

Your real error is in print , that is in print(numero_bases(fichero), A, T,C,G) since A , T , C and G are not defined because they are local variables to the function, they are actually values returned by numero_bases as a tuple. Depending on how you want the exit you have several options:

>>> print(numero_bases(fichero))

(5, 7, 10, 4)

Or something more elaborate using str.format and unpacking the tuple returned by the function:

>>> print("Recuento de bases:\n  A: {}\n  T:{}\n  C: {}\n  G: {}".format(*numero_bases(fichero)))

Recuento de bases:
  A: 5
  T: 7
  C: 10
  G: 4

If you want the function to return the string as a result, you should only do something like:

return "Recuento de bases:\n  A: {}\n  T:{}\n  C: {}\n  G: {}".format(A, T, C, G)

Or in Python> = 3.6 you can also use formatted string literals:

return f"Recuento de bases:\n  A: {A}\n  T:{T}\n  C: {C}\n  G: {G}"
    
answered by 20.10.2017 / 14:06
source