-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLargeLocusCodeAllocation.cpp
More file actions
385 lines (334 loc) · 9.86 KB
/
LargeLocusCodeAllocation.cpp
File metadata and controls
385 lines (334 loc) · 9.86 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
/*
* LargeLocusCodeAllocation.cpp
*
* Created on: 23.05.2011
* Author: Alexander Dilthey
*/
#include "LargeLocusCodeAllocation.h"
#include "MHC-PRG.h"
#include "Utilities.h"
#include <boost/algorithm/string.hpp>
#include <assert.h>
#include <set>
#include <algorithm>
#include <iostream>
string separatorForSerialization = "|||";
LargeLocusCodeAllocation::LargeLocusCodeAllocation() {
}
LargeLocusCodeAllocation::~LargeLocusCodeAllocation() {
}
std::string LargeLocusCodeAllocation::deCode(std::string locus, int value)
{
assert(coded_values_rev.count(locus) > 0);
if(coded_values_rev[locus].count(value) > 0)
{
return coded_values_rev[locus][value];
}
else
{
cerr << "Non-assigned allele for "+locus+ " value: "+Utilities::ItoStr((unsigned int)value);
assert(coded_values_rev[locus].count(value) > 0);
return 0;
}
}
int LargeLocusCodeAllocation::invert(string locus, int emission)
{
string nucleotide = deCode(locus, emission);
string invertedNucleotide;
if(nucleotide == "A")
{
invertedNucleotide = "T";
}
else if(nucleotide == "C")
{
invertedNucleotide = "G";
}
else if(nucleotide == "G")
{
invertedNucleotide = "C";
}
else if(nucleotide == "T")
{
invertedNucleotide = "A";
}
else
{
errEx("Cannot invert this nucleotide: "+nucleotide);
assert(1==0);
}
return doCode(locus, invertedNucleotide);
}
int LargeLocusCodeAllocation::doCode(std::string locus, std::string value)
{
if((value == "?") || (value == "????"))
{
return '0';
}
if(coded_values[locus].count(value) > 0)
{
return coded_values[locus][value];
}
else
{
int start = '0';
int elements_in = coded_values[locus].size();
int new_index = (int) start+elements_in+1;
coded_values[locus][value] = new_index;
coded_values_rev[locus][new_index] = value;
return new_index;
}
}
vector<string> LargeLocusCodeAllocation::getLoci()
{
vector<string> loci;
for(map<string, map<string, int> >::iterator StringCodes = coded_values.begin(); StringCodes != coded_values.end(); StringCodes++)
{
string locus = StringCodes->first;
loci.push_back(locus);
}
return loci;
}
bool LargeLocusCodeAllocation::knowCode(string locus, int code)
{
return (coded_values_rev[locus].count(code) > 0);
}
bool LargeLocusCodeAllocation::knowAllele(string locus, string allele)
{
return (coded_values[locus].count(allele) > 0);
}
vector<int> LargeLocusCodeAllocation::getAlleles(string locus)
{
vector<int> alleles;
for(map<int, string>::iterator CodeAllele = coded_values_rev[locus].begin(); CodeAllele != coded_values_rev[locus].end(); CodeAllele++)
{
int allele = CodeAllele->first;
assert(allele != '0');
alleles.push_back(allele);
}
return alleles;
}
/*
vector<LargeMutateAwayTo> LargeLocusCodeAllocation::mutateAway(string locus, int baseAllele, int mode, bool restrict2DHLAsingletons)
{
// *
// * The current behaviour of this function is best described as follows:
// * - assume that we are using it on a non-HLA locus. Then, the probability to observe the same allele as the baseAllele
// * is given by the mode setting, and if we observe missing data, it is split evenly among all possible (usually
// * SNP) alleles.
// * - now, think about HLA alleles. In general, the function does the same as for SNPs, except for one special
// * situation: mode == 3 (i.e. we are doing HLA type inference) && restrict2DHLAsingletons == true
// * (i.e. we only want, wherever possible, 4-digit alleles in the inference results). In this case, the missing
// * data probabilities are manipulated to exclude all unnecessary 2-digit alleles, and the probabilities are split
// * evenly for missing data (remember that loci that inference should be made for are marked as missing data).
// *
vector<int> locus_alleles = getAlleles(locus);
int num_of_alleles = locus_alleles.size();
bool locus_is_HLA = locusIsHLA(locus);
assert(baseAllele != '0');
double emission_same_allele = 0.0;
// todo check later if required
if(mode == 0)
{
emission_same_allele = 1 - CONFIG.mutation_build_graph;
assert(restrict2DHLAsingletons == false);
}
else if(mode == 1)
{
emission_same_allele = 1 - CONFIG.mutation_sample_graph;
assert(restrict2DHLAsingletons == false);
}
else if(mode == 2)
{
emission_same_allele = 1 - CONFIG.mutation_inference;
}
else
{
assert(string("Unknown mode for mutateAway") == string(""));
}
if(locus_is_HLA)
{
double mutation = 1 - emission_same_allele;
mutation = mutation / CONFIG.HLA_mutation_scaling;
emission_same_allele = 1 - mutation;
}
vector<LargeMutateAwayTo> forReturn;
if(! restrict2DHLAsingletons)
{
LargeMutateAwayTo missingData;
missingData.targetAllele = '0';
missingData.p = (double)1/(double)num_of_alleles;
forReturn.push_back(missingData);
}
else
{
if(locus_is_HLA)
{
set<int> locus_alleles_2Drestricted = getAlleles_2DrestrictOnSingletons(locus);
int num_of_alleles_2Drestricted = locus_alleles_2Drestricted.size();
if(locus_alleles_2Drestricted.count(baseAllele) > 0)
{
LargeMutateAwayTo missingData;
missingData.targetAllele = '0';
missingData.p = (double)1/(double)num_of_alleles_2Drestricted;
forReturn.push_back(missingData);
}
else
{
LargeMutateAwayTo missingData;
missingData.targetAllele = '0';
missingData.p = 0;
forReturn.push_back(missingData);
}
}
else
{
LargeMutateAwayTo missingData;
missingData.targetAllele = '0';
missingData.p = (double)1/(double)num_of_alleles;
forReturn.push_back(missingData);
}
}
for(unsigned int i = 0; i < locus_alleles.size(); i++)
{
int c = locus_alleles.at(i);
LargeMutateAwayTo mut;
mut.targetAllele = c;
if(c == baseAllele)
{
if(num_of_alleles == 1)
{
mut.p = 1;
}
else
{
mut.p = emission_same_allele;
}
}
else
{
mut.p = (1 - emission_same_allele) / ((double)num_of_alleles-1);
}
forReturn.push_back(mut);
}
return forReturn;
}
*/
bool LargeLocusCodeAllocation::locusIsHLA(string locus)
{
if(locus.length() < 3)
return false;
return ((locus.substr(0, 3) == "HLA") || (locus.substr(0, 3) == "KIR"));
}
bool LargeLocusCodeAllocation::allele4D(string locus, int allele)
{
assert(locusIsHLA(locus) == true);
string actualType = deCode(locus, allele);
assert(actualType.length() == 4);
return (actualType.substr(2,2) != "00");
}
string LargeLocusCodeAllocation::alleleString2D(string allele)
{
assert(allele.length() == 4);
return allele.substr(0,2)+"00";
}
vector<int> LargeLocusCodeAllocation::getAlleles4DOnly(string locus)
{
assert(locusIsHLA(locus) == true);
vector<int> all_alleles = getAlleles(locus);
vector<int> alleles_4D;
for(unsigned int i = 0; i < all_alleles.size(); i++)
{
if(allele4D(locus, all_alleles.at(i)))
{
alleles_4D.push_back(all_alleles.at(i));
}
}
return alleles_4D;
}
set<int> LargeLocusCodeAllocation::getAlleles_2DrestrictOnSingletons (string locus)
{
if(restrictedHLAcache.count(locus) > 0)
{
return restrictedHLAcache[locus];
}
else
{
assert(locusIsHLA(locus) == true);
set<string> alleles_2D_implied_by_4D;
vector<int> alleles_4D = getAlleles4DOnly(locus);
for(unsigned int i = 0; i < alleles_4D.size(); i++)
{
string allele_4D_string = deCode(locus, alleles_4D.at(i));
string allele_2D_string = alleleString2D(allele_4D_string);
alleles_2D_implied_by_4D.insert(allele_2D_string);
}
set<int> forReturn;
vector<int> alleles_all = getAlleles(locus);
for(unsigned int i = 0; i < alleles_all.size(); i++)
{
int allele = alleles_all.at(i);
if(allele4D(locus, allele))
{
forReturn.insert(allele);
}
else
{
string alleleString = deCode(locus, allele);
assert(alleleString.length() == 4);
if(alleles_2D_implied_by_4D.count(alleleString) == 0)
{
forReturn.insert(allele);
}
}
}
restrictedHLAcache[locus] = forReturn;
return forReturn;
}
}
vector<string> LargeLocusCodeAllocation::serializeIntoVector()
{
vector<string> forReturn;
for(map<string, map<string, int> >::iterator locusIt = coded_values.begin(); locusIt != coded_values.end(); locusIt++)
{
string locus = locusIt->first;
for(map<string, int>::iterator codeIt = coded_values[locus].begin(); codeIt != coded_values[locus].end(); codeIt++)
{
string originalAllele = codeIt->first;
int codedAllele = codeIt->second;
vector<string> lineVector;
lineVector.push_back(locus);
lineVector.push_back(originalAllele);
lineVector.push_back(Utilities::ItoStr((unsigned int)codedAllele));
forReturn.push_back(boost::algorithm::join(lineVector, separatorForSerialization));
}
}
return forReturn;
}
void LargeLocusCodeAllocation::readFromVector(vector<string> lines)
{
for(unsigned int i = 0; i < lines.size(); i++)
{
string line = lines.at(i);
vector<string> lineFields;
boost::iter_split(lineFields, line, boost::first_finder(separatorForSerialization, boost::is_iequal()));
if(lineFields.size() != 3)
{
errEx("Cannot read CODE from line, expect 3 fields, got" + Utilities::ItoStr(lineFields.size())+ "! Line: "+line);
}
string locus = lineFields.at(0);
string originalAllele = lineFields.at(1);
string integerString = lineFields.at(2);
int codedChar = Utilities::StrtoI(integerString);
if(codedChar < 0)
{
errEx("Weird codedChar value: cannot convert back! "+integerString);
}
int codedAllele = (int) codedChar;
coded_values[locus][originalAllele] = codedAllele;
coded_values_rev[locus][codedAllele] = originalAllele;
// if(locus == "L106379")
// {
// cout << locus << " -" << codedAllele << "-==" << integerString << " stands for " << originalAllele << "\n";
// }
}
}