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, 2021 at 21:37

3 Answers 3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .