【问题标题】:Init method; len() of self object初始化方法; self 对象的 len()
【发布时间】:2011-12-12 21:04:39
【问题描述】:
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 测序列表,但我不能使用该列表来搜索核苷酸。我的大学只允许我输入 1 个文件,而不是在定义中使用其他参数,而是“自我” 顺便说一句,它是一个类,但它拒绝我发布它.. 类称为序列


【问题讨论】:

标签: python initialization


【解决方案1】:

看起来像是一个错字。您在__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)].

【讨论】:

  • 我也改变了:for x in range(len(self.bl)): if self.bl[x]==">": pgenes.append(x) 但它仍然返回一个空列表
  • 我改为(虽然我还不熟悉这种风格)pgenes = [x for x in self.bl if x==">"],但它返回一个空列表跨度>
  • 假设self.bl == ">"(从初始化不变),那就难怪了。 len(pgenes) 将是 1,所以 range(1)[0]。因此if i!=len(pgenes)-1False,因此不会将任何内容附加到nGC
  • 为什么不改?我在 def fromFile 中将其更改为:for i in nstr: self.bl.append(i) 所以它打印正确的 DNA 列表: self.bl 打印 ['>','A','T','G', 'A', etc..] 但是当我尝试计算 (G+C)/(A+T) 的比率时,它除了返回一个空列表之外没有做任何事情
  • 好的,我明白你的意思了。不过,看起来你让事情变得比他们需要的复杂得多。 >到底是什么意思?它是否在字符串中多次出现?你能举一个self.bl 的典型例子,然后对应的pgenes 应该是什么样子?现在,该例程将所有>s(或其索引)从self.bl 收集到pgenes...
猜你喜欢
  • 1970-01-01
  • 2013-07-20
  • 2015-07-16
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
相关资源
最近更新 更多