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.
• 3,275 views
•
link
3 answers
awk '{if(/>.*/) {print} else {print substr($0, 1, 30)} }' test.fa
test.fa
>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQTATVIDWDQIREASQTQRRQAAAIANAPVKQGVVHEPIDAGVMAGNVPAEQRNAASIVQSIDGSKLSQISDRLPKFIKQGSDEVVYGKHVVVSKLGPEVIGLILDLIKAQPANRALLLAKLQAISNDGNPEASNFMGFVFEYGLFGAVKN
output
>a47619p2-
MVKIALFGRNITLPILIFIGFVFLHDASAQ
• 0 views
•
link
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).
• 0 views
•
link
Log in to answer this question.
You could convert to tabular format with
seqkitand use the substring function fromawk: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:20is enough.