-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathF2Counter.h
128 lines (96 loc) · 3.01 KB
/
F2Counter.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
/*
* F2Counter
* Date: Oct-15-2012
* Author : Gabriel Renaud [email protected]
*
*/
#ifndef F2Counter_h
#define F2Counter_h
#include <iostream>
#include <ostream>
#include <vector>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include "libgab.h"
//#include <iostream>
//#include <fstream>
using namespace std;
class F2Counter{
private:
public:
//counter of mutations
double f2Sum; //(f1-f2)^2
unsigned int counterSites;
double nInds1;
double nInds2;
double hz1;
double hz2;
/* double nsnp1; */
/* double nsnp2; */
double nsnp;
F2Counter(); //constructor
// vector<double> performBoostraps() const;
void reinitializedCounters();
string headerForCount() const;
double computeF2() const;
void addAlleleCounts(int c1_1,int c2_1,double f_1,int c1_2,int c2_2,double f_2);
F2Counter & operator+=(const F2Counter & other);
F2Counter & operator-=(const F2Counter & other);
/* double toreturn = 0; */
/* for (int i = 0; i < nsnp; i++){ */
/* double n1 = mean_ninds[p1]; */
/* double n2 = mean_ninds[p2]; */
/* double f1 = gsl_matrix_get(alfreqs, i, p1); */
/* double f2 = gsl_matrix_get(alfreqs, i, p2); */
/* double diff = f1-f2; */
/* double hz1 = f1*(1-f1); */
/* double hz2 = f2*(1-f2); */
/* double toadd = diff*diff - hz1/(2*n1) - hz2/(2*n2); */
/* toreturn += toadd; */
/* } */
/* toreturn = toreturn/ (double) nsnp; */
/* return toreturn; */
//taken from countData from treemix
friend ostream& operator<<(ostream& os, const F2Counter & ct){
double f2unscaled = double(ct.f2Sum);///double(ct.counterSites);
double meanNind1 = double(ct.nInds1)/double(ct.nsnp);
double meanNind2 = double(ct.nInds2)/double(ct.nsnp);
double meanHZ1 = double(ct.hz1)/double(2*meanNind1);
double meanHZ2 = double(ct.hz2)/double(2*meanNind2);
double f2= ( f2unscaled - meanHZ1 - meanHZ2 ) / double(ct.nsnp);
// 324.75 9344 324.75 9304.5 9335 227.5 217.25 9344 0.0108933
// 324.75 9344 9304.5 9335 227.5 217.25 9344 0.0108933
os<<ct.f2Sum<<"\t"<<ct.counterSites<<"\t"<<ct.nInds1<<"\t"<<ct.nInds2<<"\t"<<ct.hz1<<"\t"<<ct.hz2<<"\t"<<ct.nsnp<<"\t"<<f2<<"\t";
return os;
}
friend istream &operator>>(istream &is , F2Counter &ct){
//cerr<<"DstatCounter in"<<endl;
//double dst;
is>>ct.f2Sum>>ct.counterSites>>ct.nInds1>>ct.nInds2>>ct.hz1>>ct.hz2>>ct.nsnp;
double f2;
string f2Str;
char c;
is.get(c);
if(c != '\t'){
cerr<<"A single f2 should have 8 fields"<<endl;
}
while (is.get(c)){ // loop getting single characters
if(c == '\t') break;
f2Str+=c;
//cerr<<"#"<<c<<"#"<<endl;
}
if(f2Str == "-nan"){
f2 = -1.0*numeric_limits<double>::quiet_NaN();
}else{
f2 = destringify<double>(f2Str);
}
/* //cerr<<"#"; */
/* for(int i=0;i<=15;i++){ */
/* cerr<<ct.count[i]<<" "; */
/* } */
/* cerr<<"i:"<<ident<<" m:"<<mutations<<" d:"<<dist<<"# "; */
return is;
}
};
#endif