This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract same FASTA from a Multi FASTA

Hi. I'm an absolute beginner to programming and work with linux terminal with bioinformatic file

I have a multifasta (database) file (Trascritti.fa) with thousand of transcripts in fasta format.

Like this:

>VIT_201s0011g03530.1
AATTAAGCATAAATACTCACTCTTACCCCCTTATTTTCTTATCTCTCATCACTTTTGGTGCGAAGAATTG
GACCATGAGAACAAGCTGCAATGGGTGTAGGGTTCTTCGCAAGGCATGCAGCCAAGACTGCATCATCAAA
CCTTGCCTTGAGTGGATCAAAAATCCTGATTTCCAAGCCAATGCTACTCTCTTCCTTGCCAAATTCTATG
>VIT_201s0011g03540.1
CAGGTAGCGTGAAGTTAAACCCTAGCGCTTTAGACAAACAGCTGTAGTCACCGCCCACAAACACCCCCAC
AGCCTCTGAGACACCACCTCAAACCTTTCCACTTAAATACACATCCCTCACACCCTTTTCAATTCCGTAC
TATAAATCTCTCTGCAACAACGGCAGCAACGCCCTACAGCCATGAGGATGAGCTGCAATGGCTGTAGGGT
>VIT_201s0011g03550.1
CATGCAAAGCTGAACGCGATGCTGTGATTGGTGGTAAGTGGTAGTTGAGTAAATTTGACAGTGAAGGAAG
GCCGAAATGGTAAAAGACTAAGGCTAGAAGTAGAATACCACTGTTCTTCTCATCACGTGGGCCCATGAAA
TACTGCATGACCCATGAGGCTCCCTCTCCTGCTCACTCTCTCTATCATTCGCTCTCGCCCAAAATAGCCT
>VIT_201s0011g03560.1
TTCGCCTTCTCTTTCTCTCTGAAACCCTCTCTTTCTCTCTCTAGACCAAGAGATGGGAGAAGGAAAGGGT
TCCACTCTAGTCCATCTAGTTGTGGTGGTTCTGAGTCTCGTCGCCTTTGGCTTTGCCGTTGCTGCTGAGC
GCCGCAGAAGCGTCGGTACAATAGTTACAGATGATCGAAATGCTACCTACTGTGTTTACAACTCTGATGT
>VIT_201s0011g03570.1
ATGGTGAAAGTTCCCAAGTCGAGAGATGGAGAGAATCACGTTAAAGTGCACAAGTATGGGGTGGGGAAGA
CGAAGAAGAGAGTGAAGGAGGAGGTGGGGAAGGTGGAAGACGAGAAGAGGGATGACAGAGAATCCATTGC
>VIT_201s0011g03580.2
TGATGCGATATTTATCAGATTTTTATTTATTTTATTTTATTAATACTTTGTTAAGGAGTTGGTGCCAAAA
CCGATGAGACTTCTCGCGGGACGCACCGCCTGTGTGAGGGAGTAAAAAAAATAATTAAAAATAAATAAAG

And an other ID.txt file that have only a list of id like this

VIT_201s0011g03540
VIT_201s0011g03550
VIT_201s0011g03560

The two file are in the same directory

I need a simple way to extract the ID with sequence from the multifasta (database)

Your contribution will be appreciate with the best regards

fasta linux sequence biopython

I've a working machine with linux ubuntu 14

Until now I've find this line with awk for linux shell:

awk '/^>/ {P=index($0,"out")==0} {if(P) print} ' in.fasta > out.fasta

This remove all sequences with the header pattern out. and save only the other to a new file.

Is possible to adapt it to my case?

Also tried:

$ fastaindex Trascritti.fa seq.idx

And then:

$ fastafetch -f Trascritti.fa -i seq.idx -Fq <(sort -u ID.txt )

The shell give:

] (missing -F ?)*: Could not find identifier [
exiting ...

Also tried:

grep -Fwf ID.txt -A 1  Trascritti.fa | grep -v '^--$'  > out.fasta

But the out.fasta is blank

Tried

$ grep VIT_201s0026g00010 Trascritti.fa

it gives only the heder but not the sequence

>VIT_201s0026g00010.1

Is there a way to extract the heder with the sequence using this method?

It is good to try many things. But instead of putting so many comments, EDIT the question stating what you tried.

Hello ilbiotecnologo!

We believe that this post does not fit the main topic of this site.

This question has been answered dozens of times. Please search the site.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

5 answers

Converting the multi fasta file to oneline fasta format (1st line has header, 2nd line has sequence, 3 line has header, 4th line sequence...so on) is very helpful in manipulating the files. You can use fasta_formatter program from fastx-toolkit to do that.

Then run:

fasta_formatter -i in.fasta -o in_OneLine.fasta -w 0

Now, extract the sequences using simple fgrep from your terminal

fgrep -A1 -w -f <(cat ID.txt) in_OneLine.fasta > output.fasta

This was answered many a times here. Spending some time for searching similar threads would be helpful.This is one and there are many.

Before proceeding to the following link, do this to your fasta file. This modifies all the headers of your fasta file so that they match to the IDs of your another file.

perl -pe 's/\..//' fasta_file.fa > modified_fasta.fa

A: perl code to extract sequences from multi-line fasta works on all test files but

This solution is similar to Prakki Rama's but without the fastx dependency. The output sequences are wrapped in the same way as in input:

awk -v RS=">" 'NR>1 {gsub("\n", "\t"); print ">"$0}' seq.fa \
| fgrep -w -f <(cat ID.txt) \
| tr '\t' '\n' \
| grep -v '^$'

The fastest solution would be to use the BBMap package, like this:

filterbyname.sh in=Trascritti.fa out=filtered.fa names=ID.txt

Try seqtk

Command for above example would be:

seqtk subseq Trascritti.fa ID.txt > output.fa

Log in to answer this question.