StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes
StochHMM::trellis Class Reference

#include <trellis.h>

List of all members.

Public Member Functions

 trellis ()
 trellis (model *h, sequences *sqs)
 ~trellis ()
void reset ()
modelgetModel ()
sequencesgetSeq ()
void viterbi ()
void viterbi (model *h, sequences *sqs)
void forward ()
void forward (model *h, sequences *sqs)
void backward ()
void backward (model *h, sequences *sqs)
void posterior ()
void posterior (model *h, sequences *sqs)
void stochastic_viterbi ()
void stochastic_viterbi (model *h, sequences *sqs)
void stochastic_forward ()
void stochastic_forward (model *h, sequences *sqs)
void nth_viterbi ()
void nth_viterbi (model *h, sequences *sqs)
void baum_welch ()
void naive_forward ()
void naive_forward (model *h, sequences *sqs)
void naive_backward ()
void naive_backward (model *h, sequences *sqs)
void naive_viterbi ()
void naive_viterbi (model *h, sequences *sqs)
void naive_baum_welch ()
void naive_baum_welch (model *h, sequences *sqs)
void naive_stochastic_viterbi ()
void naive_stochastic_viterbi (model *h, sequences *sqs)
void naive_stochastic_forward ()
void naive_stochastic_forward (model *h, sequences *sqs)
void naive_nth_viterbi (size_t n)
void naive_nth_viterbi (model *h, sequences *sqs, size_t n)
void simple_viterbi ()
void simple_viterbi (model *h, sequences *sqs)
void simple_forward ()
void simple_forward (model *h, sequences *sqs)
void simple_backward ()
void simple_backward (model *h, sequences *sqs)
void simple_posterior ()
void simple_posterior (model *h, sequences *sqs)
void simple_stochastic_viterbi ()
void simple_stochastic_viterbi (model *h, sequences *sqs)
void simple_simple_stochastic_viterbi (model *h, sequences *sqs)
void simple_stochastic_forward ()
void simple_stochastic_forward (model *h, sequences *sqs)
void simple_baum_welch ()
void simple_baum_welch (model *h, sequences *sqs)
void simple_nth_viterbi (size_t n)
void simple_nth_viterbi (model *h, sequences *sqs, size_t n)
void fast_complex_viterbi ()
void fast_complex_viterbi (model *h, sequences *sqs)
void fast_complex_backward ()
void fast_complex_backward (model *h, sequences *sqs)
void fast_complex_stochastic_viterbi ()
void fast_complex_stochastic_viterbi (model *h, sequences *sqs)
void fast_complex_stochastic_forward ()
void fast_complex_stochastic_forward (model *h, sequences *sqs)
void fast_complex_baum_welch ()
void fast_complex_baum_welch (model *h, sequences *sqs)
void sparse_complex_viterbi ()
void sparse_complex_viterbi (model *h, sequences *sqs)
void sparse_complex_backward ()
void sparse_complex_backward (model *h, sequences *sqs)
void sparse_complex_stochastic_forward ()
void sparse_complex_stochastic_forward (model *h, sequences *sqs)
void sparse_complex_baum_welch ()
void sparse_complex_baum_welch (model *h, sequences *sqs)
void traceback (traceback_path &path)
void traceback (traceback_path &path, size_t position, size_t state)
void traceback_nth (traceback_path &path, size_t n)
void traceback_posterior (traceback_path &path)
void traceback_stoch_posterior (traceback_path &path)
void traceback_stoch_posterior (multiTraceback &paths, size_t reps)
void traceback_nth_viterbi (multiTraceback &)
void stochastic_traceback (traceback_path &path)
void stochastic_traceback (multiTraceback &, size_t)
bool store ()
void store (bool val)
void print ()
std::string stringify ()
void export_trellis (std::ifstream &)
void export_trellis (std::string &file)
modelget_model ()
void update_transitions ()
void update_emissions ()
double_2Dget_naive_forward_scores ()
double_2Dget_naive_backward_scores ()
double_2Dget_naive_viterbi_scores ()
double_2Dget_dbl_posterior ()
float_2DgetForwardTable ()
float_2DgetBackwardTable ()
double_2DgetPosteriorTable ()
double getForwardProbability ()
double getBackwardProbability ()

Private Member Functions

double getEndingTransition (size_t)
double getTransition (state *st, size_t trans_to_state, size_t sequencePosition)
size_t get_explicit_duration_length (transition *trans, size_t sequencePosition, size_t state_iter, size_t to_state)
double transitionFuncTraceback (state *st, size_t position, transitionFuncParam *func)

Private Attributes

modelhmm
sequencesseqs
size_t nth_size
size_t state_size
size_t seq_size
trellisType type
bool store_values
bool exDef_defined
int_2Dtraceback_table
stochTablestochastic_table
alt_simple_stochTablealt_simple_stochastic_table
float_2Dviterbi_score
float_2Dforward_score
float_2Dbackward_score
double_2Dposterior_score
double_2Ddbl_forward_score
double_2Ddbl_viterbi_score
double_2Ddbl_backward_score
double_2Ddbl_posterior_score
double_3Ddbl_baum_welch_score
std::vector< std::vector
< std::vector< nthScore > * > > * 
naive_nth_scores
std::vector< nthTrace > * nth_traceback_table
double ending_viterbi_score
int16_t ending_viterbi_tb
double ending_forward_prob
double ending_backward_prob
std::vector< nthScore > * ending_nth_viterbi
std::vector< double > * scoring_current
std::vector< double > * scoring_previous
std::vector< double > * alt_scoring_current
std::vector< double > * alt_scoring_previous
std::vector< size_t > * explicit_duration_current
std::vector< size_t > * explicit_duration_previous
std::vector< size_t > * swap_ptr_duration
std::vector< double > * swap_ptr
std::vector< std::vector
< double > * > 
complex_emissions
std::vector< std::vector
< std::map< uint16_t, double > * > * > * 
complex_transitions
std::vector< std::vector
< nthScore > > * 
nth_scoring_current
std::vector< std::vector
< nthScore > > * 
nth_scoring_previous
std::vector< std::vector
< nthScore > > * 
nth_swap_ptr

Detailed Description

Definition at line 103 of file trellis.h.


Constructor & Destructor Documentation

StochHMM::trellis::trellis ( )
StochHMM::trellis::trellis ( model h,
sequences sqs 
)

Definition at line 55 of file trellis.cpp.

References alt_scoring_current, alt_scoring_previous, backward_score, dbl_backward_score, dbl_forward_score, dbl_posterior_score, dbl_viterbi_score, ending_backward_prob, ending_forward_prob, ending_nth_viterbi, ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), forward_score, StochHMM::sequences::getLength(), hmm, naive_nth_scores, nth_scoring_current, nth_scoring_previous, nth_size, nth_traceback_table, posterior_score, scoring_current, scoring_previous, seq_size, seqs, StochHMM::SIMPLE, SIZE_MAX, StochHMM::model::state_size(), state_size, stochastic_table, store_values, traceback_table, type, and viterbi_score.

StochHMM::trellis::~trellis ( )

Member Function Documentation

void StochHMM::trellis::backward ( )

Definition at line 25 of file backward.cpp.

References simple_backward().

Referenced by backward().

{
//if (hmm->isBasic()){
//}
}
void StochHMM::trellis::backward ( model h,
sequences sqs 
)
void StochHMM::trellis::baum_welch ( )
void StochHMM::trellis::export_trellis ( std::ifstream &  )
void StochHMM::trellis::export_trellis ( std::string &  file)
void StochHMM::trellis::fast_complex_backward ( )
void StochHMM::trellis::fast_complex_backward ( model h,
sequences sqs 
)
void StochHMM::trellis::fast_complex_baum_welch ( )
void StochHMM::trellis::fast_complex_baum_welch ( model h,
sequences sqs 
)
void StochHMM::trellis::fast_complex_stochastic_forward ( )
void StochHMM::trellis::fast_complex_stochastic_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::fast_complex_stochastic_viterbi ( )
void StochHMM::trellis::fast_complex_stochastic_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::fast_complex_viterbi ( )

Store transition in a table (more memory, but faster than sparse complex) *Need testing and more develepment;

Definition at line 665 of file viterbi.cpp.

References ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), explicit_duration_current, explicit_duration_previous, StochHMM::model::get_explicit(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::model::isBasic(), scoring_current, scoring_previous, seq_size, seqs, state_size, swap_ptr, swap_ptr_duration, and traceback_table.

Referenced by viterbi().

{
//Initialize the traceback table
if (traceback_table != NULL){
}
traceback_table = new int_2D(seq_size,std::vector<int16_t> (state_size,-1));
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_previous == NULL || scoring_current == NULL || traceback_table == NULL){
std::cerr << "Can't allocate Viterbi score and traceback table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
//If model is not a basic model, then we need to initialize the explicit duration vector
//bool extend_duration keeps track of whether the transition to same state was selected.
if (!hmm->isBasic()){
explicit_duration_current = new(std::nothrow) std::vector<size_t>(state_size,0);
explicit_duration_previous= new(std::nothrow) std::vector<size_t>(state_size,0);
}
bool extend_duration(false);
std::vector<bool>* duration = hmm->get_explicit();
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t i = 0; i < state_size; ++i){
if ((*initial_to)[i]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
viterbi_temp = (*hmm)[i]->get_emission_prob(*seqs,0) + getTransition(init, i, 0);
if (viterbi_temp > -INFINITY){
if ((*scoring_current)[i] < viterbi_temp){
(*scoring_current)[i] = viterbi_temp;
}
next_states |= (*(*hmm)[i]->getTo());
// if ((*duration)[i]){
// (*explicit_duration_previous)[i]=1;
// }
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
//TODO: Check use of external definitions below.
// std::cout << "\nPosition:\t" << position << "\n";
// std::cout << "Letter:\t" << seqs->seqValue(0, position)+1 << std::endl;
for (size_t i = 0; i < state_size; ++i){ //i is current state that emits value
if (!current_states[i]){
continue;
}
//current_state = (*hmm)[i];
//emission = current_state->get_emission(*seqs,position);
emission = (*hmm)[i]->get_emission_prob(*seqs, position);
// std::cout << "State Emission:\t" << i << "\t" << exp(emission) << std::endl;
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, i);
}
from_trans = (*hmm)[i]->getFrom();
for (size_t j = 0; j < state_size ; ++j) { //j is previous state
if (!(*from_trans)[j]){
continue;
}
if ((*scoring_previous)[j] != -INFINITY){
viterbi_temp = getTransition((*hmm)[j], i , position) + emission + (*scoring_previous)[j];
// std::cout << "TransFrom: "<< j << "\tto\t" << i << "\t" << exp(getTransition((*hmm)[j],i,position)) << std::endl;
//
// std::cout << "Temp Viterbi:\t" << viterbi_temp << std::endl;
if (viterbi_temp > (*scoring_current)[i]){
//If transition is from same to same then if it is
//explicit duration we'll need to change
extend_duration = (i==j) ? true : false;
(*scoring_current)[i] = viterbi_temp;
(*traceback_table)[position][i] = j;
}
next_states |= (*(*hmm)[i]->getTo());
}
}
//If explicit durations vector defined, and transition from-to same
//then we'll increment the value. Otherwise, set to zero;
if (extend_duration && (*duration)[i]){
(*explicit_duration_current)[i]=2;
}
else{
(*explicit_duration_current)[i]=(*explicit_duration_previous)[i]+1;
}
extend_duration=false;
}
else{
(*explicit_duration_current)[i]=0;
}
}
}
explicit_duration_current->assign(state_size,0);
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
scoring_previous = scoring_current;
for(size_t i = 0; i < state_size ;++i){
if ((*scoring_previous)[i] > -INFINITY){
viterbi_temp = (*scoring_previous)[i] + (*hmm)[i]->getEndTrans();
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
}
}
}
scoring_previous = NULL;
}
void StochHMM::trellis::fast_complex_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::forward ( )

