00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00034 #ifndef __POLY_FUNCTIONAL_HPP__
00035 #define __POLY_FUNCTIONAL_HPP__
00036
00037 #include <Sequence/stateCounter.hpp>
00038 #include <Sequence/PolyTableManip.hpp>
00039 #include <Sequence/typedefs.hpp>
00040 #include <vector>
00041 #include <string>
00042 #include <utility>
00043
00044 namespace Sequence
00045 {
00046 struct countStates
00051 {
00052 template<typename charItr>
00053 inline stateCounter operator()( charItr beg,
00054 charItr end,
00055 const bool & haveOutgroup = false,
00056 const size_t & outgroup = 0 )
00065 {
00066 stateCounter c;
00067 if(beg>=end || beg+outgroup>=end) return c;
00068 for( charItr itr=beg ;
00069 itr != end ;
00070 ++itr )
00071 {
00072 if ( (!haveOutgroup)
00073
00074 || ( haveOutgroup && ( unsigned(itr-beg) != outgroup ) ) )
00075 {
00076 c(*itr);
00077 }
00078 }
00079 return c;
00080 }
00081 };
00082
00083 struct countDerivedStates
00088 {
00089 template<typename charItr>
00090 inline stateCounter operator()( charItr beg,
00091 charItr end,
00092 const bool & haveOutgroup = true,
00093 const size_t & outgroup = 0 )
00102 {
00103 stateCounter c;
00104 if(beg>=end || beg+outgroup>=end || haveOutgroup == false) return c;
00105 for( charItr itr=beg ;
00106 itr != end ;
00107 ++itr )
00108 {
00109
00110 if ( (unsigned(itr-beg) != outgroup)
00111 && (*itr != *(beg+outgroup)) )
00112 {
00113 c(*itr);
00114 }
00115 }
00116 return c;
00117 }
00118 };
00119
00153 struct ssh : public std::binary_function< double,polymorphicSite,double >
00154
00155 {
00156 inline double operator()( double & _ssh,
00157 const polymorphicSite & site,
00158 const bool & haveOutgroup = false,
00159 const unsigned & outgroup = 0 ) const
00168 {
00169 stateCounter c = countStates()(site.second.begin(),
00170 site.second.end(),
00171 haveOutgroup,outgroup);
00172
00173 unsigned nsam = site.second.length() -
00174 unsigned( haveOutgroup==true ? 1 : 0 ) - c.n;
00175 double hom=0.;
00176 if (c.gap==0 && nsam > 1)
00177 {
00178 hom += (c.a > 0) ? double(c.a) * double(c.a-1) : 0.;
00179 hom += (c.g > 0) ? double(c.g) * double(c.g-1) : 0.;
00180 hom += (c.c > 0) ? double(c.c) * double(c.c-1) : 0.;
00181 hom += (c.t > 0) ? double(c.t) * double(c.t-1) : 0.;
00182 hom += (c.zero > 0) ? double(c.zero) * double(c.zero-1) : 0.;
00183 hom += (c.one > 0) ? double(c.one) * double(c.one-1) : 0.;
00184 }
00185 return _ssh += (hom > 0.) ? 1.- (hom/( double(nsam)*double(nsam-1) )) : 0.;
00186 }
00187 };
00188
00224 template<typename counter>
00225 struct nmuts
00226 {
00230 typedef unsigned result_type;
00231 inline unsigned operator()(unsigned & nm,
00232 const polymorphicSite & site,
00233 const bool & haveOutgroup = false,
00234 const unsigned & outgroup = 0 ) const
00243 {
00244 stateCounter c = counter()(site.second.begin(),
00245 site.second.end(),
00246 haveOutgroup,outgroup);
00247 unsigned n = 0;
00248 n += (c.a > 0) ? 1 : 0;
00249 n += (c.g > 0) ? 1 : 0;
00250 n += (c.c > 0) ? 1 : 0;
00251 n += (c.t > 0) ? 1 : 0;
00252 n += (c.zero > 0) ? 1 : 0;
00253 n += (c.one > 0) ? 1 : 0;
00254 return nm += (n>=2) ? n-1 : 0;
00255 }
00256 };
00257 }
00258 #endif