-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
69 lines (53 loc) · 2.94 KB
/
test.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
63
64
65
66
67
68
69
from IGCexpansion.HMMTract import *
from IGCexpansion.CodonGeneconv import ReCodonGeneconv
import numdifftools as nd
if __name__ == '__main__':
pair = ["EDN", "ECP"]
paralog = pair
state_list = ['No IGC event (Si = 0)','At least one IGC event (Si > 0)']
newicktree = './input_tree.newick'
Force = None
output_ctrl = ''
summary_mat = []
print
print '**' + '_'.join(paralog)+ '**', output_ctrl
alignment_file = './EDN_ECP_Cleaned.fasta'
MG94_IGC = ReCodonGeneconv( newicktree, alignment_file, paralog, Model = 'MG94', Force = Force, clock = None)
IGC_sitewise_lnL_file = './Summary/' + '_'.join(paralog) + '_MG94_nonclock_sw_lnL.txt'
Force_sitewise_lnL_file = './Summary/Force_' + '_'.join(paralog) + '_MG94_nonclock_sw_lnL.txt'
Total_blen = sum([MG94_IGC.edge_to_blen[edge] for edge in MG94_IGC.edge_list if edge != ('N0', 'Tamarin')])
seq_index_file = './' + '_'.join(paralog) + '_seq_index.txt'
test = HMMTract(IGC_sitewise_lnL_file, Force_sitewise_lnL_file, state_list,
Total_blen, MG94_IGC.tau, seq_index_file)
MG94_IGC_lnL = -test.objective_1D(False, [0.0])
print
print 'Tract length 1 lnL: ', MG94_IGC_lnL, output_ctrl
if not "YNL069C" in paralog:
test.objective_1D(False, [np.log(3.0 / 200.0)])
result = test.get_mle(False)
# Now use numdifftools to get Hessian (it's rather 2nd derivative though)
#f = nd.Hessian(partial(test.objective_1D, False))
f = nd.Derivative(partial(test.objective_1D, False), n = 1)
ff = nd.Derivative(partial(test.objective_1D, False), n = 2)
#fisher_info = f(test.x[1:])[0,0]
first_deriv = -f(test.x[1:])
second_deriv = -ff(test.x[1:])
# store summary values
summary_mat.append([MG94_IGC_lnL, -result['fun'], 3.0 / test.tract_p,
test.get_marginal_state_distn()[0], test.get_marginal_state_distn()[1],
first_deriv, second_deriv])
lnL_array, Viterbi_path = test.Viterbi()
print 'Maximum lnL: ', -result['fun'], output_ctrl
print 'Estimated average tract length: ', 3.0 / test.tract_p, output_ctrl
print 'Distn of S: ', test.get_marginal_state_distn(), output_ctrl
print 'Viterbi path: ', ''.join([str(item) for item in Viterbi_path]), output_ctrl
lnL_arr = test.get_posterior()
np.savetxt('./summary/' + '_'.join(paralog) + '_MG94_nonclock_HMM_log_posterior_ratio.txt', lnL_arr[1, :] - lnL_arr[0, :])
np.savetxt('./summary/' + '_'.join(paralog) + '_MG94_nonclock_HMM_Viterbi_path.txt', Viterbi_path)
if True:
lnL_surf = []
tract_p_list = np.log(3.0 / np.arange(3, 501))
for ln_tract_p in tract_p_list:
lnL_surf.append(test.objective_1D(False, [ln_tract_p]))
np.savetxt('./summary/' + '_'.join(paralog) + '_MG94_nonclock_HMM_lnL_surface.txt', np.array(lnL_surf))
np.savetxt('./HMM_tract_MG94_nonclock_summary.txt', np.matrix(summary_mat), delimiter = '\t')