StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
index.cpp
Go to the documentation of this file.
1 //
2 // index.cpp
3 
4 //Copyright (c) 2007-2012 Paul C Lott
5 //University of California, Davis
6 //Genome and Biomedical Sciences Facility
7 //UC Davis Genome Center
8 //Ian Korf Lab
9 //Website: www.korflab.ucdavis.edu
10 //Email: lottpaul@gmail.com
11 //
12 //Permission is hereby granted, free of charge, to any person obtaining a copy of
13 //this software and associated documentation files (the "Software"), to deal in
14 //the Software without restriction, including without limitation the rights to
15 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16 //the Software, and to permit persons to whom the Software is furnished to do so,
17 //subject to the following conditions:
18 //
19 //The above copyright notice and this permission notice shall be included in all
20 //copies or substantial portions of the Software.
21 //
22 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24 //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
25 //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26 //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 
29 #include "index.h"
30 namespace StochHMM{
31 
32  //!Create an index
34  index=0;
35  amb=NULL;
36  ambiguous=false;
37  }
38 
39  //!Create an Index by copy
40  Index::Index(const Index& a){
41  index=a.index;
43 
44  if (amb!=NULL){
45  amb=new(std::nothrow) std::vector<size_t>(a.amb->size());
46 
47  if (amb==NULL){
48  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
49  exit(1);
50  }
51 
52  copy( a.amb->begin(), a.amb->end(), amb->begin());
53  }
54  }
55 
56  //!Destroy Index
58  delete amb;
59  amb=NULL;
60  }
61 
62 
63 
64  //!Assign Index by copy
65  //!\param rhs
67  index=rhs.index;
68  ambiguous=rhs.ambiguous;
69 
70  if (amb!=NULL){
71  amb=new(std::nothrow) std::vector<size_t>(rhs.amb->size());
72 
73  if (amb==NULL){
74  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
75  exit(1);
76  }
77 
78  copy( rhs.amb->begin(), rhs.amb->end(), amb->begin());
79  }
80  return *this;
81  }
82 
83  //! Add indexes together
84  //! \param lhs
85  //! \return Index with values added
87  Index temp;
88  temp.index=lhs.index + this->index;
89  if (ambiguous){
90  if (lhs.ambiguous){
91  temp.ambiguous=true;
92  temp.amb=new(std::nothrow) std::vector<size_t>;
93 
94  if (temp.amb==NULL){
95  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
96  exit(1);
97  }
98 
99  addVectorCombinatorial(*temp.amb, *this->amb, *lhs.amb);
100 
101  }
102  else{
103  temp.ambiguous=true;
104  temp.amb=new(std::nothrow) std::vector<size_t>(*this->amb);
105 
106  if (temp.amb==NULL){
107  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
108  exit(1);
109  }
110 
111  addValueToVector(*temp.amb, lhs.index);
112  }
113  }
114  else{
115  if (lhs.ambiguous){
116  temp.ambiguous=true;
117  temp.amb=new(std::nothrow) std::vector<size_t>(*lhs.amb);
118 
119  if (temp.amb==NULL){
120  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
121  exit(1);
122  }
123 
124  addValueToVector(*temp.amb, this->index);
125  }
126  }
127  return temp;
128  }
129 
130  //!Add and Assign
131 
132  void Index::operator+= (const Index& lhs){
133  if (!ambiguous && !lhs.ambiguous){
134  index+=lhs.index;
135  return;
136  }
137  else if (ambiguous){
138  if (lhs.ambiguous){
139  std::vector<size_t>* temp = new(std::nothrow) std::vector<size_t>;
140 
141  if (temp==NULL){
142  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
143  exit(1);
144  }
145 
146  addVectorCombinatorial(*temp, *(this->amb), *(lhs.amb));
147  delete amb;
148  amb=temp;
149  }
150  else{
151  addValueToVector(*this->amb, lhs.index);
152  }
153  }
154  else{
155  if (lhs.ambiguous){
156  amb=new(std::nothrow) std::vector<size_t>(*lhs.amb);
157  ambiguous = true;
158 
159  if (amb==NULL){
160  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
161  exit(1);
162  }
163 
165  }
166  }
167  return;
168  }
169 
170  //!Add integer index to Index
171  //! \param lhs Integer index to add to Index
172  void Index::operator+= (const size_t lhs){
173  if (ambiguous){
174  addValueToVector(*amb, lhs);
175  }
176  else{
177  index+=lhs;
178  }
179  return;
180  }
181 
182  //!Multiply Indices by integer
183  //! \param lhs Integer value
184  void Index::operator*= (const size_t lhs){
185  if (ambiguous){
186  multiplyValueToVector(*amb, lhs);
187  }
188  else{
189  index*=lhs;
190  }
191  return;
192  }
193 
194 
195  //!Multiply Indices by integer
196  Index Index::operator* (const size_t lhs){
197  Index temp;
198  if (ambiguous){
199  multiplyValueToVector(*amb, lhs);
200  }
201  else{
202  index*=lhs;
203  }
204  return temp;
205  }
206 
207  //!Get the number of indices in Index
208  size_t Index::size(){
209  if (ambiguous){
210  return amb->size();
211  }
212  else{
213  return 1;
214  }
215  }
216 
217 
218  //! Get the index at position in Index
219  //!\param iter Integer iterator of position
220  //!\return int Integer value of index
221  size_t Index::operator[](const size_t iter){
222  if (!ambiguous && iter==0){
223  return index;
224  }
225  else if (ambiguous){
226  return (*amb)[iter];
227  }
228  else{
229  std::cerr << "iterator: " << iter << " called on unambiguous value. Can only access with Iterator = 0" << std::endl;
230  exit(5);
231  }
232  }
233 
234 
235  /*! \fn void setAmbiguous(const std::vector<int>& vec)
236  \brief Sets the index class to ambiguous
237  \param vec vector of ints that correspond to multiple indices created by ambiguous characters
238  */
239  void Index::setAmbiguous(const std::vector<size_t>& vec){
240  amb=new(std::nothrow) std::vector<size_t>(vec);
241 
242  if (amb==NULL){
243  std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
244  exit(1);
245  }
246 
247  ambiguous = true;
248  return;
249  }
250 
251 }