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::setParameters(){
19 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
20 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pgroup);
21 CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup);
22 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
23 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
24 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
25 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
27 vector<string> myArray;
28 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
32 m->errorOut(e, "SharedCommand", "setParameters");
36 //**********************************************************************************************************************
37 string SharedCommand::getHelpString(){
39 string helpString = "";
40 helpString += "The make.shared command reads a list and group file and creates a shared file, as well as a rabund file for each group.\n";
41 helpString += "The make.shared command parameters are list, group, ordergroup, groups and label. list and group are required unless a current file is available.\n";
42 helpString += "The groups parameter allows you to indicate which groups you want to include, group names should be separated by dashes. ex. groups=A-B-C. Default is all groups in your groupfile.\n";
43 helpString += "The label parameter allows you to indicate which labels you want to include, label names should be separated by dashes. Default is all labels in your list file.\n";
44 helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n";
48 m->errorOut(e, "SharedCommand", "getHelpString");
52 //**********************************************************************************************************************
53 SharedCommand::SharedCommand(){
55 abort = true; calledHelp = true;
57 //initialize outputTypes
58 vector<string> tempOutNames;
59 outputTypes["rabund"] = tempOutNames;
60 outputTypes["shared"] = tempOutNames;
61 outputTypes["group"] = tempOutNames;
64 m->errorOut(e, "SharedCommand", "SharedCommand");
68 //**********************************************************************************************************************
69 SharedCommand::SharedCommand(string option) {
71 abort = false; calledHelp = false;
74 //allow user to run help
75 if(option == "help") { help(); abort = true; calledHelp = true; }
76 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
80 vector<string> myArray = setParameters();
82 OptionParser parser(option);
83 map<string, string> parameters = parser.getParameters();
85 ValidParameters validParameter;
86 map<string, string>::iterator it;
88 //check to make sure all parameters are valid for command
89 for (it = parameters.begin(); it != parameters.end(); it++) {
90 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("list");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["list"] = inputDir + it->second; }
106 it = parameters.find("group");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["group"] = inputDir + it->second; }
114 it = parameters.find("ordergroup");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
124 //if the user changes the output directory command factory will send this info to us in the output parameter
125 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
127 //check for required parameters
128 listfile = validParameter.validFile(parameters, "list", true);
129 if (listfile == "not open") { listfile = ""; abort = true; }
130 else if (listfile == "not found") {
131 listfile = m->getListFile();
132 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
133 else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
134 }else { m->setListFile(listfile); }
136 ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
137 if (ordergroupfile == "not open") { abort = true; }
138 else if (ordergroupfile == "not found") { ordergroupfile = ""; }
140 groupfile = validParameter.validFile(parameters, "group", true);
141 if (groupfile == "not open") { groupfile = ""; abort = true; }
142 else if (groupfile == "not found") {
143 groupfile = m->getGroupFile();
144 if (groupfile != "") {
145 m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
146 groupMap = new GroupMap(groupfile);
148 int error = groupMap->readMap();
149 if (error == 1) { abort = true; }
150 m->namesOfGroups = groupMap->namesOfGroups;
152 else { m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
154 groupMap = new GroupMap(groupfile);
156 int error = groupMap->readMap();
157 if (error == 1) { abort = true; }
158 m->namesOfGroups = groupMap->namesOfGroups;
159 m->setGroupFile(groupfile);
162 string groups = validParameter.validFile(parameters, "groups", false);
163 if (groups == "not found") { groups = ""; }
165 m->splitAtDash(groups, Groups);
169 //check for optional parameter and set defaults
170 // ...at some point should added some additional type checking...
171 string label = validParameter.validFile(parameters, "label", false);
172 if (label == "not found") { label = ""; }
174 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
175 else { allLines = 1; }
180 catch(exception& e) {
181 m->errorOut(e, "SharedCommand", "SharedCommand");
185 //**********************************************************************************************************************
187 int SharedCommand::execute(){
190 if (abort == true) { if (calledHelp) { return 0; } return 2; }
192 //getting output filename
195 if (outputDir == "") { outputDir += m->hasPath(filename); }
197 filename = outputDir + m->getRootName(m->getSimpleName(filename));
198 filename = filename + "shared";
199 outputTypes["shared"].push_back(filename);
201 m->openOutputFile(filename, out);
202 pickedGroups = false;
204 //if hte user has not specified any groups then use them all
205 if (Groups.size() == 0) {
206 Groups = groupMap->namesOfGroups; m->Groups = Groups;
207 }else { pickedGroups = true; }
209 //fill filehandles with neccessary ofstreams
212 for (i=0; i<Groups.size(); i++) {
214 filehandles[Groups[i]] = temp;
218 fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
220 //clears file before we start to write to it below
221 for (int i=0; i<Groups.size(); i++) {
222 m->mothurRemove((fileroot + Groups[i] + ".rabund"));
223 outputNames.push_back((fileroot + Groups[i] + ".rabund"));
224 outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
228 string errorOff = "no error";
231 //if user provided an order file containing the order the shared file should be in read it
232 if (ordergroupfile != "") { readOrderFile(); }
234 input = new InputData(listfile, "shared");
235 SharedList = input->getSharedListVector();
236 string lastLabel = SharedList->getLabel();
237 vector<SharedRAbundVector*> lookup;
239 if (m->control_pressed) {
240 delete input; delete SharedList; delete groupMap;
241 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
242 out.close(); m->mothurRemove(filename);
243 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
247 if ((m->Groups.size() == 0) && (SharedList->getNumSeqs() != groupMap->getNumSeqs())) { //if the user has not specified any groups and their files don't match exit with error
248 m->mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); m->mothurOutEndLine();
251 m->mothurRemove(filename); //remove blank shared file you made
253 createMisMatchFile();
256 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
260 delete input; delete SharedList; delete groupMap;
265 //if user has specified groups make new groupfile for them
266 if (pickedGroups) { //make new group file
268 if (m->Groups.size() < 4) {
269 for (int i = 0; i < m->Groups.size(); i++) {
270 groups += m->Groups[i] + ".";
272 }else { groups = "merge"; }
274 string newGroupFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + groups + "groups";
275 outputTypes["group"].push_back(newGroupFile);
276 outputNames.push_back(newGroupFile);
278 m->openOutputFile(newGroupFile, outGroups);
280 vector<string> names = groupMap->getNamesSeqs();
282 for (int i = 0; i < names.size(); i++) {
283 groupName = groupMap->getGroup(names[i]);
284 if (isValidGroup(groupName, m->Groups)) {
285 outGroups << names[i] << '\t' << groupName << endl;
291 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
292 set<string> processedLabels;
293 set<string> userLabels = labels;
295 while((SharedList != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
296 if (m->control_pressed) {
297 delete input; delete SharedList; delete groupMap;
298 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
299 out.close(); m->mothurRemove(filename);
300 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
304 if(allLines == 1 || labels.count(SharedList->getLabel()) == 1){
306 lookup = SharedList->getSharedRAbundVector();
308 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
309 if (pickedGroups) { //check for otus with no seqs in them
310 eliminateZeroOTUS(lookup);
313 if (m->control_pressed) {
314 delete input; delete SharedList; delete groupMap;
315 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
316 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
317 out.close(); m->mothurRemove(filename);
318 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
322 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
323 printSharedData(lookup); //prints info to the .shared file
324 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
326 processedLabels.insert(SharedList->getLabel());
327 userLabels.erase(SharedList->getLabel());
330 if ((m->anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
331 string saveLabel = SharedList->getLabel();
334 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
336 lookup = SharedList->getSharedRAbundVector();
337 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
338 if (pickedGroups) { //check for otus with no seqs in them
339 eliminateZeroOTUS(lookup);
343 if (m->control_pressed) {
344 delete input; delete SharedList; delete groupMap;
345 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
346 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
347 out.close(); m->mothurRemove(filename);
348 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
352 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
353 printSharedData(lookup); //prints info to the .shared file
354 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
356 processedLabels.insert(SharedList->getLabel());
357 userLabels.erase(SharedList->getLabel());
359 //restore real lastlabel to save below
360 SharedList->setLabel(saveLabel);
364 lastLabel = SharedList->getLabel();
367 SharedList = input->getSharedListVector(); //get new list vector to process
370 //output error messages about any remaining user labels
371 set<string>::iterator it;
372 bool needToRun = false;
373 for (it = userLabels.begin(); it != userLabels.end(); it++) {
374 if (processedLabels.count(lastLabel) != 1) {
379 //run last label if you need to
380 if (needToRun == true) {
381 if (SharedList != NULL) { delete SharedList; }
382 SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
384 lookup = SharedList->getSharedRAbundVector();
385 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
386 if (pickedGroups) { //check for otus with no seqs in them
387 eliminateZeroOTUS(lookup);
390 if (m->control_pressed) {
391 delete input; delete groupMap;
392 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it3->second; }
393 out.close(); m->mothurRemove(filename);
394 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
398 if (!m->printedHeaders) { lookup[0]->printHeaders(out); }
399 printSharedData(lookup); //prints info to the .shared file
400 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
406 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
410 delete input; delete groupMap;
412 if (m->control_pressed) {
413 m->mothurRemove(filename);
414 for (int i=0; i<Groups.size(); i++) { m->mothurRemove((fileroot + Groups[i] + ".rabund")); }
418 //set rabund file as new current rabundfile
420 itTypes = outputTypes.find("rabund");
421 if (itTypes != outputTypes.end()) {
422 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
425 itTypes = outputTypes.find("shared");
426 if (itTypes != outputTypes.end()) {
427 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSharedFile(current); }
430 itTypes = outputTypes.find("group");
431 if (itTypes != outputTypes.end()) {
432 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
435 m->mothurOutEndLine();
436 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
437 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
438 m->mothurOut(filename); m->mothurOutEndLine();
439 m->mothurOutEndLine();
443 catch(exception& e) {
444 m->errorOut(e, "SharedCommand", "execute");
448 //**********************************************************************************************************************
449 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
452 if (order.size() == 0) { //user has not specified an order so do aplabetically
453 sort(thislookup.begin(), thislookup.end(), compareSharedRabunds);
457 //initialize bin values
458 for (int i = 0; i < thislookup.size(); i++) {
459 out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
460 thislookup[i]->print(out);
462 m->Groups.push_back(thislookup[i]->getGroup());
464 RAbundVector rav = thislookup[i]->getRAbundVector();
465 m->openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
466 rav.print(*(filehandles[thislookup[i]->getGroup()]));
467 (*(filehandles[thislookup[i]->getGroup()])).close();
470 //create a map from groupName to each sharedrabund
471 map<string, SharedRAbundVector*> myMap;
472 map<string, SharedRAbundVector*>::iterator myIt;
474 for (int i = 0; i < thislookup.size(); i++) {
475 myMap[thislookup[i]->getGroup()] = thislookup[i];
480 //loop through ordered list and print the rabund
481 for (int i = 0; i < order.size(); i++) {
482 myIt = myMap.find(order[i]);
484 if(myIt != myMap.end()) { //we found it
485 out << (myIt->second)->getLabel() << '\t' << (myIt->second)->getGroup() << '\t';
486 (myIt->second)->print(out);
488 m->Groups.push_back((myIt->second)->getGroup());
490 RAbundVector rav = (myIt->second)->getRAbundVector();
491 m->openOutputFileAppend(fileroot + (myIt->second)->getGroup() + ".rabund", *(filehandles[(myIt->second)->getGroup()]));
492 rav.print(*(filehandles[(myIt->second)->getGroup()]));
493 (*(filehandles[(myIt->second)->getGroup()])).close();
495 m->mothurOut("Can't find shared info for " + order[i] + ", skipping."); m->mothurOutEndLine();
502 catch(exception& e) {
503 m->errorOut(e, "SharedCommand", "printSharedData");
507 //**********************************************************************************************************************
508 int SharedCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
511 vector<SharedRAbundVector*> newLookup;
512 for (int i = 0; i < thislookup.size(); i++) {
513 SharedRAbundVector* temp = new SharedRAbundVector();
514 temp->setLabel(thislookup[i]->getLabel());
515 temp->setGroup(thislookup[i]->getGroup());
516 newLookup.push_back(temp);
520 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
521 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
523 //look at each sharedRabund and make sure they are not all zero
525 for (int j = 0; j < thislookup.size(); j++) {
526 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
529 //if they are not all zero add this bin
531 for (int j = 0; j < thislookup.size(); j++) {
532 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
534 //if there is a bin label use it otherwise make one
536 //else{ cout << "bin # " << i << " is all zeros" << endl; }
539 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
540 thislookup = newLookup;
545 catch(exception& e) {
546 m->errorOut(e, "SharedCommand", "eliminateZeroOTUS");
550 //**********************************************************************************************************************
551 int SharedCommand::createMisMatchFile() {
553 ofstream outMisMatch;
554 string outputMisMatchName = outputDir + m->getRootName(m->getSimpleName(listfile));
556 //you have sequences in your list file that are not in your group file
557 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) {
558 outputMisMatchName += "missing.group";
559 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();
561 m->openOutputFile(outputMisMatchName, outMisMatch);
563 map<string, string> listNames;
564 map<string, string>::iterator itList;
566 //go through list and if group returns "not found" output it
567 for (int i = 0; i < SharedList->getNumBins(); i++) {
568 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; }
570 string names = SharedList->get(i);
572 while (names.find_first_of(',') != -1) {
573 string name = names.substr(0,names.find_first_of(','));
574 names = names.substr(names.find_first_of(',')+1, names.length());
575 string group = groupMap->getGroup(name);
577 if(group == "not found") { outMisMatch << name << endl; }
579 itList = listNames.find(name);
580 if (itList != listNames.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
581 else { listNames[name] = name; }
585 string group = groupMap->getGroup(names);
586 if(group == "not found") { outMisMatch << names << endl; }
588 itList = listNames.find(names);
589 if (itList != listNames.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
590 else { listNames[names] = names; }
597 }else {//you have sequences in your group file that are not in you list file
599 outputMisMatchName += "missing.name";
600 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();
602 map<string, string> namesInList;
603 map<string, string>::iterator itList;
605 //go through listfile and get names
606 for (int i = 0; i < SharedList->getNumBins(); i++) {
607 if (m->control_pressed) { return 0; }
610 string names = SharedList->get(i);
612 while (names.find_first_of(',') != -1) {
613 string name = names.substr(0,names.find_first_of(','));
614 names = names.substr(names.find_first_of(',')+1, names.length());
616 itList = namesInList.find(name);
617 if (itList != namesInList.end()) { m->mothurOut(name + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
619 namesInList[name] = name;
623 itList = namesInList.find(names);
624 if (itList != namesInList.end()) { m->mothurOut(names + " is in your list file more than once. Sequence names must be unique. please correct."); m->mothurOutEndLine(); }
627 namesInList[names] = names;
630 //get names of sequences in groupfile
631 vector<string> seqNames = groupMap->getNamesSeqs();
633 map<string, string>::iterator itMatch;
635 m->openOutputFile(outputMisMatchName, outMisMatch);
637 //loop through names in seqNames and if they aren't in namesIn list output them
638 for (int i = 0; i < seqNames.size(); i++) {
639 if (m->control_pressed) { outMisMatch.close(); m->mothurRemove(outputMisMatchName); return 0; }
641 itMatch = namesInList.find(seqNames[i]);
643 if (itMatch == namesInList.end()) {
645 outMisMatch << seqNames[i] << endl;
653 catch(exception& e) {
654 m->errorOut(e, "SharedCommand", "createMisMatchFile");
659 //**********************************************************************************************************************
661 SharedCommand::~SharedCommand(){
666 //**********************************************************************************************************************
667 int SharedCommand::readOrderFile() {
673 m->openInputFile(ordergroupfile, in);
677 in >> thisGroup; m->gobble(in);
679 order.push_back(thisGroup);
681 if (m->control_pressed) { order.clear(); break; }
687 catch(exception& e) {
688 m->errorOut(e, "SharedCommand", "readOrderFile");
692 //**********************************************************************************************************************
694 bool SharedCommand::isValidGroup(string groupname, vector<string> groups) {
696 for (int i = 0; i < groups.size(); i++) {
697 if (groupname == groups[i]) { return true; }
702 catch(exception& e) {
703 m->errorOut(e, "SharedCommand", "isValidGroup");
707 /************************************************************/