]> git.donarmstrong.com Git - mothur.git/blobdiff - distancecommand.cpp
added checks for ^C to quit command instead of program
[mothur.git] / distancecommand.cpp
index c615b8068ae1576b07f2e64a2742bfcb36e3e9b3..f05cb768e0c433daf1a976827b9371333e37acb7 100644 (file)
 
 //**********************************************************************************************************************
 
-DistanceCommand::DistanceCommand(string option){
+DistanceCommand::DistanceCommand(string option) {
        try {
                abort = false;
                Estimators.clear();
-               
+                               
                //allow user to run help
                if(option == "help") { help(); abort = true; }
                
@@ -56,7 +56,7 @@ DistanceCommand::DistanceCommand(string option){
 
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
-                       if (fastafile == "not found") { mothurOut("fasta is a required parameter for the dist.seqs command."); mothurOutEndLine(); abort = true; }
+                       if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the dist.seqs command."); m->mothurOutEndLine(); abort = true; }
                        else if (fastafile == "not open") { abort = true; }     
                        else{
                                ifstream inFASTA;
@@ -92,7 +92,7 @@ DistanceCommand::DistanceCommand(string option){
                        
                        output = validParameter.validFile(parameters, "output", false);         if(output == "not found"){      output = "column"; }
                        
-                       if ((output != "column") && (output != "lt") && (output != "square")) { mothurOut(output + " is not a valid output form. Options are column, lt and square. I will use column."); mothurOutEndLine(); output = "column"; }
+                       if ((output != "column") && (output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are column, lt and square. I will use column."); m->mothurOutEndLine(); output = "column"; }
                        
                        ValidCalculators validCalculator;
                        
@@ -118,7 +118,7 @@ DistanceCommand::DistanceCommand(string option){
                                
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "DistanceCommand");
+               m->errorOut(e, "DistanceCommand", "DistanceCommand");
                exit(1);
        }
 }
@@ -137,21 +137,21 @@ DistanceCommand::~DistanceCommand(){
 
 void DistanceCommand::help(){
        try {
-               mothurOut("The dist.seqs command reads a file containing sequences and creates a distance file.\n");
-               mothurOut("The dist.seqs command parameters are fasta, calc, countends, output, cutoff and processors.  \n");
-               mothurOut("The fasta parameter is required.\n");
-               mothurOut("The calc parameter allows you to specify the method of calculating the distances.  Your options are: nogaps, onegap or eachgap. The default is onegap.\n");
-               mothurOut("The countends parameter allows you to specify whether to include terminal gaps in distance.  Your options are: T or F. The default is T.\n");
-               mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
-               mothurOut("The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n");
-               mothurOut("The processors parameter allows you to specify number of processors to use.  The default is 1.\n");
-               mothurOut("The dist.seqs command should be in the following format: \n");
-               mothurOut("dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) \n");
-               mothurOut("Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3).\n");
-               mothurOut("Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc).\n\n");
+               m->mothurOut("The dist.seqs command reads a file containing sequences and creates a distance file.\n");
+               m->mothurOut("The dist.seqs command parameters are fasta, calc, countends, output, cutoff and processors.  \n");
+               m->mothurOut("The fasta parameter is required.\n");
+               m->mothurOut("The calc parameter allows you to specify the method of calculating the distances.  Your options are: nogaps, onegap or eachgap. The default is onegap.\n");
+               m->mothurOut("The countends parameter allows you to specify whether to include terminal gaps in distance.  Your options are: T or F. The default is T.\n");
+               m->mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
+               m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n");
+               m->mothurOut("The processors parameter allows you to specify number of processors to use.  The default is 1.\n");
+               m->mothurOut("The dist.seqs command should be in the following format: \n");
+               m->mothurOut("dist.seqs(fasta=yourFastaFile, calc=yourCalc, countends=yourEnds, cutoff= yourCutOff, processors=yourProcessors) \n");
+               m->mothurOut("Example dist.seqs(fasta=amazon.fasta, calc=eachgap, countends=F, cutoff= 2.0, processors=3).\n");
+               m->mothurOut("Note: No spaces between parameter labels (i.e. calc), '=' and parameters (i.e.yourCalc).\n\n");
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "help");
+               m->errorOut(e, "DistanceCommand", "help");
                exit(1);
        }
 }
@@ -208,16 +208,24 @@ int DistanceCommand::execute(){
                ifstream inFASTA;
                driver(0, numSeqs, outputFile, cutoff);
 #endif
+               if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
                
                if (output == "square") {  convertMatrix(outputFile); }
                
+               if (m->control_pressed) { delete distCalculator; remove(outputFile.c_str()); return 0; }
+               
                delete distCalculator;
                
+               m->mothurOutEndLine();
+               m->mothurOut("Output File Name: "); m->mothurOutEndLine();
+               m->mothurOut(outputFile); m->mothurOutEndLine();
+               m->mothurOutEndLine();
+
                return 0;
                
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "execute");
+               m->errorOut(e, "DistanceCommand", "execute");
                exit(1);
        }
 }
@@ -238,7 +246,7 @@ void DistanceCommand::createProcesses(string filename) {
                        }else if (pid == 0){
                                driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff);
                                exit(0);
-                       }else { mothurOut("unable to spawn the necessary processes."); mothurOutEndLine(); exit(0); }
+                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
                }
        
                //force parent to wait until all the processes are done
@@ -249,7 +257,7 @@ void DistanceCommand::createProcesses(string filename) {
 #endif
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "createProcesses");
+               m->errorOut(e, "DistanceCommand", "createProcesses");
                exit(1);
        }
 }
