libsequence  1.9.5
PolyFunctional.hpp
Go to the documentation of this file.
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 
34 #ifndef __POLY_FUNCTIONAL_HPP__
35 #define __POLY_FUNCTIONAL_HPP__
36 
38 #include <Sequence/typedefs.hpp>
39 #include <vector>
40 #include <string>
41 #include <utility>
42 
43 namespace Sequence
44 {
45  struct __attribute__ ((deprecated)) countStates
50  {
51  template<typename charItr>
52  inline stateCounter operator()( charItr beg,
53  charItr end,
54  const bool & haveOutgroup = false,
55  const size_t & outgroup = 0 )
64  {
65  stateCounter c;
66  if(beg>=end || beg+outgroup>=end) return c;
67  for( charItr itr=beg ;
68  itr != end ;
69  ++itr )
70  {
71  if ( (!haveOutgroup)
72  //casting to unsigned is safe since itr is always >= beg
73  || ( haveOutgroup && ( unsigned(itr-beg) != outgroup ) ) )
74  {
75  c(*itr);
76  }
77  }
78  return c;
79  }
80  };
81 
87  {
88  template<typename charItr>
89  inline stateCounter operator()( charItr beg,
90  charItr end,
91  const bool & haveOutgroup = true,
92  const size_t & outgroup = 0 )
101  {
102  stateCounter c;
103  if(beg>=end || beg+outgroup>=end || haveOutgroup == false) return c;
104  for( charItr itr=beg ;
105  itr != end ;
106  ++itr )
107  {
108  //casting to unsigned is safe since itr is always >= beg
109  if ( (unsigned(itr-beg) != outgroup)
110  && (*itr != *(beg+outgroup)) )
111  {
112  c(*itr);
113  }
114  }
115  return c;
116  }
117  };
118 
152  struct __attribute__ ((deprecated)) ssh : public std::binary_function< double,polymorphicSite,double >
153 
154  {
155  inline double operator()( double & _ssh,
156  const polymorphicSite & site,
157  const bool & haveOutgroup = false,
158  const unsigned & outgroup = 0 ) const
167  {
168  stateCounter c = countStates()(site.second.begin(),
169  site.second.end(),
170  haveOutgroup,outgroup);
171 
172  unsigned nsam = site.second.length() -
173  unsigned( haveOutgroup==true ? 1 : 0 ) - c.n;
174  double hom=0.;
175  if (c.gap==0 && nsam > 1)
176  {
177  hom += (c.a > 0) ? double(c.a) * double(c.a-1) : 0.;
178  hom += (c.g > 0) ? double(c.g) * double(c.g-1) : 0.;
179  hom += (c.c > 0) ? double(c.c) * double(c.c-1) : 0.;
180  hom += (c.t > 0) ? double(c.t) * double(c.t-1) : 0.;
181  hom += (c.zero > 0) ? double(c.zero) * double(c.zero-1) : 0.;
182  hom += (c.one > 0) ? double(c.one) * double(c.one-1) : 0.;
183  }
184  return _ssh += (hom > 0.) ? 1.- (hom/( double(nsam)*double(nsam-1) )) : 0.;
185  }
186  };
187 
223  template<typename counter>
224  struct nmuts
225  {
229  typedef unsigned result_type;
230  inline unsigned operator()(unsigned & nm,
231  const polymorphicSite & site,
232  const bool & haveOutgroup = false,
233  const unsigned & outgroup = 0 ) const
242  {
243  stateCounter c = counter()(site.second.begin(),
244  site.second.end(),
245  haveOutgroup,outgroup);
246  unsigned n = 0;
247  n += (c.a > 0) ? 1 : 0;
248  n += (c.g > 0) ? 1 : 0;
249  n += (c.c > 0) ? 1 : 0;
250  n += (c.t > 0) ? 1 : 0;
251  n += (c.zero > 0) ? 1 : 0;
252  n += (c.one > 0) ? 1 : 0;
253  return nm += (n>=2) ? n-1 : 0;
254  }
255  };
256 }
257 #endif
bool operator()(const std::pair< key, value > &l, const std::pair< key, value > &r) const
keep track of state counts at a site in an alignment or along a sequence
The namespace in which this library resides.
unsigned operator()(unsigned &nm, const polymorphicSite &site, const bool &haveOutgroup=false, const unsigned &outgroup=0) const
class __attribute__((deprecated)) FST
Functor to count the number of states, excluding gaps and missing data, in a range of characters...
Definition: FST.hpp:41
typedefs used by libsequence
Functor to count the number of derived states, excluding gaps and missing data, in a range of charact...
stateCounter operator()(charItr beg, charItr end, const bool &haveOutgroup=true, const size_t &outgroup=0)
Calculate the number of mutations at a polymorphic site.
declaration of Sequence::stateCounter, a class to keep track of nucleotide counts either at a site in...
std::pair< double, std::string > polymorphicSite