5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedcommand.h"
11 //********************************************************************************************************************
12 //sorts lowest to highest
13 inline bool compareSharedRabunds(SharedRAbundVector* left, SharedRAbundVector* right){
14 return (left->getGroup() < right->getGroup());
16 //**********************************************************************************************************************
18 SharedCommand::SharedCommand(string o) : outputDir(o) {
20 globaldata = GlobalData::getInstance();
22 //getting output filename
23 filename = globaldata->inputFileName;
24 if (outputDir == "") { outputDir += hasPath(filename); }
26 filename = outputDir + getRootName(getSimpleName(filename));
27 filename = filename + "shared";
29 openOutputFile(filename, out);
32 groupMap = globaldata->gGroupmap;
34 //if hte user has not specified any groups then use them all
35 if (globaldata->Groups.size() == 0) {
36 groups = groupMap->namesOfGroups;
37 }else{ //they have specified groups
38 groups = globaldata->Groups;
42 //fill filehandles with neccessary ofstreams
45 for (i=0; i<groups.size(); i++) {
47 filehandles[groups[i]] = temp;
51 fileroot = outputDir + getRootName(getSimpleName(globaldata->getListFile()));
53 //clears file before we start to write to it below
54 for (int i=0; i<groups.size(); i++) {
55 remove((fileroot + groups[i] + ".rabund").c_str());
56 outputNames.push_back((fileroot + groups[i] + ".rabund"));
61 m->errorOut(e, "SharedCommand", "SharedCommand");
65 //**********************************************************************************************************************
67 int SharedCommand::execute(){
71 string errorOff = "no error";
74 //if user provided an order file containing the order the shared file should be in read it
75 if (globaldata->getOrderGroupFile() != "") { readOrderFile(); }
78 read = new ReadOTUFile(globaldata->inputFileName);
79 read->read(&*globaldata);
82 input = globaldata->ginput;
83 SharedList = globaldata->gSharedList;
84 string lastLabel = SharedList->getLabel();
85 vector<SharedRAbundVector*> lookup;
87 if (m->control_pressed) {
88 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
89 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
90 out.close(); remove(filename.c_str());
91 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
95 if ((globaldata->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) { //if the user has not specified any groups and their files don't match exit with error
96 m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine();
99 remove(filename.c_str()); //remove blank shared file you made
101 createMisMatchFile();
104 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
108 globaldata->ginput = NULL;
110 globaldata->gSharedList = NULL;
115 //if user has specified groups make new groupfile for them
116 if (globaldata->Groups.size() != 0) { //make new group file
118 for (int i = 0; i < globaldata->Groups.size(); i++) {
119 groups += globaldata->Groups[i] + ".";
122 string newGroupFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + groups + "groups";
124 openOutputFile(newGroupFile, outGroups);
126 vector<string> names = groupMap->getNamesSeqs();
128 for (int i = 0; i < names.size(); i++) {
129 groupName = groupMap->getGroup(names[i]);
130 if (isValidGroup(groupName, globaldata->Groups)) {
131 outGroups << names[i] << '\t' << groupName << endl;
137 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
138 set<string> processedLabels;
139 set<string> userLabels = globaldata->labels;
141 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
142 if (m->control_pressed) {
143 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
144 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
145 out.close(); remove(filename.c_str());
146 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
150 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
152 lookup = SharedList->getSharedRAbundVector();
154 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
155 if (pickedGroups) { //check for otus with no seqs in them
156 eliminateZeroOTUS(lookup);
159 if (m->control_pressed) {
160 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
161 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
162 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
163 out.close(); remove(filename.c_str());
164 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
168 printSharedData(lookup); //prints info to the .shared file
169 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
171 processedLabels.insert(SharedList->getLabel());
172 userLabels.erase(SharedList->getLabel());
175 if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
176 string saveLabel = SharedList->getLabel();
179 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
181 lookup = SharedList->getSharedRAbundVector();
182 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
183 if (pickedGroups) { //check for otus with no seqs in them
184 eliminateZeroOTUS(lookup);
188 if (m->control_pressed) {
189 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
190 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
191 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
192 out.close(); remove(filename.c_str());
193 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
197 printSharedData(lookup); //prints info to the .shared file
198 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
200 processedLabels.insert(SharedList->getLabel());
201 userLabels.erase(SharedList->getLabel());
203 //restore real lastlabel to save below
204 SharedList->setLabel(saveLabel);
208 lastLabel = SharedList->getLabel();
211 SharedList = input->getSharedListVector(); //get new list vector to process
214 //output error messages about any remaining user labels
215 set<string>::iterator it;
216 bool needToRun = false;
217 for (it = userLabels.begin(); it != userLabels.end(); it++) {
218 if (processedLabels.count(lastLabel) != 1) {
223 //run last label if you need to
224 if (needToRun == true) {
225 if (SharedList != NULL) { delete SharedList; }
226 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
228 lookup = SharedList->getSharedRAbundVector();
229 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
230 if (pickedGroups) { //check for otus with no seqs in them
231 eliminateZeroOTUS(lookup);
234 if (m->control_pressed) {
235 delete input; globaldata->ginput = NULL;
236 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
237 out.close(); remove(filename.c_str());
238 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
242 printSharedData(lookup); //prints info to the .shared file
243 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
247 globaldata->gSharedList = NULL;
251 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
256 //change format to shared to speed up commands
257 globaldata->setFormat("sharedfile");
258 globaldata->setListFile("");
259 globaldata->setGroupFile("");
260 globaldata->setSharedFile(filename);
262 if (m->control_pressed) {
263 delete input; globaldata->ginput = NULL;
264 remove(filename.c_str());
265 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
269 m->mothurOutEndLine();
270 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
271 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
272 m->mothurOut(filename); m->mothurOutEndLine();
273 m->mothurOutEndLine();
277 catch(exception& e) {
278 m->errorOut(e, "SharedCommand", "execute");
282 //**********************************************************************************************************************
283 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
286 if (order.size() == 0) { //user has not specified an order so do aplabetically
287 sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
289 //initialize bin values
290 for (int i = 0; i < thislookup.size(); i++) {
291 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
292 thislookup[i]->print(out);
294 RAbundVector rav = thislookup[i]->getRAbundVector();
295 openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
296 rav.print(*(filehandles[thislookup[i]->getGroup()]));
297 (*(filehandles[thislookup[i]->getGroup()])).close();
300 //create a map from groupName to each sharedrabund
301 map<string, SharedRAbundVector*> myMap;
302 map<string, SharedRAbundVector*>::iterator myIt;
304 for (int i = 0; i < thislookup.size(); i++) {
305 myMap[thislookup[i]->getGroup()] = thislookup[i];
309 //loop through ordered list and print the rabund
310 for (int i = 0; i < order.size(); i++) {
311 myIt = myMap.find(order[i]);
313 if(myIt != myMap.end()) { //we found it
314 out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
315 (myIt->second)->print(out);
317 RAbundVector rav = (myIt->second)->getRAbundVector();
318 openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
319 rav.print(*(filehandles[(myIt->second)->getGroup()]));
320 (*(filehandles[(myIt->second)->getGroup()])).close();
322 m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
329 catch(exception& e) {
330 m->errorOut(e, "SharedCommand", "printSharedData");
334 //**********************************************************************************************************************
335 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
338 vector<SharedRAbundVector*> newLookup;
339 for (int i = 0; i < thislookup.size(); i++) {
340 SharedRAbundVector* temp = new SharedRAbundVector();
341 temp->setLabel(thislookup[i]->getLabel());
342 temp->setGroup(thislookup[i]->getGroup());
343 newLookup.push_back(temp);
347 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
348 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
350 //look at each sharedRabund and make sure they are not all zero
352 for (int j = 0; j < thislookup.size(); j++) {
353 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
356 //if they are not all zero add this bin
358 for (int j = 0; j < thislookup.size(); j++) {
359 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
362 //else{ cout << "bin # " << i << " is all zeros" << endl; }
365 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
366 thislookup = newLookup;
371 catch(exception& e) {
372 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
376 //**********************************************************************************************************************
377 int SharedCommand::createMisMatchFile() {
379 ofstream outMisMatch;
380 string outputMisMatchName = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
382 //you have sequences in your list file that are not in your group file
383 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
384 outputMisMatchName += "missing.group";
385 m->mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
387 openOutputFile(outputMisMatchName, outMisMatch);
389 map<string, string> listNames;
390 map<string, string>::iterator itList;
392 //go through list and if group returns "not found" output it
393 for (int i = 0; i < SharedList->getNumBins(); i++) {
394 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
396 string names = SharedList->get(i);
398 while (names.find_first_of(',') != -1) {
399 string name = names.substr(0,names.find_first_of(','));
400 names = names.substr(names.find_first_of(',')+1, names.length());
401 string group = groupMap->getGroup(name);
403 if(group == "not found") { outMisMatch << name << endl; }
405 itList = listNames.find(name);
406 if (itList != listNames.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
407 else { listNames[name] = name; }
411 string group = groupMap->getGroup(names);
412 if(group == "not found") { outMisMatch << names << endl; }
414 itList = listNames.find(names);
415 if (itList != listNames.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
416 else { listNames[names] = names; }
423 }else {//you have sequences in your group file that are not in you list file
425 outputMisMatchName += "missing.name";
426 m->mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); m->mothurOutEndLine();
428 map<string, string> namesInList;
429 map<string, string>::iterator itList;
431 //go through listfile and get names
432 for (int i = 0; i < SharedList->getNumBins(); i++) {
433 if (m->control_pressed) { return 0; }
436 string names = SharedList->get(i);
438 while (names.find_first_of(',') != -1) {
439 string name = names.substr(0,names.find_first_of(','));
440 names = names.substr(names.find_first_of(',')+1, names.length());
442 itList = namesInList.find(name);
443 if (itList != namesInList.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
445 namesInList[name] = name;
449 itList = namesInList.find(names);
450 if (itList != namesInList.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
453 namesInList[names] = names;
456 //get names of sequences in groupfile
457 vector<string> seqNames = groupMap->getNamesSeqs();
459 map<string, string>::iterator itMatch;
461 openOutputFile(outputMisMatchName, outMisMatch);
463 //loop through names in seqNames and if they aren't in namesIn list output them
464 for (int i = 0; i < seqNames.size(); i++) {
465 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
467 itMatch = namesInList.find(seqNames[i]);
469 if (itMatch == namesInList.end()) {
471 outMisMatch << seqNames[i] << endl;
479 catch(exception& e) {
480 m->errorOut(e, "SharedCommand", "createMisMatchFile");
485 //**********************************************************************************************************************
487 SharedCommand::~SharedCommand(){
492 //**********************************************************************************************************************
493 int SharedCommand::readOrderFile() {
499 openInputFile(globaldata->getOrderGroupFile(), in);
503 in >> thisGroup; gobble(in);
505 order.push_back(thisGroup);
507 if (m->control_pressed) { order.clear(); break; }
513 catch(exception& e) {
514 m->errorOut(e, "SharedCommand", "readOrderFile");
518 //**********************************************************************************************************************
520 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
522 for (int i = 0; i < groups.size(); i++) {
523 if (groupname == groups[i]) { return true; }
528 catch(exception& e) {
529 m->errorOut(e, "SharedCommand", "isValidGroup");
533 /************************************************************/