weixin_46944866
vic zai
2020-05-07 12:36
采纳率: 28.6%
浏览 1.4k

运行代码出现Process finished with exit code 0并未输出结果,请问如何修改才能输出结果?

网上查过要调用,但本人新手不会,请问各位大神指教,如何调用并输出

def create_matrix_zeros(nrows,ncols):
    res = []
    for i in range(0,nrows):
        res.append([0]*ncols)
    return res

def print_matrix(mat):
    for i in range(0,len(mat)):
        print(mat[i])

class MyMotifs:
    def __init__(self,seqs=[],pwm=[],alphabet=None):
        if seqs:
            self.size = len(seqs[0])
            self.seqs = seqs
            self.alphabet = seqs[0].alphabet()
            self.do_counts()
            self.create_pwm()
        else:
            self.pwm = pwm
            self.size = len(pwm[0])
            self.alphabet = alphabet

    def __len__(self):
        return self.size

    def do_counts(self):
        # noinspection PyAttributeOutsideInit
        self.counts = create_matrix_zeros(len(self.alphabet),self.size)
        for s in self.seqs:
            for i in range(self.size):
                lin = self.alphabet.index(s[i])
                self.counts[lin][i] += 1

    def create_pwm(self):
        if self.counts is None:
            self.do_counts()
        self.pwm = create_matrix_zeros(len(self.alphabet),self.size)
        for i in range(len(self.alphabet)):
            for j in range(self.size):
                # noinspection PyTypeChecker
                self.pwm[i][j] = float(self.counts[i][j]) / len(self.seqs)

    def consensus(self):
        res = ""
        for j in range(self.size):
            maxcol = self.counts[0][j]
            maxcoli = 0
            for i in range(1,len(self.alphabet)):
                if self.counts[i][j]>maxcol:
                    maxcol = self.counts[i][j]
                    maxcoli = i
            res += self.alphabet[maxcoli]
        return res

    def masked_consensus(self):
        res = ""
        for j in range(self.size):
            maxcol = self.counts[0][j]
            maxcoli = 0
            for i in range(1,len(self.alphabet)):
                if self.counts[i][j]>maxcol:
                    maxcol = self.counts[i][j]
                    maxcoli = i
            if maxcol>len(self.seqs) / 2:
                res += self.alphabet[maxcoli]
            else:
                res += "-"
        return res

    def probability_sequence(self,seq):
        res = 1.0
        for i in range(self.size):
            lin = self.alphabet.index(seq[i])
            res *= self.pwm[lin][i]
            return res

    def probability_all_positions(self,seq):
        res = []
        for k in range(len(seq)-self.size+1):
            res.append(self.probability_sequence(seq))
        return res

    def most_probable_sequence(self,seq):
        maximum = -1.0
        maxind = -1
        for k in range(len(seq)-self.size):
            p = self.probability_sequence(seq[k:k+self.size])
            if p > maximum:
                maximum = p
                maxind = k
        return maxind

    def create_motif(self,seqs):
        from Bio.Seq import Seq
        l = []
        for s in seqs:
            ind = self.most_probable_sequence(s.seq)
            subseq = Seq(s[ind:(ind+self.size)],s.get_seq_biotype() )
            l.append(subseq)

        return MyMotifs(l)

def test():
    from Bio.Seq import Seq
    seq1 = Seq("AAAGTT")
    seq2 = Seq("CACGTG")
    seq3 = Seq("TTGGGT")
    seq4 = Seq("GACCGT")
    seq5 = Seq("AACCAT")
    seq6 = Seq("AACCCT")
    seq7 = Seq("AAACCT")
    seq8 = Seq("GAACCT")
    lseqs = [seq1,seq2,seq3,seq4,seq5,seq6,seq7,seq8]
    motifs = MyMotifs(lseqs)
    print ("Counts matrix")
    print_matrix(motifs.counts)
    print ("PWM")
    print_matrix(motifs.pwm)
    print ("Sequence alphabet")
    print(motifs.alphabet)

    for s in lseqs:
        print(s)
    print ("Consensus sequence")
    print(motifs.consensus())
    print ("Masked Consensus sequence")
    print(motifs.masked_consensus())

    print(motifs.probability_sequence("AAACCT"))
    print(motifs.probability_sequence("ATACAG"))
    print(motifs.most_probable_sequence("CTATAAACCTTACATC"))
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • satann
    Nora持续行 2020-05-07 14:48
    已采纳

    在最后一行添加test()
    如下所示

    #...省略以上所有
        print(motifs.probability_sequence("AAACCT"))
        print(motifs.probability_sequence("ATACAG"))
        print(motifs.most_probable_sequence("CTATAAACCTTACATC"))
    test()
    

    然后python 文件名 执行该文件即可

    点赞 评论

相关推荐