Definition at line 27 of file forward.cpp.

References simple_forward().

Referenced by forward().

{
//if (hmm->isBasic()){
//}
}
void StochHMM::trellis::forward ( model h,
sequences sqs 
)
double_2D* StochHMM::trellis::get_dbl_posterior ( )
inline

Definition at line 300 of file trellis.h.

References dbl_posterior_score.

size_t StochHMM::trellis::get_explicit_duration_length ( transition trans,
size_t  sequencePosition,
size_t  state_iter,
size_t  to_state 
)
private

Traceback to get the duration length of the state. If duration is already being tracked in the table then it will return value +1. Otherwise, it will traceback through the trellis until the traceback identifier is reached.

Returns:
length of traceback (Giving duration)

Definition at line 271 of file trellis.cpp.

References StochHMM::DIFF_STATE, explicit_duration_previous, StochHMM::state::getGFF(), StochHMM::state::getIterator(), StochHMM::state::getLabel(), StochHMM::state::getName(), StochHMM::model::getState(), StochHMM::transition::getTracebackIdentifier(), StochHMM::transition::getTracebackString(), hmm, SIZE_MAX, StochHMM::START_INIT, StochHMM::STATE_GFF, StochHMM::STATE_LABEL, and StochHMM::STATE_NAME.

Referenced by getTransition().

{
if ((*explicit_duration_previous)[state_iter]!=0){
// std::cout << "Duration:\t" << (*explicit_duration_previous)[state_iter]+1 << std::endl;
return (*explicit_duration_previous)[state_iter]+1;
}
//If duration hasn't been defined then traceback until the ending parameter is reached
size_t length(0);
size_t tbState(state_iter); //First traceback pointer to use in traceback_table
//tracebackIdentifier traceback_identifier = previousState->transi[transitionTo].traceback_identifier;
tracebackIdentifier traceback_identifier = trans->getTracebackIdentifier();
//string identifier = previousState->transi[transitionTo].traceback_string;
std::string identifier = trans->getTracebackString();
//Traceback through trellis until the correct identifier is reached.
for(size_t trellPos=sequencePosition-1 ; trellPos != SIZE_MAX ;trellPos--){
length++;
tbState = (*traceback_table)[trellPos][tbState];
if (tbState == SIZE_MAX){
break;
}
state* st = hmm->getState(tbState);
//Check to see if stop conditions of traceback are met, if so break;
if(traceback_identifier == START_INIT && tbState == SIZE_MAX) {break;}
else if (traceback_identifier == DIFF_STATE && state_iter != st->getIterator()) { break;}
else if (traceback_identifier == STATE_NAME && identifier.compare(st->getName())==0) { break;}
else if (traceback_identifier == STATE_LABEL && identifier.compare(st->getLabel())==0) { break;}
else if (traceback_identifier == STATE_GFF && identifier.compare(st->getGFF())==0) { break;}
}
// std::cout << "Duration:\t" << length+1 << std::endl;
return length+1;
}
model* StochHMM::trellis::get_model ( )
inline

Definition at line 289 of file trellis.h.

References hmm.

{return hmm;}
double_2D* StochHMM::trellis::get_naive_backward_scores ( )
inline

Definition at line 298 of file trellis.h.

References dbl_backward_score.

double_2D* StochHMM::trellis::get_naive_forward_scores ( )
inline

Definition at line 297 of file trellis.h.

References dbl_forward_score.

double_2D* StochHMM::trellis::get_naive_viterbi_scores ( )
inline

Definition at line 299 of file trellis.h.

References dbl_viterbi_score.

double StochHMM::trellis::getBackwardProbability ( )
inline

Definition at line 308 of file trellis.h.

References ending_backward_prob.

Referenced by print_limited_posterior(), and print_posterior().

float_2D* StochHMM::trellis::getBackwardTable ( )
inline

Definition at line 304 of file trellis.h.

References backward_score.

{return backward_score;}
double StochHMM::trellis::getEndingTransition ( size_t  )
private
double StochHMM::trellis::getForwardProbability ( )
inline

Definition at line 307 of file trellis.h.

References ending_forward_prob.

Referenced by print_limited_posterior(), and print_posterior().

float_2D* StochHMM::trellis::getForwardTable ( )
inline

Definition at line 303 of file trellis.h.

References forward_score.

{return forward_score;}
model* StochHMM::trellis::getModel ( )
inline

Definition at line 109 of file trellis.h.

References hmm.

Referenced by print_limited_posterior(), and print_posterior().

{return hmm;}
double_2D* StochHMM::trellis::getPosteriorTable ( )
inline

Definition at line 305 of file trellis.h.

References posterior_score.

Referenced by print_limited_posterior(), and print_posterior().

{return posterior_score;}
sequences* StochHMM::trellis::getSeq ( )
inline

Definition at line 110 of file trellis.h.

References seqs.

Referenced by print_limited_posterior(), and print_posterior().

{return seqs;}
double StochHMM::trellis::getTransition ( state st,
size_t  trans_to_state,
size_t  sequencePosition 
)
private

Definition at line 220 of file trellis.cpp.

References StochHMM::DURATION, StochHMM::transition::FunctionDefined(), get_explicit_duration_length(), StochHMM::transition::getExtFunction(), StochHMM::state::getIterator(), StochHMM::state::getTrans(), StochHMM::transition::getTransition(), StochHMM::transition::getTransitionType(), StochHMM::LEXICAL, seqs, StochHMM::STANDARD, traceback_table, and transitionFuncTraceback().

Referenced by fast_complex_viterbi(), naive_backward(), naive_baum_welch(), naive_forward(), naive_nth_viterbi(), naive_stochastic_forward(), naive_stochastic_viterbi(), naive_viterbi(), simple_backward(), simple_forward(), simple_nth_viterbi(), simple_posterior(), simple_simple_stochastic_viterbi(), simple_stochastic_forward(), simple_stochastic_viterbi(), simple_viterbi(), and sparse_complex_viterbi().

{
double transition_prob(-INFINITY);
transition* trans = st->getTrans(trans_to_state);
if (trans==NULL){
return transition_prob;
}
transType trans_type= trans->getTransitionType();
if (trans_type == STANDARD ){ //if the transition type is standard then just return the standard probability
transition_prob= trans->getTransition(0,NULL);
}
else if (trans_type == DURATION){
//If the traceback_table isn't defined, then we need to generate it using the viterbi algorithm
if (traceback_table==NULL){
(*this).viterbi();
}
//Calculate the duration length from the traceback table
size_t size = get_explicit_duration_length(trans,sequencePosition, st->getIterator(), trans_to_state);
transition_prob=trans->getTransition(size,NULL);
}
else if (trans_type == LEXICAL){
transition_prob=trans->getTransition(sequencePosition, seqs);
}
//Is external function define for the transition
if (trans->FunctionDefined()){
//If the traceback_table isn't defined, then we need to generate it using the viterbi algorithm
if (traceback_table==NULL){
(*this).viterbi();
}
transition_prob+=transitionFuncTraceback(st, sequencePosition, trans->getExtFunction());
}
return transition_prob;
}
void StochHMM::trellis::naive_backward ( )

Naive Backward Implements the backward algorithm (simple coded, little or no optimizations) Stores the scores as doubles in table. Scoring table accessible from trellis -> getNaiveBackward();

Definition at line 35 of file backward.cpp.

References StochHMM::addLog(), dbl_backward_score, ending_backward_prob, StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), hmm, seq_size, seqs, SIZE_MAX, and state_size.

Referenced by naive_baum_welch().

{
dbl_backward_score = new double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
double emission(-INFINITY);
double backward_temp(-INFINITY);
double trans(-INFINITY);
double previous(-INFINITY);
//Initialize Backward score with transition from Ending Cell
for(size_t st_current = 0 ; st_current < state_size; st_current++){
trans = (*hmm)[st_current]->getEndTrans();
if (trans != -INFINITY){
(*dbl_backward_score)[seq_size-1][st_current] = trans;
}
}
typedef std::numeric_limits< double > dbl;
std::cout << std::setprecision(dbl::digits10);
for (size_t position = seq_size-2; position != SIZE_MAX ; position--){
for(size_t st_current = 0; st_current < state_size ; st_current++){
for (size_t st_previous = 0; st_previous < state_size; st_previous++){
previous = (*dbl_backward_score)[position+1][st_previous];
if (previous == -INFINITY){
continue;
}
trans = getTransition(hmm->getState(st_current), st_previous, position);
emission = (*hmm)[st_previous]->get_emission_prob(*seqs, position+1);
if (trans == -INFINITY || emission == -INFINITY){
continue;
}
backward_temp = previous + emission + trans;
if ((*dbl_backward_score)[position][st_current] == -INFINITY){
(*dbl_backward_score)[position][st_current] = backward_temp;
}
else{
(*dbl_backward_score)[position][st_current] = addLog( backward_temp, (*dbl_backward_score)[position][st_current]);
}
}
}
}
//Calculate Final Probability
ending_backward_prob = -INFINITY;
state* init = hmm->getInitial();
for (size_t st_current=0; st_current < state_size; st_current++ ){
if ((*dbl_backward_score)[0][st_current] == -INFINITY){
continue;
}
previous = (*dbl_backward_score)[0][st_current];
trans = getTransition(init, st_current, 0);
emission = (*hmm)[st_current]->get_emission_prob(*seqs, 0);
if (trans == -INFINITY || emission == -INFINITY){
continue;
}
backward_temp = previous + emission + trans;
if (ending_backward_prob == -INFINITY){
ending_backward_prob = backward_temp;
}
else{
}
}
}
void StochHMM::trellis::naive_backward ( model h,
sequences sqs 
)
void StochHMM::trellis::naive_baum_welch ( )

