Question: remove sequences with non-canonical nucleotides from fasta file
0
gravatar for grant.hovhannisyan
18 months ago by
grant.hovhannisyan1.7k 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 • 622 views
ADD COMMENTlink modified 18 months ago by Eric Lim1.4k • written 18 months ago by grant.hovhannisyan1.7k
1
gravatar for Pierre Lindenbaum
18 months ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum122k 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 18 months ago • written 18 months ago by Pierre Lindenbaum122k

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

ADD REPLYlink written 18 months ago by grant.hovhannisyan1.7k
1
gravatar for Eric Lim
18 months ago by
Eric Lim1.4k
Boston
Eric Lim1.4k 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 18 months ago by Eric Lim1.4k
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: 1562 users visited in the last hour