Hi

Extracting only first sequence from the multiline fasta file

    perl -02 -ne '/(>[^>]+)/ && print $1' OG0012884.fa

    >TRINITY_DN9_c0_g1_i1.p1
    MPMKGRFPIRRTLEFLRSGTVVFKDSVKILTVNYNTHGERSDGARKFVFFNIPQIQYQNP
    WIQILMFKNMTPSPFLRFYLDDGEQVLVDVEGKNHKQIVEHVKTILGKNDVLLEADKQVQ
    KEHSHPAHFGPKTYCLRECMCEVGGQVPCPGVVPLPKEMTGKYWTALRAGSAI*

How do I extract only the 2nd and 9th sequence, ?

Using Bash able to print only 1st ID in a fasta file ?

    head -1 OG0012884.fa
    >TRINITY_DN9_c0_g1_i1.p1

How do i extract only the 2nd ID  ?


Help is need, suggestions please.




Here's an awk based solution which counts the headers as they come and prints the 2nd and 9th along with their sequence on the respective next lines.

    awk '{if ($1 ~ "^>") count+=1} { if ( count == 2 || count == 9 ) { print $0; t = NR+1};} { if ( NR == t ) { print $0 }}'  OG0012884.fa

If you happen to be working with a file that contains the sequence on the same line:

    awk '{if ($1 ~ "^>") count+=1} { if ( count == 2 || count == 9 ) print $0}' OG0012884.fa


Heres the pseudocode, if you care to follow along.

    for line in file:
        if line starts with ">":
            count += 1
        if count == 2 or count == 3:
            print line
            t = current line number + 1
        if current line number = t:
            print line