在python中使用滑动窗口概念的GC倾斜方法

问题描述 投票:2回答:1

我已经完成了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
python python-3.x jupyter-notebook bioinformatics
1个回答
0
投票

我希望这有助于理解,

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) )
© www.soinside.com 2019 - 2024. All rights reserved.