我正在尝试编写扭曲的基因组功能,但不断得到错误:
Failed test #2.
Test Dataset: AGCGTGCCGAAATATGCCGCCAGACCTGCTGCGGTGGCCTCGCCGACTTCACGGATGCCAAGTGCATAGAGGAAGCGAGCAAAGGTGGTTTCTTTCGCTTTATCCAGCGCGTTAACCACGTTCTGTGCCGACTTT
Your output: ['0', '0']
Correct output: ['0', '0', '1', '0', '1', '1', '2', '1', '0', '1', '1', '1', '1', '1', '1', '1', '2', '1', '0', '1', '0', '-1', '-1', '0', '0', '-1', '-2', '-2', '-1', '-2', '-2', '-1', '-2', '-1', '0', '0', '1', '2', '1', '0', '0', '-1', '0', '-1', '-2', '-1', '-1', '-2', '-2', '-2', '-3', '-3', '-4', '-3', '-2', '-2', '-2', '-1', '-2', '-3', '-3', '-3', '-2', '-2', '-1', '-2', '-2', '-2', '-2', '-1', '-1', '0', '1', '1', '1', '2', '1', '2', '2', '3', '2', '2', '2', '2', '3', '4', '4', '5', '6', '6', '6', '6', '5', '5', '5', '5', '4', '5', '4', '4', '4', '4', '4', '4', '3', '2', '2', '3', '2', '3', '2', '3', '3', '3', '3', '3', '2', '1', '1', '0', '1', '1', '1', '0', '0', '1', '1', '2', '1', '0', '1', '1', '0', '0', '0', '0']
我代码:Genome = "CATGGGCATCGGCCATACGCC"
def SymbolArray(Genome, symbol):
array = {}
n = len(Genome)
ExtendedGenome = Genome + Genome[0:n//2]
for i in range(n):
array[i] = PatternCount(symbol, ExtendedGenome[i:i+(n//2)])
return array
def Skew(Genome):
skew = {}
skew[0]=0
n = len(Genome)
for i in range(1, n+1):
skew[i] = skew[i-1]
if Genome[i-1] == "G":
skew[i] = skew[i-1]+1
elif Genome[i-1] == "C":
skew[i] = skew[i-1]-1
else:
skew[i] = skew[i-1]
return skew
for i in skew.items():
Skew(Genome)
这个问题比你想象的要简单。最大的问题似乎是:你的return
语句在循环中,而不是在循环之后;当你想要一个数组时,你使用了一个字典;你的范围的末端偏离了1;你有一个不必要的递归调用Skew()
。
下面是代码的简化:
Genome = "AGCGTGCCGAAATATGCCGCCAGACCTGCTGCGGTGGCCTCGCCGACTTCACGGATGCCAAGTGCATAGAGGAAGCGAGCAAAGGTGGTTTCTTTCGCTTTATCCAGCGCGTTAACCACGTTCTGTGCCGACTTT"
def Skew(genome):
skew = [0]
for i in range(1, len(genome)):
skew.append(skew[-1])
if genome[i - 1] == "G":
skew[i] = skew[i - 1] + 1
elif genome[i - 1] == "C":
skew[i] = skew[i - 1] - 1
return skew
print(Skew(Genome))
你能让我知道我能以字典的形式使用它吗?
如果你想让skew
容器是一个字典,就像你原来的那样,你可以这样做:
def Skew(genome):
skew = {0:0}
for i in range(1, len(genome)):
if genome[i - 1] == "G":
skew[i] = skew[i - 1] + 1
elif genome[i - 1] == "C":
skew[i] = skew[i - 1] - 1
else:
skew[i] = skew[i - 1]
return [value for (key, value) in sorted(skew.items())]
但是,我不推荐这样做。字典通常用于表示稀疏数组,但这里的情况并非如此。实现这一点的另一种方法是使用OrderedDict——它将允许您避免列表推导并简单地返回skew.values()
。
def Skew(Genome):
skew = {}
for base in range(1, len(Genome)+1): # since we start at 1 and not 0 as we should we are adding one to the length
if Genome[base - 1] == "G": # subtracting one since we start at one so base at the 0 position has to be included
skew[base] = skew[base - 1] + 1
elif Genome[base - 1] == "C":
skew[base] = skew[base - 1] - 1
else:
skew[base] = skew[base - 1]
return skew