I'm using a 1st step Transition matrix to generate the DNA sequences. Now I need to give a probability to the transition matrix to change every 1000 steps. Let's say, every 1000 steps, there is 40% probability the transition matrix will change. Every row should add to 1 after the change. Now I don't know how to access the value in the nested dictionary data in python, and how to implement the 40% probability changing. I attached my code here, any suggestion is appreciated ~
#!/usr/bin/env python
import sys, random
length = 10000
tran_matrix = {'a': {'a':0.495,'c':0.113,'g':0.129,'t':0.263},
'c': {'a':0.129,'c':0.063,'g':0.413,'t':0.395},
't': {'a':0.213,'c':0.495,'g':0.263,'t':0.029},
'g': {'a':0.263,'c':0.129,'g':0.295,'t':0.313}}
initial_p = {'a':0.25,'c':0.25,'t':0.25,'g':0.25}
def choose(dist):
r = random.random()
sum = 0.0
keys = dist.keys()
for k in keys:
sum += dist[k]
if sum > r:
return k
return keys[-1]
c = choose(initial_p)
for i in range(length):
sys.stdout.write(c)
c = choose(tran_matrix[c])