我正在尝试创建一个搜索文本的程序,并根据所需的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时通过算法运行该数字列表。它可能不是最漂亮/最简单的/最好,但它奏效了。感谢大家的编辑!
另外,请注意递归限制增加。我知道这可能不是很好。欢迎任何关于迭代器的建议(我还没有学到这一点!)
答案 0 :(得分:0)
问题似乎与您的函数pat2Num()
和计算l
的行有关。目前尚不清楚l
假设被计算为什么。
原因是l
在pattern == 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
并在调用函数之前将其存储在某处。然后总和值是列表或类似的东西。
希望这有帮助