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