我已经完成了python的初学者课程,我正在解决一个问题,以提高我的编码技能。在这个问题中,我必须通过将整个序列分成等长的子序列来计算GC-skew。我在一台jupyter笔记本上工作。我必须创建一段代码,以便从序列中获得C和G的数量,然后计算每个窗口中的GC偏斜。窗口大小= 5kb,增量为1kb。
到目前为止,我所做的是首先将序列存储在一个列表中,并接受用户输入框/窗口的长度和框的增量。然后我尝试创建一个循环来计算每个窗口中的C和G的数量,但在这里我面临一个问题,因为我不是获取窗口/框中的C和G的数量,而是从循环运行的次数的整个序列中获取C和G的数量。我想要每个窗口中C的总数和G的总数。
请建议如何获取每个重叠滑动窗口/框的字符数量和GC偏斜。另外,在python中有没有滑动窗口的概念,我可以在这里使用它?
char = []
with open('keratin.txt') as f:
for line in f:
line = line.strip()
for ch in line:
char.append(ch)
print(char)
len(char)
f1 = open('keratin.txt','r')
f2 = open('keratin.txt','a+')
lob = input('Enter length of box =')
iob = input('Enter the increment of the box =')
i=0
lob = 5000
iob = 1000
nob = 1 #no. of boxes
for i in range (0,len(char)-lob):
b = i
while( b < lob + i and b < len(char)):
nC = 0
nG = 0
if char[b] == 'C':
nC = nC + 1
elif char[b] == 'G':
nG = nG + 1
b = b + 1
print(nC)
print(nG)
i = i + iob
nob = nob + 1
发布于 2018-06-25 02:33:51
我希望这能帮助理解,
number_of_C_and_G = []
# Go from 0 to end, skipping length of box and increment. 0, 6000, 12000 ...
for i in range(0, len(char), lob+inc):
nC = 0
nG = 0
# Go from start to length of box, 0 to 5000, 6000 to 11000 ...
for j in range(i, lob):
if char[j] == 'C':
nC += 1
else if char[j] == 'G':
nG += 1
# Put the value for the box in the list
number_of_C_and_G.append( (nC, nG) )
https://stackoverflow.com/questions/50999495
复制相似问题