This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Appending strings to fasta header from another file

Hello there,

I have a A.fasta file containing:

>AAL09996.1
ATGTCTAGCAAGCCGTCGAATATGCTTGATGAAGTTACCCTGTATACTCACTACGGCCTATCCGTGGCGAAGAAGCTCGGTGCGAATATGGTCGATGCTTTCCGGTCGGCCTTTTCGGTCAATGATGACATTCGTCAGGTGTATTACCGCGACAAGGGCATCTCTCACGCCAAAGCCGGTCGTTATTCCGAGGCAGTGGTGATGCTCGAGCAGGTTTACGATGCCGATGCCTTCGACGTGGAAGTGGCGCTGCATTTGGGAATCGCCTACGTTAAGACCGGGGCCGTTGATCGCGGCACCGAGTTGCTTGAGCGCTCCATCGCCGATGCGCCTGACAACATCAAGGTCGCGACCGTCCTTGGCCTGACCTATGTGCAGGTGCAAAAATATGATTTGGCCGTGCCTCTACTGGTCAAGGTGGCGGAAGCCAATCCGGTGAATTTCAATGTCCGCTTCCGTCTCGGGGTGGCGCTGGATAATCTGGGCCGCTTTGACGAAGCCATCGACAGTTTCAAGATCGCTTTGGGGCTGCGTCCCAATGAAGGCAAGGTGCATCGCGCAATCGCGTACAGCTACGAGCAGATGGGCTCGCACGAAGAGGCTTTGCCGCATTTTAAGAAGGCCAATGAACTCGATGAACGTTCGGCCGTCTAA
>AAR90856.1
ATGTCTAGCAAGCCGTCGGATATTCTTGACGAGGTCACTCTTTACGCTCACTACGGCCTTTCGGTGGCGAAGAAGCTCGGAATGAACATGGTCGATGCGTTCCGTGCGGCCTTTTCCGTCAACGACGACATCCGCCAGGTGTATTACCGCGACAAGGGCATCTCCCACGCCAAGGCCGGGCGCTATTCCCAGGCCGTCATGCTGCTGGAGCAGGTCTACGACGCCGATGCCTTCGATGTGGATGTGGCTCTGCACCTGGGAATCGCCTATGTGAAGACCGGCGCCGTCGATCGCGGCACCGAACTACTCGAACGTTCCTTGGCCGATGCGCCCGACAACGTGAAGGTGGCGACCGTTCTCGGCCTGACCTATGTGCAGGTGCAAAAGTACGATCTGGCCGTTCCGCTGCTGATCAAGGTGGCCGAGGCCAATCCCATCAATTTCAACGTCCGGTTCCGTCTGGGCGTGGCGCTGGACAACCTCGGCCGTTTCGACGAAGCCATCGACAGCTTCAAGATCGCGCTGGGCCTGCGTCCCAATGAAGGCAAGGTGCATCGCGCCATCGCCTTCAGCTATGAGCAGATGGGCCGGCACGAGGAAGCCTTGCCGCATTTCAAGAAGGCCAATGAACTTGACGAAGGGGCCTCGGTCTGA
...

I also have another file, A.taxonomy:

AAL09996.1  Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum gryphiswaldense
AAR90856.1  Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum magnetotacticum

etc etc

How do I modify the fasta headers so that it appears like this:

