5 * Created by Sarah Westcott on 7/22/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
12 //***************************************************************************************************************
13 void DeCalculator::setMask(string m) {
17 if (seqMask.length() != 0) {
18 //whereever there is a base in the mask, save that value is query and subject
19 for (int i = 0; i < seqMask.length(); i++) {
20 if (isalpha(seqMask[i])) {
25 for (int i = 0; i < alignLength; i++) { h.insert(i); }
29 errorOut(e, "DeCalculator", "setMask");
33 //***************************************************************************************************************
34 void DeCalculator::runMask(Sequence* seq) {
37 string q = seq->getAligned();
38 string tempQuery = "";
40 //whereever there is a base in the mask, save that value is query and subject
41 set<int>::iterator setit;
42 for ( setit=h.begin() ; setit != h.end(); setit++ ) {
43 tempQuery += q[*setit];
47 seq->setAligned(tempQuery);
48 seq->setUnaligned(tempQuery);
51 errorOut(e, "DeCalculator", "runMask");
55 //***************************************************************************************************************
56 //num is query's spot in querySeqs
57 void DeCalculator::trimSeqs(Sequence* query, Sequence* subject, map<int, int>& trim) {
60 string q = query->getAligned();
61 string s = subject->getAligned();
64 for (int i = 0; i < q.length(); i++) {
65 //cout << "query = " << q[i] << " subject = " << s[i] << endl;
66 if (isalpha(q[i]) && isalpha(s[i])) { front = i; break; }
68 //cout << endl << endl;
70 for (int i = q.length(); i >= 0; i--) {
71 //cout << "query = " << q[i] << " subject = " << s[i] << endl;
72 if (isalpha(q[i]) && isalpha(s[i])) { back = i; break; }
79 errorOut(e, "DeCalculator", "trimSeqs");
83 //***************************************************************************************************************
84 //find the window breaks for each sequence - this is so you can move ahead by bases.
85 vector<int> DeCalculator::findWindows(Sequence* query, int front, int back, int& size, int increment) {
90 int cutoff = back - front; //back - front
92 //if window is set to default
93 if (size == 0) { if (cutoff > 1200) { size = 300; }
94 else{ size = (cutoff / 4); } }
95 else if (size > (cutoff / 4)) {
96 mothurOut("You have selected to large a window size for sequence " + query->getName() + ". I will choose an appropriate window size."); mothurOutEndLine();
100 /* string seq = query->getAligned().substr(front, cutoff);
104 for (int l = 0; l < seq.length(); l++) { if (isalpha(seq[l])) { numBases++; } }
105 //cout << "num Bases = " << numBases << endl;
107 win.push_back(front);
108 //cout << front << '\t';
109 //move ahead increment bases at a time until all bases are in a window
111 int totalBases = 0; //used to eliminate window of blanks at end of sequence
113 seq = query->getAligned();
114 for (int m = front; m < (back - size) ; m++) {
116 //count number of bases you see
117 if (isalpha(seq[m])) { countBases++; }
119 //if you have seen enough bases to make a new window
120 if (countBases >= increment) {
121 //total bases is the number of bases in a window already.
122 totalBases += countBases;
123 //cout << "total bases = " << totalBases << endl;
124 win.push_back(m); //save spot in alignment
126 countBases = 0; //reset bases you've seen in this window
129 //no need to continue if all your bases are in a window
130 if (totalBases == numBases) { break; }
134 //get last window if needed
135 if (totalBases < numBases) { win.push_back(back-size); }
136 //cout << endl << endl;
139 //this follows wigeon, but we may want to consider that it chops off the end values if the sequence cannot be evenly divided into steps
140 for (int m = front; m < (back - size) ; m+=increment) { win.push_back(m); }
147 catch(exception& e) {
148 errorOut(e, "DeCalculator", "findWindows");
153 //***************************************************************************************************************
154 vector<float> DeCalculator::calcObserved(Sequence* query, Sequence* subject, vector<int> window, int size) {
158 //cout << "query length = " << query->getAligned().length() << '\t' << " subject length = " << subject.getAligned().length() << endl;
159 for (int m = 0; m < window.size(); m++) {
161 string seqFrag = query->getAligned().substr(window[m], size);
162 string seqFragsub = subject->getAligned().substr(window[m], size);
163 //cout << "start point = " << window[m] << " end point = " << window[m]+size << endl;
165 for (int b = 0; b < seqFrag.length(); b++) {
167 if (seqFrag[b] != seqFragsub[b]) { diff++; }
170 //percentage of mismatched bases
172 dist = diff / (float) seqFrag.length() * 100;
174 temp.push_back(dist);
179 catch(exception& e) {
180 errorOut(e, "DeCalculator", "calcObserved");
184 //***************************************************************************************************************
185 float DeCalculator::calcDist(Sequence* query, Sequence* subject, int front, int back) {
188 //so you only look at the trimmed part of the sequence
189 int cutoff = back - front;
192 //from first startpoint with length back-front
193 string seqFrag = query->getAligned().substr(front, cutoff);
194 string seqFragsub = subject->getAligned().substr(front, cutoff);
197 for (int b = 0; b < seqFrag.length(); b++) {
199 if((!isalpha(seqFrag[b])) && (!isalpha(seqFragsub[b]))) { gaps++; }
200 if (seqFrag[b] != seqFragsub[b]) { diff++; }
203 //percentage of mismatched bases
204 float dist = diff / (float) (seqFrag.length()-gaps) * 100;
208 catch(exception& e) {
209 errorOut(e, "DeCalculator", "calcDist");
214 //***************************************************************************************************************
215 vector<float> DeCalculator::calcExpected(vector<float> qav, float coef) {
219 vector<float> queryExpected;
221 for (int m = 0; m < qav.size(); m++) {
223 float expected = qav[m] * coef;
225 queryExpected.push_back(expected);
228 return queryExpected;
231 catch(exception& e) {
232 errorOut(e, "DeCalculator", "calcExpected");
236 //***************************************************************************************************************
237 float DeCalculator::calcDE(vector<float> obs, vector<float> exp) {
241 float sum = 0.0; //sum = sum from 1 to m of (oi-ei)^2
242 for (int m = 0; m < obs.size(); m++) { sum += ((obs[m] - exp[m]) * (obs[m] - exp[m])); }
244 float de = sqrt((sum / (obs.size() - 1)));
248 catch(exception& e) {
249 errorOut(e, "DeCalculator", "calcDE");
254 //***************************************************************************************************************
256 vector<float> DeCalculator::calcFreq(vector<Sequence*> seqs, string filename) {
260 string freqfile = getRootName(filename) + "freq";
263 openOutputFile(freqfile, outFreq);
265 string length = toString(seqs.size()); //if there are 5000 seqs in the template then set precision to 3
266 int precision = length.length() - 1;
269 outFreq.setf(ios::fixed, ios::floatfield); outFreq.setf(ios::showpoint);
271 //at each position in the sequence
272 for (int i = 0; i < seqs[0]->getAligned().length(); i++) {
274 vector<int> freq; freq.resize(4,0);
277 //find the frequency of each nucleotide
278 for (int j = 0; j < seqs.size(); j++) {
280 char value = seqs[j]->getAligned()[i];
282 if(toupper(value) == 'A') { freq[0]++; }
283 else if(toupper(value) == 'T' || toupper(value) == 'U') { freq[1]++; }
284 else if(toupper(value) == 'G') { freq[2]++; }
285 else if(toupper(value) == 'C') { freq[3]++; }
289 //find base with highest frequency
291 for (int m = 0; m < freq.size(); m++) { if (freq[m] > highest) { highest = freq[m]; } }
293 float highFreq = highest / (float) (seqs.size());
296 Pi = (highFreq - 0.25) / 0.75;
298 //cannot have probability less than 0.
299 if (Pi < 0) { Pi = 0.0; }
301 //saves this for later
302 outFreq << setprecision(precision) << i << '\t' << highFreq << endl;
304 if (h.count(i) > 0) {
314 catch(exception& e) {
315 errorOut(e, "DeCalculator", "calcFreq");
319 //***************************************************************************************************************
320 vector<float> DeCalculator::findQav(vector<int> window, int size, vector<float> probabilityProfile) {
322 vector<float> averages;
324 //for each window find average
325 for (int m = 0; m < window.size(); m++) {
329 //while you are in the window for this sequence
331 for (int j = window[m]; j < (window[m]+size); j++) {
332 average += probabilityProfile[j];
336 average = average / count;
338 //save this windows average
339 averages.push_back(average);
344 catch(exception& e) {
345 errorOut(e, "DeCalculator", "findQav");
350 //***************************************************************************************************************
351 vector< vector<float> > DeCalculator::getQuantiles(vector<Sequence*> seqs, vector<int> windowSizesTemplate, int window, vector<float> probProfile, int increment, int start, int end) {
353 vector< vector<float> > quan;
355 //percentage of mismatched pairs 1 to 100
360 for(int i = start; i < end; i++){
362 mothurOut("Processing template sequence " + toString(i)); mothurOutEndLine();
363 Sequence* query = seqs[i];
365 //compare to every other sequence in template
366 for(int j = 0; j < i; j++){
368 Sequence* subject = seqs[j];
371 map<int, int>::iterator it;
373 trimSeqs(query, subject, trim);
376 int front = it->first; int back = it->second;
378 //reset window for each new comparison
379 windowSizesTemplate[i] = window;
381 vector<int> win = findWindows(query, front, back, windowSizesTemplate[i], increment);
383 vector<float> obsi = calcObserved(query, subject, win, windowSizesTemplate[i]);
385 vector<float> q = findQav(win, windowSizesTemplate[i], probProfile);
387 float alpha = getCoef(obsi, q);
389 vector<float> exp = calcExpected(q, alpha);
391 float de = calcDE(obsi, exp);
393 float dist = calcDist(query, subject, front, back);
397 //dist-1 because vector indexes start at 0.
398 quan[dist-1].push_back(de);
406 catch(exception& e) {
407 errorOut(e, "DeCalculator", "findQav");
411 //***************************************************************************************************************
412 void DeCalculator::removeObviousOutliers(vector< vector<float> >& quantiles) {
416 for (int i = 0; i < quantiles.size(); i++) {
418 //find mean of this quantile score
419 sort(quantiles[i].begin(), quantiles[i].end());
421 float average = quantiles[i][int(quantiles[i].size() * 0.5)];
422 cout << i << "\taverage = " << average << "\tquantiles[i].size = " << quantiles[i].size() << endl;
423 vector<float> newQuanI;
424 //look at each value in quantiles to see if it is an outlier
425 for (int j = 0; j < quantiles[i].size(); j++) {
427 float highCutOff, lowCutOff;
430 highCutOff = sqrt(((quantiles[i][j] - average + 3) * (quantiles[i][j] - average + 3)) / (float)(quantiles[i].size() - 1));
433 lowCutOff = sqrt(((quantiles[i][j] - average - 3) * (quantiles[i][j] - average + 3)) / (float)(quantiles[i].size() - 1));
434 //cout << "high = " << highCutOff << " low = " << lowCutOff << " de = " << quantiles[i][j] << endl;
435 //if this is below the highcutff and above the lowcutoff
436 if ((quantiles[i][j] < highCutOff) && (quantiles[i][j] > lowCutOff)) {
438 newQuanI.push_back(quantiles[i][j]);
440 }else { cout << "removed outlier: high = " << highCutOff << " low = " << lowCutOff << " de = " << quantiles[i][j] << endl; }
443 quantiles[i] = newQuanI;
448 catch(exception& e) {
449 errorOut(e, "DeCalculator", "removeObviousOutliers");
454 //***************************************************************************************************************
455 float DeCalculator::findAverage(vector<float> myVector) {
459 for (int i = 0; i < myVector.size(); i++) { total += myVector[i]; }
461 float average = total / (float) myVector.size();
466 catch(exception& e) {
467 errorOut(e, "DeCalculator", "findAverage");
472 //***************************************************************************************************************
473 float DeCalculator::getCoef(vector<float> obs, vector<float> qav) {
476 //find average prob for this seqs windows
477 float probAverage = findAverage(qav);
479 //find observed average
480 float obsAverage = findAverage(obs);
482 float coef = obsAverage / probAverage;
486 catch(exception& e) {
487 errorOut(e, "DeCalculator", "getCoef");
491 //***************************************************************************************************************