Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-16T09:18:14.497Z
Answers.Text: This was really helpful, thank you so much
Answers.UserDisplayName: Sofia
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-15T19:42:28.960Z
Answers.LastActivityDate: 2019-10-15T19:42:28.960Z
Answers.UserDisplayName: Chris_Rands
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-24T15:27:20.963Z
Comments.Text: Does this answer your question? [Remove/delete sequences by ID from multifasta](https://bioinformatics.stackexchange.com/a/14421/29)
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-26T19:25:11.910Z
Comments.Text: 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?
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-26T19:25:41.540Z
Comments.Text: @KonradRudolph I think the question is more about writing the fasta file well formated.
Comments.UserDisplayName: Kamil S Jaron
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-28T08:07:41.893Z
Comments.Text: @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).
Comments.UserDisplayName: Konrad Rudolph
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-28T09:06:23.743Z
Comments.Text: 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.
Comments.UserDisplayName: Kamil S Jaron
Tags: <python><fasta>
Title: Filtering fasta files by ID
ThreadText: # 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**: @username_1 I think the question is more about writing the fasta file well formated.  
  > **username_1**: @username_2 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](https://biopython.org/) 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  
  
  ---
  
  