-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBirdseedData.cpp
614 lines (558 loc) · 18.8 KB
/
BirdseedData.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* BirdseedData.cpp
*
* Created on: Feb 12, 2012
* Author: billwhite
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "BirdseedData.h"
#include "Insilico.h"
#include "StringUtils.h"
using namespace std;
using namespace insilico;
BirdseedData::BirdseedData() {
snpsFilename = "";
subjectLabelsFilename = "";
hasSubjectLabels = false;
excludeSnpsFilename = "";
includeSnpsFilename = "";
hasIncludedSnps = false;
hasExcludedSnps = false;
phenosFilename = "";
hasPhenotypes = false;
}
BirdseedData::~BirdseedData() {
}
bool BirdseedData::LoadData(string snpsFile, string phenoFile, string subjectsFile,
string includeSnpsFile, string excludeSnpsFile) {
// temporary string for reading file lines
string line;
map<string, string> labelsMap;
/// read subjects file if specified
if(subjectsFile != "") {
subjectLabelsFilename = subjectsFile;
cout << Timestamp() << "Reading subject names to labels map from ["
<< subjectLabelsFilename << "]" << endl;
ifstream subjectsStream(subjectLabelsFilename.c_str());
if (!subjectsStream.is_open()) {
cerr << "ERROR: Could not open subjects file: "
<< subjectLabelsFilename << endl;
return false;
}
int lineNumber = 0;
while (getline(subjectsStream, line)) {
++lineNumber;
string trimmedLine = trim(line);
if (!trimmedLine.size()) {
cout << "WARNING: Blank line skipped at line number: "
<< lineNumber << endl;
continue;
}
vector<string> labelKeyValue;
split(labelKeyValue, trimmedLine, "\t");
labelsMap[labelKeyValue[0]] = labelKeyValue[1];
}
subjectsStream.close();
hasSubjectLabels = true;
}
/// read SNP exclusion file
if(excludeSnpsFile != "") {
excludeSnpsFilename = excludeSnpsFile;
cout << Timestamp() << "Reading excluded SNPs from ["
<< excludeSnpsFilename << "]" << endl;
ifstream excludeSnpsStream(excludeSnpsFilename.c_str());
if (!excludeSnpsStream.is_open()) {
cerr << "ERROR: Could not open SNP exclusion file: "
<< excludeSnpsFilename << endl;
return false;
}
int lineNumber = 0;
while (getline(excludeSnpsStream, line)) {
++lineNumber;
string trimmedLine = trim(line);
if (!trimmedLine.size()) {
cout << "WARNING: Blank line skipped at line number: "
<< lineNumber << endl;
continue;
}
excludeSnps.push_back(trimmedLine);
}
excludeSnpsStream.close();
hasExcludedSnps = true;
cout << Timestamp() << excludeSnps.size()
<< " SNPs in exclusion list" << endl;
}
/// read SNP inclusion file
if(includeSnpsFile != "") {
includeSnpsFilename = includeSnpsFile;
cout << Timestamp() << "Reading included SNPs from ["
<< includeSnpsFilename << "]" << endl;
ifstream includeSnpsStream(includeSnpsFilename.c_str());
if (!includeSnpsStream.is_open()) {
cerr << "ERROR: Could not open SNP inclusion file: "
<< includeSnpsFilename << endl;
return false;
}
int lineNumber = 0;
while (getline(includeSnpsStream, line)) {
++lineNumber;
string trimmedLine = trim(line);
if (!trimmedLine.size()) {
cout << "WARNING: Blank line skipped at line number: "
<< lineNumber << endl;
continue;
}
includeSnps.push_back(trimmedLine);
}
includeSnpsStream.close();
hasIncludedSnps = true;
cout << Timestamp() << includeSnps.size()
<< " SNPs in inclusion list" << endl;
}
/// read SNPs data from the Birdsuite Birdseed SNP call file
snpsFilename = snpsFile;
ifstream genotypesStream(snpsFilename.c_str());
if (!genotypesStream.is_open()) {
cerr << "ERROR: Could not open SNPs file: " << snpsFilename << endl;
return false;
}
cout << Timestamp() << "Reading SNPs from [" << snpsFilename << "]"
<< endl;
/// skip any header comment lines
bool readingComments = true;
while(readingComments) {
if(getline(genotypesStream, line)) {
if(line[0] != '#') {
readingComments = false;
}
}
else {
cerr << "Unexpected end-of-file reading " << snpsFilename << endl;
return false;
}
}
/// assumption: past any comment rows and at the the header row
/// 7 fields per subject
const int COLUMNS_PER_SUBJECT = 7;
// cout << "File header:" << endl << line << endl;
vector<string> tokens;
split(tokens, line, "\t");
vector<string>::const_iterator it = tokens.begin();
string probeId = *it;
unsigned int numSubjects = 0;
++it;
for (; it != tokens.end()-1; it += COLUMNS_PER_SUBJECT) {
string subjectName = *it;
// cout << Timestamp() << "HEADER: Sample name: [" << subjectName << "]" << endl;
// check that the subject name is in the subjKeys list
// Remove quotes from subject names
subjectName.erase(remove(subjectName.begin(), subjectName.end(), '"'), subjectName.end());
// add to the subjectNames list
subjectNames.push_back(subjectName);
if(hasSubjectLabels) {
string nameKey = subjectName.substr(0, 7);
if(labelsMap.find(nameKey) != labelsMap.end()) {
subjectLabels.push_back(labelsMap[nameKey]);
}
else {
cerr << "ERROR: Subject names file given but no key found for "
<< "subject name in data set: " << nameKey << endl;
return false;
}
}
++numSubjects;
}
if(hasSubjectLabels) {
if(subjectLabels.size() != subjectNames.size()) {
cerr << "ERROR: Number of subjects read from file " << subjectNames.size()
<< " does not equal the number of labels specified in the subjects file "
<< subjectLabelsFilename << ", which has " << subjectLabels.size()
<< " labels" << endl;
return false;
}
}
if(subjectNames.size()) {
cout << Timestamp() << subjectNames.size() << " subjects included" << endl;
}
else {
cerr << "No subjects read from the file header" << endl;
return false;
}
/// read SNP genotypes across all SNPs and all subjects
vector<vector<string> > fileGenotypes;
cout << Timestamp()
<< "Reading and encoding SNP data for all subjects" << endl;
unsigned int lineNumber = 0;
vector<string> tmpSelectedSnpNames;
int numExcludedSnps = 0;
int numIncludedSnps = 0;
int numMissingSnps = 0;
while (getline(genotypesStream, line)) {
++lineNumber;
if(lineNumber % 100000 == 0) {
cout << Timestamp() << lineNumber << endl;
}
string trimmedLine = trim(line);
// no blank lines in the data section
if (!trimmedLine.size()) {
cout << "WARNING: Blank line skipped at line number: "
<< lineNumber << endl;
continue;
}
/// split the line into genotypes
vector<string> birdseedLineParts;
split(birdseedLineParts, trimmedLine, "\t");
/// first field is the Affymetrix SNP ID
string snpID = birdseedLineParts[0];
/// check for the inclusion/exclusion of this snpID
if(hasExcludedSnps) {
if(find(excludeSnps.begin(), excludeSnps.end(), snpID) != excludeSnps.end()) {
/// found - skip this SNP
++numExcludedSnps;
continue;
}
}
if(hasIncludedSnps) {
if(find(includeSnps.begin(), includeSnps.end(), snpID) == includeSnps.end()) {
/// not found - skip this SNP
continue;
}
else {
++numIncludedSnps;
}
}
tmpSelectedSnpNames.push_back(snpID);
//cout << Timestamp() << "Splitting SNP [" << snpID
// << "] line into genotypes and allele counts" << endl;
vector<string> thisSnpGenotypes;
vector<double> thisSnpConfidences;
map<char, unsigned int> thisSnpAlleles;
map<string, unsigned int> thisSnpGenotypeCounts;
char allele1, allele2;
int count = 0;
for (unsigned int colIdx = COLUMNS_PER_SUBJECT;
colIdx < birdseedLineParts.size();
colIdx += COLUMNS_PER_SUBJECT) {
string thisStringGenotype = birdseedLineParts[colIdx-1];
double thisConfidence = boost::lexical_cast<double>(birdseedLineParts[colIdx-5]);
thisSnpConfidences.push_back(thisConfidence);
thisSnpGenotypes.push_back(thisStringGenotype);
++count;
/// skip missing genotypes for allele updates
if(thisStringGenotype != "---") {
++thisSnpGenotypeCounts[thisStringGenotype];
allele1 = thisStringGenotype[0];
allele2 = thisStringGenotype[1];
++thisSnpAlleles[allele1];
++thisSnpAlleles[allele2];
// if(snpID == "SNP_A-1978185") {
// cout << colIdx << ": " << count << ": " << thisStringGenotype << "("
// << allele1 << "," << allele2 << ")" << endl;
// }
}
else {
/// missing data detected
++numMissingSnps;
//cout << "MISSING!" << endl;
}
}
// cout << endl;
/// save the attributes for this SNP
confidences.push_back(thisSnpConfidences);
snpAlleleCounts.push_back(thisSnpAlleles);
genotypeCounts.push_back(thisSnpGenotypeCounts);
fileGenotypes.push_back(thisSnpGenotypes);
}
cout << Timestamp() << lineNumber << endl;
genotypesStream.close();
cout << Timestamp() << "Read " << numMissingSnps << " missing SNPs from Birdseed file" << endl;
int numSnps = tmpSelectedSnpNames.size();
if((int) snpAlleleCounts.size() != numSnps) {
cerr << "Allele counts vector not equal to number of SNPs" << endl;
return false;
}
if((int) genotypeCounts.size() != numSnps) {
cerr << "Genotype counts vector not equal to number of SNPs" << endl;
return false;
}
cout << Timestamp() << "Read " << numSnps << " SNPs from Birdseed file" << endl;
// --------------------------------------------------------------------------
/// for each SNP, map two-allele genotypes to integers using allele frequencies
cout << Timestamp() << "Mapping genotype strings to integers" << endl;
int monomorphs = 0;
for(int snpIndex=0; snpIndex < numSnps; ++snpIndex) {
// cout << endl << "Getting SNP info for index: " << snpIndex << endl;
map<char, unsigned int> thisAlleleCounts = snpAlleleCounts[snpIndex];
map<string, unsigned int> thisGenotypeMap = genotypeCounts[snpIndex];
string majorAllele = " ";
string minorAllele = " ";
map<string, int> thisGenotypeStringMap;
int majorAlleleCount = 0;
// cout << "allele map size: " << thisAlleleCounts.size() << endl;
// cout << "genotype map size: " << thisGenotypeMap.size() << endl;
if(thisGenotypeMap.size() == 0) {
// all missing SNPs
cout << Timestamp() << "WARNING: SNP " << tmpSelectedSnpNames[snpIndex]
<< " has all missing data - skipping" << endl;
continue;
}
if(thisGenotypeMap.size() == 1) {
cout << Timestamp() << "WARNING: SNP " << tmpSelectedSnpNames[snpIndex]
<< " is monomorphic - keeping" << endl;
map<char, unsigned int>::const_iterator monoIt = thisAlleleCounts.begin();
char onlyAllele = monoIt->first;
unsigned int onlyAlleleCount = monoIt->second;
majorAllele[0] = onlyAllele;
minorAllele[0] = onlyAllele;
majorAlleleCount = onlyAlleleCount;
++monomorphs;
thisGenotypeStringMap[majorAllele + majorAllele] = 0;
thisGenotypeStringMap[majorAllele + minorAllele] = 0;
thisGenotypeStringMap[minorAllele + minorAllele] = 0;
}
else {
map<char, unsigned int>::const_iterator thisAlleleCountsIt =
thisAlleleCounts.begin();
char allele1 = thisAlleleCountsIt->first;
int allele1Count = thisAlleleCountsIt->second;
++thisAlleleCountsIt;
char allele2 = thisAlleleCountsIt->first;
int allele2Count = thisAlleleCountsIt->second;
majorAllele[0] = allele1;
minorAllele[0] = allele2;
if(allele1Count > allele2Count) {
majorAllele[0] = allele1;
minorAllele[0] = allele2;
majorAlleleCount = allele1Count;
}
else {
majorAllele[0] = allele2;
minorAllele[0] = allele1;
majorAlleleCount = allele2Count;
}
thisGenotypeStringMap[majorAllele + majorAllele] = 0;
thisGenotypeStringMap[majorAllele + minorAllele] = 1;
thisGenotypeStringMap[minorAllele + majorAllele] = 1;
thisGenotypeStringMap[minorAllele + minorAllele] = 2;
}
// cout << tmpSelectedSnpNames[snpIndex] << endl;
snpNames.push_back(tmpSelectedSnpNames[snpIndex]);
snpMajorMinorAlleles.push_back(make_pair(majorAllele[0], minorAllele[0]));
// cout << majorAllele << "/" << minorAllele << endl;
// cout << "mapping strings to ints through map" << endl;
/// map genotype string vector to genotype int vector
vector<string> thisSnpGenotypes = fileGenotypes[snpIndex];
vector<int> thisSnpGenotypesInt;
for(size_t sampleIndex=0; sampleIndex < thisSnpGenotypes.size(); ++sampleIndex) {
string thisGenotypeString = thisSnpGenotypes[sampleIndex];
if(thisGenotypeString == "---") {
thisSnpGenotypesInt.push_back(MISSING_ATTRIBUTE_VALUE);
missingValues[subjectNames[sampleIndex]].push_back(snpIndex);
}
else {
// check to see if the genotype string is in the map
if(thisGenotypeStringMap.find(thisGenotypeString) ==
thisGenotypeStringMap.end()) {
cerr << "ERROR: " << snpNames[snpIndex]
<< ", genotype [" << thisGenotypeString
<< "] not found in lookup map" << endl;
exit(EXIT_FAILURE);
}
int thisGenotypeInt = thisGenotypeStringMap[thisGenotypeString];
thisSnpGenotypesInt.push_back(thisGenotypeInt);
}
}
snpGenotypes.push_back(thisSnpGenotypesInt);
double majorAlleleFreq = ((double) majorAlleleCount) /
(thisSnpGenotypesInt.size() * 2);
// cout << "MAF: " << majorAlleleFreq << endl;
snpMajorAlleleFreq.push_back(majorAlleleFreq);
if(snpIndex && (snpIndex % 100000 == 0)) {
cout << Timestamp() << snpIndex << endl;
}
}
cout << Timestamp() << subjectNames.size() << " subjects read and encoded" << endl;
cout << Timestamp() << snpGenotypes.size() << " SNPs read and encoded" << endl;
cout << Timestamp() << missingValues.size() << " subjects had missing value(s)" << endl;
if(monomorphs) {
cout << Timestamp() << monomorphs << " monomorphic SNPs detected"
<< endl;
}
/// read phenotypes
if(phenoFile != "") {
cout << Timestamp() << "Reading phenotypes from file: " << phenoFile << endl;
phenosFilename = phenoFile;
ifstream phenosStream(phenosFilename.c_str());
if (!phenosStream.is_open()) {
cerr << "ERROR: Could not open phenotypes file: " << phenosFilename << endl;
return false;
}
cout << Timestamp() << "Reading phenotypes from [" << phenosFilename << "]"
<< endl;
lineNumber = 0;
while (getline(phenosStream, line)) {
++lineNumber;
string trimmedLine = trim(line);
if (!trimmedLine.size()) {
cout << "WARNING: Blank line skipped at line number: "
<< lineNumber << endl;
continue;
}
if((trimmedLine != "0") && (trimmedLine != "1")) {
cerr << "ERROR: Phenotype is not 0 or 1 on line: " << lineNumber << endl;
return false;
}
int thisPhenotype = boost::lexical_cast<int>(trimmedLine);
phenotypes.push_back(thisPhenotype);
}
phenosStream.close();
if(phenotypes.size() != subjectNames.size()) {
cerr << "ERROR: Number of phenotypes read: " << phenotypes.size()
<< " is not equal to the number of sample names read from the"
<< " counts file: " << subjectNames.size() << endl;
return false;
}
hasPhenotypes = true;
}
else {
for(int i=0; i < (int) subjectNames.size(); ++i) {
phenotypes.push_back(MISSING_DISCRETE_CLASS_VALUE);
}
hasPhenotypes = false;
}
cout << Timestamp() << "Read " << subjectNames.size() << " samples with "
<< snpGenotypes.size() << " SNPs each" << endl;
return true;
}
vector<string> BirdseedData::GetSubjectNames() {
return subjectNames;
}
vector<string> BirdseedData::GetSubjectLabels() {
return subjectLabels;
}
bool BirdseedData::HasSubjectLabels() {
return hasSubjectLabels;
}
vector<string> BirdseedData::GetSNPNames() {
return snpNames;
}
int BirdseedData::GetNumSubjects() {
return subjectNames.size();
}
int BirdseedData::GetNumSNPs() {
return snpNames.size();
}
vector<int> BirdseedData::GetSubjectGenotypes(int subjectIndex) {
if((subjectIndex < 0) || (subjectIndex >= (int) subjectNames.size())) {
cerr << "ERROR: BirdseedData::GetSampleCounts, index out of range: "
<< subjectIndex << endl;
exit(EXIT_FAILURE);
}
vector<int> returnVector;
for(int i=0; i < (int) snpNames.size(); ++i) {
returnVector.push_back(snpGenotypes[i][subjectIndex]);
}
return returnVector;
}
vector<double> BirdseedData::GetSubjectCallConfidences(int subjectIndex) {
if((subjectIndex < 0) || (subjectIndex >= (int) subjectNames.size())) {
cerr << "ERROR: BirdseedData::GetSubjectCallConfidences, index out of range: "
<< subjectIndex << endl;
exit(EXIT_FAILURE);
}
vector<double> returnVector;
for(int i=0; i < (int) snpNames.size(); ++i) {
returnVector.push_back(confidences[i][subjectIndex]);
}
return returnVector;
}
int BirdseedData::GetSamplePhenotype(int subjectIndex) {
if((subjectIndex < 0) || (subjectIndex >= (int) subjectNames.size())) {
cerr << "ERROR: BirdseedData::GetSamplePhenotype, index out of range: "
<< subjectIndex << endl;
exit(EXIT_FAILURE);
}
return phenotypes[subjectIndex];
}
void BirdseedData::PrintInfo() {
cout << Timestamp() << "Birdseed Statistics:" << endl;
cout << Timestamp() << "Subjects: " << subjectNames.size( )<< endl;
cout << Timestamp() << "SNPs: " << snpNames.size( )<< endl;
if(hasPhenotypes) {
cout << Timestamp() << "Has Phenotypes" << endl;
}
else {
cout << Timestamp() << "Does Not Have Phenotypes" << endl;
}
if(hasSubjectLabels) {
cout << Timestamp() << "Using Subject Labels from Command-line File" << endl;
}
else {
cout << Timestamp() << "Using Subject Labels from Original Data File" << endl;
}
}
bool BirdseedData::HasPhenotypes() {
return hasPhenotypes;
}
pair<char, char> BirdseedData::GetMajorMinorAlleles(int snpIndex) {
pair<char, char> returnPair;
if((snpIndex >= 0) && snpIndex < (int) snpMajorMinorAlleles.size()) {
returnPair = snpMajorMinorAlleles[snpIndex];
}
else {
cerr << "ERROR: SNP index out of range" << snpIndex << endl;
}
return returnPair;
}
double BirdseedData::GetMajorAlleleFrequency(int snpIndex) {
double returnFreq = -1;
if((snpIndex >= 0) && snpIndex < (int) snpMajorAlleleFreq.size()) {
returnFreq = snpMajorAlleleFreq[snpIndex];
}
else {
cerr << "ERROR: SNP index out of range" << snpIndex << endl;
}
return returnFreq;
}
map<char, unsigned int> BirdseedData::GetAlleleCounts(int snpIndex) {
map<char, unsigned int> returnMap;
if((snpIndex >= 0) && snpIndex < (int) snpAlleleCounts.size()) {
returnMap = snpAlleleCounts[snpIndex];
}
else {
cerr << "ERROR: SNP index out of range" << snpIndex << endl;
}
return returnMap;
}
map<string, unsigned int> BirdseedData::GetGenotypeCounts(int snpIndex) {
map<string, unsigned int> returnMap;
if((snpIndex >= 0) && snpIndex < (int) genotypeCounts.size()) {
returnMap = genotypeCounts[snpIndex];
}
else {
cerr << "ERROR: SNP index out of range" << snpIndex << endl;
}
return returnMap;
}
bool BirdseedData::GetMissingValues(std::string subjectName,
std::vector<unsigned int>& missingValueIndices) {
if(missingValues.find(subjectName) != missingValues.end()) {
missingValueIndices = missingValues[subjectName];
}
return true;
}
void BirdseedData::PrintAlleleCounts() {
for(unsigned int i=0; i < genotypeCounts.size(); ++i) {
cout << "--------------------------------------------------" << endl;
map<string, unsigned int>::const_iterator it = genotypeCounts[i].begin();
for(; it != genotypeCounts[i].end(); ++it) {
cout << it->first << " " << it->second << endl;
}
}
}