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