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