]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
worked on parselist and shared commands, removing memory leaks and allowing any numbe...
[mothur.git] / sharedcommand.cpp
1 /*
2  *  sharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedcommand.h"
11
12 //**********************************************************************************************************************
13
14 SharedCommand::SharedCommand(){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 //getting output filename
19                 filename = globaldata->inputFileName;
20                 filename = getRootName(filename);
21                 filename = filename + "shared";
22                 openOutputFile(filename, out);
23         }
24         catch(exception& e) {
25                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28         catch(...) {
29                 cout << "An unknown error has occurred in the SharedCommand class function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
30                 exit(1);
31         }
32
33 }
34 //**********************************************************************************************************************
35
36 int SharedCommand::execute(){
37         try {
38                 
39                 cout << "creating sharedfile...";
40                 //lookup.clear();
41                 int count = 1;
42                 string errorOff = "no error";
43                         
44                 //read in listfile
45                 read = new ReadOTUFile(globaldata->inputFileName);      
46                 read->read(&*globaldata); 
47
48                 input = globaldata->ginput;
49                 SharedList = globaldata->gSharedList;
50                 SharedListVector* lastList = SharedList;
51                 vector<SharedRAbundVector*> lookup; 
52                                 
53                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
54                 set<string> processedLabels;
55                 set<string> userLabels = globaldata->labels;
56                 set<int> userLines = globaldata->lines;
57                 
58                 
59                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
60                         
61
62                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
63                                         lookup = SharedList->getSharedRAbundVector();
64                                         printSharedData(lookup); //prints info to the .shared file
65                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
66                                 
67                                         processedLabels.insert(SharedList->getLabel());
68                                         userLabels.erase(SharedList->getLabel());
69                                         userLines.erase(count);
70                         }
71                         
72                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
73                                         lookup = lastList->getSharedRAbundVector();
74                                         printSharedData(lookup); //prints info to the .shared file
75                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
76                                         
77                                         processedLabels.insert(lastList->getLabel());
78                                         userLabels.erase(lastList->getLabel());
79                         }
80                         
81                         if (count != 1) { delete lastList; }
82                         lastList = SharedList;  
83                                                                 
84                         SharedList = input->getSharedListVector(); //get new list vector to process
85                         
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(lastList->getLabel()) != 1) {
95                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
96                                 needToRun = true;
97                         }else {
98                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
99                         }
100                 }
101                 
102                 //run last line if you need to
103                 if (needToRun == true)  {
104                         lookup = lastList->getSharedRAbundVector();
105                         printSharedData(lookup); //prints info to the .shared file
106                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
107
108                 }
109                 
110                 delete lastList; globaldata->gSharedList = NULL;
111                 delete read;
112                 
113                 out.close();
114                 
115                 cout << "complete." << endl;
116                 return 0;
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126
127 }
128 //**********************************************************************************************************************
129 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
130         try {
131                 
132                 //initialize bin values
133                 for (int i = 0; i < thislookup.size(); i++) {
134                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
135                         thislookup[i]->print(out);
136                 }
137  
138         }
139         catch(exception& e) {
140                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141                 exit(1);
142         }
143         catch(...) {
144                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145                 exit(1);
146         }
147         
148 }
149
150 //**********************************************************************************************************************
151
152 SharedCommand::~SharedCommand(){
153         //delete list;
154         
155         
156 }
157
158 //**********************************************************************************************************************