Extract sequences from partial Header 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?
&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

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 &gt; DNA_Sequence.fa
You could use Biopython. 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(&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;)

This code needs to be optimised if you are dealing with large sequences like human genome fasta as it holds everything in memory.