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 | Friends
StochHMM::ExDefSequence Class Reference

#include <externDefinitions.h>

List of all members.

Public Member Functions

 ExDefSequence ()
 ExDefSequence (size_t size)
bool parse (std::ifstream &, stateInfo &)
bool defined (size_t)
bool isAbsolute (size_t)
size_t getAbsState (size_t)
bool isWeighted (size_t)
bool isWeighted (size_t, size_t)
double getWeight (size_t, size_t)
void print ()
 Copy constructor for ExDefSequence.
std::string stringify ()

Private Member Functions

bool _parseAbsDef (stringList &ln, stateInfo &)
 Parse the absolute def from stringList give the stateInfo.
bool _parseWeightDef (stringList &ln, stateInfo &)
 Parse the weightDef from stringList give the stateInfo.

Private Attributes

sparseArray< ExDef * > defs

Friends

class sequences

Detailed Description

Contains external definitions for the sequence An external definition is how a particular position of the sequence is either weighted toward a state or in some cases is absolutely produced by a given state Each position can contain an external definition either absolute or states weighted by some value;

Definition at line 55 of file externDefinitions.h.


Constructor & Destructor Documentation

StochHMM::ExDefSequence::ExDefSequence ( )
inline

Definition at line 58 of file externDefinitions.h.

{};
StochHMM::ExDefSequence::ExDefSequence ( size_t  size)
inline

Creates an ExDefSequence

Parameters:
sizeamount of ExDef in the sequence

Definition at line 62 of file externDefinitions.h.

:defs(size){};

Member Function Documentation

bool StochHMM::ExDefSequence::_parseAbsDef ( stringList ln,
stateInfo info 
)
private

Parse the absolute def from stringList give the stateInfo.

Definition at line 220 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), defs, StochHMM::stringList::size(), SIZE_MAX, StochHMM::split_line(), StochHMM::stateInfo::stateIterByName, and StochHMM::stringToInt().

Referenced by parse().

{
bool start=false;
bool stop=false;
bool state=false;
size_t startPosition(SIZE_MAX);
size_t stopPosition(SIZE_MAX);
std::vector<size_t> path;
for (size_t i=2;i<ln.size();i++){
std::string& tag=ln[i];
if (ln.size()<=i+1){
std::cerr << "Missing additional information for Absolute Definition: TAG: " << ln[i] << std::endl;
return false;
}
if (tag.compare("START")==0){
int tempInt;
if (!stringToInt(ln[i+1], tempInt)){
std::cerr << "Value in External Definition START is not numeric: " << ln[i+1] << std::endl;
return false;
}
startPosition=tempInt;
start=true;
i++;
}
else if (tag.compare("END")==0){
int tempInt;
if (!stringToInt(ln[i+1], tempInt)){
std::cerr << "Value in External Definition END is not numeric: " << ln[i+1] << std::endl;
return false;
}
stopPosition=tempInt;
stop=true;
i++;
}
else if (tag.compare("TRACE")==0){
std::string& trace=ln[i+1];
std::vector<std::string> traces;
split_line(traces, trace);
//Check trace size;
size_t expectedSize=stopPosition-startPosition+1;
if (traces.size()!=expectedSize){
std::cerr << "Expected external definition trace size: " << expectedSize << "\n but got " << trace << std::endl;
return false;
}
path.assign(traces.size(),-2);
for(size_t k=0;k<traces.size();k++){
if (info.stateIterByName.count(traces[k])){
path[k]=info.stateIterByName[traces[k]];
}
else{
std::cerr << "External definition Trace state name: " << traces[k] << " doesn't exist in the model" << std::endl;
return false;
}
}
state=true;
i++;
}
else{
std::cerr << "Invalid tag found in sequence external definition: " << ln[i] <<std::endl;
}
}
//If everything is defined correctly then return true
if (start && stop && state){
for(size_t i=startPosition-1;i<stopPosition;i++){
size_t state=path[i-(startPosition-1)];
if (defs.defined(i)){
if (!defs[i]->absolute){
std::cerr << "Absolute overlaps weighted state definition" << std::endl;
}
else if (state!=defs[i]->getState()){
std::cerr << "Two absolute paths defined for " << i+1 << " position in sequence" <<std::endl;
}
}
else{
defs[i] = new (std::nothrow) ExDef;
defs[i]->setState(state);
}
}
return true;
}
return false;
}
bool StochHMM::ExDefSequence::_parseWeightDef ( stringList ln,
stateInfo info 
)
private

