StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modelTemplate.cpp
Go to the documentation of this file.
1 //
2 // stateTemplate.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 "modelTemplate.h"
29 namespace StochHMM{
30 
32 
33  stencil::stencil(std::string& txt){
34  parse(txt);
35  }
36 
37  /*!Takes a template string and finds all user defined variables (userParameters). Adds these to a set of strings */
38 
39  bool stencil::parse(std::string& txt){
40 
41  states = txt;
42  size_t opening=txt.find("<<");
43  size_t closing=0;
44  //!Find all userParameters Marks
45  while (opening!=std::string::npos){
46  closing=txt.find(">>",opening+1);
47  std::string tagName=txt.substr(opening+2,closing-opening-2);
48  UserParameterKeys.insert(tagName);
49  opening=txt.find("<<",closing+1);
50  }
51  return true;
52  }
53 
54 
55 
56 
57  //!Returns a string with template filled out
58  /*!Takes the template map and fills it out according to the specific Template you are looking at and the ID number that you want to use
59  \param Template Name of template you want to return
60  \param ID The number iterated in this return
61  \param map_template map string of strings filled out in other subroutines
62  */
63  std::string stencil::getTemplate (std::string& Template, std::string& ID, std::map<std::string,std::string>& UserValues){
64 
65  std::string FilledOutModel = states;
66  size_t beginposition=FilledOutModel.find("<<");
67  size_t endposition=0;
68  //!Find all userParameters and replace
69  while (beginposition!=std::string::npos){
70  endposition=FilledOutModel.find(">>",beginposition+1);
71  /*!Check the coordinates and replace with proper coordinats
72  The name should be only the tagName without brackets
73  But the start should be the position of first bracket and stop
74  should be the position of the last bracket */
75  std::string tagName=FilledOutModel.substr(beginposition+2,endposition-beginposition-2);
76  std::string ValueToFill = UserValues[tagName];
77  FilledOutModel.replace(beginposition,(endposition+2-beginposition),ValueToFill);
78  beginposition=FilledOutModel.find("<<",endposition+1);
79  }
80 
81  beginposition=FilledOutModel.find("((");
82  endposition=0;
83  while (beginposition!=std::string::npos){
84  endposition=FilledOutModel.find("))",beginposition+1);
85  /*! */
86  std::string tagName=FilledOutModel.substr(beginposition+2,endposition-beginposition-2);
87  std::string ValueToFill = tagName + ID;
88  FilledOutModel.replace(beginposition,(endposition+2-beginposition),ValueToFill);
89  beginposition=FilledOutModel.find("((",endposition+1);
90  }
91  return FilledOutModel;
92  }
93 
95 
96  templates::templates(std::string& template_string){
97  templates::parse(template_string);
98  }
99 
101  std::map<std::string,stencil*>::iterator it;
102  for(it=temps.begin();it!=temps.end();it++){
103  std::string key = (*it).first;
104  delete temps[key];
105  }
106  }
107 
108  //!Parses the model templates string
109  /*!Takes the string passed to it and parses out each template to then be added to the Templates structure using stensil on each one */
110  bool templates::parse(std::string& model_temp){
111 
112  std::string copyofpassedstring = model_temp;
113  size_t model_end_position=model_temp.find("TEMPLATE: ",10);
114  size_t model_start_position=0;
115  std::string modelstring = "";
116  std::string stenName;
117  size_t stenNameStart = 10;
118  size_t stenNameEnd;
119  while (model_start_position!=std::string::npos){
120  stencil* stencil_to_push = new(std::nothrow) stencil();
121  if (stencil_to_push==NULL){
122  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
123  exit(1);
124  }
125 
126  model_end_position=copyofpassedstring.find("TEMPLATE: ",(model_start_position + 10)) - 1;
127 
128  modelstring=model_temp.substr(model_start_position,model_end_position - model_start_position);
129 
130  stenNameEnd=modelstring.find("\n");
131 
132  stenName=model_temp.substr(stenNameStart,stenNameEnd-stenNameStart);
133 
134  stencil_to_push->parse(modelstring);
135  //Need to correctly push the stencil to the map
136  temps[stenName] = stencil_to_push;
137  model_start_position=model_end_position + 1;
138  }
139  return true;
140  }
141 
142 
143  //!Adds more templates when passed a template string
144  void templates::addTemplate(std::string& template_string){
145  templates::parse(template_string);
146  }
147 
148 
149 
150  //!Returns a string with template filled out
151  /*!Takes the template map and fills it out according to the specific Template you are looking at and the ID number that you want to use
152  \param Template Name of template you want to return
153  \param ID The number iterated in this return
154  */
155  std::string templates::getTemplate (std::string& Template, std::string& ID,std::map<std::string,std::string>& UserVals){
156 
157  std::string FilledOutModel = temps[Template]->getTemplate(Template,ID,UserVals);
158  return FilledOutModel;
159  }
160 
161 
162 
163 
164 
165 // //!Returns all stencils in templates as a single string
166 // std::string templates::returnAllFilledTemplates(std::string& ID,std::map<std::string,std::string>& UserVals){
167 // std::map<std::string,stencil*>::iterator it;
168 // std::string FilledOutModel = "";
169 // for(it=temps.begin();it!=temps.end();it++){
170 // std::string key = (*it).first;
171 // FilledOutModel = FilledOutModel + returnFilledTemplate (key,ID,UserVals);
172 // }
173 // return FilledOutModel;
174 // }
175 }
176