+}
+//**********************************************************************************************************************
+string SharedCommand::getHelpString(){
+ try {
+ string helpString = "";
+ helpString += "The make.shared command reads a list and group file and creates a shared file, as well as a rabund file for each group.\n";
+ helpString += "The make.shared command parameters are list, group, ordergroup, groups and label. list and group are required unless a current file is available.\n";
+ helpString += "The groups parameter allows you to indicate which groups you want to include, group names should be separated by dashes. ex. groups=A-B-C. Default is all groups in your groupfile.\n";
+ helpString += "The label parameter allows you to indicate which labels you want to include, label names should be separated by dashes. Default is all labels in your list file.\n";
+ helpString += "The ordergroup parameter allows you to indicate the order of the groups in the sharedfile, by default the groups are listed alphabetically.\n";
+ return helpString;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "getHelpString");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+SharedCommand::SharedCommand(){
+ try {
+ abort = true; calledHelp = true;
+ setParameters();
+ //initialize outputTypes
+ vector<string> tempOutNames;
+ outputTypes["rabund"] = tempOutNames;
+ outputTypes["shared"] = tempOutNames;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "SharedCommand");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+SharedCommand::SharedCommand(string option) {
+ try {
+ abort = false; calledHelp = false;
+ allLines = 1;
+
+ //allow user to run help
+ if(option == "help") { help(); abort = true; calledHelp = true; }
+
+ else {
+
+ vector<string> myArray = setParameters();
+
+ OptionParser parser(option);
+ map<string, string> parameters = parser.getParameters();
+
+ ValidParameters validParameter;
+ map<string, string>::iterator it;
+
+ //check to make sure all parameters are valid for command
+ for (it = parameters.begin(); it != parameters.end(); it++) {
+ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
+ }
+
+ //if the user changes the input directory command factory will send this info to us in the output parameter
+ string inputDir = validParameter.validFile(parameters, "inputdir", false);
+ if (inputDir == "not found"){ inputDir = ""; }
+ else {
+ string path;
+ it = parameters.find("list");
+ //user has given a template file
+ if(it != parameters.end()){
+ path = m->hasPath(it->second);
+ //if the user has not given a path then, add inputdir. else leave path alone.
+ if (path == "") { parameters["list"] = inputDir + it->second; }
+ }
+
+ it = parameters.find("group");
+ //user has given a template file
+ if(it != parameters.end()){
+ path = m->hasPath(it->second);
+ //if the user has not given a path then, add inputdir. else leave path alone.
+ if (path == "") { parameters["group"] = inputDir + it->second; }
+ }
+
+ it = parameters.find("ordergroup");
+ //user has given a template file
+ if(it != parameters.end()){
+ path = m->hasPath(it->second);
+ //if the user has not given a path then, add inputdir. else leave path alone.
+ if (path == "") { parameters["ordergroup"] = inputDir + it->second; }
+ }
+ }
+
+
+ //if the user changes the output directory command factory will send this info to us in the output parameter
+ outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
+
+ //check for required parameters
+ listfile = validParameter.validFile(parameters, "list", true);
+ if (listfile == "not open") { listfile = ""; abort = true; }
+ else if (listfile == "not found") {
+ listfile = m->getListFile();
+ if (listfile != "") { m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
+ else { m->mothurOut("You have no current list file and the list parameter is required."); m->mothurOutEndLine(); abort = true; }
+ }
+
+ ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
+ if (ordergroupfile == "not open") { abort = true; }
+ else if (ordergroupfile == "not found") { ordergroupfile = ""; }
+
+ groupfile = validParameter.validFile(parameters, "group", true);
+ if (groupfile == "not open") { abort = true; }
+ else if (groupfile == "not found") {
+ groupfile = m->getGroupFile();
+ if (groupfile != "") {
+ m->mothurOut("Using " + groupfile + " as input file for the group parameter."); m->mothurOutEndLine();
+ groupMap = new GroupMap(groupfile);
+
+ int error = groupMap->readMap();
+ if (error == 1) { abort = true; }
+ m->namesOfGroups = groupMap->namesOfGroups;
+ }
+ else { m->mothurOut("You have no current group file and the group parameter is required."); m->mothurOutEndLine(); abort = true; }
+ }else {
+ groupMap = new GroupMap(groupfile);
+
+ int error = groupMap->readMap();
+ if (error == 1) { abort = true; }
+ m->namesOfGroups = groupMap->namesOfGroups;
+ m->setGroupFile(groupfile);
+ }
+
+ string groups = validParameter.validFile(parameters, "groups", false);
+ if (groups == "not found") { groups = ""; }
+ else {
+ m->splitAtDash(groups, Groups);
+ m->Groups = Groups;
+ }
+
+ //check for optional parameter and set defaults
+ // ...at some point should added some additional type checking...
+ string label = validParameter.validFile(parameters, "label", false);
+ if (label == "not found") { label = ""; }
+ else {
+ if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
+ else { allLines = 1; }
+ }
+
+ //getting output filename
+ filename = listfile;
+ if (outputDir == "") { outputDir += m->hasPath(filename); }
+
+ filename = outputDir + m->getRootName(m->getSimpleName(filename));
+ filename = filename + "shared";
+ outputTypes["shared"].push_back(filename);
+
+ m->openOutputFile(filename, out);
+ pickedGroups = false;
+
+ //if hte user has not specified any groups then use them all
+ if (Groups.size() == 0) {
+ Groups = groupMap->namesOfGroups; m->Groups = Groups;
+ }
+
+ //fill filehandles with neccessary ofstreams
+ int i;
+ ofstream* temp;
+ for (i=0; i<Groups.size(); i++) {
+ temp = new ofstream;
+ filehandles[Groups[i]] = temp;
+ }
+
+ //set fileroot
+ fileroot = outputDir + m->getRootName(m->getSimpleName(listfile));
+
+ //clears file before we start to write to it below
+ for (int i=0; i<Groups.size(); i++) {
+ remove((fileroot + Groups[i] + ".rabund").c_str());
+ outputNames.push_back((fileroot + Groups[i] + ".rabund"));
+ outputTypes["rabund"].push_back((fileroot + Groups[i] + ".rabund"));
+ }
+
+ }
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SharedCommand", "SharedCommand");