Python 偏斜基因组
Python Skew Genome
我正在尝试编写倾斜基因组函数,但不断出现错误:
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))
Can you also let me know can I use it in dictionary form?
如果您希望 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——它可以让您避免列表理解,只需 return 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
我正在尝试编写倾斜基因组函数,但不断出现错误:
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))
Can you also let me know can I use it in dictionary form?
如果您希望 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——它可以让您避免列表理解,只需 return 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