-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
136 lines (123 loc) · 4.08 KB
/
markov.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import random as rm
import os
from progressbar import ProgressBar
pbar = ProgressBar()
os.system('cls')
states = ['T', 'C', 'A', 'G']
transitionName = [['TT', 'TC', 'TA', 'TG'], ['CT', 'CC', 'CA', 'CG'],
['AT', 'AC', 'AA', 'AG'], ['GT', 'GC', 'GA', 'GG']]
T = []
C = []
A = []
G = []
transitionMatrix = [T, C, A, G]
lam = 7.3333 * 10 ** -10
years = int(input('Enter the number of years to simulate the site over: '))
its = int(input('Enter the number of iterations to run: '))
for base in states:
if base == 'T':
T.append(1 - (3 * lam * years))
else:
T.append(lam * years)
for base in states:
if base == 'C':
C.append(1 - (3 * lam * years))
else:
C.append(lam * years)
for base in states:
if base == 'A':
A.append(1 - (3 * lam * years))
else:
A.append(lam * years)
for base in states:
if base == 'G':
G.append(1 - (3 * lam * years))
else:
G.append(lam * years)
startingBase = str(input('Enter the starting base: '))
def basePrediction(steps):
currentBase = startingBase
sequence = [currentBase]
i = 0
prob = 1
while i != steps:
if currentBase == 'T':
transition = np.random.choice(transitionName[0], replace=True, p=transitionMatrix[0])
if transition == 'TT':
sequence.append('T')
elif transition == 'TC':
currentBase = 'C'
sequence.append('C')
elif transition == 'TA':
currentBase = 'A'
sequence.append('A')
else:
currentBase = 'G'
sequence.append('G')
elif currentBase == 'C':
transition = np.random.choice(transitionName[1], replace=True, p=transitionMatrix[1])
if transition == 'CT':
currentBase = 'T'
sequence.append('T')
elif transition == 'CC':
sequence.append('C')
elif transition == 'CA':
currentBase = 'A'
sequence.append('A')
else:
currentBase = 'G'
sequence.append('G')
elif currentBase == 'A':
transition = np.random.choice(transitionName[2], replace=True, p=transitionMatrix[2])
if transition == 'AT':
currentBase = 'T'
sequence.append('T')
elif transition == 'AC':
currentBase = 'C'
sequence.append('C')
elif transition == 'AA':
sequence.append('A')
else:
currentBase = 'G'
sequence.append('G')
elif currentBase == 'G':
transition = np.random.choice(transitionName[3], replace=True, p=transitionMatrix[3])
if transition == 'GT':
currentBase = 'T'
sequence.append('T')
elif transition == 'GC':
currentBase = 'C'
sequence.append('C')
elif transition == 'GA':
currentBase = 'A'
sequence.append('A')
else:
sequence.append('G')
i += 1
return sequence
sequenceComp = []
countT = 0
countC = 0
countA = 0
countG = 0
print('Working...')
for iterations in pbar(range(0, its)):
sequenceComp.append(basePrediction(years))
for sequences in sequenceComp:
if sequences[-1] == 'T':
countT += 1
elif sequences[-1] == 'C':
countC += 1
elif sequences[-1] == 'A':
countA += 1
else:
countG += 1
percT = (countT / its) * 100
percC = (countC / its) * 100
percA = (countA / its) * 100
percG = (countG / its) * 100
print('The probability of starting at base {} and ending at base T is: '.format(startingBase), percT, '%')
print('The probability of starting at base {} and ending at base C is: '.format(startingBase), percC, '%')
print('The probability of starting at base {} and ending at base A is: '.format(startingBase), percA, '%')
print('The probability of starting at base {} and ending at base G is: '.format(startingBase), percG, '%')