]> git.donarmstrong.com Git - mothur.git/blob - raredisplay.cpp
adding treeclimber and unifrac pieces
[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                 rename(tempOutName.c_str(), tempInName.c_str());        
119         }
120         catch(exception& e) {
121                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124         catch(...) {
125                 cout << "An unknown error has occurred in the RareDisplay class function reset. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128 }
129
130 /***********************************************************************/
131
132 void RareDisplay::close(){
133         try {
134                 output->initFile(label);
135         
136                 openInputFile(tempInName, tempInFile);
137         
138                 while(!tempInFile.eof()){
139                         int nSeqs;
140                         tempInFile >> nSeqs;
141                 
142                         vector<double> data(3,0);
143                         double variance = 0;
144                 
145                         tempInFile >> data[0];
146                         tempInFile >> variance;
147                 
148                         double ci = 1.96 * pow(variance, 0.5);
149                         data[1] = data[0] - ci;
150                         data[2] = data[0] + ci;
151                 
152                         output->output(nSeqs, data);
153                 
154                         gobble(tempInFile);
155                 }
156                 tempInFile.close();
157         
158                 remove(tempInName.c_str());
159                 remove(tempOutName.c_str());
160         
161                 nIters = 1;
162                 output->resetFile();
163         }
164         catch(exception& e) {
165                 cout << "Standard Error: " << e.what() << " has occurred in the RareDisplay class Function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166                 exit(1);
167         }
168         catch(...) {
169                 cout << "An unknown error has occurred in the RareDisplay class function close. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172 }
173
174 /***********************************************************************/
175