<p>You're assuming 2-line FASTA format, but the sequence can (and typically does) span multiple lines. You can use <a href="https://biopython.org/" rel="noreferrer">Biopython</a> to build a simple parser:</p>

<pre><code>from Bio import SeqIO

for record in SeqIO.parse("file_name.fa", "fasta"):
    if "16S" in record.description:
        print(record.format("fasta"))
</code></pre>

CC BY-SA 4.0
2019-10-16T09:18:14.497Z
This was really helpful, thank you so much
Sofia
CC BY-SA 4.0
2019-10-15T19:42:28.960Z
2019-10-15T19:42:28.960Z
Chris_Rands
<p>I have this code to filter some IDs from a fasta file:</p>

<pre><code>for line in file_open:
    line=line.rstrip()
    if '16S' in line:
        header=line
        header=header[0] + header[1:]
        seq=file_open.readline()
        seq=seq.strip()
        print(header + '\n'  + seq)
</code></pre>

<p>however, this code doesn't get me all content of each ID sequence. I get this result:</p>

<pre><code>&gt;ID1
AGTTTTGCCGAATTCCTTTAGGATTTTGTGAGGAACATTGTGTTAAATGTATTTATAGTTCTACTAATTC
</code></pre>

<p>When I should get this:</p>

<pre><code>&gt;ID1
AGTTTTGCCGAATTCCTTTAGGATTTTGTGAGGAACATTGTGTTAAATGTATTTATAGTTCTACTAATTC
TGCGAAGGTAGCATAATCATTAGCCTTTTGATTTGAGGCTGGAATGAATGGTTTGACGAGAGATGGTCTG
GGGCCGTGGTATACTGACCATGCGAAGGTAGCATAATCATTAGCCTCTTGATTTGAGGCTGGAATGAATG
GGGCCGTGGTATAC
</code></pre>

<p>Thanks</p>

CC BY-SA 4.0
2020-09-24T15:27:20.963Z
Does this answer your question? [Remove/delete sequences by ID from multifasta](https://bioinformatics.stackexchange.com/a/14421/29)
Konrad Rudolph
CC BY-SA 4.0
2020-09-26T19:25:11.910Z
Well, the code you wrote prints the seq on a single line (and without any newline afterwards), are you sure that's not the only problem?
Kamil S Jaron
CC BY-SA 4.0
2020-09-26T19:25:41.540Z
@KonradRudolph I think the question is more about writing the fasta file well formated.
Kamil S Jaron
CC BY-SA 4.0
2020-09-28T08:07:41.893Z
@KamilSJaron No, the question isn’t about formatting. Check the output in the question again. It isn’t all on one line, it’s *truncated* (as in, only the first line is read and written, subsequent lines are omitted).
Konrad Rudolph
CC BY-SA 4.0
2020-09-28T09:06:23.743Z
I thought that the truncated line could be due to copypasting. Now I see, it's not about the formatting of output, but expected formatting of the input.
Kamil S Jaron
<python><fasta>
Filtering fasta files by ID
# Filtering fasta files by ID

(Asked by: username_0 on 2019-10-15)

I have this code to filter some IDs from a fasta file:
for line in file_open:
    line=line.rstrip()
    if '16S' in line:
        header=line
        header=header[0] + header[1:]
        seq=file_open.readline()
        seq=seq.strip()
        print(header + '\n'  + seq)

however, this code doesn't get me all content of each ID sequence. I get this result:
>ID1
AGTTTTGCCGAATTCCTTTAGGATTTTGTGAGGAACATTGTGTTAAATGTATTTATAGTTCTACTAATTC

When I should get this:
>ID1
AGTTTTGCCGAATTCCTTTAGGATTTTGTGAGGAACATTGTGTTAAATGTATTTATAGTTCTACTAATTC
TGCGAAGGTAGCATAATCATTAGCCTTTTGATTTGAGGCTGGAATGAATGGTTTGACGAGAGATGGTCTG
GGGCCGTGGTATACTGACCATGCGAAGGTAGCATAATCATTAGCCTCTTGATTTGAGGCTGGAATGAATG
GGGCCGTGGTATAC

Thanks

**Tags:** python, fasta
> **Comments:**
> **username_1**: Does this answer your question? [Remove/delete sequences by ID from multifasta](https://bioinformatics.stackexchange.com/a/14421/29)  
> **username_2**: Well, the code you wrote prints the seq on a single line (and without any newline afterwards), are you sure that's not the only problem?  
> **username_2**: @KonradRudolph I think the question is more about writing the fasta file well formated.  
> **username_1**: @KamilSJaron No, the question isn’t about formatting. Check the output in the question again. It isn’t all on one line, it’s *truncated* (as in, only the first line is read and written, subsequent lines are omitted).  
> **username_2**: I thought that the truncated line could be due to copypasting. Now I see, it's not about the formatting of output, but expected formatting of the input.  

---

## 1 answers

### Answer by username_3 on 2019-10-15. Score: 8 | Accepted answer

You're assuming 2-line FASTA format, but the sequence can (and typically does) span multiple lines. You can use Biopython to build a simple parser:
from Bio import SeqIO

for record in SeqIO.parse("file_name.fa", "fasta"):
    if "16S" in record.description:
        print(record.format("fasta"))

> **Comments:**
> **username_0**: This was really helpful, thank you so much  

---

