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

2024-06-01 12:07:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在找工作

IndexError: list assignment index out of range.

第78行的错误
编写此代码是为了在生物信息学中找到motif 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()

Tags: ofinposselfreturnifdistdef
1条回答
网友
1楼 · 发布于 2024-06-01 12:07:35

xrange(x,y)xy-1(x, x+1.... y-1)。在您的代码中,可以执行xrange(1,j),因为这不会包括j。但是如果你把它换成xrange(j,1,-1),你就去(j, j-1.... 2)。你知道吗

基本上,您可能需要根据预期的范围将其更改为xrange(j-1,0,-1)。你知道吗

相关问题 更多 >