Parse the weightDef from stringList give the stateInfo.

Definition at line 318 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), defs, StochHMM::stringList::size(), StochHMM::stateInfo::stateIterByGff, StochHMM::stateInfo::stateIterByLabel, StochHMM::stateInfo::stateIterByName, StochHMM::stringToDouble(), and StochHMM::stringToInt().

Referenced by parse().

{
bool start(false);
bool stop(false);
bool state(false);
bool value(false);
bool valType(false);
size_t startPosition(0);
size_t stopPosition(0);
std::set<size_t> definedStates;
std::set<size_t>::iterator setIterator;
double val(0);
for (size_t line_iter=2; line_iter < ln.size();line_iter++){
std::string& tag=ln[line_iter];
if (ln.size()<=line_iter+1){
std::cerr << "External Definition for Weighted Def is missing values: " << ln[line_iter] << std::endl;
return false;
}
if (tag.compare("START")==0){
int tempInt;
if (!stringToInt(ln[line_iter+1], tempInt)){
std::cerr << "Value in External Definition START is not numeric: " << ln[line_iter+1] << std::endl;
return false;
}
startPosition=tempInt;
start=true;
line_iter++;
}
else if (tag.compare("END")==0){
size_t tempInt;
if (!stringToInt(ln[line_iter+1], tempInt)){
std::cerr << "Value in External Definition END is not numeric: " << ln[line_iter+1] << std::endl;
return false;
}
stopPosition=tempInt;
stop=true;
line_iter++;
}
else if (tag.compare("STATE_NAME")==0){
definedStates.insert(info.stateIterByName[ln[line_iter+1]]);
state=true;
line_iter++;
}
else if (tag.compare("STATE_LABEL")==0){
std::vector<size_t>& temp=info.stateIterByLabel[ln[line_iter+1]];
for(size_t temp_iter=0; temp_iter < temp.size();temp_iter++){
definedStates.insert(temp[temp_iter]);
}
state=true;
line_iter++;
}
else if (tag.compare("STATE_GFF")==0){
std::vector<size_t>& temp = info.stateIterByGff[ln[line_iter+1]];
for(size_t temp_iter=0; temp_iter < temp.size(); temp_iter++){
definedStates.insert(temp[temp_iter]);
}
state=true;
line_iter++;
}
else if (tag.compare("VALUE")==0){
double tempValue;
if(!stringToDouble(ln[line_iter+1], tempValue)){
std::cerr << "VALUE couldn't be converted to numerical value: " << ln[line_iter+1] << std::endl;
}
val=tempValue;
value=true;
line_iter++;
}
else if (tag.compare("VALUE_TYPE")==0){
std::string &type=ln[line_iter+1];
if (type.compare("P(X)")==0){
val=log(val);
valType=true;
}
else if (type.compare("LOG")==0){
valType=true;
}
else{
valType=false;
}
line_iter++;
}
else{
std::cerr << "Invalid tag found in Sequence external definition: " << ln[line_iter] << std::endl;
}
}
//Check to see that there are valid states defined in the set
if (definedStates.size()>0){
state=true;
}
else{
std::cerr << "No valid states defined by External definition" <<std::endl;
state=false;
}
//If everything is define correctly then return true
if (start && stop && state && value && valType){
for (size_t position=startPosition-1; position < stopPosition; position++){
//Already have a defined external def at position
if (defs.defined(position)){
if (defs[position]->absolute){
std::cerr << "Can't add weight to absolute external definition" << std::endl;
return false;
}
}
else{
defs[position] = new(std::nothrow) weightDef(info.stateIterByName.size());
if (defs[position]==NULL){
std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
exit(1);
}
}
// Add states and values to definition
for (setIterator=definedStates.begin();setIterator!=definedStates.end();setIterator++){
defs[position]->assignWeight(*setIterator, val);
}
}
return true;
}
return false;
}
bool StochHMM::ExDefSequence::defined ( size_t  position)

