This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Selecting the last 100 nt from sequences

Hi,

I have fasta file containing loci of like 500 introns. I don't know how to have just the last 100 bases using awk command lines.

Thanks, Farid

rna-seq

Thanks RamRS. I think this command works when I know the length of the introns. But I have different lengths and I want the last 100 bases from each sequence.

You could use length($seq) in biooawk to calculate length on the fly. I don't see why you need to know length before you start the entire operation. It just needs to be calculated before the substr step.

Thanks everyone. The sed -Ee 's/^.*(.{100})$/\1/' file.fasta worked great. Appreciate it.

Please accept answers that worked for you. You can accept more than one answer if they all work.

Upvote|Bookmark|Accept

2 answers

with seqkit:

$ seqkit subseq -r -100:-1 input.fa

if fasta sequence is in single line, with awk:

$ awk -v OFS="\n" '{getline seq} {print $0, substr(seq, length(seq)-99, length(seq))}' test.fa

if fasta sequence is in single line, with sed:

$ sed  -n  '/^>/p;/^>/! s/.*\(.\{100\}\)/\1/p' test.fa

Maybe this will work for you, unless you have IDs longer than 100 characters.

perl -pe 'if(/\>/){s/\n/\t/}; s/\n//; s/\>/\n\>/' | sed -Ee '/^$/d ; s/^.*(.{100})$/\1/' file.fasta

You will get fasta file in single line, removed the empty lines and get the last characters.

You're making another assumption here: that the FASTA entries are single-line. If you have multi-line FASTA, this script won't work. Plus, this script cannot handle empty lines. It would also look a lot cleaner with extended regex sed:

sed -Ee 's/^.*(.{100})$/\1/' file.fasta

You're right, I modified my answer.

Cool, thanks for following up.

Log in to answer this question.