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