我有一本叫self.__sequences的字典,上面写着"ID:DNA序列“,下面是该字典的一部分
{
'1111758': ('TTAGAGTTTGATCCTGGCTCAGAACGAACGCTGGCGGCAGGCCTAA\n', ''),
'1111762': ('AGAGTTTGATCCTGGCTCAGATTGA\n', ''),
'1111763': ('AGAGTTTGATCCTGGCCTT\n', '')
}我想要计算特定序列ID (some_id)的gc conent。也就是说,如果some_id在字典中,返回该ID的DNA序列的gc内容;如果some_id不存在,则返回一条错误消息。
Ps.gc content= (G+C)/(A+T+G+C)
我编写了以下代码(函数在类下),但它提供了错误消息。如果有人能帮我改进我的代码,我很感激。
def compute_gc_content(self, some_id=''):
"""compute the gc conent for sequence ID (some_id). If some_id is in the
dictionary, return the gc content of the DNA sequence for that ID; if some_id
does not exist,return an error message"""
self.some_id = some_id
for i in range(len(self.__sequences)):
if self.some_id in self.__sequences.keys():
return (self.some_id.values['G']+self.some_id.values['C'])/float(len(self.__sequences))
else:
return "This ID does not exist"因此,如果我打印compute_gc_content('1111758'),我想打印gc内容的值,比如0.23。
发布于 2014-02-12 10:35:41
我不确定我是否正确理解你。
def compute_gc_content(self, some_id=''):
if some_id in self.__sequences:
seq = self.__sequences['some_id'][0]
return (seq.count('G')+seq.count('C'))/float(len(seq))
else:
return "This ID does not exist"不需要使用in self.__sequences.keys(),in self.__sequences也做同样的事情。
发布于 2014-02-12 10:37:17
这就是你要找的吗?
import itertools
class gc:
def __init__(self):
self.__sequences = {'1111758': ('TTAGAGTTTGATCCTGGCTCAGAACGAACGCTGGCGGCAGGCCTAA\n', ''), '1111762': ('AGAGTTTGATCCTGGCTCAGATTGA\n', ''), '1111763': ('AGAGTTTGATCCTGGCCTT\n', '')}
def compute_gc_content(self, some_id=''):
"""compute the gc conent for sequence ID (some_id). If some_id is in the
dictionary, return the gc content of the DNA sequence for that ID; if some_id
does not exist,return an error message"""
self.some_id = some_id
for i in range(len(self.__sequences)):
if self.some_id in self.__sequences.keys():
return (float)(self.__sequences[some_id][0].count('G')+self.__sequences[some_id][0].count('C'))/(len(self.__sequences[some_id][0]))
else:
return "This ID does not exist"
print gc().compute_gc_content('1111758')https://stackoverflow.com/questions/21724886
复制相似问题