]> git.donarmstrong.com Git - mothur.git/blob - inputdata.cpp
created mothurOut class to handle logfiles
[mothur.git] / inputdata.cpp
1 /*
2  *  inputdata.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 "inputdata.h"
11 #include "ordervector.hpp"
12 #include "listvector.hpp"
13 #include "rabundvector.hpp"
14
15 /***********************************************************************/
16
17 InputData::InputData(string fName, string f) : format(f){
18         m = MothurOut::getInstance();
19         openInputFile(fName, fileHandle);
20         filename = fName;
21         
22 }
23
24 /***********************************************************************/
25
26
27 InputData::~InputData(){
28         fileHandle.close();
29 //      delete output;
30         
31 }
32
33 /***********************************************************************/
34
35 InputData::InputData(string fName, string orderFileName, string f) : format(f){
36         try {
37                 m = MothurOut::getInstance();
38                 ifstream ofHandle;
39                 openInputFile(orderFileName, ofHandle);
40                 string name;
41
42                 int count = 0;
43         
44                 while(ofHandle){
45                         ofHandle >> name;
46                         orderMap[name] = count;
47                         count++;
48                         gobble(ofHandle);
49                 }
50                 ofHandle.close();
51         
52                 openInputFile(fName, fileHandle);
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "InputData", "InputData");
56                 exit(1);
57         }
58 }
59 /***********************************************************************/
60
61 ListVector* InputData::getListVector(){
62         try {
63                 if(fileHandle){
64                         if(format == "list") {
65                                 list = new ListVector(fileHandle);
66                         }else{ list = NULL;  }
67                                         
68                         gobble(fileHandle);
69                         return list;
70                 }
71                 else{
72                         return NULL;
73                 }
74         }
75         catch(exception& e) {
76                 m->errorOut(e, "InputData", "getListVector");
77                 exit(1);
78         }
79 }
80
81 /***********************************************************************/
82
83 ListVector* InputData::getListVector(string label){
84         try {
85                 ifstream in;
86                 string  thisLabel;
87                 openInputFile(filename, in);
88                 
89                 if(in){
90
91                         if (format == "list") {
92                         
93                                 while (in.eof() != true) {
94                                         
95                                         list = new ListVector(in);
96                                         thisLabel = list->getLabel();
97                                         
98                                         //if you are at the last label
99                                         if (thisLabel == label) {  break;  }
100                                         //so you don't loose this memory
101                                         else {  delete list;    }
102                                         gobble(in);
103                                 }
104                         }else{ list = NULL;  }
105                         
106                         in.close();
107                         return list;
108                 }
109                 else{
110                         return NULL;
111                 }
112         }
113         catch(exception& e) {
114                 m->errorOut(e, "InputData", "getListVector");
115                 exit(1);
116         }
117 }
118
119
120 /***********************************************************************/
121
122 SharedListVector* InputData::getSharedListVector(){
123         try {
124                 if(fileHandle){
125                         if (format == "shared")  {
126                                 SharedList = new SharedListVector(fileHandle);
127                         }else{ SharedList = NULL;  }
128                                         
129                         gobble(fileHandle);
130                         return SharedList;
131                 }
132                 else{
133                         return NULL;
134                 }
135         }
136         catch(exception& e) {
137                 m->errorOut(e, "InputData", "getSharedListVector");
138                 exit(1);
139         }
140 }
141 /***********************************************************************/
142
143 SharedListVector* InputData::getSharedListVector(string label){
144         try {
145                 ifstream in;
146                 string  thisLabel;
147                 openInputFile(filename, in);
148                 
149                 if(in){
150
151                         if (format == "shared")  {
152                         
153                                 while (in.eof() != true) {
154                                         
155                                         SharedList = new SharedListVector(in);
156                                         thisLabel = SharedList->getLabel();
157                                         
158                                         //if you are at the last label
159                                         if (thisLabel == label) {  break;  }
160                                         //so you don't loose this memory
161                                         else {  delete SharedList;      }
162                                         gobble(in);
163                                 }
164
165                         }else{ SharedList = NULL;  }
166                                 
167                         in.close();
168                         return SharedList;
169                         
170                 }else{
171                         return NULL;
172                 }
173         }
174         catch(exception& e) {
175                 m->errorOut(e, "InputData", "getSharedListVector");
176                 exit(1);
177         }
178 }
179
180
181
182 /***********************************************************************/
183
184 SharedOrderVector* InputData::getSharedOrderVector(){
185         try {
186                 if(fileHandle){
187                         if (format == "sharedfile")  {
188                                 SharedOrder = new SharedOrderVector(fileHandle);
189                         }else{ SharedOrder = NULL;  }
190                                 
191                         gobble(fileHandle);
192                         return SharedOrder;
193                         
194                 }else{
195                         return NULL;
196                 }
197         }
198         catch(exception& e) {
199                 m->errorOut(e, "InputData", "getSharedOrderVector");
200                 exit(1);
201         }
202 }
203
204 /***********************************************************************/
205
206 SharedOrderVector* InputData::getSharedOrderVector(string label){
207         try {
208                 ifstream in;
209                 string  thisLabel;
210                 openInputFile(filename, in);
211                 
212                 if(in){
213
214                         if (format == "sharedfile")  {
215                         
216                                 while (in.eof() != true) {
217                                         
218                                         SharedOrder = new SharedOrderVector(in);
219                                         thisLabel = SharedOrder->getLabel();
220                                         
221                                         //if you are at the last label
222                                         if (thisLabel == label) {  break;  }
223                                         //so you don't loose this memory
224                                         else {  delete SharedOrder;     }
225                                         gobble(in);
226                                 }
227
228                         }else{ SharedOrder = NULL;  }
229                                 
230                         in.close();
231                         return SharedOrder;
232                         
233                 }else{
234                         return NULL;
235                 }
236         }
237         catch(exception& e) {
238                 m->errorOut(e, "InputData", "getSharedOrderVector");
239                 exit(1);
240         }
241 }
242
243
244
245 /***********************************************************************/
246
247 OrderVector* InputData::getOrderVector(){
248         try {
249                 if(fileHandle){
250                         if((format == "list") || (format == "listorder")) {
251                                 input = new ListVector(fileHandle);
252                         }
253                         else if (format == "shared")  {
254                                 input = new SharedListVector(fileHandle);
255                         }
256                         else if(format == "rabund"){
257                                 input = new RAbundVector(fileHandle);
258                         }
259                         else if(format == "order"){             
260                                 input = new OrderVector(fileHandle);
261                         }
262                         else if(format == "sabund"){
263                                 input = new SAbundVector(fileHandle);
264                         }
265                         
266                         gobble(fileHandle);
267                         
268                         output = new OrderVector();     
269                         *output = (input->getOrderVector());
270                 
271                         return output;
272                 }
273                 else{
274                         return NULL;
275                 }
276         }
277         catch(exception& e) {
278                 m->errorOut(e, "InputData", "getOrderVector");
279                 exit(1);
280         }
281 }
282
283 /***********************************************************************/
284 OrderVector* InputData::getOrderVector(string label){
285         try {
286         
287                 ifstream in;
288                 string  thisLabel;
289                 openInputFile(filename, in);
290                 
291                 if(in){
292                         if((format == "list") || (format == "listorder")) {
293                         
294                                 while (in.eof() != true) {
295                                         
296                                         input = new ListVector(in);
297                                         thisLabel = input->getLabel();
298                                         
299                                         //if you are at the last label
300                                         if (thisLabel == label) {  break;  }
301                                         //so you don't loose this memory
302                                         else {  delete input;   }
303                                         gobble(in);
304                                 }
305                         }
306                         else if (format == "shared")  {
307                                 
308                                 while (in.eof() != true) {
309                                         
310                                         input = new SharedListVector(in);
311                                         thisLabel = input->getLabel();
312                                         
313                                         //if you are at the last label
314                                         if (thisLabel == label) {  break;  }
315                                         //so you don't loose this memory
316                                         else {  delete input;   }
317                                         gobble(in);
318                                 }
319
320                         }
321                         else if(format == "rabund"){
322                                 
323                                 while (in.eof() != true) {
324                                         
325                                         input = new RAbundVector(in);
326                                         thisLabel = input->getLabel();
327                                         
328                                         //if you are at the last label
329                                         if (thisLabel == label) {  break;  }
330                                         //so you don't loose this memory
331                                         else {  delete input;   }
332                                         gobble(in);
333                                 }
334
335                         }
336                         else if(format == "order"){                     
337                                 
338                                 while (in.eof() != true) {
339                                         
340                                         input = new OrderVector(in);
341                                         thisLabel = input->getLabel();
342                                         
343                                         //if you are at the last label
344                                         if (thisLabel == label) {  break;  }
345                                         //so you don't loose this memory
346                                         else {  delete input;   }
347                                         gobble(in);
348                                 }
349
350                         }
351                         else if(format == "sabund"){
352                                 
353                                 while (in.eof() != true) {
354                                         
355                                         input = new SAbundVector(in);
356                                         thisLabel = input->getLabel();
357                                         
358                                         //if you are at the last label
359                                         if (thisLabel == label) {  break;  }
360                                         //so you don't loose this memory
361                                         else {  delete input;   }
362                                         gobble(in);
363                                         
364                                 }
365
366                         }
367                         
368                         in.close();             
369
370                         output = new OrderVector();
371                         *output = (input->getOrderVector());
372                         
373                         return output;
374
375                 }
376                 else{
377                         return NULL;
378                 }
379         }
380         catch(exception& e) {
381                 m->errorOut(e, "InputData", "getOrderVector");
382                 exit(1);
383         }
384 }
385
386 /***********************************************************************/
387 //this is used when you don't need the order vector
388 vector<SharedRAbundVector*> InputData::getSharedRAbundVectors(){
389         try {
390                 if(fileHandle){
391                         if (format == "sharedfile")  {
392                                 SharedRAbundVector* SharedRAbund = new SharedRAbundVector(fileHandle);
393                                 if (SharedRAbund != NULL) {
394                                         return SharedRAbund->getSharedRAbundVectors();
395                                 }
396                         }else if (format == "shared") {
397                                 SharedList = new SharedListVector(fileHandle);
398                                 if (SharedList != NULL) {
399                                         return SharedList->getSharedRAbundVector();
400                                 }
401                         }
402                         gobble(fileHandle);
403                 }
404                                 
405                 //this is created to signal to calling function that the input file is at eof
406                 vector<SharedRAbundVector*> null;  null.push_back(NULL);
407                 return null;
408                 
409         }
410         catch(exception& e) {
411                 m->errorOut(e, "InputData", "getSharedRAbundVectors");
412                 exit(1);
413         }
414 }
415 /***********************************************************************/
416 vector<SharedRAbundVector*> InputData::getSharedRAbundVectors(string label){
417         try {
418                 ifstream in;
419                 string  thisLabel;
420                 
421                 openInputFile(filename, in);
422                 
423                 if(in){
424                         if (format == "sharedfile")  {
425                                 while (in.eof() != true) {
426                                         
427                                         SharedRAbundVector* SharedRAbund = new SharedRAbundVector(in);
428                                         if (SharedRAbund != NULL) {
429                                                 thisLabel = SharedRAbund->getLabel();
430                                                 //if you are at the last label
431                                                 if (thisLabel == label) {  in.close(); return SharedRAbund->getSharedRAbundVectors();  }
432                                                 else {
433                                                         //so you don't loose this memory
434                                                         vector<SharedRAbundVector*> lookup = SharedRAbund->getSharedRAbundVectors(); 
435                                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
436                                                         delete SharedRAbund;
437                                                 }
438                                         }else{  break;  }
439                                         gobble(in);
440                                         
441                                 }
442                         }else if (format == "shared") {
443                                 while (in.eof() != true) {
444                                         
445                                         SharedList = new SharedListVector(in);
446                                         if (SharedList != NULL) {
447                                                 thisLabel = SharedList->getLabel();
448                                                 //if you are at the last label
449                                                 if (thisLabel == label) {  in.close(); return SharedList->getSharedRAbundVector();  }
450                                                 else {
451                                                         //so you don't loose this memory
452                                                         delete SharedList;
453                                                 }
454                                         }else{  break;  }
455                                         gobble(in);
456                                         
457                                 }
458                         
459                         }
460                 }
461                                 
462                 //this is created to signal to calling function that the input file is at eof
463                 vector<SharedRAbundVector*> null;  null.push_back(NULL);
464                 in.close();
465                 return null;
466         
467         }
468         catch(exception& e) {
469                 m->errorOut(e, "InputData", "getSharedRAbundVectors");
470                 exit(1);
471         }
472 }
473
474
475 /***********************************************************************/
476
477 SAbundVector* InputData::getSAbundVector(){
478         try {
479                 if(fileHandle){
480                         if (format == "list") {
481                                 input = new ListVector(fileHandle);
482                         }
483                         else if (format == "shared")  {
484                                 input = new SharedListVector(fileHandle);
485                         }
486                         else if(format == "rabund"){
487                                 input = new RAbundVector(fileHandle);
488                         }
489                         else if(format == "order"){                     
490                                 input = new OrderVector(fileHandle);
491                         }
492                         else if(format == "sabund"){
493                                 input = new SAbundVector(fileHandle);
494                         }
495                                         
496                         gobble(fileHandle);
497
498                         sabund = new SAbundVector();
499                         *sabund = (input->getSAbundVector());
500
501                         return sabund;
502                 }
503                 else{
504                         return NULL;
505                 }
506         }
507         catch(exception& e) {
508                 m->errorOut(e, "InputData", "getSAbundVector");
509                 exit(1);
510         }
511 }
512 /***********************************************************************/
513 SAbundVector* InputData::getSAbundVector(string label){
514         try {
515         
516                 ifstream in;
517                 string  thisLabel;
518                 openInputFile(filename, in);
519                 
520                 if(in){
521                         if (format == "list") {
522                         
523                                 while (in.eof() != true) {
524                                         
525                                         input = new ListVector(in);
526                                         thisLabel = input->getLabel();
527                                         
528                                         //if you are at the last label
529                                         if (thisLabel == label) {  break;  }
530                                         //so you don't loose this memory
531                                         else {  delete input;   }
532                                         gobble(in);
533                                 }
534                         }
535                         else if (format == "shared")  {
536                                 
537                                 while (in.eof() != true) {
538                                         
539                                         input = new SharedListVector(in);
540                                         thisLabel = input->getLabel();
541                                         
542                                         //if you are at the last label
543                                         if (thisLabel == label) {  break;  }
544                                         //so you don't loose this memory
545                                         else {  delete input;   }
546                                         gobble(in);
547                                 }
548
549                         }
550                         else if(format == "rabund"){
551                                 
552                                 while (in.eof() != true) {
553                                         
554                                         input = new RAbundVector(in);
555                                         thisLabel = input->getLabel();
556                                         
557                                         //if you are at the last label
558                                         if (thisLabel == label) {  break;  }
559                                         //so you don't loose this memory
560                                         else {  delete input;   }
561                                         gobble(in);
562                                 }
563
564                         }
565                         else if(format == "order"){                     
566                                 
567                                 while (in.eof() != true) {
568                                         
569                                         input = new OrderVector(in);
570                                         thisLabel = input->getLabel();
571                                         
572                                         //if you are at the last label
573                                         if (thisLabel == label) {  break;  }
574                                         //so you don't loose this memory
575                                         else {  delete input;   }
576                                         gobble(in);
577                                 }
578
579                         }
580                         else if(format == "sabund"){
581                                 
582                                 while (in.eof() != true) {
583                                         
584                                         input = new SAbundVector(in);
585                                         thisLabel = input->getLabel();
586                                         
587                                         //if you are at the last label
588                                         if (thisLabel == label) {  break;  }
589                                         //so you don't loose this memory
590                                         else {  delete input;   }
591                                         gobble(in);
592                                         
593                                 }
594
595                         }
596                         
597                         in.close();             
598
599                         sabund = new SAbundVector();
600                         *sabund = (input->getSAbundVector());
601
602                         return sabund;
603
604                 }
605                 else{
606                         return NULL;
607                 }
608         }
609         catch(exception& e) {
610                 m->errorOut(e, "InputData", "getSAbundVector");
611                 exit(1);
612         }
613 }
614
615 /***********************************************************************/
616 RAbundVector* InputData::getRAbundVector(){
617         try {
618                 if(fileHandle){
619                         if (format == "list") {
620                                 input = new ListVector(fileHandle);
621                         }
622                         else if (format == "shared")  {
623                                 input = new SharedListVector(fileHandle);
624                         }
625                         else if(format == "rabund"){
626                                 input = new RAbundVector(fileHandle);
627                         }
628                         else if(format == "order"){                     
629                                 input = new OrderVector(fileHandle);
630                         }
631                         else if(format == "sabund"){
632                                 input = new SAbundVector(fileHandle);
633                         }
634                                         
635                         gobble(fileHandle);
636
637                         rabund = new RAbundVector();
638                         *rabund = (input->getRAbundVector());
639
640                         return rabund;
641                 }
642                 else{
643                         return NULL;
644                 }
645         }
646         catch(exception& e) {
647                 m->errorOut(e, "InputData", "getRAbundVector");
648                 exit(1);
649         }
650 }
651 /***********************************************************************/
652 RAbundVector* InputData::getRAbundVector(string label){
653         try {
654         
655                 ifstream in;
656                 string  thisLabel;
657                 openInputFile(filename, in);
658                 
659                 if(in){
660                         if (format == "list") {
661                         
662                                 while (in.eof() != true) {
663                                         
664                                         input = new ListVector(in);
665                                         thisLabel = input->getLabel();
666                                         
667                                         //if you are at the last label
668                                         if (thisLabel == label) {  break;  }
669                                         //so you don't loose this memory
670                                         else {  delete input;   }
671                                         gobble(in);
672                                 }
673                         }
674                         else if (format == "shared")  {
675                                 
676                                 while (in.eof() != true) {
677                                         
678                                         input = new SharedListVector(in);
679                                         thisLabel = input->getLabel();
680                                         
681                                         //if you are at the last label
682                                         if (thisLabel == label) {  break;  }
683                                         //so you don't loose this memory
684                                         else {  delete input;   }
685                                         gobble(in);
686                                 }
687
688                         }
689                         else if(format == "rabund"){
690                                 
691                                 while (in.eof() != true) {
692                                         
693                                         input = new RAbundVector(in);
694                                         thisLabel = input->getLabel();
695                                         
696                                         //if you are at the last label
697                                         if (thisLabel == label) {  break;  }
698                                         //so you don't loose this memory
699                                         else {  delete input;   }
700                                         gobble(in);
701                                 }
702
703                         }
704                         else if(format == "order"){                     
705                                 
706                                 while (in.eof() != true) {
707                                         
708                                         input = new OrderVector(in);
709                                         thisLabel = input->getLabel();
710                                         
711                                         //if you are at the last label
712                                         if (thisLabel == label) {  break;  }
713                                         //so you don't loose this memory
714                                         else {  delete input;   }
715                                         gobble(in);
716                                 }
717
718                         }
719                         else if(format == "sabund"){
720                                 
721                                 while (in.eof() != true) {
722                                         
723                                         input = new SAbundVector(in);
724                                         thisLabel = input->getLabel();
725                                         
726                                         //if you are at the last label
727                                         if (thisLabel == label) {  break;  }
728                                         //so you don't loose this memory
729                                         else {  delete input;   }
730                                         gobble(in);
731                                         
732                                 }
733
734                         }
735                         
736                         in.close();             
737
738                         rabund = new RAbundVector();
739                         *rabund = (input->getRAbundVector());
740
741                         return rabund;
742                 }
743                 else{
744                         return NULL;
745                 }
746         }
747         catch(exception& e) {
748                 m->errorOut(e, "InputData", "getRAbundVector");
749                 exit(1);
750         }
751 }
752
753 /***********************************************************************/
754
755
756