BioPython Error Messaging
1
0
Entering edit mode
7.6 years ago
chrisgbeldam ▴ 20

Hi All

I've written some BioPython but I want to be able to generate an error message if the protein searched for is not in my fasta file? I've use try and except but there must be a better way to do it than that?

for myuniprot in myuniprots:
try:
    for record in SeqIO.parse("uniprot_sprot.fasta", "fasta"):
        protein_id = record.id.split('|')[1] if '|' in record.id else record.id
        protein_name = record.id.split('|')[2] if '|' in record.id else record.id
        if protein_id == myuniprot:
            protein_sequence = (record.seq)
            seq = 1
            for n in range(len(protein_sequence)-15):
                current = str((protein_sequence[n:n+16]))
                seqfilename = myuniprot + "_" + str(seq)
                textfilename = seqfilename + ".txt"
                seq += 1
                if not os.path.exists(myuniprot):
                    os.makedirs(myuniprot)
                b = open(myuniprot+ "/" + textfilename, 'w')
                b.write(current)
                b.close()
except:
       print("my uniprot was not found")
python Biopython error errormessage • 2.0k views
ADD COMMENT
1
Entering edit mode

Never use except without specifying which error you are catching (e.g. KeyError). You use try-except if you expect a specific error in rare cases.

So which line throws the exception? Can't you write an if statement for that?

ADD REPLY
0
Entering edit mode

I don't see how this is bioinformatics - this is purely programming.

ADD REPLY
1
Entering edit mode

I'm asking here because it's in relation to BioPython and fasta files which are Bioinformatics

ADD REPLY
0
Entering edit mode

This is a programming question - it concerns loops and conditions. I'm not convinced it qualifies as bioinformatics just because it involves BioPython. I'd redirect you to SO but that place is hell.

My $0.02 - create a dict of IDs and check against that. Or you know, use bioawk if you're not particular about BioPython.

ADD REPLY
0
Entering edit mode

Why not just print your own error message to STDERR?

Something to the effect of:

if data not in mylist:
    print >> sys.stderr, 'Text'
ADD REPLY
2
Entering edit mode

Argh, nasty synthax!

print('Text', file=sys.stderr)

or

sys.stderr.write('Text')
ADD REPLY
0
Entering edit mode

Copied off an old SO post ;)

ADD REPLY
1
Entering edit mode
7.6 years ago
Bioaln ▴ 360

You can not do exception handling otherwise, if you are reading the file record by record. This means that for each new record, you, without previous knowledge on fasta IDs, check whether it exists.

If you need to check the existence without try/except, create a dictionary of your query sequences and then simply do:

if currentRecord not in mySeqDict:
    print ("Not-existing") 
else:
    ...other code...

Hope this helps..

ADD COMMENT

Login before adding your answer.

Traffic: 1626 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6