File: calculator.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (484 lines) | stat: -rw-r--r-- 18,828 bytes parent folder | download | duplicates (2)
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
//
//  calculator.cpp
//  Mothur
//
//  Created by Sarah Westcott on 4/21/20.
//  Copyright © 2020 Schloss Lab. All rights reserved.
//

#include "calculator.h"

/***********************************************************************/
int DistCalc::setStart(string seqA, string seqB) {
    try {
        int start = 0;
        int alignLength = seqA.length();
        
        for(int i=0;i<alignLength;i++){
            if((seqA[i] != '.' || seqB[i] != '.')){ //one of you is not a terminal gap
                start = i;
                break;
            }
        }
        
        return start;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setStart");
        exit(1);
    }
}
/***********************************************************************/
int DistCalc::setEnd(string seqA, string seqB) {
    try {
        int end = 0;
        int alignLength = seqA.length();
        
        for(int i=alignLength-1;i>=0;i--){
            if((seqA[i] != '.' || seqB[i] != '.')){ //one of you is not a terminal gap
                end = i;
                break;
            }
        }
        return end;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setEnd");
        exit(1);
    }
}
/***********************************************************************/
// this assumes that sequences start and end with '.'s instead of'-'s.
int DistCalc::setStartIgnoreTermGap(string seqA, string seqB, bool& overlap) {
    try {
        
        int start = 0;
        int alignLength = seqA.length();
        
        for(int i=0;i<alignLength;i++){
            if(seqA[i] != '.' && seqB[i] != '.' && seqA[i] != '-' && seqB[i] != '-' ){ //skip leading gaps
                start = i;
                
                overlap = true;
                break;
            }
        }
        
        return start;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setStartIgnoreTermGap");
        exit(1);
    }
}
/***********************************************************************/
// this assumes that sequences start and end with '.'s instead of'-'s.
int DistCalc::setEndIgnoreTermGap(string seqA, string seqB, bool& overlap) {
    try {
        
        int end = 0;
        int alignLength = seqA.length();
        
        for(int i=alignLength-1;i>=0;i--){
            if(seqA[i] != '.' && seqB[i] != '.' && seqA[i] != '-' && seqB[i] != '-' ){ //ignore terminal gaps
                end = i;
                
                overlap = true;
                break;
            }
        }
        
        return end;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setEndIgnoreTermGap");
        exit(1);
    }
}
/***********************************************************************/

