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

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

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