0

Input file: 131751_pphA.fasta

>ID:NDNDCOEC_02118 |[Genus species]|strain|PANS_1_2_annot.gbk|pphA|855|NODE_3_length_422941_cov_112.146787422941(422941):170566-171420:1 ^^ Genus species strain strain.|neighbours:ID:NDNDCOEC_02117(1),ID:NDNDCOEC_02119(1)|neighbour_genes:hypothetical protein,ntaA| | aligned:1-284 (284)
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP
>ID:KJDCINFB_03194 |[Genus species]|strain|PNA_1_5_annot.gbk|pphA|855|NODE_5_length_527105_cov_93.286545527105(527105):274765-275619:1 ^^ Genus species strain strain.|neighbours:ID:KJDCINFB_03193(1),ID:KJDCINFB_03195(1)|neighbour_genes:hypothetical protein,ntaA| | aligned:1-284 (284)
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP
>ID:LBFHNJKP_02554 |[Genus species]|strain|PANS_1_6_annot.gbk|pphA|855|NODE_4_length_527158_cov_95.108790527158(527158):251540-252394:-1 ^^ Genus species strain strain.|neighbours:ID:LBFHNJKP_02553(-1),ID:LBFHNJKP_02555(-1)|neighbour_genes:ntaA,hypothetical protein| | aligned:1-284 (284)
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP
>ID:GPMHBDBL_03046 |[Genus species]|strain|PNA_200_2_annot.gbk|pphA_2|855|NODE_4_length_530984_cov_86.347264530984(530984):275036-275890:1 ^^ Genus species strain strain.|neighbours:ID:GPMHBDBL_03045(1),ID:GPMHBDBL_03047(1)|neighbour_genes:hypothetical protein,ntaA| | aligned:1-284 (284)
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP

Desired output: four separate output files:

PANS_1_2_pphA.fasta

>PANS_1_2_pphA
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP

PNA_1_5_pphA.fasta

>PNA_1_5_pphA
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP

PANS_1_6_pphA.fasta

>PANS_1_6_pphA
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP

PNA_200_2_pphA_2.fasta

>PNA_200_2_pphA_2
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP

The multifasta input file (131751_pphA.fasta) contains four fasta sequences with headers. I want four output files that are individual fasta sequences with their names and headers named as per the strain as mentioned above. For example, one of headers in the input fasta contains the strain information as |strain|PANS_1_2_annot.gbk|pphA|. The output file should have its name as PANS_1_2_pphA.fasta and its header as >PANS_1_2_pphA.

Similarly the other output files as
PNA_1_5_pphA.fasta with header >PNA_1_5_pphA
PANS_1_6_pphA.fasta with header >PANS_1_6_pphA
PNA_200_2_pphA_2.fasta with header >PNA_200_2_pphA_2

Tried the following code:

awk -F "|" '/^>/ {close(F); ID=$1; gsub("^>", "", ID); F=ID".fasta"} {print >> F}' 123764_pphA.fasta 

Resulting in fasta output files with following names:

ID:BKKCPFME_02840 .fasta  ID:EKPOMJAO_03222 .fasta  ID:HEIIBHGJ_01315 .fasta  ID:KBMOKBJB_03162 .fasta  ID:LECGKDGM_03166 .fasta
1
  • please note that only one answer you can chose as accepted answer not multiple answers, so when you chose an answer as accepted you confirm that your question is resolved by that answer you marked as accepted. I'm writing this because you changed the accepted answer almost on your all questions recently. Feb 16 '21 at 21:37
4
awk -F'|' '
    NR%2{ close(fileName); hdr=$4 $5; sub("annot.gbk", "", hdr); fileName=hdr".fasta";
          print ">"hdr >fileName; next; };
        { print >fileName; }' infile
0
1

A non awk solution, assuming, given the example, that the file must be splited every 2 lines:


# split the file every 2 lines and save in files prefixed FOO.
split -l2 131751_pphA.fasta FOO.

# loop over the files
for f in FOO.*; do 
  # `awk` and `sed` to get the pattern to use as file name and first line, i.e "PANS_1_2_pphA"
  n=$(awk -F'|' '{ print $4$5 }' "$f" | \
    sed 's/annot\.gbk//')

  # copy the 2nd line of the file into a new file named as pattern+.fasta
  sed -n 2p "$f" > "$n.fasta"

  # add the pattern in the created file
  sed -i "1i>\\$n" "$n.fasta"

  # remove the splited files
  rm "$f"
done

$ cat PANS_1_2_pphA.fasta 
PANS_1_2_pphA
MIKKLIAEKGTLIFIEAHNPLSALIASKAEQTNSEGRIVKFDGIWSSSLTDSASRGIPDNETLALSSRLENIADIRNVTDMPIIMDADTGGKPEHFSYYVKRMINNGVNGVIIEDKTGLKKNSLFGTEVEQTLADINDFSEKIKRGKSAVYIDDFMIIARLESLIAGFDVEHALERADAYVEAGADGIMIHSCKKTPDEVFLFSTKFRKKYPSVPLICVPTTYSATSNRELSEAGFNVIIYANHMLRAAYKAMENVSKEILRYGRTAEIEKSCMSVKEIISLIP


0
0
awk -F '|' '
    /^>/ {
        close(out)
        head = $4
        sub("_[^_]*$","_" $5, head)
        $0 = ">" head
        out = head ".fasta"
    } { print >out }' 131751_pphA.fasta

This is similar to αғsнιη's answer but allows each sequence to be more than one line (which is possible in a generic FastA file).

I'm also trimming the header name a bit differently, trimming off anything after the last _ in .gbk filename (?) that is found in the fourth |-delimited field on the FastA header line, and adding the fifth field.

-6
count=`awk 'END{print NR}' filename`

for ((i=1;i<=$count;i++)); do j=$(($i+1)); m=`awk -F "|" -v i="$i" 'NR==i{k=substr($4,1,8);print k"_"$5}' filename`; echo $m >"$m.fasta";echo -e "\n" >> "$m.fasta" ;awk -v j="$j" 'NR==j {print $0}' filename >> "$m.fasta";i=$j; done

Tested and worked fine

3
  • As far as I can tell, what with this being all on a single long line, and nothing to explain what it does, this seems to run two copies of awk for each input line? And all they do is to find and process a single line of the input. So for each line to process, all previous lines are also read, turning this into O(n^2). That, plus launching all the child processes makes it really slow, so slow that even with the eight-line input in the question, it takes over half a second on my machine, a noticeable time.
    – ilkkachu
    Apr 1 '21 at 13:51
  • You are parsing the whole file twice for each sequence in the file. Assuming this is executed on a file that contains a line for each gene in a species, that means reading the file about 800000000 times.
    – Kusalananda
    Apr 1 '21 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.