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