--- /dev/null
+/*
+ * getsharedotucommand.cpp
+ * Mothur
+ *
+ * Created by westcott on 9/22/09.
+ * Copyright 2009 Schloss Lab. All rights reserved.
+ *
+ */
+
+#include "getsharedotucommand.h"
+
+//**********************************************************************************************************************
+
+GetSharedOTUCommand::GetSharedOTUCommand(string option){
+ try {
+
+ globaldata = GlobalData::getInstance();
+ abort = false;
+ allLines = 1;
+ labels.clear();
+
+ //allow user to run help
+ if(option == "help") { help(); abort = true; }
+
+ else {
+ //valid paramters for this command
+ string Array[] = {"label","groups","fasta","list","group","output"};
+ vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+
+ OptionParser parser(option);
+ map<string,string> parameters = parser.getParameters();
+
+ ValidParameters validParameter;
+
+ //check to make sure all parameters are valid for command
+ for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
+ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
+ }
+
+ //check for required parameters
+ listfile = validParameter.validFile(parameters, "list", true);
+ if (listfile == "not open") { abort = true; }
+ else if (listfile == "not found") { listfile = ""; }
+ else { globaldata->setListFile(listfile); globaldata->setFormat("list"); }
+
+ groupfile = validParameter.validFile(parameters, "group", true);
+ if (groupfile == "not open") { abort = true; }
+ else if (groupfile == "not found") { groupfile = ""; }
+
+ if ((listfile == "") || (groupfile == "")) { mothurOut("The list and group parameters are required."); mothurOutEndLine(); abort = true; }
+
+ //check for optional parameter and set defaults
+ // ...at some point should added some additional type checking...
+ label = validParameter.validFile(parameters, "label", false);
+ if (label == "not found") { label = ""; }
+ else {
+ if(label != "all") { splitAtDash(label, labels); allLines = 0; }
+ else { allLines = 1; }
+ }
+
+ output = validParameter.validFile(parameters, "output", false);
+ if (output == "not found") { output = ""; }
+
+ groups = validParameter.validFile(parameters, "groups", false);
+ if (groups == "not found") { groups = ""; }
+ else {
+ splitAtDash(groups, Groups);
+ globaldata->Groups = Groups;
+ }
+
+ fastafile = validParameter.validFile(parameters, "fasta", true);
+ if (fastafile == "not open") { abort = true; }
+ else if (fastafile == "not found") { fastafile = ""; }
+
+ }
+
+ }
+ catch(exception& e) {
+ errorOut(e, "GetSharedOTUCommand", "GetSharedOTUCommand");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+
+void GetSharedOTUCommand::help(){
+ try {
+ mothurOut("The get.sharedotu command parameters are list, group, label, groups, output and fasta. The list and group parameters are required.\n");
+ mothurOut("The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n");
+ mothurOut("The groups parameter allows you to select groups you would like to know the shared info for, and are separated by dashes.\n");
+ mothurOut("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.\n");
+ mothurOut("The output parameter allows you to output the list of names without the group and bin number added. \n");
+ mothurOut("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");
+ mothurOut("The get.sharedotu command outputs a .names file for each distance level containing a list of sequences in the OTUs shared by the groups specified.\n");
+ mothurOut("The get.sharedotu command should be in the following format: get.sabund(label=yourLabels, groups=yourGroups, fasta=yourFastafile, output=yourOutput).\n");
+ mothurOut("Example get.sharedotu(label=unique-0.01, group=forest-pasture, fasta=amazon.fasta, output=accnos).\n");
+ mothurOut("The default value for label is all labels in your inputfile. The default for groups is all groups in your file.\n");
+ mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
+ }
+ catch(exception& e) {
+ errorOut(e, "GetSharedOTUCommand", "help");
+ exit(1);
+ }
+}
+
+//**********************************************************************************************************************
+
+GetSharedOTUCommand::~GetSharedOTUCommand(){}
+
+//**********************************************************************************************************************
+
+int GetSharedOTUCommand::execute(){
+ try {
+
+ if (abort == true) { return 0; }
+
+ groupMap = new GroupMap(groupfile);
+ groupMap->readMap();
+ globaldata->gGroupmap = groupMap;
+
+ if (Groups.size() == 0) {
+ Groups = groupMap->namesOfGroups;
+ }
+
+ //put groups in map to find easier
+ for(int i = 0; i < Groups.size(); i++) {
+ groupFinder[Groups[i]] = Groups[i];
+ }
+
+ if (fastafile != "") {
+ ifstream inFasta;
+ openInputFile(fastafile, inFasta);
+
+ while(!inFasta.eof()) {
+ Sequence seq(inFasta);
+ seqs.push_back(seq);
+ }
+ inFasta.close();
+ }
+
+ ListVector* lastlist = NULL;
+ string lastLabel = "";
+
+ //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;
+
+ ifstream in;
+ openInputFile(listfile, in);
+
+ //as long as you are not at the end of the file or done wih the lines you want
+ while((!in.eof()) && ((allLines == 1) || (userLabels.size() != 0))) {
+
+ list = new ListVector(in);
+
+ if(allLines == 1 || labels.count(list->getLabel()) == 1){
+ mothurOut(list->getLabel());
+ process(list);
+
+ processedLabels.insert(list->getLabel());
+ userLabels.erase(list->getLabel());
+ }
+
+ if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+
+ mothurOut(lastlist->getLabel());
+ process(lastlist);
+
+ processedLabels.insert(lastlist->getLabel());
+ userLabels.erase(lastlist->getLabel());
+ }
+
+ lastLabel = list->getLabel();
+
+ if (lastlist != NULL) { delete lastlist; }
+ lastlist = list;
+ }
+
+ in.close();
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ mothurOut("Your file does not include the label " + *it);
+ if (processedLabels.count(lastLabel) != 1) {
+ mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
+ needToRun = true;
+ }else {
+ mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ mothurOut(lastlist->getLabel());
+ process(lastlist);
+
+ processedLabels.insert(lastlist->getLabel());
+ userLabels.erase(lastlist->getLabel());
+ }
+
+
+ //reset groups parameter
+ globaldata->Groups.clear();
+
+ if (lastlist != NULL) { delete lastlist; }
+ return 0;
+ }
+
+ catch(exception& e) {
+ errorOut(e, "GetSharedOTUCommand", "execute");
+ exit(1);
+ }
+}
+/***********************************************************/
+void GetSharedOTUCommand::process(ListVector* shared) {
+ try {
+
+ map<string, string> fastaMap;
+
+ ofstream outNames;
+ string outputFileNames = getRootName(listfile) + shared->getLabel() + ".names";
+ openOutputFile(outputFileNames, outNames);
+
+ bool wroteSomething = false;
+
+ //go through each bin, find out if shared
+ for (int i = 0; i < shared->getNumBins(); i++) {
+
+ bool sharedByAll = true;
+
+ map<string, int> atLeastOne;
+ for (int m = 0; m < Groups.size(); m++) {
+ atLeastOne[Groups[m]] = 0;
+ }
+
+ vector<string> namesOfSeqsInThisBin;
+
+ string names = shared->get(i);
+ while ((names.find_first_of(',') != -1) && sharedByAll) {
+ string name = names.substr(0,names.find_first_of(','));
+ names = names.substr(names.find_first_of(',')+1, names.length());
+
+ //find group
+ string seqGroup = groupMap->getGroup(name);
+ if (output != "accnos") {
+ namesOfSeqsInThisBin.push_back((name + "|" + seqGroup + "|" + toString(i)));
+ }else { namesOfSeqsInThisBin.push_back(name); }
+
+ if (seqGroup == "not found") { mothurOut(name + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
+
+ //is this seq in one of hte groups we care about
+ it = groupFinder.find(seqGroup);
+ if (it == groupFinder.end()) { sharedByAll = false; } //you have a sequence from a group you don't want
+ else { atLeastOne[seqGroup]++; }
+ }
+
+ //get last name
+ //find group
+ if (sharedByAll) {
+ string seqGroup = groupMap->getGroup(names);
+ if (output != "accnos") {
+ namesOfSeqsInThisBin.push_back((names + "|" + seqGroup + "|" + toString(i)));
+ }else { namesOfSeqsInThisBin.push_back(names); }
+
+ if (seqGroup == "not found") { mothurOut(names + " is not in your groupfile. Please correct."); mothurOutEndLine(); exit(1); }
+
+ //is this seq in one of hte groups we care about
+ it = groupFinder.find(seqGroup);
+ if (it == groupFinder.end()) { sharedByAll = false; } //you have a sequence from a group you don't want
+ else { atLeastOne[seqGroup]++; }
+ }
+
+ //make sure you have at least one seq from each group you want
+ map<string, int>::iterator it2;
+ for (it2 = atLeastOne.begin(); it2 != atLeastOne.end(); it2++) {
+ if (it2->second == 0) { sharedByAll = false; }
+ }
+
+ //if shared, save names of seqs in that bin
+ if (sharedByAll) {
+
+ wroteSomething = true;
+
+ //output list of names
+ for (int j = 0; j < namesOfSeqsInThisBin.size(); j++) {
+ outNames << namesOfSeqsInThisBin[j] << endl;
+
+ if (fastafile != "") {
+ if (output != "accnos") {
+ string seqName = namesOfSeqsInThisBin[j].substr(0,namesOfSeqsInThisBin[j].find_last_of('|'));
+ seqName = seqName.substr(0,seqName.find_last_of('|'));
+ fastaMap[seqName] = namesOfSeqsInThisBin[j]; //fastaMap needs to contain just the seq name for output later
+ }else {
+ fastaMap[namesOfSeqsInThisBin[j]] = namesOfSeqsInThisBin[j];
+ }
+ }
+ }
+ }
+
+
+
+ }
+
+ outNames.close();
+
+ if (!wroteSomething) {
+ remove(outputFileNames.c_str());
+ string outputString = " - No otus shared by groups";
+
+ string groupString = "";
+ for (int h = 0; h < Groups.size(); h++) {
+ groupString += " " + Groups[h];
+ }
+
+ outputString += groupString + ".";
+ mothurOut(outputString); mothurOutEndLine();
+ }else { mothurOutEndLine(); }
+
+ //if fasta file provided output new fasta file
+ if ((fastafile != "") && wroteSomething) {
+ string outputFileFasta = getRootName(fastafile) + shared->getLabel() + ".fasta";
+ ofstream outFasta;
+ openOutputFile(outputFileFasta, outFasta);
+
+ for (int k = 0; k < seqs.size(); k++) {
+ //if this is a sequence we want, output it
+ it = fastaMap.find(seqs[k].getName());
+ if (it != fastaMap.end()) {
+
+ if (output != "accnos") {
+ outFasta << ">" << it->second << endl;
+ }else {
+ outFasta << ">" << it->first << endl;
+ }
+
+ outFasta << seqs[k].getAligned() << endl;
+ }
+ }
+
+ outFasta.close();
+ }
+
+
+ }
+ catch(exception& e) {
+ errorOut(e, "GetSharedOTUCommand", "process");
+ exit(1);
+ }
+}
+
+//**********************************************************************************************************************
outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(408 - ((int)toString(sharedBC[0] - sharedABC).length() / 2)) + "\" y=\"305\">" + toString(sharedBC[0] - sharedABC) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedABC).length() / 2)) + "\" y=\"280\">" + toString(sharedABC) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"660\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedAB[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"680\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedAC[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"700\">The number of sepecies shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedBC[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"720\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and combined groups " + lookup[1]->getGroup() + lookup[2]->getGroup() + " is " + toString(sharedAwithBC[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"740\">The number of sepecies shared between groups " + lookup[1]->getGroup() + " and combined groups " + lookup[0]->getGroup() + lookup[2]->getGroup() + " is " + toString(sharedBwithAC[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"760\">The number of sepecies shared between groups " + lookup[2]->getGroup() + " and combined groups " + lookup[0]->getGroup() + lookup[1]->getGroup() + " is " + toString(sharedCwithAB[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"660\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedAB[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"680\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedAC[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"700\">The number of species shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedBC[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"720\">The number of species shared between groups " + lookup[0]->getGroup() + " and combined groups " + lookup[1]->getGroup() + lookup[2]->getGroup() + " is " + toString(sharedAwithBC[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"740\">The number of species shared between groups " + lookup[1]->getGroup() + " and combined groups " + lookup[0]->getGroup() + lookup[2]->getGroup() + " is " + toString(sharedBwithAC[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"760\">The number of species shared between groups " + lookup[2]->getGroup() + " and combined groups " + lookup[0]->getGroup() + lookup[1]->getGroup() + " is " + toString(sharedCwithAB[0]) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"580\">The number of species in group " + lookup[0]->getGroup() + " is " + toString(numA[0]);
if (numA.size() == 3) {
outsvg << " the lci is " + toString(numA[1]) + " and the hci is " + toString(numA[2]) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(408 - ((int)toString(sharedbc[0] - sharedabc[0]).length() / 2)) + "\" y=\"305\">" + toString(sharedbc[0] - sharedabc[0]) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"" + toString(343 - ((int)toString(sharedabc[0]).length() / 2)) + "\" y=\"280\">" + toString(sharedabc[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"660\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedab[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"680\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedac[0]) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"700\">The number of sepecies shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedbc[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"660\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedab[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"680\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedac[0]) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"700\">The number of species shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedbc[0]) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"580\">The number of species in group " + lookup[0]->getGroup() + " is " + toString(numA[0]);
if (numA.size() == 3) {
outsvg << " the lci is " + toString(numA[1]) + " and the hci is " + toString(numA[2]) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"530\">The number of species in group " + lookup[2]->getGroup() + " is " + toString(numC) + "</text>\n";
outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"550\">The number of species in group " + lookup[3]->getGroup() + " is " + toString(numD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"570\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedAB) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"590\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedAC) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"610\">The number of sepecies shared between groups " + lookup[0]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedAD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"630\">The number of sepecies shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedBC) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"650\">The number of sepecies shared between groups " + lookup[1]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedBD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"670\">The number of sepecies shared between groups " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedCD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"570\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[1]->getGroup() + " is " + toString(sharedAB) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"590\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedAC) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"610\">The number of species shared between groups " + lookup[0]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedAD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"630\">The number of species shared between groups " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedBC) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"650\">The number of species shared between groups " + lookup[1]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedBD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"670\">The number of species shared between groups " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedCD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"690\">The number of sepecies shared between groups " + lookup[0]->getGroup() + ", " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedABC) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"710\">The number of sepecies shared between groups " + lookup[0]->getGroup() + ", " + lookup[1]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedABD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"730\">The number of sepecies shared between groups " + lookup[0]->getGroup() + ", " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedACD) + "</text>\n";
- outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"750\">The number of sepecies shared between groups " + lookup[1]->getGroup() + ", " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedBCD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"690\">The number of species shared between groups " + lookup[0]->getGroup() + ", " + lookup[1]->getGroup() + " and " + lookup[2]->getGroup() + " is " + toString(sharedABC) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"710\">The number of species shared between groups " + lookup[0]->getGroup() + ", " + lookup[1]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedABD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"730\">The number of species shared between groups " + lookup[0]->getGroup() + ", " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedACD) + "</text>\n";
+ outsvg << "<text fill=\"black\" class=\"seri\" x=\"175\" y=\"750\">The number of species shared between groups " + lookup[1]->getGroup() + ", " + lookup[2]->getGroup() + " and " + lookup[3]->getGroup() + " is " + toString(sharedBCD) + "</text>\n";
//make adjustments
sharedABC = sharedABC - sharedABCD;