libsequence  1.9.5
Snn.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/SummStats/Snn.hpp>
25 
26 namespace Sequence
27 {
28  double Snn_statistic( const unsigned individuals[],
29  const std::vector< std::vector<double> > & dkj,
30  const unsigned config[],
31  const size_t & npop,
32  const unsigned & nsam)
33 {
34  /*
35  notation for variables follows Hudson's paper
36  */
37  double snn = 0.;
38 
39  //store the d_kj for the whole sample
40  double * d_kj = new double[nsam-1];
41 
42  //store d_kj for within-population comparisons
43  std::vector<double> d_kj_win;
44  for(unsigned k=0; k<nsam ; ++k)
45  {
46  //find out which pop ind k is in
47  unsigned pop = 0,ttl=0;
48  while (pop < npop)
49  {
50  ttl += config[pop];
51  if (k < ttl)
52  break;
53  pop++;
54  }
55  d_kj_win.clear();
56  for(unsigned j = 0,dummy=0; j < nsam ; ++j)
57  {
58  if (k!=j)
59  {
60  unsigned a=*(individuals+j),b=*(individuals+k);
61  if (a>b)
62  std::swap(a,b);
63 
64  double ndiffs = dkj[a][b];
65  d_kj[dummy++] = ndiffs;
66  //figure out what pop j is in;
67  unsigned pop_j=0,ttl=0;
68  while (pop_j < npop)
69  {
70  ttl += config[pop_j];
71  if (j < ttl)
72  break;
73  pop_j++;
74  }
75  if (pop==pop_j)
76  d_kj_win.push_back(ndiffs);
77  }
78  }
79  //Calculate T_k
80  double min = d_kj[0];
81  for (unsigned j = 1 ; j < nsam-1 ; ++j)
82  if (d_kj[j] < min) min = d_kj[j];
83 
84  std::ptrdiff_t T_k = std::count(d_kj,d_kj+(nsam-1),min);
85 
86  //Calculate M_k
87  std::ptrdiff_t M_k = std::count(d_kj_win.begin(),
88  d_kj_win.end(),min);
89  snn += double(M_k)/double(T_k);
90  }
91  delete [] d_kj;
92  return snn/double(nsam);
93 }
94 
95 }
double Snn_statistic(const unsigned individuals[], const std::vector< std::vector< double > > &dkj, const unsigned config[], const size_t &npop, const unsigned &nsam) __attribute__((deprecated))
Definition: Snn.cc:28
The namespace in which this library resides.