异常处理并在python中继续使用for循环

时间:2014-11-13 15:56:51

标签: python for-loop bioinformatics continue custom-error-handling

我有以下代码:

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=[]

使用(自定义方法)hugo2uniprot

获取uniprot IDS
    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])

搜索GO术语(使用生物服务QuickGO)并存储为字典

    GO_dict = {}
    q = QuickGO()

一些大的基因名单我没有回复任何命中。 这会导致我想要处理的AttributeError然后转到列表中的下一个项目。当前代码返回错误:AttributeError:'int'对象没有属性'split'。错误源自bioservices模块(所以当我调用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

有没有人有任何建议让异常处理工作,所以它完成迭代指定的列表而不是停止脚本?

1 个答案:

答案 0 :(得分:1)

好吧我刚刚意识到问题是错误来自不同的代码......所以我本质上是在尝试什么都不做。我已经修好了,感谢所有回复的人。