]> git.donarmstrong.com Git - mothur.git/blobdiff - hclustercommand.cpp
changes while testing
[mothur.git] / hclustercommand.cpp
index 3e99473bb6d742cd759c6a5a3eed55373df58549..b991ccd6289b9afd44628c12bf451eb86529fa44 100644 (file)
 //**********************************************************************************************************************
 vector<string> HClusterCommand::setParameters(){       
        try {
-               CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
-               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
-               CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
-               CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "",false,false); parameters.push_back(pcutoff);
-               CommandParameter pprecision("precision", "Number", "", "100", "", "", "",false,false); parameters.push_back(pprecision);
-               CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "",false,false); parameters.push_back(pmethod);
-               CommandParameter phard("hard", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(phard);
-               CommandParameter psorted("sorted", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psorted);
-               CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pshowabund);
-               CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(ptiming);          
-               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
-               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
+               CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none","list-rabund-sabund",false,false,true); parameters.push_back(pphylip);
+               CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName","",false,false,true); parameters.push_back(pname);
+               CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName","list-rabund-sabund",false,false,true); parameters.push_back(pcolumn);
+               CommandParameter pcutoff("cutoff", "Number", "", "10", "", "", "","",false,false,true); parameters.push_back(pcutoff);
+               CommandParameter pprecision("precision", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pprecision);
+               CommandParameter pmethod("method", "Multiple", "furthest-nearest-average-weighted", "average", "", "", "","",false,false); parameters.push_back(pmethod);
+               CommandParameter phard("hard", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(phard);
+               CommandParameter psorted("sorted", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(psorted);
+               CommandParameter pshowabund("showabund", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pshowabund);
+               CommandParameter ptiming("timing", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(ptiming);               
+               CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
+               CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
                        
                vector<string> myArray;
                for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
@@ -51,6 +51,24 @@ string HClusterCommand::getHelpString(){
                exit(1);
        }
 }
+//**********************************************************************************************************************
+string HClusterCommand::getOutputPattern(string type) {
+    try {
+        string pattern = "";
+        
+        if (type == "list") {  pattern = "[filename],[clustertag],list"; } 
+        else if (type == "rabund") {  pattern = "[filename],[clustertag],rabund"; } 
+        else if (type == "sabund") {  pattern = "[filename],[clustertag],sabund"; }
+        else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
+        
+        return pattern;
+    }
+    catch(exception& e) {
+        m->errorOut(e, "HClusterCommand", "getOutputPattern");
+        exit(1);
+    }
+}
+
 //**********************************************************************************************************************
 HClusterCommand::HClusterCommand(){    
        try {
@@ -135,16 +153,17 @@ HClusterCommand::HClusterCommand(string option)  {
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
                        else if (phylipfile == "not found") { phylipfile = ""; }        
-                       else {  distfile = phylipfile;  format = "phylip";      }
+                       else {  distfile = phylipfile;  format = "phylip";      m->setPhylipFile(phylipfile); }
                        
                        columnfile = validParameter.validFile(parameters, "column", true);
                        if (columnfile == "not open") { abort = true; } 
                        else if (columnfile == "not found") { columnfile = ""; }
-                       else {  distfile = columnfile; format = "column";       }
+                       else {  distfile = columnfile; format = "column";       m->setColumnFile(columnfile); }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }   
                        else if (namefile == "not found") { namefile = ""; }
+                       else { m->setNameFile(namefile); }
                        
                        if ((phylipfile == "") && (columnfile == "")) { 
                                //is there are current file available for either of these?
@@ -181,14 +200,14 @@ HClusterCommand::HClusterCommand(string option)  {
                        if (temp == "not found") { temp = "100"; }
                        //saves precision legnth for formatting below
                        length = temp.length();
-                       convert(temp, precision); 
+                       m->mothurConvert(temp, precision); 
                        
                        temp = validParameter.validFile(parameters, "hard", false);                     if (temp == "not found") { temp = "T"; }
                        hard = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "cutoff", false);
                        if (temp == "not found") { temp = "10"; }
-                       convert(temp, cutoff); 
+                       m->mothurConvert(temp, cutoff); 
                        cutoff += (5 / (precision * 10.0)); 
                        
                        method = validParameter.validFile(parameters, "method", false);
@@ -217,14 +236,22 @@ HClusterCommand::HClusterCommand(string option)  {
                                else if (method == "nearest")   { tag = "nn";  }
                                else if (method == "weighted")  { tag = "wn";  }
                                else                                                    { tag = "an";  }
+                
+                map<string, string> variables;
+                variables["[filename]"] = fileroot;
+                variables["[clustertag]"] = tag;
                        
-                               m->openOutputFile(fileroot+ tag + ".sabund",    sabundFile);
-                               m->openOutputFile(fileroot+ tag + ".rabund",    rabundFile);
-                               m->openOutputFile(fileroot+ tag + ".list",              listFile);
-                               
-                               outputNames.push_back(fileroot+ tag + ".sabund"); outputTypes["sabund"].push_back(fileroot+ tag + ".sabund");
-                               outputNames.push_back(fileroot+ tag + ".rabund"); outputTypes["rabund"].push_back(fileroot+ tag + ".rabund");
-                               outputNames.push_back(fileroot+ tag + ".list"); outputTypes["list"].push_back(fileroot+ tag + ".list");
+                               string sabundFileName = getOutputFileName("sabund",variables);
+                string rabundFileName = getOutputFileName("rabund",variables);
+                string listFileName = getOutputFileName("list", variables);
+                
+                m->openOutputFile(sabundFileName,      sabundFile);
+                m->openOutputFile(rabundFileName,      rabundFile);
+                m->openOutputFile(listFileName,        listFile);
+                
+                outputNames.push_back(sabundFileName); outputTypes["sabund"].push_back(sabundFileName);
+                outputNames.push_back(rabundFileName); outputTypes["rabund"].push_back(rabundFileName);
+                outputNames.push_back(listFileName); outputTypes["list"].push_back(listFileName);
                        }
                }
        }
@@ -259,7 +286,7 @@ int HClusterCommand::execute(){
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                                return 0;  
                        }
                        
@@ -275,7 +302,7 @@ int HClusterCommand::execute(){
                        sabundFile.close();
                        rabundFile.close();
                        listFile.close();
-                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                        return 0;  
                }
 
@@ -305,35 +332,41 @@ int HClusterCommand::execute(){
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                                return 0;  
                }
 
+               float saveCutoff = cutoff;
                
                while (seqs.size() != 0){
                
                        seqs = cluster->getSeqs();
                        
+                       //to account for cutoff change in average neighbor
+                       if (seqs.size() != 0) {
+                               if (seqs[0].dist > cutoff) { break; }
+                       }
+                       
                        if (m->control_pressed) {  
                                delete cluster;
                                sabundFile.close();
                                rabundFile.close();
                                listFile.close();
-                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                                return 0;  
                        }
 
                        for (int i = 0; i < seqs.size(); i++) {  //-1 means skip me
                                
                                if (seqs[i].seq1 != seqs[i].seq2) {
-                                       cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
+                                       cutoff = cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
                                        
                                        if (m->control_pressed) {  
                                                delete cluster;
                                                sabundFile.close();
                                                rabundFile.close();
                                                listFile.close();
-                                               for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                                               for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                                                return 0;  
                                        }
 
@@ -366,7 +399,7 @@ int HClusterCommand::execute(){
                        sabundFile.close();
                        rabundFile.close();
                        listFile.close();
-                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                        return 0;  
                }
                                        
@@ -383,10 +416,18 @@ int HClusterCommand::execute(){
                delete cluster;
                
                if (m->control_pressed) {  
-                       for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());  } outputTypes.clear();
+                       for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);  } outputTypes.clear();
                        return 0;  
                }
                
+               
+               if (saveCutoff != cutoff) { 
+                       if (hard)       {  saveCutoff = m->ceilDist(saveCutoff, precision);     }
+                       else            {       saveCutoff = m->roundDist(saveCutoff, precision);  }
+                       
+                       m->mothurOut("changed cutoff to " + toString(cutoff)); m->mothurOutEndLine(); 
+               }
+               
                //set list file as new current listfile
                string current = "";
                itTypes = outputTypes.find("list");