>AAL09996.1 Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum gryphiswaldense
ATGTCTAGCAAGCCGTCGAATATGCTTGATGAAGTTACCCTGTATACTCACTACGGCCTATCCGTGGCGAAGAAGCTCGGTGCGAATATGGTCGATGCTTTCCGGTCGGCCTTTTCGGTCAATGATGACATTCGTCAGGTGTATTACCGCGACAAGGGCATCTCTCACGCCAAAGCCGGTCGTTATTCCGAGGCAGTGGTGATGCTCGAGCAGGTTTACGATGCCGATGCCTTCGACGTGGAAGTGGCGCTGCATTTGGGAATCGCCTACGTTAAGACCGGGGCCGTTGATCGCGGCACCGAGTTGCTTGAGCGCTCCATCGCCGATGCGCCTGACAACATCAAGGTCGCGACCGTCCTTGGCCTGACCTATGTGCAGGTGCAAAAATATGATTTGGCCGTGCCTCTACTGGTCAAGGTGGCGGAAGCCAATCCGGTGAATTTCAATGTCCGCTTCCGTCTCGGGGTGGCGCTGGATAATCTGGGCCGCTTTGACGAAGCCATCGACAGTTTCAAGATCGCTTTGGGGCTGCGTCCCAATGAAGGCAAGGTGCATCGCGCAATCGCGTACAGCTACGAGCAGATGGGCTCGCACGAAGAGGCTTTGCCGCATTTTAAGAAGGCCAATGAACTCGATGAACGTTCGGCCGTCTAA
>AAR90856.1 Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum magnetotacticum
ATGTCTAGCAAGCCGTCGGATATTCTTGACGAGGTCACTCTTTACGCTCACTACGGCCTTTCGGTGGCGAAGAAGCTCGGAATGAACATGGTCGATGCGTTCCGTGCGGCCTTTTCCGTCAACGACGACATCCGCCAGGTGTATTACCGCGACAAGGGCATCTCCCACGCCAAGGCCGGGCGCTATTCCCAGGCCGTCATGCTGCTGGAGCAGGTCTACGACGCCGATGCCTTCGATGTGGATGTGGCTCTGCACCTGGGAATCGCCTATGTGAAGACCGGCGCCGTCGATCGCGGCACCGAACTACTCGAACGTTCCTTGGCCGATGCGCCCGACAACGTGAAGGTGGCGACCGTTCTCGGCCTGACCTATGTGCAGGTGCAAAAGTACGATCTGGCCGTTCCGCTGCTGATCAAGGTGGCCGAGGCCAATCCCATCAATTTCAACGTCCGGTTCCGTCTGGGCGTGGCGCTGGACAACCTCGGCCGTTTCGACGAAGCCATCGACAGCTTCAAGATCGCGCTGGGCCTGCGTCCCAATGAAGGCAAGGTGCATCGCGCCATCGCCTTCAGCTATGAGCAGATGGGCCGGCACGAGGAAGCCTTGCCGCATTTCAAGAAGGCCAATGAACTTGACGAAGGGGCCTCGGTCTGA

The headers will be joined together by a tab.

Thank you and will be appreciate any help.

fasta

What have you tried?

Searching on this site before posting helps.

I would not put a tab in the header of a fasta file, and I personally use python and a dictionary for this.

1 answer

Not the most elegant one liner in the world, but it works. Note, having tabs and whitespace in the header makes life much harder, and makes this solution quite fragile if your taxonomy strings deviate in format much.

join -j 1 <(sort A.taxonomy|sed 's/^/>/gi') <(paste - - < A.fasta | sort) | awk '{print $1 "\t" $2 " " $3 "\n" $4}'

Explained:

join -j 1 <(fileA) <(fileB)

Join fileA based on the first (and only) field, to fileB based on the first field (the >header string). Both files are provided as process substitions <(...) to allow these manipulations:

fileA:

The taxonomy file,

AAL09996.1  Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum gryphiswaldense

gets sorted, sort A.taxonomy and then passed to sed which prepends a > so that the strings match for join to use: |sed 's/^/>/gi'. This is all wrapped in a process substitution.

Resulting in:

>AAL09996.1  Bacteria;Proteobacteria;Alphaproteobacteria;Rhodospirillales;Rhodospirillaceae;Magnetospirillum;Magnetospirillum gryphiswaldense

The files themselves should already be sorted, but join often complains if they aren't so its safest to sort them anyway.

fileB:

The fasta file. The sequences are linearised which is handy, but they need to be 'tabulated'.

paste - - < A.fasta | sort

A.fasta is read into paste - - which places the sequences and headers on one line, and they are then sorted for good measure, yielding:

>AAL09996.1 ATGTCTAGCAAGCCGTCGA...
>AAR90856.1 ATGTCTAGCAAGCCGTCGG... #(shortened for the post)

Finally, awk reshuffles all the columns to put the tabs and newlines in to get the desired fasta format:

| awk '{print $1 "\t" $2 " " $3 "\n" $4}'

In this case, $1 is the >header, $2 is the taxonomy, $3 is the species name (annoying white space) and $4 is the sequence.

Thank you very much for your help. :)

Log in to answer this question.