2 * getsharedotucommand.cpp
5 * Created by westcott on 9/22/09.
6 * Copyright 2009 Schloss Lab. All rights reserved.
10 #include "getsharedotucommand.h"
11 #include "sharedutilities.h"
13 //**********************************************************************************************************************
14 vector<string> GetSharedOTUCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "sharedFasta", "none", "none","fasta",false,false); parameters.push_back(pfasta);
17 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "groupList","",false,false,true); parameters.push_back(pgroup);
18 CommandParameter plist("list", "InputTypes", "", "", "sharedList", "sharedList", "groupList","sharedseq",false,false,true); parameters.push_back(plist);
19 CommandParameter pshared("shared", "InputTypes", "", "", "sharedList-sharedFasta", "sharedList", "none","sharedseq",false,false,true); parameters.push_back(pshared);
20 CommandParameter poutput("output", "Multiple", "accnos-default", "default", "", "", "","",false,false); parameters.push_back(poutput);
21 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
22 CommandParameter puniquegroups("uniquegroups", "String", "", "", "", "", "","",false,false,true); parameters.push_back(puniquegroups);
23 CommandParameter psharedgroups("sharedgroups", "String", "", "", "", "", "","",false,false,true); parameters.push_back(psharedgroups);
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, "GetSharedOTUCommand", "setParameters");
36 //**********************************************************************************************************************
37 string GetSharedOTUCommand::getHelpString(){
39 string helpString = "";
40 helpString += "The get.sharedseqs command parameters are list, group, shared, label, uniquegroups, sharedgroups, output and fasta. The list and group or shared parameters are required, unless you have valid current files.\n";
41 helpString += "The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n";
42 helpString += "The uniquegroups and sharedgroups parameters allow you to select groups you would like to know the shared info for, and are separated by dashes.\n";
43 helpString += "If you enter your groups under the uniquegroups parameter mothur will return the otus that contain ONLY sequences from those groups.\n";
44 helpString += "If you enter your groups under the sharedgroups parameter mothur will return the otus that contain sequences from those groups and may also contain sequences from other groups.\n";
45 helpString += "If you do not enter any groups then the get.sharedseqs command will return sequences that are unique to all groups in your group or shared file.\n";
46 helpString += "The fasta parameter allows you to input a fasta file and outputs a fasta file for each distance level containing only the sequences that are in OTUs shared by the groups specified. It can only be used with a list and group file not the shared file input.\n";
47 helpString += "The output parameter allows you to output the list of names without the group and bin number added. \n";
48 helpString += "With this option you can use the names file as an input in get.seqs and remove.seqs commands. To do this enter output=accnos. \n";
49 helpString += "The get.sharedseqs command outputs a .names file for each distance level containing a list of sequences in the OTUs shared by the groups specified.\n";
50 helpString += "The get.sharedseqs command should be in the following format: get.sharedseqs(list=yourListFile, group=yourGroupFile, label=yourLabels, uniquegroups=yourGroups, fasta=yourFastafile, output=yourOutput).\n";
51 helpString += "Example get.sharedseqs(list=amazon.fn.list, label=unique-0.01, group=amazon.groups, uniquegroups=forest-pasture, fasta=amazon.fasta, output=accnos).\n";
52 helpString += "The output to the screen is the distance and the number of otus at that distance for the groups you specified.\n";
53 helpString += "The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n";
54 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n";
58 m->errorOut(e, "GetSharedOTUCommand", "getHelpString");
62 //**********************************************************************************************************************
63 string GetSharedOTUCommand::getOutputPattern(string type) {
67 if (type == "fasta") { pattern = "[filename],[distance],[group],shared.fasta"; }
68 else if (type == "accnos") { pattern = "[filename],[distance],[group],accnos"; }
69 else if (type == "sharedseqs") { pattern = "[filename],[distance],[group],shared.seqs"; }
70 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
75 m->errorOut(e, "GetSharedOTUCommand", "getOutputPattern");
79 //**********************************************************************************************************************
80 GetSharedOTUCommand::GetSharedOTUCommand(){
82 abort = true; calledHelp = true;
84 vector<string> tempOutNames;
85 outputTypes["fasta"] = tempOutNames;
86 outputTypes["accnos"] = tempOutNames;
87 outputTypes["sharedseqs"] = tempOutNames;
90 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
94 //**********************************************************************************************************************
95 GetSharedOTUCommand::GetSharedOTUCommand(string option) {
98 abort = false; calledHelp = false;
102 //allow user to run help
103 if(option == "help") { help(); abort = true; calledHelp = true; }
104 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
107 vector<string> myArray = setParameters();
109 OptionParser parser(option);
110 map<string,string> parameters = parser.getParameters();
112 ValidParameters validParameter;
113 map<string,string>::iterator it;
115 //check to make sure all parameters are valid for command
116 for (it = parameters.begin(); it != parameters.end(); it++) {
117 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
120 //initialize outputTypes
121 vector<string> tempOutNames;
122 outputTypes["fasta"] = tempOutNames;
123 outputTypes["accnos"] = tempOutNames;
124 outputTypes["sharedseqs"] = tempOutNames;
126 //if the user changes the output directory command factory will send this info to us in the output parameter
127 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
129 //if the user changes the input directory command factory will send this info to us in the output parameter
130 string inputDir = validParameter.validFile(parameters, "inputdir", false);
131 if (inputDir == "not found"){ inputDir = ""; }
134 it = parameters.find("fasta");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["fasta"] = inputDir + it->second; }
142 it = parameters.find("list");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["list"] = inputDir + it->second; }
150 it = parameters.find("shared");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["shared"] = inputDir + it->second; }
158 it = parameters.find("group");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["group"] = inputDir + it->second; }
168 //check for required parameters
169 listfile = validParameter.validFile(parameters, "list", true);
170 if (listfile == "not open") { abort = true; }
171 else if (listfile == "not found") { listfile = ""; }
172 else { format = "list"; m->setListFile(listfile); }
174 groupfile = validParameter.validFile(parameters, "group", true);
175 if (groupfile == "not open") { abort = true; }
176 else if (groupfile == "not found") { groupfile = ""; }
177 else { m->setGroupFile(groupfile); }
179 sharedfile = validParameter.validFile(parameters, "shared", true);
180 if (sharedfile == "not open") { abort = true; }
181 else if (sharedfile == "not found") { sharedfile = ""; }
182 else { m->setSharedFile(sharedfile); }
184 fastafile = validParameter.validFile(parameters, "fasta", true);
185 if (fastafile == "not open") { abort = true; }
186 else if (fastafile == "not found") { fastafile = ""; }
187 else { m->setFastaFile(fastafile); }
190 if ((sharedfile == "") && (listfile == "")) { //look for currents
191 //is there are current file available for either of these?
192 //give priority to shared, then list
193 sharedfile = m->getSharedFile();
194 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
196 listfile = m->getListFile();
197 if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
199 m->mothurOut("No valid current files. You must provide a shared or list file."); m->mothurOutEndLine();
203 }else if ((sharedfile != "") && (listfile != "")) {
204 m->mothurOut("You may enter ONLY ONE of the following: shared or list."); m->mothurOutEndLine(); abort = true;
207 if (listfile != "") {
208 if (groupfile == ""){
209 groupfile = m->getGroupFile();
210 if (groupfile != "") { m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine(); }
211 else { m->mothurOut("You need to provide a group file if you are going to use the list format."); m->mothurOutEndLine(); abort = true;
216 if ((sharedfile != "") && (fastafile != "")) { m->mothurOut("You cannot use the fasta file with the shared file."); m->mothurOutEndLine(); abort = true; }
218 //check for optional parameter and set defaults
219 // ...at some point should added some additional type checking...
220 label = validParameter.validFile(parameters, "label", false);
221 if (label == "not found") { label = ""; }
223 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
224 else { allLines = 1; }
227 output = validParameter.validFile(parameters, "output", false);
228 if (output == "not found") { output = ""; }
229 else if (output == "default") { output = ""; }
231 groups = validParameter.validFile(parameters, "uniquegroups", false);
232 if (groups == "not found") { groups = ""; }
234 userGroups = "unique." + groups;
235 m->splitAtDash(groups, Groups);
238 groups = validParameter.validFile(parameters, "sharedgroups", false);
239 if (groups == "not found") { groups = ""; }
242 m->splitAtDash(groups, Groups);
249 catch(exception& e) {
250 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
254 //**********************************************************************************************************************
256 int GetSharedOTUCommand::execute(){
259 if (abort == true) { if (calledHelp) { return 0; } return 2; }
261 if ( sharedfile != "") { runShared(); }
263 m->setGroups(Groups);
264 groupMap = new GroupMap(groupfile);
265 int error = groupMap->readMap();
266 if (error == 1) { delete groupMap; return 0; }
268 if (m->control_pressed) { delete groupMap; return 0; }
270 if (Groups.size() == 0) {
271 Groups = groupMap->getNamesOfGroups();
273 //make string for outputfile name
274 userGroups = "unique.";
275 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
276 userGroups = userGroups.substr(0, userGroups.length()-1);
278 //sanity check for group names
280 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
281 util.setGroups(Groups, namesOfGroups);
282 groupMap->setNamesOfGroups(namesOfGroups);
285 //put groups in map to find easier
286 for(int i = 0; i < Groups.size(); i++) {
287 groupFinder[Groups[i]] = Groups[i];
290 if (fastafile != "") {
292 m->openInputFile(fastafile, inFasta);
294 while(!inFasta.eof()) {
295 if (m->control_pressed) { outputTypes.clear(); inFasta.close(); delete groupMap; return 0; }
297 Sequence seq(inFasta); m->gobble(inFasta);
298 if (seq.getName() != "") { seqs.push_back(seq); }
303 ListVector* lastlist = NULL;
304 string lastLabel = "";
306 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
307 set<string> processedLabels;
308 set<string> userLabels = labels;
311 m->openInputFile(listfile, in);
313 //as long as you are not at the end of the file or done wih the lines you want
314 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
316 if (m->control_pressed) {
317 if (lastlist != NULL) { delete lastlist; }
318 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
319 delete groupMap; return 0;
322 list = new ListVector(in);
324 if(allLines == 1 || labels.count(list->getLabel()) == 1){
325 m->mothurOut(list->getLabel());
328 processedLabels.insert(list->getLabel());
329 userLabels.erase(list->getLabel());
332 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
333 string saveLabel = list->getLabel();
335 m->mothurOut(lastlist->getLabel());
338 processedLabels.insert(lastlist->getLabel());
339 userLabels.erase(lastlist->getLabel());
341 //restore real lastlabel to save below
342 list->setLabel(saveLabel);
345 lastLabel = list->getLabel();
347 if (lastlist != NULL) { delete lastlist; }
353 //output error messages about any remaining user labels
354 set<string>::iterator it;
355 bool needToRun = false;
356 for (it = userLabels.begin(); it != userLabels.end(); it++) {
357 m->mothurOut("Your file does not include the label " + *it);
358 if (processedLabels.count(lastLabel) != 1) {
359 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
362 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
366 //run last label if you need to
367 if (needToRun == true) {
368 m->mothurOut(lastlist->getLabel());
371 processedLabels.insert(lastlist->getLabel());
372 userLabels.erase(lastlist->getLabel());
376 //reset groups parameter
379 if (lastlist != NULL) { delete lastlist; }
381 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete groupMap; return 0; }
383 //set fasta file as new current fastafile
385 itTypes = outputTypes.find("fasta");
386 if (itTypes != outputTypes.end()) {
387 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
390 if (output == "accnos") {
391 itTypes = outputTypes.find("accnos");
392 if (itTypes != outputTypes.end()) {
393 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
397 m->mothurOutEndLine();
398 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
399 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
400 m->mothurOutEndLine();
406 catch(exception& e) {
407 m->errorOut(e, "GetSharedOTUCommand", "execute");
411 /***********************************************************/
412 int GetSharedOTUCommand::process(ListVector* shared) {
415 map<string, string> fastaMap;
418 string outputFileNames;
420 if (outputDir == "") { outputDir += m->hasPath(listfile); }
421 map<string, string> variables;
422 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
423 variables["[distance]"] = shared->getLabel();
424 variables["[group]"] = userGroups;
425 if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
426 else { outputFileNames = getOutputFileName("accnos", variables); }
428 m->openOutputFile(outputFileNames, outNames);
430 bool wroteSomething = false;
433 //go through each bin, find out if shared
434 for (int i = 0; i < shared->getNumBins(); i++) {
435 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
437 bool uniqueOTU = true;
439 map<string, int> atLeastOne;
440 for (int f = 0; f < Groups.size(); f++) {
441 atLeastOne[Groups[f]] = 0;
444 vector<string> namesOfSeqsInThisBin;
446 string names = shared->get(i);
447 vector<string> binNames;
448 m->splitAtComma(names, binNames);
449 for(int j = 0; j < binNames.size(); j++) {
450 string name = binNames[j];
453 string seqGroup = groupMap->getGroup(name);
454 if (output != "accnos") {
455 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i+1)));
456 }else { namesOfSeqsInThisBin.push_back(name); }
458 if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
460 //is this seq in one of hte groups we care about
461 it = groupFinder.find(seqGroup);
462 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
463 else { atLeastOne[seqGroup]++; }
466 //make sure you have at least one seq from each group you want
467 bool sharedByAll = true;
468 map<string, int>::iterator it2;
469 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
470 if (it2->second == 0) { sharedByAll = false; }
473 //if the user wants unique bins and this is unique then print
474 //or this the user wants shared bins and this bin is shared then print
475 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
477 wroteSomething = true;
480 //output list of names
481 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
482 outNames << namesOfSeqsInThisBin[j] << endl;
484 if (fastafile != "") {
485 if (output != "accnos") {
486 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
487 seqName = seqName.substr(0,seqName.find_last_of('|'));
488 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
490 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
499 if (!wroteSomething) {
500 m->mothurRemove(outputFileNames);
501 string outputString = "\t" + toString(num) + " - No otus shared by groups";
503 string groupString = "";
504 for (int h = 0; h < Groups.size(); h++) {
505 groupString += " " + Groups[h];
508 outputString += groupString + ".";
509 m->mothurOut(outputString); m->mothurOutEndLine();
511 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
512 outputNames.push_back(outputFileNames);
513 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
514 else { outputTypes["accnos"].push_back(outputFileNames); }
517 //if fasta file provided output new fasta file
518 if ((fastafile != "") && wroteSomething) {
519 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
520 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
521 string outputFileFasta = getOutputFileName("fasta", variables);
523 m->openOutputFile(outputFileFasta, outFasta);
524 outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
526 for (int k = 0; k < seqs.size(); k++) {
527 if (m->control_pressed) { outFasta.close(); return 0; }
529 //if this is a sequence we want, output it
530 it = fastaMap.find(seqs[k].getName());
531 if (it != fastaMap.end()) {
533 if (output != "accnos") {
534 outFasta << ">" << it->second << endl;
536 outFasta << ">" << it->first << endl;
539 outFasta << seqs[k].getAligned() << endl;
549 catch(exception& e) {
550 m->errorOut(e, "GetSharedOTUCommand", "process");
554 /***********************************************************/
555 int GetSharedOTUCommand::runShared() {
557 InputData input(sharedfile, "sharedfile");
558 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
559 string lastLabel = lookup[0]->getLabel();
561 if (Groups.size() == 0) {
562 Groups = m->getGroups();
564 //make string for outputfile name
565 userGroups = "unique.";
566 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
567 userGroups = userGroups.substr(0, userGroups.length()-1);
569 //sanity check for group names
571 vector<string> allGroups = m->getAllGroups();
572 util.setGroups(Groups, allGroups);
575 //put groups in map to find easier
576 for(int i = 0; i < Groups.size(); i++) {
577 groupFinder[Groups[i]] = Groups[i];
580 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
581 set<string> processedLabels;
582 set<string> userLabels = labels;
584 //as long as you are not at the end of the file or done wih the lines you want
585 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
586 if (m->control_pressed) {
587 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
588 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
592 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
593 m->mothurOut(lookup[0]->getLabel());
596 processedLabels.insert(lookup[0]->getLabel());
597 userLabels.erase(lookup[0]->getLabel());
600 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
601 string saveLabel = lookup[0]->getLabel();
603 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
604 lookup = input.getSharedRAbundVectors(lastLabel);
606 m->mothurOut(lookup[0]->getLabel());
609 processedLabels.insert(lookup[0]->getLabel());
610 userLabels.erase(lookup[0]->getLabel());
612 //restore real lastlabel to save below
613 lookup[0]->setLabel(saveLabel);
616 lastLabel = lookup[0]->getLabel();
618 //get next line to process
619 //prevent memory leak
620 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
621 lookup = input.getSharedRAbundVectors();
624 if (m->control_pressed) {
625 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
626 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
629 //output error messages about any remaining user labels
630 set<string>::iterator it;
631 bool needToRun = false;
632 for (it = userLabels.begin(); it != userLabels.end(); it++) {
633 m->mothurOut("Your file does not include the label " + *it);
634 if (processedLabels.count(lastLabel) != 1) {
635 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
638 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
642 //run last label if you need to
643 if (needToRun == true) {
644 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
645 lookup = input.getSharedRAbundVectors(lastLabel);
647 m->mothurOut(lookup[0]->getLabel());
649 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
652 //reset groups parameter
658 catch(exception& e) {
659 m->errorOut(e, "GetSharedOTUCommand", "runShared");
663 /***********************************************************/
664 int GetSharedOTUCommand::process(vector<SharedRAbundVector*>& lookup) {
667 string outputFileNames;
668 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
669 map<string, string> variables;
670 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
671 variables["[distance]"] = lookup[0]->getLabel();
672 variables["[group]"] = userGroups;
673 if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
674 else { outputFileNames = getOutputFileName("accnos", variables); }
677 m->openOutputFile(outputFileNames, outNames);
679 bool wroteSomething = false;
682 //go through each bin, find out if shared
683 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
684 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
686 bool uniqueOTU = true;
687 map<string, int> atLeastOne;
688 for (int f = 0; f < Groups.size(); f++) { atLeastOne[Groups[f]] = 0; }
690 set<string> namesOfGroupsInThisBin;
692 for(int j = 0; j < lookup.size(); j++) {
693 string seqGroup = lookup[j]->getGroup();
694 string name = m->currentBinLabels[i];
696 if (lookup[j]->getAbundance(i) != 0) {
697 if (output != "accnos") {
698 namesOfGroupsInThisBin.insert(name + "|" + seqGroup + "|" + toString(lookup[j]->getAbundance(i)));
699 }else { namesOfGroupsInThisBin.insert(name); }
701 //is this seq in one of the groups we care about
702 it = groupFinder.find(seqGroup);
703 if (it == groupFinder.end()) { uniqueOTU = false; } //you have sequences from a group you don't want
704 else { atLeastOne[seqGroup]++; }
708 //make sure you have at least one seq from each group you want
709 bool sharedByAll = true;
710 map<string, int>::iterator it2;
711 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
712 if (it2->second == 0) { sharedByAll = false; }
715 //if the user wants unique bins and this is unique then print
716 //or this the user wants shared bins and this bin is shared then print
717 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
719 wroteSomething = true;
722 //output list of names
723 for (set<string>::iterator itNames = namesOfGroupsInThisBin.begin(); itNames != namesOfGroupsInThisBin.end(); itNames++) {
724 outNames << (*itNames) << endl;
730 if (!wroteSomething) {
731 m->mothurRemove(outputFileNames);
732 string outputString = "\t" + toString(num) + " - No otus shared by groups";
734 string groupString = "";
735 for (int h = 0; h < Groups.size(); h++) {
736 groupString += " " + Groups[h];
739 outputString += groupString + ".";
740 m->mothurOut(outputString); m->mothurOutEndLine();
742 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
743 outputNames.push_back(outputFileNames);
744 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
745 else { outputTypes["accnos"].push_back(outputFileNames); }
750 catch(exception& e) {
751 m->errorOut(e, "GetSharedOTUCommand", "process");
756 //**********************************************************************************************************************