"]): self.str=emps self.bl=l def fromFile(self,seqfile):-6ren">
gpt4 book ai didi

python - 初始化方法;自身对象的 len()

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:41 28 4
gpt4 key购买 nike

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 个文件并且不使用定义中的其他参数,但是“ self ”顺便说一句,这是一个类,但它拒绝我发布它......类名为 Sequence


最佳答案

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

关于python - 初始化方法;自身对象的 len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480825/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com