]> git.donarmstrong.com Git - mothur.git/blobdiff - mgclustercommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / mgclustercommand.cpp
index 269e08853e16478918e830a279b68958954fe16a..23f359eae8ab7fe84e9b86faf96d54d33ac8dcd3 100644 (file)
@@ -9,14 +9,64 @@
 
 #include "mgclustercommand.h"
 
+
+//**********************************************************************************************************************
+vector<string> MGClusterCommand::getValidParameters(){ 
+       try {
+               string Array[] =  {"blast", "method", "name", "hard", "cutoff", "precision", "length", "min", "penalty", "hcluster","merge","outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+MGClusterCommand::MGClusterCommand(){  
+       try {
+               abort = true; calledHelp = true; 
+               vector<string> tempOutNames;
+               outputTypes["list"] = tempOutNames;
+               outputTypes["rabund"] = tempOutNames;
+               outputTypes["sabund"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "MGClusterCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> MGClusterCommand::getRequiredParameters(){      
+       try {
+               string Array[] =  {"blast"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> MGClusterCommand::getRequiredFiles(){   
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MGClusterCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
 //**********************************************************************************************************************
 MGClusterCommand::MGClusterCommand(string option) {
        try {
                globaldata = GlobalData::getInstance();
-               abort = false;
+               abort = false; calledHelp = false;   
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -34,6 +84,12 @@ MGClusterCommand::MGClusterCommand(string option) {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["list"] = tempOutNames;
+                       outputTypes["rabund"] = tempOutNames;
+                       outputTypes["sabund"] = tempOutNames;
+                       
                        //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 = "";          }
@@ -42,7 +98,7 @@ MGClusterCommand::MGClusterCommand(string option) {
                                it = parameters.find("blast");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["blast"] = inputDir + it->second;            }
                                }
@@ -50,7 +106,7 @@ MGClusterCommand::MGClusterCommand(string option) {
                                it = parameters.find("name");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["name"] = inputDir + it->second;             }
                                }
@@ -65,7 +121,7 @@ MGClusterCommand::MGClusterCommand(string option) {
                        //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 += hasPath(blastfile); //if user entered a file with a path then preserve it  
+                               outputDir += m->hasPath(blastfile); //if user entered a file with a path then preserve it       
                        }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
@@ -97,16 +153,16 @@ MGClusterCommand::MGClusterCommand(string option) {
                        convert(temp, penalty); 
                        
                        temp = validParameter.validFile(parameters, "min", false);                              if (temp == "not found") { temp = "true"; }
-                       minWanted = isTrue(temp); 
+                       minWanted = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "merge", false);                    if (temp == "not found") { temp = "true"; }
-                       merge = isTrue(temp); 
+                       merge = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "hcluster", false);                 if (temp == "not found") { temp = "false"; }
-                       hclusterWanted = isTrue(temp); 
+                       hclusterWanted = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "F"; }
-                       hard = isTrue(temp);
+                       hard = m->isTrue(temp);
                }
 
        }
@@ -145,7 +201,7 @@ MGClusterCommand::~MGClusterCommand(){}
 int MGClusterCommand::execute(){
        try {
                
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //read names file
                if (namefile != "") {
@@ -153,7 +209,7 @@ int MGClusterCommand::execute(){
                        nameMap->readMap();
                }else{ nameMap= new NameAssignment(); }
                
-               string fileroot = outputDir + getRootName(getSimpleName(blastfile));
+               string fileroot = outputDir + m->getRootName(m->getSimpleName(blastfile));
                string tag = "";
                time_t start;
                float previousDist = 0.00000;
@@ -167,7 +223,7 @@ int MGClusterCommand::execute(){
                list = new ListVector(nameMap->getListVector());
                RAbundVector* rabund = new RAbundVector(list->getRAbundVector());
                
-               if (m->control_pressed) { delete nameMap; delete read; delete list; delete rabund; return 0; }
+               if (m->control_pressed) { outputTypes.clear(); delete nameMap; delete read; delete list; delete rabund; return 0; }
                
                start = time(NULL);
                oldList = *list;
@@ -179,13 +235,14 @@ int MGClusterCommand::execute(){
                else                                                    { tag = "an";  }
                
                //open output files
-               openOutputFile(fileroot+ tag + ".list",  listFile);
-               openOutputFile(fileroot+ tag + ".rabund",  rabundFile);
-               openOutputFile(fileroot+ tag + ".sabund",  sabundFile);
+               m->openOutputFile(fileroot+ tag + ".list",  listFile);
+               m->openOutputFile(fileroot+ tag + ".rabund",  rabundFile);
+               m->openOutputFile(fileroot+ tag + ".sabund",  sabundFile);
                
                if (m->control_pressed) { 
                        delete nameMap; delete read; delete list; delete rabund; 
                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                       outputTypes.clear();
                        return 0; 
                }
                
@@ -206,6 +263,7 @@ int MGClusterCommand::execute(){
                        if (m->control_pressed) { 
                                delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster;
                                listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               outputTypes.clear();
                                return 0; 
                        }
                
@@ -217,15 +275,16 @@ int MGClusterCommand::execute(){
                                if (m->control_pressed) { 
                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster;
                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                       outputTypes.clear();
                                        return 0; 
                                }
                                
                                float dist = distMatrix->getSmallDist();
                                float rndDist;
                                if (hard) {
-                                       rndDist = ceilDist(dist, precision); 
+                                       rndDist = m->ceilDist(dist, precision); 
                                }else{
-                                       rndDist = roundDist(dist, precision); 
+                                       rndDist = m->roundDist(dist, precision); 
                                }
                                
                                if(previousDist <= 0.0000 && dist != previousDist){
@@ -239,6 +298,7 @@ int MGClusterCommand::execute(){
                                                if (m->control_pressed) { 
                                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster; delete temp;
                                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                                       outputTypes.clear();
                                                        return 0; 
                                                }
                                                
@@ -269,6 +329,7 @@ int MGClusterCommand::execute(){
                                        if (m->control_pressed) { 
                                                        delete nameMap; delete distMatrix; delete list; delete rabund; delete cluster; delete temp;
                                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                                                       outputTypes.clear();
                                                        return 0; 
                                        }
                                        
@@ -298,6 +359,7 @@ int MGClusterCommand::execute(){
                        if (m->control_pressed) { 
                                delete nameMap;  delete list; delete rabund; 
                                listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               outputTypes.clear();
                                return 0; 
                        }
                
@@ -309,11 +371,12 @@ int MGClusterCommand::execute(){
                        
                        vector<seqDist> seqs; seqs.resize(1); // to start loop
                        //ifstream inHcluster;
-                       //openInputFile(distFile, inHcluster);
+                       //m->openInputFile(distFile, inHcluster);
                        
                        if (m->control_pressed) { 
                                delete nameMap;  delete list; delete rabund; delete hcluster;
                                listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
+                               outputTypes.clear();
                                return 0; 
                        }
 
@@ -326,6 +389,7 @@ int MGClusterCommand::execute(){
                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
                                        remove(distFile.c_str());
                                        remove(overlapFile.c_str());
+                                       outputTypes.clear();
                                        return 0; 
                                }
                                
@@ -340,14 +404,15 @@ int MGClusterCommand::execute(){
                                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
                                                        remove(distFile.c_str());
                                                        remove(overlapFile.c_str());
+                                                       outputTypes.clear();
                                                        return 0; 
                                                }
        
                                                float rndDist;
                                                if (hard) {
-                                                       rndDist = ceilDist(seqs[i].dist, precision); 
+                                                       rndDist = m->ceilDist(seqs[i].dist, precision); 
                                                }else{
-                                                       rndDist = roundDist(seqs[i].dist, precision); 
+                                                       rndDist = m->roundDist(seqs[i].dist, precision); 
                                                }
                                                                                                
                                                if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
@@ -363,6 +428,7 @@ int MGClusterCommand::execute(){
                                                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
                                                                        remove(distFile.c_str());
                                                                        remove(overlapFile.c_str());
+                                                                       outputTypes.clear();
                                                                        return 0; 
                                                                }
 
@@ -398,6 +464,7 @@ int MGClusterCommand::execute(){
                                                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
                                                        remove(distFile.c_str());
                                                        remove(overlapFile.c_str());
+                                                       outputTypes.clear();
                                                        return 0; 
                                        }
                                        
@@ -429,16 +496,37 @@ int MGClusterCommand::execute(){
                        listFile.close(); rabundFile.close(); sabundFile.close(); remove((fileroot+ tag + ".list").c_str()); remove((fileroot+ tag + ".rabund").c_str()); remove((fileroot+ tag + ".sabund").c_str());
                        globaldata->setListFile("");
                        globaldata->setFormat("");
+                       outputTypes.clear();
                        return 0; 
                }
                
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-               m->mothurOut(fileroot+ tag + ".list"); m->mothurOutEndLine();   
-               m->mothurOut(fileroot+ tag + ".rabund"); m->mothurOutEndLine(); 
-               m->mothurOut(fileroot+ tag + ".sabund"); m->mothurOutEndLine(); 
+               m->mothurOut(fileroot+ tag + ".list"); m->mothurOutEndLine();   outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
+               m->mothurOut(fileroot+ tag + ".rabund"); m->mothurOutEndLine(); outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
+               m->mothurOut(fileroot+ tag + ".sabund"); m->mothurOutEndLine(); outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
                m->mothurOutEndLine();
                
+               //set list file as new current listfile
+               string current = "";
+               itTypes = outputTypes.find("list");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
+               }
+               
+               //set rabund file as new current rabundfile
+               itTypes = outputTypes.find("rabund");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
+               }
+               
+               //set sabund file as new current sabundfile
+               itTypes = outputTypes.find("sabund");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
+               }
+               
+               
                m->mothurOut("It took " + toString(time(NULL) - start) + " seconds to cluster."); m->mothurOutEndLine();
                        
                return 0;
@@ -477,7 +565,7 @@ ListVector* MGClusterCommand::mergeOPFs(map<string, int> binInfo, float dist){
                int count = 0;
                
                if (hclusterWanted) {  
-                       openInputFile(overlapFile, inOverlap);  
+                       m->openInputFile(overlapFile, inOverlap);  
                        if (inOverlap.eof()) {  done = true;  }
                }else { if (overlapMatrix.size() == 0)  {  done = true;  } } 
                
@@ -498,15 +586,18 @@ ListVector* MGClusterCommand::mergeOPFs(map<string, int> binInfo, float dist){
                                if (!inOverlap.eof()) {
                                        string firstName, secondName;
                                        float overlapDistance;
-                                       inOverlap >> firstName >> secondName >> overlapDistance; gobble(inOverlap);
+                                       inOverlap >> firstName >> secondName >> overlapDistance; m->gobble(inOverlap);
                                        
-                                       map<string,int>::iterator itA = nameMap->find(firstName);
-                                       map<string,int>::iterator itB = nameMap->find(secondName);
-                                       if(itA == nameMap->end()){  cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);  }
-                                       if(itB == nameMap->end()){  cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);  }
+                                       //commented out because we check this in readblast already
+                                       //map<string,int>::iterator itA = nameMap->find(firstName);
+                                       //map<string,int>::iterator itB = nameMap->find(secondName);
+                                       //if(itA == nameMap->end()){  cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);  }
+                                       //if(itB == nameMap->end()){  cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);  }
                                        
-                                       overlapNode.seq1 = itA->second;
-                                       overlapNode.seq2 = itB->second;
+                                       //overlapNode.seq1 = itA->second;
+                                       //overlapNode.seq2 = itB->second;
+                                       overlapNode.seq1 = nameMap->get(firstName);
+                                       overlapNode.seq2 = nameMap->get(secondName);
                                        overlapNode.dist = overlapDistance;
                                }else { inOverlap.close(); break; }
                        } 
@@ -568,12 +659,12 @@ ListVector* MGClusterCommand::mergeOPFs(map<string, int> binInfo, float dist){
 void MGClusterCommand::sortHclusterFiles(string unsortedDist, string unsortedOverlap) {
        try {
                //sort distFile
-               string sortedDistFile = sortFile(unsortedDist, outputDir);
+               string sortedDistFile = m->sortFile(unsortedDist, outputDir);
                remove(unsortedDist.c_str());  //delete unsorted file
                distFile = sortedDistFile;
                
                //sort overlap file
-               string sortedOverlapFile = sortFile(unsortedOverlap, outputDir);
+               string sortedOverlapFile = m->sortFile(unsortedOverlap, outputDir);
                remove(unsortedOverlap.c_str());  //delete unsorted file
                overlapFile = sortedOverlapFile;
        }