This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Regular expression problem while trying to interpolate variable as pattern in Python

I'm trying to learn Python to perform some tasks in the lab.

I was trying to find a pattern in a FASTA file, interpolating pattern and fasta as string variables. This way the program doesn't find anything. On the other hand, when I try to do the same, but writing the pattern string instead of using the variable interpolation, it works and finds the pattern. Could you please help me to figure out what the problem is?

Here is my code:

   firstname = "header1"
   for record in SeqIO.parse("prueba_fasta.fasta", "fasta"):
        print ">" + record.id + "\n" + record.seq
        fa = strrecord.id)
        print fa
        fir = str(firstname)
        print fir
        matches = re.search (fir, fa)
        if matches:
            print ">" + record.id + "\n" + record.seq

Thanks in advance

Uxue

python biopython regex

Unrelated advice, you should/could use print(seq_record.format("fasta").strip()) to properly write (to stdout) instead of using the awkward print ">" + record.id + "\n" + record.seq

In addition, you miss something here: fa = strrecord.id), I guess you mean: fa = str( record.id)

I'm sorry for the mistake, this is the right version of the code:

 firstname = "header1"
       for record in SeqIO.parse("prueba_fasta.fasta", "fasta"):
            print ">" + record.id + "\n" + record.seq
            fa = strrecord.id)
            print fa
            fir = str(firstname)
            print fir
            matches = re.search (fir, fa)
            if matches:
                print(record.format("fasta").strip())

Thank you!

Oh right, there is some weird auto formatting going on with str( record.id).

Which pattern exactly are you trying to match? So if something occurs in the fasta identifier you want to keep it?

2 answers

try to use matches.group()

re.search return object so if you use the method group() it will return string if it found matches, other wise it returns None

Thank you very much for your inside! I will try to follow your recomendations in order to fix this problem.

You just want to print any fasta entry where the header contains the text "header1"?

you can just:

for record in SeqIO.parse("prueba_fasta.fasta", "fasta"):
    if strrecord.id).find('header1') != -1:
        print ">" + record.id + "\n" + record.seq

Thank you for you answer. I have already fix the problem.

Log in to answer this question.