-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkov.py
More file actions
58 lines (44 loc) · 1.68 KB
/
Copy pathmarkov.py
File metadata and controls
58 lines (44 loc) · 1.68 KB
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
#! /usr/bin/env python
#-*- coding:utf-8 -*-
#import plotly.offline as py #Dé-commentez si vous avez plotly
#import plotly.graph_objs as go #idem
import math
#py.init_notebook_mode() #idem
def weight_matrix(possibilities, data_str):
matrix = [[0]*len(possibilities) for _ in range(len(possibilities))]
last_c = 0
for c in data_str:
if last_c != 0:
matrix[possibilities.index(c)][possibilities.index(last_c)] +=1.0/len(data_str)
last_c = c
return matrix
# def make_heatmap(matrix,axis_labels,filename): #Décommentez si vous avez plotly
# data = [go.Heatmap(z=matrix,x=axis_labels,y=axis_labels)]
# py.plot(data, filename=filename)
def get_string_from_fasta_file(fasta_name):
ret_str = ""
with open(fasta_name,"r") as feb:
next(feb)
for line in feb:
ret_str+= line
ret_str = ret_str.replace('\n','')
return ret_str
def compute_log_likelihood(m,possibilities,data_str):
last_c = 0
log_sum = 0
for c in data_str:
if last_c != 0:
log_sum += math.log(m[possibilities.index(c)][possibilities.index(last_c)])
last_c = c
return log_sum
if __name__ == '__main__':
poss = ['A','T','C','G']
ebola = get_string_from_fasta_file("ebola.fasta")
narna = get_string_from_fasta_file("narna.fasta")
unknown = get_string_from_fasta_file("unknown.fasta")
m_eb = weight_matrix(poss, ebola)
#make_heatmap(m_eb,poss,"ebola") #Dé-commentez si vous avez installé plotly
m_na = weight_matrix(poss,narna)
#make_heatmap(m_na,poss,"narna") #idem
print "Ebola likelihood : ",compute_log_likelihood(m_eb, poss, unknown)
print "Narna likelihood : ",compute_log_likelihood(m_na, poss, unknown)