+/***********************************************************/
+int GetSharedOTUCommand::runShared() {
+ try {
+ InputData input(sharedfile, "sharedfile");
+ vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
+ string lastLabel = lookup[0]->getLabel();
+
+ if (Groups.size() == 0) {
+ Groups = m->getGroups();
+
+ //make string for outputfile name
+ userGroups = "unique.";
+ for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
+ userGroups = userGroups.substr(0, userGroups.length()-1);
+ }else {
+ //sanity check for group names
+ SharedUtil util;
+ vector<string> allGroups = m->getAllGroups();
+ util.setGroups(Groups, allGroups);
+ }
+
+ //put groups in map to find easier
+ for(int i = 0; i < Groups.size(); i++) {
+ groupFinder[Groups[i]] = Groups[i];
+ }
+
+ //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+ set<string> processedLabels;
+ set<string> userLabels = labels;
+
+ //as long as you are not at the end of the file or done wih the lines you want
+ while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+ if (m->control_pressed) {
+ outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
+ }
+
+
+ if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
+ m->mothurOut(lookup[0]->getLabel());
+ process(lookup);
+
+ processedLabels.insert(lookup[0]->getLabel());
+ userLabels.erase(lookup[0]->getLabel());
+ }
+
+ if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+ string saveLabel = lookup[0]->getLabel();
+
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ lookup = input.getSharedRAbundVectors(lastLabel);
+
+ m->mothurOut(lookup[0]->getLabel());
+ process(lookup);
+
+ processedLabels.insert(lookup[0]->getLabel());
+ userLabels.erase(lookup[0]->getLabel());
+
+ //restore real lastlabel to save below
+ lookup[0]->setLabel(saveLabel);
+ }
+
+ lastLabel = lookup[0]->getLabel();
+
+ //get next line to process
+ //prevent memory leak
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ lookup = input.getSharedRAbundVectors();
+ }
+
+ if (m->control_pressed) {
+ outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
+ }
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ m->mothurOut("Your file does not include the label " + *it);
+ if (processedLabels.count(lastLabel) != 1) {
+ m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
+ needToRun = true;
+ }else {
+ m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
+ lookup = input.getSharedRAbundVectors(lastLabel);
+
+ m->mothurOut(lookup[0]->getLabel());
+ process(lookup);
+ for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
+ }
+
+ //reset groups parameter
+ m->clearGroups();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "GetSharedOTUCommand", "runShared");
+ exit(1);
+ }
+}
+/***********************************************************/
+int GetSharedOTUCommand::process(vector<SharedRAbundVector*>& lookup) {
+ try {
+
+ string outputFileNames;
+ if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
+ variables["[distance]"] = lookup[0]->getLabel();
+ variables["[group]"] = userGroups;
+ if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
+ else { outputFileNames = getOutputFileName("accnos", variables); }
+
+ ofstream outNames;
+ m->openOutputFile(outputFileNames, outNames);
+
+ bool wroteSomething = false;
+ int num = 0;
+
+ //go through each bin, find out if shared
+ for (int i = 0; i < lookup[0]->getNumBins(); i++) {
+ if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
+
+ bool uniqueOTU = true;
+ map<string, int> atLeastOne;
+ for (int f = 0; f < Groups.size(); f++) { atLeastOne[Groups[f]] = 0; }
+
+ set<string> namesOfGroupsInThisBin;
+
+ for(int j = 0; j < lookup.size(); j++) {
+ string seqGroup = lookup[j]->getGroup();
+ string name = m->currentSharedBinLabels[i];
+
+ if (lookup[j]->getAbundance(i) != 0) {
+ if (output != "accnos") {
+ namesOfGroupsInThisBin.insert(name + "|" + seqGroup + "|" + toString(lookup[j]->getAbundance(i)));
+ }else { namesOfGroupsInThisBin.insert(name); }
+
+ //is this seq in one of the groups we care about
+ it = groupFinder.find(seqGroup);
+ if (it == groupFinder.end()) { uniqueOTU = false; } //you have sequences from a group you don't want
+ else { atLeastOne[seqGroup]++; }
+ }
+ }
+
+ //make sure you have at least one seq from each group you want
+ bool sharedByAll = true;
+ map<string, int>::iterator it2;
+ for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
+ if (it2->second == 0) { sharedByAll = false; }
+ }
+
+ //if the user wants unique bins and this is unique then print
+ //or this the user wants shared bins and this bin is shared then print
+ if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
+
+ wroteSomething = true;
+ num++;
+
+ //output list of names
+ for (set<string>::iterator itNames = namesOfGroupsInThisBin.begin(); itNames != namesOfGroupsInThisBin.end(); itNames++) {
+ outNames << (*itNames) << endl;
+ }
+ }
+ }
+ outNames.close();
+
+ if (!wroteSomething) {
+ m->mothurRemove(outputFileNames);
+ string outputString = "\t" + toString(num) + " - No otus shared by groups";
+
+ string groupString = "";
+ for (int h = 0; h < Groups.size(); h++) {
+ groupString += " " + Groups[h];
+ }
+
+ outputString += groupString + ".";
+ m->mothurOut(outputString); m->mothurOutEndLine();
+ }else {
+ m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
+ outputNames.push_back(outputFileNames);
+ if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
+ else { outputTypes["accnos"].push_back(outputFileNames); }
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "GetSharedOTUCommand", "process");
+ exit(1);
+ }
+}