]> git.donarmstrong.com Git - mothur.git/blobdiff - preclustercommand.cpp
fixed metastats, added resize to cluster.classic, added code to kill children if...
[mothur.git] / preclustercommand.cpp
index 66efd469fcf8631d8537b6c7e90ea066ef72c028..3a3aafc7c44fc3385bbb3284abbbc0b09701c13f 100644 (file)
 //**********************************************************************************************************************
 inline bool comparePriority(seqPNode first, seqPNode second) {  return (first.numIdentical > second.numIdentical); }
 //**********************************************************************************************************************
+vector<string> PreClusterCommand::getValidParameters(){        
+       try {
+               string Array[] =  {"fasta", "name", "diffs", "outputdir","inputdir"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PreClusterCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+PreClusterCommand::PreClusterCommand(){        
+       try {
+               abort = true;
+               //initialize outputTypes
+               vector<string> tempOutNames;
+               outputTypes["fasta"] = tempOutNames;
+               outputTypes["name"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PreClusterCommand", "PreClusterCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> PreClusterCommand::getRequiredParameters(){     
+       try {
+               string Array[] =  {"fasta"};
+               vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PreClusterCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> PreClusterCommand::getRequiredFiles(){  
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PreClusterCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 
 PreClusterCommand::PreClusterCommand(string option) {
        try {
@@ -36,6 +85,11 @@ PreClusterCommand::PreClusterCommand(string option) {
                                if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["fasta"] = tempOutNames;
+                       outputTypes["name"] = 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 = "";          }
@@ -44,7 +98,7 @@ PreClusterCommand::PreClusterCommand(string option) {
                                it = parameters.find("fasta");
                                //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["fasta"] = inputDir + it->second;            }
                                }
@@ -52,7 +106,7 @@ PreClusterCommand::PreClusterCommand(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;             }
                                }
@@ -66,7 +120,7 @@ PreClusterCommand::PreClusterCommand(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(fastafile); //if user entered a file with a path then preserve it  
+                               outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it       
                        }
 
                        //check for optional parameter and set defaults
@@ -115,8 +169,12 @@ int PreClusterCommand::execute(){
                
                if (abort == true) { return 0; }
                
+               int start = time(NULL);
+               
                //reads fasta file and return number of seqs
                int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
+               
+               if (m->control_pressed) { return 0; }
        
                if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0;  }
                if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0;  }
@@ -139,6 +197,9 @@ int PreClusterCommand::execute(){
                                
                                //try to merge it with all smaller seqs
                                for (int j = i+1; j < numSeqs; j++) {
+                                       
+                                       if (m->control_pressed) { return 0; }
+                                       
                                        if (alignSeqs[j].active) {  //this sequence has not been merged yet
                                                //are you within "diff" bases
                                                int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned());
@@ -157,24 +218,33 @@ int PreClusterCommand::execute(){
                        
                        //remove from active list 
                                alignSeqs[i].active = 0;
+                               
                        }//end if active i
-                       if(i % 100 == 0)        { cout << i << '\t' << numSeqs - count << '\t' << count << endl;        }
+                       if(i % 100 == 0)        { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); }
                }
                
-               string fileroot = outputDir + getRootName(getSimpleName(fastafile));
+               if(numSeqs % 100 != 0)  { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine();   }
+       
+               
+               string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile));
                
-               string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
+               string newFastaFile = fileroot + "precluster" + m->getExtension(fastafile);
                string newNamesFile = fileroot + "precluster.names";
                
+               if (m->control_pressed) { return 0; }
                
                m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine();
-               m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); 
+               m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); 
                printData(newFastaFile, newNamesFile);
                
+               m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); 
+               
+               if (m->control_pressed) { remove(newFastaFile.c_str()); remove(newNamesFile.c_str()); return 0; }
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-               m->mothurOut(newFastaFile); m->mothurOutEndLine();      
-               m->mothurOut(newNamesFile); m->mothurOutEndLine();      
+               m->mothurOut(newFastaFile); m->mothurOutEndLine();      outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile);
+               m->mothurOut(newNamesFile); m->mothurOutEndLine();      outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile);
                m->mothurOutEndLine();
 
                return 0;
@@ -195,25 +265,27 @@ int PreClusterCommand::readFASTA(){
                //ifstream inNames;
                ifstream inFasta;
                
-               //openInputFile(namefile, inNames);
-               openInputFile(fastafile, inFasta);
+               //m->openInputFile(namefile, inNames);
+               m->openInputFile(fastafile, inFasta);
                
                //string firstCol, secondCol, nameString;
                length = 0;
                
                while (!inFasta.eof()) {
-       
+                       
+                       if (m->control_pressed) { inFasta.close(); return 0; }
+                       
                        //inNames >> firstCol >> secondCol;
                        //nameString = secondCol;
                        
-                       //gobble(inNames);
+                       //m->gobble(inNames);
                        //int size = 1;
                        //while (secondCol.find_first_of(',') != -1) { 
                        //      size++;
                        //      secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
                        //}
                        
-                       Sequence seq(inFasta);  gobble(inFasta);
+                       Sequence seq(inFasta);  m->gobble(inFasta);
                        
                        if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
                                if (namefile != "") {
@@ -223,12 +295,12 @@ int PreClusterCommand::readFASTA(){
                                        else{
                                                seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
                                                alignSeqs.push_back(tempNode);
-                                               if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
+                                               if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
                                        }       
                                }else { //no names file, you are identical to yourself 
                                        seqPNode tempNode(1, seq, seq.getName());
                                        alignSeqs.push_back(tempNode);
-                                       if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
+                                       if (seq.getAligned().length() > length) {  length = seq.getAligned().length();  }
                                }
                        }
                }
@@ -270,8 +342,8 @@ void PreClusterCommand::printData(string newfasta, string newname){
                ofstream outFasta;
                ofstream outNames;
                
-               openOutputFile(newfasta, outFasta);
-               openOutputFile(newname, outNames);
+               m->openOutputFile(newfasta, outFasta);
+               m->openOutputFile(newname, outNames);
                                
                
                for (int i = 0; i < alignSeqs.size(); i++) {
@@ -290,22 +362,21 @@ void PreClusterCommand::printData(string newfasta, string newname){
                exit(1);
        }
 }
-
 /**************************************************************************************************/
 
 void PreClusterCommand::readNameFile(){
        try {
                ifstream in;
-               openInputFile(namefile, in);
+               m->openInputFile(namefile, in);
                string firstCol, secondCol;
                                
                while (!in.eof()) {
-                       in >> firstCol >> secondCol; gobble(in);
+                       in >> firstCol >> secondCol; m->gobble(in);
                        names[firstCol] = secondCol;
                        int size = 1;
-                       while (secondCol.find_first_of(',') != -1) { 
-                               size++;
-                               secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
+
+                       for(int i=0;i<secondCol.size();i++){
+                               if(secondCol[i] == ','){        size++; }
                        }
                        sizes[firstCol] = size;
                }