-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcskss_gwas_permute.cc
353 lines (329 loc) · 9.25 KB
/
cskss_gwas_permute.cc
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//tony's ad-hoc test
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <functional>
#include <cmath>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <utility>
#include <cassert>
#include <ctest.h>
#include <isbinary.hpp>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_statistics_double.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <boost/bind.hpp>
#include <Sequence/Portability/random_shuffle.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>
using namespace std;
struct FETconfig
{
unsigned minor_cases,minor_controls;
double lnpv;
FETconfig(const unsigned &mica,const unsigned&mico,const double & p):
minor_cases(mica),minor_controls(mico),lnpv(p)
{
}
};
bool operator==(const FETconfig & lhs,
const FETconfig & rhs)
{
return (lhs.minor_cases == rhs.minor_cases &&
lhs.minor_controls == rhs.minor_controls);
}
bool operator<(const FETconfig & lhs,
const FETconfig & rhs)
{
return (lhs.minor_cases < rhs.minor_cases &&
lhs.minor_controls < rhs.minor_controls);
}
bool file_exists( const char * infile )
{
ifstream in(infile);
if(!in) return false;
return true;
}
int main( int argc, char ** argv )
{
int argn=1;
const char * infile = argv[argn++]; //the case-control genotype file
if(!file_exists(infile))
{
cerr << infile << " does not exist\n";
exit(10);
}
const char * outfile = argv[argn++];//name of output file
const unsigned ncases=atoi(argv[argn++]);//number of cases (assumes = no. controls)
const double minfreq = atof(argv[argn++]);//max MAF (0.05 for paper)
const unsigned mmarker = atoi(argv[argn++]); //max no. markers to use
const unsigned seed = atoi(argv[argn++]);
vector< vector<int> > data;
if(isbinary(infile))
{
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(boost::iostreams::file_source(infile,ios_base::in|ios_base::binary));
string temp,temp2;
getline(in,temp);
//figure out how many columns there are.
istringstream figure(temp);
unsigned ncol=0;
while( ! figure.eof() )
{
figure >> temp2 >> ws;
++ncol;
}
//now, read in the data, and store by column
data = vector<vector<int> >(ncol-4,vector<int>());
//deal with that first line of input again
istringstream firstline(temp);
int foo;
for(unsigned i=0;i<ncol-4;++i)
{
firstline >> foo >> ws;
data[i].push_back(foo);//atoi(temp.c_str()));
}
int genotype;
while(! in.eof() )
{
for(unsigned i=0;i<ncol-4;++i)
{
in >> genotype >> ws;
data[i].push_back(genotype);
}
if(!in.eof())
getline(in,temp); //skip rest of line
in >> ws;
}
}
else
{
ifstream in;
string temp,temp2;
getline(in,temp);
//figure out how many columns there are.
istringstream figure(temp);
unsigned ncol=0;
while( ! figure.eof() )
{
figure >> temp2 >> ws;
++ncol;
}
//now, read in the data, and store by column
data = vector<vector<int> >(ncol-4,vector<int>());
//deal with that first line of input again
istringstream firstline(temp);
int foo;
for(unsigned i=0;i<ncol-4;++i)
{
firstline >> foo >> ws;
data[i].push_back(foo);
}
int genotype;
while(! in.eof() )
{
for(unsigned i=0;i<ncol-4;++i)
{
in >> genotype >> ws;
data[i].push_back(genotype);
}
if(!in.eof())
getline(in,temp); //skip rest of line
in >> ws;
}
}
//stuff for fexact
int a=2,y=2;
int work=100000;
int leading = 2;
double expect,percnt,emin,prt,pre;
//get the unique data columns, and do allele freq and mincount checks.
vector<int> keep(data.size(),1);
unsigned failcount = 0;
for(unsigned i=0; i < data.size() ; ++i )
{
bool done = false;
vector<int> tcol(data[i].size());
transform(data[i].begin(),data[i].end(),
tcol.begin(),
bind2nd(plus<int>(),1));
unsigned sum = accumulate(tcol.begin(),tcol.end(),0);
if ( double(sum) < minfreq * double(4*ncases) )
//it is 4*ncases because we assume ncases = ncontrols, and diploid!!!
{
keep[i]=0;
++failcount;
done = true;
}
for( unsigned j=i+1 ; !done&&j < data.size() ; ++j )
{
if( data[i] == data[j] )
{
keep[i]=0;
done = true;
}
}
}
vector< vector<int> > reduced_data;
for( unsigned i=0;i<keep.size();++i )
{
if( keep[i] )
{
reduced_data.push_back(data[i]);
}
}
data.clear();
//now, get the FET for each of these markers in the columns we kept
vector<double> origFET;
vector<FETconfig> vFETconfig;
for( unsigned site=0;site<reduced_data.size() ;++site )
{
//vector<int> tcol;
//add 1 to each genotype to make MAF counting easy
transform(reduced_data[site].begin(),reduced_data[site].end(),
reduced_data[site].begin(),
//std::back_inserter(tcol),
bind2nd(plus<int>(),1));
assert(reduced_data[site].size() == 2*ncases);
//calculate FET
unsigned minor_control = accumulate(reduced_data[site].begin(),reduced_data[site].begin()+ncases,0);
unsigned major_control = 6000-minor_control;
unsigned minor_cases = accumulate(reduced_data[site].begin()+ncases,reduced_data[site].end(),0);
unsigned major_cases = 6000 - minor_cases;
vector<FETconfig>::iterator itr = find(vFETconfig.begin(),vFETconfig.end(),FETconfig(minor_cases,minor_control,0.));
if( itr == vFETconfig.end() )
{
double ctable[4];
ctable[0] = minor_control;
ctable[1] = minor_cases;
ctable[2] = major_control;
ctable[3] = major_cases;
expect=percnt=emin=prt=pre=-1.;
fexact(&a,&y,ctable,
&leading,&expect,&percnt,&emin,&prt,&pre,&work);
origFET.push_back( -log(pre) );//add -log(pval) to array
vFETconfig.push_back(FETconfig(minor_cases,minor_control,-log(pre)));
}
else
{
origFET.push_back( itr->lnpv );
}
}
//sort in descending order
sort(origFET.begin(),origFET.end(),
greater<double>());
//log of expected pvals
vector<double> logEpv;
for(unsigned i=0; i < origFET.size() ; ++i )
{
logEpv.push_back( -log( double(i+1)/double(origFET.size()) ) );
}
vector<double> kss;
for( unsigned i = 0 ; i < min(size_t(250),origFET.size()) ; ++i )//keep max of 1st 250
{
kss.push_back( (origFET[i]-logEpv[i])/log(10.) );
}
const double observed = accumulate(kss.begin(),kss.begin()+mmarker+1,0.);
if( ! isfinite(observed) )
{
ofstream out(outfile);
for(unsigned i=0;i<1001;++i)
{
out << "NA\n";
}
out.close();
exit(1);
}
//now, permute
vector<unsigned> indexes;
for(unsigned i = 0 ; i < reduced_data[0].size() ; ++i )
{
indexes.push_back(i);
}
bool permuting = true;
unsigned permutation = 0;
unsigned winners = 0;
unsigned npermutes = 1000;
//unsigned npermutes[] = {100,1000};
unsigned PI = 0;
const unsigned MAXP = 6;
gsl_rng * r = gsl_rng_alloc(gsl_rng_taus2);
gsl_rng_set(r,seed);
vector<double> pdist;
for( ; permutation < npermutes ; ++permutation )
{
vector<double> permuteFET;
Sequence::random_shuffle(indexes.begin(),indexes.end(),
boost::bind(gsl_ran_flat,r,0.,_1));
for( unsigned site=0;site<reduced_data.size() ;++site )
{
//vector<int> tcol;
//add 1 to each genotype to make MAF counting easy
assert(reduced_data[site].size() == 2*ncases);
//calculate FET
unsigned minor_control = 0;
unsigned minor_cases = 0;
for(unsigned j=0;j<reduced_data[site].size();++j)
{
if(j < ncases)
{
minor_cases += reduced_data[site][indexes[j]];
}
else
{
minor_control += reduced_data[site][indexes[j]];
}
}
unsigned major_control = 6000-minor_control;
unsigned major_cases = 6000 - minor_cases;
vector<FETconfig>::iterator itr = find(vFETconfig.begin(),vFETconfig.end(),
FETconfig(minor_cases,minor_control,0.));
if( itr == vFETconfig.end() )
{
double ctable[4];
ctable[0] = minor_control;
ctable[1] = minor_cases;
ctable[2] = major_control;
ctable[3] = major_cases;
expect=percnt=emin=prt=pre=-1.;
fexact(&a,&y,ctable,
&leading,&expect,&percnt,&emin,&prt,&pre,&work);
permuteFET.push_back( -log(pre) );//add -log(pval) to array
vFETconfig.push_back(FETconfig(minor_cases,minor_control,-log(pre)));
}
else
{
permuteFET.push_back(itr->lnpv);
}
}
//sort in descending order
sort(permuteFET.begin(),permuteFET.end(),
greater<double>());
vector<double> permlogEpv;
for(unsigned i=0; i < permuteFET.size() ; ++i )
{
permlogEpv.push_back( -log( double(i+1)/double(permuteFET.size()) ) );
}
vector<double> pkss;
for( unsigned i = 0 ; i < min(size_t(250),permuteFET.size()) ; ++i )//keep max of 1st 250
{
pkss.push_back( (permuteFET[i]-permlogEpv[i])/log(10.) );
}
double permstat = accumulate(pkss.begin(),pkss.begin()+mmarker+1,0.);
pdist.push_back(permstat);
}
ofstream out(outfile);
out << observed << '\n';
for(unsigned i=0;i<pdist.size();++i)
{
out << pdist[i] << '\n';
}
out.close();
}