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::alignment Class Reference

#include <alignment.h>

List of all members.

Public Member Functions

 alignment ()
double align (alignment_type typ)
double align (sequence &target, sequence &query, alignment_type typ)
double align (sequence &target, sequence &query, alignment_type typ, double match, double mismatch, double gap, double gapext)
double align (sequence &target, sequence &query, alignment_type typ, const mmMatrix &, double gap, double gapext)
double calcLambda (std::vector< double > &)
double estimateLambda ()
double calc_pvalue ()
void setMatrix (mmMatrix &)
void setMatch (double)
void setMismatch (double)
void setGap (double value)
void setGapExt (double value)
void setTarget (sequence &)
void setQuery (sequence &)
int getHighScore ()
size_t getAlignmentSize ()
std::string getAlignment ()
void printAlignment ()
void printTrellis ()
std::string traceback ()
std::string stochasticTraceback ()

Private Member Functions

void _reset ()
void _resetSeqs ()
double _getGapScore (size_t, size_t, tracebackDirection)
void _initTrellis (size_t, size_t)

Private Attributes

sequencetarget
sequencequery
tracktr
std::vector< std::vector< cell > > * trellis
std::vector< std::vector
< double > > * 
mMatrix
double gap
double gap_ext
double high_score
size_t start_position
size_t end_position

Detailed Description

Definition at line 46 of file alignment.h.


Constructor & Destructor Documentation

StochHMM::alignment::alignment ( )

Definition at line 19 of file alignment.cpp.

:gap(0),gap_ext(0),high_score(0),start_position(0),target(NULL),query(NULL),tr(NULL){
}

Member Function Documentation

double StochHMM::alignment::_getGapScore ( size_t  queryIndex,
size_t  targetIndex,
tracebackDirection  dir 
)
private

Definition at line 167 of file alignment.cpp.

References gap, gap_ext, StochHMM::LEFT, and StochHMM::UP.

Referenced by align().

{
if (gap == gap_ext){
return gap;
}
else{
if (dir == LEFT){
if ( (*trellis)[queryIndex][targetIndex].getTraceback() == LEFT){
return gap_ext;
}
else{
return gap;
}
}
else{
if ( (*trellis)[queryIndex][targetIndex].getTraceback() == UP){
return gap_ext;
}
else{
return gap;
}
}
}
}
void StochHMM::alignment::_initTrellis ( size_t  rows,
size_t  columns 
)
private

Definition at line 192 of file alignment.cpp.

References gap, gap_ext, StochHMM::LEFT, StochHMM::NONE, StochHMM::cell::setDiag(), StochHMM::cell::setLeft(), StochHMM::cell::setScore(), StochHMM::cell::setTraceback(), StochHMM::cell::setUp(), trellis, and StochHMM::UP.

Referenced by align().

{
if (trellis!=NULL){
delete trellis;
trellis=NULL;
}
cell x;
x.setTraceback(NONE);
x.setScore(0.0);
x.setDiag(0.0);
x.setLeft(0.0);
x.setUp(0.0);
std::vector<cell> row (columns+1,x);
trellis= new std::vector<std::vector<cell> > (rows+1, row);
(*trellis)[0][0].setTraceback(NONE);
(*trellis)[0][1].setScore(gap);
(*trellis)[0][1].setTraceback(LEFT);
(*trellis)[1][0].setScore(gap);
(*trellis)[1][0].setTraceback(UP);
for(size_t i=2;i<=columns;i++){
(*trellis)[0][i].setScore((*trellis)[0][i-1].getScore()+gap_ext);
(*trellis)[0][i].setTraceback(LEFT);
}
for(size_t j=2;j<=rows;j++){
(*trellis)[j][0].setScore((*trellis)[j-1][0].getScore()+gap_ext);
(*trellis)[j][0].setTraceback(UP);
}
return;
}
void StochHMM::alignment::_reset ( )
private

Definition at line 24 of file alignment.cpp.

References end_position, gap, gap_ext, high_score, mMatrix, query, start_position, target, and tr.

{
target=NULL;
query=NULL;
tr=NULL;
delete mMatrix;
mMatrix=NULL;
gap=0;
return;
}
void StochHMM::alignment::_resetSeqs ( )
private

Definition at line 38 of file alignment.cpp.

