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