1 /*
2  *  raredisplay.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 11/18/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9 
10 #include "raredisplay.h"
11 
12 /***********************************************************************/
13 
init(string label)14 void RareDisplay::init(string label){
15 	try {
16         lock_guard<std::mutex> guard(mutex);
17 		this->label = label;
18 	}
19 	catch(exception& e) {
20 		m->errorOut(e, "RareDisplay", "init");
21 		exit(1);
22 	}
23 }
24 
25 /***********************************************************************/
26 
update(SAbundVector & rank)27 void RareDisplay::update(SAbundVector& rank){
28 	try {
29         lock_guard<std::mutex> guard(mutex);
30 
31 		int newNSeqs = rank.getNumSeqs();
32 		vector<double> data = estimate->getValues(&rank);
33 
34 		map<int, vector<double> >::iterator it = results.find(newNSeqs);
35         if (it == results.end()) { //first iter for this count
36             vector<double> temp;
37             temp.push_back(data[0]);
38             results[newNSeqs] = temp;
39         }else {
40             it->second.push_back(data[0]);
41         }
42 	}
43 	catch(exception& e) {
44 		m->errorOut(e, "RareDisplay", "update");
45 		exit(1);
46 	}
47 }
48 
49 /***********************************************************************/
update(vector<SharedRAbundVector * > shared,int numSeqs)50 void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs) {
51 	try {
52         lock_guard<std::mutex> guard(mutex);
53 
54 		vector<double> data = estimate->getValues(shared);
55 
56 		map<int, vector<double> >::iterator it = results.find(numSeqs);
57         if (it == results.end()) { //first iter for this count
58             vector<double> temp;
59             temp.push_back(data[0]);
60             results[numSeqs] = temp;
61         }else {
62             it->second.push_back(data[0]);
63         }
64 	}
65 	catch(exception& e) {
66 		m->errorOut(e, "RareDisplay", "update");
67 		exit(1);
68 	}
69 }
70 
71 /***********************************************************************/
72 
reset()73 void RareDisplay::reset(){
74 	try {
75         lock_guard<std::mutex> guard(mutex);
76 		nIters++;
77 	}
78 	catch(exception& e) {
79 		m->errorOut(e, "RareDisplay", "reset");
80 		exit(1);
81 	}
82 }
83 
84 /***********************************************************************/
85 //assumes only one thread will run close
close()86 void RareDisplay::close(){
87 	try {
88 		output->setLabelName(label);
89 
90 		for (map<int, vector<double> >::iterator it = results.begin(); it != results.end(); it++) {
91 
92 			vector<double> data(3,0);
93 
94             sort((it->second).begin(), (it->second).end());
95 
96             vector<double> thisResults = it->second;
97             double meanResults = util.getAverage(thisResults);
98 			data[0] = meanResults;
99 			data[1] = (it->second)[(int)(0.025*(nIters-1))];
100 			data[2] = (it->second)[(int)(0.975*(nIters-1))];
101 
102 			output->updateOutput(it->first, data);
103 		}
104 
105 		nIters = 1;
106         results.clear();
107 
108 		output->resetFile();
109 	}
110 	catch(exception& e) {
111 		m->errorOut(e, "RareDisplay", "close");
112 		exit(1);
113 	}
114 }
115 /***********************************************************************/
116