StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
externalFuncs.cpp
Go to the documentation of this file.
1 //
2 // externalFuncs.cpp
3 //Copyright (c) 2007-2012 Paul C Lott
4 //University of California, Davis
5 //Genome and Biomedical Sciences Facility
6 //UC Davis Genome Center
7 //Ian Korf Lab
8 //Website: www.korflab.ucdavis.edu
9 //Email: lottpaul@gmail.com
10 //
11 //Permission is hereby granted, free of charge, to any person obtaining a copy of
12 //this software and associated documentation files (the "Software"), to deal in
13 //the Software without restriction, including without limitation the rights to
14 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
15 //the Software, and to permit persons to whom the Software is furnished to do so,
16 //subject to the following conditions:
17 //
18 //The above copyright notice and this permission notice shall be included in all
19 //copies or substantial portions of the Software.
20 //
21 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
23 //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
24 //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 
28 #include "externalFuncs.h"
29 namespace StochHMM{
30 
31  //TODO: Create non-model import way of creating and defining externalFuncs class
32 
33  //!Create transFuncParam from a stringList parsed from model for given track and applying a weight as defined in the model
34  //! \param lst stringList from parsing the line in the function
35  //! \param trcks tracks defined in the model
36  //! \param wts pointer to weights as defined in the model
37  //! \param funcs pointer to the state functions defined by user and referenced in the model
38 
40  transFunc=NULL;
41  }
42 
43 // transitionFuncParam::transitionFuncParam(stringList& lst, tracks& trcks, weights* wts, StateFuncs* funcs){
44 // transFunc=NULL;
45 // parse(lst,trcks, wts,funcs);
46 // }
47 
48 
49  //!Parse the stringList from model import
51 
52  size_t idx;
53 
54  //FUNCTION NAME (REQUIRED)
55  if (lst.contains("FUNCTION")){
56  idx=lst.indexOf("FUNCTION");
57  idx++;
58  transFuncName = lst[idx];
59  if (funcs!=NULL){
61  }
62  }
63  else{
64  std::cerr << "Tag was parsed but contains no FUNCTION: . Please check the formatting of the tags\n" << std::endl;
65  return false;
66 
67  //errorInfo(sCantParseLine, "Tag was parsed but contains no FUNCTION: . Please check the formatting of the tags\n");
68  }
69 
70  //Implement Which track to pass to function
71 
72  if (lst.contains("TRACK")){
73  idx=lst.indexOf("TRACK");
74  idx++;
75  trackName=lst[idx];
77  }
78  else{
79  std::cerr << "Tag was parsed but contains no TRACK: . Please check the formatting of the tags\n" << std::endl;
80  return false;
81  //errorInfo(sCantParseLine, "Tag was parsed but contains no TRACK: . Please check the formatting of the tags\n");
82  }
83 
84 
85 
86  if (lst.contains("SCALE")){
87  idx=lst.indexOf("SCALE");
88  idx++;
89  if (isNumeric(lst[idx])){
90  transFuncScaling = new(std::nothrow) weight;
91 
92  if (transFuncScaling==NULL){
93  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
94  exit(1);
95  }
96 
97  double tempValue;
98  if (!stringToDouble(lst[idx], tempValue)){
99  std::cerr << "Ambiguous Value couldn't be parsed: "<< lst[idx] << std::endl;
100  return false;
101  }
102 
103  transFuncScaling->setAbsolute(tempValue);
104  }
105  else{
106  std::string weightName=lst[idx];
107  if (wts->count(weightName)){
108  transFuncScaling = (*wts)[weightName];
109  }
110  }
111 
112  }
113 
114 
115  //Process Traceback Commands
116 
117  //Parse the TO Traceback Labels in the function tag
118  const std::string tbLabels[]= {"TO_LABEL","TB->LABEL","TO_GFF","TB->GFF","TO_STATE","TB->STATE","TO_START","TB->START","DIFF_STATE"};
120 
121  for(int i=0;i<9;i++){
122  if (lst.contains(tbLabels[i])){
123 
124  transFuncTraceback=true;
125 
126  idx=lst.indexOf(tbLabels[i]);
128 
129  if (typs[i]!=START_INIT || typs[i]!=DIFF_STATE){
130  transFuncTracebackString=lst[idx+1];
131  }
132  }
133  }
134 
135 
136  // Parse the Combine tags
137  if (transFuncTraceback){
138  //Process Traceback Combining Commands
139  std::string combineLabels[] = {"COMBINE_LABEL","COMBINE_GFF","COMBINE_STATE", "NO_COMBINE"};
140  const combineIdentifier comtyps[] = {STATELABEL,STATEGFF,STATENAME,FULL};
141  bool combineTypeProvided=false;
142  for(int i=0;i<4;i++){
143  if (lst.contains(combineLabels[i])){
144  idx=lst.indexOf(combineLabels[i]);
145  transFuncCombineIdentifier=comtyps[i];
146  transFuncCombineString=lst[idx+1];
147  combineTypeProvided=true;
148  }
149  }
150 
151  if (!combineTypeProvided){
152  std::cerr << "Transition Function tag with a traceback was called, but no CombineType was provided. If no traceback is needed then please remove traceback command. \n" << std::endl;
153  return false;
154 
155  //errorInfo(sTagIncorrect, "Transition Function tag with a traceback was called, but no CombineType was provided. If no traceback is needed then please remove traceback command. \n");
156  }
157  }
158 
159 
160  return true;
161  }
162 
163  //!Print the externalFuncts to stdout
165  std::cout<<stringify()<<std::endl;
166  }
167 
168  //!Get string representation of the externalFuncs
169  //! \return std::string Representation of externalFuncs
171  std::string exFuncString;
172  exFuncString+="[ FUNCTION:\t" + transFuncName + "\t";
173  exFuncString+="TRACK:\t" + trackName + "\t";
174  if (transFuncTraceback){
175  exFuncString+= (transFuncTracebackIdentifier == STATE_NAME) ? "TO_STATE:\t" + transFuncTracebackString + "\t":
178  (transFuncTracebackIdentifier == START_INIT) ? "TO_START:\t": "DIFF_STATE:\t" ;
179 
180  exFuncString+= (transFuncCombineIdentifier == STATENAME) ? "COMBINE_STATE:\t" + transFuncCombineString + "\t":
181  (transFuncCombineIdentifier == STATELABEL) ? "COMBINE_LABEL:\t" + transFuncCombineString + "\t":
182  (transFuncCombineIdentifier == STATEGFF) ? "COMBINE_GFF:\t" + transFuncCombineString :"NO_COMBINE:\t";
183  }
184 
185  if (transFuncScaling!=NULL){
187  exFuncString+="SCALE:\t" + double_to_string(transFuncScaling->getAbsolute());
188  }
189  else{
190  exFuncString+="SCALE:\t" + transFuncScaling->getName();
191  }
192  }
193 
194  exFuncString += " ]";
195 
196  return exFuncString;
197  }
198 
199 
200  //! Add function to externFunc
201  //! \param funcName std::string name given to function (as referenced in model)
202  //! \param function Pointer to pt2StateFunc*, function to use
203  //! \param scaling Pointer to weight, how to weight the functions results before applying to emission/transition
204  void transitionFuncParam::setTransFunc(std::string& funcName, transitionFunc* function, weight* scaling){
205  transFunc=function;
206  transFuncName=funcName;
207  transFuncScaling=scaling;
208  }
209 
210  //!Set all the traceback and combine types for the traceback before calling the function
211  //! \param tbIdent tracebackIdentifier
212  //! \param tbString Traceback string to use (State/GFF/Label)
213  //! \param cbIdent combineIdentifier
214  //! \param cbString Combine string to use when editing traceback (State/GFF/Label)
215  void transitionFuncParam::setTransTB(tracebackIdentifier tbIdent,std::string& tbString, combineIdentifier cbIdent, std::string& cbString){
217  transFuncTracebackString=tbString;
219  transFuncCombineString=cbString;
220  transFuncTraceback=true;
221  return;
222  }
223 
224 
225  double transitionFuncParam::evaluate(const std::string* fullSequence, size_t pos, const std::string* partialSequence,size_t length){
226  return (*transFunc)(fullSequence, pos, partialSequence,length);
227  }
228 
229 
230 
231  emissionFuncParam::emissionFuncParam(std::string& functionName, StateFuncs* funcs,track* trk){
232  emissionFunction=NULL;
233 
234  emissionFunction=funcs->getEmissionFunction(functionName);
235 
236  emissionFuncTrack=trk;
237  trackName=trk->getName();
238  trackNumber=trk->getIndex();
239  emissionFuncName = functionName;
240  return;
241  }
242 
243 
244  //!Create emissionFuncParam from a stringList parsed from model for given track and applying a weight as defined in the model
245  //! \param lst stringList from parsing the line in the function
246  //! \param trcks tracks defined in the model
247  //! \param wts pointer to weights as defined in the model
248  //! \param funcs pointer to the state functions defined by user and referenced in the model
249 
251  emissionFunction=NULL;
252  //parse(lst,trcks, wts,funcs);
253  }
254 
255 
256  //!Parse the stringList from model import
257  bool emissionFuncParam::parse(stringList& lst, tracks& trcks, weights* wts, StateFuncs* funcs){
258 
259  size_t idx;
260 
261  //FUNCTION NAME (REQUIRED)
262  if (lst.contains("FUNCTION")){
263  idx=lst.indexOf("FUNCTION");
264  idx++;
265  emissionFuncName = lst[idx];
266  if (funcs!=NULL){
268  }
269  }
270  else{
271  std::cerr << "Tag was parsed but contains no FUNCTION: . Please check the formatting of the tags\n" << std::endl;
272  return false;
273 
274  //errorInfo(sCantParseLine, "Tag was parsed but contains no FUNCTION: . Please check the formatting of the tags\n");
275  }
276 
277  //Implement Which track to pass to function
278 
279  if (lst.contains("TRACK")){
280  idx=lst.indexOf("TRACK");
281  idx++;
282  trackName=lst[idx];
285  }
286  else{
287  std::cerr << "Tag was parsed but contains no TRACK: . Please check the formatting of the tags\n" << std::endl;
288  return false;
289 
290  //errorInfo(sCantParseLine, "Tag was parsed but contains no TRACK: . Please check the formatting of the tags\n");
291  }
292 
293 
294 
295  if (lst.contains("SCALE")){
296  idx=lst.indexOf("SCALE");
297  idx++;
298  if (isNumeric(lst[idx])){
299  emissionFuncScaling = new(std::nothrow) weight;
300 
301  if (emissionFuncScaling==NULL){
302  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
303  exit(1);
304  }
305 
306  double tempValue;
307  if (!stringToDouble(lst[idx], tempValue)){
308  std::cerr << "SCALE value could not be converted to numerical value: "<< lst[idx] << std::endl;
309  return false;
310  }
311 
312  emissionFuncScaling->setAbsolute(tempValue);
313  }
314  else{
315  std::string weightName=lst[idx];
316  if (wts->count(weightName)){
317  emissionFuncScaling = (*wts)[weightName];
318  }
319  }
320 
321  }
322 
323  return true;
324  }
325 
326  //!Print the externalFuncts to stdout
328  std::cout<<stringify()<<std::endl;
329  }
330 
331  //!Get string representation of the externalFuncs
332  //! \return std::string Representation of externalFuncs
334  std::string exFuncString;
335  exFuncString+="[ FUNCTION:\t" + emissionFuncName + "\t";
336  exFuncString+="TRACK:\t" + trackName + "\t";
337 
338  if (emissionFuncScaling!=NULL){
340  exFuncString+="SCALE:\t" + double_to_string(emissionFuncScaling->getAbsolute());
341  }
342  else{
343  exFuncString+="SCALE:\t" + emissionFuncScaling->getName();
344  }
345  }
346 
347  exFuncString += " ]";
348 
349  return exFuncString;
350  }
351 
352 
353  double emissionFuncParam::evaluate(const std::string* fullSequence, size_t pos){
354  double val = (*emissionFunction)(fullSequence, pos);
355 
356  if (emissionFuncScaling!=NULL){
358  }
359 
360  return val;
361  }
362 
363  double emissionFuncParam::evaluate(sequences& seqs , size_t pos){
364  double val = (*emissionFunction)(seqs.getUndigitized(trackNumber),pos);
365 
366  if (emissionFuncScaling!=NULL){
368  }
369 
370  return val;
371  }
372 
373  double emissionFuncParam::evaluate(sequence& seq , size_t pos){
374  double val = (*emissionFunction)(seq.getUndigitized(),pos);
375 
376  if (emissionFuncScaling!=NULL){
378  }
379 
380  return val;
381  }
382 
383 }