References end_position, high_score, mMatrix, query, start_position, and target.

Referenced by setQuery(), and setTarget().

{
target=NULL;
query=NULL;
delete mMatrix;
mMatrix=NULL;
}
double StochHMM::alignment::align ( alignment_type  typ)

Definition at line 247 of file alignment.cpp.

References _getGapScore(), _initTrellis(), StochHMM::cGlobal, StochHMM::cLocal, StochHMM::DIAGONAL, StochHMM::sequence::getSymbol(), StochHMM::LEFT, printTrellis(), query, StochHMM::sequence::seqValue(), StochHMM::sequence::size(), target, and StochHMM::UP.

Referenced by align(), and calc_pvalue().

{ //(sequence &top, sequence &side, double gap, double mismatch, double match){
int top_size=target->size();
int side_size=query->size();
_initTrellis(side_size, top_size);
double max_score(-INFINITY);
size_t max_row_index(0);
size_t max_col_index(0);
//Fill
for(size_t top_index=1;top_index<=top_size;top_index++){
for(size_t side_index=1;side_index<=side_size;side_index++){
//table[side_index-1][top_index-1].setDiag();
short targetLetter = query->seqValue(side_index-1);
short queryLetter = target->seqValue(top_index-1);
double diagonal_score = (*trellis)[side_index-1][top_index-1].getScore() + (*mMatrix)[queryLetter][targetLetter];
double left_score=(*trellis)[side_index][top_index-1].getScore()+_getGapScore(side_index, top_index-1, LEFT);
double up_score=(*trellis)[side_index-1][top_index].getScore()+_getGapScore(side_index-1, top_index, UP);
if (typ == cLocal){
if (diagonal_score<0.0){
diagonal_score=0.0;
}
else if (diagonal_score>max_score){
max_score = diagonal_score;
max_row_index=side_index;
max_col_index=top_index;
}
if (left_score<0.0){
left_score=0.0;
}
else if (left_score>max_score){
max_score = left_score;
max_row_index=side_index;
max_col_index=top_index;
}
if (up_score<0.0){
up_score=0.0;
}
else if (up_score>max_score){
max_score = up_score;
max_row_index=side_index;
max_col_index=top_index;
}
}
if (diagonal_score>=up_score && diagonal_score >= left_score){
(*trellis)[side_index][top_index].setScore(diagonal_score);
(*trellis)[side_index][top_index].setTraceback(DIAGONAL);
}
else if(up_score>=left_score){
(*trellis)[side_index][top_index].setScore(up_score);
(*trellis)[side_index][top_index].setTraceback(UP);
}
else{
(*trellis)[side_index][top_index].setScore(left_score);
(*trellis)[side_index][top_index].setTraceback(LEFT);
}
}
}
std::string align_top;
std::string align_side;
std::string align_status;
size_t top_iter;
size_t side_iter;
if (typ == cGlobal){
top_iter=top_size;
side_iter=side_size;
}
else if (typ == cLocal){
top_iter=max_col_index;
side_iter=max_row_index;
}
while (top_iter!=0 || side_iter!=0){
std::cout << side_iter << "," << top_iter << "\t" << (*trellis)[side_iter][top_iter].getScore() << std::endl;
switch ((*trellis)[side_iter][top_iter].getTraceback()){
case DIAGONAL:
align_side+=query->getSymbol(--side_iter);
align_top+=target->getSymbol(--top_iter);
align_status+= (query->seqValue(side_iter) != target->seqValue(top_iter)) ? ' ' : '|';
break;
case UP:
align_side+=query->getSymbol(--side_iter);
align_top+="-";
align_status+=" ";
break;
case LEFT:
//cout << "Case LEFT:" << side_size << "\t" << top_size << endl;
align_top+=target->getSymbol(--top_iter);
align_side+="-";
align_status+=" ";
break;
default:
//cout << "We have a problem Houston\n" << endl;
break;
}
if (typ == cLocal && fabs((*trellis)[side_iter][top_iter].getScore())<0.001){
break;
}
}
std::reverse(align_top.begin(), align_top.end());
std::reverse(align_side.begin(), align_side.end());
std::reverse(align_status.begin(), align_status.end());
std::cout << align_top << std::endl << align_status << std::endl << align_side << std::endl;
return (*trellis)[side_size][top_size].getScore();
}
double StochHMM::alignment::align ( sequence target,
sequence query,
alignment_type  typ 
)
double StochHMM::alignment::align ( sequence target,
sequence query,
alignment_type  typ,
double  match,
double  mismatch,
double  gap,
double  gapext 
)

