libsequence  1.9.5
PolyTable.cc
1 /*
2 
3 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
4 
5 Remove the brackets to email me.
6 
7 This file is part of libsequence.
8 
9 libsequence is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 libsequence is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 long with libsequence. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #include <Sequence/PolyTable.hpp>
26 #include <cctype>
27 #include <algorithm>
28 #include <iostream>
29 #include <iterator>
30 #include <stdexcept>
31 
39 namespace Sequence
40 {
41  struct PolyTable::PolyTableImpl
42  {
43  std::vector<double> pos;
44  std::vector<std::string> data;
45  polySiteVector pv;
46  bool non_const_access;
47  PolyTableImpl() : pos(std::vector<double>()),
48  data(std::vector<std::string>()),
49  pv(polySiteVector()),
50  non_const_access(true)
51  {
52  }
53 
54  PolyTableImpl( std::vector<double> && __positions,
55  std::vector<std::string> && __data ) : pos(std::forward<std::vector<double> >(__positions)),
56  data(std::forward<std::vector<std::string> >(__data)),
57  pv(polySiteVector()),
58  non_const_access(true)
59  {
60  std::for_each(std::begin(data),std::end(data),[this](const std::string & __s) {
61  if(this->pos.size()!=__s.size())
62  {
63  this->pos.clear();
64  this->data.clear();
65  this->pv.clear();
66  throw std::runtime_error("PolyTable: number of positions != length of data element");
67  }
68  });
69  }
70 
71 
72  bool empty() const { return pos.empty() && data.empty(); }
73  void clear() { pos.clear();data.clear();pv.clear(); }
74  bool assign(PolyTable::const_site_iterator beg,
75  PolyTable::const_site_iterator end);
76  template<typename postype,typename datatype>
77  bool assign_data( postype && __pos,
78  datatype && __data )
79  {
80  non_const_access=true;
81  pos=std::forward<postype>(__pos);
82  data=std::forward<datatype>(__data);
83  if( std::find_if( std::begin(data),std::end(data),
84  [this](const std::string & __s) {
85  return __s.size() != pos.size();
86  } ) != data.cend() )
87  {
88  this->clear();
89  return false;
90  }
91  return true;
92  }
93 
94  };
95 
96  bool PolyTable::PolyTableImpl::assign(PolyTable::const_site_iterator beg,
97  PolyTable::const_site_iterator end)
98  {
99  non_const_access = true; //set to true in case an exception is thrown
100  this->clear();
101  if(std::distance(beg,end) < 1) return true;
102  pos.resize(std::vector<double>::size_type(end-beg));
103  pv.resize(std::vector<double>::size_type(end-beg));
104  data.resize(beg->second.length());
105  size_t nsam = beg->second.length();
106  std::string::const_iterator sb,se;
107  typedef PolyTable::const_site_iterator::difference_type DTYPE;
108  DTYPE i=0,j=0;
109  while((beg+i)<end)
110  {
111  pv[unsigned(i)]=*(beg+i);
112  if ((beg+i)->second.length() != nsam)
113  {
114  //If we toss an exception, let's make sure we leave an empty object.
115  this->clear();
116  return false;
117  }
118  pos[unsigned(i)] = (beg+i)->first;
119  sb = (beg+i)->second.begin();
120  se = (beg+i)->second.end();
121  j = 0;
122  while( (sb+j) < se )
123  {
124  data[unsigned(j)] += *(sb+j);
125  ++j;
126  }
127  ++i;
128  }
129  non_const_access = false; //everything worked, all private data are assigned, so set to false
130  return true;
131  }
132  /*
133  bool PolyTable::PolyTableImpl::assign( std::vector<double> && __positions,
134  std::vector<std::string> && __data )
135  {
136  non_const_access = true;
137  this->clear();
138  std::swap(pos,__positions);
139  std::swap(data,__data);
140 
141  if( std::find_if( std::begin(data),std::end(data),
142  [this](const std::string __s) {
143  return __s.size() != pos.size();
144  } ) != data.cend() )
145  {
146  this->clear();
147  return false;
148  }
149  return true;
150  }
151  */
152  PolyTable::PolyTable() : impl(std::unique_ptr<PolyTableImpl>(new PolyTableImpl()))
153  {
154  }
155 
156  PolyTable::PolyTable( std::vector<double> __positions,
157  std::vector<std::string> __data ) : impl(std::unique_ptr<PolyTableImpl>(new PolyTableImpl(std::move(__positions),
158  std::move(__data))))
159  {
160  }
161 
162  PolyTable::PolyTable(PolyTable && rhs) : impl(std::unique_ptr<PolyTableImpl>(new PolyTableImpl()))
163  {
164  std::swap(this->impl,rhs.impl);
165  }
166 
167  PolyTable::PolyTable(const PolyTable & rhs) : impl(std::unique_ptr<PolyTableImpl>(new PolyTableImpl(*rhs.impl.get())))
168  {
169  }
170 
171  PolyTable & PolyTable::operator=(PolyTable && rhs)
172  {
173  auto x = std::unique_ptr<PolyTableImpl>(new PolyTableImpl());
174  std::swap(this->impl,rhs.impl);
175  std::swap(x,rhs.impl);
176  return *this;
177  }
178 
179  PolyTable & PolyTable::operator=(const PolyTable & rhs)
180  {
181  this->impl->pos = rhs.impl->pos;
182  this->impl->data = rhs.impl->data;
183  return *this;
184  }
185 
186  PolyTable::PolyTable(PolyTable::const_site_iterator beg,
187  PolyTable::const_site_iterator end) : impl(std::unique_ptr<PolyTableImpl>(new PolyTableImpl()))
188  {
189  if (beg>=end)
190  {
191  return;
192  }
193  assign(beg,end);
194  }
195 
196  PolyTable::~PolyTable (void)
197  {
198  }
199 
200  bool PolyTable::empty() const
201  {
202  return impl->empty();
203  }
204 
205  bool PolyTable::operator==(const PolyTable &rhs) const
206  {
207  return (this->impl->pos == rhs.impl->pos) &&
208  (this->impl->data == rhs.impl->data);
209  }
210 
211  bool PolyTable::operator!=(const PolyTable &rhs) const
212  {
213  return !(*this==rhs);
214  }
215 
216  bool PolyTable::assign( const std::vector<double> & __positions,
217  const std::vector<std::string> & __data )
218  {
219  return impl->assign_data(__positions,__data);
220  }
221 
222  bool PolyTable::assign( std::vector<double> && __positions,
223  std::vector<std::string> && __data )
224  {
225  return impl->assign_data(std::move(__positions),
226  std::move(__data));
227  }
228 
229  bool PolyTable::assign(PolyTable::const_site_iterator beg,
230  PolyTable::const_site_iterator end)
231  {
232  return impl->assign(beg,end);
233  }
234 
235  void PolyTable::swap( PolyTable & pt)
236  {
237  std::swap(this->impl,pt.impl);
238  }
239 
240  PolyTable::data_iterator PolyTable::begin()
241  {
242  impl->non_const_access=true;
243  return impl->data.begin();
244  }
245 
246  PolyTable::data_iterator PolyTable::end()
247  {
248  impl->non_const_access=true;
249  return impl->data.end();
250  }
251 
252  PolyTable::const_data_iterator PolyTable::begin() const
253  {
254  return impl->data.begin();
255  }
256 
257  PolyTable::const_data_iterator PolyTable::cbegin() const
258  {
259  return impl->data.cbegin();
260  }
261 
262  PolyTable::const_data_iterator PolyTable::end() const
263  {
264  return impl->data.end();
265  }
266 
267  PolyTable::const_data_iterator PolyTable::cend() const
268  {
269  return impl->data.cend();
270  }
271 
272  PolyTable::pos_iterator PolyTable::pbegin()
273  {
274  impl->non_const_access=true;
275  return impl->pos.begin();
276  }
277 
278  PolyTable::pos_iterator PolyTable::pend()
279  {
280  impl->non_const_access=true;
281  return impl->pos.end();
282  }
283 
284  PolyTable::const_pos_iterator PolyTable::pbegin() const
285  {
286  return impl->pos.begin();
287  }
288 
289  PolyTable::const_pos_iterator PolyTable::pend() const
290  {
291  return impl->pos.end();
292  }
293 
294  PolyTable::const_pos_iterator PolyTable::pcbegin() const
295  {
296  return impl->pos.cbegin();
297  }
298 
299  PolyTable::const_pos_iterator PolyTable::pcend() const
300  {
301 
302  return impl->pos.cend();
303  }
304 
305  PolyTable::const_site_iterator PolyTable::sbegin() const
306  {
307  if(impl->non_const_access == true)
308  {
309  impl->pv = polySiteVector(make_polySiteVector(*this));
310  impl->non_const_access=false;
311  }
312  return impl->pv.begin();
313  }
314 
315  PolyTable::const_site_iterator PolyTable::send() const
316  {
317  if(impl->non_const_access == true)
318  {
319  impl->pv = polySiteVector(make_polySiteVector(*this));
320  impl->non_const_access=false;
321  }
322  return impl->pv.end();
323  }
324 
325  PolyTable::const_site_iterator PolyTable::scbegin() const
326  {
327  if(impl->non_const_access == true)
328  {
329  impl->pv = polySiteVector(make_polySiteVector(*this));
330  impl->non_const_access=false;
331  }
332  return impl->pv.cbegin();
333  }
334 
335  PolyTable::const_site_iterator PolyTable::scend() const
336  {
337  if(impl->non_const_access == true)
338  {
339  impl->pv = polySiteVector(make_polySiteVector(*this));
340  impl->non_const_access=false;
341  }
342  return impl->pv.cend();
343  }
344 
345 
346 
347  std::vector < double >
348  PolyTable::GetPositions (void) const
349  {
350  return impl->pos;
351  }
352 
353  std::vector <std::string > PolyTable::GetData (void) const
354  {
355  return impl->data;
356  }
357 
358  PolyTable::const_reference PolyTable::operator[] (const size_type & i) const
359  {
360  return (impl->data[i]);
361  }
362 
363  PolyTable::reference PolyTable::operator[] (const size_type & i)
364 
365  {
366  impl->non_const_access=true;
367  return (impl->data[i]);
368  }
369 
370  PolyTable::size_type PolyTable::size (void) const
371  {
372  return impl->data.size();
373  }
374 
375  double PolyTable::position (const std::vector<double>::size_type & i) const
376  {
377  return impl->pos[i];
378  }
379 
380  unsigned PolyTable::numsites (void) const
381  {
382  return unsigned(impl->pos.size());
383  }
384 
385  //non-member functions
386  std::istream & operator>> (std::istream & s, PolyTable & c)
387  {
388  return c.read (s);
389  }
390 
391  std::ostream & operator<< (std::ostream & o, const PolyTable & c)
392  {
393  return c.print (o);
394  }
395 } //ns Sequence
396 
The base class for polymorphism tables.
polySiteVector make_polySiteVector(const Sequence::PolyTable &data) __attribute__((deprecated))
STL namespace.
Sequence::PolyTable, a virtual base class for polymorphism tables.
The namespace in which this library resides.
std::vector< polymorphicSite > polySiteVector
std::ostream & operator<<(std::ostream &s, const AlignStream< T > &c)
std::istream & operator>>(std::istream &s, AlignStream< T > &c)
declaration of Sequence::stateCounter, a class to keep track of nucleotide counts either at a site in...