]> git.donarmstrong.com Git - mothur.git/blob - raredisplay.cpp
changes while testing
[mothur.git] / raredisplay.cpp
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
14 void RareDisplay::init(string label){
15         try {
16                 this->label = label;
17         }
18         catch(exception& e) {
19                 m->errorOut(e, "RareDisplay", "init");
20                 exit(1);
21         }
22 }
23
24 /***********************************************************************/
25
26 void RareDisplay::update(SAbundVector* rank){
27         try {
28                 int newNSeqs = rank->getNumSeqs();
29                 vector<double> data = estimate->getValues(rank);
30
31                 map<int, vector<double> >::iterator it = results.find(newNSeqs);
32         if (it == results.end()) { //first iter for this count
33             vector<double> temp;
34             temp.push_back(data[0]);
35             results[newNSeqs] = temp;
36         }else {
37             it->second.push_back(data[0]);
38         }
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "RareDisplay", "update");
42                 exit(1);
43         }
44 }
45
46 /***********************************************************************/
47 void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs, int numGroupComb) {
48         try {
49                 vector<double> data = estimate->getValues(shared); 
50                 
51                 map<int, vector<double> >::iterator it = results.find(numSeqs);
52         if (it == results.end()) { //first iter for this count
53             vector<double> temp;
54             temp.push_back(data[0]);
55             results[numSeqs] = temp;
56         }else {
57             it->second.push_back(data[0]);
58         }
59         }
60         catch(exception& e) {
61                 m->errorOut(e, "RareDisplay", "update");
62                 exit(1);
63         }
64 }
65
66 /***********************************************************************/
67
68 void RareDisplay::reset(){
69         try {
70                 nIters++;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "RareDisplay", "reset");
74                 exit(1);
75         }
76 }
77
78 /***********************************************************************/
79
80 void RareDisplay::close(){
81         try {
82                 output->initFile(label);
83         
84                 for (map<int, vector<double> >::iterator it = results.begin(); it != results.end(); it++) {
85                 
86                         vector<double> data(3,0);
87             
88             sort((it->second).begin(), (it->second).end());
89             
90             //lci=results[int(0.025*iter)] and hci=results[int(0.975*iter)]
91                         data[0] = (it->second)[(int)(0.50*(nIters-1))];
92             //data[0] = m->getAverage(it->second);
93                         data[1] = (it->second)[(int)(0.025*(nIters-1))];
94                         data[2] = (it->second)[(int)(0.975*(nIters-1))];
95             //cout << nIters << '\t' << (int)(0.025*(nIters-1)) << '\t' << (int)(0.975*(nIters-1)) << endl;
96             
97             //cout << it->first << '\t' << data[1] << '\t' << data[2] << endl;
98                 
99                         output->output(it->first, data);
100                 }
101                 
102                 nIters = 1;
103         results.clear();
104                 
105                 output->resetFile();
106         }
107         catch(exception& e) {
108                 m->errorOut(e, "RareDisplay", "close");
109                 exit(1);
110         }
111 }
112 /***********************************************************************/
113
114 void RareDisplay::inputTempFiles(string filename){
115         try {
116                 ifstream in;
117                 m->openInputFile(filename, in);
118                 
119                 int thisIters, size;
120                 in >> thisIters >> size; m->gobble(in);
121         nIters += thisIters;
122                 
123                 for (int i = 0; i < size; i++) {
124                         int tempCount;
125             in >> tempCount; m->gobble(in);
126             map<int, vector<double> >::iterator it = results.find(tempCount);
127             if (it != results.end()) {
128                 for (int j = 0; j < thisIters; j++) {
129                     double value;
130                     in >> value; m->gobble(in);
131                     (it->second).push_back(value);
132                 }
133             }else {
134                 vector<double> tempValues;
135                 for (int j = 0; j < thisIters; j++) {
136                     double value;
137                     in >> value; m->gobble(in);
138                     tempValues.push_back(value);
139                 }
140                 results[tempCount] = tempValues;
141             }
142                 }
143                 
144                 in.close();
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "RareDisplay", "inputTempFiles");
148                 exit(1);
149         }
150 }
151
152 /***********************************************************************/
153
154 void RareDisplay::outputTempFiles(string filename){
155         try {
156                 ofstream out;
157                 m->openOutputFile(filename, out);
158                 
159                 out << nIters-1 << '\t' << results.size() << endl;
160                 
161                 for (map<int, vector<double> >::iterator it = results.begin(); it != results.end(); it++) {
162             out << it->first << '\t';
163             for(int i = 0; i < (it->second).size(); i++) {
164                 out << (it->second)[i] << '\t';
165             }
166             out << endl;
167                 }
168                 
169                 out.close();
170         }
171         catch(exception& e) {
172                 m->errorOut(e, "RareDisplay", "outputTempFiles");
173                 exit(1);
174         }
175 }
176
177
178 /***********************************************************************/
179