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);
236 if (Groups.size() > 4) { userGroups = "unique.selected_groups"; } //if too many groups then the filename becomes too big.
239 groups = validParameter.validFile(parameters, "sharedgroups", false);
240 if (groups == "not found") { groups = ""; }
243 m->splitAtDash(groups, Groups);
244 if (Groups.size() > 4) { userGroups = "selected_groups"; } //if too many groups then the filename becomes too big.
251 catch(exception& e) {
252 m->errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
256 //**********************************************************************************************************************
258 int GetSharedOTUCommand::execute(){
261 if (abort == true) { if (calledHelp) { return 0; } return 2; }
263 if ( sharedfile != "") { runShared(); }
265 m->setGroups(Groups);
266 groupMap = new GroupMap(groupfile);
267 int error = groupMap->readMap();
268 if (error == 1) { delete groupMap; return 0; }
270 if (m->control_pressed) { delete groupMap; return 0; }
272 if (Groups.size() == 0) {
273 Groups = groupMap->getNamesOfGroups();
275 //make string for outputfile name
276 userGroups = "unique.";
277 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
278 userGroups = userGroups.substr(0, userGroups.length()-1);
279 if (Groups.size() > 4) { userGroups = "unique.selected_groups"; } //if too many groups then the filename becomes too big.
281 //sanity check for group names
283 vector<string> namesOfGroups = groupMap->getNamesOfGroups();
284 util.setGroups(Groups, namesOfGroups);
285 groupMap->setNamesOfGroups(namesOfGroups);
288 //put groups in map to find easier
289 for(int i = 0; i < Groups.size(); i++) {
290 groupFinder[Groups[i]] = Groups[i];
293 if (fastafile != "") {
295 m->openInputFile(fastafile, inFasta);
297 while(!inFasta.eof()) {
298 if (m->control_pressed) { outputTypes.clear(); inFasta.close(); delete groupMap; return 0; }
300 Sequence seq(inFasta); m->gobble(inFasta);
301 if (seq.getName() != "") { seqs.push_back(seq); }
306 ListVector* lastlist = NULL;
307 string lastLabel = "";
309 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
310 set<string> processedLabels;
311 set<string> userLabels = labels;
314 m->openInputFile(listfile, in);
316 //as long as you are not at the end of the file or done wih the lines you want
317 while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
319 if (m->control_pressed) {
320 if (lastlist != NULL) { delete lastlist; }
321 for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear();
322 delete groupMap; return 0;
325 list = new ListVector(in);
327 if(allLines == 1 || labels.count(list->getLabel()) == 1){
328 m->mothurOut(list->getLabel());
331 processedLabels.insert(list->getLabel());
332 userLabels.erase(list->getLabel());
335 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
336 string saveLabel = list->getLabel();
338 m->mothurOut(lastlist->getLabel());
341 processedLabels.insert(lastlist->getLabel());
342 userLabels.erase(lastlist->getLabel());
344 //restore real lastlabel to save below
345 list->setLabel(saveLabel);
348 lastLabel = list->getLabel();
350 if (lastlist != NULL) { delete lastlist; }
356 //output error messages about any remaining user labels
357 set<string>::iterator it;
358 bool needToRun = false;
359 for (it = userLabels.begin(); it != userLabels.end(); it++) {
360 m->mothurOut("Your file does not include the label " + *it);
361 if (processedLabels.count(lastLabel) != 1) {
362 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
365 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
369 //run last label if you need to
370 if (needToRun == true) {
371 m->mothurOut(lastlist->getLabel());
374 processedLabels.insert(lastlist->getLabel());
375 userLabels.erase(lastlist->getLabel());
379 //reset groups parameter
382 if (lastlist != NULL) { delete lastlist; }
384 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete groupMap; return 0; }
386 //set fasta file as new current fastafile
388 itTypes = outputTypes.find("fasta");
389 if (itTypes != outputTypes.end()) {
390 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
393 if (output == "accnos") {
394 itTypes = outputTypes.find("accnos");
395 if (itTypes != outputTypes.end()) {
396 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
400 m->mothurOutEndLine();
401 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
402 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
403 m->mothurOutEndLine();
409 catch(exception& e) {
410 m->errorOut(e, "GetSharedOTUCommand", "execute");
414 /***********************************************************/
415 int GetSharedOTUCommand::process(ListVector* shared) {
418 map<string, string> fastaMap;
421 string outputFileNames;
423 if (outputDir == "") { outputDir += m->hasPath(listfile); }
424 map<string, string> variables;
425 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(listfile));
426 variables["[distance]"] = shared->getLabel();
427 variables["[group]"] = userGroups;
428 if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
429 else { outputFileNames = getOutputFileName("accnos", variables); }
431 m->openOutputFile(outputFileNames, outNames);
433 bool wroteSomething = false;
436 //go through each bin, find out if shared
437 vector<string> binLabels = shared->getLabels();
438 for (int i = 0; i < shared->getNumBins(); i++) {
439 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
441 bool uniqueOTU = true;
443 map<string, int> atLeastOne;
444 for (int f = 0; f < Groups.size(); f++) {
445 atLeastOne[Groups[f]] = 0;
448 vector<string> namesOfSeqsInThisBin;
450 string names = shared->get(i);
451 vector<string> binNames;
452 m->splitAtComma(names, binNames);
453 for(int j = 0; j < binNames.size(); j++) {
454 string name = binNames[j];
457 string seqGroup = groupMap->getGroup(name);
458 if (output != "accnos") {
459 namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + binLabels[i]));
460 }else { namesOfSeqsInThisBin.push_back(name); }
462 if (seqGroup == "not found") { m->mothurOut(name + " is not in your groupfile. Please correct."); m->mothurOutEndLine(); exit(1); }
464 //is this seq in one of hte groups we care about
465 it = groupFinder.find(seqGroup);
466 if (it == groupFinder.end()) { uniqueOTU = false; } //you have a sequence from a group you don't want
467 else { atLeastOne[seqGroup]++; }
470 //make sure you have at least one seq from each group you want
471 bool sharedByAll = true;
472 map<string, int>::iterator it2;
473 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
474 if (it2->second == 0) { sharedByAll = false; }
477 //if the user wants unique bins and this is unique then print
478 //or this the user wants shared bins and this bin is shared then print
479 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
481 wroteSomething = true;
484 //output list of names
485 for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
486 outNames << namesOfSeqsInThisBin[j] << endl;
488 if (fastafile != "") {
489 if (output != "accnos") {
490 string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
491 seqName = seqName.substr(0,seqName.find_last_of('|'));
492 fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
494 fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
503 if (!wroteSomething) {
504 m->mothurRemove(outputFileNames);
505 string outputString = "\t" + toString(num) + " - No otus shared by groups";
507 string groupString = "";
508 for (int h = 0; h < Groups.size(); h++) {
509 groupString += " " + Groups[h];
512 outputString += groupString + ".";
513 m->mothurOut(outputString); m->mothurOutEndLine();
515 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
516 outputNames.push_back(outputFileNames);
517 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
518 else { outputTypes["accnos"].push_back(outputFileNames); }
521 //if fasta file provided output new fasta file
522 if ((fastafile != "") && wroteSomething) {
523 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
524 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
525 string outputFileFasta = getOutputFileName("fasta", variables);
527 m->openOutputFile(outputFileFasta, outFasta);
528 outputNames.push_back(outputFileFasta); outputTypes["fasta"].push_back(outputFileFasta);
530 for (int k = 0; k < seqs.size(); k++) {
531 if (m->control_pressed) { outFasta.close(); return 0; }
533 //if this is a sequence we want, output it
534 it = fastaMap.find(seqs[k].getName());
535 if (it != fastaMap.end()) {
537 if (output != "accnos") {
538 outFasta << ">" << it->second << endl;
540 outFasta << ">" << it->first << endl;
543 outFasta << seqs[k].getAligned() << endl;
553 catch(exception& e) {
554 m->errorOut(e, "GetSharedOTUCommand", "process");
558 /***********************************************************/
559 int GetSharedOTUCommand::runShared() {
561 InputData input(sharedfile, "sharedfile");
562 vector<SharedRAbundVector*> lookup = input.getSharedRAbundVectors();
563 string lastLabel = lookup[0]->getLabel();
565 if (Groups.size() == 0) {
566 Groups = m->getGroups();
568 //make string for outputfile name
569 userGroups = "unique.";
570 for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; }
571 userGroups = userGroups.substr(0, userGroups.length()-1);
572 if (Groups.size() > 4) { userGroups = "unique.selected_groups"; } //if too many groups then the filename becomes too big.
574 //sanity check for group names
576 vector<string> allGroups = m->getAllGroups();
577 util.setGroups(Groups, allGroups);
580 //put groups in map to find easier
581 for(int i = 0; i < Groups.size(); i++) {
582 groupFinder[Groups[i]] = Groups[i];
585 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
586 set<string> processedLabels;
587 set<string> userLabels = labels;
589 //as long as you are not at the end of the file or done wih the lines you want
590 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
591 if (m->control_pressed) {
592 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
593 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
597 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
598 m->mothurOut(lookup[0]->getLabel());
601 processedLabels.insert(lookup[0]->getLabel());
602 userLabels.erase(lookup[0]->getLabel());
605 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
606 string saveLabel = lookup[0]->getLabel();
608 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
609 lookup = input.getSharedRAbundVectors(lastLabel);
611 m->mothurOut(lookup[0]->getLabel());
614 processedLabels.insert(lookup[0]->getLabel());
615 userLabels.erase(lookup[0]->getLabel());
617 //restore real lastlabel to save below
618 lookup[0]->setLabel(saveLabel);
621 lastLabel = lookup[0]->getLabel();
623 //get next line to process
624 //prevent memory leak
625 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
626 lookup = input.getSharedRAbundVectors();
629 if (m->control_pressed) {
630 outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }
631 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0;
634 //output error messages about any remaining user labels
635 set<string>::iterator it;
636 bool needToRun = false;
637 for (it = userLabels.begin(); it != userLabels.end(); it++) {
638 m->mothurOut("Your file does not include the label " + *it);
639 if (processedLabels.count(lastLabel) != 1) {
640 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
643 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
647 //run last label if you need to
648 if (needToRun == true) {
649 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
650 lookup = input.getSharedRAbundVectors(lastLabel);
652 m->mothurOut(lookup[0]->getLabel());
654 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
657 //reset groups parameter
663 catch(exception& e) {
664 m->errorOut(e, "GetSharedOTUCommand", "runShared");
668 /***********************************************************/
669 int GetSharedOTUCommand::process(vector<SharedRAbundVector*>& lookup) {
672 string outputFileNames;
673 if (outputDir == "") { outputDir += m->hasPath(sharedfile); }
674 map<string, string> variables;
675 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
676 variables["[distance]"] = lookup[0]->getLabel();
677 variables["[group]"] = userGroups;
678 if (output != "accnos") { outputFileNames = getOutputFileName("sharedseqs", variables); }
679 else { outputFileNames = getOutputFileName("accnos", variables); }
682 m->openOutputFile(outputFileNames, outNames);
684 bool wroteSomething = false;
687 //go through each bin, find out if shared
688 for (int i = 0; i < lookup[0]->getNumBins(); i++) {
689 if (m->control_pressed) { outNames.close(); m->mothurRemove(outputFileNames); return 0; }
691 bool uniqueOTU = true;
692 map<string, int> atLeastOne;
693 for (int f = 0; f < Groups.size(); f++) { atLeastOne[Groups[f]] = 0; }
695 set<string> namesOfGroupsInThisBin;
697 for(int j = 0; j < lookup.size(); j++) {
698 string seqGroup = lookup[j]->getGroup();
699 string name = m->currentSharedBinLabels[i];
701 if (lookup[j]->getAbundance(i) != 0) {
702 if (output != "accnos") {
703 namesOfGroupsInThisBin.insert(name + "|" + seqGroup + "|" + toString(lookup[j]->getAbundance(i)));
704 }else { namesOfGroupsInThisBin.insert(name); }
706 //is this seq in one of the groups we care about
707 it = groupFinder.find(seqGroup);
708 if (it == groupFinder.end()) { uniqueOTU = false; } //you have sequences from a group you don't want
709 else { atLeastOne[seqGroup]++; }
713 //make sure you have at least one seq from each group you want
714 bool sharedByAll = true;
715 map<string, int>::iterator it2;
716 for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
717 if (it2->second == 0) { sharedByAll = false; }
720 //if the user wants unique bins and this is unique then print
721 //or this the user wants shared bins and this bin is shared then print
722 if ((unique && uniqueOTU && sharedByAll) || (!unique && sharedByAll)) {
724 wroteSomething = true;
727 //output list of names
728 for (set<string>::iterator itNames = namesOfGroupsInThisBin.begin(); itNames != namesOfGroupsInThisBin.end(); itNames++) {
729 outNames << (*itNames) << endl;
735 if (!wroteSomething) {
736 m->mothurRemove(outputFileNames);
737 string outputString = "\t" + toString(num) + " - No otus shared by groups";
739 string groupString = "";
740 for (int h = 0; h < Groups.size(); h++) {
741 groupString += " " + Groups[h];
744 outputString += groupString + ".";
745 m->mothurOut(outputString); m->mothurOutEndLine();
747 m->mothurOut("\t" + toString(num)); m->mothurOutEndLine();
748 outputNames.push_back(outputFileNames);
749 if (output != "accnos") { outputTypes["sharedseqs"].push_back(outputFileNames); }
750 else { outputTypes["accnos"].push_back(outputFileNames); }
755 catch(exception& e) {
756 m->errorOut(e, "GetSharedOTUCommand", "process");
761 //**********************************************************************************************************************