本文整理汇总了Python中Bio.MarkovModel类的典型用法代码示例。如果您正苦于以下问题:Python MarkovModel类的具体用法?Python MarkovModel怎么用?Python MarkovModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MarkovModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_save_and_load
def test_save_and_load(self):
states = "NR"
alphabet = "AGTC"
p_initial = array([1.0, 0.0])
p_transition = array([[0.75, 0.25], [0.25, 0.75]])
p_emission = array(
[[0.45, 0.36, 0.06, 0.13], [0.24, 0.18, 0.12, 0.46]])
markov_model_save = MarkovModel.MarkovModel(
states,
alphabet,
p_initial,
p_transition,
p_emission)
handle = StringIO()
MarkovModel.save(markov_model_save, handle)
handle.seek(0)
markov_model_load = MarkovModel.load(handle)
self.assertEqual(''.join(markov_model_load.states), states)
self.assertEqual(''.join(markov_model_load.alphabet), alphabet)
self.assertTrue(array_equal(markov_model_load.p_initial, p_initial))
self.assertTrue(array_equal
(markov_model_load.p_transition, p_transition))
self.assertTrue(array_equal(markov_model_load.p_emission, p_emission))
示例2: test_logvecadd
def test_logvecadd(self):
vec1 = log(array([1, 2, 3, 4]))
vec2 = log(array([5, 6, 7, 8]))
sumvec = array([1.79175947, 2.07944154, 2.30258509, 2.48490665])
self.assertTrue(
array_equal(around(MarkovModel._logvecadd(vec1, vec2), decimals=3), around(sumvec, decimals=3)))
示例3: _create_mm
def _create_mm(self, motif_num, alphabet):
try:
# Only EDeN has original_motives_list
input_motif = self.original_motives_list[motif_num - 1]
except AttributeError:
input_motif = self.motives_list[motif_num - 1]
headers, instances = [list(x) for x in zip(*input_motif)]
lengths = [len(instances[i]) for i in range(len(instances))]
median_len = int(math.ceil(np.median(lengths)))
# Hidden states for Markov Model
states = [str(i + 1) for i in range(median_len)]
print "original samples: %d" % len(instances)
print "states:", len(states)
# under sampling
if (len(instances) * len(states)) > 500:
samples = 500 / len(states)
# samples = 50 # fixed sampling
print 'sample size = %d' % samples
instances = random.sample(instances, samples)
instances = random.sample(instances, samples)
try:
mm = MarkovModel.train_bw(states=states,
alphabet=alphabet,
training_data=instances)
except RuntimeError, msg:
raise RuntimeError("Motif data is too large. " + str(msg))
示例4: test_mle
def test_mle(self):
states = ["0", "1", "2", "3"]
alphabet = ["A", "C", "G", "T"]
training_data = [("AACCCGGGTTTTTTT", "001112223333333"),
("ACCGTTTTTTT", "01123333333"),
("ACGGGTTTTTT", "01222333333"),
("ACCGTTTTTTTT", "011233333333"), ]
training_outputs = array([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3], [
0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3], [0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3], [0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3]])
training_states = array([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3], [
0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3], [0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3], [0, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3]])
p_initial = array([1., 0., 0., 0.])
p_transition = array([[0.2, 0.8, 0., 0.],
[0., 0.5, 0.5, 0.],
[0., 0., 0.5, 0.5],
[0., 0., 0., 1.]])
p_emission = array(
[[0.66666667, 0.11111111, 0.11111111, 0.11111111],
[0.08333333, 0.75, 0.08333333, 0.08333333],
[0.08333333, 0.08333333, 0.75, 0.08333333],
[0.03125, 0.03125, 0.03125, 0.90625]])
p_initial_out, p_transition_out, p_emission_out = MarkovModel._mle(
len(states), len(alphabet), training_outputs, training_states, None, None, None)
self.assertTrue(
array_equal(around(p_initial_out, decimals=3), around(p_initial, decimals=3)))
self.assertTrue(
array_equal(around(p_transition_out, decimals=3), around(p_transition, decimals=3)))
self.assertTrue(
array_equal(around(p_emission_out, decimals=3), around(p_emission, decimals=3)))
示例5: mostLikely
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)
示例6: test_uniform_norm
def test_uniform_norm(self):
shape = (4, 3)
matrix = array([[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333],
[0.33333333, 0.33333333, 0.33333333]])
self.assertTrue(
array_equal(around(MarkovModel._uniform_norm(shape), decimals=3), around(matrix, decimals=3)))
示例7: test_random_norm
def test_random_norm(self):
random.seed(0)
shape = (4, 3)
matrix = array([[0.29399155, 0.38311672, 0.32289173],
[0.33750765, 0.26241723, 0.40007512],
[0.1908342, 0.38890714, 0.42025866],
[0.22501625, 0.46461061, 0.31037314]])
self.assertTrue(
array_equal(around(MarkovModel._random_norm(shape), decimals=3), around(matrix, decimals=3)))
示例8: test_normalize_and_copy_and_check
def test_normalize_and_copy_and_check(self):
matrix_in1 = array(
[[1.1, 2.2, 3.3], [4.4, 5.5, 6.6], [7.7, 8.8, 9.9]])
matrix_in2 = array([1, 2, 3])
matrix_out1 = array(
[[0.16666667, 0.33333333, 0.5], [0.26666667, 0.33333333, 0.4], [0.29166667, 0.33333333, 0.375]])
matrix_out2 = array([0.16666667, 0.33333333, 0.5])
self.assertTrue(
array_equal(around(MarkovModel._normalize(matrix_in1), decimals=3), around(matrix_out1, decimals=3)))
self.assertTrue(
array_equal(around(MarkovModel._normalize(matrix_in2), decimals=3), around(matrix_out2, decimals=3)))
shape1 = (3, 3)
shape2 = (3,)
self.assertTrue(
array_equal(around(MarkovModel._copy_and_check(matrix_out1, shape1), decimals=3), around(matrix_out1, decimals=3)))
self.assertTrue(
array_equal(around(MarkovModel._copy_and_check(matrix_out2, shape2), decimals=3), around(matrix_out2, decimals=3)))
示例9: test_readline_and_check_start
def test_readline_and_check_start(self):
states = "NR"
alphabet = "AGTC"
markov_model = MarkovModel.MarkovModel(states, alphabet)
line = "This is a \n string with two lines \n"
handle = StringIO(line)
start = "This is a \n"
self.assertEqual(
start, MarkovModel._readline_and_check_start(handle, start))
示例10: test_backward
def test_backward(self):
states = ["CP", "IP"]
outputs = [2, 1, 0]
lp_transition = log([[0.7, 0.3], [0.5, 0.5]])
lp_emission = log([[0.6, 0.1, 0.3], [0.1, 0.7, 0.2]])
matrix = array([[-3.45776773, -3.10109279, -0.51082562, 0.],
[-3.54045945, -1.40649707, -2.30258509, 0.]])
self.assertTrue(
array_equal(around(MarkovModel._backward(
len(states), len(outputs), lp_transition, lp_emission, outputs), decimals=3),
around(matrix, decimals=3)))
示例11: test_logsum_and_exp_logsum
def test_logsum_and_exp_logsum(self):
matrix = array(
[[1.1, 2.2, 3.3], [4.4, 5.5, 6.6], [7.7, 8.8, 9.9]])
matrix1 = array([1, 2, 3])
output = 10.304721798
output1 = 3.40760596444
self.assertEqual(
float('%.3f' % MarkovModel._logsum(matrix)),
float('%.3f' % output))
self.assertEqual(
float('%.3f' % MarkovModel._logsum(matrix1)),
float('%.3f' % output1))
output2 = 29873.342245
output3 = 30.1928748506
self.assertEqual(
float('%.3f' % MarkovModel._exp_logsum(matrix)),
float('%.3f' % output2))
self.assertEqual(
float('%.3f' % MarkovModel._exp_logsum(matrix1)),
float('%.3f' % output3))
示例12: test_topcoder5
def test_topcoder5(self):
# N
states = "NR"
alphabet = "AGTC"
p_initial = array([1.0, 0.0])
p_transition = array([[0.84, 0.16],
[0.25, 0.75]])
p_emission = array([[0.26, 0.37, 0.08, 0.29],
[0.31, 0.13, 0.33, 0.23]])
markov_model = MarkovModel.MarkovModel(
states, alphabet, p_initial, p_transition, p_emission)
states = MarkovModel.find_states(markov_model, "T")
self.assertEqual(len(states), 1)
state_list, state_float = states[0]
self.assertEqual(state_list, ["N"])
示例13: test_topcoder4
def test_topcoder4(self):
# NRRRRRRRRRR
states = "NR"
alphabet = "AGTC"
p_initial = array([1.0, 0.0])
p_transition = array([[0.55, 0.45],
[0.15, 0.85]])
p_emission = array([[0.75, 0.03, 0.01, 0.21],
[0.34, 0.11, 0.39, 0.16]])
markov_model = MarkovModel.MarkovModel(
states, alphabet, p_initial, p_transition, p_emission)
states = MarkovModel.find_states(markov_model, "TTAGCAGTGCG")
self.assertEqual(len(states), 1)
state_list, state_float = states[0]
self.assertEqual(state_list, ['N', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'])
示例14: test_topcoder3
def test_topcoder3(self):
# NRRRRRRRRRRRNNNNRRRRRRRRR
states = "NR"
alphabet = "AGTC"
p_initial = array([1.0, 0.0])
p_transition = array([[0.75, 0.25],
[0.25, 0.75]])
p_emission = array([[0.45, 0.36, 0.06, 0.13],
[0.24, 0.18, 0.12, 0.46]])
markov_model = MarkovModel.MarkovModel(
states, alphabet, p_initial, p_transition, p_emission)
states = MarkovModel.find_states(markov_model, "CCGTACTTACCCAGGACCGCAGTCC")
self.assertEqual(len(states), 1)
state_list, state_float = states[0]
self.assertEqual(state_list, ['N', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'N', 'N', 'N', 'N', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'])
示例15: test_topcoder2
def test_topcoder2(self):
# NNNRRRNNRRNRRN
states = "NR"
alphabet = "AGTC"
p_initial = array([1.0, 0.0])
p_transition = array([[0.56, 0.44],
[0.25, 0.75]])
p_emission = array([[0.04, 0.14, 0.62, 0.20],
[0.39, 0.15, 0.04, 0.42]])
markov_model = MarkovModel.MarkovModel(
states, alphabet, p_initial, p_transition, p_emission)
states = MarkovModel.find_states(markov_model, "CCTGAGTTAGTCGT")
self.assertEqual(len(states), 1)
state_list, state_float = states[0]
self.assertEqual(state_list, ['N', 'N', 'N', 'R', 'R', 'R', 'N', 'N', 'R', 'R', 'N', 'R', 'R', 'N'])