]> git.donarmstrong.com Git - mothur.git/blob - raredisplay.cpp
fixed some bugs
[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                 if(nIters != 1){
18                         tempInFile.clear();
19                         openOutputFile(tempOutName, tempOutFile);
20                         openInputFile(tempInName, tempInFile);
21                 }
22                 else{
23                         openOutputFile(tempOutName, tempOutFile);
24                 }
25         }
26         catch(exception& e) {
27                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function init. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
28                 exit(1);
29         }
30         catch(...) {
31                 cout << "An unknown error has occurred in the RareDisplay class function init. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32                 exit(1);
33         }
34 }
35
36 /***********************************************************************/
37
38 void RareDisplay::update(SAbundVector* rank){
39         try {
40                 int newNSeqs = rank->getNumSeqs();
41                 vector<double> data = estimate->getValues(rank);
42
43                 if(nIters != 1){
44
45                         double oldNSeqs, oldMean, oldVar;
46                 
47                         tempInFile >> oldNSeqs >> oldMean >> oldVar;
48                 
49                         double oldS = oldVar * ( nIters - 2 );
50                         double delta = data[0] - oldMean;
51                         double newMean = oldMean + delta / nIters;
52                         double newS = oldS + delta * ( data[0] - newMean );
53                         double newVar = newS / ( nIters - 1 );
54
55                         tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
56                 }
57                 else{
58                         tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
59                 }
60         }
61         catch(exception& e) {
62                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63                 exit(1);
64         }
65         catch(...) {
66                 cout << "An unknown error has occurred in the RareDisplay class function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67                 exit(1);
68         }
69 }
70
71 /***********************************************************************/
72 void RareDisplay::update(vector<SharedRAbundVector*> shared, int numSeqs, int numGroupComb) {
73         try {
74                 vector<double> data = estimate->getValues(shared); 
75                 double newNSeqs = data[0];
76                 
77                 if(nIters != 1){
78                         double oldNSeqs, oldMean, oldVar;
79                 
80                         tempInFile >> oldNSeqs >> oldMean >> oldVar;
81                 
82                         double oldS = oldVar * ( nIters - 2 );
83                         double delta = data[0] - oldMean;
84                         double newMean = oldMean + delta / nIters;
85                         double newS = oldS + delta * ( data[0] - newMean );
86                         double newVar = newS / ( nIters - 1 );
87
88                         tempOutFile << newNSeqs << '\t' << newMean << '\t' << newVar << endl;
89                 }
90                 else{
91                         tempOutFile << newNSeqs << '\t' << data[0] << '\t' << 0 << endl;
92                 }
93         }
94         catch(exception& e) {
95                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
96                 exit(1);
97         }
98         catch(...) {
99                 cout << "An unknown error has occurred in the RareDisplay class function update. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100                 exit(1);
101         }
102 }
103
104 /***********************************************************************/
105
106 void RareDisplay::reset(){
107         try {
108                 if(nIters != 1){
109                         tempOutFile.close();
110                         tempInFile.close();
111                 }
112                 else{
113                         tempOutFile.close();
114                 }
115         
116                 nIters++;
117         
118                 remove(tempInName.c_str());
119                 renameOk = rename(tempOutName.c_str(), tempInName.c_str());     
120                 
121                 //checks to make sure user was able to rename and remove successfully
122                 if (renameOk != 0) { cout << "Unable to rename the necessary temp files." << endl; }
123         }
124         catch(exception& e) {
125                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128         catch(...) {
129                 cout << "An unknown error has occurred in the RareDisplay class function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130                 exit(1);
131         }
132 }
133
134 /***********************************************************************/
135
136 void RareDisplay::close(){
137         try {
138                 
139                 output->initFile(label);
140         
141                 openInputFile(tempInName, tempInFile);
142         
143                 while(!tempInFile.eof()){
144                         int nSeqs;
145                         tempInFile >> nSeqs;
146                 
147                         vector<double> data(3,0);
148                         double variance = 0;
149                 
150                         tempInFile >> data[0];
151                         tempInFile >> variance;
152                 
153                         double ci = 1.96 * pow(variance, 0.5);
154                         data[1] = data[0] - ci;
155                         data[2] = data[0] + ci;
156                 
157                         output->output(nSeqs, data);
158                 
159                         gobble(tempInFile);
160                 }
161                 tempInFile.close();
162         
163                 remove(tempInName.c_str());
164                 remove(tempOutName.c_str());
165                 
166                 nIters = 1;
167                 output->resetFile();
168         }
169         catch(exception& e) {
170                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171                 exit(1);
172         }
173         catch(...) {
174                 cout << "An unknown error has occurred in the RareDisplay class function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
175                 exit(1);
176         }
177 }
178
179 /***********************************************************************/
180