]> git.donarmstrong.com Git - mothur.git/blob - getsabundcommand.cpp
added get.rabund and get.sabund command and fixed bug introduced by line by line...
[mothur.git] / getsabundcommand.cpp
1 /*
2  *  getsabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "getsabundcommand.h"
11
12 //**********************************************************************************************************************
13
14 GetSAbundCommand::GetSAbundCommand(){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 filename = getRootName(globaldata->inputFileName) + "sabund";
18                 
19                 openOutputFile(filename, out);
20         }
21         catch(exception& e) {
22                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
23                 exit(1);
24         }
25         catch(...) {
26                 cout << "An unknown error has occurred in the GetSAbundCommand class function GetSAbundCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
27                 exit(1);
28         }       
29                         
30 }
31
32 //**********************************************************************************************************************
33
34 GetSAbundCommand::~GetSAbundCommand(){
35 }
36
37 //**********************************************************************************************************************
38
39 int GetSAbundCommand::execute(){
40         try {
41                 int count = 1;
42                 
43                 //using order vector so you don't have to distinguish between the list and rabund files
44                 read = new ReadOTUFile(globaldata->inputFileName);      
45                 read->read(&*globaldata); 
46                 
47                 order = globaldata->gorder;
48                 lastOrder = order;
49                 input = globaldata->ginput;
50                                                 
51                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
52                 set<string> processedLabels;
53                 set<string> userLabels = globaldata->labels;
54                 set<int> userLines = globaldata->lines;
55
56                 
57                 while((order != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
58                         
59                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
60                                         cout << order->getLabel() << '\t' << count << endl;
61                                         sabund = new SAbundVector();
62                                         *sabund = (order->getSAbundVector());
63                                         sabund->print(out);
64                                         delete sabund;
65
66                                         processedLabels.insert(order->getLabel());
67                                         userLabels.erase(order->getLabel());
68                                         userLines.erase(count);
69                         }
70                         
71                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
72                                         cout << lastOrder->getLabel() << '\t' << count << endl;
73                                         sabund = new SAbundVector();
74                                         *sabund = (lastOrder->getSAbundVector());
75                                         sabund->print(out);
76                                         delete sabund;
77
78                                         processedLabels.insert(lastOrder->getLabel());
79                                         userLabels.erase(lastOrder->getLabel());
80                         }
81                         
82                         if (count != 1) { delete lastOrder; }
83                         lastOrder = order;      
84                                         
85                         order = (input->getOrderVector());
86                         count++;
87                 }
88                 
89                 //output error messages about any remaining user labels
90                 set<string>::iterator it;
91                 bool needToRun = false;
92                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
93                         cout << "Your file does not include the label "<< *it; 
94                         if (processedLabels.count(lastOrder->getLabel()) != 1) {
95                                 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
96                                 needToRun = true;
97                         }else {
98                                 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
99                         }
100                 }
101                 
102                 //run last line if you need to
103                 if (needToRun == true)  {
104                         cout << lastOrder->getLabel() << '\t' << count << endl;
105                         sabund = new SAbundVector();
106                         *sabund = (lastOrder->getSAbundVector());
107                         sabund->print(out);
108                         delete sabund;
109                 }
110                 delete lastOrder;
111
112                 out.close();
113                 return 0;               
114         }
115
116         catch(exception& e) {
117                 cout << "Standard Error: " << e.what() << " has occurred in the GetSAbundCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120         catch(...) {
121                 cout << "An unknown error has occurred in the GetSAbundCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }       
124 }
125
126 //**********************************************************************************************************************
127
128