Biostar Beta. Not for public use.
Extract only sequences that have N nucleotides
0
Entering edit mode
21 months ago
waqasnayab • 180
Pakistan

Hi,

I have a fasta file. From that fasta file, I need to extract only those sequences that have Ns nucleotides.

seq1

AGCGGCGTAACGTCGTAGTC

seq2

ACGCGTACNNNNNNTGCGA

I want output like this:

seq1

AGCGGCGTAACGTCGTAGTC

Regards,

Waqas

genome sequencing • 1.1k views
ADD COMMENTlink
0
Entering edit mode

Your example has no Ns, which is the exact opposite of what you say you want.

Are the sequences always on a single line?

ADD REPLYlink
0
Entering edit mode

You mean, you want to exclude the sequences with Ns?

awk -v header="" '{ if($1~/^>/){header=$1}else if($1!~/N/){print header; print $1;}}' fastaFile

Note, this only work if you don't have multi-line fasta

ADD REPLYlink
1
Entering edit mode
16 months ago
France/Nantes/Institut du Thorax - INSE…

linearize the fasta https://gist.github.com/lindenb/2c0d4e11fd8a96d4c345

filter with awk and a regular expression, convert back to fasta

awk -f linearizefasta.awk < input.fa |  awk -F '\t' '($2 ~ /N/)' | tr "\t" "\n"
ADD COMMENTlink
1
Entering edit mode
16 months ago
Daniel ♦ 3.7k
Cardiff University

With one-line fasta and with sequence names specifically 'seq#' (i.e. no letter 'N's), you can super easily do:

grep -B 1 'N' input.fasta >output_no_Ns.fasta

But that is assuming your data is in the specific format above. Multi line fasta or N's in your headers would break it. But this is what I'd do if I had this data.

ADD COMMENTlink
1
Entering edit mode
21 months ago
waqasnayab • 180
Pakistan

Hi,

First, I linearized multi-line fasta file to single-line fasta file:

https://www.biostars.org/p/9262/

awk '/^>/ {printf("\n%s\n",$0);next; } { printf("%s",$0);}  END {printf("\n");}' < file.fa

Than, @Daniel command:

grep -B 1 'N' input.fasta >output_no_Ns.fasta

I apologized the community for not explaining my question properly.

Best,

Waqas.

ADD COMMENTlink
1
Entering edit mode

Thanks for letting us know! You can upvote and tick the helping answers, to finish the question thread.

ADD REPLYlink
0
Entering edit mode
15 months ago
Anima Mundi ♦ 2.4k
Italy

In Python, for a file named foo.fa containing linearised FASTAs (prunes FASTAs with Ns):

header = ''

for line in open('foo.fa'):
    if '>' in line:
        header = line
    elif line == '\n':
        pass
    elif 'N' not in line.upper():
        print header,
        print line,
ADD COMMENTlink
0
Entering edit mode
18 months ago
Walnut Creek, USA

With the BBMap package:

reformat.sh in=file.fasta out=fixed.fasta maxns=0

ADD COMMENTlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.3.1