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 //whereever there is a base in the mask, save that value is query and subject
18 for (int i = 0; i < seqMask.length(); i++) {
19 if (isalpha(seqMask[i])) {
26 errorOut(e, "DeCalculator", "setMask");
30 //***************************************************************************************************************
31 void DeCalculator::runMask(Sequence* seq) {
34 string q = seq->getAligned();
35 string tempQuery = "";
37 //whereever there is a base in the mask, save that value is query and subject
38 set<int>::iterator setit;
39 for ( setit=h.begin() ; setit != h.end(); setit++ ) {
40 tempQuery += q[*setit];
44 seq->setAligned(tempQuery);
45 seq->setUnaligned(tempQuery);
48 errorOut(e, "DeCalculator", "runMask");
52 //***************************************************************************************************************
53 //num is query's spot in querySeqs
54 void DeCalculator::trimSeqs(Sequence* query, Sequence* subject, map<int, int>& trim) {
57 string q = query->getAligned();
58 string s = subject->getAligned();
61 for (int i = 0; i < q.length(); i++) {
62 //cout << "query = " << q[i] << " subject = " << s[i] << endl;
63 if (isalpha(q[i]) && isalpha(s[i])) { front = i; break; }
65 //cout << endl << endl;
67 for (int i = q.length(); i >= 0; i--) {
68 //cout << "query = " << q[i] << " subject = " << s[i] << endl;
69 if (isalpha(q[i]) && isalpha(s[i])) { back = i; break; }
76 errorOut(e, "DeCalculator", "trimSeqs");
80 //***************************************************************************************************************
81 //find the window breaks for each sequence - this is so you can move ahead by bases.
82 vector<int> DeCalculator::findWindows(Sequence* query, int front, int back, int& size, int increment) {
87 int cutoff = back - front; //back - front
89 //if window is set to default
90 if (size == 0) { if (cutoff > 1200) { size = 300; }
91 else{ size = (cutoff / 4); } }
92 else if (size > (cutoff / 4)) {
93 mothurOut("You have selected to large a window size for sequence " + query->getName() + ". I will choose an appropriate window size."); mothurOutEndLine();
97 string seq = query->getAligned().substr(front, cutoff);
101 for (int l = 0; l < seq.length(); l++) { if (isalpha(seq[l])) { numBases++; } }
102 //cout << "num Bases = " << numBases << endl;
104 win.push_back(front);
105 //cout << front << '\t';
106 //move ahead increment bases at a time until all bases are in a window
108 int totalBases = 0; //used to eliminate window of blanks at end of sequence
110 seq = query->getAligned();
111 for (int m = front; m < (back - size) ; m++) {
113 //count number of bases you see
114 if (isalpha(seq[m])) { countBases++; }
116 //if you have seen enough bases to make a new window
117 if (countBases >= increment) {
118 //total bases is the number of bases in a window already.
119 totalBases += countBases;
120 //cout << "total bases = " << totalBases << endl;
121 win.push_back(m); //save spot in alignment
123 countBases = 0; //reset bases you've seen in this window
126 //no need to continue if all your bases are in a window
127 if (totalBases == numBases) { break; }
131 //get last window if needed
132 if (totalBases < numBases) { win.push_back(back-size); cout << back-size << endl;}
133 //cout << endl << endl;
138 catch(exception& e) {
139 errorOut(e, "DeCalculator", "findWindows");
144 //***************************************************************************************************************
145 vector<float> DeCalculator::calcObserved(Sequence* query, Sequence* subject, vector<int> window, int size) {
149 //cout << "query length = " << query->getAligned().length() << '\t' << " subject length = " << subject.getAligned().length() << endl;
150 for (int m = 0; m < window.size(); m++) {
152 string seqFrag = query->getAligned().substr(window[m], size);
153 string seqFragsub = subject->getAligned().substr(window[m], size);
154 //cout << "start point = " << window[m] << " end point = " << window[m]+size << endl;
156 for (int b = 0; b < seqFrag.length(); b++) {
158 if (seqFrag[b] != seqFragsub[b]) { diff++; }
161 //percentage of mismatched bases
163 dist = diff / (float) seqFrag.length() * 100;
165 temp.push_back(dist);
170 catch(exception& e) {
171 errorOut(e, "DeCalculator", "calcObserved");
175 //***************************************************************************************************************
176 float DeCalculator::calcDist(Sequence* query, Sequence* subject, int front, int back) {
179 //so you only look at the trimmed part of the sequence
180 int cutoff = back - front;
182 //from first startpoint with length back-front
183 string seqFrag = query->getAligned().substr(front, cutoff);
184 string seqFragsub = subject->getAligned().substr(front, cutoff);
187 for (int b = 0; b < seqFrag.length(); b++) {
188 if (seqFrag[b] != seqFragsub[b]) { diff++; }
191 //percentage of mismatched bases
192 float dist = diff / (float) seqFrag.length() * 100;
196 catch(exception& e) {
197 errorOut(e, "DeCalculator", "calcDist");
202 //***************************************************************************************************************
203 vector<float> DeCalculator::calcExpected(vector<float> qav, float coef) {
207 vector<float> queryExpected;
209 for (int m = 0; m < qav.size(); m++) {
211 float expected = qav[m] * coef;
213 queryExpected.push_back(expected);
216 return queryExpected;
219 catch(exception& e) {
220 errorOut(e, "DeCalculator", "calcExpected");
224 //***************************************************************************************************************
225 float DeCalculator::calcDE(vector<float> obs, vector<float> exp) {
229 float sum = 0.0; //sum = sum from 1 to m of (oi-ei)^2
230 for (int m = 0; m < obs.size(); m++) { sum += ((obs[m] - exp[m]) * (obs[m] - exp[m])); }
232 float de = sqrt((sum / (obs.size() - 1)));
236 catch(exception& e) {
237 errorOut(e, "DeCalculator", "calcDE");
242 //***************************************************************************************************************
244 vector<float> DeCalculator::calcFreq(vector<Sequence*> seqs, string filename) {
248 string freqfile = getRootName(filename) + "freq";
251 openOutputFile(freqfile, outFreq);
253 string length = toString(seqs.size()); //if there are 5000 seqs in the template then set precision to 3
254 int precision = length.length() - 1;
257 outFreq.setf(ios::fixed, ios::floatfield); outFreq.setf(ios::showpoint);
259 //at each position in the sequence
260 for (int i = 0; i < seqs[0]->getAligned().length(); i++) {
262 vector<int> freq; freq.resize(4,0);
265 //find the frequency of each nucleotide
266 for (int j = 0; j < seqs.size(); j++) {
268 char value = seqs[j]->getAligned()[i];
270 if(toupper(value) == 'A') { freq[0]++; }
271 else if(toupper(value) == 'T' || toupper(value) == 'U') { freq[1]++; }
272 else if(toupper(value) == 'G') { freq[2]++; }
273 else if(toupper(value) == 'C') { freq[3]++; }
277 //find base with highest frequency
279 for (int m = 0; m < freq.size(); m++) { if (freq[m] > highest) { highest = freq[m]; } }
281 float highFreq = highest / (float) (seqs.size());
284 Pi = (highFreq - 0.25) / 0.75;
286 //cannot have probability less than 0.
287 if (Pi < 0) { Pi = 0.0; }
289 //saves this for later
290 outFreq << setprecision(precision) << i << '\t' << highFreq << endl;
292 if (h.count(i) > 0) {
302 catch(exception& e) {
303 errorOut(e, "DeCalculator", "calcFreq");
307 //***************************************************************************************************************
308 vector<float> DeCalculator::findQav(vector<int> window, int size, vector<float> probabilityProfile) {
310 vector<float> averages;
312 //for each window find average
313 for (int m = 0; m < window.size(); m++) {
317 //while you are in the window for this sequence
319 for (int j = window[m]; j < (window[m]+size); j++) {
321 //is this a spot that is included in the mask
322 if (h.count(j) > 0) {
323 average += probabilityProfile[j];
328 average = average / count;
330 //save this windows average
331 averages.push_back(average);
336 catch(exception& e) {
337 errorOut(e, "DeCalculator", "findQav");
342 //***************************************************************************************************************
343 vector< vector<float> > DeCalculator::getQuantiles(vector<Sequence*> seqs, vector<int> windowSizesTemplate, int window, vector<float> probProfile, int increment, int start, int end) {
345 vector< vector<float> > quan;
347 //percentage of mismatched pairs 1 to 100
352 for(int i = start; i < end; i++){
354 mothurOut("Processing template sequence " + toString(i)); mothurOutEndLine();
355 Sequence* query = seqs[i];
357 //compare to every other sequence in template
358 for(int j = 0; j < i; j++){
360 Sequence* subject = seqs[j];
363 map<int, int>::iterator it;
365 trimSeqs(query, subject, trim);
368 int front = it->first; int back = it->second;
370 //reset window for each new comparison
371 windowSizesTemplate[i] = window;
373 vector<int> win = findWindows(query, front, back, windowSizesTemplate[i], increment);
375 vector<float> obsi = calcObserved(query, subject, win, windowSizesTemplate[i]);
377 vector<float> q = findQav(win, windowSizesTemplate[i], probProfile);
379 float alpha = getCoef(obsi, q);
381 vector<float> exp = calcExpected(q, alpha);
383 float de = calcDE(obsi, exp);
385 float dist = calcDist(query, subject, front, back);
389 //dist-1 because vector indexes start at 0.
390 quan[dist-1].push_back(de);
398 catch(exception& e) {
399 errorOut(e, "DeCalculator", "findQav");
403 //***************************************************************************************************************
404 void DeCalculator::removeObviousOutliers(vector< vector<float> >& quantiles) {
408 for (int i = 0; i < quantiles.size(); i++) {
410 //find mean of this quantile score
411 float average = findAverage(quantiles[i]);
413 vector<float> newQuanI;
414 //look at each value in quantiles to see if it is an outlier
415 for (int j = 0; j < quantiles[i].size(); j++) {
417 float highCutoff, lowCutOff;
426 catch(exception& e) {
427 errorOut(e, "DeCalculator", "removeObviousOutliers");
432 //***************************************************************************************************************
433 float DeCalculator::findAverage(vector<float> myVector) {
437 for (int i = 0; i < myVector.size(); i++) { total += myVector[i]; }
439 float average = total / (float) myVector.size();
444 catch(exception& e) {
445 errorOut(e, "DeCalculator", "findAverage");
450 //***************************************************************************************************************
451 float DeCalculator::getCoef(vector<float> obs, vector<float> qav) {
454 //find average prob for this seqs windows
455 float probAverage = 0.0;
456 for (int j = 0; j < qav.size(); j++) { probAverage += qav[j]; }
457 probAverage = probAverage / (float) qav.size();
459 //find observed average
460 float obsAverage = 0.0;
461 for (int j = 0; j < obs.size(); j++) { obsAverage += obs[j]; }
462 obsAverage = obsAverage / (float) obs.size();
463 //cout << "sum ai / m = " << probAverage << endl;
464 //cout << "sum oi / m = " << obsAverage << endl;
466 float coef = obsAverage / probAverage;
470 catch(exception& e) {
471 errorOut(e, "DeCalculator", "getCoef");
475 //***************************************************************************************************************