A looping does not end to start another. How to solve? (Python)

Asked

Viewed 85 times

0

Hello, I’m new to the forum and I’m still learning how to use the interface. If I posted in the wrong place or something like that, I’m sorry. I created an account to ask a question about a problem that’s bothering me a lot. I’m making a program in python 3 that takes a Fasta file and makes a list of the "extensions" of genomic sequences. The problem is that in one part of the code, more precisely in that function, I can’t get the other for begin to give the result (in this case, the id of the largest number, i.e., the largest extension in the genomic sequence). The first for stays in an eternal looping and I don’t know how to get him out of it.

def identifiers():
  lista = []
  for record in SeqIO.parse(fasta, "fasta"):
    lista.append(len(record))
  for record in SeqIO.parse(fasta, "fasta"):
    if len(record) == lista_sort[-1]:
      print(record.id)

Does anyone know how to solve this?

  • Matheus, also put the contents of the variable fastaand what is the expected result with the code.

  • I couldn’t upload the file. Is there any way to do this on the site itself?

1 answer

0

The variable fasta is defined somewhere outside the function? I found the following example:

from Bio import SeqIO
for record in SeqIO.parse("example.fasta", "fasta"):
    print(record.id)

From what I understand, Seqio.parse’s first argument must be a file name or a variable equivalent to the file name. If your file is called fasta.fasta, for example, try using:

def identifiers():
  lista = []
  for record in SeqIO.parse("fasta.fasta", "fasta"):
    lista.append(len(record))
  for record in SeqIO.parse("fasta.fasta", "fasta"):
    if len(record) == lista_sort[-1]:
      print(record.id)
  • I forgot to say. The variable is, yes, globally defined. But the first "for" does not make a "break" when it finishes running and does not let the other "for" start. Anyway, the result does not come out.

  • Matheus, you got it rolling?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.