-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_extension.cpp
executable file
·182 lines (163 loc) · 8.59 KB
/
python_extension.cpp
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
181
//-------------------------------------------------------------------------------------
// Copyright 2014 Michael Peeri
//
// This file is part of hmmdsl.
// hmmdsl is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// hmmdsl is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with hmmdsl. If not, see <http://www.gnu.org/licenses/>.
//-------------------------------------------------------------------------------------
#include <boost/python.hpp>
#include "test_fasta.hpp"
#include "common.hpp"
#include "model.hpp"
#include "viterbi.hpp"
#include "forward.hpp"
#include "backward.hpp"
#include "baumwelch.hpp"
#include "emit.hpp"
#include "read_matrix.hpp"
#include "perturb_model.hpp"
using namespace boost::python;
char const* greet(size_t x)
{
static char const* msgs[] = { "Hello", "Boost.Python", "World!" };
if( x > 2)
throw std::range_error("greet: index out of range");
return msgs[x];
}
template<typename Algo>
void translator_SymbolNotInAlphabet(const typename MatrixModel<Algo>::SymbolNotInAlphabet& err)
{
PyErr_SetString(PyExc_LookupError, "HMM MatrixModel: Symbol not in alphabet");
}
BOOST_PYTHON_MODULE(hmmdsl_py)
{
def("greet", greet, "return one of 3 greetings" );
typedef baumwelch_algo::EM<algo_t,baumwelch_algo::MultipleSequenceBW<algo_t> > em_t;
typedef baumwelch_algo::EM<algo_hmm_t,baumwelch_algo::MultipleSequenceBW<algo_hmm_t> > em_hmm_t;
algo_t::probability_type (algo_t::model_type::*pfxe)(const algo_t::model_type::StateId& state, const algo_t::symbol_type& sym) const = &algo_t::model_type::e;
algo_hmm_t::probability_type (algo_hmm_t::model_type::*pfxe_hmm)(const algo_hmm_t::model_type::StateId& state, const algo_hmm_t::symbol_type& sym) const = &algo_hmm_t::model_type::e;
void (algo_t::model_type::*pfx_debug_print)() const = &algo_t::model_type::debug_print;
void (algo_hmm_t::model_type::*pfx_debug_print_hmm)() const = &algo_hmm_t::model_type::debug_print;
class_<algo_t::model_type>("Model")
.def("AddState", &algo_t::model_type::AddState)
.def("SetTransition", &algo_t::model_type::SetTransition)
.def("SetTransitionLogspace", &algo_t::model_type::SetTransitionLogspace)
.def("AddAlphabetSymbol", &algo_t::model_type::AddAlphabetSymbol)
.def("SetEmissionProbability", &algo_t::model_type::SetEmissionProbability)
.def("SetEmissionProbabilityLogspace", &algo_t::model_type::SetEmissionProbabilityLogspace)
.def("GetStateName", &algo_t::model_type::GetStateName)
.def("num_symbols", &algo_t::model_type::num_symbols)
.def("num_states", &algo_t::model_type::num_states)
.def("get_symbol", &algo_t::model_type::get_symbol)
.def("SetDefaultProbabilities", &algo_t::model_type::SetDefaultProbabilities)
.def("a", &algo_t::model_type::a)
.def("e", pfxe)
.def("debug_print", pfx_debug_print )
.def("is_silent", &algo_t::model_type::is_silent)
.def("GetInitialState", &algo_t::model_type::GetInitialState)
.def("GetTerminalState", &algo_t::model_type::GetTerminalState)
.def("SetFixedEmissionsState", &algo_t::model_type::SetFixedEmissionsState)
.def("MergeEmissionsClass", &algo_t::model_type::MergeEmissionsClass)
.def("GetEta", &algo_t::model_type::GetEta)
.def("GetMu", &algo_t::model_type::GetMu)
.def("SetEta", &algo_t::model_type::SetEta)
.def("SetMu", &algo_t::model_type::SetMu)
.def("emissions_entropy", &algo_t::model_type::emissions_entropy)
.def("split_state", &algo_t::model_type::split_state)
.def("GetStateLearningMode", &algo_t::model_type::GetStateLearningMode)
.def("SetStateLearningMode", &algo_t::model_type::SetStateLearningMode)
.def("GetAlphabet", &algo_t::model_type::GetAlphabet)
;
class_<algo_hmm_t::model_type>("HMMModel")
.def("AddState", &algo_hmm_t::model_type::AddState)
.def("SetTransition", &algo_hmm_t::model_type::SetTransition)
.def("SetTransitionLogspace", &algo_hmm_t::model_type::SetTransitionLogspace)
.def("AddAlphabetSymbol", &algo_hmm_t::model_type::AddAlphabetSymbol)
.def("SetEmissionProbability", &algo_hmm_t::model_type::SetEmissionProbability)
.def("SetEmissionProbabilityLogspace", &algo_hmm_t::model_type::SetEmissionProbabilityLogspace)
.def("GetStateName", &algo_hmm_t::model_type::GetStateName)
.def("num_symbols", &algo_hmm_t::model_type::num_symbols)
.def("num_states", &algo_hmm_t::model_type::num_states)
.def("get_symbol", &algo_hmm_t::model_type::get_symbol)
.def("SetDefaultProbabilities", &algo_hmm_t::model_type::SetDefaultProbabilities)
.def("a", &algo_hmm_t::model_type::a)
.def("e", pfxe_hmm)
.def("debug_print", pfx_debug_print_hmm )
.def("is_silent", &algo_hmm_t::model_type::is_silent)
.def("GetInitialState", &algo_hmm_t::model_type::GetInitialState)
.def("GetTerminalState", &algo_hmm_t::model_type::GetTerminalState)
.def("SetFixedEmissionsState", &algo_hmm_t::model_type::SetFixedEmissionsState)
.def("MergeEmissionsClass", &algo_hmm_t::model_type::MergeEmissionsClass)
.def("emissions_entropy", &algo_hmm_t::model_type::emissions_entropy)
.def("GetStateLearningMode", &algo_hmm_t::model_type::GetStateLearningMode)
.def("SetStateLearningMode", &algo_hmm_t::model_type::SetStateLearningMode)
.def("GetAlphabet", &algo_t::model_type::GetAlphabet)
;
class_<em_t>("EM", init<algo_t::model_type&, const std::string>() )
.def(init<algo_t::model_type&, const std::string, const boost::python::list&>() )
.def("reestimate_a", &em_t::reestimate_a)
.def("reestimate_b", &em_t::reestimate_b)
.def("reestimate_scale", &em_t::reestimate_scale)
.def("reestimate_shape", &em_t::reestimate_shape)
.def("debug_print", &em_t::debug_print)
.def("forward", &em_t::peek_forward)
.def("forward_begin", &em_t::peek_forward_begin)
.def("backward", &em_t::peek_backward)
.def("backward_begin", &em_t::peek_backward_begin)
.def("gamma", &em_t::peek_gamma)
.def("sigma_t_xi", &em_t::peek_sigma_t_xi)
;
class_<em_hmm_t>("HMMEM", init<algo_hmm_t::model_type&, const std::string>() )
.def(init<algo_hmm_t::model_type&, const std::string, const boost::python::list&>() )
.def("reestimate_a", &em_hmm_t::reestimate_a)
.def("reestimate_b", &em_hmm_t::reestimate_b)
.def("reestimate_scale", &em_hmm_t::reestimate_scale)
.def("reestimate_shape", &em_hmm_t::reestimate_shape)
.def("debug_print", &em_hmm_t::debug_print)
;
class_<random_emitter<algo_t> >("Emitter", init<const algo_t::model_type&>() )
.def("emit", &random_emitter<algo_t>::emit)
.def("emit_fixed_length", &random_emitter<algo_t>::emit_fixed_length)
.def("test_random_transition", &random_emitter<algo_t>::test_random_transition)
;
class_<boost::multi_array<algo_t::probability_type,2> >("Matrix")
;
class_<perturb_model<algo_t> >("Perturb", init<algo_t::model_type&>() )
.def("perturb_emissions", &perturb_model<algo_t>::perturb_emissions)
;
class_<perturb_model<algo_hmm_t> >("Perturb_hmm", init<algo_hmm_t::model_type&>() )
.def("perturb_emissions", &perturb_model<algo_hmm_t>::perturb_emissions)
;
def("test_model", test_model<algo_t> );
def("train_model", train_model<algo_t> );
def("score_sequences", score_sequences<algo_t> );
def("score_sequences2", score_sequences2<algo_t> );
def("InitializeNullModel", InitializeNullModel<algo_t::model_type> );
// def("test_vector", test_vector ); // Test for output to python arrays
def("test_read_matrix_sum", read_matrix::test_read_matrix_sum<double> );
def("read_matrix", read_matrix::read_matrix<double> );
def("count_fasta", fasta::count_fasta );
def("relax_emissions", relax_emissions<algo_t>);
register_exception_translator<MatrixModel<algo_t>::SymbolNotInAlphabet>(&translator_SymbolNotInAlphabet<algo_t>);
def("align_viterbi", align_viterbi<algo_t> );
def("print_model_statistics", print_model_statistics<algo_t> );
def("test_model_hmm", test_model<algo_hmm_t> );
def("train_model_hmm", train_model<algo_hmm_t> );
def("score_sequences_hmm", score_sequences<algo_hmm_t> );
def("score_sequences2_hmm", score_sequences2<algo_hmm_t> );
def("InitializeNullModel_hmm", InitializeNullModel<algo_hmm_t::model_type> );
// def("test_vector", test_vector ); // Test for output to python arrays
def("relax_emissions_hmm", relax_emissions<algo_hmm_t>);
register_exception_translator<MatrixModel<algo_hmm_t>::SymbolNotInAlphabet>(&translator_SymbolNotInAlphabet<algo_hmm_t>);
def("align_viterbi_hmm", align_viterbi<algo_hmm_t> );
}