@@ -277,6 +285,9 @@ int DistanceCommand::driver(int startLine, int endLine, string dFileName, float
                                outFile << name << '\t';        
                        }
                        for(int j=0;j<i;j++){
+                               
+                               if (m->control_pressed) { outFile.close(); return 0;  }
+                               
                                distCalculator->calcDist(alignDB.get(i), alignDB.get(j));
                                double dist = distCalculator->getDist();
                                
@@ -295,23 +306,23 @@ int DistanceCommand::driver(int startLine, int endLine, string dFileName, float
                        if (output == "lt") { outFile << endl; }
                        
                        if(i % 100 == 0){
-                               mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); mothurOutEndLine();
+                               m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
                        }
                        
                }
-               mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); mothurOutEndLine();
+               m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
                
                outFile.close();
                
                return 1;
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "driver");
+               m->errorOut(e, "DistanceCommand", "driver");
                exit(1);
        }
 }
 /**************************************************************************************************/
-void DistanceCommand::convertMatrix(string outputFile) {
+int DistanceCommand::convertMatrix(string outputFile) {
        try{
 
                //sort file by first column so the distances for each row are together
@@ -353,6 +364,8 @@ void DistanceCommand::convertMatrix(string outputFile) {
                //openInputFile(outfile, in);
                
                while(!in.eof()) {
+                       if (m->control_pressed) { in.close(); remove(outfile.c_str()); out.close(); return 0; }
+                       
                        in >> first >> second >> dist; gobble(in);
                                
                        if (first != currentRow) {
@@ -388,9 +401,11 @@ void DistanceCommand::convertMatrix(string outputFile) {
                
                remove(outfile.c_str());
                
+               return 1;
+               
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "convertMatrix");
+               m->errorOut(e, "DistanceCommand", "convertMatrix");
                exit(1);
        }
 }
@@ -413,7 +428,7 @@ void DistanceCommand::appendFiles(string temp, string filename) {
                output.close();
        }
        catch(exception& e) {
-               errorOut(e, "DistanceCommand", "appendFiles");
+               m->errorOut(e, "DistanceCommand", "appendFiles");
                exit(1);
        }
 }