2

I have the following code:

import Bio
from bioservices import KEGGParser, UniProt, QuickGO

#start a new method of my custom 'Retrieve_Data' class
def locate_common_GO(self,list_of_genes,GO_term):
    
    #initialize variables and classes
    q = QuickGO()
    a = Retrieve_Data()
    b=[]

Get the uniprot IDS using (custom method) hugo2uniprot

for i in range(0,len(list_of_genes)):
    b.append(a.hugo2uniprot(list_of_genes[i],'hsa'))
    print 'Gene: {} \t UniProtID: {}'.format(list_of_genes[i],b[i])
    

Search for GO terms (using bioservices' QuickGO) and store as dictionary

GO_dict = {}
q = QuickGO()

Some of the large list of gene names that I have do not return any hits. These cause an AttributeError that I want to handle then move on to the next item in the list. The current code returns the error:

AttributeError: 'int' object has no attribute 'split'.

The error originates within the bioservices module (so when I'm calling q.Annotation)

for i in range(len(b)):
    try:
        GO_dict[list_of_genes[i]] = q.Annotation(protein=b[i], frmt="tsv", _with=True,tax=9606, source="UniProt", col="goName")
    except AttributeError:
        continue

# The rest of this code is irrelevant to my problem but I've included it for completeness: 
# Essentially it is designed to locate specific keywords in the results of the above code and return the gene name that they came from. 
keys = GO_dict.keys()
matches = []
for gene in range(0,len(keys)):
    if GO_term in GO_dict[keys[gene]].splitlines():
        matches.append(keys[gene])
return matches

Does anybody have any suggestions to get the exception handling working so it finishes iterating over the specified list instead of halting the script?

Cleb
  • 25,102
  • 20
  • 116
  • 151
CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • What are the details of the `AttributeError` and what do you mean by "does not work"? Also, using `range()` instead of looping over a list directly is a code smell in Python. Perhaps you want [`enumerate()`](https://docs.python.org/2/library/functions.html#enumerate) so you have an index counter? – Tom Nov 13 '14 at 16:07
  • Is it raising an exception other than `AttributeError`? What is causing the script to stop execution? More information would be useful. – slurms Nov 13 '14 at 16:07
  • @Tom - are you trying to tell me that enumerate() will do the same job as range(n,m)? – CiaranWelsh Nov 13 '14 at 16:12
  • @slurms - I've edited the question to try and provide more detail – CiaranWelsh Nov 13 '14 at 16:13
  • @user3059024: can you add a print statement below the first for loop to confirm it completes the loop or doesn't? If it completes the loop it will print something, if it doesn't nothing will be printed. – slurms Nov 13 '14 at 16:18
  • Yes, I have done this and the loop does not complete. – CiaranWelsh Nov 13 '14 at 16:21
  • 1
    Perhaps it's failing on the first loop (Get the uniprot IDS using (custom method) hugo2uniprot). Does it get through all of the genes? It shouldn't fail on the second as the loop is explicitly catching `AttributeError`... – slurms Nov 13 '14 at 16:30
  • @slurms - Yes, that was the answer. I've just realized. Thanks for responding – CiaranWelsh Nov 13 '14 at 16:32
  • @user3059024: no, I'm not saying enumerate is the same function as range. I'm suggesting you may not need the counters you're using in your loops. In Python they're often superfluous, replaceable by enumerate (which accepts a list and returns a list element and index counter) or by zip if you have two lists that go together. – Tom Nov 13 '14 at 19:04

1 Answers1

1

Okay I've just realized that the problem is that the error was coming from a different bit of code... So I was essentially trying to handle nothing. I've fixed it now, thanks to all those who responded.

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106