StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
text.h
Go to the documentation of this file.
1 //text.h
2 
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 #ifndef TEXT_H
28 #define TEXT_H
29 
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 #include <stdlib.h>
36 #ifndef SIZE_MAX
37 #define SIZE_MAX ((size_t)-1)
38 #endif
39 
40 namespace StochHMM{
41 
42  /*! \defgroup Text Text Handling
43  *
44  *
45  */
46 
47 
48 
49  /*! \class stringList
50  \brief Stringlist is an list of strings that contains parsed comments and
51  can be used to split string or remove whitespace from a string
52 
53  */
54 
55  class stringList{
56  public:
57  stringList();
58  stringList(std::string&,std::string&,std::string&,bool);
59 
60  //MUTATORS
61  //! Set remove whitespace flag. When splitting string it will remove Whitespace first and then split the string.
62  inline void setRemoveWS(){removeWS=true;}; //!<
63 
64 
65  //! Unset remove whitespace flag. When splitting string it will remove Whitespace first and then split the string.
66  inline void unsetRemoveWS(){removeWS=false;};
67 
68  //! Set whitespace character. When splitting string it will remove defined whitespace characters before splitting the string.
69  //! \param txt String of whitespace characters to remove from sequence
70  inline void setWhitespace(const std::string& txt){whitespace=txt;};
71 
72  //! Set delimiter characters. String will be split on these characters
73  //! \param txt String of text-delimiters to use in splitting string
74  inline void setDelimiters(const std::string& txt){delimiters=txt;};
75 
76  void getLine(std::istream&);
77  void processString(std::string&); //Remove comment, whitespace, and split string
78  void splitString(const std::string&); //Split string with delimiter defined in stringList
79  void splitString(const std::string&, const std::string&); //Split string with given delimiter
80  void splitString(const std::string&, size_t);
81  void splitND(const std::string&, const std::string&);
82  void removeLWS(const std::string&);
83  void removeComments();
84 
85  //! Clears all values from the stringList, including comments, whitespace, and delimiters
86  inline void clear(){line.clear();comment.clear();whitespace.clear();delimiters.clear();};
87 
88  bool fromTable(std::string&);
89  bool fromTable(std::istream&);
90  bool fromTrack(std::string&);
91  bool fromTrack(std::istream&);
92  bool fromAlpha(const std::string&,size_t);
93  bool fromAlpha(std::istream&,size_t);
94  bool fromTxt(std::string&);
95  bool fromTxt(std::istream&);
96  bool fromNext(std::string&);
97  bool fromNext(std::istream&);
98  bool fromDef(std::string&,std::string&, std::string&);
99  bool fromDef(std::istream&,std::string&, std::string&);
100 
101  //! Add string to StringList
102  //! \param txt String to add to the stringList
103  inline void operator<< (std::string& txt){line.push_back(txt);};
104 
105  //! Add string to StringList
106  //! \param txt String to add to the stringList
107  inline void push_back (std::string& txt){line.push_back(txt);};
108 
109  std::string pop_ith(size_t);
110 
111  //! Copy StringList
112  //! \param lst stringList to copy
114 
115  //ACCESSORS
116  //! Access string at iterator
117  //! \param iter Position to return;
118  inline std::string& operator[](size_t iter){return line[iter];};
119 
120  //! Return any comments parsed from the line
121  inline std::string& getComment(){return comment;};
122 
123  //! Returns the list as a Vector of Doubles
124  std::vector<double> toVecDouble();
125 
126  void toVecInt(std::vector<int>&);
127 
128 
129  size_t indexOf(const std::string&);
130  size_t indexOf(const std::string&,size_t);
131  bool contains(const std::string&);
132  bool containsExact(const std::string& txt);
133  void print();
134  std::string stringify();
135 
136  //! Return the amount of strings in the stringList
137  inline size_t size(){return line.size();};
138 
139 
140  private:
141  void _splitIndividual(std::string&, size_t);
142  bool foundAlphaDelimiter(const std::string&);
143 
144  std::vector<std::string> line;
145  std::string comment;
146 
147  bool removeWS;
148  std::string whitespace;
149  std::string delimiters;
150  };
151 
152 
153 
154 
155  stringList& splitString(const std::string&,const std::string&);
156 
157  void clear_whitespace(std::string&, std::string);
158  void replaceChar(std::string&, char ,char);
159  void removeLeadingWS(std::string&,const std::string&);
160  std::string parseComment(std::string&,char);
161  void getKeyValue(std::string&,std::string&,std::string&);
162 
163  std::string join(std::vector<int>&, char);
164  std::string join(std::vector<size_t>&, char);
165  std::string join(std::vector<short>&,char);
166  std::string join(std::vector<double>&, char);
167  std::string join(std::vector<std::string>&, char);
168 
169  std::string int_to_string(int);
170  std::string int_to_string(size_t);
171  std::string double_to_string(double);
172  std::string double_to_string(float);
173 
174  bool stringToInt(std::string&, int&);
175  bool stringToInt(std::string&, size_t&);
176  bool stringToDouble(std::string&, double&);
177 
178  bool isNumeric(const std::string&);
179 
180 
181  void split_line(std::vector<std::string>&, std::string& );
182 
183  stringList extractTag(std::string&);
184  std::pair<size_t,size_t> balanced_brackets(const std::string&, const std::string& );
185  std::pair<size_t,size_t> balanced_brackets(const std::string&, const std::string& , size_t );
186 
187  std::string slurpFile(std::string&);
188 
189  /*
190  inline double convertToDouble (const std::string& s){
191  istd::stringstream i(s);
192  double x;
193  if (!(i>>x)){
194  throw BadConversion("convertToDouble(\"" + s "\")"));
195  }
196  return x;
197  }
198  */
199 
200  /**@}*/ //End of Text Handling Group
201 
202 }
203 #endif /*TEXT_H*/