]> git.donarmstrong.com Git - mothur.git/blob - fileoutput.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[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                 
89                 remove(inName.c_str());
90                 renameOk = rename(outName.c_str(), inName.c_str());
91                 
92                 //checks to make sure user was able to rename and remove successfully
93                 if ((renameOk != 0)) { cout << "Unable to rename necessary files." << endl; }
94
95         }
96         catch(exception& e) {
97                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98                 exit(1);
99         }
100         catch(...) {
101                 cout << "An unknown error has occurred in the ThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }       
104 }
105
106 /***********************************************************************/
107 /***********************************************************************/
108
109 ThreeColumnFile2::~ThreeColumnFile2(){
110         
111         inFile.close();
112         outFile.close();
113         remove(outName.c_str());
114 };
115
116 /***********************************************************************/
117
118 void ThreeColumnFile2::initFile(string label, vector<string> tags){
119         try {
120                 if(counter != 0){
121                         openOutputFile(outName, outFile);
122                         openInputFile(inName, inFile);
123
124                         string inputBuffer;
125                         getline(inFile, inputBuffer);
126                 
127                         outFile <<  inputBuffer << '\t'; 
128                         for(int i = 1; i < tags.size(); i++) {
129                                 outFile << label + tags[i] << '\t';
130                         }
131                         outFile << endl;
132                 }
133                 else{
134                         openOutputFile(outName, outFile);
135                         for(int i = 0; i < tags.size(); i++) {
136                                 outFile << label + tags[i] << '\t';
137                         }
138                         outFile << endl;
139
140                 }
141
142                 outFile.setf(ios::fixed, ios::floatfield);
143                 outFile.setf(ios::showpoint);
144         }
145         catch(exception& e) {
146                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147                 exit(1);
148         }
149         catch(...) {
150                 cout << "An unknown error has occurred in the ThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151                 exit(1);
152         }
153 }
154
155 /***********************************************************************/
156
157 void ThreeColumnFile2::output(vector<double> data){
158         try {
159         
160                 if(counter != 0){               
161                         string inputBuffer;
162                         getline(inFile, inputBuffer);
163
164                         outFile << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
165                 }
166                 else{
167                         outFile << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
168
169                 }
170
171         }
172         catch(exception& e) {
173                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile2 class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174                 exit(1);
175         }
176         catch(...) {
177                 cout << "An unknown error has occurred in the ThreeColumnFile2 class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178                 exit(1);
179         }
180 };
181
182 /***********************************************************************/
183
184 void ThreeColumnFile2::resetFile(){
185         try {
186                 if(counter != 0){
187                         outFile.close();
188                         inFile.close();
189                 }
190                 else{
191                         outFile.close();
192                 }
193                 counter = 1;
194                 
195                 remove(inName.c_str());
196                 renameOk = rename(outName.c_str(), inName.c_str());
197                 
198                 //checks to make sure user was able to rename and remove successfully
199                 if ((renameOk != 0)) { cout << "Unable to rename necessary files." << endl; }
200
201         }
202         catch(exception& e) {
203                 cout << "Standard Error: " << e.what() << " has occurred in the ThreeColumnFile2 class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206         catch(...) {
207                 cout << "An unknown error has occurred in the ThreeColumnFile2 class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }       
210 }
211
212 /***********************************************************************/
213 /***********************************************************************/
214
215 SharedThreeColumnFile::~SharedThreeColumnFile(){
216         
217         inFile.close();
218         outFile.close();
219         remove(outName.c_str());
220 };
221
222 /***********************************************************************/
223
224 void SharedThreeColumnFile::initFile(string label){
225         try {
226                 if(counter != 0){
227                         openOutputFile(outName, outFile);
228                         openInputFile(inName, inFile);
229
230                         string inputBuffer;
231                         getline(inFile, inputBuffer);
232                 
233                         outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
234                 }
235                 else{
236                         openOutputFile(outName, outFile);
237                         outFile << "numsampled\t" << groupLabel << '\t' << label << "\tlci\thci" << endl;
238                 }
239
240                 outFile.setf(ios::fixed, ios::floatfield);
241                 outFile.setf(ios::showpoint);
242         }
243         catch(exception& e) {
244                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
245                 exit(1);
246         }
247         catch(...) {
248                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249                 exit(1);
250         }
251 }
252
253 /***********************************************************************/
254
255 void SharedThreeColumnFile::output(int nSeqs, vector<double> data){
256         try {
257                 if(counter != 0){               
258                         string inputBuffer;
259                         getline(inFile, inputBuffer);
260                 
261                         outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
262                 }
263                 else{
264                         outFile << numGroup << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
265                         numGroup++;
266                 }
267         }
268         catch(exception& e) {
269                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }
272         catch(...) {
273                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274                 exit(1);
275         }
276 };
277
278 /***********************************************************************/
279
280 void SharedThreeColumnFile::resetFile(){
281         try {
282                 if(counter != 0){
283                         outFile.close();
284                         inFile.close();
285                 }
286                 else{
287                         outFile.close();
288                 }
289                 counter = 1;
290                 remove(inName.c_str());
291                 renameOk = rename(outName.c_str(), inName.c_str());
292                 
293                 //checks to make sure user was able to rename and remove successfully
294                 if ((renameOk != 0)) { cout << "Unable to rename necessary files." << endl; }
295
296         }
297         catch(exception& e) {
298                 cout << "Standard Error: " << e.what() << " has occurred in the SharedThreeColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299                 exit(1);
300         }
301         catch(...) {
302                 cout << "An unknown error has occurred in the SharedThreeColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303                 exit(1);
304         }       
305 }
306
307 /***********************************************************************/
308
309 /***********************************************************************/
310
311 OneColumnFile::~OneColumnFile(){
312         
313         inFile.close();
314         outFile.close();
315         remove(outName.c_str());        
316 };
317
318 /***********************************************************************/
319
320 void OneColumnFile::initFile(string label){
321         try {
322                 if(counter != 0){
323                         openOutputFile(outName, outFile);
324                         openInputFile(inName, inFile);
325                 
326                         string inputBuffer;
327                         getline(inFile, inputBuffer);
328                 
329                         outFile <<  inputBuffer << '\t' << label << endl;
330                 }
331                 else{
332                         openOutputFile(outName, outFile);
333                         outFile << "numsequences\t" << label << endl;
334                 }
335         
336                 outFile.setf(ios::fixed, ios::floatfield);
337                 outFile.setf(ios::showpoint);
338         }
339         catch(exception& e) {
340                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
341                 exit(1);
342         }
343         catch(...) {
344                 cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
345                 exit(1);
346         }       
347 }
348
349 /***********************************************************************/
350
351 void OneColumnFile::output(int nSeqs, vector<double> data){
352         try {   
353                 if(counter != 0){               
354                         string inputBuffer;
355                         getline(inFile, inputBuffer);
356                 
357                         outFile <<  inputBuffer << setprecision(4) << '\t'  << data[0] << endl;
358                 }
359                 else{   
360                         outFile << nSeqs << setprecision(4) << '\t' << data[0] << endl;
361                 }
362         }
363         catch(exception& e) {
364                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
365                 exit(1);
366         }
367         catch(...) {
368                 cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369                 exit(1);
370         }       
371 };
372
373 /***********************************************************************/
374
375 void OneColumnFile::resetFile(){
376         try {
377                 if(counter != 0){
378                         outFile.close();
379                         inFile.close();
380                 }
381                 else{
382                         outFile.close();
383                 }       
384                 counter = 1;
385                 remove(inName.c_str());
386                 renameOk = rename(outName.c_str(), inName.c_str());
387                 
388                 //checks to make sure user was able to rename and remove successfully
389                 if ((renameOk != 0)) { cout << "Unable to rename necessary files." << endl; }
390
391         }
392         catch(exception& e) {
393                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
394                 exit(1);
395         }
396         catch(...) {
397                 cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
398                 exit(1);
399         }       
400 }
401
402 /***********************************************************************/
403 /***********************************************************************/
404
405 SharedOneColumnFile::~SharedOneColumnFile(){
406         
407         inFile.close();
408         outFile.close();
409         remove(outName.c_str());        
410 };
411
412 /***********************************************************************/
413
414 void SharedOneColumnFile::initFile(string label){
415         try {
416                 if(counter != 0){
417                         openOutputFile(outName, outFile);
418                         openInputFile(inName, inFile);
419                 
420                         string inputBuffer;
421                         getline(inFile, inputBuffer);
422                 
423                         outFile <<  inputBuffer << '\t' << label  << endl;
424
425                 }
426                 else{
427                         openOutputFile(outName, outFile);
428                         outFile << "sampled\t" << label << endl;
429                 
430                 }
431         
432                 outFile.setf(ios::fixed, ios::floatfield);
433                 outFile.setf(ios::showpoint);
434         }
435         catch(exception& e) {
436                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
437                 exit(1);
438         }
439         catch(...) {
440                 cout << "An unknown error has occurred in the OneColumnFile class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
441                 exit(1);
442         }       
443 }
444
445 /***********************************************************************/
446
447 void SharedOneColumnFile::output(int nSeqs, vector<double> data){
448         try {   
449                         string dataOutput;
450                         float sam;
451                         sam = data[0];
452                         dataOutput = "";
453                         for (int i = 0; i < data.size(); i++) {
454                                 dataOutput = dataOutput + "\t" + toString(data[i]);
455                         }
456                         if(counter != 0){               
457                                 string inputBuffer;
458                                 getline(inFile, inputBuffer);
459
460                                 outFile <<  inputBuffer << setprecision(2) << '\t' << dataOutput << endl;
461                         }
462                         else{   
463                                 outFile << nSeqs << setprecision(2) << '\t' << dataOutput << endl;
464                         }
465         }
466         catch(exception& e) {
467                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
468                 exit(1);
469         }
470         catch(...) {
471                 cout << "An unknown error has occurred in the OneColumnFile class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
472                 exit(1);
473         }       
474 };
475
476 /***********************************************************************/
477
478 void SharedOneColumnFile::resetFile(){
479         try {
480                 if(counter != 0){
481                         outFile.close();
482                         inFile.close();
483                 }
484                 else{
485                         outFile.close();
486                 }       
487                 counter = 1;
488                 
489                 remove(inName.c_str());
490                 renameOk = rename(outName.c_str(), inName.c_str());
491                 
492                 //checks to make sure user was able to rename and remove successfully
493                 if ((renameOk != 0)) { cout << "Unable to rename necessary files." << endl; }
494
495                 
496         }
497         catch(exception& e) {
498                 cout << "Standard Error: " << e.what() << " has occurred in the OneColumnFile class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
499                 exit(1);
500         }
501         catch(...) {
502                 cout << "An unknown error has occurred in the OneColumnFile class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
503                 exit(1);
504         }       
505 }
506
507 /***********************************************************************/