Answers.Body: <p>You could use <a href="http://biopython.org/DIST/docs/tutorial/Tutorial.html" rel="nofollow noreferrer">Biopython</a>. If its a fasta file, it will be complicated to write fasta output (especially multiline fasta) with <code>grep</code> or <code>awk</code>. Simple solution is to use biopython, so that you can even match for any complex patterns in the fasta header.</p>
  <pre><code>from Bio import SeqIO
  
  rna_records  = []
  dna_records = []
  
  for seq in SeqIO.parse(&quot;in.fa&quot;,&quot;fasta&quot;):
  
      if &quot;RNA&quot; in seq.id:
          rna_records.append(seq)
  
      elif &quot;DNA&quot; in seq.id:
          dna_records.append(seq)
  
  SeqIO.write(rna_records, &quot;RNA_out.fa&quot;,&quot;fasta&quot;)
  SeqIO.write(dna_records, &quot;DNA_out.fa&quot;,&quot;fasta&quot;)
  </code></pre>
  <p><strong>This code needs to be optimised if you are dealing with large sequences like human genome fasta as it holds everything in memory.</strong></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-22T13:09:56.003Z
Answers.Text: Dear Thank for this solution, Can i manipulate of the RNA and DNA session in case I'm looking for other words excess in the ID header?
Answers.UserDisplayName: M.Bioinfo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-22T13:10:48.553Z
Answers.Text: I have to only add my input here ("in.fa","fasta") ?
Answers.UserDisplayName: M.Bioinfo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-22T12:50:57.387Z
Answers.LastActivityDate: 2020-07-22T12:50:57.387Z
Answers.UserDisplayName: geek_y
Answers.Body: <p>Here is a solution with <code>awk</code>:</p>
  <p>This is the example fasta file which I used:</p>
  <pre><code>&gt;Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  &gt;Test 2 RNA 
  ACGGT
  GGTA
  CGGA
  &gt;DNA - TEST 3
  ACG
  GATA
  AGGT
  </code></pre>
  <p>You can create a file called <code>fasta_search.awk</code> and insert following code:</p>
  <pre><code>#!/usr/bin/awk -f
  BEGIN {
      hit=0
  }
  {
      if ($0 ~ /^&gt;/) 
      {   
          if ($0 ~ search)
          {
              hit=1;
              print $0; 
          }
          else 
          {
              hit=0; 
          }
       }   
      else if(hit==1)
      {   
          print $0
      }   
  }
  </code></pre>
  <p>Now you can run:
  <code>àwk -v search=&quot;DNA&quot; -f fasta_search.awk my_fastas.fa</code></p>
  <p><code>search</code> is therefore a awk variable which could be any String you are looking for.</p>
  <p>As result I get this:</p>
  <pre><code>&gt;Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  &gt;DNA - TEST 3
  ACG
  GATA
  AGGT
  </code></pre>
  <p>To get all your keywords, you could run:</p>
  <pre><code>for i in DNA RNA LTR; do awk -v search=&quot;$i&quot; -f fasta_search.awk my_fastas.fa &gt; lib_$i.fasta; done
  </code></pre>
  <p>This results in <code>head lib_*</code>:</p>
  <pre><code>==&gt; lib_DNA.fasta &lt;==
  &gt;Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  &gt;DNA - TEST 3
  ACG
  GATA
  AGGT
  
  ==&gt; lib_LTR.fasta &lt;==
  
  ==&gt; lib_RNA.fasta &lt;==
  &gt;Test 2 RNA
  ACGGT
  GGTA
  CGGA
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-22T14:10:13.270Z
Answers.LastActivityDate: 2020-07-22T14:10:13.270Z
Answers.UserDisplayName: Mr_Z
Body: <p>I have a library I would like to split them based on their headers. Some are related to RNA and DNA.</p>
  <p>The header contains much information, but the most important the presence of DNA or RNA/LTR.., these partial words could be in between or at the beginning.</p>
  <p><strong>The point is the knowledge of how to extract sequences from a partial header occur in between the ID</strong></p>
  <p>My question is how to use grep or awk to grep the header that has one of these words along with sequences?
  Note that The sequences are more than one line.</p>
  <p>Or perhaps grep the specific word and ignore what before and after?</p>
  <pre><code>&gt;Tigger16a#DNA/TcMar-Tigger DF0000028 TcMar-Tigger **DNA** transposon 
  &gt;rnd-4_family-38#SINE/MIR ( Recon Family Size = 20, Final Multiple Alignmen 
  &gt;rnd-6_family-31751#LTR/Gypsy ( Recon Family Size = 26, Final Multiple Alignment Size = 22 )
  &gt;RNA2558#LTR/ERVL 
  &gt;NonDNA1#LINE/I-Jockey 
  &gt;DNA5#DNA/TcMar-Tc1
  </code></pre>
  <p>I have tried to create a list of the Required IDs using grep creating the list and extracting the sequences but for some reasons, the output has more sequences than those specified in the DNAID.txt list.</p>
  <pre><code>grep -A1000 -w -f DNAID.txt.fa MyLibrary &gt; DNA_Sequence.fa
  </code></pre>
  