Definition at line 235 of file alignment.cpp.

References align(), setGap(), setGapExt(), setMatch(), setMismatch(), setQuery(), and setTarget().

{
setTarget(tgt);
setQuery(qu);
setMatch(mtch);
setMismatch(mismtch);
setGap(gp);
setGapExt(gp_ext);
return align(typ);
}
double StochHMM::alignment::align ( sequence target,
sequence query,
alignment_type  typ,
const mmMatrix ,
double  gap,
double  gapext 
)
double StochHMM::alignment::calc_pvalue ( )

Definition at line 400 of file alignment.cpp.

References align(), StochHMM::cGlobal, query, and setQuery().

{
std::vector<double> scores;
for(int i=0;i<100;++i){
sequence* oldQuery=query;
sequence shuffled=shuffle(query);
setQuery(shuffled);
double score = align(cGlobal);
scores.push_back(score);
std::cout<< score << std::endl;
query=oldQuery;
}
return 0.0;
}
double StochHMM::alignment::calcLambda ( std::vector< double > &  frequencies)

Definition at line 382 of file alignment.cpp.

{
return 0;
}
double StochHMM::alignment::estimateLambda ( )

Definition at line 387 of file alignment.cpp.

References mMatrix.

{
size_t alphaSize = mMatrix->size();
double estimatedFreq= 1/static_cast<double>(alphaSize);
double expectedScore(0);
for ( size_t row=0; row<alphaSize; ++row){
for ( size_t col=0; col<alphaSize; ++col){
}
}
}
std::string StochHMM::alignment::getAlignment ( )
size_t StochHMM::alignment::getAlignmentSize ( )
inline

Definition at line 72 of file alignment.h.

References end_position, and start_position.

{
return 0;
}
int StochHMM::alignment::getHighScore ( )
inline

Definition at line 70 of file alignment.h.

References high_score.

{return high_score;}
void StochHMM::alignment::printAlignment ( )
void StochHMM::alignment::printTrellis ( )

Definition at line 226 of file alignment.cpp.

Referenced by align().

{
for (size_t row=0;row<trellis->size();++row){
for (size_t col = 0; col<(*trellis)[row].size();++col){
std::cout << (*trellis)[row][col].getScore() << " " << (*trellis)[row][col].getTraceback() << "\t";
}
std::cout << std::endl;
}
}
void StochHMM::alignment::setGap ( double  value)
inline

Definition at line 64 of file alignment.h.

Referenced by align().

{gap = value;}
void StochHMM::alignment::setGapExt ( double  value)
inline

Definition at line 65 of file alignment.h.

References gap_ext.

Referenced by align().

{gap_ext = value;}
void StochHMM::alignment::setMatch ( double  value)

Definition at line 48 of file alignment.cpp.

References mMatrix, query, StochHMM::sequence::size(), and target.

Referenced by align().

{
if (target==NULL && query==NULL){
std::cerr << "Either target or query sequence must be set before defining match matrix" << std::endl;
return;
}
else{
size_t alphaSize=(target!=NULL) ? target->size() : query->size();
if (mMatrix==NULL){
std::vector<double> row (alphaSize, 0.0);
mMatrix->insert(mMatrix->begin(),alphaSize,row);
//std::cout << mMatrix->size() <<std::endl;
//std::cout << (*mMatrix)[0].size();
}
else if (mMatrix->size()!=alphaSize){
delete mMatrix;
mMatrix=NULL;
std::vector<double> row (alphaSize,0.0);
mMatrix->insert(mMatrix->begin(),alphaSize,row);
}
for(size_t i=0;i<alphaSize;++i){
(*mMatrix)[i][i]=value;
}
}
return;
}
void StochHMM::alignment::setMatrix ( mmMatrix matrix)

Definition at line 78 of file alignment.cpp.

References mMatrix, query, StochHMM::sequence::size(), and target.

