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