+LibShuffCommand::LibShuffCommand(string option) {
+ try {
+ abort = false; calledHelp = false;
+
+ //allow user to run help
+ if(option == "help") { help(); abort = true; calledHelp = true; }
+ else if(option == "citation") { citation(); abort = true; calledHelp = true;}
+
+ else {
+ vector<string> myArray = setParameters();
+
+ OptionParser parser(option);
+ map<string, string> parameters = parser.getParameters();
+ map<string,string>::iterator it;
+
+ ValidParameters validParameter;
+
+ //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; }
+ }
+
+ //initialize outputTypes
+ vector<string> tempOutNames;
+ outputTypes["coverage"] = tempOutNames;
+ outputTypes["libshuffsummary"] = tempOutNames;
+
+ string inputDir = validParameter.validFile(parameters, "inputdir", false);
+ if (inputDir == "not found"){ inputDir = ""; }
+ else {
+ string path;
+ it = parameters.find("phylip");
+ //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["phylip"] = 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; }
+ }
+ }
+
+ //check for required parameters
+ phylipfile = validParameter.validFile(parameters, "phylip", true);
+ if (phylipfile == "not open") { phylipfile = ""; abort = true; }
+ else if (phylipfile == "not found") {
+ phylipfile = m->getPhylipFile();
+ if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
+ else {
+ m->mothurOut("You must provide a phylip file."); m->mothurOutEndLine();
+ abort = true;
+ }
+ }else { m->setPhylipFile(phylipfile); }
+
+ //check for required parameters
+ groupfile = validParameter.validFile(parameters, "group", true);
+ if (groupfile == "not open") { groupfile = ""; 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(); }
+ else {
+ m->mothurOut("You must provide a group file."); m->mothurOutEndLine();
+ abort = true;
+ }
+ }else { m->setGroupFile(groupfile); }
+
+ //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 = "";
+ outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
+ }
+
+ //check for optional parameter and set defaults
+ // ...at some point should added some additional type checking...
+ groups = validParameter.validFile(parameters, "groups", false);
+ if (groups == "not found") { groups = ""; savegroups = groups; }
+ else {
+ savegroups = groups;
+ m->splitAtDash(groups, Groups);
+ m->setGroups(Groups);
+ }
+
+ string temp;
+ temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10000"; }
+ m->mothurConvert(temp, iters);
+
+ temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "1.0"; }
+ m->mothurConvert(temp, cutOff);
+
+ temp = validParameter.validFile(parameters, "step", false); if (temp == "not found") { temp = "0.01"; }
+ m->mothurConvert(temp, step);
+
+ temp = validParameter.validFile(parameters, "sim", false); if (temp == "not found") { temp = "F"; }
+ sim = m->isTrue(temp);
+
+ userform = validParameter.validFile(parameters, "form", false); if (userform == "not found") { userform = "integral"; }
+
+ }
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "LibShuffCommand", "LibShuffCommand");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+
+int LibShuffCommand::execute(){
+ try {
+
+ if (abort == true) { if (calledHelp) { return 0; } return 2; }
+
+ //read files
+ groupMap = new GroupMap(groupfile);
+ int error = groupMap->readMap();
+ if (error == 1) { delete groupMap; return 0; }
+
+ ifstream in;
+ m->openInputFile(phylipfile, in);
+ matrix = new FullMatrix(in, groupMap, sim); //reads the matrix file
+ in.close();
+
+ if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
+
+ //if files don't match...
+ if (matrix->getNumSeqs() < groupMap->getNumSeqs()) {
+ m->mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences."); m->mothurOutEndLine();
+ //create new group file
+ if(outputDir == "") { outputDir += m->hasPath(groupfile); }
+
+ string newGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "editted.groups";
+ outputNames.push_back(newGroupFile);
+ ofstream outGroups;
+ m->openOutputFile(newGroupFile, outGroups);
+
+ for (int i = 0; i < matrix->getNumSeqs(); i++) {
+ if (m->control_pressed) { delete groupMap; delete matrix; outGroups.close(); m->mothurRemove(newGroupFile); return 0; }
+
+ Names temp = matrix->getRowInfo(i);
+ outGroups << temp.seqName << '\t' << temp.groupName << endl;
+ }
+ outGroups.close();
+
+ m->mothurOut(newGroupFile + " is a new group file containing only the sequence that are in your distance file. I will read this file instead."); m->mothurOutEndLine();
+
+ //read new groupfile
+ delete groupMap;
+ groupfile = newGroupFile;
+
+ groupMap = new GroupMap(groupfile);
+ groupMap->readMap();
+
+ if (m->control_pressed) { delete groupMap; delete matrix; m->mothurRemove(newGroupFile); return 0; }
+ }
+
+
+ setGroups(); //set the groups to be analyzed and sorts them
+
+ if (numGroups < 2) { m->mothurOut("[ERROR]: libshuff requires at least 2 groups, you only have " + toString(numGroups) + ", aborting."); m->mothurOutEndLine(); m->control_pressed = true; }
+
+ if (m->control_pressed) { delete groupMap; delete matrix; return 0; }
+
+ /********************************************************************************************/
+ //this is needed because when we read the matrix we sort it into groups in alphabetical order
+ //the rest of the command and the classes used in this command assume specific order
+ /********************************************************************************************/
+ matrix->setGroups(groupMap->getNamesOfGroups());
+ vector<int> sizes;
+ for (int i = 0; i < (groupMap->getNamesOfGroups()).size(); i++) { sizes.push_back(groupMap->getNumSeqs((groupMap->getNamesOfGroups())[i])); }
+ matrix->setSizes(sizes);
+
+
+ if(userform == "discrete"){
+ form = new DLibshuff(matrix, iters, step, cutOff);
+ }
+ else{
+ form = new SLibshuff(matrix, iters, cutOff);
+ }
+
+ savedDXYValues = form->evaluateAll();
+ savedMinValues = form->getSavedMins();
+
+ if (m->control_pressed) { delete form; m->clearGroups(); delete matrix; delete groupMap; return 0; }
+
+ pValueCounts.resize(numGroups);
+ for(int i=0;i<numGroups;i++){
+ pValueCounts[i].assign(numGroups, 0);
+ }
+
+ if (m->control_pressed) { outputTypes.clear(); delete form; m->clearGroups(); delete matrix; delete groupMap; return 0; }
+
+ Progress* reading = new Progress();
+
+ for(int i=0;i<numGroups-1;i++) {
+ for(int j=i+1;j<numGroups;j++) {
+
+ if (m->control_pressed) { outputTypes.clear(); delete form; m->clearGroups(); delete matrix; delete groupMap; delete reading; return 0; }