按python代码列出分配索引超出范围?

按python代码列出分配索引超出范围?,python,Python,我不断收到一封信 索引器错误:列表分配索引超出范围 第78行的错误 编写此代码的目的是为生物信息学找到模体DNA 我们如何解决这个错误或问题 这是我的代码: from math import log class MotifMedianFinding(object): def __init__(self, input_file): super(MotifMedianFinding, self).__init__() self.input_lines = o

我不断收到一封信

索引器错误:列表分配索引超出范围

第78行的错误
编写此代码的目的是为生物信息学找到模体DNA
我们如何解决这个错误或问题

这是我的代码:

from math import log
class MotifMedianFinding(object):

    def __init__(self, input_file):
        super(MotifMedianFinding, self).__init__()
        self.input_lines = open("C:\\Users\\A.Khassawneh\\Desktop\\fasta.txt")

    def output(self):
        #main method to call both functions
        sequences = {}
        for line in self.input_lines:
            if '>' in line:
                sequences[line] = self.input_lines.next()

        for label, seq in sequences.iteritems():
            print "DNA:" + seq + "\n\n\n\n\n"
            median = self.median_string(seq, 5,5, len(seq))
            self.motif(seq, median,5,len(seq))

    def median_string(self, dna, t, n, l):
        #bound and search method of calulating median string
        start_pos = start_pos = [1,1,1,1,1]
        best_dist = 1000000000
        i = 1
        while i > 0:
            if i < l:
                prefix = str(start_pos)
                opt_dist = self.hamming_score(prefix, dna)
                if opt_dist > best_dist:
                    s,i = self.bypass(start_pos,i,l,4)
                else:
                    s,i = self.next_vertex(start_pos,i,l,4)
            else:
                word = str(s)
                if self.hamming_score(word, dna) < best_dist:
                    best_dist = self.hamming_score(word, dna)
                    bestword = word
                s,i = self.next_vertex(start_pos,i,l,4)
        print "Best Word: %s (tot_dis = %s)" % (bestword,best_dist)
        return bestword


    def motif(self, dna, t, n, l):
        #bound and search method of calculating motif
        start_pos = [1,1,1,1,1]
        best_score = 0
        i = 1
        while 1 > 0:
            if i < t:
                opt_score = Score(s, i, dna) + (t-1) * l
                if opt_score < best_score:
                    start_pos, i = self.bypass(start_pos, i, t, n-l+1)
                else:
                    start_pos, i = self.next_vertex(start_pos, i, t, n-l+1)
            else:
                if self.score(start_pos, dna) > best_score:
                    best_score = self.score(start_pos)
                    best_motif = str(s)
                start_pos, i = self.next_vertex(start_pos, i, t, n-l+1)
        print "motif consensus string: %s (consensus_score = %s) " % (best_motif, best_score)
        print "motif positions/string s=(s1..st): %s" % ', '.join(start_pos)
        return best_motif

    def bypass(vertex, level, l, k):
        #skip uncessary calculations in the tree
        j = level
        for ind in xrange(j,1,-1):
            if a[j] < k:
                a[j] = a[j] + 1
                return vertex, j
        return vertex, 0 

    def next_vertex(self, vertex, level, L, k):
        #transverse the tree of a strand of genes
        if level <L:
            vertex[level+1] = 1
            return vertex,level+1
        else:
            j = L
            for ind in xrange(j,1,-1):
                if vertex[ind] < k:
                    vertex[j]  = vertex[j] + 1
                    return vertex, j
        return vertex, 0

    def score(start_pos):
        # biggest score of motif
        total = 0
        for i in start_pos:
            total += i
        return total

    def hamming_score(self, s, dna):
        pass



motif_median = MotifMedianFinding('HMP-part.fa')
motif_median.output()
从数学导入日志
类motimedianfinding(对象):
def_uuuinit_uuu(自,输入文件):
超级(motimedinfinding,self)。\uuuu init\uuuuu()
self.input_lines=open(“C:\\Users\\A.khassoweh\\Desktop\\fasta.txt”)
def输出(自):
#调用这两个函数的主方法
序列={}
对于self.input_行中的行:
如果行中有“>”:
sequences[line]=self.input\u lines.next()
对于标签,在sequences.iteritems()中添加seq:
打印“DNA:+seq+”\n\n\n\n”
中位数=自身中位数字符串(序号5,5,长度(序号))
自我主题(序号、中位数、5、长度(序号))
定义中位数字符串(自身、dna、t、n、l):
#计算中位数字符串的边界和搜索方法
开始位置=开始位置=[1,1,1,1]
最佳距离=100000000
i=1
当i>0时:
如果i最佳距离:
s、 i=自旁路(启动位置,i,l,4)
其他:
s、 i=自身。下一个顶点(开始位置,i,l,4)
其他:
word=str(s)
如果self.hamming_分数(单词、dna)<最佳距离:
最佳距离=自我。汉明分数(单词、dna)
最佳单词
s、 i=自身。下一个顶点(开始位置,i,l,4)
打印“最佳单词:%s(tot\u dis=%s)”%(最佳单词,最佳距离)
返回最佳单词
def基序(自身、dna、t、n、l):
#计算模体的界和搜索方法
开始位置=[1,1,1,1,1]
最佳分数=0
i=1
当1>0时:
如果i最佳评分:
最佳得分=自我得分(开始位置)
最佳主题=str(s)
开始位置,i=self.下一个顶点(开始位置,i,t,n-l+1)
打印“主题共识字符串:%s(共识分数=%s)”%(最佳主题,最佳分数)
打印“motif positions/string s=(s1..st):%s”%,'。连接(开始位置)
返回最佳主题
def旁通(顶点、标高、l、k):
#跳过树中的非必要计算
j=水平
对于X范围内的ind(j,1,-1):
如果a[j]xrange(x,y)
x
变为
y-1
(x,x+1…y-1)
。在您的代码中,可以执行
xrange(1,j)
,因为这不包括
j
。但是如果你把它换成
xrange(j,1,-1)
,你就变成
(j,j-1….2)


基本上,您可能需要将其更改为
xrange(j-1,0,-1)
,具体取决于您的预期范围。

请编辑您的问题:添加错误的完整回溯,从atting中修复代码,以及一些关于代码正确作用的背景信息,而不是我建议执行的
print j
print len(vertex)
在第78行之前,因为j似乎太大。您可能需要使用
L-1
而不是
L
对其进行初始化?