-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoutput.cpp
More file actions
508 lines (412 loc) · 15.1 KB
/
Copy pathoutput.cpp
File metadata and controls
508 lines (412 loc) · 15.1 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Standard Libraries
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
// imports
using std::cout;
using std::ios;
using std::iterator;
using std::ofstream;
using std::setprecision;
using std::string;
// Custom headers
#include "duo.h"
#include "options.h"
#include "output.h"
// RELATIONSHIP const string comes from helper.h
// Used to maintain flexibility in adding relationship classes
// LOG declared in snpduo.cpp
extern ofstream LOG;
int const PRECISE = 7; // used to specify precision for output
//////////////////
// Log function //
//////////////////
void writeLog( string const& s )
{
LOG << s;
LOG.flush();
if (par::verbose)
{
cout << s;
cout.flush();
}
}
////////////////////////////////
// Duo Class output functions //
////////////////////////////////
void DuoMap::printCounts()
{
string file = par::out + ".count";
ofstream COUNTS( file.c_str(), ios::out );
if (not COUNTS.good()) { error("Could not create count file " + string2Filename(file)); }
writeLog( "Writing pair-wise IBS counts to " + string2Filename(file) + "\n");
COUNTS << "FID1,IID1,FID2,IID2,IBS0,IBS1,IBS2,IBS2*\n";
for (vector<Duo>::iterator currIter = begin(); currIter != end(); ++currIter)
{
COUNTS
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2()->getIid() << ","
<< (*currIter).getIbs0Count() << "," << (*currIter).getIbs1Count() << "," << (*currIter).getIbs2Count()
<< "," << (*currIter).getIbs2StarCount() << "\n";
}
COUNTS.close();
}
void DuoMap::printMeanSD()
{
string file = par::out + ".summary";
ofstream MEANSD( file.c_str(), ios::out );
if (not MEANSD.good()) { error( "Could not create Mean / SD file " + string2Filename(file) + "\n" ); }
writeLog( "Writing pair-wise Mean and SD summary information to " + string2Filename(file) + "\n" );
MEANSD.setf( ios::fixed );
MEANSD << "FID1,IID1,FID2,IID2,IBS0,IBS1,IBS2,IBS2*,Mean_IBS,SD_IBS,IBS2*%,Informative%,IBS2*%_Of_Informative" << "\n";
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
MEANSD
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2()->getIid() << ","
<< (*currIter).getIbs0Count() << "," << (*currIter).getIbs1Count() << "," << (*currIter).getIbs2Count()
<< "," << (*currIter).getIbs2StarCount() << "," << setprecision( PRECISE ) << (*currIter).getMeanIbs()
<< "," << setprecision( PRECISE ) << (*currIter).getSdIbs() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercent() << "," << setprecision( PRECISE ) << (*currIter).getInformativePercent()
<< "," << setprecision( PRECISE ) << (*currIter).getIbs2StarPercentOfInformative() << "\n";
++currIter;
}
MEANSD.close();
}
void DuoMap::printRelationships()
{
string file = par::out + ".specified";
ofstream RELATE( file.c_str(), ios::out );
if (not RELATE.good()) { error( "Couldn't create file " + string2Filename(file) + "\n" ); }
writeLog("Writing pair-wise relationships specified from input Pedigrees to " + string2Filename(file) + "\n");
if (count() == 0) { error( "Asked to print relationships, but no specified relationships found!" ); }
RELATE << "FID1,IID1,FID2,IID2,SpecifiedRelationship" << "\n";
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
RELATE
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2() << ","
<< RELATIONSHIP[ (*currIter).getSpecifiedRelationship() ] << "\n";
++currIter;
}
RELATE.close();
}
void DuoMap::printSpecifiedAndCalculated()
{
string file = par::out + ".theoretical";
ofstream THEORY( file.c_str(), ios::out );
if (not THEORY.good()) { error( "Could not write to file " + string2Filename(file) + "\n" ); }
THEORY.setf( ios::fixed );
writeLog("Writing theoretical relationships to " + string2Filename(file) + "\n");
THEORY << "FID1,IID1,FID2,IID2,IBS0,IBS1,IBS2,IBS2*,Mean_IBS,SD_IBS,IBS2*%,Informative%,IBS2*_Percent_Of_Informative,SpecifiedRelationship,CalculatedRelationship" << "\n";
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while( currIter != endIter )
{
THEORY
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2()->getIid() << ","
<< (*currIter).getIbs0Count() << "," << (*currIter).getIbs1Count() << "," << (*currIter).getIbs2Count() << ","
<< (*currIter).getIbs2StarCount() << "," << setprecision( PRECISE ) << (*currIter).getMeanIbs() << ","
<< setprecision( PRECISE ) << (*currIter).getSdIbs() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercent() << "," << setprecision( PRECISE ) << (*currIter).getInformativePercent()
<< "," << setprecision( PRECISE ) << (*currIter).getIbs2StarPercentOfInformative() << ","
<< RELATIONSHIP[ (*currIter).getSpecifiedRelationship() ] << "," << RELATIONSHIP[ (*currIter).getCalculatedRelationship() ]
<< "\n";
++currIter;
}
THEORY.close();
}
void DuoMap::printCalculatedOnly()
{
string file = par::out + ".calculated";
ofstream THEORY( file.c_str(), ios::out );
if (not THEORY.good()) { error( "Could not create file " + string2Filename(file) + "\n" ); }
THEORY.setf( ios::fixed );
writeLog( "Writing theoretical relationships to " + string2Filename(file) + "\n" );
THEORY << "FID1,IID1,FID2,IID2,IBS0,IBS1,IBS2,IBS2*,Mean_IBS,SD_IBS,IBS2*%,Informative%,IBS2*%_Of_Informative,CalculatedRelationship" << "\n";
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while( currIter != endIter )
{
THEORY
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2()->getIid() << ","
<< (*currIter).getIbs0Count() << "," << (*currIter).getIbs1Count() << "," << (*currIter).getIbs2Count()
<< "," << (*currIter).getIbs2StarCount() << "," << setprecision( PRECISE ) << (*currIter).getMeanIbs()
<< "," << setprecision( PRECISE ) << (*currIter).getSdIbs() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercent() << "," << (*currIter).getInformativePercent() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercentOfInformative() << "," << RELATIONSHIP[ (*currIter).getCalculatedRelationship() ] << "\n";
++currIter;
}
THEORY.close();
}
void DuoMap::printConflicted()
{
string file = par::out + ".conflicted";
ofstream CONFLICTED( file.c_str(), ios::out );
if (not CONFLICTED.good()) { error( "Could not create file " + string2Filename(file) + "\n" ); }
CONFLICTED.setf( ios::fixed );
writeLog( "Writing relationships that conflict with specified Pedigrees to " + string2Filename(file) + "\n" );
CONFLICTED << "FID1,IID1,FID2,IID2,IBS0,IBS1,IBS2,IBS2*,Mean_IBS,SD_IBS,IBS2*%,Informative%,IBS2*%_Of_Informative,SpecifiedRelationship,CalculatedRelationship" << "\n";
vector<Duo>::iterator currIter = begin();
vector<Duo>::iterator endIter = end();
while (currIter != endIter)
{
if ((*currIter).getSpecifiedRelationship() != (*currIter).getCalculatedRelationship())
{
CONFLICTED
<< (*currIter).getInd1()->getFid() << "," << (*currIter).getInd1()->getIid() << ","
<< (*currIter).getInd2()->getFid() << "," << (*currIter).getInd2()->getIid() << ","
<< (*currIter).getIbs0Count() << "," << (*currIter).getIbs1Count() << "," << (*currIter).getIbs2Count()
<< "," << (*currIter).getIbs2StarCount() << "," << setprecision( PRECISE ) << (*currIter).getMeanIbs()
<< "," << setprecision( PRECISE ) << (*currIter).getSdIbs() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercent() << "," << (*currIter).getInformativePercent() << "," << setprecision( PRECISE )
<< (*currIter).getIbs2StarPercentOfInformative() << "," << RELATIONSHIP[ (*currIter).getCalculatedRelationship() ] << "\n";
}
++currIter;
}
CONFLICTED.close();
}
////////////////////////////////
// Ped Class output functions //
////////////////////////////////
void Ped::write(LocusMap& lm)
{
string file = par::out + ".ped";
ofstream PED( file.c_str(), ios::out );
if (not PED.good()) { error("Could not write to file " + string2Filename(file) + "\n"); }
writeLog("Writing ped file " + string2Filename(file) + "\n");
for (vector<Person*>::iterator perIter = begin(); perIter != end(); ++perIter)
{
PED
<< (*perIter)->getFid() << " " << (*perIter)->getIid() << " "
<< (*perIter)->getPid() << " " << (*perIter)->getMid() << " "
<< (*perIter)->getSex() << " " << (*perIter)->getPheno();
// iterate through SNPs AND locus
vector<bool>::iterator a1Iter = (*perIter)->beginIterA1();
vector<bool>::iterator a2Iter = (*perIter)->beginIterA2();
vector<bool>::iterator called = (*perIter)->beginIterHasGenotype();
vector<Locus>::iterator mapIter = lm.begin();
while (a1Iter != (*perIter)->endIterA1() and a2Iter != (*perIter)->endIterA2() and called != (*perIter)->endIterHasGenotype() and mapIter != lm.end())
{
// passes bool values for has a genotype call, and what alleles 1 and 2 are.
PED << fileGenotypeString( *called, *a1Iter, *a2Iter, *mapIter );
++a1Iter;
++a2Iter;
++called;
++mapIter;
}
PED << "\n";
}
PED.close();
}
/////////////////////////////////////
// LocusMap Class output functions //
/////////////////////////////////////
void LocusMap::write()
{
string file = par::out + ".map";
ofstream MAP( file.c_str(), ios::out );
if (not MAP.good()) { error( "Couldn't write map file " + string2Filename(file) + "\n" ); }
writeLog("Writing map file " + string2Filename(file) + "\n");
for (vector<Locus>::iterator currIter = begin(); currIter != end(); ++currIter)
{
MAP
<< currIter->getChrom() << " "
<< currIter->getId() << " "
<< currIter->getGenetic() << " "
<< currIter->getPosition() << "\n";
}
MAP.close();
}
///////////////////////////////
// Transposed format writing //
///////////////////////////////
void writeTranspose( Ped& ped, LocusMap& lm )
{
// Write the tfam file
string file = par::out + ".tfam";
ofstream TFAM( file.c_str(), ios::out );
if (not TFAM.good()) { error( "Couldn't write TFAM file " + string2Filename(file) + "\n" ); }
writeLog( "Writing tfam file " + string2Filename(file) + "\n" );
for (vector<Person*>::iterator perIter = ped.begin(); perIter != ped.end(); ++perIter)
{
TFAM
<< (*perIter)->getFid() << " " << (*perIter)->getIid() << " "
<< (*perIter)->getPid() << " " << (*perIter)->getMid() << " "
<< (*perIter)->getSex() << " " << (*perIter)->getPheno() << "\n";
}
TFAM.close();
// Write the TPED file
file = par::out + ".tped";
ofstream TPED( file.c_str(), ios::out );
if (not TPED.good()) { error( "Couldn't write TPED file " + string2Filename(file) + "\n" ); }
writeLog( "Writing tped file " + string2Filename(file) + "\n" );
unsigned int perCount = 0;
for (vector<Locus>::iterator mapIter = lm.begin(); mapIter != lm.end(); ++mapIter)
{
TPED
<< mapIter->getChrom() << " "
<< mapIter->getId() << " "
<< mapIter->getGenetic() << " "
<< mapIter->getPosition();
for (vector<Person*>::iterator perIter = ped.begin(); perIter != ped.end(); ++perIter)
{
TPED << ped.fileGenotypeString( (*perIter)->getHasGenotype(perCount), (*perIter)->getA1(perCount), (*perIter)->getA2(perCount), *mapIter );
++perIter;
}
TPED << "\n";
++perCount;
}
}
///////////////////////////////////////////////////
// Function to write data in a SNPDuo compatible //
// AA/AB/BB coding nomenclature //
///////////////////////////////////////////////////
void writeForWeb( Ped& ped, LocusMap& lm )
{
string file = par::out + ".csv";
string geno = "";
string chrTmp = "";
writeLog( "Writing data to Custom format file for Web SNPDuo to file " + string2Filename(file) + "\n" );
// Open file
ofstream WEB( file.c_str(), ios::out );
if (not WEB.good()) { error( "Cannot write Web SNPDuo output " + string2Filename(file) + "!"); }
// Write headers
WEB << "Chromosome,Physical.Position,RSID,A/B";
for (vector<Person*>::iterator perIter = ped.begin(); perIter != ped.end(); ++perIter)
{
WEB << "," << (*perIter)->getFid() << "_" << (*perIter)->getIid();
}
WEB << "\n";
unsigned int counter = 0;
for (vector<Locus>::iterator mapIter = lm.begin(); mapIter != lm.end(); ++mapIter)
{
WEB << CHROM_STRINGS[ mapIter->getChrom() ] << "," << mapIter->getPosition() << "," << mapIter->getId() << ",";
if (mapIter->getHasAllele1())
{
if (mapIter->getHasAllele2())
{
if (mapIter->getAllele1Major())
{
WEB << mapIter->getAllele1() << "/" << mapIter->getAllele2();
}
else
{
WEB << mapIter->getAllele2() << "/" << mapIter->getAllele1();
}
}
else
{
WEB << mapIter->getAllele1() << "/N";
}
}
else
{
// if there is no allele 1 there should be no allele 2 (allele 1 is populated first)
WEB << "N/N";
}
/////////////////////////////////
// Process each persons allele //
/////////////////////////////////
// ATTN may be a good place to cache the allele1 major / allele 2 major
// check performance at some point to see if it impacts speed.
for (vector<Person*>::iterator perIter = ped.begin(); perIter != ped.end(); ++perIter)
{
if ( mapIter->getHasAllele1() )
{
if ( (*perIter)->getHasGenotype(counter) )
{
if ( mapIter->getAllele1Major() )
{
// allele 1 is major allele in current population
// allele 1 == A
// allele 2 == B
if ( (*perIter)->getA1(counter))
{
// has one copy of allele 2
if ( (*perIter)->getA2(counter) )
{
// homozygous allele 2
WEB << ",BB";
}
else
{
// heterozygous
WEB << ",AB";
}
}
else
{
// does not have a first copy of allele 2
if ( (*perIter)->getA2(counter) )
{
// heterozygous
WEB << ",AB";
}
else
{
// homozygous allele 1
WEB << ",AA";
}
}
}
else
{
// allele 2 is major allele in current population
// allele 1 == B
// allele 2 == A
if ( (*perIter)->getA1(counter))
{
// has one copy of allele 2
if ( (*perIter)->getA2(counter) )
{
// homozygous allele 2
WEB << ",AA";
}
else
{
// heterozygous
WEB << ",AB";
}
}
else
{
// does not have a first copy of allele 2
if ( (*perIter)->getA2(counter) )
{
// heterozygous
WEB << ",AB";
}
else
{
// homozygous allele 1
WEB << ",BB";
}
}
}
}
else
{
WEB << ",NC";
}
}
else
{
// no genotype means this is a no call
// if there are no alleles for this marker then it should be caught in the previous if statement
WEB << ",NC";
}
}
WEB << "\n";
++counter;
}
// Close file
WEB.close();
}