-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpairsample.h
145 lines (111 loc) · 3 KB
/
pairsample.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef GROUPSAMPLE_H
#define GROUPSAMPLE_H
#include <vector>
#include "sample.h"
#include "stats_multi.h"
template<class SEQ>
class PairSample
{
public:
typedef Sample<SEQ> sample_t;
protected:
const sample_t & _p1, & _p2;
mutable CachedValue<int> _spd;
mutable CachedValue<double> _fst;
mutable CachedValue<double> _patterson_D;
mutable CachedValue<PolyL> _poly_l;
mutable CachedValue<pair<int, int> > _navascues_R;
mutable CachedValue<pair<double, double> > _navascues_W;
public:
PairSample(const sample_t & p1, const sample_t & p2)
: _p1(p1), _p2(p2)
{
}
int sum_pairwise_differences() const
{
if (_spd.ready())
return _spd;
int n_poly_sites = std::max(_p1.n_sites(), _p2.n_sites());
return _spd = sum_pair_diff(
_p1.alleles().begin(), _p1.alleles().begin() + n_poly_sites,
_p2.alleles().begin(), _p2.alleles().begin() + n_poly_sites);
}
double fst() const
{
if (_fst.ready())
return _fst;
const int n_pairs = _p1.size() * _p2.size();
const int count_pair_piT =
_p1.size() * (_p1.size() - 1) / 2 + _p2.size() * (_p2.size() - 1) / 2 +
n_pairs;
const double piT =
(_p1.sum_pairwise_differences() + _p2.sum_pairwise_differences() +
sum_pairwise_differences()) / (double) count_pair_piT;
return _fst(piT >= 1.0e-7 ?
1.0 - (_p1.theta_pi()+_p2.theta_pi())/2.0 / piT :
undefined());
}
double patterson_D() const
{
return CACHED_OR_COMPUTE(_patterson_D, ::patterson_D(
_p1.alleles().begin(), _p1.alleles().end(),
_p2.alleles().begin(), _p2.alleles().end()));
}
// name?
double dn() const
{
return sum_pairwise_differences() - (_p1.theta_pi() + _p2.theta_pi())/2;
}
const PolyL & poly_l() const
{
if (!_poly_l.ready())
{
_poly_l.get().compute(
_p1.alleles().begin(), _p1.alleles().end(),
_p2.alleles().begin(), _p2.alleles().end());
_poly_l.set_ready();
}
return _poly_l;
}
int n_bial_sites() const
{ return poly_l().bialsites; }
int n_multi_sites() const
{ return poly_l().multisites; }
int sfout() const
{ return poly_l().sfout; }
int sfA() const
{ return poly_l().sfA; }
int sfB() const
{ return poly_l().sfB; }
int sxA() const
{ return poly_l().sxA; }
int sxB() const
{ return poly_l().sxB; }
int sxAfB() const
{ return poly_l().sxAfB; }
int sxBfA() const
{ return poly_l().sxBfA; }
int ss() const
{ return poly_l().ss; }
const pair<int, int> & navascues_R() const
{
return CACHED_OR_COMPUTE(_navascues_R, ::navascues_R(
_p1.alleles().begin(), _p1.alleles().end(),
_p2.alleles().begin(), _p2.alleles().end()));
}
int Rf() const
{ return navascues_R().first; }
int Rs() const
{ return navascues_R().second; }
const pair<double, double> & navascues_W() const
{
return CACHED_OR_COMPUTE(_navascues_W, navascues_W_01(
_p1.alleles().begin(), _p1.alleles().end(),
_p2.alleles().begin(), _p2.alleles().end()));
}
double Wx2s1() const
{ return navascues_W().first; }
double Wx1s2() const
{ return navascues_W().second; }
};
#endif // GROUPSAMPLE_H