{
if (target==NULL && query==NULL){
std::cerr << "Either target or query sequence must be set before defining match matrix" << std::endl;
return;
}
else{
size_t alphaSize=(target!=NULL) ? target->size() : query->size();
if (mMatrix==NULL){
mMatrix=&matrix;
}
else{
delete mMatrix;
mMatrix=&matrix;
}
}
return;
}
void StochHMM::alignment::setMismatch ( double  value)

Definition at line 96 of file alignment.cpp.

References mMatrix, query, StochHMM::sequence::size(), and target.

Referenced by align().

{
if (target==NULL && query==NULL){
std::cerr << "Either target or query sequence must be set before defining match matrix" << std::endl;
return;
}
else{
size_t alphaSize=(target!=NULL) ? target->size() : query->size();
if (mMatrix==NULL){
std::vector<double> row (0.0,alphaSize);
mMatrix->insert(mMatrix->begin(),alphaSize,row);
}
else if (mMatrix->size()!=alphaSize){
delete mMatrix;
mMatrix=NULL;
std::vector<double> row (0.0,alphaSize);
mMatrix->insert(mMatrix->begin(),alphaSize,row);
}
for(size_t row=0;row<alphaSize;++row){
for(size_t col=0;col<alphaSize;++col){
if (row!=col){
(*mMatrix)[row][col]=value;
}
}
}
}
return;
}
void StochHMM::alignment::setQuery ( sequence seq)

Definition at line 147 of file alignment.cpp.

References _resetSeqs(), end_position, StochHMM::sequence::getTrack(), high_score, query, start_position, and tr.

Referenced by align(), and calc_pvalue().

{
track* temp = seq.getTrack();
if (tr==temp){
query= &seq;
}
else if (tr==NULL){
query = &seq;
tr=temp;
}
else{
query = &seq;
tr=temp;
}
return;
}
void StochHMM::alignment::setTarget ( sequence seq)

Definition at line 127 of file alignment.cpp.

References _resetSeqs(), end_position, StochHMM::sequence::getTrack(), high_score, start_position, target, and tr.

Referenced by align().

{
track* temp = seq.getTrack();
if (tr==temp){
target= &seq;
}
else if (tr==NULL){
target = &seq;
tr=temp;
}
else{
target = &seq;
tr=temp;
}
return;
}
std::string StochHMM::alignment::stochasticTraceback ( )
std::string StochHMM::alignment::traceback ( )

Member Data Documentation

size_t StochHMM::alignment::end_position
private

Definition at line 101 of file alignment.h.

Referenced by _reset(), _resetSeqs(), getAlignmentSize(), setQuery(), and setTarget().

double StochHMM::alignment::gap
private

Definition at line 96 of file alignment.h.

Referenced by _getGapScore(), _initTrellis(), and _reset().

double StochHMM::alignment::gap_ext
private

Definition at line 97 of file alignment.h.

Referenced by _getGapScore(), _initTrellis(), _reset(), and setGapExt().

double StochHMM::alignment::high_score
private

Definition at line 99 of file alignment.h.

Referenced by _reset(), _resetSeqs(), getHighScore(), setQuery(), and setTarget().

std::vector<std::vector<double> >* StochHMM::alignment::mMatrix
private

Definition at line 94 of file alignment.h.

Referenced by _reset(), _resetSeqs(), estimateLambda(), setMatch(), setMatrix(), and setMismatch().

sequence* StochHMM::alignment::query
private

Definition at line 89 of file alignment.h.

Referenced by _reset(), _resetSeqs(), align(), calc_pvalue(), setMatch(), setMatrix(), setMismatch(), and setQuery().

size_t StochHMM::alignment::start_position
private

Definition at line 100 of file alignment.h.

Referenced by _reset(), _resetSeqs(), getAlignmentSize(), setQuery(), and setTarget().

sequence* StochHMM::alignment::target
private

Definition at line 88 of file alignment.h.

Referenced by _reset(), _resetSeqs(), align(), setMatch(), setMatrix(), setMismatch(), and setTarget().

track* StochHMM::alignment::tr
private

Definition at line 91 of file alignment.h.

Referenced by _reset(), setQuery(), and setTarget().

std::vector<std::vector<cell> >* StochHMM::alignment::trellis
private

Definition at line 93 of file alignment.h.

Referenced by _initTrellis().


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