-
Notifications
You must be signed in to change notification settings - Fork 55
/
GenomicRangeQuery_v2.py
59 lines (45 loc) · 1.7 KB
/
GenomicRangeQuery_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, P, Q):
# write your code in Python 3.6
cumulative_A = [0] * ( len(S) +1 )
cumulative_C = [0] * ( len(S) +1 )
cumulative_G = [0] * ( len(S) +1 )
# cumulative_T = [0] * len(S)
for i in range( len(S) ):
if S[i] == 'A':
cumulative_A[i+1] = cumulative_A[i] + 1
cumulative_C[i+1] = cumulative_C[i]
cumulative_G[i+1] = cumulative_G[i]
elif S[i] == 'C':
cumulative_A[i+1] = cumulative_A[i]
cumulative_C[i+1] = cumulative_C[i] + 1
cumulative_G[i+1] = cumulative_G[i]
elif S[i] == 'G':
cumulative_A[i+1] = cumulative_A[i]
cumulative_C[i+1] = cumulative_C[i]
cumulative_G[i+1] = cumulative_G[i] + 1
else:
cumulative_A[i+1] = cumulative_A[i]
cumulative_C[i+1] = cumulative_C[i]
cumulative_G[i+1] = cumulative_G[i]
#print(cumulative_A)
#print(cumulative_C)
#print(cumulative_G)
M = len(P) # =len(Q)
result = [0] * M
for j in range( M ):
start_position = P[j]
end_position = Q[j] + 1 # inclusive
num_A = cumulative_A[end_position] - cumulative_A[start_position]
num_C = cumulative_C[end_position] - cumulative_C[start_position]
num_G = cumulative_G[end_position] - cumulative_G[start_position]
if num_A > 0:
result[j] = 1
elif num_C > 0:
result[j] = 2
elif num_G > 0:
result[j] = 3
else:
result[j] = 4
return result