Definition at line 21 of file baum_welch.cpp.

References StochHMM::addLog(), dbl_backward_score, dbl_baum_welch_score, dbl_forward_score, StochHMM::model::getState(), getTransition(), hmm, naive_backward(), naive_forward(), seq_size, seqs, and state_size.

{
if (dbl_forward_score == NULL){
}
if (dbl_backward_score == NULL){
}
dbl_baum_welch_score = new (std::nothrow) double_3D(seq_size, std::vector<std::vector<double> >(state_size, std::vector<double>(state_size, -INFINITY)));
if (dbl_baum_welch_score == NULL){
std::cerr << "Can't allocate memory. OUT OF MEMORY\t" << __FUNCTION__ << std::endl;
exit(2);
}
double sum(-INFINITY);
for(size_t position = 0; position < seq_size-1; position++){ // Time(t)
sum = (-INFINITY);
for (size_t previous = 0; previous < state_size ; previous++){ // state(i)
for (size_t current = 0; current < state_size; current++){ // state(j)
(*dbl_baum_welch_score)[position][previous][current] = (*dbl_forward_score)[position][previous] + getTransition(hmm->getState(previous), current, position) + (*hmm)[current]->get_emission_prob(*seqs, position+1) + (*dbl_backward_score)[position+1][current];
sum = addLog((*dbl_baum_welch_score)[position][previous][current], sum);
}
}
for (size_t previous = 0; previous < state_size ; previous++){ // state(i)
for (size_t current = 0; current < state_size; current++){ // state(j)
(*dbl_baum_welch_score)[position][previous][current] -= sum;
}
}
}
return;
}
void StochHMM::trellis::naive_baum_welch ( model h,
sequences sqs 
)

Definition at line 16 of file baum_welch.cpp.

{
}
void StochHMM::trellis::naive_forward ( )

Definition at line 177 of file forward.cpp.

References StochHMM::addLog(), dbl_forward_score, ending_forward_prob, exDef_defined, StochHMM::state::get_emission_prob(), StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), StochHMM::sequences::getWeight(), hmm, seq_size, seqs, and state_size.

Referenced by naive_baum_welch().

{
dbl_forward_score = new (std::nothrow) double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
if (dbl_forward_score == NULL){
std::cerr << "Can't allocate Forward table and traceback table. OUT OF MEMORY\t" << __FUNCTION__ << std::endl;
exit(2);
}
double emission(-INFINITY);
double forward_temp(-INFINITY);
double trans(-INFINITY);
double previous(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
//Calculate from Initial states
for(size_t st = 0; st < state_size; ++st){
forward_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
(*dbl_forward_score)[0][st]=forward_temp;
}
//Calculate Forward for all states
for (size_t position = 1 ; position < seq_size ; ++position){
for (size_t st_current = 0; st_current < state_size; ++st_current){
//Calc emissions
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
previous = (*dbl_forward_score)[position-1][st_previous];
if (previous == -INFINITY){
continue;
}
trans = getTransition(hmm->getState(st_previous), st_current, position);
if (trans !=-INFINITY){
forward_temp = previous + emission + trans;
if ((*dbl_forward_score)[position][st_current]==-INFINITY){
(*dbl_forward_score)[position][st_current] = forward_temp;
}
else{
(*dbl_forward_score)[position][st_current] = addLog(forward_temp, (*dbl_forward_score)[position][st_current]);
}
}
}
}
}
//Calculate Ending Transition
ending_forward_prob = -INFINITY;
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
if ((*hmm)[st_previous]->getEndTrans() != -INFINITY){
if ((*dbl_forward_score)[seq_size-1][st_previous] != -INFINITY){
ending_forward_prob = addLog((*dbl_forward_score)[seq_size-1][st_previous] + (*hmm)[st_previous]->getEndTrans(), ending_forward_prob);
}
}
}
return;
}
void StochHMM::trellis::naive_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::naive_nth_viterbi ( size_t  n)

Definition at line 194 of file nth_best.cpp.

References ending_nth_viterbi, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::state::get_emission_prob(), StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), StochHMM::sequences::getWeight(), hmm, naive_nth_scores, nth_size, seq_size, seqs, StochHMM::sort_scores(), and state_size.

Referenced by perform_nbest_decoding().

{
nth_size = n;
naive_nth_scores = new (std::nothrow) std::vector<std::vector<std::vector<nthScore >* > >(seq_size, std::vector<std::vector<nthScore>* >(state_size,NULL));
std::vector<nthScore>* temp_scores;
double emission(-INFINITY);
double viterbi_temp(-INFINITY);
double trans(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
//Calculate from Initial states
for(size_t st = 0; st < state_size; ++st){
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
if (viterbi_temp == -INFINITY){
continue;
}
temp_scores = new (std::nothrow) std::vector<nthScore>;
temp_scores->push_back(nthScore(-1,-1,viterbi_temp));
//nth_table[0][st]=temp_scores;
(*naive_nth_scores)[0][st]=temp_scores;
}
//Calculate Forward for all states
for (size_t position = 1 ; position < seq_size ; ++position){
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_current = 0; st_current < state_size; ++st_current){
//Calc emissions
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
temp_scores = new (std::nothrow) std::vector<nthScore>;
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
trans = getTransition(hmm->getState(st_previous), st_current, position);
if (trans== -INFINITY){
continue;
}
if ((*naive_nth_scores)[position-1][st_previous] == NULL){
continue;
}
for(size_t vit_previous=0; vit_previous < (*naive_nth_scores)[position-1][st_previous]->size(); vit_previous++){
viterbi_temp = emission + trans + (*(*naive_nth_scores)[position-1][st_previous])[vit_previous].score;
temp_scores->push_back(nthScore(st_previous,vit_previous,viterbi_temp));
}
}
sort_scores(*temp_scores);
if (temp_scores->size() > nth_size){
temp_scores->resize(nth_size);
}
(*naive_nth_scores)[position][st_current]=temp_scores;
}
}
//Calculate Ending Transition
temp_scores = new(std::nothrow) std::vector<nthScore>;
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
//Check validity of transition to end for the state
if ((*hmm)[st_previous]->getEndTrans() != -INFINITY){
//Check to see that previous viterbi scores are defined for the state
if ((*naive_nth_scores)[seq_size-1][st_previous] != NULL){
//Calculate viterbi score for each score in previous cell
for (size_t prev_scores=0; prev_scores < (*naive_nth_scores)[seq_size-1][st_previous]->size(); prev_scores++){
viterbi_temp = (*(*naive_nth_scores)[seq_size-1][st_previous])[prev_scores].score + (*hmm)[st_previous]->getEndTrans();
temp_scores->push_back(nthScore(st_previous, prev_scores, viterbi_temp));
}
}
}
}
sort_scores(*temp_scores);
if (temp_scores->size() > nth_size){
temp_scores->resize(nth_size);
}
ending_nth_viterbi = temp_scores;
return;
}
void StochHMM::trellis::naive_nth_viterbi ( model h,
sequences sqs,
size_t  n 
)
void StochHMM::trellis::naive_stochastic_forward ( )

Definition at line 174 of file stoch_forward.cpp.

References StochHMM::addLog(), dbl_forward_score, ending_forward_prob, exDef_defined, StochHMM::stochTable::finalize(), StochHMM::state::get_emission_prob(), StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::stochTable::push(), seq_size, seqs, SIZE_MAX, state_size, and stochastic_table.

{
dbl_forward_score = new (std::nothrow) double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
stochastic_table = new (std::nothrow) stochTable(seq_size);
if (dbl_forward_score == NULL){
std::cerr << "Can't allocate Forward table and traceback table. OUT OF MEMORY\t" << __FUNCTION__ << std::endl;
exit(2);
}
double emission(-INFINITY);
double forward_temp(-INFINITY);
double trans(-INFINITY);
double previous(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
//Calculate from Initial states
for(size_t st = 0; st < state_size; ++st){
forward_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
(*dbl_forward_score)[0][st]=forward_temp;
}
//Calculate Forward for all states
for (size_t position = 1 ; position < seq_size ; ++position){
for (size_t st_current = 0; st_current < state_size; ++st_current){
//Calc emissions
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
previous = (*dbl_forward_score)[position-1][st_previous];
if (previous == -INFINITY){
continue;
}
trans = getTransition(hmm->getState(st_previous), st_current, position);
if (trans !=-INFINITY ){
forward_temp = previous + emission + trans;
//Save partial value to stochastic table
stochastic_table->push(position-1,st_current,st_previous,forward_temp);
if ((*dbl_forward_score)[position][st_current]==-INFINITY){
(*dbl_forward_score)[position][st_current] = forward_temp;
}
else{
(*dbl_forward_score)[position][st_current] = addLog(forward_temp, (*dbl_forward_score)[position][st_current]);
}
}
}
}
}
//Calculate Ending Transition
ending_forward_prob = -INFINITY;
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
if ((*hmm)[st_previous]->getEndTrans() != -INFINITY){
if ((*dbl_forward_score)[seq_size-1][st_previous] != -INFINITY){
forward_temp = (*dbl_forward_score)[seq_size-1][st_previous] + (*hmm)[st_previous]->getEndTrans();
stochastic_table->push(seq_size-1,SIZE_MAX, st_previous,forward_temp);
}
}
}
//stochastic_table->print();
return;
}
void StochHMM::trellis::naive_stochastic_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::naive_stochastic_viterbi ( )

Definition at line 181 of file stoch_viterbi.cpp.

References dbl_viterbi_score, ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::stochTable::finalize(), StochHMM::state::get_emission_prob(), StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::stochTable::push(), seq_size, seqs, SIZE_MAX, state_size, stochastic_table, and traceback_table.

Referenced by naive_stochastic_viterbi().

