]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added hcluster command and fixed some bugs, namely one with 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                 groupMap = globaldata->gGroupmap;
25                 
26                 //fill filehandles with neccessary ofstreams
27                 int i;
28                 ofstream* temp;
29                 for (i=0; i<groupMap->getNumGroups(); i++) {
30                         temp = new ofstream;
31                         filehandles[groupMap->namesOfGroups[i]] = temp;
32                 }
33                 
34                 //set fileroot
35                 fileroot = getRootName(globaldata->getListFile());
36                 
37                 //clears file before we start to write to it below
38                 for (int i=0; i<groupMap->getNumGroups(); i++) {
39                         remove((fileroot + groupMap->namesOfGroups[i] + ".rabund").c_str());
40                 }
41
42         }
43         catch(exception& e) {
44                 errorOut(e, "SharedCommand", "SharedCommand");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49
50 int SharedCommand::execute(){
51         try {
52                 
53                 //lookup.clear();
54                 string errorOff = "no error";
55                 //errorOff = "";
56                         
57                 //read in listfile
58                 read = new ReadOTUFile(globaldata->inputFileName);      
59                 read->read(&*globaldata); 
60                 delete read;
61
62                 input = globaldata->ginput;
63                 SharedList = globaldata->gSharedList;
64                 string lastLabel = SharedList->getLabel();
65                 vector<SharedRAbundVector*> lookup; 
66                 
67                 if (SharedList->getNumSeqs() != groupMap->getNumSeqs()) {  
68                         mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine(); 
69                         
70                         out.close();
71                         remove(filename.c_str()); //remove blank shared file you made
72                         
73                         createMisMatchFile();
74                         
75                         //delete memory
76                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
77                                 delete it3->second;
78                         }
79                         delete SharedList;
80                         globaldata->gSharedList = NULL;
81                         
82                         return 1; 
83                 }
84                 
85                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
86                 set<string> processedLabels;
87                 set<string> userLabels = globaldata->labels;    
88                 
89                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
90                         
91
92                         if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
93                                         
94                                         lookup = SharedList->getSharedRAbundVector();
95                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
96                                         
97                                         printSharedData(lookup); //prints info to the .shared file
98                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
99                                 
100                                         processedLabels.insert(SharedList->getLabel());
101                                         userLabels.erase(SharedList->getLabel());
102                         }
103                         
104                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
105                                         string saveLabel = SharedList->getLabel();
106                                         
107                                         delete SharedList;
108                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
109                                         
110                                         lookup = SharedList->getSharedRAbundVector();
111                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
112                                         
113                                         printSharedData(lookup); //prints info to the .shared file
114                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
115                                         
116                                         processedLabels.insert(SharedList->getLabel());
117                                         userLabels.erase(SharedList->getLabel());
118                                         
119                                         //restore real lastlabel to save below
120                                         SharedList->setLabel(saveLabel);
121                         }
122                         
123                 
124                         lastLabel = SharedList->getLabel();
125                                 
126                         delete SharedList;
127                         SharedList = input->getSharedListVector(); //get new list vector to process
128                 }
129                 
130                 //output error messages about any remaining user labels
131                 set<string>::iterator it;
132                 bool needToRun = false;
133                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
134                         if (processedLabels.count(lastLabel) != 1) {
135                                 needToRun = true;
136                         }
137                 }
138                 
139                 //run last label if you need to
140                 if (needToRun == true)  {
141                         if (SharedList != NULL) {       delete SharedList;      }
142                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
143                                         
144                         lookup = SharedList->getSharedRAbundVector();
145                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
146                         
147                         printSharedData(lookup); //prints info to the .shared file
148                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
149                         delete SharedList;
150                 }
151                 
152                 globaldata->gSharedList = NULL;
153                 
154                 out.close();
155                 
156                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
157                         delete it3->second;
158                 }
159
160                 
161                 return 0;
162         }
163         catch(exception& e) {
164                 errorOut(e, "SharedCommand", "execute");
165                 exit(1);
166         }
167 }
168 //**********************************************************************************************************************
169 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
170         try {
171                 
172                 //initialize bin values
173                 for (int i = 0; i < thislookup.size(); i++) {
174 //cout << "in printData " << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() <<  endl;
175                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
176                         thislookup[i]->print(out);
177                         
178                         RAbundVector rav = thislookup[i]->getRAbundVector();
179                         openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
180                         rav.print(*(filehandles[thislookup[i]->getGroup()]));
181                         (*(filehandles[thislookup[i]->getGroup()])).close();
182                 }
183  
184         }
185         catch(exception& e) {
186                 errorOut(e, "SharedCommand", "printSharedData");
187                 exit(1);
188         }
189 }
190 //**********************************************************************************************************************
191 void SharedCommand::createMisMatchFile() {
192         try {
193                 ofstream outMisMatch;
194                 string outputMisMatchName = getRootName(globaldata->inputFileName);
195                 
196                 //you have sequences in your list file that are not in your group file
197                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
198                         outputMisMatchName += "missing.group";
199                         mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
200                         
201                         openOutputFile(outputMisMatchName, outMisMatch);
202                         
203                         //go through list and if group returns "not found" output it
204                         for (int i = 0; i < SharedList->getNumBins(); i++) {
205                         
206                                 string names = SharedList->get(i); 
207                                 
208                                 while (names.find_first_of(',') != -1) { 
209                                         string name = names.substr(0,names.find_first_of(','));
210                                         names = names.substr(names.find_first_of(',')+1, names.length());
211                                         string group = groupMap->getGroup(name);
212                                         
213                                         if(group == "not found") {      outMisMatch << name << endl;  }
214                                 }
215                                 
216                                 //get last name
217                                 string group = groupMap->getGroup(names);
218                                 if(group == "not found") {      outMisMatch << names << endl;  }                                
219                         }
220                         
221                         outMisMatch.close();
222                         
223                 
224                 }else {//you have sequences in your group file that are not in you list file
225                         
226                         outputMisMatchName += "missing.name";
227                         mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
228                         
229                         map<string, string> namesInList;
230                         
231                         //go through listfile and get names
232                         for (int i = 0; i < SharedList->getNumBins(); i++) {
233                                 
234                                 string names = SharedList->get(i); 
235                 
236                                 while (names.find_first_of(',') != -1) { 
237                                         string name = names.substr(0,names.find_first_of(','));
238                                         names = names.substr(names.find_first_of(',')+1, names.length());
239                                         
240                                         namesInList[name] = name;
241                                 }
242                                 
243                                 //get last name
244                                 namesInList[names] = names;                             
245                         }
246                         
247                         //get names of sequences in groupfile
248                         vector<string> seqNames = groupMap->getNamesSeqs();
249                 
250                         map<string, string>::iterator itMatch;
251                         
252                         openOutputFile(outputMisMatchName, outMisMatch);
253                         
254                         //loop through names in seqNames and if they aren't in namesIn list output them
255                         for (int i = 0; i < seqNames.size(); i++) {
256                                 
257                                 itMatch = namesInList.find(seqNames[i]);
258                                 
259                                 if (itMatch == namesInList.end()) {
260                                 
261                                         outMisMatch << seqNames[i] << endl; 
262                                 }
263                         }               
264                         outMisMatch.close();
265                 }
266  
267         }
268         catch(exception& e) {
269                 errorOut(e, "SharedCommand", "createMisMatchFile");
270                 exit(1);
271         }
272 }
273
274 //**********************************************************************************************************************
275
276 SharedCommand::~SharedCommand(){
277         //delete list;
278         
279         
280 }
281
282 //**********************************************************************************************************************