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