]> git.donarmstrong.com Git - mothur.git/blobdiff - preclustercommand.cpp
chimeras, fix to sabundvector and sharedsabundvector that caused getRabundVector...
[mothur.git] / preclustercommand.cpp
index 89e6cbfb9e2560e4860cae8dd6a0ee4632a687da..fa5329b1e4094b4c87d60b5b4dfbd211f1bc1aeb 100644 (file)
@@ -22,31 +22,59 @@ PreClusterCommand::PreClusterCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"fasta", "name", "diffs"};
+                       string Array[] =  {"fasta", "name", "diffs", "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 it2 = parameters.begin(); it2 != parameters.end(); it2++) { 
                                if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) {  abort = true;  }
                        }
                        
+                       //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("fasta");
+                               //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["fasta"] = 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;             }
+                               }
+                       }
+
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
                        if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; }
                        else if (fastafile == "not open") { abort = true; }     
                        
+                       //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  
+                       }
+
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
                        namefile = validParameter.validFile(parameters, "name", true);
-
                        if (namefile == "not found") { namefile =  "";  }
                        else if (namefile == "not open") { abort = true; }      
-//                     else {  readNameFile();  }
+                       else {  readNameFile();  }
                        
                        string temp     = validParameter.validFile(parameters, "diffs", false);                         if(temp == "not found"){        temp = "1"; }
                        convert(temp, diffs); 
@@ -88,7 +116,7 @@ int PreClusterCommand::execute(){
                if (abort == true) { return 0; }
                
                //reads fasta file and return number of seqs
-               int numSeqs = readNamesFASTA(); //fills alignSeqs and makes all seqs active
+               int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active
        
                if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0;  }
                if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0;  }
@@ -132,9 +160,11 @@ int PreClusterCommand::execute(){
                        }//end if active i
                        if(i % 100 == 0)        { cout << i << '\t' << numSeqs - count << '\t' << count << endl;        }
                }
-       
-               string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile);
-               string newNamesFile = getRootName(fastafile) + "precluster.names";
+               
+               string fileroot = outputDir + getRootName(getSimpleName(fastafile));
+               
+               string newFastaFile = fileroot + "precluster" + getExtension(fastafile);
+               string newNamesFile = fileroot + "precluster.names";
                
                
                mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine();
@@ -149,80 +179,60 @@ int PreClusterCommand::execute(){
                exit(1);
        }
 }
+
 /**************************************************************************************************/
-int PreClusterCommand::readFASTA(){
-       try {
-//             ifstream inFasta;
-//             openInputFile(fastafile, inFasta);
-//             length = 0;
-////           map<string, string>::iterator it;
-//
-//             while (!inFasta.eof()) {
-//                     Sequence temp(inFasta);  //read seq
-//                     
-//                     if (temp.getName() != "") {  
-//                             if (namefile != "") {
-//                                     //make sure fasta and name files match
-//                                     it = names.find(temp.getName());
-//                                     if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
-//                             }else { sizes[temp.getName()] = 1; }
-//                             
-//                             seqPNode tempNode(sizes[temp.getName()], temp);
-//                             alignSeqs.push_back(tempNode); 
-//                             active[temp.getName()] = true;
-//                     }
-//                     gobble(inFasta);
-//             }
-//             inFasta.close();
-//             
-//             if (alignSeqs.size() != 0) {  length = alignSeqs[0].seq.getAligned().length();  }
-//             
-               return alignSeqs.size();
-       }
-       catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "readFASTA");
-               exit(1);
-       }
-}
-/**************************************************************************************************/
 
-int PreClusterCommand::readNamesFASTA(){
+//this requires the names and fasta file to be in the same order
+
+int PreClusterCommand::readFASTA(){
        try {
-               ifstream inNames;
+               //ifstream inNames;
                ifstream inFasta;
                
-               openInputFile(namefile, inNames);
+               //openInputFile(namefile, inNames);
                openInputFile(fastafile, inFasta);
                
-               string firstCol, secondCol, nameString;
+               //string firstCol, secondCol, nameString;
                length = 0;
                
-               while (inFasta && inNames) {
+               while (!inFasta.eof()) {
        
-                       inNames >> firstCol >> secondCol;
-                       nameString = secondCol;
+                       //inNames >> firstCol >> secondCol;
+                       //nameString = secondCol;
                        
-                       gobble(inNames);
-                       int size = 1;
-                       while (secondCol.find_first_of(',') != -1) { 
-                               size++;
-                               secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
+                       //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);
+                       
+                       if (seq.getName() != "") {  //can get "" if commented line is at end of fasta file
+                               if (namefile != "") {
+                                       itSize = sizes.find(seq.getName());
+                                       
+                                       if (itSize == sizes.end()) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
+                                       else{
+                                               seqPNode tempNode(itSize->second, seq, names[seq.getName()]);
+                                               alignSeqs.push_back(tempNode);
+                                               if (seq.getAligned().length() > length) {  length = alignSeqs[0].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();  }
+                               }
                        }
-                       Sequence seq(inFasta);
-                       if (seq.getName() != firstCol) { mothurOut(seq.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); }
-                       else{
-                               seqPNode tempNode(size, seq, nameString);
-                               alignSeqs.push_back(tempNode);
-                               if (seq.getAligned().length() > length) {  length = alignSeqs[0].seq.getAligned().length();  }
-                       }                       
                }
                inFasta.close();
-               inNames.close();
+               //inNames.close();
                return alignSeqs.size();
        }
        
        catch(exception& e) {
-               errorOut(e, "PreClusterCommand", "readNamesFASTA");
+               errorOut(e, "PreClusterCommand", "readFASTA");
                exit(1);
        }
 }
@@ -277,4 +287,30 @@ void PreClusterCommand::printData(string newfasta, string newname){
 
 /**************************************************************************************************/
 
+void PreClusterCommand::readNameFile(){
+       try {
+               ifstream in;
+               openInputFile(namefile, in);
+               string firstCol, secondCol;
+                               
+               while (!in.eof()) {
+                       in >> firstCol >> secondCol; 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());
+                       }
+                       sizes[firstCol] = size;
+               }
+               in.close();
+       }
+       catch(exception& e) {
+               errorOut(e, "PreClusterCommand", "readNameFile");
+               exit(1);
+       }
+}
+
+/**************************************************************************************************/
+