libsequence  1.9.5
Coalesce.tcc
1 // -*- C++ -*-
2 #ifndef __SEQUENCE_COALESCENT_BITS_COALESCE_TCC__
3 #define __SEQUENCE_COALESCENT_BITS_COALESCE_TCC__
4 
5 namespace Sequence
6 {
7  namespace coalsim {
8 #ifndef DOXYGEN_SKIP
9  template<typename uniform_generator>
10  std::pair<int,int> pick2_in_deme_details( uniform_generator & uni,
11  const std::vector<Sequence::coalsim::chromosome> & sample,
12  const int & current_nsam,
13  const int & deme_nsam,
14  const int & deme )
15  {
16  assert( deme_nsam > 0 );
17  assert( deme >= 0 );
18  assert( deme_nsam <= current_nsam );
19  std::pair<int,int> two = pick2(uni,deme_nsam);
20  assert(two.first != two.second);
21  int c1=0,c2=0;
22  for(int i=0,j=0;i<current_nsam;++i)
23  {
24  if( (sample.begin()+i)->pop == deme && j == two.first )
25  c1 = i;
26  else if ( (sample.begin()+i)->pop == deme && j == two.second )
27  c2 = i;
28  if( (sample.begin()+i)->pop == deme ) ++j;
29  }
30  assert(c1!=c2);
31  return std::make_pair(std::min(c1,c2),std::max(c1,c2));
32  }
33 
34  template<typename uniform_generator>
35  std::pair<int,int> pick2_details(uniform_generator & uni, const int & nsam)
36  {
37  //int i = int(uni(std::cref(0),std::cref(nsam)));
38  int i = int(uni(0,std::cref(nsam)));
39  int j;
40  while( (j = int(uni(0,std::cref(nsam)))) == i )
41  {
42  }
43  if(i>j) std::swap(i,j);
44  return std::make_pair(i,j);
45  }
46 
47  template<typename uniform_generator>
48  std::pair<int,int> pick2_in_deme( uniform_generator & uni,
49  const std::vector<Sequence::coalsim::chromosome> & sample,
50  const int & current_nsam,
51  const int & deme_nsam,
52  const int & deme )
53  /*!
54  \brief Choose two random chromosomes from the same deme
55  \param uni A random number generator taking two arguments, a and b, and returning
56  a random variable distributed uniformly over [a,b)
57  \param sample The current state of the simulated sample
58  \param current_snam The total sample size being simuled (the sum of sample sizes over all demes)
59  \param deme_nsam The sample size of the deme from which you wish to sample
60  \param deme The index ( 0 <= deme < # populations ) of the deme from which you wish to sample
61  \return A pair of integers which contains the indexes of two chromosomes in @a sample
62  \ingroup coalescent
63  */
64  {
65  return pick2_in_deme_details(uni,sample,current_nsam,deme_nsam,deme);
66  }
67 #endif
68 
69  template<typename uniform_generator>
70  std::pair<int,int> pick2_in_deme( const uniform_generator & uni,
71  const std::vector<Sequence::coalsim::chromosome> & sample,
72  const int & current_nsam,
73  const int & deme_nsam,
74  const int & deme )
75  /*!
76  \brief Choose two random chromosomes from the same deme
77  \param uni A random number generator taking two arguments, a and b, and returning
78  a random variable distributed uniformly over [a,b)
79  \param sample The current state of the simulated sample
80  \param current_nsam The total sample size being simuled (the sum of sample sizes over all demes)
81  \param deme_nsam The sample size of the deme from which you wish to sample
82  \param deme The index ( 0 <= deme < # populations ) of the deme from which you wish to sample
83  \return A pair of integers which contains the indexes of two chromosomes in @a sample
84  \ingroup coalescent
85  */
86  {
87  return pick2_in_deme_details(uni,sample,current_nsam,deme_nsam,deme);
88  }
89 
90  template<typename uniform_generator>
91  std::pair<int,int> pick2( uniform_generator & uni, const int & nsam)
92  /*!
93  \param uni a random number function/object capable of returning
94  a double-precision random number between 0 and \a nsam-1
95  \param nsam the current sample size in the simulation
96  \return A pair of integers which contains the indexes of two chromosomes in @a sample
97  \ingroup coalescent
98  */
99  {
100  return pick2_details(uni,nsam);
101  }
102 
103  template<typename uniform_generator>
104  std::pair<int,int> pick2( const uniform_generator & uni, const int & nsam)
105  /*!
106  \param uni a random number function/object capable of returning
107  a double-precision random number between 0 and \a nsam-1
108  \param nsam the current sample size in the simulation
109  \return A pair of integers which contains the indexes of two chromosomes in @a sample
110  \ingroup coalescent
111  */
112  {
113  return pick2_details(uni,nsam);
114  }
115  }
116 }
117 
118 #endif