-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnvqt.cpp
291 lines (198 loc) · 7.58 KB
/
cnvqt.cpp
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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2008 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <iterator>
#include "plink.h"
#include "helper.h"
#include "options.h"
#include "perm.h"
extern Plink * PP;
void Plink::runTestCNVwithQT(Perm & perm)
{
// Permutation test for mean difference in QT between people with
// versus without a CNV. By default two-sided, unless
// par::segment_test_force_1sided = T
// Optionally allowed for this to operate on smoothed data (i.e.
// average of event count over a KB window, forwards and backwards
// from the given position)
// Also performs genome-wide burden analyses for QTs -- is there
// an association between CNV size and QT, for example, etc. These
// are based on standard correlation
int validN = 0;
double grandMean = 0;
for (int i=0; i<n; i++)
{
if ( !sample[i]->missing )
{
grandMean += sample[i]->phenotype;
++validN;
}
}
grandMean /= (double)validN;
printLOG("Total sample mean is " + dbl2str(grandMean) + ", based on "
+ int2str( validN ) + " individuals\n");
//////////////////////////////////////////
// Test positons = MAP positions (nl_all)
// Test positions = summed segment counts ( get from original counts )
// Test position = aggregate statistics ( 7 tests)
int nt = nl_all;
// IGNORE THIS FOR NOW...
// if ( par::seg_test_region )
// nt = coverage_aff.size();
////////////////////////////////////////////////////////////////////
// //
// Set up for individual burden tests? //
// //
////////////////////////////////////////////////////////////////////
// if ( par::cnv_indiv_perm )
// nt = 7;
// Option per-individual summary tests? (4 tests)
// Correlation between QT and these measures:
// total # segs
// # people w/ 1+ seg
// total kb length
// mean segment length
// gene-count
// atleast-1-gene-count
// gene-enrichment
////////////////////////////////////////////////////////////////////
// //
// Initialise permutation procedures //
// //
////////////////////////////////////////////////////////////////////
perm.setTests(nt);
perm.setPermClusters(*this);
perm.originalOrder();
vector_t original(nt);
////////////////////////////////////////////////////////////////////
// //
// Standard positional tests //
// //
////////////////////////////////////////////////////////////////////
if ( par::cnv_indiv_perm )
error("Not implemented --cnv-indiv-perm for QTs yet");
// Test statistic is difference in QT bewteen people with
// versus without a CNV at this position
vector_t count;
vector_t m1;
vector_t m0;
original = testCNVwithQT(grandMean, validN, nt, count, m1, m0);
////////////////////////////////////////////////////////////////////
// //
// Report to summary file //
// //
////////////////////////////////////////////////////////////////////
string f = par::output_file_name + ".cnv.qt.summary";
printLOG("Writing CNV QT summary to [ "+f+" ]\n");
ofstream FOUT;
FOUT.open( f.c_str() , ios::out );
FOUT.precision(4);
FOUT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(12) << "BP" << " "
<< setw(8) << "NCNV" << " "
<< setw(12) << "M1" << " "
<< setw(12) << "M0" << "\n";
for (int l=0; l<nt; l++)
{
FOUT << setw(4) << locus[l]->chr << " "
<< setw(par::pp_maxsnp) << locus[l]->name << " "
<< setw(12) << locus[l]->bp << " "
<< setw(8) << count[l] << " ";
if ( count[l] > 0 )
FOUT << setw(12) << m1[l] << " ";
else
FOUT << setw(12) << "NA" << " ";
FOUT << setw(12) << m0[l] << "\n";
}
FOUT.close();
////////////////////////////////////////////////////////////////////
// //
// Run permutations //
// //
////////////////////////////////////////////////////////////////////
bool finished = false;
while(!finished)
{
perm.permuteInCluster();
vector_t pr = testCNVwithQT(grandMean, validN, nt, count, m1, m0);
finished = perm.update(pr,original);
}
if (!par::silent)
cout << "\n\n";
////////////////////////////////////////////////////////////////////
// //
// Display permuted results //
// //
////////////////////////////////////////////////////////////////////
f += ".mperm";
printLOG("Writing CNV QT permutation results to [ "+f+" ]\n");
FOUT.open( f.c_str() , ios::out );
FOUT.precision(4);
FOUT << setw(4) << "CHR" << " "
<< setw(par::pp_maxsnp) << "SNP" << " "
<< setw(12) << "BP" << " "
<< setw(12) << "EMP1" << " "
<< setw(12) << "EMP2" << "\n";
for (int l=0; l<nt; l++)
{
FOUT << setw(4) << locus[l]->chr << " "
<< setw(par::pp_maxsnp) << locus[l]->name << " "
<< setw(12) << locus[l]->bp << " "
<< setw(12) << perm.pvalue( l ) << " "
<< setw(12) << perm.max_pvalue( l ) << "\n";
}
FOUT.close();
}
vector_t Plink::testCNVwithQT( double grandMean, int validN, int nt ,
vector_t & count,
vector_t & m1,
vector_t & m0 )
{
vector_t score(nt,0);
m1.clear();
m0.clear();
count.clear();
m1.resize(nt,0);
m0.resize(nt, grandMean * validN) ;
count.resize(nt,0);
// Calculate QT mean for people with CNVs
vector<Segment>::iterator s = segment.begin();
while ( s != segment.end() )
{
for (int l = s->start ; l <= s->finish; l++)
{
++count[ l ];
m1[ l ] += s->p1->pperson->phenotype;
}
++s;
}
// Calculate QT mean for all other people, given grand mean
for ( int l = 0 ; l < nl_all ; l++ )
{
int k = validN - (int)count[l] ;
m0[l] = k > 0 ? ( m0[l] - m1[l] ) / (double)k : 0 ;
m1[l] = count[ l ] > 0 ?
m1[l] / count[ l ] :
0 ;
score[ l ] = m1[l] - m0[l];
if ( par::segment_test_force_1sided )
{
if ( score[ l ] < 0 )
score[ l ] = 0;
}
}
return score;
}