This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Sequence filtration based on length

Hello Everyone,

I want to filter sequence based on length between 15 - 30 nt from large file of RNA sequences (fasta format).

How to do this?

I appreciate any help.

Thanks

grep perl rna-seq sequence

1 answer

Assuming that your sequences are on one line:

​$ awk '{ \
    if ($0 ~ /^>/) { \
        header = $0; \
    } \
    else { \
        l = length($0); \
        if ((l >= 15) && (l <= 30)) { \
            printf("%s\n%s\n", header, $0); \
        } \
    } \
}' foo.fa

Look at this Biostars question if you need to preprocess your FASTA file to put its sequences on one line.

Thanks for your response. How to get those sequences along their corresponding header?

Input file in .fasta format containing many sequences with different headers.

I added some changes. Hope it helps.

Its working. Thanks a lot.

Log in to answer this question.