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

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

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