vector<int> DistCalc::setStartsIgnoreTermGap(classifierOTU seqA, classifierOTU otu, vector<int> cols){
    try {
        vector<int> starts; starts.resize(otu.numSeqs, -1);
        
        int alignLength = cols.size();
        
        int seqAStart = 0;
        for(int i=0;i<alignLength;i++){ //for each column we want to include
            if ((seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')){
                seqAStart = i; break;
            }
        }
        
        //set start positions
        int numset = 0;
        for(int i=seqAStart;i<alignLength;i++){ //start can't be before seqAStart because of the &&
            
            if(numset == otu.numSeqs) { break; }
            
            vector<char> thisColumn = otu.otuData[cols[i]];
            if (thisColumn.size() != otu.numSeqs) { //all seqs at this spot are identical
                
                char thisChar = thisColumn[0];
                
                if ((thisChar == '.') || (thisChar == '-')) { } //every seq in otu is a '.' or '-' at this location, move to next column
                else { //this is a base in all locations, you are done
                    for (int k = 0; k < starts.size(); k++) {
                        if ((starts[k] == -1) && (seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')) { starts[k] = i; numset++; } //any unset starts are set to this location
                    }
                    break;
                }
            }else{
                for(int j=0;j<otu.numSeqs;j++){ //for each reference
                    if((thisColumn[j] != '.') && (thisColumn[j] != '-') && (starts[j] == -1) && (seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')){ //seq j hasn't set the start value and its a base
                        starts[j] = i; numset++;
                    }
                }
            }
        }
        
        return starts;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setStartsIgnoreTermGap");
        exit(1);
    }
}
/***********************************************************************/

vector<int> DistCalc::setEndsIgnoreTermGap(classifierOTU seqA, classifierOTU otu, vector<int> cols){
    try {
        vector<int> ends; ends.resize(otu.numSeqs, -1);
        
        int alignLength = cols.size();
        
        int seqAEnd = 0;
        for(int i=alignLength-1;i>=0;i--){//for each column we want to include
            if ((seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')) {
                seqAEnd = i; break;
            }
        }
        
        //set start positions
        int numset = 0;
        for(int i=seqAEnd;i>=0;i--){ //for each column we want to include
            
            if(numset == otu.numSeqs) { break; }
            
            vector<char> thisColumn = otu.otuData[cols[i]];
            if (thisColumn.size() != otu.numSeqs) { //all seqs at this spot are identical
                
                char thisChar = thisColumn[0];
                
                if ((thisChar == '.') || (thisChar == '-')){ } //every seq in otu is a '.' at this location, move to next column
                else { //this is a base in all locations, you are done
                    for (int k = 0; k < ends.size(); k++) {
                        if ((ends[k] == -1) && (seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')){ ends[k] = i; numset++; } //any unset starts are set to this location
                    }
                    break;
                }
            }else{
                for(int j=0;j<otu.numSeqs;j++){ //for each reference
                    if((thisColumn[j] != '.') && (thisColumn[j] != '-') && (ends[j] == -1) && (seqA.otuData[cols[i]][0] != '.') && (seqA.otuData[cols[i]][0] != '-')) { //seq j hasn't set the start value and its a base
                        ends[j] = i; numset++;
                    }
                }
            }
        }
        
        return ends;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setEndsIgnoreTermGap");
        exit(1);
    }
}
/***********************************************************************/

vector<int> DistCalc::setStarts(classifierOTU seqA, classifierOTU otu, vector<int> cols){
    try {
        vector<int> starts; starts.resize(otu.numSeqs, 0);
        
        int alignLength = cols.size();
        
        int seqAStart = 0;
        for(int i=0;i<alignLength;i++){ //for each column we want to include
            if (seqA.otuData[cols[i]][0] != '.') {
                seqAStart = i; break;
            }
        }
        
        //set start positions
        int numset = 0;
        for(int i=0;i<alignLength;i++){ //for each column we want to include
            
            if (seqAStart <= i) { //our query seq starts before this point so set rest of unset starts to query start
                for (int k = 0; k < starts.size(); k++) {
                    if (starts[k] == 0) { starts[k] = seqAStart; numset++; }
                }
                break;
            }else if(numset == otu.numSeqs) { break; }
            
            vector<char> thisColumn = otu.otuData[cols[i]];
            if (thisColumn.size() != otu.numSeqs) { //all seqs at this spot are identical
                
                char thisChar = thisColumn[0];
                
                if (thisChar == '.') { } //every seq in otu is a '.' at this location, move to next column
                else { //this is a base in all locations, you are done
                    for (int k = 0; k < starts.size(); k++) {
                        if (starts[k] == 0) { starts[k] = i; numset++; } //any unset starts are set to this location
                    }
                    break;
                }
            }else{
                for(int j=0;j<otu.numSeqs;j++){ //for each reference
                    if((thisColumn[j] != '.') && (starts[j] == 0)){ //seq j hasn't set the start value and its a base
                        starts[j] = i; numset++;
                    }
                }
            }
        }
        
        return starts;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setStarts");
        exit(1);
    }
}
/***********************************************************************/

vector<int> DistCalc::setEnds(classifierOTU seqA, classifierOTU otu, vector<int> cols){
    try {
        vector<int> ends; ends.resize(otu.numSeqs, 0);
        
        int alignLength = cols.size();
        
        int seqAEnd = 0;
        for(int i=alignLength-1;i>=0;i--){//for each column we want to include
            if (seqA.otuData[cols[i]][0] != '.') {
                seqAEnd = i; break;
            }
        }
        
        //set start positions
        int numset = 0;
        for(int i=alignLength-1;i>=0;i--){ //for each column we want to include
            
            if (seqAEnd <= i) { //our query seq starts before this point so set rest of unset starts to query start
                for (int k = 0; k < ends.size(); k++) {
                    if (ends[k] == 0) { ends[k] = seqAEnd; numset++; }
                }
                break;
            }else if(numset == otu.numSeqs) { break; }
            
            vector<char> thisColumn = otu.otuData[cols[i]];
            if (thisColumn.size() != otu.numSeqs) { //all seqs at this spot are identical
                
                char thisChar = thisColumn[0];
                
                if (thisChar == '.') { } //every seq in otu is a '.' at this location, move to next column
                else { //this is a base in all locations, you are done
                    for (int k = 0; k < ends.size(); k++) {
                        if (ends[k] == 0) { ends[k] = i; numset++; } //any unset starts are set to this location
                    }
                    break;
                }
            }else{
                for(int j=0;j<otu.numSeqs;j++){ //for each reference
                    if((thisColumn[j] != '.') && (ends[j] == 0)){ //seq j hasn't set the start value and its a base
                        ends[j] = i; numset++;
                    }
                }
            }
        }
        
        return ends;
    }
    catch(exception& e) {
        m->errorOut(e, "DistCalc", "setEnds");
        exit(1);
    }
}

/***********************************************************************/
//nb1 and nb2 have size 1, unless amino acid = B or Z
void DistCalc::predict(vector<int> nb1, vector<int> nb2, double& p, double& dp, double& d2p, double& tt, double eigs[20], double probs[20][20]){
    try {
        double q;
        
        for (int i = 0; i < nb1.size(); i++) {
            
            for (int l = 0; l < 20; l++) {
              
                double elambdat = exp(tt * eigs[l]);
                
               // printf("l = %ld, nb1 = %ld, nb2 = %ld\n", l, nb1[i], nb2[i]);
               // printf("l = %ld, eig[m] = %f, prob[m][nb1 - 1] = %f, prob[m][nb2 - 1] = %f\n", l, eigs[l], probs[l][nb1[i] - 1], probs[l][nb2[i] - 1]);

                
                q = probs[l][nb1[i]-1] * probs[l][nb2[i]-1] * elambdat;
                p += q;
                
                dp += eigs[l] * q;
                
                double TEMP = eigs[l];
                
                d2p += TEMP * TEMP * q;
            }
        }
        
        //printf("p = %f, q = %f, tt = %f\n", p, q, tt);
       // printf("dp = %f, d2p = %f\n", dp, d2p);
    }
    catch(exception& e) {
        m->errorOut(e,  "DistCalc", "predict");
        exit(1);
    }
}
/***********************************************************************/
//nb1 and nb2 have size 1, unless amino acid = B or Z
void DistCalc::fillNums(vector<int>& numAs, vector<int>& numBs, int numA, int numB){
    try {
        
        if (numA == asx) { //B asn or asp (3 or 4)
          if (numB == asx) { //B asn or asp
              numAs.push_back(3); numBs.push_back(3); //asn, asn
              numAs.push_back(3); numBs.push_back(4); //asn, asp
              numAs.push_back(4); numBs.push_back(3); //asp, asn
              numAs.push_back(4); numBs.push_back(4); //asp, asp
          }else {
              if (numB == glx) { //Z gln or glu (6 or 7)
                  numAs.push_back(3); numBs.push_back(6); //asn, gln
                  numAs.push_back(3); numBs.push_back(7); //asn, glu
                  numAs.push_back(4); numBs.push_back(6); //asp, gln
                  numAs.push_back(4); numBs.push_back(7); //asp, glu
              }else {
                  if (numB < ser2) { numB++; }
                  numAs.push_back(3); numBs.push_back(numB); //asn, numB
                  numAs.push_back(4); numBs.push_back(numB); //asp, numB
              }
          }
        }else {
            if (numA == glx) { //Z gln or glu (6 or 7)
                if (numB == asx) { //B asn or asp
                    numAs.push_back(6); numBs.push_back(3); //gln, asn
                    numAs.push_back(6); numBs.push_back(4); //gln, asp
                    numAs.push_back(7); numBs.push_back(3); //glu, asn
                    numAs.push_back(7); numBs.push_back(4); //glu, asp
                }else {
                    if (numB == glx) { //Z gln or glu (6 or 7)
                        numAs.push_back(6); numBs.push_back(6); //gln, gln
                        numAs.push_back(6); numBs.push_back(7); //gln, glu
                        numAs.push_back(7); numBs.push_back(6); //glu, gln
                        numAs.push_back(7); numBs.push_back(7); //glu, glu
                    }else {
                        if (numB < ser2) { numB++; }
                        numAs.push_back(6); numBs.push_back(numB); //gln, numB
                        numAs.push_back(7); numBs.push_back(numB); //glu, numB
                    }
                }
            }else {
                if (numA < ser2) { numA++; }
                if (numB == asx) { //B asn or asp
                    numAs.push_back(numA); numBs.push_back(3); //numA, asn
                    numAs.push_back(numA); numBs.push_back(4); //numA, asp
                    numAs.push_back(numA); numBs.push_back(3); //numA, asn
                    numAs.push_back(numA); numBs.push_back(4); //numA, asp
                }else if (numB == glx) { //Z gln or glu (6 or 7)
                    numAs.push_back(numA); numBs.push_back(6); //numA, gln
                    numAs.push_back(numA); numBs.push_back(7); //numA, glu
                    numAs.push_back(numA); numBs.push_back(6); //numA, gln
                    numAs.push_back(numA); numBs.push_back(7); //numA, glu
                }
            }
        }
    }
    catch(exception& e) {
        m->errorOut(e,  "DistCalc", "fillNums");
        exit(1);
    }
}
/***********************************************************************/
double DistCalc::makeDists(Protein A, Protein B, double eigs[20], double probs[20][20]){
    try {
        int numBases = A.getAlignLength();
        vector<AminoAcid> seqA = A.getAligned();
        vector<AminoAcid> seqB = B.getAligned();
        
        bool inf = false; bool neginfinity = false; bool overlap = false;
        double delta, lnlike, slope, curv, tt;
        tt = 0.1; delta = tt / 2.0;
        
        for (int l = 0; l < 20; l++) {
            
            //reset for this attempt
            lnlike = 0.0; slope = 0.0; curv = 0.0; neginfinity = false; overlap = false;
            double oldweight = 1.0;
            
            if (m->getControl_pressed()) { break; }
            
            for (int i = 0; i < numBases; i++) {
                int numA = seqA[i].getNum();
                int numB = seqB[i].getNum();
                
                if (numA != stop && numA != del && numA != quest && numA != unk &&
                    numB != stop && numB != del && numB != quest && numB != unk) {
            
                    double p = 0.0; double dp = 0.0; double d2p = 0.0; overlap = true;
                    
                    vector<int> numAs; vector<int> numBs;
                    if (numA != asx && numA != glx && numB != asx && numB != glx) {
                        if (numA < ser2) { numA++; }
                        if (numB < ser2) { numB++; }
                        numAs.push_back(numA); numBs.push_back(numB); //+1 avoid 0
                    }else {
                        fillNums(numAs, numBs, numA, numB);
                    }
                    
                    predict(numAs, numBs, p, dp, d2p, tt, eigs, probs);
                    
                    if (p <= 0.0) {
                        neginfinity = true;
                    }else {
                        slope += oldweight*dp / p;
                        curv += oldweight*(d2p / p - dp * dp / (p * p));
                        
                        //printf("%ld:%ld, dp = %f, p = %f, d2p = %f\n", l, i, dp, p, d2p);

                        //printf("%ld:%ld, slope = %f, curv = %f, oldweight[i] = %ld\n", l, i, slope, curv, oldweight);
                    }
                }//endif stop
            }//endif bases
            
            if (!overlap) {
                tt = -1.0; l += 20; inf = true;
            }else if (!neginfinity) {
                if (curv < 0.0) {
                    tt -= slope / curv;
                    if (tt > 10000.0) { tt = -1.0; l += 20; inf = true;  }
                }else {
                    if ((slope > 0.0 && delta < 0.0) || (slope < 0.0 && delta > 0.0)) { delta /= -2; }
                    tt += delta;
                }
            }else {
                delta /= -2;
                tt += delta;
            }
            
            if (tt < 0.00001 && !inf) { tt = 0.00001; }
            
        }//endif attempts
        
        dist = tt;
        //exit(1);
        
        return dist;
    }
    catch(exception& e) {
        m->errorOut(e,  "DistCalc", "makeDists");
        exit(1);
    }
}
/***********************************************************************/