-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathduo.cpp
More file actions
494 lines (425 loc) · 11.8 KB
/
Copy pathduo.cpp
File metadata and controls
494 lines (425 loc) · 11.8 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Standard Libraries
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
// Namespace
using std::iterator;
using std::sqrt;
using std::string;
// Custom Libraries
#include "duo.h"
#include "options.h"
#include "output.h"
#include "ped.h" // for Ped::personCount
////////////////////////
// Standard functions //
////////////////////////
void DuoMap::fillIdentities( Ped& p )
{
unsigned long int comparisons = (p.getPersonCount() * (p.getPersonCount() - 1)) / 2; // all possible pairwise combinations without replacement
reserve(comparisons);
for (unsigned long int i = 0; i < p.getPersonCount() - 1; ++i)
{
for (unsigned long int j = i + 1; j < p.getPersonCount(); ++j)
{
Duo tmpDuo( p[i], p[j] );
push_back( tmpDuo );
}
}
}
void DuoMap::getCounts( Ped& ped )
{
writeLog("Getting IBS counts\n");
// Go comparison by comparison to calculate counts
for (unsigned long int i = 0; i < ped.getPersonCount() - 1; ++i)
{
for (unsigned long int j = i + 1; j < ped.getPersonCount(); ++j)
{
unsigned long int ibs0Count = 0;
unsigned long int ibs1Count = 0;
unsigned long int ibs2Count = 0;
unsigned long int ibs2StarCount = 0;
vector<bool>::iterator i1a1 = ped[i]->beginIterA1();
vector<bool>::iterator i1a2 = ped[i]->beginIterA2();
vector<bool>::iterator i2a1 = ped[j]->beginIterA1();
vector<bool>::iterator i2a2 = ped[j]->beginIterA2();
vector<bool>::iterator i1NC = ped[i]->beginIterHasGenotype();
vector<bool>::iterator i2NC = ped[j]->beginIterHasGenotype();
while (i1a1 != ped[i]->endIterA1())
{
if (*i1NC)
{
if (*i2NC)
{
if (*i1a1 == *i2a1)
{
if (*i1a2 == *i2a2)
{
if (*i1a1 != *i1a2)
{
++ibs2StarCount;
}
++ibs2Count;
}
else
{
++ibs1Count;
}
}
else
{
if (*i1a2 == *i2a2)
{
++ibs1Count;
}
else
{
if (*i1a1 == *i1a2)
{
++ibs0Count;
}
else
{
++ibs2Count;
++ibs2StarCount;
}
}
}
}
}
++i1a1;
++i1a2;
++i2a1;
++i2a2;
++i1NC;
++i2NC;
}
Duo newDuo( ped[i], ped[j], ibs0Count, ibs1Count, ibs2Count, ibs2StarCount );
push_back(newDuo);
}
}
}
// void DuoMap::getCounts( Ped& ped )
// {
// writeLog("Getting IBS counts\n");
// // Go comparison by comparison to calculate counts
// for (unsigned long int i = 0; i < ped.getPersonCount() - 1; ++i)
// {
// for (unsigned long int j = i + 1; j < ped.getPersonCount(); ++j)
// {
// IbsCount tmp = getIBS( ped[i], ped[j] );
// Duo tmpDuo( ped[i], ped[j], tmp.getIbs0(), tmp.getIbs1(), tmp.getIbs2(), tmp.getIbs2Star() );
// push_back( tmpDuo ); // add to the vector
// }
// }
// }
IbsCount getIBS( Person* i1, Person* i2 )
{
///////////////////////////
// CONSIDER CHANGING TO A FUNCTION WITHIN THE DUO CLASS
// AND THE IBSCOUNT OBJECT COULD THEN BE PASSED DIRECTLY INTO A VECTOR, OR PASSED DIRECTLY INTO THE APPROPRIATE VECTOR FOR STORAGE
///////////////////////////
IbsCount tmp;
vector<bool>::iterator i1a1 = i1->beginIterA1();
vector<bool>::iterator i1a2 = i1->beginIterA2();
vector<bool>::iterator i2a1 = i2->beginIterA1();
vector<bool>::iterator i2a2 = i2->beginIterA2();
vector<bool>::iterator i1NC = i1->beginIterHasGenotype();
vector<bool>::iterator i2NC = i2->beginIterHasGenotype();
while (i1a1 != i1->endIterA1())
{
if (*i1NC)
{
if (*i2NC)
{
if (*i1a1 == *i2a1)
{
if (*i1a2 == *i2a2)
{
if (*i1a1 != *i1a2) { tmp.addIbs2Star(); }
tmp.addIbs2();
}
else tmp.addIbs1();
}
else
{
if (*i1a2 == *i2a2) tmp.addIbs1();
else
{
if (*i1a1 == *i1a2) tmp.addIbs0();
else
{
tmp.addIbs2();
tmp.addIbs2Star();
}
}
}
}
}
++i1a1;
++i1a2;
++i2a1;
++i2a2;
++i1NC;
++i2NC;
}
return tmp;
}
void DuoMap::getMeanAndSDFromCounts( )
{
writeLog("Calculating Mean and Standard Deviation of IBS\n");
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while ( currIter != endIter )
{
int ibs0 = (*currIter).getIbs0Count();
int ibs1 = (*currIter).getIbs1Count();
int ibs2 = (*currIter).getIbs2Count();
int ibs2star = (*currIter).getIbs2StarCount();
double mean = static_cast<double>((ibs2 * 2) + ibs1) / static_cast<double>(ibs0 + ibs1 + ibs2);
// set all the values in the current duo object
(*currIter).setMeanIbs( mean );
(*currIter).setSdIbs( calculateSD( mean, ibs0, ibs1, ibs2 ) );
(*currIter).setIbs2StarPercent( static_cast<double>(ibs2star) / static_cast<double>(ibs2 + ibs1 + ibs0) );
(*currIter).setInformativePercent( static_cast<double>((ibs2star + ibs0)) / static_cast<double>(ibs2 + ibs0 + ibs1) );
(*currIter).setIbs2StarPercentOfInformative( static_cast<double>(ibs2star) / static_cast<double>(ibs2star + ibs0) );
++currIter;
}
}
// ATTN POSSIBLE OPTIMIZATION
double calculateSD (double mean, int ibs0, int ibs1, int ibs2)
{
double tmp0 = ( 0.0 - mean ) * ( 0.0 - mean ) * static_cast<double>(ibs0);
double tmp1 = ( 1.0 - mean ) * ( 1.0 - mean ) * static_cast<double>(ibs1);
double tmp2 = ( 2.0 - mean ) * ( 2.0 - mean ) * static_cast<double>(ibs2);
return sqrt( (tmp2 + tmp1 + tmp0) / static_cast<double>(ibs0 + ibs1 + ibs2) );
}
void DuoMap::specifiedRelationships( Ped& ped )
{
writeLog("Getting specified relationships\n");
if (count() == 0) { fillIdentities( ped ); }
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
// Different family IDs = unrelated
if ((*currIter).getInd1()->getFid() != (*currIter).getInd2()->getFid()) { (*currIter).setSpecifiedRelationship( UNREL ); }
// Same family ID, but no parents = unrelated
else if ((*currIter).getInd1()->getPid() == "0" and (*currIter).getInd1()->getMid() == "0" and (*currIter).getInd2()->getPid() == "0" and (*currIter).getInd2()->getMid() == "0")
{ (*currIter).setSpecifiedRelationship( UNREL ); }
else
{
bool sharemother = false;
bool sharefather = false;
bool checkshare = false;
// ATTN POSSIBLE LOCAL VAR FOR OPTIMIZATION
// Ind 1 has a father
if ((*currIter).getInd1()->getPid() != "0")
{
// Ind1 father ID == Ind2 individual ID = parent child
if ((*currIter).getInd1()->getPid() == (*currIter).getInd2()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Ind2 has a father ID
else if ((*currIter).getInd2()->getPid() != "0")
{
// ParentID Ind2 == IndID of Ind1 = parent child
if ((*currIter).getInd2()->getPid() == (*currIter).getInd1()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Have same father
else if ((*currIter).getInd1()->getPid() == (*currIter).getInd2()->getPid())
{
sharefather = true;
checkshare = true;
}
// Have different father
else
{
sharefather = false;
checkshare = true;
}
}
// Ind2 doesn't have father ID
else
{
sharefather = false;
checkshare = true;
}
}
// Ind 1 doesn't have father listed
else
{
// Ind2 does have a father
if ((*currIter).getInd2()->getPid() != "0")
{
// Ind2 paternal == Ind1 ID = parent child
if ((*currIter).getInd2()->getPid() == (*currIter).getInd1()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Don't share father
else
{
sharefather = false;
checkshare = true;
}
}
// If Ind2 has no father listed can't share father
else
{
sharefather = false;
checkshare = true;
}
}
// Ind1 has mother ID
if ((*currIter).getInd1()->getMid() != "0")
{
// Ind1 MID == Ind2 IndID = parent child
if ((*currIter).getInd1()->getMid() == (*currIter).getInd2()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Ind2 has mother ID
else if ((*currIter).getInd2()->getMid() != "0")
{
// Ind2 mother ID == Ind2 IndID = parent child
if ((*currIter).getInd2()->getMid() == (*currIter).getInd1()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Ind1 mother == Ind2 mother
else if ((*currIter).getInd1()->getMid() == (*currIter).getInd2()->getMid())
{
sharemother = true;
checkshare = true;
}
// Ind1 different mother than Ind2
else
{
sharemother = false;
checkshare = true;
}
}
// Ind1 has mother ID but Ind2 doesn't
else
{
sharemother = false;
checkshare = true;
}
}
// Ind1 doesn't have mother ID
else if ((*currIter).getInd1()->getMid() == "0")
{
// Ind2 has mother ID
if ((*currIter).getInd2()->getMid() != "0")
{
// Ind2 mother ID == Ind1 IndID
if ((*currIter).getInd2()->getMid() == (*currIter).getInd1()->getIid())
{
(*currIter).setSpecifiedRelationship( PARCHLD );
continue;
}
// Ind2 mother ID isn't Ind1 IID
else
{
sharemother = false;
checkshare = true;
}
}
else
{
sharemother = false;
checkshare = true;
}
}
if (checkshare)
{
if (sharefather and sharemother) (*currIter).setSpecifiedRelationship( SIBS );
else if (sharefather or sharemother) (*currIter).setSpecifiedRelationship( OTHERREL );
else (*currIter).setSpecifiedRelationship( UNREL );
}
}
++currIter;
}
}
void DuoMap::oldCalculatedRelationships( )
{
writeLog("Getting calculated relationships (old method)\n");
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
if ((*currIter).getMeanIbs() > IDENT_MEANCUTOFF and (*currIter).getSdIbs() < IDENT_SDCUTOFF)
{
(*currIter).setCalculatedRelationship( IDENT );
}
else if ((*currIter).getSdIbs() > isFirstDegreeClassifier( (*currIter).getMeanIbs(), (*currIter).getSdIbs() ))
{
if ((*currIter).getSdIbs() < whichFirstDegreeClassifier( (*currIter).getMeanIbs(), (*currIter).getSdIbs() ))
{
(*currIter).setCalculatedRelationship( PARCHLD );
}
else
{
(*currIter).setCalculatedRelationship( SIBS );
}
}
// Methods for other relationships can go here
else
{
(*currIter).setCalculatedRelationship( UNREL );
}
++currIter;
}
}
void DuoMap::calculatedRelationships( )
{
writeLog("Getting calculated relationships\n");
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
if ((*currIter).getMeanIbs() > NEWIDENT_MEANCUTOFF and (*currIter).getSdIbs() < NEWIDENT_SDCUTOFF)
{
(*currIter).setCalculatedRelationship( IDENT ); // identical samples
}
else if ((*currIter).getInformativePercent() >= NEWPARCHLD_CUTOFF)
{
(*currIter).setCalculatedRelationship( PARCHLD ); // parchild
}
else if ((*currIter).getInformativePercent() >= NEWSIB_CUTOFF)
{
(*currIter).setCalculatedRelationship( SIBS ); // sibling
}
else if ((*currIter).getInformativePercent() >= NEWSECOND_CUTOFF)
{
(*currIter).setCalculatedRelationship( SECOND_DEGREE ); // second
}
else if ((*currIter).getInformativePercent() >= NEWSECONDORTHIRD_CUTOFF)
{
(*currIter).setCalculatedRelationship( SECOND_OR_THIRD_DEGREE ); // second or third
}
else if ((*currIter).getInformativePercent() >= NEWTHIRD_CUTOFF)
{
(*currIter).setCalculatedRelationship( THIRD_DEGREE ); // third
}
else if ((*currIter).getInformativePercent() >= NEWCRYPTIC_CUTOFF)
{
(*currIter).setCalculatedRelationship( CRYPTIC ); // cryptic
}
else
{
(*currIter).setCalculatedRelationship( UNREL );
}
++currIter;
}
}