{
traceback_table = new(std::nothrow) int_2D(seq_size, std::vector<int16_t>(state_size,-1));
dbl_viterbi_score = new (std::nothrow) double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
stochastic_table = new (std::nothrow) stochTable(seq_size);
double emission(-INFINITY);
double viterbi_temp(-INFINITY);
double trans(-INFINITY);
double previous(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
state* init = hmm->getInitial();
//Calculate from Initial states
for(size_t st = 0; st < state_size; ++st){
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
(*dbl_viterbi_score)[0][st]=viterbi_temp;
(*traceback_table)[0][st]=-1;
}
//Calculate Forward for all states
for (size_t position = 1 ; position < seq_size ; ++position){
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_current = 0; st_current < state_size; ++st_current){
//Calc emissions
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
previous = (*dbl_viterbi_score)[position-1][st_previous];
if (previous == -INFINITY){
continue;
}
trans = getTransition(hmm->getState(st_previous), st_current, position);
if (trans !=-INFINITY){
viterbi_temp = emission + trans + previous;
//Save partial value to stochastic table
stochastic_table->push(position-1,st_current,st_previous,viterbi_temp);
if (viterbi_temp > (*dbl_viterbi_score)[position][st_current]){
(*dbl_viterbi_score)[position][st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = previous;
}
}
}
}
}
//Calculate Ending Transition
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
if ((*hmm)[st_previous]->getEndTrans() != -INFINITY){
if ((*dbl_viterbi_score)[seq_size-1][st_previous] != -INFINITY){
viterbi_temp = (*dbl_viterbi_score)[seq_size-1][st_previous] + (*hmm)[st_previous]->getEndTrans();
stochastic_table->push(seq_size-1,SIZE_MAX, st_previous,viterbi_temp);
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
}
//stochastic_table->print();
return;
}
void StochHMM::trellis::naive_stochastic_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::naive_viterbi ( )

Definition at line 201 of file viterbi.cpp.

References dbl_viterbi_score, ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::state::get_emission_prob(), StochHMM::model::getInitial(), StochHMM::model::getState(), getTransition(), StochHMM::sequences::getWeight(), hmm, seq_size, seqs, state_size, and traceback_table.

Referenced by naive_viterbi().

{
traceback_table = new(std::nothrow) int_2D(seq_size, std::vector<int16_t>(state_size,-1));
dbl_viterbi_score = new (std::nothrow) double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
double emission(-INFINITY);
double viterbi_temp(-INFINITY);
double trans(-INFINITY);
double previous(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
state* init = hmm->getInitial();
//Calculate from Initial states
for(size_t st = 0; st < state_size; ++st){
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
(*dbl_viterbi_score)[0][st]=viterbi_temp;
(*traceback_table)[0][st]=-1;
}
//Calculate Forward for all states
for (size_t position = 1 ; position < seq_size ; ++position){
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_current = 0; st_current < state_size; ++st_current){
//Calc emissions
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
previous = (*dbl_viterbi_score)[position-1][st_previous];
if (previous == -INFINITY){
continue;
}
trans = getTransition(hmm->getState(st_previous), st_current, position);
if (trans !=-INFINITY){
viterbi_temp = emission + trans + previous;
if (viterbi_temp > (*dbl_viterbi_score)[position][st_current]){
(*dbl_viterbi_score)[position][st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = st_previous;
}
}
}
}
}
//Calculate Ending Transition
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){
if ((*hmm)[st_previous]->getEndTrans() != -INFINITY){
if ((*dbl_viterbi_score)[seq_size-1][st_previous] != -INFINITY){
viterbi_temp = (*dbl_viterbi_score)[seq_size-1][st_previous] + (*hmm)[st_previous]->getEndTrans();
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
}
return;
}
void StochHMM::trellis::naive_viterbi ( model h,
sequences sqs 
)

Definition at line 187 of file viterbi.cpp.

References exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::sequences::getLength(), hmm, naive_viterbi(), seq_size, seqs, StochHMM::model::state_size(), and state_size.

{
//Initialize the table
hmm = h;
seqs = sqs;
//TODO: determine which model and chose the type of algorithm to use;
}
void StochHMM::trellis::nth_viterbi ( )
void StochHMM::trellis::nth_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::posterior ( )

Definition at line 13 of file posterior.cpp.

References simple_posterior().

Referenced by perform_posterior(), perform_stochastic_decoding(), and posterior().

{
//if (hmm->isBasic()){
//}
}
void StochHMM::trellis::posterior ( model h,
sequences sqs 
)
void StochHMM::trellis::print ( )
void StochHMM::trellis::reset ( )

Definition at line 148 of file trellis.cpp.

References alt_scoring_current, alt_scoring_previous, backward_score, dbl_backward_score, dbl_forward_score, dbl_posterior_score, dbl_viterbi_score, ending_backward_prob, ending_forward_prob, ending_nth_viterbi, ending_viterbi_score, ending_viterbi_tb, exDef_defined, forward_score, hmm, naive_nth_scores, nth_scoring_current, nth_scoring_previous, nth_size, nth_traceback_table, posterior_score, scoring_current, scoring_previous, seq_size, seqs, StochHMM::SIMPLE, SIZE_MAX, state_size, stochastic_table, store_values, traceback_table, type, and viterbi_score.

void StochHMM::trellis::simple_backward ( )

Definition at line 131 of file backward.cpp.

References StochHMM::addLog(), backward_score, ending_backward_prob, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::state::get_emission_prob(), StochHMM::model::getEndingFrom(), StochHMM::model::getInitial(), getTransition(), StochHMM::sequences::getWeight(), hmm, scoring_current, scoring_previous, seq_size, seqs, SIZE_MAX, state_size, and swap_ptr.

Referenced by backward(), and simple_backward().

{
// if (!hmm->isBasic()){
// std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
// return;
// }
//Allocate backward score table
backward_score = new (std::nothrow) float_2D(seq_size, std::vector<float>(state_size,-INFINITY));
//Allocate scoring vectors
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_previous == NULL || scoring_current == NULL || backward_score == NULL){
std::cerr << "Can't allocate Backward score table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double backward_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
std::bitset<STATE_MAX>* ending_from = hmm->getEndingFrom();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate initial Backward from ending state
for(size_t st_current = 0; st_current < state_size; ++st_current){
if ((*ending_from)[st_current]){ //if the bitset is set (meaning there is a transition to this state)
backward_temp = (*hmm)[st_current]->getEndTrans();
if (backward_temp > -INFINITY){
(*backward_score)[seq_size-1][st_current] = backward_temp;
(*scoring_current)[st_current] = backward_temp;
next_states[st_current] = 1;
}
}
}
for(size_t position = seq_size-2; position != SIZE_MAX ; --position ){
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){ //i is previous state that emits value
if (!current_states[st_previous]){
continue;
}
emission = (*hmm)[st_previous]->get_emission_prob(*seqs, position+1);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position+1, st_previous);
}
if (emission == -INFINITY){
continue;
}
from_trans = (*hmm)[st_previous]->getFrom();
for (size_t st_current = 0; st_current < state_size ; ++st_current) { //j is current state
if (!(*from_trans)[st_current]){
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
backward_temp = (*scoring_previous)[st_previous] + emission + getTransition((*hmm)[st_current], st_previous , position);
if ((*scoring_current)[st_current] == -INFINITY){
(*scoring_current)[st_current] = backward_temp;
(*backward_score)[position][st_current] = backward_temp;
}
else{
(*scoring_current)[st_current] = addLog(backward_temp, (*scoring_current)[st_current]);
(*backward_score)[position][st_current] = (*scoring_current)[st_current];
}
next_states[st_current] = 1;
}
}
}
}
ending_backward_prob = -INFINITY;
state* init = hmm->getInitial();
for(size_t i = 0; i < state_size ;++i){
if ((*scoring_current)[i] != -INFINITY){
backward_temp = (*scoring_current)[i] + (*hmm)[i]->get_emission_prob(*seqs,0) + getTransition(init, i, 0);
if (backward_temp > -INFINITY){
if (ending_backward_prob == -INFINITY){
ending_backward_prob = backward_temp;
}
else{
}
}
}
}
}
void StochHMM::trellis::simple_backward ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_baum_welch ( )
void StochHMM::trellis::simple_baum_welch ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_forward ( )

Definition at line 44 of file forward.cpp.

References StochHMM::addLog(), ending_forward_prob, exDef_defined, StochHMM::sequences::exDefDefined(), forward_score, StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, scoring_current, scoring_previous, seq_size, seqs, state_size, and swap_ptr.

Referenced by forward(), and simple_forward().

{
// if (!hmm->isBasic()){
// std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
// return;
// }
forward_score = new (std::nothrow) float_2D(seq_size, std::vector<float>(state_size,-INFINITY));
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_previous= new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_current == NULL || scoring_previous == NULL || forward_score == NULL){
std::cerr << "Can't allocate forward score table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double forward_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
// std::cout << "Position: 0" << std::endl;
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
forward_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
if (forward_temp > -INFINITY){
(*forward_score)[0][st] = forward_temp;
(*scoring_current)[st] = forward_temp;
next_states |= (*(*hmm)[st]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
// std::cout << "\nPosition: " << position << std::endl;
for (size_t st_current = 0; st_current < state_size; ++st_current){ //i is current state that emits value
if (!current_states[st_current]){
continue;
}
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
from_trans = (*hmm)[st_current]->getFrom();
for (size_t previous = 0; previous < state_size ; ++previous) { //j is previous state
if (!(*from_trans)[previous]){
continue;
}
if ((*scoring_previous)[previous] != -INFINITY){
forward_temp = (*scoring_previous)[previous] + emission + getTransition((*hmm)[previous], st_current , position);
if ((*scoring_current)[st_current] == -INFINITY){
(*scoring_current)[st_current] = forward_temp;
(*forward_score)[position][st_current] = forward_temp;
}
else{
(*scoring_current)[st_current] = addLog(forward_temp, (*scoring_current)[st_current]);
(*forward_score)[position][st_current] = (*scoring_current)[st_current]; }
next_states |= (*(*hmm)[st_current]->getTo());
}
}
// std::cout << "State: " << current <<"\t" << exp((*forward_score)[position][current]) << std::endl;
}
}
//Swap current and previous scores
scoring_previous->assign(state_size,-INFINITY);
ending_forward_prob = -INFINITY;
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] != -INFINITY){
forward_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (forward_temp > -INFINITY){
if (ending_forward_prob == -INFINITY){
ending_forward_prob = forward_temp;
}
else{
}
}
}
}
}
void StochHMM::trellis::simple_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_nth_viterbi ( size_t  n)

Definition at line 31 of file nth_best.cpp.

References ending_nth_viterbi, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::model::isBasic(), nth_scoring_current, nth_scoring_previous, nth_size, nth_swap_ptr, nth_traceback_table, seq_size, seqs, StochHMM::sort_scores(), and state_size.

