<p><code>any()</code> loops over the iterator until the first truthy item is found or the end is reached. The iterator is not reset to the start after this. Using a list will change that behaviour since a list is not exhausted like a iterator. So, remove your <code>any()</code> call and you will see a sequence record printed. Your <code>'fake_fasta_file.fa'</code> is actually recognised by Biopython as valid fasta:</p>
<pre><code>&gt;&gt;&gt; SeqIO.read('fake_fasta_file.fa', 'fasta')
SeqRecord(seq=Seq(''), id='ftfyfiuuogpigèi', name='ftfyfiuuogpigèi', description='  ftfyfiuuogpigèi', dbxrefs=[])
</code></pre>
<p>Biopython ignores lines before the first line beginning with <code>'&gt;'</code> (as possible comment lines) and then starts collecting records, in this case an empty sequence record is still a record. This behaviour is deliberate from Biopython, although you could argue if it's best or not, it's unlikely to change for historical reasons.</p>
<p>What is <em>real</em> fasta? (philosophical pause) If you want to validate a particular definition of FASTA format then you can write your own parser depending on the exact specification and rules you want.</p>
<p>Edit, e.g.</p>
<pre><code>from Bio import SeqIO

records = SeqIO.parse(&quot;myfile.fa&quot;, &quot;fasta&quot;)
first = next(records, None)
if first is None:
    print(&quot;No fasta records&quot;)
elif first.seq == &quot;&quot;:
    print(&quot;Empty sequence&quot;)
elif all(c not in &quot;ATCGN&quot; for c in first.seq.upper()):
    print(&quot;Non ATCGN characters&quot;)
</code></pre>
<p>etc.</p>

CC BY-SA 4.0
2020-12-13T17:29:34.570Z
thank You very much indeed for the very informative and detailed answer. Given I am really a newbie is there any chance you could point me to pre-made fasta validator I could borrow for my script ? (i was thinking about valid if >header than sequence on one or multiple lines and a way to check the characters used in the sequences)
pippo1980
CC BY-SA 4.0
2020-12-13T17:58:52.953Z
@pippo1980 see edit (untested)
Chris_Rands
CC BY-SA 4.0
2020-12-13T18:20:10.577Z
cheers !!!!!!!!!
pippo1980
CC BY-SA 4.0
2020-12-13T15:46:25.593Z
2020-12-13T17:58:42.347Z
2020-12-13T17:58:42.347Z
Chris_Rands
<p>Hi I am trying to learn Python3 and Biopython,</p>
<p>I am trying to check imported fasta file before processing.
So far using:</p>
<pre><code>sequences = SeqIO.parse(&quot;fake_fasta_file&quot;, &quot;fasta&quot;)

if not any(sequa):
        print('\n  SOMETHING IS WRONG WITH YOUR INPUT FILE \n')
        exit()
</code></pre>
<p>works but as soon as I enter a '&gt;' in my fake_fasta_file , as an example</p>
<pre><code>nihvivicòvo-l
,mnjobùJN
, Békibpç
, mnkn

nbigp
ù
mlkn

mlnù
&gt;  ftfyfiuuogpigèi

</code></pre>
<p>any(sequences) returns True even if:</p>
<pre><code>for seq_record in sequences:
       print(seq_record)

</code></pre>
<p>does not print anything</p>
<p>Any better way to check for real fasta file in Biopython ?</p>
<p>Or maybe it my fault using any() on the iterator instead of creating</p>
<p>a list of seq_record and checking on that ?</p>

<fasta><python><seqio>
Biopython SeqIO check input file
# Biopython SeqIO check input file

(Asked by: username_0 on 2020-12-13)

Hi I am trying to learn Python3 and Biopython,
I am trying to check imported fasta file before processing.
So far using:
sequences = SeqIO.parse("fake_fasta_file", "fasta")

if not any(sequa):
        print('\n  SOMETHING IS WRONG WITH YOUR INPUT FILE \n')
        exit()

works but as soon as I enter a '>' in my fake_fasta_file , as an example
nihvivicòvo-l
,mnjobùJN
, Békibpç
, mnkn

nbigp
ù
mlkn

mlnù
>  ftfyfiuuogpigèi


any(sequences) returns True even if:
for seq_record in sequences:
       print(seq_record)


does not print anything
Any better way to check for real fasta file in Biopython ?
Or maybe it my fault using any() on the iterator instead of creating
a list of seq_record and checking on that ?

**Tags:** fasta, python, seqio

---

## 1 answers

### Answer by username_1 on 2020-12-13. Score: 4 | Accepted answer

any() loops over the iterator until the first truthy item is found or the end is reached. The iterator is not reset to the start after this. Using a list will change that behaviour since a list is not exhausted like a iterator. So, remove your any() call and you will see a sequence record printed. Your 'fake_fasta_file.fa' is actually recognised by Biopython as valid fasta:
>>> SeqIO.read('fake_fasta_file.fa', 'fasta')
SeqRecord(seq=Seq(''), id='ftfyfiuuogpigèi', name='ftfyfiuuogpigèi', description='  ftfyfiuuogpigèi', dbxrefs=[])

Biopython ignores lines before the first line beginning with '>' (as possible comment lines) and then starts collecting records, in this case an empty sequence record is still a record. This behaviour is deliberate from Biopython, although you could argue if it's best or not, it's unlikely to change for historical reasons.
What is real fasta? (philosophical pause) If you want to validate a particular definition of FASTA format then you can write your own parser depending on the exact specification and rules you want.
Edit, e.g.
from Bio import SeqIO

records = SeqIO.parse("myfile.fa", "fasta")
first = next(records, None)
if first is None:
    print("No fasta records")
elif first.seq == "":
    print("Empty sequence")
elif all(c not in "ATCGN" for c in first.seq.upper()):
    print("Non ATCGN characters")

etc.

> **Comments:**
> **username_0**: thank You very much indeed for the very informative and detailed answer. Given I am really a newbie is there any chance you could point me to pre-made fasta validator I could borrow for my script ? (i was thinking about valid if >header than sequence on one or multiple lines and a way to check the characters used in the sequences)  
> **username_1**: @pippo1980 see edit (untested)  
> **username_0**: cheers !!!!!!!!!  

---

