This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract N amino acids from fasta file

Hi, I want to extract the first N aminoacids from sequences in a fasta file. I have this sequences,

>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQTATVIDWDQIREASQTQRRQAAAIANAPVK
QGVVHEPIDAGVMAGNVPAEQRNAASIVQSIDGSKLSQISDRLPKFIKQGSDEVVYGKHV
VVSKLGPEVIGLILDLIKAQPANRALLLAKLQAISNDGNPEASNFMGFVFEYGLFGAVKN

for example, I want this sequence with only 30 aa, like:

>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQ

Is there a program that can do this to all sequences in linux terminal? I hope you can help me. Thank you.

fasta sequence

You could convert to tabular format with seqkit and use the substring function from awk:

seqkit fx2tab file.fasta  | awk -v FS="\t" '{print ">"$1"\n"substr($2,1,30)}'

Be careful! This approach makes a lot of assumptions about the structure of the FASTA file.

Yes, it does. Sorry, I thought the input file was tabular format. I updated the comment.

seqkit subseq -r 1:20 is enough.

3 answers

Using seqkit:

$ seqkit subseq -r 1:30 input.fasta
awk '{if(/>.*/) {print} else {print substr($0, 1, 30)} }' test.fa

test.fa

>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQTATVIDWDQIREASQTQRRQAAAIANAPVKQGVVHEPIDAGVMAGNVPAEQRNAASIVQSIDGSKLSQISDRLPKFIKQGSDEVVYGKHVVVSKLGPEVIGLILDLIKAQPANRALLLAKLQAISNDGNPEASNFMGFVFEYGLFGAVKN

output

>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQ

With biopython:

#Usage: python3 scriptname.py file.fasta
import sys
from Bio import SeqIO

for i in SeqIO.parse(sys.argv[1], "fasta"):
    print(f">{i.description}\n{i.seq[0:30]}")

Or as a one-liner:

$ python3 -c 'import sys; from Bio import SeqIO; [print(f">{i.description}\n{i.seq[0:30]}") for i in SeqIO.parse(sys.argv[1], "fasta")];' file.fasta

Replace [0:30] with whatever range you like (it doesn't have to start at zero either).

Log in to answer this question.