]> git.donarmstrong.com Git - mothur.git/blob - fileoutput.cpp
updated kruskalwallis struct
[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         m->mothurRemove(outName);
19 }
20
21 /***********************************************************************/
22
23 void ThreeColumnFile::initFile(string label){
24         try {
25                 if(counter != 0){
26                         m->openOutputFile(outName, outFile);
27                         m->openInputFile(inName, inFile);
28
29                         string inputBuffer;
30                         inputBuffer = m->getline(inFile);
31                 
32                         outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
33                 }
34                 else{
35                         m->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                 m->errorOut(e, "ThreeColumnFile", "initFile");
44                 exit(1);
45         }
46 }
47
48 /***********************************************************************/
49
50 void ThreeColumnFile::output(int nSeqs, vector<double> data){
51         try {
52                 if(counter != 0){               
53                         string inputBuffer;
54                         inputBuffer = m->getline(inFile);
55                 
56                         outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
57                 }
58                 else{
59                         outFile << nSeqs << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
60                 }
61         }
62         catch(exception& e) {
63                 m->errorOut(e, "ThreeColumnFile", "output");
64                 exit(1);
65         }
66 }
67
68 /***********************************************************************/
69
70 void ThreeColumnFile::resetFile(){
71         try {
72                 if(counter != 0){
73                         outFile.close();
74                         inFile.close();
75                 }
76                 else{
77                         outFile.close();
78                 }
79                 counter = 1;
80                 
81                 m->mothurRemove(inName);
82                 renameOk = rename(outName.c_str(), inName.c_str());
83                 
84                 //renameFile(outName, inName);
85                 
86                 //checks to make sure user was able to rename and remove successfully
87                 if ((renameOk != 0)) { 
88                         m->mothurOut("Unable to rename " + outName); m->mothurOutEndLine();
89                         perror(" : ");
90                 }       
91         }
92         catch(exception& e) {
93                 m->errorOut(e, "ThreeColumnFile", "resetFile");
94                 exit(1);
95         }
96 }
97
98 /***********************************************************************/
99 /***********************************************************************/
100
101 ColumnFile::~ColumnFile(){
102         
103         inFile.close();
104         outFile.close();
105         m->mothurRemove(outName);
106 }
107
108 /***********************************************************************/
109
110 void ColumnFile::initFile(string label, vector<string> tags){
111         try {
112                 if(counter != 0){
113                         m->openOutputFile(outName, outFile);
114                         m->openInputFile(inName, inFile);
115
116                         string inputBuffer;
117                         inputBuffer = m->getline(inFile);
118                 
119                         outFile <<  inputBuffer << '\t'; 
120                         for(int i = 0; i < tags.size(); i++) {
121                                 outFile << label + tags[i] << '\t';
122                         }
123                         outFile << endl;
124                 }
125                 else{
126                         m->openOutputFile(outName, outFile);
127                         for(int i = 0; i < tags.size(); i++) {
128                                 outFile << label + tags[i] << '\t';
129                         }
130                         outFile << endl;
131                 }
132
133                 outFile.setf(ios::fixed, ios::floatfield);
134                 outFile.setf(ios::showpoint);
135         }
136         catch(exception& e) {
137                 m->errorOut(e, "ColumnFile", "initFile");
138                 exit(1);
139         }
140 }
141
142 /***********************************************************************/
143
144 void ColumnFile::output(vector<double> data){
145         try {
146         
147                 if(counter != 0){               
148                         string inputBuffer;
149                         inputBuffer = m->getline(inFile);
150
151                         outFile << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(iters.length());
152                         for (int i = 1; i< data.size(); i++) {
153                                 outFile << '\t' << data[i]; 
154                         }
155                         outFile << endl;
156                 }
157                 else{
158                         outFile << setprecision(6) << data[0] << setprecision(iters.length());
159                         for (int i = 1; i< data.size(); i++) {
160                                 outFile << '\t' << data[i]; 
161                         }
162                         outFile << endl;
163                 }
164
165         }
166         catch(exception& e) {
167                 m->errorOut(e, "ColumnFile", "output");
168                 exit(1);
169         }
170 }
171
172 /***********************************************************************/
173
174 void ColumnFile::resetFile(){
175         try {
176                 if(counter != 0){
177                         outFile.close();
178                         inFile.close();
179                 }
180                 else{
181                         outFile.close();
182                 }
183                 counter = 1;
184                 
185                 m->mothurRemove(inName);
186                 renameOk = rename(outName.c_str(), inName.c_str());
187                 
188                 //renameFile(outName, inName);
189                 
190                 //checks to make sure user was able to rename and remove successfully
191                 if ((renameOk != 0)) { 
192                         m->mothurOut("Unable to rename " + outName); m->mothurOutEndLine();
193                         perror(" : ");
194                 }       
195         }
196         catch(exception& e) {
197                 m->errorOut(e, "ColumnFile", "resetFile");
198                 exit(1);
199         }
200 }
201
202 /***********************************************************************/
203 /***********************************************************************/
204
205 SharedThreeColumnFile::~SharedThreeColumnFile(){
206         
207         inFile.close();
208         outFile.close();
209         m->mothurRemove(outName);
210 }
211
212 /***********************************************************************/
213
214 void SharedThreeColumnFile::initFile(string label){
215         try {
216                 if(counter != 0){
217                         m->openOutputFile(outName, outFile);
218                         m->openInputFile(inName, inFile);
219
220                         string inputBuffer;
221                         inputBuffer = m->getline(inFile);
222                 
223                         outFile <<  inputBuffer << '\t' << label << "\tlci\thci" << endl;
224                 }
225                 else{
226                         m->openOutputFile(outName, outFile);
227                         outFile << "numsampled\t" << groupLabel << '\t' << label << "\tlci\thci" << endl;
228                 }
229
230                 outFile.setf(ios::fixed, ios::floatfield);
231                 outFile.setf(ios::showpoint);
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "SharedThreeColumnFile", "initFile");
235                 exit(1);
236         }
237 }
238
239 /***********************************************************************/
240
241 void SharedThreeColumnFile::output(int nSeqs, vector<double> data){
242         try {
243                 if(counter != 0){               
244                         string inputBuffer;
245                         inputBuffer = m->getline(inFile);
246                 
247                         outFile <<  inputBuffer << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
248                 }
249                 else{
250                         outFile << numGroup << setprecision(4) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
251                         numGroup++;
252                 }
253         }
254         catch(exception& e) {
255                 m->errorOut(e, "SharedThreeColumnFile", "output");
256                 exit(1);
257         }
258 }
259
260 /***********************************************************************/
261
262 void SharedThreeColumnFile::resetFile(){
263         try {
264                 if(counter != 0){
265                         outFile.close();
266                         inFile.close();
267                 }
268                 else{
269                         outFile.close();
270                 }
271                 counter = 1;
272                 
273                 m->mothurRemove(inName);
274                 renameOk = rename(outName.c_str(), inName.c_str());
275                 
276                 //renameFile(outName, inName);
277                 
278                 //checks to make sure user was able to rename and remove successfully
279                 if ((renameOk != 0)) { 
280                         m->mothurOut("Unable to rename " + outName); m->mothurOutEndLine();
281                         perror(" : ");
282                 }       
283         }
284         catch(exception& e) {
285                 m->errorOut(e, "SharedThreeColumnFile", "resetFile");
286                 exit(1);
287         }
288 }
289
290 /***********************************************************************/
291
292 /***********************************************************************/
293
294 OneColumnFile::~OneColumnFile(){
295         
296         inFile.close();
297         outFile.close();
298         m->mothurRemove(outName);       
299 }
300
301 /***********************************************************************/
302
303 void OneColumnFile::initFile(string label){
304         try {
305                 if(counter != 0){
306                         m->openOutputFile(outName, outFile);
307                         m->openInputFile(inName, inFile);
308                 
309                         string inputBuffer;
310                         inputBuffer = m->getline(inFile);
311                 
312                         outFile <<  inputBuffer << '\t' << label << endl;
313                 }
314                 else{
315                         m->openOutputFile(outName, outFile);
316                         outFile << "numsampled\t" << label << endl;
317                 }
318         
319                 outFile.setf(ios::fixed, ios::floatfield);
320                 outFile.setf(ios::showpoint);
321         }
322         catch(exception& e) {
323                 m->errorOut(e, "OneColumnFile", "initFile");
324                 exit(1);
325         }
326 }
327
328 /***********************************************************************/
329
330 void OneColumnFile::output(int nSeqs, vector<double> data){
331         try {   
332                 if(counter != 0){               
333                         string inputBuffer;
334                         inputBuffer = m->getline(inFile);
335                 
336                         outFile <<  inputBuffer << setprecision(4) << '\t'  << data[0] << endl;
337                 }
338                 else{   
339                         outFile << nSeqs << setprecision(4) << '\t' << data[0] << endl;
340                 }
341         }
342         catch(exception& e) {
343                 m->errorOut(e, "OneColumnFile", "output");
344                 exit(1);
345         }
346 }
347
348 /***********************************************************************/
349
350 void OneColumnFile::resetFile(){
351         try {
352                 if(counter != 0){
353                         outFile.close();
354                         inFile.close();
355                 }else{
356                         outFile.close();
357                 }       
358                 counter = 1;
359                 
360                 m->mothurRemove(inName);
361                 renameOk = rename(outName.c_str(), inName.c_str());
362                 
363                 //renameFile(outName, inName);
364                 
365                 //checks to make sure user was able to rename and remove successfully
366                 if ((renameOk != 0)) { 
367                         m->mothurOut("Unable to rename " + outName); m->mothurOutEndLine();
368                         perror(" : ");
369                 }       
370
371         }
372         catch(exception& e) {
373                 m->errorOut(e, "OneColumnFile", "resetFile");
374                 exit(1);
375         }
376 }
377
378 /***********************************************************************/
379 /***********************************************************************/
380
381 SharedOneColumnFile::~SharedOneColumnFile(){
382         
383         inFile.close();
384         outFile.close();
385         m->mothurRemove(outName);       
386 }
387
388 /***********************************************************************/
389
390 void SharedOneColumnFile::initFile(string label){
391         try {
392                 if(counter != 0){
393                         m->openOutputFile(outName, outFile);
394                         m->openInputFile(inName, inFile);
395                 
396                         string inputBuffer;
397                         inputBuffer = m->getline(inFile);
398                 
399                         outFile <<  inputBuffer << '\t' << label  << endl;
400
401                 }
402                 else{
403                         m->openOutputFile(outName, outFile);
404                         outFile << "sampled\t" << label << endl;
405                 
406                 }
407         
408                 outFile.setf(ios::fixed, ios::floatfield);
409                 outFile.setf(ios::showpoint);
410         }
411         catch(exception& e) {
412                 m->errorOut(e, "SharedOneColumnFile", "initFile");
413                 exit(1);
414         }
415 }
416
417 /***********************************************************************/
418
419 void SharedOneColumnFile::output(int nSeqs, vector<double> data){
420         try {   
421                         string dataOutput;
422                         float sam;
423                         sam = data[0];
424                         dataOutput = "";
425                         for (int i = 0; i < data.size(); i++) {
426                                 dataOutput = dataOutput + "\t" + toString(data[i]);
427                         }
428                         if(counter != 0){               
429                                 string inputBuffer;
430                                 inputBuffer = m->getline(inFile);
431
432                                 outFile <<  inputBuffer << setprecision(2) << '\t' << dataOutput << endl;
433                         }
434                         else{   
435                                 outFile << nSeqs << setprecision(2) << '\t' << dataOutput << endl;
436                         }
437         }
438         catch(exception& e) {
439                 m->errorOut(e, "SharedOneColumnFile", "output");
440                 exit(1);
441         }
442 }
443
444 /***********************************************************************/
445
446 void SharedOneColumnFile::resetFile(){
447         try {
448                 if(counter != 0){
449                         outFile.close();
450                         inFile.close();
451                 }
452                 else{
453                         outFile.close();
454                 }       
455                 counter = 1;
456
457                 m->mothurRemove(inName);
458                 renameOk = rename(outName.c_str(), inName.c_str());
459                 
460                 //renameFile(outName, inName);
461                 
462                 //checks to make sure user was able to rename and remove successfully
463                 if ((renameOk != 0)) { 
464                         m->mothurOut("Unable to rename " + outName); m->mothurOutEndLine();
465                         perror(" : ");
466                 }       
467         }
468         catch(exception& e) {
469                 m->errorOut(e, "SharedOneColumnFile", "resetFile");
470                 exit(1);
471         }
472 }
473
474 /***********************************************************************/