{
if (!hmm->isBasic()){
std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
return;
}
//Initialize the traceback table
if (ending_nth_viterbi != NULL){delete ending_nth_viterbi;}
nth_scoring_previous = new (std::nothrow) std::vector<std::vector<nthScore> > (state_size);
nth_scoring_current = new (std::nothrow) std::vector<std::vector<nthScore> > (state_size);
nth_traceback_table = new (std::nothrow) std::vector<nthTrace>(seq_size);
ending_nth_viterbi = new(std::nothrow) std::vector<nthScore>;
if (nth_scoring_previous == NULL || nth_scoring_current == NULL || nth_traceback_table == NULL){
std::cerr << "Can't allocate Viterbi score and traceback table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
double trans(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
if (viterbi_temp > -INFINITY){
(*nth_scoring_current)[st].push_back(nthScore(-1,-1,viterbi_temp));
next_states |= (*(*hmm)[st]->getTo());
}
}
}
//Each position in the sequence
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
nth_scoring_previous->assign(state_size,std::vector<nthScore>());
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
//Current states
for (size_t st_current = 0; st_current < state_size; ++st_current){ //Current state that emits value
//Check to see if current state is valid
if (!current_states[st_current]){
continue;
}
//Get emission of current state
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
//Get list of states that are valid previous states
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //for previous states
if (!(*from_trans)[st_previous]){
continue;
}
//Check that previous state has transition to current state
//and that the previous viterbi score is not -INFINITY
if (!(*nth_scoring_previous)[st_previous].empty()){
trans = getTransition((*hmm)[st_previous], st_current, position);
if (trans== -INFINITY){
continue;
}
for(size_t vit_previous=0; vit_previous < (*nth_scoring_previous)[st_previous].size(); vit_previous++){
viterbi_temp = emission + trans + (*nth_scoring_previous)[st_previous][vit_previous].score;
(*nth_scoring_current)[st_current].push_back(nthScore(st_previous,vit_previous,viterbi_temp));
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
if ((*nth_scoring_current)[st_current].size() > nth_size){
(*nth_scoring_current)[st_current].resize(nth_size);
}
for (size_t i=0; i < (*nth_scoring_current)[st_current].size(); i++){
(*nth_traceback_table)[position].assign(st_current, i , (*nth_scoring_current)[st_current][i].st_tb, (*nth_scoring_current)[st_current][i].score_tb);
}
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
nth_scoring_previous->assign(state_size,std::vector<nthScore>());
//Calculate ending viterbi score and traceback from END state
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if (!(*nth_scoring_previous)[st_previous].empty()){
trans = (*hmm)[st_previous]->getEndTrans();
if (trans == -INFINITY){
continue;
}
for(size_t vit_previous=0; vit_previous < (*nth_scoring_previous)[st_previous].size(); vit_previous++){
viterbi_temp = trans + (*nth_scoring_previous)[st_previous][vit_previous].score;
ending_nth_viterbi->push_back(nthScore(st_previous,vit_previous,viterbi_temp));
}
}
}
if (ending_nth_viterbi->size() > nth_size){
}
}
void StochHMM::trellis::simple_nth_viterbi ( model h,
sequences sqs,
size_t  n 
)
void StochHMM::trellis::simple_posterior ( )

Definition at line 56 of file posterior.cpp.

References StochHMM::addLog(), ending_backward_prob, ending_forward_prob, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::state::get_emission_prob(), StochHMM::model::getEndingFrom(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, posterior_score, scoring_current, scoring_previous, seq_size, seqs, SIZE_MAX, state_size, and swap_ptr.

Referenced by posterior(), and simple_posterior().

{
// if (!hmm->isBasic()){
// std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
// return;
// }
//posterior_score = new (std::nothrow) float_2D(seq_size, std::vector<float>(state_size,-INFINITY));
posterior_score = new (std::nothrow) double_2D(seq_size, std::vector<double>(state_size,-INFINITY));
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_previous= new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_current == NULL || scoring_previous == NULL || posterior_score == NULL){
std::cerr << "Can't allocate Posterior score table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double forward_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Forward from transitions from INIT (initial) state
for(size_t i = 0; i < state_size; ++i){
if ((*initial_to)[i]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
forward_temp = (*hmm)[i]->get_emission_prob(*seqs,0) + getTransition(init, i, 0);
if (forward_temp > -INFINITY){
(*scoring_current)[i] = forward_temp;
next_states |= (*(*hmm)[i]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
(*posterior_score)[position-1].assign(scoring_current->begin(), scoring_current->end());
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
for (size_t current = 0; current < state_size; ++current){ //i is current state that emits value
if (!current_states[current]){
continue;
}
emission = (*hmm)[current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, current);
}
if (emission == -INFINITY){
continue;
}
from_trans = (*hmm)[current]->getFrom();
for (size_t previous = 0; previous < state_size ; ++previous) { //j is previous state
if (!(*from_trans)[previous]){
continue;
}
if ((*scoring_previous)[previous] != -INFINITY){
forward_temp = (*scoring_previous)[previous] + emission + getTransition((*hmm)[previous], current , position);
if ((*scoring_current)[current] == -INFINITY){
(*scoring_current)[current] = forward_temp;
}
else{
(*scoring_current)[current] = addLog(forward_temp, (*scoring_current)[current]);
}
next_states |= (*(*hmm)[current]->getTo());
}
}
}
}
(*posterior_score)[seq_size-1].assign(scoring_current->begin(), scoring_current->end());
//Swap current and previous scores
scoring_previous->assign(state_size,-INFINITY);
ending_forward_prob = -INFINITY;
for(size_t i = 0; i < state_size ;++i){
if ((*scoring_previous)[i] != -INFINITY){
forward_temp = (*scoring_previous)[i] + (*hmm)[i]->getEndTrans();
if (forward_temp > -INFINITY){
if (ending_forward_prob == -INFINITY){
ending_forward_prob = forward_temp;
}
else{
}
}
}
}
// Perform Backward Algorithm
double backward_temp(-INFINITY);
scoring_previous->assign(state_size, -INFINITY);
scoring_current->assign(state_size, -INFINITY);
// std::vector<float> posterior_sum(seq_size,-INFINITY);
std::vector<double> posterior_sum(seq_size,-INFINITY);
std::bitset<STATE_MAX>* ending_from = hmm->getEndingFrom();
//Calculate initial Backward from ending state
for(size_t st_current = 0; st_current < state_size; ++st_current){
if ((*ending_from)[st_current]){ //if the bitset is set (meaning there is a transition to this state)
backward_temp = (*hmm)[st_current]->getEndTrans();
if (backward_temp > -INFINITY){
(*scoring_current)[st_current] = backward_temp;
next_states[st_current] = 1;
}
}
}
for(size_t position = seq_size-2; position != SIZE_MAX ; --position ){
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
for (size_t i=0;i<state_size;++i){
if ((*posterior_score)[position+1][i] != -INFINITY && (*scoring_current)[i]!= -INFINITY){
(*posterior_score)[position+1][i] = ((double)(*posterior_score)[position+1][i] + (double)(*scoring_current)[i]) - ending_forward_prob;
if ((*posterior_score)[position+1][i] > -7.6009){ //Above significant value;
posterior_sum[position+1] = addLog(posterior_sum[position+1], (*posterior_score)[position+1][i]);
}
}
}
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_previous = 0; st_previous < state_size; ++st_previous){ //i is current state that emits value
if (!current_states[st_previous]){
continue;
}
emission = (*hmm)[st_previous]->get_emission_prob(*seqs, position+1);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position+1, st_previous);
}
if (emission == -INFINITY){
continue;
}
from_trans = (*hmm)[st_previous]->getFrom();
for (size_t st_current = 0; st_current < state_size ; ++st_current) { //j is previous state
if (!(*from_trans)[st_current]){
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
backward_temp =(*scoring_previous)[st_previous] + emission + getTransition((*hmm)[st_current], st_previous , position);
if ((*scoring_current)[st_current] == -INFINITY){
(*scoring_current)[st_current] = backward_temp;
}
else{
(*scoring_current)[st_current] = addLog(backward_temp, (*scoring_current)[st_current]);
}
next_states[st_current] = 1;
}
}
}
}
for (size_t i=0;i<state_size;++i){
(*posterior_score)[0][i] = ((double)(*posterior_score)[0][i] + (double)(*scoring_current)[i]) - ending_forward_prob;
posterior_sum[0] = addLog(posterior_sum[0], (*posterior_score)[0][i]);
}
ending_backward_prob = -INFINITY;
init = hmm->getInitial();
for(size_t i = 0; i < state_size ;++i){
if ((*scoring_current)[i] != -INFINITY){
backward_temp = (*scoring_current)[i] + (*hmm)[i]->get_emission_prob(*seqs,0) + getTransition(init, i, 0);
if (backward_temp > -INFINITY){
if (ending_backward_prob == -INFINITY){
ending_backward_prob = backward_temp;
}
else{
}
}
}
}
if (abs(ending_backward_prob - ending_forward_prob) > 0.0000001){
std::cerr << "Ending sequence probabilities calculated by Forward and Backward algorithm are different. They should be the same.\t" << __FUNCTION__ << std::endl;
}
for (size_t i=0;i<seq_size;i++){
for(size_t j=0;j<state_size;j++){
if ((*posterior_score)[i][j] == -INFINITY){
continue;
}
// std::cout << "Sum:\t" << exp(posterior_sum[i]) << std::endl;
// std::cout << "Score:\t" << exp((*posterior_score)[i][j]) << std::endl;
if ((*posterior_score)[i][j] > -7.6009){ //Above significant value;
(*posterior_score)[i][j] -= posterior_sum[i];
}
else{
(*posterior_score)[i][j] = -INFINITY;
}
}
}
}
void StochHMM::trellis::simple_posterior ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_simple_stochastic_viterbi ( model h,
sequences sqs 
)

Definition at line 407 of file stoch_viterbi.cpp.

References alt_simple_stochastic_table, ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::alt_simple_stochTable::finalize(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), StochHMM::sequences::getLength(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::alt_simple_stochTable::push(), StochHMM::alt_simple_stochTable::push_ending(), scoring_current, scoring_previous, seq_size, seqs, StochHMM::model::state_size(), state_size, swap_ptr, and traceback_table.

{
hmm = h;
seqs = sqs;
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
traceback_table = new int_2D(seq_size,std::vector<int16_t> (state_size,-1));
alt_simple_stochastic_table = new (std::nothrow) alt_simple_stochTable(state_size,seq_size);
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);;
if (viterbi_temp > -INFINITY){
if ((*scoring_current)[st] < viterbi_temp){
(*scoring_current)[st] = viterbi_temp;
}
next_states |= (*(*hmm)[st]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_current = 0; st_current < state_size; ++st_current){ //i is current state that emits value
//Check to see if current state is valid
if (!current_states[st_current]){
continue;
}
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
//Get list of states that are valid previous states
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //j is previous state
if (!(*from_trans)[st_previous]){
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
viterbi_temp = getTransition((*hmm)[st_previous], st_current , position) + emission + (*scoring_previous)[st_previous];
if (viterbi_temp == -INFINITY){
continue;
}
//Save partial value to stochastic table
alt_simple_stochastic_table->push(position-1,st_current,st_previous,viterbi_temp);
if (viterbi_temp > (*scoring_current)[st_current]){
(*scoring_current)[st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = st_previous;
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
scoring_previous = scoring_current;
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] > -INFINITY){
viterbi_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (viterbi_temp == -INFINITY){
continue;
}
alt_simple_stochastic_table->push_ending(st_previous,viterbi_temp);
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
//alt_simple_stochastic_table->print();
scoring_previous = NULL;
}
void StochHMM::trellis::simple_stochastic_forward ( )

Definition at line 30 of file stoch_forward.cpp.

References StochHMM::addLog(), ending_forward_prob, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::stochTable::finalize(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::stochTable::push(), scoring_current, scoring_previous, seq_size, seqs, SIZE_MAX, state_size, stochastic_table, and swap_ptr.

Referenced by stochastic_forward().

{
// if (!hmm->isBasic()){
// std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
// return;
// }
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_previous= new (std::nothrow) std::vector<double> (state_size,-INFINITY);
stochastic_table = new (std::nothrow) stochTable(seq_size);
if (scoring_current == NULL || scoring_previous == NULL || stochastic_table == NULL){
std::cerr << "Can't allocate forward score table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double forward_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
// std::cout << "Position: 0" << std::endl;
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
forward_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
if (forward_temp > -INFINITY){
(*scoring_current)[st] = forward_temp;
next_states |= (*(*hmm)[st]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
// std::cout << "\nPosition: " << position << std::endl;
for (size_t st_current = 0; st_current < state_size; ++st_current){ //i is current state that emits value
if (!current_states[st_current]){
continue;
}
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //j is previous state
if (!(*from_trans)[st_previous]){
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
forward_temp = (*scoring_previous)[st_previous] + emission + getTransition((*hmm)[st_previous], st_current , position);
if (forward_temp == -INFINITY){
continue;
}
stochastic_table->push(position-1,st_current,st_previous,forward_temp);
if ((*scoring_current)[st_current] == -INFINITY){
(*scoring_current)[st_current] = forward_temp;
}
else{
(*scoring_current)[st_current] = addLog(forward_temp, (*scoring_current)[st_current]);
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
}
}
//Swap current and previous scores
scoring_previous->assign(state_size,-INFINITY);
ending_forward_prob = -INFINITY;
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] != -INFINITY){
forward_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (forward_temp == -INFINITY){
continue;
}
stochastic_table->push(seq_size-1,SIZE_MAX, st_previous,forward_temp);
if (forward_temp > -INFINITY){
if (ending_forward_prob == -INFINITY){
ending_forward_prob = forward_temp;
}
else{
}
}
}
}
//stochastic_table->print();
}
void StochHMM::trellis::simple_stochastic_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_stochastic_viterbi ( )

Definition at line 42 of file stoch_viterbi.cpp.

References ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::stochTable::finalize(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::stochTable::push(), scoring_current, scoring_previous, seq_size, seqs, SIZE_MAX, state_size, stochastic_table, swap_ptr, and traceback_table.

Referenced by simple_stochastic_viterbi(), and stochastic_viterbi().

{
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
traceback_table = new int_2D(seq_size,std::vector<int16_t> (state_size,-1));
stochastic_table = new (std::nothrow) stochTable(seq_size);
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);;
if (viterbi_temp > -INFINITY){
if ((*scoring_current)[st] < viterbi_temp){
(*scoring_current)[st] = viterbi_temp;
}
next_states |= (*(*hmm)[st]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
for (size_t st_current = 0; st_current < state_size; ++st_current){ //i is current state that emits value
//Check to see if current state is valid
if (!current_states[st_current]){
continue;
}
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
//Get list of states that are valid previous states
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //j is previous state
if (!(*from_trans)[st_previous]){
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
viterbi_temp = getTransition((*hmm)[st_previous], st_current , position) + emission + (*scoring_previous)[st_previous];
if (viterbi_temp == -INFINITY){
continue;
}
//Save partial value to stochastic table
stochastic_table->push(position-1,st_current,st_previous,viterbi_temp);
if (viterbi_temp > (*scoring_current)[st_current]){
(*scoring_current)[st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = st_previous;
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
scoring_previous = scoring_current;
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] > -INFINITY){
viterbi_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (viterbi_temp == -INFINITY){
continue;
}
stochastic_table->push(seq_size-1,SIZE_MAX, st_previous,viterbi_temp);
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
//stochastic_table->print();
scoring_previous = NULL;
}
void StochHMM::trellis::simple_stochastic_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::simple_viterbi ( )

Definition at line 45 of file viterbi.cpp.

References ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::model::isBasic(), scoring_current, scoring_previous, seq_size, seqs, state_size, swap_ptr, and traceback_table.

Referenced by simple_viterbi(), and viterbi().

{
if (!hmm->isBasic()){
std::cerr << "Model isn't a simple/basic HMM. Use complex algorithms\n";
return;
}
//Initialize the traceback table
if (traceback_table != NULL){
}
traceback_table = new int_2D(seq_size,std::vector<int16_t> (state_size,-1));
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_previous == NULL || scoring_current == NULL || traceback_table == NULL){
std::cerr << "Can't allocate Viterbi score and traceback table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
ending_viterbi_score = -INFINITY;
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t st = 0; st < state_size; ++st){
if ((*initial_to)[st]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
viterbi_temp = (*hmm)[st]->get_emission_prob(*seqs,0) + getTransition(init, st, 0);
if (viterbi_temp > -INFINITY){
if ((*scoring_current)[st] < viterbi_temp){
(*scoring_current)[st] = viterbi_temp;
}
next_states |= (*(*hmm)[st]->getTo());
}
}
}
//Each position in the sequence
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
//Current states
for (size_t st_current = 0; st_current < state_size; ++st_current){ //Current state that emits value
//Check to see if current state is valid
if (!current_states[st_current]){
continue;
}
//Get emission of current state
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
if (emission == -INFINITY){
continue;
}
//Get list of states that are valid previous states
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //for previous states
if (!(*from_trans)[st_previous]){
continue;
}
//Check that previous state has transition to current state
//and that the previous viterbi score is not -INFINITY
if ((*scoring_previous)[st_previous] != -INFINITY){
viterbi_temp = getTransition((*hmm)[st_previous], st_current , position) + emission + (*scoring_previous)[st_previous];
if (viterbi_temp > (*scoring_current)[st_current]){
(*scoring_current)[st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = st_previous;
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Calculate ending viterbi score and traceback from END state
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] > -INFINITY){
viterbi_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
}
void StochHMM::trellis::simple_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::sparse_complex_backward ( )
void StochHMM::trellis::sparse_complex_backward ( model h,
sequences sqs 
)
void StochHMM::trellis::sparse_complex_baum_welch ( )
void StochHMM::trellis::sparse_complex_baum_welch ( model h,
sequences sqs 
)
void StochHMM::trellis::sparse_complex_stochastic_forward ( )
void StochHMM::trellis::sparse_complex_stochastic_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::sparse_complex_viterbi ( )

Sparse Complex Viterbi Stores the transition duration probabilities in a hashmap (memory efficient, slower) The duratio probabilities can then be used in forward and backward algorithms.

Definition at line 470 of file viterbi.cpp.

References complex_transitions, ending_viterbi_score, ending_viterbi_tb, exDef_defined, StochHMM::sequences::exDefDefined(), explicit_duration_current, explicit_duration_previous, StochHMM::model::get_explicit(), StochHMM::model::getInitial(), StochHMM::model::getInitialTo(), getTransition(), StochHMM::sequences::getWeight(), hmm, StochHMM::model::isBasic(), scoring_current, scoring_previous, seq_size, seqs, state_size, swap_ptr, swap_ptr_duration, and traceback_table.

{
//Initialize the traceback table
if (traceback_table != NULL){
}
traceback_table = new int_2D(seq_size,std::vector<int16_t> (state_size,1));
scoring_previous = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
scoring_current = new (std::nothrow) std::vector<double> (state_size,-INFINITY);
if (scoring_previous == NULL || scoring_current == NULL || traceback_table == NULL){
std::cerr << "Can't allocate Viterbi score and traceback table. OUT OF MEMORY" << std::endl;
exit(2);
}
std::bitset<STATE_MAX> next_states;
std::bitset<STATE_MAX> current_states;
double viterbi_temp(-INFINITY);
double emission(-INFINITY);
bool exDef_position(false);
double transition_prob(-INFINITY);
ending_viterbi_score = -INFINITY;
//If model is not a basic model, then we need to initialize the explicit duration vector
//bool extend_duration keeps track of whether the transition to same state was selected.
if (!hmm->isBasic()){
explicit_duration_current = new(std::nothrow) std::vector<size_t>(state_size,0);
explicit_duration_previous= new(std::nothrow) std::vector<size_t>(state_size,0);
}
bool extend_duration(false);
// Get list of States with explicit duration
std::vector<bool>* duration = hmm->get_explicit();
//Initialize Duration storage table
complex_transitions = new std::vector<std::vector< std::map<uint16_t,double>* >* > (state_size,NULL);
for(size_t i=0; i < state_size; i++){
if((*duration)[i]){
(*complex_transitions)[i] = new std::vector<std::map<uint16_t,double>* > (seq_size, NULL);
}
}
state* init = hmm->getInitial();
std::bitset<STATE_MAX>* initial_to = hmm->getInitialTo();
std::bitset<STATE_MAX>* from_trans(NULL);
//Calculate Viterbi from transitions from INIT (initial) state
for(size_t i = 0; i < state_size; ++i){
if ((*initial_to)[i]){ //if the bitset is set (meaning there is a transition to this state), calculate the viterbi
//Transitions here are guarenteed to be standard from the initial state
viterbi_temp = (*hmm)[i]->get_emission_prob(*seqs,0) + getTransition(init, i, 0);
if (viterbi_temp > -INFINITY){
if ((*scoring_current)[i] < viterbi_temp){
(*scoring_current)[i] = viterbi_temp;
}
next_states |= (*(*hmm)[i]->getTo());
}
}
}
for(size_t position = 1; position < seq_size ; ++position ){
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
//Swap current_states and next states sets
current_states.reset();
current_states |= next_states;
next_states.reset();
exDef_position = seqs->exDefDefined(position);
}
//TODO: Check use of external definitions below.
//std::cout << "\nPosition:\t" << position << "\n";
// std::cout << "Letter:\t" << seqs->seqValue(0, position) << std::endl;
for (size_t st_current = 0; st_current < state_size; ++st_current){ //i is current state that emits value
if (!current_states[st_current]){
continue;
}
emission = (*hmm)[st_current]->get_emission_prob(*seqs, position);
//Check External definitions
if (exDef_defined && exDef_position){
emission += seqs->getWeight(position, st_current);
}
//Get list of state that transition to current state
from_trans = (*hmm)[st_current]->getFrom();
for (size_t st_previous = 0; st_previous < state_size ; ++st_previous) { //j is previous state
if (!(*from_trans)[st_previous]){ //if transition not possible next
continue;
}
if ((*scoring_previous)[st_previous] != -INFINITY){
transition_prob = getTransition((*hmm)[st_previous], st_current , position);
if ((*duration)[st_previous]){
if ((*(*complex_transitions)[st_current])[position] == NULL){
(*(*complex_transitions)[st_current])[position] = new std::map<uint16_t,double>;
}
(*(*(*complex_transitions)[st_current])[position])[st_previous] = transition_prob;
}
viterbi_temp = transition_prob + emission + (*scoring_previous)[st_previous];
if (viterbi_temp > (*scoring_current)[st_current]){
//If transition is from same to same then if it is
//explicit duration we'll need to change
extend_duration = (st_current==st_previous) ? true : false;
(*scoring_current)[st_current] = viterbi_temp;
(*traceback_table)[position][st_current] = st_previous;
}
next_states |= (*(*hmm)[st_current]->getTo());
}
}
//If explicit durations vector defined, and transition from-to same
//then we'll increment the value. Otherwise, set to zero;
if (extend_duration && (*duration)[st_current]){
if ((*explicit_duration_previous)[st_current]==0){
(*explicit_duration_current)[st_current]=2;
}
else{
(*explicit_duration_current)[st_current]=(*explicit_duration_previous)[st_current]+1;
}
extend_duration=false;
}
else{
(*explicit_duration_current)[st_current]=0;
}
}
}
explicit_duration_current->assign(state_size,0);
}
}
//TODO: Calculate ending and set the final viterbi and traceback pointer
//Swap current and previous viterbi scores
scoring_previous->assign(state_size,-INFINITY);
scoring_previous = scoring_current;
for(size_t st_previous = 0; st_previous < state_size ;++st_previous){
if ((*scoring_previous)[st_previous] > -INFINITY){
viterbi_temp = (*scoring_previous)[st_previous] + (*hmm)[st_previous]->getEndTrans();
if (viterbi_temp > ending_viterbi_score){
ending_viterbi_score = viterbi_temp;
ending_viterbi_tb = st_previous;
}
}
}
scoring_previous = NULL;
}
void StochHMM::trellis::sparse_complex_viterbi ( model h,
sequences sqs 
)
void StochHMM::trellis::stochastic_forward ( )

Definition at line 24 of file stoch_forward.cpp.

References simple_stochastic_forward().

Referenced by perform_stochastic_decoding(), and stochastic_forward().

{
//if (hmm->isBasic()){
//}
}
void StochHMM::trellis::stochastic_forward ( model h,
sequences sqs 
)
void StochHMM::trellis::stochastic_traceback ( traceback_path path)

Definition at line 470 of file trellis.cpp.

References stochastic_table, and StochHMM::stochTable::traceback().

Referenced by perform_stochastic_decoding().

{
return;
}
void StochHMM::trellis::stochastic_traceback ( multiTraceback paths,
size_t  reps 
)

Definition at line 477 of file trellis.cpp.

References StochHMM::multiTraceback::assign(), hmm, stochastic_table, and StochHMM::stochTable::traceback().

{
for(size_t i=0;i<reps;i++){
traceback_path pth(hmm);
paths.assign(pth);
}
return;
}
void StochHMM::trellis::stochastic_viterbi ( )
void StochHMM::trellis::stochastic_viterbi ( model h,
sequences sqs 
)
bool StochHMM::trellis::store ( )
inline

Definition at line 282 of file trellis.h.

References store_values.

{return store_values;}
void StochHMM::trellis::store ( bool  val)
inline

Definition at line 283 of file trellis.h.

References store_values.

{store_values=val; return;}
std::string StochHMM::trellis::stringify ( )
void StochHMM::trellis::traceback ( traceback_path path)

Perform traceback through trellis

Returns:
path trackback_path

Definition at line 402 of file trellis.cpp.

References ending_viterbi_score, ending_viterbi_tb, StochHMM::traceback_path::getModel(), hmm, StochHMM::traceback_path::push_back(), seq_size, StochHMM::traceback_path::setModel(), StochHMM::traceback_path::setScore(), and traceback_table.

Referenced by perform_viterbi_decoding().

{
if (seq_size==0 || traceback_table == NULL){
return;
}
if (path.getModel() == NULL){
path.setModel(hmm);
}
if (ending_viterbi_score == -INFINITY){
return;
}
else{
path.setScore(ending_viterbi_score);
path.push_back(ending_viterbi_tb);
int16_t pointer = ending_viterbi_tb;
for( size_t position = seq_size -1 ; position>0 ; position--){
pointer = (*traceback_table)[position][pointer];
if (pointer == -1){
std::cerr << "No valid path at Position: " << position << std::endl;
return;
}
path.push_back(pointer);
}
}
return;
}
void StochHMM::trellis::traceback ( traceback_path path,
size_t  position,
size_t  state 
)

Perform a traceback starting at position and state given

Parameters:
[out]traceback_path
positionPosition to start traceback
stateState to begin traceback

Definition at line 440 of file trellis.cpp.

References StochHMM::traceback_path::getModel(), hmm, StochHMM::traceback_path::push_back(), seq_size, StochHMM::traceback_path::setModel(), and traceback_table.

{
if (seq_size == 0 || traceback_table == NULL){
return;
}
if (path.getModel() == NULL){
path.setModel(hmm);
}
int16_t pointer = (*traceback_table)[position][state];
if (pointer == -1){
std::cerr << "No valid path at State: " << state << " from Position: " << position << std::endl;
return;
}
for( size_t pos = position - 1 ; pos>0 ; pos--){
pointer = (*traceback_table)[pos][pointer];
if (pointer == -1){
std::cerr << "No valid path at State: " << state << " from Position: " << position << std::endl;
return;
}
path.push_back(pointer);
}
return;
}
void StochHMM::trellis::traceback_nth ( traceback_path path,
size_t  n 
)

Definition at line 489 of file trellis.cpp.

References ending_nth_viterbi, naive_nth_scores, nth_size, nth_traceback_table, StochHMM::traceback_path::push_back(), StochHMM::nthScore::score_tb, seq_size, StochHMM::traceback_path::setScore(), and StochHMM::nthScore::st_tb.

Referenced by perform_nbest_decoding().

{
if (seq_size == 0 || n > nth_size || (nth_traceback_table == NULL && naive_nth_scores == NULL)){
return;
}
if (nth_traceback_table != NULL){
int16_t st_pointer = (*ending_nth_viterbi)[n].st_tb;
int16_t sc_pointer = (*ending_nth_viterbi)[n].score_tb;
path.setScore((*ending_nth_viterbi)[n].score);
path.push_back(st_pointer);
for( size_t position = seq_size -1 ; position>0 ; position--){
(*nth_traceback_table)[position].get(st_pointer,sc_pointer);
if (st_pointer == -1){
std::cerr << "No valid path at Position: " << position << std::endl;
return;
}
path.push_back(st_pointer);
}
}
else{
int16_t st_pointer = (*ending_nth_viterbi)[n].st_tb;
int16_t sc_pointer = (*ending_nth_viterbi)[n].score_tb;
path.setScore((*ending_nth_viterbi)[n].score);
path.push_back(st_pointer);
for( size_t position = seq_size -1 ; position>0 ; position--){
nthScore& temp = (*(*naive_nth_scores)[position][st_pointer])[sc_pointer];
st_pointer = temp.st_tb;
sc_pointer = temp.score_tb;
if (st_pointer == -1){
std::cerr << "No valid path at Position: " << position << std::endl;
return;
}
path.push_back(st_pointer);
}
}
return;
}
void StochHMM::trellis::traceback_nth_viterbi ( multiTraceback )
void StochHMM::trellis::traceback_posterior ( traceback_path path)

Definition at line 331 of file posterior.cpp.

References StochHMM::max(), posterior_score, StochHMM::traceback_path::push_back(), seq_size, SIZE_MAX, and state_size.

Referenced by perform_posterior().

{
if (posterior_score == NULL){
std::cerr << __FUNCTION__ << " called before trellis::posterior was completed\n";
exit(2);
}
double max(-INFINITY);
int16_t max_ptr(-1);
for(size_t position=seq_size-1; position != SIZE_MAX ;position--){
max = -INFINITY;
max_ptr = -1 ;
for (size_t st=0; st < state_size; st++){
if ((*posterior_score)[position][st] > max){
max = (*posterior_score)[position][st];
max_ptr = st;
}
}
path.push_back(max_ptr);
}
return;
}
void StochHMM::trellis::traceback_stoch_posterior ( traceback_path path)

Definition at line 355 of file posterior.cpp.

References posterior_score, StochHMM::traceback_path::push_back(), seq_size, SIZE_MAX, and state_size.

Referenced by perform_stochastic_decoding(), and traceback_stoch_posterior().

{
for (size_t position =seq_size-1; position != SIZE_MAX; --position){
double random=((double)rand()/((double)(RAND_MAX)+(double)(1)));
double cumulative_prob(0.0);
for (size_t st = 0; st < state_size ; ++st){
cumulative_prob+=exp((*posterior_score)[position][st]);
if (random < cumulative_prob){
path.push_back( (int16_t) st);
}
}
}
return;
}
void StochHMM::trellis::traceback_stoch_posterior ( multiTraceback paths,
size_t  reps 
)

Definition at line 370 of file posterior.cpp.

References StochHMM::multiTraceback::assign(), hmm, and traceback_stoch_posterior().

{
for (size_t i = 0; i < reps; i++){
traceback_path path(hmm);
paths.assign(path);
}
return;
}
double StochHMM::trellis::transitionFuncTraceback ( state st,
size_t  position,
transitionFuncParam func 
)
private

When a transitionFunc is to be called it must performs a traceback and get the required sequence to pass to the function

Parameters:

Definition at line 317 of file trellis.cpp.

References StochHMM::DIFF_STATE, StochHMM::transitionFuncParam::evaluate(), StochHMM::FULL, StochHMM::track::getAlphaMax(), StochHMM::transitionFuncParam::getCombineName(), StochHMM::transitionFuncParam::getCombineType(), StochHMM::state::getGFF(), StochHMM::track::getIndex(), StochHMM::state::getIterator(), StochHMM::state::getLabel(), StochHMM::state::getName(), StochHMM::sequences::getSeq(), StochHMM::model::getState(), StochHMM::sequence::getSymbol(), StochHMM::transitionFuncParam::getTracebackName(), StochHMM::transitionFuncParam::getTracebackType(), StochHMM::transitionFuncParam::getTrack(), StochHMM::sequences::getUndigitized(), hmm, StochHMM::track::isAlpha(), seqs, SIZE_MAX, StochHMM::START_INIT, StochHMM::STATE_GFF, StochHMM::STATE_LABEL, StochHMM::STATE_NAME, StochHMM::STATEGFF, StochHMM::STATELABEL, and StochHMM::STATENAME.

Referenced by getTransition().

{
std::vector<int> tracebackPath;
std::vector<std::string> tracebackString;
//How far to traceback
tracebackIdentifier traceback_identifier = func->getTracebackType();
const std::string& tracebackIdentifierName = func->getTracebackName();
//What to combine
combineIdentifier combineIdent = func->getCombineType();
const std::string& combineIdentName = func->getCombineName();
//Deterimine which track to use
track* alphaTrack = func->getTrack();
size_t trackIndex = alphaTrack->getIndex();
if (!alphaTrack->isAlpha()){
std::cerr << "External transition function called on track that isn't discrete (ALPHANUMERIC)\n";
exit(2);
}
const sequence* seq = seqs->getSeq(trackIndex);
int16_t tb_state(st->getIterator());
int16_t starting_state = tb_state;
for(size_t trellisPos = position-1; trellisPos != SIZE_MAX ; --trellisPos){
tracebackPath.push_back(tb_state);
if ((combineIdent == FULL) ||
(combineIdent == STATENAME && combineIdentName.compare(st->getName())==0)||
(combineIdent == STATELABEL && combineIdentName.compare(st->getLabel())==0)||
(combineIdent == STATEGFF && combineIdentName.compare(st->getGFF())==0))
{
tracebackString.push_back(seq->getSymbol(trellisPos));
}
tb_state= (*traceback_table)[trellisPos][tb_state];
state* temp_st = hmm->getState(tb_state);
//Check to see if stop conditions of traceback are met, if so break;
if(traceback_identifier == START_INIT && tb_state == -1) {break;}
else if (traceback_identifier == DIFF_STATE && starting_state != tb_state) { break;}
else if (traceback_identifier == STATE_NAME && tracebackIdentifierName.compare(temp_st->getName())==0){ break;}
else if (traceback_identifier == STATE_LABEL && tracebackIdentifierName.compare(temp_st->getLabel())==0) { break;}
else if (traceback_identifier == STATE_GFF && tracebackIdentifierName.compare(temp_st->getGFF())==0) { break;}
}
size_t length=tracebackPath.size();
std::string CombinedString;
size_t maxSymbolSize = alphaTrack->getAlphaMax();
//For single letter characters
if (maxSymbolSize ==1){
for(std::vector<std::string>::reverse_iterator rit = tracebackString.rbegin(); rit!=tracebackString.rend();++rit){
CombinedString+=(*rit);
}
}
else{ // For kmer words > 1 in length
std::vector<std::string>::reverse_iterator rit = tracebackString.rbegin();
CombinedString+=(*rit);
++rit;
for(; rit!=tracebackString.rend();++rit){
CombinedString+="," + (*rit);
}
}
//Call the transitionFunc and get the score back
double transitionValue = func->evaluate(seqs->getUndigitized(trackIndex), position, &CombinedString, length);
return transitionValue;
}
void StochHMM::trellis::update_emissions ( )

Definition at line 89 of file baum_welch.cpp.

{
}
void StochHMM::trellis::update_transitions ( )

Definition at line 59 of file baum_welch.cpp.

References StochHMM::addLog(), dbl_backward_score, dbl_baum_welch_score, dbl_forward_score, ending_forward_prob, StochHMM::model::getStateName(), hmm, seq_size, and state_size.

{
std::cout << "Transitions to Start:\n";
double updated(-INFINITY);
for(size_t st = 0 ; st < state_size ; st++){
updated = ((*dbl_backward_score)[0][st] + (*dbl_forward_score)[0][st])-ending_forward_prob;
std::cout << hmm->getStateName(st) << "\t" << exp(updated) << std::endl;
}
float_2D numerator(state_size, std::vector<float>(state_size,-INFINITY));
float_2D denominator(state_size, std::vector<float>(state_size,-INFINITY));
for (size_t position = 0; position < seq_size-1; position++){
for (size_t i = 0; i < state_size; i++){
for (size_t j = 0; j < state_size; j++){
numerator[i][j]= addLog((double)numerator[i][j], (*dbl_baum_welch_score)[position][i][j]);
denominator[i][j] = addLog((double)denominator[i][j], ((*dbl_forward_score)[position][i] + (*dbl_backward_score)[position][i])-ending_forward_prob);
}
}
}
for (size_t i = 0; i < state_size; i++){
std::cout << hmm->getStateName(i) << "\t";
for (size_t j = 0; j < state_size; j++){
std::cout << exp((numerator[i][j]-denominator[i][j])) << "\t";
}
std::cout << std::endl;
}
}
void StochHMM::trellis::viterbi ( )

Definition at line 25 of file viterbi.cpp.

References fast_complex_viterbi(), hmm, StochHMM::model::isBasic(), and simple_viterbi().

Referenced by perform_viterbi_decoding(), and viterbi().

{
if (hmm->isBasic()){
}
else{
}
}
void StochHMM::trellis::viterbi ( model h,
sequences sqs 
)

Definition at line 13 of file viterbi.cpp.

References exDef_defined, StochHMM::sequences::exDefDefined(), StochHMM::sequences::getLength(), hmm, seq_size, seqs, StochHMM::model::state_size(), state_size, and viterbi().

{
//Initialize the table
hmm = h;
seqs = sqs;
//TODO: determine which model and chose the type of algorithm to use;
}

Member Data Documentation

std::vector<double>* StochHMM::trellis::alt_scoring_current
private

Definition at line 367 of file trellis.h.

Referenced by reset(), trellis(), and ~trellis().

std::vector<double>* StochHMM::trellis::alt_scoring_previous
private

Definition at line 368 of file trellis.h.

Referenced by reset(), trellis(), and ~trellis().

alt_simple_stochTable* StochHMM::trellis::alt_simple_stochastic_table
private

Definition at line 336 of file trellis.h.

Referenced by simple_simple_stochastic_viterbi().

float_2D* StochHMM::trellis::backward_score
private

Definition at line 341 of file trellis.h.

Referenced by getBackwardTable(), reset(), simple_backward(), trellis(), and ~trellis().

std::vector<std::vector<double>* > StochHMM::trellis::complex_emissions
private

Definition at line 376 of file trellis.h.

std::vector<std::vector<std::map<uint16_t,double>* >* >* StochHMM::trellis::complex_transitions
private

Definition at line 377 of file trellis.h.

Referenced by sparse_complex_viterbi().

double_2D* StochHMM::trellis::dbl_backward_score
private
double_3D* StochHMM::trellis::dbl_baum_welch_score
private

Definition at line 348 of file trellis.h.

Referenced by naive_baum_welch(), and update_transitions().

double_2D* StochHMM::trellis::dbl_forward_score
private
double_2D* StochHMM::trellis::dbl_posterior_score
private

Definition at line 347 of file trellis.h.

Referenced by get_dbl_posterior(), reset(), trellis(), and ~trellis().

double_2D* StochHMM::trellis::dbl_viterbi_score
private
double StochHMM::trellis::ending_backward_prob
private
double StochHMM::trellis::ending_forward_prob
private
std::vector<nthScore>* StochHMM::trellis::ending_nth_viterbi
private
double StochHMM::trellis::ending_viterbi_score
private
int16_t StochHMM::trellis::ending_viterbi_tb
private
bool StochHMM::trellis::exDef_defined
private
std::vector<size_t>* StochHMM::trellis::explicit_duration_current
private

Definition at line 370 of file trellis.h.

Referenced by fast_complex_viterbi(), and sparse_complex_viterbi().

std::vector<size_t>* StochHMM::trellis::explicit_duration_previous
private
float_2D* StochHMM::trellis::forward_score
private

Definition at line 340 of file trellis.h.

Referenced by getForwardTable(), reset(), simple_forward(), trellis(), and ~trellis().

model* StochHMM::trellis::hmm
private
std::vector<std::vector<std::vector<nthScore >* > >* StochHMM::trellis::naive_nth_scores
private

Definition at line 349 of file trellis.h.

Referenced by naive_nth_viterbi(), reset(), traceback_nth(), trellis(), and ~trellis().

std::vector<std::vector<nthScore> >* StochHMM::trellis::nth_scoring_current
private

Definition at line 379 of file trellis.h.

Referenced by reset(), simple_nth_viterbi(), trellis(), and ~trellis().

std::vector<std::vector<nthScore> >* StochHMM::trellis::nth_scoring_previous
private

Definition at line 380 of file trellis.h.

Referenced by reset(), simple_nth_viterbi(), trellis(), and ~trellis().

size_t StochHMM::trellis::nth_size
private

Definition at line 320 of file trellis.h.

Referenced by naive_nth_viterbi(), reset(), simple_nth_viterbi(), traceback_nth(), and trellis().

std::vector<std::vector<nthScore> >* StochHMM::trellis::nth_swap_ptr
private

Definition at line 381 of file trellis.h.

Referenced by simple_nth_viterbi().

std::vector<nthTrace>* StochHMM::trellis::nth_traceback_table
private

Definition at line 352 of file trellis.h.

Referenced by reset(), simple_nth_viterbi(), traceback_nth(), trellis(), and ~trellis().

double_2D* StochHMM::trellis::posterior_score
private
std::vector<double>* StochHMM::trellis::scoring_current
private
std::vector<double>* StochHMM::trellis::scoring_previous
private
size_t StochHMM::trellis::seq_size
private
sequences* StochHMM::trellis::seqs
private
size_t StochHMM::trellis::state_size
private
stochTable* StochHMM::trellis::stochastic_table
private
bool StochHMM::trellis::store_values
private

Definition at line 328 of file trellis.h.

Referenced by reset(), store(), and trellis().

std::vector<double>* StochHMM::trellis::swap_ptr
private
std::vector<size_t>* StochHMM::trellis::swap_ptr_duration
private

Definition at line 372 of file trellis.h.

Referenced by fast_complex_viterbi(), and sparse_complex_viterbi().

int_2D* StochHMM::trellis::traceback_table
private
trellisType StochHMM::trellis::type
private

Definition at line 326 of file trellis.h.

Referenced by reset(), and trellis().

float_2D* StochHMM::trellis::viterbi_score
private

Definition at line 339 of file trellis.h.

Referenced by reset(), trellis(), and ~trellis().


The documentation for this class was generated from the following files: