-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlinkDataset.cpp
423 lines (384 loc) · 14.2 KB
/
PlinkDataset.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
/*
* PlinkDataset.cpp - Bill White - 2/24/11
*
* Collection class holding DatasetInstance from Plink format files
* .map and .ped pairs
*/
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <time.h>
#include <boost/lexical_cast.hpp>
#include "StringUtils.h"
#include "PlinkDataset.h"
#include "Insilico.h"
using namespace std;
using namespace insilico;
using boost::lexical_cast;
PlinkDataset::PlinkDataset() : Dataset::Dataset() {
missingClassValuesToCheck.push_back("0");
missingClassValuesToCheck.push_back("-9");
}
bool PlinkDataset::LoadSnps(string filename) {
snpsFilename = filename;
cout << Timestamp() << "PlinkDataset loading" << endl;
filenameBase = GetFileBasename(snpsFilename);
cout << Timestamp() << "Plink filename prefix for map and ped files: "
<< filenameBase << endl;
// temporary string for reading file lines
string line;
/// read attribute information from the map file
string mapFilename = filenameBase + ".map";
ifstream mapDataStream(mapFilename.c_str());
if(!mapDataStream.is_open()) {
cerr << "ERROR: Could not open plink map file: " << mapFilename << endl;
return false;
}
cout << Timestamp() << "Reading plink map/attribute metadata from "
<< mapFilename << endl;
unsigned int mapLineNumber = 0;
MapFileType mapFileType = ERROR_FILE;
map<MapFileType, string> mapFileString;
mapFileString[ERROR_FILE] = "ERROR: Unindentified file type";
mapFileString[MAP3_FILE] = "MAP3 File Tye";
mapFileString[MAP4_FILE] = "MAP4 File Type";
unsigned int attrIdx = 0;
while(getline(mapDataStream, line)) {
++mapLineNumber;
string trimmedLine = trim(line);
if(trimmedLine[0] == '#') {
continue;
}
if(trimmedLine.size() == 0) {
continue;
}
vector<string> tokens;
split(tokens, trimmedLine);
if(tokens.size() == 3) {
mapFileType = MAP3_FILE;
classColumn = 2;
} else {
if(tokens.size() == 4) {
mapFileType = MAP4_FILE;
classColumn = 5;
} else {
cerr << "ERROR: reading plink map file line "
<< mapLineNumber << ". "
<< "Each row should have three (map3) or four "
<< "(standard map) whitespace-separated columns"
<< endl;
return false;
}
}
attributeNames.push_back(tokens[1]);
attributesMask[tokens[1]] = attrIdx;
++attrIdx;
}
mapDataStream.close();
cout << Timestamp() << mapFileString[mapFileType] << endl;
unsigned int numAttributes = attributeNames.size();
cout << Timestamp() << "Setting up attribute metadata structures for "
<< "[" << numAttributes << "] attributes" << endl;
vector<map<string, AttributeLevel> > attributeStringToInt;
attributeStringToInt.resize(numAttributes);
levelCounts.resize(numAttributes);
levelCountsByClass.resize(numAttributes);
attributeLevelsSeen.resize(numAttributes);
attributeAlleles.resize(numAttributes);
attributeAlleleCounts.resize(numAttributes);
attributeMinorAllele.resize(numAttributes);
genotypeCounts.resize(numAttributes);
attributeMutationTypes.resize(numAttributes);
/// Detect the class type
bool classDetected = false;
switch (DetectClassType(filename, classColumn+1, true)) {
case CASE_CONTROL_CLASS_TYPE:
cout << Timestamp() << "Case-control phenotypes detected" << endl;
hasContinuousPhenotypes = false;
classDetected = true;
break;
case CONTINUOUS_CLASS_TYPE:
cout << Timestamp() << "Continuous phenotypes detected" << endl;
hasContinuousPhenotypes = true;
classDetected = true;
break;
case MULTI_CLASS_TYPE:
cout << "ERROR: more than two discrete phenotypes detected" << endl;
break;
case NO_CLASS_TYPE:
cout << Timestamp() << "LoadSnps(): WARNING: phenotypes could not be "
"detected. All missing? " << endl;
cout << Timestamp() << "You can safely ignore this warning for "
"certain reliefseq functionality, e.g.: distance matrix calculations"
<< endl;
// might be calculating distance matrix, which doesn't need phenotypes
// so allow data set reader to continue despite missing class values
classDetected = true;
break;
}
if (!classDetected && !hasAlternatePhenotypes) {
return false;
}
vector<vector<string> > genotypeMatrix;
/// read attribute values from the ped file
string pedFilename = filenameBase + ".ped";
ifstream pedDataStream(pedFilename.c_str());
if(!pedDataStream.is_open()) {
cerr << "ERROR: Could not open plink ped file: " << pedFilename << endl;
return false;
}
cout << Timestamp() << "Reading plink attribute values from "
<< pedFilename << endl;
unsigned int pedLineNumber = 0;
unsigned int instanceIndex = 0;
// ValueType classType = NO_VALUE;
vector<pair<string, string> > alleles;
double minPheno = 0.0, maxPheno = 0.0;
while(getline(pedDataStream, line)) {
++pedLineNumber;
string trimmedLine = trim(line);
if(trimmedLine[0] == '#') {
continue;
}
if(trimmedLine.size() == 0) {
continue;
}
vector<string> pedColumnsParsed;
split(pedColumnsParsed, trimmedLine);
/// determine the MAP file type
if(mapFileType == MAP4_FILE) {
if(pedColumnsParsed.size() != numAttributes * 2 + 6) {
cerr << "ERROR: readling line " << pedLineNumber << " from the ped file"
<< endl
<< pedColumnsParsed.size() << " columns read, "
<< (numAttributes + 6) << " expected" << endl;
exit(1);
}
} else {
if(pedColumnsParsed.size() != numAttributes * 2 + 2) {
cerr << "ERROR: readling line " << pedLineNumber << " from the ped file"
<< endl
<< pedColumnsParsed.size() << " columns read, "
<< (numAttributes + 1) << " expected" << endl;
return false;
}
}
/// get ID for matching between PLINK data, numeric and pheno files
// use the PLINK IID as the instance ID - 9/18/12
// use both FID and IID so all PLINK files will work - 4/10/13
string ID = pedColumnsParsed[0] + pedColumnsParsed[1];
if(!IsLoadableInstanceID(ID)) {
cout << Timestamp() << "WARNING: Dataset instance ID [" << ID << "] skipped. "
<< "Not found in numerics and/or phenotype file(s)"
<< endl;
continue;
}
string thisClassString = pedColumnsParsed[classColumn];
/// assign class level
ClassLevel discreteClassLevel = MISSING_DISCRETE_CLASS_VALUE;
NumericLevel numericClassLevel = MISSING_NUMERIC_CLASS_VALUE;
if (hasContinuousPhenotypes) {
if (thisClassString != "-9") {
numericClassLevel = lexical_cast<NumericLevel>(thisClassString);
if (pedLineNumber == 1) {
minPheno = maxPheno = numericClassLevel;
} else {
if (numericClassLevel < minPheno) {
minPheno = numericClassLevel;
}
if (numericClassLevel > maxPheno) {
maxPheno = numericClassLevel;
}
}
} else {
if (!hasAlternatePhenotypes) {
cout << Timestamp() << "WARNING: Missing phenotype for "
<< "instance ID: " << ID << endl;
}
}
} else {
if (thisClassString != "-9") {
discreteClassLevel = lexical_cast<ClassLevel>(thisClassString) - 1;
} else {
if (!hasAlternatePhenotypes) {
cout << Timestamp() << "WARNING: Missing phenotype for "
<< "instance ID " << ID << endl;
}
}
}
DatasetInstance* newInst = new DatasetInstance(this);
if(hasContinuousPhenotypes) {
newInst->SetPredictedValueTau(numericClassLevel);
} else {
newInst->SetClass(discreteClassLevel);
classIndexes[discreteClassLevel].push_back(instanceIndex);
}
instances.push_back(newInst);
instanceIds.push_back(ID);
instancesMask[ID] = instanceIndex;
// the remaining columns in the line are gentoypes for the instance/subject
// as allele duets separated by spaces
vector<string> attributeDuets;
attributeDuets.resize(numAttributes * 2);
if(mapFileType == MAP4_FILE) {
copy(pedColumnsParsed.begin() + 6, pedColumnsParsed.end(),
attributeDuets.begin());
} else {
copy(pedColumnsParsed.begin() + 2, pedColumnsParsed.end(),
attributeDuets.begin());
}
// collapse the allelic encodings into genotypes
string littleBuff = " ";
unsigned int attrIdx = 0;
vector<string> attributesStringVector;
vector<string>::const_iterator it = attributeDuets.begin();
// cout << "Parsing duets..." << endl;
for(unsigned int i = 0; it != attributeDuets.end(); ++i, ++it) {
// cout << i << " => " << *it << endl;
if(i % 2 == 0) {
littleBuff[0] = (*it)[0];
} else {
littleBuff[1] = (*it)[0];
string uppercaseGenotype = to_upper(littleBuff);
if(uppercaseGenotype != "00") {
++attributeAlleleCounts[attrIdx][uppercaseGenotype[0]];
++attributeAlleleCounts[attrIdx][uppercaseGenotype[1]];
}
else {
missingValues[instanceIds[pedLineNumber-1]].push_back(attrIdx);
}
// cout << "\t" << attrIdx << ": " << littleBuff
// << " (" << uppercaseGenotype << ")" << endl;
++genotypeCounts[attrIdx][uppercaseGenotype];
attributesStringVector.push_back(uppercaseGenotype);
++attrIdx;
}
}
genotypeMatrix.push_back(attributesStringVector);
++instanceIndex;
// happy lights
if(instanceIndex && ((instanceIndex % 100) == 0)) {
cout << Timestamp() << instanceIndex << endl;
}
}
cout << Timestamp() << instanceIndex << " lines read" << endl;
pedDataStream.close();
// make genotype to integer map by determining the minor allele
// for each attribute
attributeStringToInt.resize(numAttributes);
for(attrIdx = 0; attrIdx < numAttributes; ++attrIdx) {
map<char, unsigned int> thisAttrMap = attributeAlleleCounts[attrIdx];
// if(thisAttrMap.size() != 2) {
// cerr << "ERROR: Only biallelic genotypes are supported" << endl;
// cerr << "ERROR: attribute: " << attrIdx << " "
// << attributeNames[attrIdx] << endl;
// return false;
// }
map<char, unsigned int>::const_iterator mapIt = thisAttrMap.begin();
char allele1 = mapIt->first;
unsigned int allele1Count = mapIt->second;
++mapIt;
char allele2 = '*';
unsigned int allele2Count = 0;
if(mapIt != thisAttrMap.end()) {
allele2 = mapIt->first;
allele2Count = mapIt->second;
}
string majorAllele = " ";
string minorAllele = " ";
double attributeMaf = 0.0;
if(allele1Count < allele2Count) {
minorAllele[0] = allele1;
attributeMaf = ((double) allele1Count) / (NumInstances() * 2.0);
majorAllele[0] = allele2;
}
else {
minorAllele[0] = allele2;
attributeMaf = ((double) allele2Count) / (NumInstances() * 2.0);
majorAllele[0] = allele1;
}
attributeAlleles[attrIdx] = make_pair(majorAllele[0], minorAllele[0]);
/// set the mutation type
attributeMutationTypes[attrIdx] =
attributeMutationMap[make_pair(minorAllele[0],
majorAllele[0])];
// cout << "Attribute: " << attrIdx
// << ", A1: " << minorAllele
// << ", A2: " << majorAllele
// << ", MAF: " << attributeMaf
// << endl;
attributeMinorAllele[attrIdx] = make_pair(minorAllele[0], attributeMaf);
/* From PLINK documentation:
* Allele codes:
* By default, the minor allele is coded A1 and the major allele is coded A2
*/
string genotype0 = majorAllele + majorAllele;
string genotype1 = minorAllele + majorAllele;
// all heterozygotes regardless of allele order are coded 1
string genotype11 = majorAllele + minorAllele;
string genotype2 = minorAllele + minorAllele;
// cout << "0: " << genotype0
// << " 1: " << genotype1
// << " 2: " << genotype2 << endl;
attributeStringToInt[attrIdx].insert(make_pair(genotype0, 0));
attributeStringToInt[attrIdx].insert(make_pair(genotype1, 1));
attributeStringToInt[attrIdx].insert(make_pair(genotype11, 1));
attributeStringToInt[attrIdx].insert(make_pair(genotype2, 2));
levelCounts[attrIdx][0] = 0;
levelCounts[attrIdx][1] = 0;
levelCounts[attrIdx][2] = 0;
}
// map all genotypes to integers to populate the data set
// PrintStringToIntMap();
for(instanceIndex = 0; instanceIndex < instances.size(); ++instanceIndex) {
for(unsigned int attributeIndex = 0;
attributeIndex < genotypeMatrix[instanceIndex].size();
++attributeIndex) {
string thisAttr = genotypeMatrix[instanceIndex][attributeIndex];
AttributeLevel thisAttrLevel;
if(thisAttr == "00") {
thisAttrLevel = MISSING_ATTRIBUTE_VALUE;
} else {
thisAttrLevel = attributeStringToInt[attributeIndex][thisAttr];
// cout << "(" << instanceIndex << "," << attributeIndex << ") -> "
// << thisAttr << " (" << thisAttrLevel << ")" << endl;
attributeLevelsSeen[attributeIndex].insert(thisAttr);
}
instances[instanceIndex]->attributes.push_back(thisAttrLevel);
}
}
cout << Timestamp() << "There are " << NumInstances()
<< " instances in the data set" << endl;
cout << Timestamp() << "There are " << instancesMask.size()
<< " instances in the instance mask" << endl;
if(hasContinuousPhenotypes) {
continuousPhenotypeMinMax = make_pair(minPheno, maxPheno);
cout << Timestamp() << "Continuous phenotypes." << endl;
} else {
cout << Timestamp() << "There are " << classIndexes.size()
<< " classes in the data set" << endl;
}
UpdateAllLevelCounts();
hasGenotypes = true;
hasAllelicInfo = true;
cout << Timestamp() << "Dataset read and transformed into integer encoding"
<< endl;
return true;
}
pair<char, double> PlinkDataset::GetAttributeMAF(unsigned int attributeIndex) {
pair<char, double> returnPair = make_pair(' ', 0.0);
if(attributeIndex < NumAttributes()) {
returnPair = attributeMinorAllele[attributeIndex];
}
return returnPair;
}
AttributeMutationType
PlinkDataset::GetAttributeMutationType(unsigned int attributeIndex) {
AttributeMutationType returnType = UNKNOWN_MUTATION;
if(attributeIndex < NumAttributes()) {
returnType = attributeMutationTypes[attributeIndex];
}
return returnType;
}