Question: remove sequences with non-canonical nucleotides from fasta file
0
gravatar for grant.hovhannisyan
2.4 years ago by
grant.hovhannisyan2.0k wrote:

I want to print sequences form fasta file which do not have non-canonical nucleotides. Example fasta is:

>1
ATAcctcatctaGTGTG
ATGCTGCTAGTZ
>2
agagagagagagagag

My code is

from Bio import SeqIO
for record in SeqIO.parse("test.fasta", "fasta") :
    if set(record.seq) <= "ATCGatcg":
                print record

Instead of print the sequence of >2, it prints both.

What am I doing wrong? Thanks

seqio fasta • 982 views
ADD COMMENTlink modified 2.4 years ago by Eric Lim1.7k • written 2.4 years ago by grant.hovhannisyan2.0k
1
gravatar for Pierre Lindenbaum
2.4 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum129k wrote:

linearize and filter with awk:

awk '/^>/ {printf("%s%s\t",(N>0?"\n":""),$0);N++;next;} {printf("%s",$0);} END {printf("\n");}' input.fa |\
awk -F '\t' '($2 ~ /^[ATGCatgc]+$/)' |\
tr "\t" "\n"

using bioalcidaejdk:

$ java -jar dist/bioalcidaejdk.jar -e 'stream().filter(F->java.util.regex.Pattern.matches("^[ATGCatgc]+$",F)).forEach(S->println(">"+S.getName()+"\n"+S));' input.fa
ADD COMMENTlink modified 2.4 years ago • written 2.4 years ago by Pierre Lindenbaum129k

Thank you Pierre, I accept your answer. But anyways, what is wrong with SeqIO?

ADD REPLYlink written 2.4 years ago by grant.hovhannisyan2.0k
1
gravatar for Eric Lim
2.4 years ago by
Eric Lim1.7k
Stoke Therapeutics, Inc
Eric Lim1.7k wrote:
from Bio import SeqIO
from Bio.Alphabet.IUPAC import IUPACUnambiguousDNA
for record in SeqIO.parse("test.fasta", "fasta"):
    if set(record.seq.upper()) <= set(IUPACUnambiguousDNA.letters):
       print(record)
ADD COMMENTlink written 2.4 years ago by Eric Lim1.7k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1001 users visited in the last hour