Tags: <awk><filtering><grep>
Title: Extract sequences from partial Header
ThreadText: # Extract sequences from partial Header
  
  (Asked by: username_0 on 2020-07-22)
  
  I have a library I would like to split them based on their headers. Some are related to RNA and DNA.
  
  The header contains much information, but the most important the presence of DNA or RNA/LTR.., these partial words could be in between or at the beginning.
  
  **The point is the knowledge of how to extract sequences from a partial header occur in between the ID**
  
  My question is how to use grep or awk to grep the header that has one of these words along with sequences?
  Note that The sequences are more than one line.
  
  Or perhaps grep the specific word and ignore what before and after?
  
  ```
  >Tigger16a#DNA/TcMar-Tigger DF0000028 TcMar-Tigger **DNA** transposon 
  >rnd-4_family-38#SINE/MIR ( Recon Family Size = 20, Final Multiple Alignmen 
  >rnd-6_family-31751#LTR/Gypsy ( Recon Family Size = 26, Final Multiple Alignment Size = 22 )
  >RNA2558#LTR/ERVL 
  >NonDNA1#LINE/I-Jockey 
  >DNA5#DNA/TcMar-Tc1
  
  ```
  
  I have tried to create a list of the Required IDs using grep creating the list and extracting the sequences but for some reasons, the output has more sequences than those specified in the DNAID.txt list.
  
  ```
  grep -A1000 -w -f DNAID.txt.fa MyLibrary > DNA_Sequence.fa
  
  ```
  
  **Tags:** awk, filtering, grep
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-07-22. Score: 2 | Accepted answer
  
  You could use [Biopython](http://biopython.org/DIST/docs/tutorial/Tutorial.html). If its a fasta file, it will be complicated to write fasta output (especially multiline fasta) with `grep` or `awk`. Simple solution is to use biopython, so that you can even match for any complex patterns in the fasta header.
  
  ```
  from Bio import SeqIO
  
  rna_records  = []
  dna_records = []
  
  for seq in SeqIO.parse("in.fa","fasta"):
  
      if "RNA" in seq.id:
          rna_records.append(seq)
  
      elif "DNA" in seq.id:
          dna_records.append(seq)
  
  SeqIO.write(rna_records, "RNA_out.fa","fasta")
  SeqIO.write(dna_records, "DNA_out.fa","fasta")
  
  ```
  
  **This code needs to be optimised if you are dealing with large sequences like human genome fasta as it holds everything in memory.**
  
  > **Comments:**
  > **username_0**: Dear Thank for this solution, Can i manipulate of the RNA and DNA session in case I'm looking for other words excess in the ID header?  
  > **username_0**: I have to only add my input here ("in.fa","fasta") ?  
  
  ---
  
  ### Answer by username_2 on 2020-07-22. Score: 1
  
  Here is a solution with `awk`:
  
  This is the example fasta file which I used:
  
  ```
  >Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  >Test 2 RNA 
  ACGGT
  GGTA
  CGGA
  >DNA - TEST 3
  ACG
  GATA
  AGGT
  
  ```
  
  You can create a file called `fasta_search.awk` and insert following code:
  
  ```
  #!/usr/bin/awk -f
  BEGIN {
      hit=0
  }
  {
      if ($0 ~ /^>/) 
      {   
          if ($0 ~ search)
          {
              hit=1;
              print $0; 
          }
          else 
          {
              hit=0; 
          }
       }   
      else if(hit==1)
      {   
          print $0
      }   
  }
  
  ```
  
  Now you can run:
  `àwk -v search="DNA" -f fasta_search.awk my_fastas.fa`
  
  `search` is therefore a awk variable which could be any String you are looking for.
  
  As result I get this:
  
  ```
  >Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  >DNA - TEST 3
  ACG
  GATA
  AGGT
  
  ```
  
  To get all your keywords, you could run:
  
  ```
  for i in DNA RNA LTR; do awk -v search="$i" -f fasta_search.awk my_fastas.fa > lib_$i.fasta; done
  
  ```
  
  This results in `head lib_*`:
  
  ```
  ==> lib_DNA.fasta <==
  >Test DNA 1
  ACGTAAGGATATAATAC
  ACGTA
  AGGATA
  >DNA - TEST 3
  ACG
  GATA
  AGGT
  
  ==> lib_LTR.fasta <==
  
  ==> lib_RNA.fasta <==
  >Test 2 RNA
  ACGGT
  GGTA
  CGGA
  
  ```
  
  
  ---
  
  