Defined if there is a external definition defined for the position

Parameters:
positionPosition of the sequence to check for external definition

Definition at line 60 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), and defs.

Referenced by StochHMM::sequences::exDefDefined().

{
if (defs.defined(position)){
return true;
}
else{
return false;
}
}
size_t StochHMM::ExDefSequence::getAbsState ( size_t  position)

Get the absolute state defined for the position in the sequence

Parameters:
positionPosition of the sequence
Returns:
integer indice to state

Definition at line 85 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), and defs.

{
if(defs.defined(position) && defs[position]->isAbsolute()){
return defs[position]->getState();
}
else{
std::cerr << "Calling getAbsState on weighted state" << std::endl;
return -1;
}
}
double StochHMM::ExDefSequence::getWeight ( size_t  position,
size_t  stateIter 
)

Get the weight for the external definition at a position in the sequence

Parameters:
positionPosition in the sequence
stateIterIndex of state to get weight
Returns:
double value of weight to apply to state at the position

Definition at line 111 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), and defs.

Referenced by StochHMM::sequences::getWeight().

{
if (defs.defined(position)){
return defs[position]->getWeight(stateIter);
}
else{
return 0.0;
}
}
bool StochHMM::ExDefSequence::isAbsolute ( size_t  position)

External definitions are either absolute or weighted. Checks to see if the *external definition at position in sequence is absolute

Parameters:
positionPosition in teh sequence
Returns:
true if the external definition is absolute

Definition at line 74 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), and defs.

{
if (defs.defined(position) && defs[position]->isAbsolute()){
return true;
}
return false;
}
bool StochHMM::ExDefSequence::isWeighted ( size_t  position)

Check to see if the external definition at the position is weighted and not absolute

Parameters:
positionPosition in the sequence
Returns:
true if the external definition is weighted

Definition at line 100 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), and defs.

{
if (defs.defined(position)){
return !defs[position]->isAbsolute();
}
return false;
}
bool StochHMM::ExDefSequence::isWeighted ( size_t  ,
size_t   
)
bool StochHMM::ExDefSequence::parse ( std::ifstream &  file,
stateInfo info 
)

Parses the ExDefSequence from a file stream

Parameters:
fileFile stream to be used to parse the External definitions from
Returns:
true if parsing was successful

Definition at line 182 of file externDefinitions.cpp.

References _parseAbsDef(), _parseWeightDef(), and StochHMM::stringList::fromDef().

Referenced by StochHMM::sequence::getFasta(), and StochHMM::sequence::getReal().

{
//use getDefs to parse the lines
//and create external definition
stringList ln;
std::string comment_char="#";
std::string ws=" []";
std::string split="\t:";
while(ln.fromDef(file, ws, split)){
if (ln[0].compare("EXDEF")==0){
if (ln[1].compare("ABSOLUTE")==0){
_parseAbsDef(ln,info);
}
else if (ln[1].compare("WEIGHTED")==0){
_parseWeightDef(ln,info);
}
else{
std::cerr << ln[0] << " not a valid external definition\n";
continue;
}
}
char nl_peek=file.peek();
if (nl_peek!='['){
break;
}
}
//defs->print();
return true;
}
void StochHMM::ExDefSequence::print ( )

Copy constructor for ExDefSequence.

Print the External definitions to stdout

Definition at line 49 of file externDefinitions.cpp.

References StochHMM::sparseArray< T >::defined(), defs, and StochHMM::sparseArray< T >::size().

{
for(size_t i = 0;i<defs.size();i++){
if (defs.defined(i)){
std::cout << i << "\t" << defs[i]->stringify() << std::endl;
}
}
return;
}
std::string StochHMM::ExDefSequence::stringify ( )

Friends And Related Function Documentation

friend class sequences
friend

Definition at line 62 of file externDefinitions.h.


Member Data Documentation

sparseArray<ExDef*> StochHMM::ExDefSequence::defs
private

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