]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
changed how we do "smart" distancing
[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                 string lastLabel = SharedList->getLabel();
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(lastLabel) != 1)) {
73                                         delete SharedList;
74                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
75                                         
76                                         lookup = SharedList->getSharedRAbundVector();
77                                         printSharedData(lookup); //prints info to the .shared file
78                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
79                                         
80                                         processedLabels.insert(SharedList->getLabel());
81                                         userLabels.erase(SharedList->getLabel());
82                         }
83                         
84                 
85                         lastLabel = SharedList->getLabel();
86                                 
87                         delete SharedList;
88                         SharedList = input->getSharedListVector(); //get new list vector to process
89                         
90                         count++;                
91                 }
92                 
93                 //output error messages about any remaining user labels
94                 set<string>::iterator it;
95                 bool needToRun = false;
96                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
97                         //cout << "Your file does not include the label "<< *it; 
98                         if (processedLabels.count(lastLabel) != 1) {
99                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
100                                 needToRun = true;
101                         }else {
102                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
103                         }
104                 }
105                 
106                 //run last line if you need to
107                 if (needToRun == true)  {
108                         delete SharedList;
109                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
110                                         
111                         lookup = SharedList->getSharedRAbundVector();
112                         printSharedData(lookup); //prints info to the .shared file
113                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
114                         delete SharedList;
115                 }
116                 
117                 globaldata->gSharedList = NULL;
118                 delete read;
119                 
120                 out.close();
121                 
122                 cout << "complete." << endl;
123                 return 0;
124         }
125         catch(exception& e) {
126                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127                 exit(1);
128         }
129         catch(...) {
130                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
131                 exit(1);
132         }
133
134 }
135 //**********************************************************************************************************************
136 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
137         try {
138                 
139                 //initialize bin values
140                 for (int i = 0; i < thislookup.size(); i++) {
141                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
142                         thislookup[i]->print(out);
143                 }
144  
145         }
146         catch(exception& e) {
147                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148                 exit(1);
149         }
150         catch(...) {
151                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152                 exit(1);
153         }
154         
155 }
156
157 //**********************************************************************************************************************
158
159 SharedCommand::~SharedCommand(){
160         //delete list;
161         
162         
163 }
164
165 //**********************************************************************************************************************