00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __SEQUENCE_BITS_SNN_TCC__
00026 #define __SEQUENCE_BITS_SNN_TCC__
00027 #include <Sequence/Portability/random_shuffle.hpp>
00028 #include <algorithm>
00029
00030 namespace Sequence
00031 {
00032 template< typename uniform_int_generator >
00033 std::pair<double,double>
00034 Snn_test_details(const PolyTable & snpTable,
00035 const unsigned config[],
00036 const size_t & npop,
00037 uniform_int_generator & uni_int,
00038 const unsigned & nperms)
00039 {
00040 std::vector < std::vector<double> > dkj(snpTable.size(),
00041 std::vector<double>(snpTable.size(),0.));
00042 for(unsigned i=0;i<snpTable.size()-1;++i)
00043 {
00044 for(unsigned j=i+1;j<snpTable.size();++j)
00045 {
00046 dkj[i][j] = Sequence::NumDiffs(snpTable[i],snpTable[j]);
00047 }
00048 }
00049 std::vector <unsigned> __individuals(snpTable.size(),0u);
00050 for(unsigned i=0;i<snpTable.size();++i)
00051 {
00052 __individuals[i]=i;
00053 }
00054 const double observed = Snn_statistic(&__individuals[0],
00055 dkj,
00056 config,
00057 npop,__individuals.size());
00058 unsigned perm = nperms;
00059 unsigned pv = 0;
00060 while( perm-- )
00061 {
00062 #ifdef FORCE_STD_RANDOM_SHUFFLE
00063 std::random_shuffle(__individuals.begin(),__individuals.end(),uni_int);
00064 #else
00065 Sequence::random_shuffle(__individuals.begin(),__individuals.end(),uni_int);
00066 #endif
00067 double permuted_stat = Snn_statistic(&__individuals[0],
00068 dkj,
00069 config,
00070 npop,__individuals.size());
00071 pv += (permuted_stat >= observed) ? 1 : 0;
00072 }
00073 return std::make_pair(observed,double(pv)/double(nperms));
00074 }
00075
00076 template< typename uniform_int_generator >
00077 std::vector< std::vector<double> >
00078 Snn_test_pairwise_details(const PolyTable & snpTable,
00079 const unsigned config[],
00080 const size_t & npop,
00081 uniform_int_generator uni_int,
00082 const unsigned & nperms)
00083 {
00084 std::vector<unsigned> offsets(npop+1);
00085 for(unsigned i=1;i<=npop;++i)
00086 {
00087 offsets[i] = offsets[i-1]+config[i-1];
00088 }
00089 std::vector < std::vector<double> > dkj(snpTable.size(),
00090 std::vector<double>(snpTable.size(),0.));
00091 for(unsigned i=0;i<snpTable.size()-1;++i)
00092 {
00093 for(unsigned j=i+1;j<snpTable.size();++j)
00094 {
00095 dkj[i][j] = Sequence::NumDiffs(snpTable[i],snpTable[j]);
00096 }
00097 }
00098 std::vector <unsigned> __individuals(snpTable.size(),0u),individuals;
00099 unsigned _config[2];
00100
00101 for(unsigned i=0;i<snpTable.size();++i)
00102 {
00103 __individuals[i]=i;
00104 }
00105 std::vector< std::vector<double> > rv;
00106 for(unsigned i=0;i< npop-1;++i)
00107 {
00108 for(unsigned j=i+1;j<npop;++j)
00109 {
00110 individuals.assign(__individuals.begin()+offsets[i],
00111 __individuals.begin()+offsets[i+1]);
00112 individuals.insert(individuals.end(),
00113 __individuals.begin()+offsets[j],
00114 __individuals.begin()+offsets[j+1]);
00115 _config[0] = config[i];
00116 _config[1] = config[j];
00117 const double observed = Snn_statistic( &individuals[0],dkj,_config,2,
00118 _config[0]+_config[1]);
00119 unsigned p =0, _nperms = nperms;
00120 while(_nperms>0)
00121 {
00122 #ifdef FORCE_STD_RANDOM_SHUFFLE
00123 std::random_shuffle(__individuals.begin(),__individuals.end(),uni_int);
00124 #else
00125 Sequence::random_shuffle(individuals.begin(),
00126 individuals.end(),
00127 uni_int);
00128 #endif
00129 double perm = Snn_statistic( &individuals[0],dkj,_config,2,
00130 _config[0]+_config[1]);
00131 if(perm>=observed) ++p;
00132 _nperms--;
00133 }
00134 std::vector<double> result(4);
00135 result[0]=double(i+1);
00136 result[1]=double(j+1);
00137 result[2]=double(observed);
00138 result[3]=double(p)/double(nperms);
00139 rv.push_back(result);
00140 }
00141 }
00142 return rv;
00143 }
00144
00145 template< typename uniform_int_generator >
00146 std::pair<double,double>
00147 Snn_test(const PolyTable & snpTable,
00148 const unsigned config[],
00149 const size_t & npop,
00150 uniform_int_generator & uni_int,
00151 const unsigned & nperms = 10000)
00163 {
00164 return Snn_test_details(snpTable,config,npop,uni_int,nperms);
00165 }
00166
00167 template< typename uniform_int_generator >
00168 std::vector< std::vector<double> >
00169 Snn_test_pairwise(const PolyTable & snpTable,
00170 const unsigned config[],
00171 const size_t & npop,
00172 uniform_int_generator & uni_int,
00173 const unsigned & nperms = 10000)
00188 {
00189 return Snn_test_pairwise_details(snpTable,config,npop,uni_int,nperms);
00190 }
00191
00192 template< typename uniform_int_generator >
00193 std::pair<double,double>
00194 Snn_test(const PolyTable & snpTable,
00195 const unsigned config[],
00196 const size_t & npop,
00197 const uniform_int_generator & uni_int,
00198 const unsigned & nperms = 10000)
00210 {
00211 return Snn_test_details(snpTable,config,npop,uni_int,nperms);
00212 }
00213
00214 template< typename uniform_int_generator >
00215 std::vector< std::vector<double> >
00216 Snn_test_pairwise(const PolyTable & snpTable,
00217 const unsigned config[],
00218 const size_t & npop,
00219 const uniform_int_generator & uni_int,
00220 const unsigned & nperms = 10000)
00235 {
00236 return Snn_test_pairwise_details(snpTable,config,npop,uni_int,nperms);
00237 }
00238 }
00239 #endif