]> git.donarmstrong.com Git - mothur.git/blobdiff - hclustercommand.cpp
added warning about merging with something above cutoff to cluster. working on chimeras
[mothur.git] / hclustercommand.cpp
index 38559500b0a81f25716b983b2274b88332e3ff56..365c80ee7956045fec1c1d82e848183f3138fe70 100644 (file)
@@ -21,16 +21,17 @@ HClusterCommand::HClusterCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"cutoff","precision","method","showabund","timing","phylip","column","name","sorted"};
+                       string Array[] =  {"cutoff","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        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 (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {
                                        abort = true;
                                }
@@ -38,6 +39,39 @@ HClusterCommand::HClusterCommand(string option){
                        
                        globaldata->newRead();
                        
+                       //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("phylip");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = 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("column");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["column"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("name");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = 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;             }
+                               }
+                       }
+
+                       //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
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
@@ -76,7 +110,7 @@ HClusterCommand::HClusterCommand(string option){
                        cutoff += (5 / (precision * 10.0));
                        
                        method = validParameter.validFile(parameters, "method", false);
-                       if (method == "not found") { method = "nearest"; }
+                       if (method == "not found") { method = "furthest"; }
                        
                        if ((method == "furthest") || (method == "nearest") || (method == "average")) { }
                        else { mothurOut("Not a valid clustering method.  Valid clustering algorithms are furthest, nearest or average."); mothurOutEndLine(); abort = true; }
@@ -93,10 +127,13 @@ HClusterCommand::HClusterCommand(string option){
                        
                                
                        if (abort == false) {
-                                                                                       
-                               fileroot = getRootName(distfile);
                                
-                               tag = "fn";  //until we figure out average and nearest methods
+                               if (outputDir == "") {  outputDir += hasPath(distfile); }
+                               fileroot = outputDir + getRootName(getSimpleName(distfile));
+                               
+                               if (method == "furthest")               { tag = "fn";  }
+                               else if (method == "nearest")   { tag = "nn";  }
+                               else                                                    { tag = "an";  }
                        
                                openOutputFile(fileroot+ tag + ".sabund",       sabundFile);
                                openOutputFile(fileroot+ tag + ".rabund",       rabundFile);
@@ -114,12 +151,12 @@ HClusterCommand::HClusterCommand(string option){
 
 void HClusterCommand::help(){
        try {
-               mothurOut("The hcluster command parameter options are cutoff, precision, method, showabund, timing, phylip, column, name and sorted. Phylip or column and name are required.\n");
+               mothurOut("The hcluster command parameter options are cutoff, precision, method, phylip, column, name, showabund, timing and sorted. Phylip or column and name are required.\n");
                mothurOut("The phylip and column parameter allow you to enter your distance file, and sorted indicates whether your column distance file is already sorted. \n");
                mothurOut("The name parameter allows you to enter your name file and is required if your distance file is in column format. \n");
                mothurOut("The hcluster command should be in the following format: \n");
                mothurOut("hcluster(column=youDistanceFile, name=yourNameFile, method=yourMethod, cutoff=yourCutoff, precision=yourPrecision) \n");
-               mothurOut("The acceptable hcluster methods is furthest, but we hope to add nearest and average in the future.\n\n");    
+               mothurOut("The acceptable hcluster methods are furthest and nearest, but we hope to add average in the future.\n\n");   
        }
        catch(exception& e) {
                errorOut(e, "HClusterCommand", "help");
@@ -161,7 +198,7 @@ int HClusterCommand::execute(){
        
                mothurOut("It took " + toString(time(NULL) - estart) + " seconds to sort. "); mothurOutEndLine();
                estart = time(NULL);
-               
+       
                //list vector made by read contains all sequence names
                if(list != NULL){
                        rabund = new RAbundVector(list->getRAbundVector());
@@ -176,42 +213,20 @@ int HClusterCommand::execute(){
                
                print_start = true;
                start = time(NULL);
-               
-//cout << "here" << endl;      
-               ifstream in;
-               openInputFile(distfile, in);
-               string firstName, secondName;
-               float distance;
-               
-               cluster = new HCluster(rabund, list);
-               bool clusteredSomething;
+                               
+               cluster = new HCluster(rabund, list, method, distfile, globaldata->nameMap, cutoff);
                vector<seqDist> seqs; seqs.resize(1); // to start loop
-               exitedBreak = false;  //lets you know if there is a distance stored in next
                
                while (seqs.size() != 0){
                
-                       seqs = getSeqs(in);
-                       random_shuffle(seqs.begin(), seqs.end());
-                       
-                       if (seqs.size() == 0) { break; } //there are no more distances
-               
+                       seqs = cluster->getSeqs();
+                               
                        for (int i = 0; i < seqs.size(); i++) {  //-1 means skip me
 
-                               if (print_start && isTrue(timing)) {
-                                       mothurOut("Clustering (" + tag + ") dist " + toString(distance) + "/" 
-                                                         + toString(roundDist(distance, precision)) 
-                                                         + "\t(precision: " + toString(precision) + ")");
-                                       cout.flush();
-                                       print_start = false;
-                               }
-                               
-       ///cout << "before cluster update" << endl;
                                if (seqs[i].seq1 != seqs[i].seq2) {
-                                       clusteredSomething = cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
+                                       cluster->update(seqs[i].seq1, seqs[i].seq2, seqs[i].dist);
                                        
                                        float rndDist = roundDist(seqs[i].dist, precision);
-                                       //cout << "after cluster update clusterSomething = " << clusteredSomething << " rndDist = " << rndDist << " rndPreviousDist = " << rndPreviousDist << endl;                     
-                                       
                                        
                                        if((previousDist <= 0.0000) && (seqs[i].dist != previousDist)){
                                                printData("unique");
@@ -219,7 +234,7 @@ int HClusterCommand::execute(){
                                        else if((rndDist != rndPreviousDist)){
                                                printData(toString(rndPreviousDist,  length-1));
                                        }
-                                       
+                               
                                        previousDist = seqs[i].dist;
                                        rndPreviousDist = rndDist;
                                        oldRAbund = *rabund;
@@ -227,16 +242,7 @@ int HClusterCommand::execute(){
                                }
                        }
                }
-               
-               in.close();
 
-               if (print_start && isTrue(timing)) {
-                       //mothurOut("Clustering (" + tag + ") for distance " + toString(previousDist) + "/" + toString(rndPreviousDist) 
-                                        //+ "\t(precision: " + toString(precision) + ", Nodes: " + toString(matrix->getNNodes()) + ")");
-                       cout.flush();
-                       print_start = false;
-               }
-       
                if(previousDist <= 0.0000){
                        printData("unique");
                }
@@ -258,11 +264,10 @@ int HClusterCommand::execute(){
                sabundFile.close();
                rabundFile.close();
                listFile.close();
-               
                delete cluster;
-               //if (isTrue(timing)) {
-                       mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
-               //}
+       
+               mothurOut("It took " + toString(time(NULL) - estart) + " seconds to cluster. "); mothurOutEndLine();
+               
                return 0;
        }
        catch(exception& e) {
@@ -302,75 +307,3 @@ void HClusterCommand::printData(string label){
 }
 //**********************************************************************************************************************
 
-vector<seqDist> HClusterCommand::getSeqs(ifstream& filehandle){
-       try {
-               string firstName, secondName;
-               float distance, prevDistance;
-               vector<seqDist> sameSeqs;
-               prevDistance = -1;
-               
-               //if you are not at the beginning of the file
-               if (exitedBreak) { 
-                       sameSeqs.push_back(next);
-                       prevDistance = next.dist;
-                       exitedBreak = false;
-               }
-       
-               //get entry
-               while (filehandle) {
-                       
-                       filehandle >> firstName >> secondName >> distance;  
-//cout << firstName << '\t' << secondName << '\t' << distance << endl;
-                       gobble(filehandle);
-                       
-                       //save first one
-                       if (prevDistance == -1) { prevDistance = distance; }
-                       
-                       map<string,int>::iterator itA = globaldata->nameMap->find(firstName);
-                       map<string,int>::iterator itB = globaldata->nameMap->find(secondName);
-                       
-                       if(itA == globaldata->nameMap->end()){
-                               cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);
-                       }
-                       if(itB == globaldata->nameMap->end()){
-                               cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);
-                       }
-                       
-                       //using cutoff
-                       if (distance > cutoff) { break; }
-                       
-                       if (distance != -1) { //-1 means skip me
-                               
-                               //are the distances the same
-                               if (distance == prevDistance) { //save in vector
-                                       seqDist temp;
-                                       temp.seq1 = itA->second;
-                                       temp.seq2 = itB->second;
-                                       temp.dist = distance;
-                                       sameSeqs.push_back(temp);
-                                       exitedBreak = false;
-                                       //what about precision??
-                                       
-                               }else{ 
-                                       next.seq1 = itA->second;
-                                       next.seq2 = itB->second;
-                                       next.dist = distance;
-                                       exitedBreak = true;
-                                       break;
-                               }
-                               
-                       }
-               }
-
-               return sameSeqs;
-       }
-       catch(exception& e) {
-               errorOut(e, "HClusterCommand", "getSeqs");
-               exit(1);
-       }
-
-
-}
-
-//**********************************************************************************************************************
-