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 vector<string> binLabels = shared->getLabels();
435 for (int i = 0; i < shared->getNumBins(); i++) {
436 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
438 bool uniqueOTU = true;
440 map<string, int> atLeastOne;
441 for (int f = 0; f < Groups.size(); f++) {
442 atLeastOne[Groups[f]] = 0;
445 vector<string> namesOfSeqsInThisBin;
447 string names = shared->get(i);
448 vector<string> binNames;
449 m->splitAtComma(names, binNames);
450 for(int j = 0; j < binNames.size(); j++) {
451 string name = binNames[j];
454 string seqGroup = groupMap->getGroup(name);
455 if (output != "accnos") {
456 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + binLabels[i]));
457 }else { namesOfSeqsInThisBin.push_back(name); }
459 if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
461 //is this seq in one of hte groups we care about
462 it = groupFinder.find(seqGroup);
463 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
464 else { atLeastOne[seqGroup]++; }
467 //make sure you have at least one seq from each group you want
468 bool sharedByAll = true;
469 map<string, int>::iterator it2;
470 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
471 if (it2->second == 0) { sharedByAll = false; }
474 //if the user wants unique bins and this is unique then print
475 //or this the user wants shared bins and this bin is shared then print
476 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
478 wroteSomething = true;
481 //output list of names
482 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
483 outNames << namesOfSeqsInThisBin[j] << endl;
485 if (fastafile != "") {
486 if (output != "accnos") {
487 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
488 seqName = seqName.substr(0,seqName.find_last_of('|'));
489 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
491 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
500 if (!wroteSomething) {
501 m->mothurRemove(outputFileNames);
502 string outputString = "\t" + toString(num) + " - No otus shared by groups";
504 string groupString = "";
505 for (int h = 0; h < Groups.size(); h++) {
506 groupString += " " + Groups[h];
509 outputString += groupString + ".";
510 m->mothurOut(outputString); m->mothurOutEndLine();
512 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
513 outputNames.push_back(outputFileNames);
514 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
515 else { outputTypes["accnos"].push_back(outputFileNames); }
518 //if fasta file provided output new fasta file
519 if ((fastafile != "") && wroteSomething) {
520 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
521 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
522 string outputFileFasta = getOutputFileName("fasta", variables);
524 m->openOutputFile(outputFileFasta, outFasta);
525 outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
527 for (int k = 0; k < seqs.size(); k++) {
528 if (m->control_pressed) { outFasta.close(); return 0; }
530 //if this is a sequence we want, output it
531 it = fastaMap.find(seqs[k].getName());
532 if (it != fastaMap.end()) {
534 if (output != "accnos") {
535 outFasta << ">" << it->second << endl;
537 outFasta << ">" << it->first << endl;
540 outFasta << seqs[k].getAligned() << endl;
550 catch(exception& e) {
551 m->errorOut(e, "GetSharedOTUCommand", "process");
555 /***********************************************************/
556 int GetSharedOTUCommand::runShared() {
558 InputData input(sharedfile, "sharedfile");
559 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
560 string lastLabel = lookup[0]->getLabel();
562 if (Groups.size() == 0) {
563 Groups = m->getGroups();
565 //make string for outputfile name
566 userGroups = "unique.";
567 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
568 userGroups = userGroups.substr(0, userGroups.length()-1);
570 //sanity check for group names
572 vector<string> allGroups = m->getAllGroups();
573 util.setGroups(Groups, allGroups);
576 //put groups in map to find easier
577 for(int i = 0; i < Groups.size(); i++) {
578 groupFinder[Groups[i]] = Groups[i];
581 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
582 set<string> processedLabels;
583 set<string> userLabels = labels;
585 //as long as you are not at the end of the file or done wih the lines you want
586 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
587 if (m->control_pressed) {
588 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
589 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
593 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
594 m->mothurOut(lookup[0]->getLabel());
597 processedLabels.insert(lookup[0]->getLabel());
598 userLabels.erase(lookup[0]->getLabel());
601 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
602 string saveLabel = lookup[0]->getLabel();
604 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
605 lookup = input.getSharedRAbundVectors(lastLabel);
607 m->mothurOut(lookup[0]->getLabel());
610 processedLabels.insert(lookup[0]->getLabel());
611 userLabels.erase(lookup[0]->getLabel());
613 //restore real lastlabel to save below
614 lookup[0]->setLabel(saveLabel);
617 lastLabel = lookup[0]->getLabel();
619 //get next line to process
620 //prevent memory leak
621 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
622 lookup = input.getSharedRAbundVectors();
625 if (m->control_pressed) {
626 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
627 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
630 //output error messages about any remaining user labels
631 set<string>::iterator it;
632 bool needToRun = false;
633 for (it = userLabels.begin(); it != userLabels.end(); it++) {
634 m->mothurOut("Your file does not include the label " + *it);
635 if (processedLabels.count(lastLabel) != 1) {
636 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
639 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
643 //run last label if you need to
644 if (needToRun == true) {
645 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
646 lookup = input.getSharedRAbundVectors(lastLabel);
648 m->mothurOut(lookup[0]->getLabel());
650 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
653 //reset groups parameter
659 catch(exception& e) {
660 m->errorOut(e, "GetSharedOTUCommand", "runShared");
664 /***********************************************************/
665 int GetSharedOTUCommand::process(vector<SharedRAbundVector*>& lookup) {
668 string outputFileNames;
669 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
670 map<string, string> variables;
671 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
672 variables["[distance]"] = lookup[0]->getLabel();
673 variables["[group]"] = userGroups;
674 if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
675 else { outputFileNames = getOutputFileName("accnos", variables); }
678 m->openOutputFile(outputFileNames, outNames);
680 bool wroteSomething = false;
683 //go through each bin, find out if shared
684 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
685 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
687 bool uniqueOTU = true;
688 map<string, int> atLeastOne;
689 for (int f = 0; f < Groups.size(); f++) { atLeastOne[Groups[f]] = 0; }
691 set<string> namesOfGroupsInThisBin;
693 for(int j = 0; j < lookup.size(); j++) {
694 string seqGroup = lookup[j]->getGroup();
695 string name = m->currentSharedBinLabels[i];
697 if (lookup[j]->getAbundance(i) != 0) {
698 if (output != "accnos") {
699 namesOfGroupsInThisBin.insert(name + "|" + seqGroup + "|" + toString(lookup[j]->getAbundance(i)));
700 }else { namesOfGroupsInThisBin.insert(name); }
702 //is this seq in one of the groups we care about
703 it = groupFinder.find(seqGroup);
704 if (it == groupFinder.end()) { uniqueOTU = false; } //you have sequences from a group you don't want
705 else { atLeastOne[seqGroup]++; }
709 //make sure you have at least one seq from each group you want
710 bool sharedByAll = true;
711 map<string, int>::iterator it2;
712 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
713 if (it2->second == 0) { sharedByAll = false; }
716 //if the user wants unique bins and this is unique then print
717 //or this the user wants shared bins and this bin is shared then print
718 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
720 wroteSomething = true;
723 //output list of names
724 for (set<string>::iterator itNames = namesOfGroupsInThisBin.begin(); itNames != namesOfGroupsInThisBin.end(); itNames++) {
725 outNames << (*itNames) << endl;
731 if (!wroteSomething) {
732 m->mothurRemove(outputFileNames);
733 string outputString = "\t" + toString(num) + " - No otus shared by groups";
735 string groupString = "";
736 for (int h = 0; h < Groups.size(); h++) {
737 groupString += " " + Groups[h];
740 outputString += groupString + ".";
741 m->mothurOut(outputString); m->mothurOutEndLine();
743 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
744 outputNames.push_back(outputFileNames);
745 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
746 else { outputTypes["accnos"].push_back(outputFileNames); }
751 catch(exception& e) {
752 m->errorOut(e, "GetSharedOTUCommand", "process");
757 //**********************************************************************************************************************