初始化方法;self对象的len()

1 投票
1 回答
1063 浏览
提问于 2025-04-17 08:14

def __init__(self,emps=str(""),l=[">"]):
    self.str=emps
    self.bl=l


def fromFile(self,seqfile):
    opf=open(seqfile,'r')                                       
    s=opf.read()                                              
    opf.close()                                                    
    lisst=s.split(">")                                             
    if s[0]==">":
        lisst.pop(0)                                                    
    nlist=[]
    for x in lisst:
        splitenter=x.split('\n')                                        
        splitenter.pop(0)                                               
        splitenter.pop()                                                
        splitstring="".join(splitenter)                                 
        nlist.append(splitstring)                                       
    nstr=">".join(nlist)                                                
    nstr=nstr.split()
    nstr="".join(nstr)
    for i in nstr:
        self.bl.append(i)
    self.str=nstr
    return nstr

def getSequence(self):
    print self.str
    print self.bl
    return self.str

def GpCratio(self):
    pgenes=[]
    nGC=[]
    for x in range(len(self.lb)):                                   
        if x==">":
            pgenes.append(x)                                           
    for i in range(len(pgenes)):                                        
        if i!=len(pgenes)-1:                                            
            c=krebscyclus[pgenes[i]:pgenes[i+1]].count('c')+0.000       
            g=krebscyclus[pgenes[i]:pgenes[i+1]].count('g')+0.000                                          
            ratio=(c+g)/(len(range(pgenes[i]+1,pgenes[i+1])))
            nGC.append(ratio)                                           
    return nGC  
s = Sequence()
s.fromFile('D:\Documents\Bioinformatics\sequenceB.txt')
print 'Sequence:\n', s.getSequence(), '\n'
print "G+C ratio:\n", s.GpCratio(), '\n'

我不明白为什么会出现这个错误:

in GpCratio     for x in range(len(self.lb)): AttributeError: Sequence instance has no attribute 'lb'. 

在我打印def getSequence中的列表时,它确实打印出了正确的DNA序列列表,但我却无法使用这个列表来查找核苷酸。我的大学只允许我输入一个文件,不能在定义中使用其他参数,除了“self”。顺便说一下,这是一个类,但我无法发布它……这个类叫做Sequence。


1 个回答

4

看起来是个打字错误。你在 __init__() 函数里定义了 self.bl,然后却试图访问 self.lb

(另外,emps=str("") 是多余的,直接用 emps="" 就可以了。)

但是即使你修正了这个打字错误,循环也不会正常工作:

for x in range(len(self.bl)):   # This iterates over a list like [0, 1, 2, 3, ...]
    if x==">":                  # This condition will never be True
        pgenes.append(x) 

你可能需要做类似这样的事情:

pgenes=[]
for x in self.bl:
    if x==">":                  # Shouldn't this be != ?
        pgenes.append(x) 

这也可以用列表推导式来写:

pgenes = [x for x in self.bl if x==">"]

在 Python 中,你几乎不需要使用 len(x) 或者 for n in range(...);你可以直接遍历序列或可迭代对象。

由于你的程序不完整,而且缺少示例数据,我无法在这里运行它来找出其他问题。也许以下内容可以给你一些启发。假设有一个包含字符 ATCG> 的字符串:

>>> gene = ">ATGAATCCGGTAATTGGCATACTGTAG>ATGATAGGAGGCTAG"
>>> pgene = ''.join(x for x in gene if x!=">")
>>> pgene
'ATGAATCCGGTAATTGGCATACTGTAGATGATAGGAGGCTAG'
>>> ratio = float(pgene.count("G") + pgene.count("C")) / (pgene.count("A") + pgene.count("T"))
>>> ratio
0.75

但是,如果你不想查看整个字符串,而是想查看单独的基因(用 > 作为分隔符),可以使用类似这样的代码:

>>> gene = ">ATGAATCCGGTAATTGGCATACTGTAG>ATGATAGGAGGCTAG"
>>> genes = [g for g in gene.split(">") if g !=""]
>>> genes
['ATGAATCCGGTAATTGGCATACTGTAG', 'ATGATAGGAGGCTAG']
>>> nGC = [float(g.count("G")+g.count("C"))/(g.count("A")+g.count("T")) for g in genes]
>>> nGC
[0.6875, 0.875]

不过,如果你想计算 GC 含量,那么当然不应该用 (G+C)/(A+T),而是 (G+C)/(A+T+G+C) --> nGC = [float(g.count("G")+g.count("C"))/len(g)]

撰写回答