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 //**********************************************************************************************************************
17 vector<string> SharedCommand::getValidParameters(){
19 vector<string> myArray;
23 m->errorOut(e, "SharedCommand", "getValidParameters");
27 //**********************************************************************************************************************
28 SharedCommand::SharedCommand(){
31 //initialize outputTypes
32 vector<string> tempOutNames;
33 outputTypes["rabund"] = tempOutNames;
34 outputTypes["shared"] = tempOutNames;
37 m->errorOut(e, "SharedCommand", "SharedCommand");
41 //**********************************************************************************************************************
42 vector<string> SharedCommand::getRequiredParameters(){
44 vector<string> myArray;
48 m->errorOut(e, "SharedCommand", "getRequiredParameters");
52 //**********************************************************************************************************************
53 vector<string> SharedCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "SharedCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
65 SharedCommand::SharedCommand(string o) : outputDir(o) {
67 globaldata = GlobalData::getInstance();
69 //initialize outputTypes
70 vector<string> tempOutNames;
71 outputTypes["rabund"] = tempOutNames;
72 outputTypes["shared"] = tempOutNames;
74 //getting output filename
75 filename = globaldata->inputFileName;
76 if (outputDir == "") { outputDir += m->hasPath(filename); }
78 filename = outputDir + m->getRootName(m->getSimpleName(filename));
79 filename = filename + "shared";
80 outputTypes["shared"].push_back(filename);
82 m->openOutputFile(filename, out);
85 groupMap = globaldata->gGroupmap;
87 //if hte user has not specified any groups then use them all
88 if (globaldata->Groups.size() == 0) {
89 groups = groupMap->namesOfGroups;
90 }else{ //they have specified groups
91 groups = globaldata->Groups;
95 //fill filehandles with neccessary ofstreams
98 for (i=0; i<groups.size(); i++) {
100 filehandles[groups[i]] = temp;
104 fileroot = outputDir + m->getRootName(m->getSimpleName(globaldata->getListFile()));
106 //clears file before we start to write to it below
107 for (int i=0; i<groups.size(); i++) {
108 remove((fileroot + groups[i] + ".rabund").c_str());
109 outputNames.push_back((fileroot + groups[i] + ".rabund"));
110 outputTypes["rabund"].push_back((fileroot + groups[i] + ".rabund"));
114 catch(exception& e) {
115 m->errorOut(e, "SharedCommand", "SharedCommand");
119 //**********************************************************************************************************************
121 int SharedCommand::execute(){
125 string errorOff = "no error";
128 //if user provided an order file containing the order the shared file should be in read it
129 if (globaldata->getOrderGroupFile() != "") { readOrderFile(); }
132 read = new ReadOTUFile(globaldata->inputFileName);
133 read->read(&*globaldata);
136 input = globaldata->ginput;
137 SharedList = globaldata->gSharedList;
138 string lastLabel = SharedList->getLabel();
139 vector<SharedRAbundVector*> lookup;
141 if (m->control_pressed) {
142 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
143 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
144 out.close(); remove(filename.c_str());
145 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
149 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
150 m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine();
153 remove(filename.c_str()); //remove blank shared file you made
155 createMisMatchFile();
158 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
162 globaldata->ginput = NULL;
164 globaldata->gSharedList = NULL;
169 //if user has specified groups make new groupfile for them
170 if (globaldata->Groups.size() != 0) { //make new group file
172 for (int i = 0; i < globaldata->Groups.size(); i++) {
173 groups += globaldata->Groups[i] + ".";
176 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + groups + "groups";
178 m->openOutputFile(newGroupFile, outGroups);
180 vector<string> names = groupMap->getNamesSeqs();
182 for (int i = 0; i < names.size(); i++) {
183 groupName = groupMap->getGroup(names[i]);
184 if (isValidGroup(groupName, globaldata->Groups)) {
185 outGroups << names[i] << '\t' << groupName << endl;
191 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
192 set<string> processedLabels;
193 set<string> userLabels = globaldata->labels;
195 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
196 if (m->control_pressed) {
197 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
198 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
199 out.close(); remove(filename.c_str());
200 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
204 if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
206 lookup = SharedList->getSharedRAbundVector();
208 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
209 if (pickedGroups) { //check for otus with no seqs in them
210 eliminateZeroOTUS(lookup);
213 if (m->control_pressed) {
214 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
216 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
217 out.close(); remove(filename.c_str());
218 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
222 printSharedData(lookup); //prints info to the .shared file
223 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
225 processedLabels.insert(SharedList->getLabel());
226 userLabels.erase(SharedList->getLabel());
229 if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
230 string saveLabel = SharedList->getLabel();
233 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
235 lookup = SharedList->getSharedRAbundVector();
236 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
237 if (pickedGroups) { //check for otus with no seqs in them
238 eliminateZeroOTUS(lookup);
242 if (m->control_pressed) {
243 delete input; delete SharedList; globaldata->ginput = NULL; globaldata->gSharedList = NULL;
244 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
245 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
246 out.close(); remove(filename.c_str());
247 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
251 printSharedData(lookup); //prints info to the .shared file
252 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
254 processedLabels.insert(SharedList->getLabel());
255 userLabels.erase(SharedList->getLabel());
257 //restore real lastlabel to save below
258 SharedList->setLabel(saveLabel);
262 lastLabel = SharedList->getLabel();
265 SharedList = input->getSharedListVector(); //get new list vector to process
268 //output error messages about any remaining user labels
269 set<string>::iterator it;
270 bool needToRun = false;
271 for (it = userLabels.begin(); it != userLabels.end(); it++) {
272 if (processedLabels.count(lastLabel) != 1) {
277 //run last label if you need to
278 if (needToRun == true) {
279 if (SharedList != NULL) { delete SharedList; }
280 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
282 lookup = SharedList->getSharedRAbundVector();
283 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
284 if (pickedGroups) { //check for otus with no seqs in them
285 eliminateZeroOTUS(lookup);
288 if (m->control_pressed) {
289 delete input; globaldata->ginput = NULL;
290 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
291 out.close(); remove(filename.c_str());
292 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
296 printSharedData(lookup); //prints info to the .shared file
297 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
301 globaldata->gSharedList = NULL;
305 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
310 //change format to shared to speed up commands
311 globaldata->setFormat("sharedfile");
312 globaldata->setListFile("");
313 globaldata->setGroupFile("");
314 globaldata->setSharedFile(filename);
317 if (m->control_pressed) {
318 delete input; globaldata->ginput = NULL;
319 remove(filename.c_str());
320 for (int i=0; i<groups.size(); i++) { remove((fileroot + groups[i] + ".rabund").c_str()); }
324 m->mothurOutEndLine();
325 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
326 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
327 m->mothurOut(filename); m->mothurOutEndLine();
328 m->mothurOutEndLine();
332 catch(exception& e) {
333 m->errorOut(e, "SharedCommand", "execute");
337 //**********************************************************************************************************************
338 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
341 if (order.size() == 0) { //user has not specified an order so do aplabetically
342 sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
344 globaldata->Groups.clear();
346 //initialize bin values
347 for (int i = 0; i < thislookup.size(); i++) {
348 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
349 thislookup[i]->print(out);
351 globaldata->Groups.push_back(thislookup[i]->getGroup());
353 RAbundVector rav = thislookup[i]->getRAbundVector();
354 m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
355 rav.print(*(filehandles[thislookup[i]->getGroup()]));
356 (*(filehandles[thislookup[i]->getGroup()])).close();
359 //create a map from groupName to each sharedrabund
360 map<string, SharedRAbundVector*> myMap;
361 map<string, SharedRAbundVector*>::iterator myIt;
363 for (int i = 0; i < thislookup.size(); i++) {
364 myMap[thislookup[i]->getGroup()] = thislookup[i];
367 globaldata->Groups.clear();
369 //loop through ordered list and print the rabund
370 for (int i = 0; i < order.size(); i++) {
371 myIt = myMap.find(order[i]);
373 if(myIt != myMap.end()) { //we found it
374 out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
375 (myIt->second)->print(out);
377 globaldata->Groups.push_back((myIt->second)->getGroup());
379 RAbundVector rav = (myIt->second)->getRAbundVector();
380 m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
381 rav.print(*(filehandles[(myIt->second)->getGroup()]));
382 (*(filehandles[(myIt->second)->getGroup()])).close();
384 m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
391 catch(exception& e) {
392 m->errorOut(e, "SharedCommand", "printSharedData");
396 //**********************************************************************************************************************
397 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
400 vector<SharedRAbundVector*> newLookup;
401 for (int i = 0; i < thislookup.size(); i++) {
402 SharedRAbundVector* temp = new SharedRAbundVector();
403 temp->setLabel(thislookup[i]->getLabel());
404 temp->setGroup(thislookup[i]->getGroup());
405 newLookup.push_back(temp);
409 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
410 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
412 //look at each sharedRabund and make sure they are not all zero
414 for (int j = 0; j < thislookup.size(); j++) {
415 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
418 //if they are not all zero add this bin
420 for (int j = 0; j < thislookup.size(); j++) {
421 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
424 //else{ cout << "bin # " << i << " is all zeros" << endl; }
427 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
428 thislookup = newLookup;
433 catch(exception& e) {
434 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
438 //**********************************************************************************************************************
439 int SharedCommand::createMisMatchFile() {
441 ofstream outMisMatch;
442 string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
444 //you have sequences in your list file that are not in your group file
445 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
446 outputMisMatchName += "missing.group";
447 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();
449 m->openOutputFile(outputMisMatchName, outMisMatch);
451 map<string, string> listNames;
452 map<string, string>::iterator itList;
454 //go through list and if group returns "not found" output it
455 for (int i = 0; i < SharedList->getNumBins(); i++) {
456 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
458 string names = SharedList->get(i);
460 while (names.find_first_of(',') != -1) {
461 string name = names.substr(0,names.find_first_of(','));
462 names = names.substr(names.find_first_of(',')+1, names.length());
463 string group = groupMap->getGroup(name);
465 if(group == "not found") { outMisMatch << name << endl; }
467 itList = listNames.find(name);
468 if (itList != listNames.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
469 else { listNames[name] = name; }
473 string group = groupMap->getGroup(names);
474 if(group == "not found") { outMisMatch << names << endl; }
476 itList = listNames.find(names);
477 if (itList != listNames.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
478 else { listNames[names] = names; }
485 }else {//you have sequences in your group file that are not in you list file
487 outputMisMatchName += "missing.name";
488 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();
490 map<string, string> namesInList;
491 map<string, string>::iterator itList;
493 //go through listfile and get names
494 for (int i = 0; i < SharedList->getNumBins(); i++) {
495 if (m->control_pressed) { return 0; }
498 string names = SharedList->get(i);
500 while (names.find_first_of(',') != -1) {
501 string name = names.substr(0,names.find_first_of(','));
502 names = names.substr(names.find_first_of(',')+1, names.length());
504 itList = namesInList.find(name);
505 if (itList != namesInList.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
507 namesInList[name] = name;
511 itList = namesInList.find(names);
512 if (itList != namesInList.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
515 namesInList[names] = names;
518 //get names of sequences in groupfile
519 vector<string> seqNames = groupMap->getNamesSeqs();
521 map<string, string>::iterator itMatch;
523 m->openOutputFile(outputMisMatchName, outMisMatch);
525 //loop through names in seqNames and if they aren't in namesIn list output them
526 for (int i = 0; i < seqNames.size(); i++) {
527 if (m->control_pressed) { outMisMatch.close(); remove(outputMisMatchName.c_str()); return 0; }
529 itMatch = namesInList.find(seqNames[i]);
531 if (itMatch == namesInList.end()) {
533 outMisMatch << seqNames[i] << endl;
541 catch(exception& e) {
542 m->errorOut(e, "SharedCommand", "createMisMatchFile");
547 //**********************************************************************************************************************
549 SharedCommand::~SharedCommand(){
554 //**********************************************************************************************************************
555 int SharedCommand::readOrderFile() {
561 m->openInputFile(globaldata->getOrderGroupFile(), in);
565 in >> thisGroup; m->gobble(in);
567 order.push_back(thisGroup);
569 if (m->control_pressed) { order.clear(); break; }
575 catch(exception& e) {
576 m->errorOut(e, "SharedCommand", "readOrderFile");
580 //**********************************************************************************************************************
582 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
584 for (int i = 0; i < groups.size(); i++) {
585 if (groupname == groups[i]) { return true; }
590 catch(exception& e) {
591 m->errorOut(e, "SharedCommand", "isValidGroup");
595 /************************************************************/