]> git.donarmstrong.com Git - mothur.git/blob - fileoutput.cpp
Initial revision
[mothur.git] / fileoutput.cpp
1 /*
2  *  fileoutput.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 "fileoutput.h"
11
12 /***********************************************************************/
13
14 ThreeColumnFile::~ThreeColumnFile(){
15         
16         inFile.close();
17         outFile.close();
18         remove(outName.c_str());
19 };
20
21 /***********************************************************************/
22
23 void ThreeColumnFile::initFile(string label){
24         try {
25                 if(counter != 0){
26                         openOutputFile(outName, outFile);
27                         openInputFile(inName, inFile);
28
29                         string inputBuffer;
30                         getline(inFile, inputBuffer);
31                 
32                         outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
33                 }
34                 else{
35                         openOutputFile(outName, outFile);
36                         outFile << "numsampled\t" << label << "\tlci\thci" << endl;
37                 }
38
39                 outFile.setf(ios::fixed, ios::floatfield);
40                 outFile.setf(ios::showpoint);
41         }
42         catch(exception& e) {
43                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }
46         catch(...) {
47                 cout << "An unknown error has occurred in the ThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50 }
51
52 /***********************************************************************/
53
54 void ThreeColumnFile::output(int nSeqs, vector<double> data){
55         try {
56                 if(counter != 0){               
57                         string inputBuffer;
58                         getline(inFile, inputBuffer);
59                 
60                         outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
61                 }
62                 else{
63                         outFile << nSeqs << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
64                 }
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the ThreeColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74 };
75
76 /***********************************************************************/
77
78 void ThreeColumnFile::resetFile(){
79         try {
80                 if(counter != 0){
81                         outFile.close();
82                         inFile.close();
83                 }
84                 else{
85                         outFile.close();
86                 }
87                 counter = 1;
88                 remove(inName.c_str());
89                 rename(outName.c_str(), inName.c_str());
90         }
91         catch(exception& e) {
92                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
93                 exit(1);
94         }
95         catch(...) {
96                 cout << "An unknown error has occurred in the ThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97                 exit(1);
98         }       
99 }
100
101 /***********************************************************************/
102 /***********************************************************************/
103
104 SharedThreeColumnFile::~SharedThreeColumnFile(){
105         
106         inFile.close();
107         outFile.close();
108         remove(outName.c_str());
109 };
110
111 /***********************************************************************/
112
113 void SharedThreeColumnFile::initFile(string label){
114         try {
115                 if(counter != 0){
116                         openOutputFile(outName, outFile);
117                         openInputFile(inName, inFile);
118
119                         string inputBuffer;
120                         getline(inFile, inputBuffer);
121                 
122                         outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
123                 }
124                 else{
125                         openOutputFile(outName, outFile);
126                         outFile << "numsampled\t" << groupLabel << '\t' << label << "\tlci\thci" << endl;
127                 }
128
129                 outFile.setf(ios::fixed, ios::floatfield);
130                 outFile.setf(ios::showpoint);
131         }
132         catch(exception& e) {
133                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
134                 exit(1);
135         }
136         catch(...) {
137                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138                 exit(1);
139         }
140 }
141
142 /***********************************************************************/
143
144 void SharedThreeColumnFile::output(int nSeqs, vector<double> data){
145         try {
146                 if(counter != 0){               
147                         string inputBuffer;
148                         getline(inFile, inputBuffer);
149                 
150                         outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
151                 }
152                 else{
153                         outFile << numGroup << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
154                         numGroup++;
155                 }
156         }
157         catch(exception& e) {
158                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159                 exit(1);
160         }
161         catch(...) {
162                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163                 exit(1);
164         }
165 };
166
167 /***********************************************************************/
168
169 void SharedThreeColumnFile::resetFile(){
170         try {
171                 if(counter != 0){
172                         outFile.close();
173                         inFile.close();
174                 }
175                 else{
176                         outFile.close();
177                 }
178                 counter = 1;
179                 remove(inName.c_str());
180                 rename(outName.c_str(), inName.c_str());
181         }
182         catch(exception& e) {
183                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186         catch(...) {
187                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }       
190 }
191
192 /***********************************************************************/
193
194 /***********************************************************************/
195
196 OneColumnFile::~OneColumnFile(){
197         
198         inFile.close();
199         outFile.close();
200         remove(outName.c_str());        
201 };
202
203 /***********************************************************************/
204
205 void OneColumnFile::initFile(string label){
206         try {
207                 if(counter != 0){
208                         openOutputFile(outName, outFile);
209                         openInputFile(inName, inFile);
210                 
211                         string inputBuffer;
212                         getline(inFile, inputBuffer);
213                 
214                         outFile <<  inputBuffer << '\t' << label << endl;
215                 }
216                 else{
217                         openOutputFile(outName, outFile);
218                         outFile << "numsequences\t" << label << endl;
219                 }
220         
221                 outFile.setf(ios::fixed, ios::floatfield);
222                 outFile.setf(ios::showpoint);
223         }
224         catch(exception& e) {
225                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226                 exit(1);
227         }
228         catch(...) {
229                 cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230                 exit(1);
231         }       
232 }
233
234 /***********************************************************************/
235
236 void OneColumnFile::output(int nSeqs, vector<double> data){
237         try {   
238                 if(counter != 0){               
239                         string inputBuffer;
240                         getline(inFile, inputBuffer);
241                 
242                         outFile <<  inputBuffer << setprecision(4) << '\t'  << data[0] << endl;
243                 }
244                 else{   
245                         outFile << nSeqs << setprecision(4) << '\t' << data[0] << endl;
246                 }
247         }
248         catch(exception& e) {
249                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
250                 exit(1);
251         }
252         catch(...) {
253                 cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254                 exit(1);
255         }       
256 };
257
258 /***********************************************************************/
259
260 void OneColumnFile::resetFile(){
261         try {
262                 if(counter != 0){
263                         outFile.close();
264                         inFile.close();
265                 }
266                 else{
267                         outFile.close();
268                 }       
269                 counter = 1;
270                 remove(inName.c_str());
271                 rename(outName.c_str(), inName.c_str());
272         }
273         catch(exception& e) {
274                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275                 exit(1);
276         }
277         catch(...) {
278                 cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279                 exit(1);
280         }       
281 }
282
283 /***********************************************************************/
284 /***********************************************************************/
285
286 SharedOneColumnFile::~OneColumnFile(){
287         
288         inFile.close();
289         outFile.close();
290         remove(outName.c_str());        
291 };
292
293 /***********************************************************************/
294
295 void SharedOneColumnFile::initFile(string label){
296         try {
297                 if(counter != 0){
298                         openOutputFile(outName, outFile);
299                         openInputFile(inName, inFile);
300                 
301                         string inputBuffer;
302                         getline(inFile, inputBuffer);
303                 
304                         outFile <<  inputBuffer << '\t' << label  << endl;
305
306                 }
307                 else{
308                         openOutputFile(outName, outFile);
309                         outFile << "sampled\t" << label << endl;
310                 
311                 }
312         
313                 outFile.setf(ios::fixed, ios::floatfield);
314                 outFile.setf(ios::showpoint);
315         }
316         catch(exception& e) {
317                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
318                 exit(1);
319         }
320         catch(...) {
321                 cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
322                 exit(1);
323         }       
324 }
325
326 /***********************************************************************/
327
328 void SharedOneColumnFile::output(int nSeqs, vector<double> data){
329         try {   
330                         string dataOutput;
331                         float sam;
332                         sam = data[0];
333                         dataOutput = "";
334                         for (int i = 0; i < data.size(); i++) {
335                                 dataOutput = dataOutput + "\t" + toString(data[i]);
336                         }
337                         if(counter != 0){               
338                                 string inputBuffer;
339                                 getline(inFile, inputBuffer);
340
341                                 outFile <<  inputBuffer << setprecision(2) << '\t' << dataOutput << endl;
342                         }
343                         else{   
344                                 outFile << nSeqs << setprecision(2) << '\t' << dataOutput << endl;
345                         }
346         }
347         catch(exception& e) {
348                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
349                 exit(1);
350         }
351         catch(...) {
352                 cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353                 exit(1);
354         }       
355 };
356
357 /***********************************************************************/
358
359 void SharedOneColumnFile::resetFile(){
360         try {
361                 if(counter != 0){
362                         outFile.close();
363                         inFile.close();
364                 }
365                 else{
366                         outFile.close();
367                 }       
368                 counter = 1;
369                 remove(inName.c_str());
370                 rename(outName.c_str(), inName.c_str());
371         }
372         catch(exception& e) {
373                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374                 exit(1);
375         }
376         catch(...) {
377                 cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }       
380 }
381
382 /***********************************************************************/