70 std::cerr <<
"Can't allocate Posterior score table. OUT OF MEMORY" << std::endl;
74 std::bitset<STATE_MAX> next_states;
75 std::bitset<STATE_MAX> current_states;
77 double forward_temp(-INFINITY);
78 double emission(-INFINITY);
79 bool exDef_position(
false);
84 std::bitset<STATE_MAX>* from_trans(NULL);
89 if ((*initial_to)[i]){
92 if (forward_temp > -INFINITY){
93 (*scoring_current)[i] = forward_temp;
94 next_states |= (*(*hmm)[i]->getTo());
100 for(
size_t position = 1; position <
seq_size ; ++position ){
110 current_states.reset();
111 current_states |= next_states;
118 for (
size_t current = 0; current <
state_size; ++current){
119 if (!current_states[current]){
123 emission = (*hmm)[current]->get_emission_prob(*
seqs, position);
129 if (emission == -INFINITY){
133 from_trans = (*hmm)[current]->getFrom();
135 for (
size_t previous = 0; previous <
state_size ; ++previous) {
136 if (!(*from_trans)[previous]){
143 forward_temp = (*scoring_previous)[previous] + emission +
getTransition((*
hmm)[previous], current , position);
146 (*scoring_current)[current] = forward_temp;
152 next_states |= (*(*hmm)[current]->getTo());
170 forward_temp = (*scoring_previous)[i] + (*hmm)[i]->getEndTrans();
172 if (forward_temp > -INFINITY){
184 double backward_temp(-INFINITY);
189 std::vector<double> posterior_sum(seq_size,-INFINITY);
195 for(
size_t st_current = 0; st_current <
state_size; ++st_current){
196 if ((*ending_from)[st_current]){
198 backward_temp = (*hmm)[st_current]->getEndTrans();
200 if (backward_temp > -INFINITY){
201 (*scoring_current)[st_current] = backward_temp;
202 next_states[st_current] = 1;
207 for(
size_t position = seq_size-2; position !=
SIZE_MAX ; --position ){
210 current_states.reset();
211 current_states |= next_states;
236 for (
size_t st_previous = 0; st_previous <
state_size; ++st_previous){
237 if (!current_states[st_previous]){
241 emission = (*hmm)[st_previous]->get_emission_prob(*
seqs, position+1);
247 if (emission == -INFINITY){
251 from_trans = (*hmm)[st_previous]->getFrom();
253 for (
size_t st_current = 0; st_current <
state_size ; ++st_current) {
254 if (!(*from_trans)[st_current]){
260 backward_temp =(*scoring_previous)[st_previous] + emission +
getTransition((*
hmm)[st_current], st_previous , position);
263 (*scoring_current)[st_current] = backward_temp;
269 next_states[st_current] = 1;
289 if (backward_temp > -INFINITY){
301 std::cerr <<
"Ending sequence probabilities calculated by Forward and Backward algorithm are different. They should be the same.\t" << __FUNCTION__ << std::endl;
315 (*posterior_score)[i][j] -= posterior_sum[i];
318 (*posterior_score)[i][j] = -INFINITY;
333 std::cerr << __FUNCTION__ <<
" called before trellis::posterior was completed\n";
337 double max(-INFINITY);
345 max = (*posterior_score)[position][st];
357 double random=((double)rand()/((double)(RAND_MAX)+(double)(1)));
358 double cumulative_prob(0.0);
362 if (random < cumulative_prob){
371 for (
size_t i = 0; i < reps; i++){