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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#!/usr/bin/env python
import StringIO
from operator import truth
from Bio import MarkovModel
from Numeric import ones, zeros, log, asarray
def print_mm(markov_model):
print "STATES: %s" % ' '.join(markov_model.states)
print "ALPHABET: %s" % ' '.join(markov_model.alphabet)
print "INITIAL:"
for i in range(len(markov_model.p_initial)):
print " %s: %.2f" % (
markov_model.states[i], markov_model.p_initial[i])
print "TRANSITION:"
for i in range(len(markov_model.p_transition)):
x = ["%.2f" % x for x in markov_model.p_transition[i]]
print " %s: %s" % (markov_model.states[i], ' '.join(x))
print "EMISSION:"
for i in range(len(markov_model.p_emission)):
x = ["%.2f" % x for x in markov_model.p_emission[i]]
print " %s: %s" % (markov_model.states[i], ' '.join(x))
print "TESTING train_visible"
states = ["0", "1", "2", "3"]
alphabet = ["A", "C", "G", "T"]
training_data = [
("AACCCGGGTTTTTTT", "001112223333333"),
("ACCGTTTTTTT", "01123333333"),
("ACGGGTTTTTT", "01222333333"),
("ACCGTTTTTTTT", "011233333333"),
]
print "Training HMM"
mm = MarkovModel.train_visible(states, alphabet, training_data)
print "Classifying"
print MarkovModel.find_states(mm, "AACGTT")
print_mm(mm)
print "TESTING baum welch"
states = ["CP", "IP"]
alphabet = ["cola", "ice_t", "lem"]
outputs = [
(2, 1, 0)
]
print "Training HMM"
p_initial = [1.0, 0.0000001]
p_transition = [[0.7, 0.3],
[0.5, 0.5]]
p_emission = [[0.6, 0.1, 0.3],
[0.1, 0.7, 0.2]]
N, M = len(states), len(alphabet)
x = MarkovModel._baum_welch(N, M, outputs,
p_initial=p_initial,
p_transition=p_transition,
p_emission=p_emission
)
p_initial, p_transition, p_emission = x
mm = MarkovModel.MarkovModel(states, alphabet,
p_initial, p_transition, p_emission)
print_mm(mm)
# Test Baum-Welch. This is hard because it is a non-deterministic
# algorithm. Each run will result in different states having to
# different emissions. In order to help this, we need to specify some
# initial probabilities to bias the final results. This is not
# implemented yet in the MarkovModel module.
## states = [
## "state0",
## "state1",
## "state2",
## "state3",
## ]
## alphabet = ["a", "c", "g", "t"]
## training_data = [
## "aacccgggttttttt",
## "accgttttttt",
## "acgggtttttt",
## "accgtttttttt",
## "aaccgtttttttt",
## "aacggttttt",
## "acccggttttt",
## "acccggggttt",
## "aacccggggtttt",
## "aaccgggtttttt"
## ]
## print "TRAINING HMM"
## ep = ones((len(states), len(alphabet)))
## hmm = MarkovModel.train_bw(states, alphabet, training_data,
## pseudo_emission=ep)
## print "CLASSIFYING"
## states = MarkovModel.find_states(hmm, "aacgtt")
## print states
## print "STATES: %s" % ' '.join(hmm.states)
## print "ALPHABET: %s" % ' '.join(hmm.alphabet)
## print "INITIAL:"
## for i in range(len(hmm.p_initial)):
## print " %s: %.2f" % (hmm.states[i], hmm.p_initial[i])
## print "TRANSITION:"
## for i in range(len(hmm.p_transition)):
## x = ["%.2f" % x for x in hmm.p_transition[i]]
## print " %s: %s" % (hmm.states[i], ' '.join(x))
## print "EMISSION:"
## for i in range(len(hmm.p_emission)):
## x = ["%.2f" % x for x in hmm.p_emission[i]]
## print " %s: %s" % (hmm.states[i], ' '.join(x))
# Do some tests from the topcoder competition.
class DNAStrand:
def mostLikely(self, normal, island, dnastrand):
states = "NR"
alphabet = "AGTC"
normal = [float(x)/100 for x in normal]
island = [float(x)/100 for x in island]
p_initial = [1.0, 0.0]
p_initial = asarray(p_initial)
p_transition = []
p_transition.append([1.0-normal[-1], normal[-1]])
p_transition.append([island[-1], 1.0-island[-1]])
p_transition = asarray(p_transition)
p_emission = [] # 2x4 matrix
p_emission.append(normal[:4])
p_emission.append(island[:4])
p_emission = asarray(p_emission)
mm = MarkovModel.MarkovModel(
states, alphabet, p_initial, p_transition, p_emission)
x = MarkovModel.find_states(mm, dnastrand)
states, x = x[0]
return ''.join(states)
ds = DNAStrand()
# NNNN
print ds.mostLikely([30, 20, 30, 20, 10],
[10, 40, 10, 40, 20],
"TGCC"
)
# NNNRRRNNRRNRRN
print ds.mostLikely([4, 14, 62, 20, 44],
[39, 15, 4, 42, 25],
"CCTGAGTTAGTCGT"
)
# NRRRRRRRRRRRNNNNRRRRRRRRR
print ds.mostLikely([45, 36, 6, 13, 25],
[24, 18, 12, 46, 25],
"CCGTACTTACCCAGGACCGCAGTCC"
)
# NRRRRRRRRRR
print ds.mostLikely([75,3,1,21,45],
[34,11,39,16,15],
"TTAGCAGTGCG"
)
# N
print ds.mostLikely([26,37,8,29,16],
[31,13,33,23,25],
"T"
)
|