-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMMPathProb.py
62 lines (52 loc) · 2.38 KB
/
HMMPathProb.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
# !/usr/bin/env python3
# HMM Set 1
# Compute the Probability of a Hidden Path
# University of California, Santa Cruz - BME 205
# Biomolecular Engineering and Bioinformatics
# Name: Zachary Mason (zmmason)
# Group Members: NONE
import sys
class ProbHiddenPath:
"""Compute the Probability of a Hidden Path."""
def __init__(self, pi, matrixA, matrixB):
"""Constructor: saves attributes from the input file."""
self.piPath = pi # saves the sequence
self.states = [matrixA[0], matrixB[0]] # saves the states of the sequence
matrixA = matrixA[1:].split() # parsing states matrix
matrixB = matrixB[1:].split() # parsing states matrix
# creating the transition matrix from each state matrix
self.transition = [[float(matrixA[0]), float(matrixA[1])], [float(matrixB[0]), float(matrixB[1])]]
def hiddenPath(self):
"""
Calculate the probability of a hidden path with the given HMM transition states assuming original
have equal probabilities (Pr(A) = Pr(B)).
"""
path = [self.states.index(state) for state in self.piPath] # creates list of states in path
probPi = 0.5 # initial probability - prob of obtaining a specific character (A,B -> 1/2, A,B,C -> 1/3, etc.)
prev = path[0] # holds the previous state in path for calculation of probabilities
for state in path[1:]:
trans_pr = self.transition[prev][state]
probPi = probPi * trans_pr # updates pi prob by getting the probability of the state * previous prob
prev = state # resets the 'prev' path index
return probPi
def main():
"""
Compute the Probability of a Hidden Path given a hidden path (pi), states of the path (states), and its
transition matrix of an HMM (transition).
"""
# sample input
# contents = ['AABBBAABABAAAABBBBAABBABABBBAABBAAAABABAABBABABBAB',
# '--------',
# 'A B',
# '--------',
# ' A B',
# 'A 0.194 0.806',
# 'B 0.273 0.727']
contents = [] # list to hold the contents of the dataset
for line in open('sample.txt'): # takes STDIN only
contents.append(line.strip())
path = ProbHiddenPath(contents[0], contents[5], contents[6])
pathProbability = path.hiddenPath()
print(pathProbability)
if __name__ == '__main__':
main()