计算模式频率

时间:2016-06-10 13:01:17

标签: python python-2.7 oop bioinformatics

我正在尝试创建一个搜索文本的程序,并根据所需的kmer长度拉出模式。然后,我希望它使用我从类中学到的算法将这些模式转换为数字,并将这些值存储到字典中,以便我可以计算每个模式的频率。我知道我可以用模式本身来做,但是赋值的目标是返回一系列数字(例如:2 2 2 1 1 3 2

到目前为止,我能够递归调用pat2num函数,但我无法计算算法4 * pat2Num(pattern) + symbol。我还没有尝试过实施这本词典。

bases = {'A': 0, 'C': 1, 'G': 2, 'T': 3}


class ComputingFrequencies:
    def __init__(self, text, k, list):
        self.text = text
        self.k = k
        self.list = list  

    def patternGenerator(self):
        return self.freqPat(self.text, self.k)  #begins the class call

    def freqPat(self, text, k):
        for i in range(0, len(text) - k + 1):   #searches length of text corresponding to size of kmer
            pattern = text[i : k + i]           #pulls out kmer
            self.list.append(pattern)           #appends kmer to list
            print self.list
        return self.pat2Nums(list)              #calls pat2Nums

    def pat2Nums(self, list):
        if len(self.list) == 0:
            return 0
        else:
            for pattern in self.list:           #calls pat2Num for each pattern in list
                self.pat2Num(pattern)        

    def pat2Num(self, pattern):
        if len(pattern) == 0:
            self.list = self.list[1:]           
            self.pat2Nums(self.list)
        else:
            symbol = pattern[-1]                #symbol is last char of pattern
            pattern = pattern[0 : -1]           #pattern is (pattern - last char)
            if symbol in bases:
                symbol = bases[symbol]          #symbol becomes number equivalent
                print symbol
                l = 4 * self.pat2Num(pattern) + symbol   #algorithm for turning pattern into number


 c = ComputingFrequencies('AGTAGT', 3, list())
 c.patternGenerator()

更新:我终于得到了一些有用的东西,产生了所需的输出。有必要将所有可能的结果初始化为0,因此大输出,大多数为0(如果你运行它)。这是代码:

import sys
sys.setrecursionlimit(10000)

bases = {'A': 0, 'C': 1, 'G': 2, 'T': 3}
algNum = list()
freqArray = dict()

class ComputingFrequencies:
    def __init__(self, text, k, list, list2):
        self.text = text
        self.k = k
        self.list = list
        self.list2 = list2  

    def patternGenerator(self):
        return self.freqPat(self.text, self.k)  #begins the class call

    def freqPat(self, text, k):
        for i in range(0, 4**k):
            freqArray[i] = 0
        for i in range(0, len(text) - k + 1):   #searches length of text corresponding to size of kmer
            pattern = text[i : k + i]           #pulls out kmer
            self.list.append(pattern)           #appends kmer to list
            print self.list
        return self.pat2Nums(list)              #calls pat2Nums

    def pat2Nums(self, list):
        if len(self.list) == 0:
            l = str(freqArray.values())
            m = l.replace(",", "")
            print m
            #r = open('file')
            #r.write(m)
        else:
            pattern = self.list[0]
            return self.pat2Num(pattern)        

    def pat2Num(self, pattern):
       symbol = pattern[-1]                #symbol is last char of pattern
       pattern = pattern[0 : -1]           #pattern is (pattern - last char)
       if symbol in bases:
            symbol = bases[symbol]         #symbol becomes number equivalent
            algNum.append(symbol)
       if len(pattern) > 0:
            return self.pat2Num(pattern)
       else:
            self.algNum(algNum, self.k)

    def algNum(self, list, k):
        while len(algNum) > 0:
            if len(algNum) == self.k:
                symbol = algNum[-1]
                prev = symbol
            else:
                symbol = algNum[-1]
                alg = 4 * prev + symbol
                prev = alg
            del algNum[-1]
        freqArray[alg] = freqArray.get(alg, 0) + 1
        self.list = self.list[1:]
        return self.pat2Nums(self.list)

 c = ComputingFrequencies('AGTAGT', 3, list())
 c.patternGenerator()

所以基本上我将算法从之前的位置拉出来,将符号值存储在另一个列表中,然后在模式长度达到0时通过算法运行该数字列表。它可能不是最漂亮/最简单的/最好,但它奏效了。感谢大家的编辑!

另外,请注意递归限制增加。我知道这可能不是很好。欢迎任何关于迭代器的建议(我还没有学到这一点!)

1 个答案:

答案 0 :(得分:0)

问题似乎与您的函数pat2Num()和计算l的行有关。目前尚不清楚l假设被计算为什么。

原因是lpattern == None之前陷入无限循环。让我们来看看它:

  • 发起pat2Num('AGT')
  • pattern = 'AGT'
  • symbol = 'T'
  • 模式变为'AG'
  • 计算l,但在公式中,您会使用新模式回忆pat2Num('AG')
  • 这意味着l从未被计算或存储为任何内容,因为之前对pat2Num()的调用从未返回任何要存储在l中的内容。
  • 重新启动该功能后,symbol现在变为G
  • pattern现在变为A
  • 计算l,但请回忆pat2Num('A'),不要在l中存储任何内容
  • symbol现在变为A
  • pattern现在变为None
  • 计算l,但现在pattern没什么,所以无类型错误或无限循环

返回并考虑行l = 4 * self.pat2Num(pattern) + symbol并计算l并在调用函数之前将其存储在某处。然后总和值是列表或类似的东西。

希望这有帮助