]> git.donarmstrong.com Git - mothur.git/blobdiff - seqerrorcommand.cpp
added otu.association command. added calcSpearman, calcKendall and calcPearson functi...
[mothur.git] / seqerrorcommand.cpp
index 007ac93df67d1e93371abd1898c9cce9c0823233..d8ebe50fb11859367256a28286dec9789cc70d36 100644 (file)
@@ -13,6 +13,7 @@
 #include "refchimeratest.h"
 #include "filterseqscommand.h"
 
+
 //**********************************************************************************************************************
 vector<string> SeqErrorCommand::setParameters(){       
        try {
@@ -24,6 +25,7 @@ vector<string> SeqErrorCommand::setParameters(){
                CommandParameter pignorechimeras("ignorechimeras", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pignorechimeras);
                CommandParameter pthreshold("threshold", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pthreshold);
                CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
+               CommandParameter psave("save", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(psave);
                CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
                CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
                
@@ -41,6 +43,15 @@ string SeqErrorCommand::getHelpString(){
        try {
                string helpString = "";
                helpString += "The seq.error command reads a query alignment file and a reference alignment file and creates .....\n";
+               helpString += "The fasta parameter...\n";
+               helpString += "The reference parameter...\n";
+               helpString += "The qfile parameter...\n";
+               helpString += "The report parameter...\n";
+               helpString += "The name parameter...\n";
+               helpString += "The ignorechimeras parameter...\n";
+               helpString += "The threshold parameter...\n";
+               helpString += "The processors parameter...\n";
+               helpString += "If the save parameter is set to true the reference sequences will be saved in memory, to clear them later you can use the clear.memory command. Default=f.";
                helpString += "Example seq.error(...).\n";
                helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
                helpString += "For more details please check out the wiki http://www.mothur.org/wiki/seq.error .\n";
@@ -55,6 +66,7 @@ string SeqErrorCommand::getHelpString(){
 SeqErrorCommand::SeqErrorCommand(){    
        try {
                abort = true; calledHelp = true; 
+               setParameters();
                vector<string> tempOutNames;
                outputTypes["error.summary"] = tempOutNames;
                outputTypes["error.seq"] = tempOutNames;
@@ -77,6 +89,7 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
        try {
                
                abort = false; calledHelp = false;   
+               rdb = ReferenceDB::getInstance();
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
@@ -163,14 +176,13 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                                if (queryFileName != "") { m->mothurOut("Using " + queryFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
                                else {  m->mothurOut("You have no current fasta file and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
                        }
-                       else if (queryFileName == "not open") { abort = true; } 
+                       else if (queryFileName == "not open") { queryFileName = ""; abort = true; }     
                        else { m->setFastaFile(queryFileName); }
                        
                        referenceFileName = validParameter.validFile(parameters, "reference", true);
                        if (referenceFileName == "not found") { m->mothurOut("reference is a required parameter for the seq.error command."); m->mothurOutEndLine(); abort = true; }
                        else if (referenceFileName == "not open") { abort = true; }     
                        
-
                        //check for optional parameters
                        namesFileName = validParameter.validFile(parameters, "name", true);
                        if(namesFileName == "not found"){       namesFileName = "";     }
@@ -201,17 +213,44 @@ SeqErrorCommand::SeqErrorCommand(string option)  {
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
                        temp = validParameter.validFile(parameters, "threshold", false);        if (temp == "not found") { temp = "1.00"; }
-                       convert(temp, threshold);  
+                       m->mothurConvert(temp, threshold);  
+                       
+                       temp = validParameter.validFile(parameters, "save", false);                     if (temp == "not found"){       temp = "f";                             }
+                       save = m->isTrue(temp); 
+                       rdb->save = save; 
+                       if (save) { //clear out old references
+                               rdb->clearMemory();     
+                       }
+                       
+                       //this has to go after save so that if the user sets save=t and provides no reference we abort
+                       referenceFileName = validParameter.validFile(parameters, "reference", true);
+                       if (referenceFileName == "not found") { 
+                               //check for saved reference sequences
+                               if (rdb->referenceSeqs.size() != 0) {
+                                       referenceFileName = "saved";
+                               }else {
+                                       m->mothurOut("[ERROR]: You don't have any saved reference sequences and the reference parameter is a required."); 
+                                       m->mothurOutEndLine();
+                                       abort = true; 
+                               }
+                       }else if (referenceFileName == "not open") { abort = true; }    
+                       else {  if (save) {     rdb->setSavedReference(referenceFileName);      }       }
+                       
                        
                        temp = validParameter.validFile(parameters, "ignorechimeras", false);   if (temp == "not found") { temp = "T"; }
                        ignoreChimeras = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
                        m->setProcessors(temp);
-                       convert(temp, processors); 
+                       m->mothurConvert(temp, processors); 
 
                        substitutionMatrix.resize(6);
                        for(int i=0;i<6;i++){   substitutionMatrix[i].resize(6,0);      }
+                       
+                       if ((namesFileName == "") && (queryFileName != "")){
+                               vector<string> files; files.push_back(queryFileName); 
+                               parser.getNameFile(files);
+                       }
                }
        }
        catch(exception& e) {
@@ -243,9 +282,9 @@ int SeqErrorCommand::execute(){
 
                if(namesFileName != ""){        weights = getWeights(); }
                
-               vector<unsigned long int> fastaFilePos;
-               vector<unsigned long int> qFilePos;
-               vector<unsigned long int> reportFilePos;
+               vector<unsigned long long> fastaFilePos;
+               vector<unsigned long long> qFilePos;
+               vector<unsigned long long> reportFilePos;
                
                setLines(queryFileName, qualFileName, reportFileName, fastaFilePos, qFilePos, reportFilePos);
                
@@ -276,7 +315,7 @@ int SeqErrorCommand::execute(){
                
                printErrorFRFile(errorForward, errorReverse);
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
 
                string errorCountFileName = queryFileName.substr(0,queryFileName.find_last_of('.')) + ".error.count";
                ofstream errorCountFile;
@@ -291,7 +330,7 @@ int SeqErrorCommand::execute(){
                }
                errorCountFile.close();
                
-               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
+               if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
 
                printSubMatrix();
                                
@@ -448,11 +487,11 @@ int SeqErrorCommand::createProcesses(string filename, string qFileName, string r
                        m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
                        
                        m->appendFiles((summaryFileName + toString(processIDS[i]) + ".temp"), summaryFileName);
-                       remove((summaryFileName + toString(processIDS[i]) + ".temp").c_str());
+                       m->mothurRemove((summaryFileName + toString(processIDS[i]) + ".temp"));
                        m->appendFiles((errorOutputFileName + toString(processIDS[i]) + ".temp"), errorOutputFileName);
-                       remove((errorOutputFileName + toString(processIDS[i]) + ".temp").c_str());
+                       m->mothurRemove((errorOutputFileName + toString(processIDS[i]) + ".temp"));
                        m->appendFiles((chimeraOutputFileName + toString(processIDS[i]) + ".temp"), chimeraOutputFileName);
-                       remove((chimeraOutputFileName + toString(processIDS[i]) + ".temp").c_str());
+                       m->mothurRemove((chimeraOutputFileName + toString(processIDS[i]) + ".temp"));
                        
                        ifstream in;
                        string tempFile =  filename + toString(processIDS[i]) + ".info.temp";
@@ -547,7 +586,7 @@ int SeqErrorCommand::createProcesses(string filename, string qFileName, string r
                        }
                        m->gobble(in);
                        
-                       in.close(); remove(tempFile.c_str());
+                       in.close(); m->mothurRemove(tempFile);
                        
                }
 #endif         
@@ -701,7 +740,7 @@ int SeqErrorCommand::driver(string filename, string qFileName, string rFileName,
                        index++;
                        
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                               unsigned long int pos = queryFile.tellg();
+                               unsigned long long pos = queryFile.tellg();
                                if ((pos == -1) || (pos >= line.end)) { break; }
                        #else
                                if (queryFile.eof()) { break; }
@@ -728,32 +767,59 @@ int SeqErrorCommand::driver(string filename, string qFileName, string rFileName,
 
 void SeqErrorCommand::getReferences(){
        try {
-               
-               ifstream referenceFile;
-               m->openInputFile(referenceFileName, referenceFile);
-               
                int numAmbigSeqs = 0;
                
                int maxStartPos = 0;
                int minEndPos = 100000;
                
-               while(referenceFile){
-                       Sequence currentSeq(referenceFile);
-                       int numAmbigs = currentSeq.getAmbigBases();
-                       if(numAmbigs > 0){      numAmbigSeqs++; }
-                       
-//                     int startPos = currentSeq.getStartPos();
-//                     if(startPos > maxStartPos)      {       maxStartPos = startPos; }
-//
-//                     int endPos = currentSeq.getEndPos();
-//                     if(endPos < minEndPos)          {       minEndPos = endPos;             }
-                       referenceSeqs.push_back(currentSeq);
+               if (referenceFileName == "saved") {
+                       int start = time(NULL);
+                       m->mothurOutEndLine();  m->mothurOut("Using sequences from " + rdb->getSavedReference() + " that are saved in memory.");        m->mothurOutEndLine();
+                       
+                       for (int i = 0; i < rdb->referenceSeqs.size(); i++) {
+                               int numAmbigs = rdb->referenceSeqs[i].getAmbigBases();
+                               if(numAmbigs > 0){      numAmbigSeqs++; }
+                               
+                               //                      int startPos = rdb->referenceSeqs[i].getStartPos();
+                               //                      if(startPos > maxStartPos)      {       maxStartPos = startPos; }
+                               //
+                               //                      int endPos = rdb->referenceSeqs[i].getEndPos();
+                               //                      if(endPos < minEndPos)          {       minEndPos = endPos;             }                               
                                
-                       m->gobble(referenceFile);
+                               referenceSeqs.push_back(rdb->referenceSeqs[i]);
+                       }
+                       referenceFileName = rdb->getSavedReference();
+                       
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to load " + toString(rdb->referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();  
+               
+               }else {
+                       int start = time(NULL);
+
+                       ifstream referenceFile;
+                       m->openInputFile(referenceFileName, referenceFile);
+                       
+                       while(referenceFile){
+                               Sequence currentSeq(referenceFile);
+                               int numAmbigs = currentSeq.getAmbigBases();
+                               if(numAmbigs > 0){      numAmbigSeqs++; }
+                               
+       //                      int startPos = currentSeq.getStartPos();
+       //                      if(startPos > maxStartPos)      {       maxStartPos = startPos; }
+       //
+       //                      int endPos = currentSeq.getEndPos();
+       //                      if(endPos < minEndPos)          {       minEndPos = endPos;             }
+                               referenceSeqs.push_back(currentSeq);
+                               
+                               if (rdb->save) { rdb->referenceSeqs.push_back(currentSeq); }
+                                       
+                               m->gobble(referenceFile);
+                       }
+                       referenceFile.close();
+                       
+                       m->mothurOut("It took " + toString(time(NULL) - start) + " to read " + toString(referenceSeqs.size()) + " sequences.");m->mothurOutEndLine();  
                }
-               referenceFile.close();
+               
                numRefs = referenceSeqs.size();
-
                
                for(int i=0;i<numRefs;i++){
                        referenceSeqs[i].padToPos(maxStartPos);
@@ -762,7 +828,7 @@ void SeqErrorCommand::getReferences(){
                
                if(numAmbigSeqs != 0){
                        m->mothurOut("Warning: " + toString(numAmbigSeqs) + " reference sequences have ambiguous bases, these bases will be ignored\n");
-               }               
+               }       
                
        }
        catch(exception& e) {
@@ -1147,7 +1213,7 @@ void SeqErrorCommand::printQualityFR(vector<vector<int> > qualForwardMap, vector
 }
 /**************************************************************************************************/
 
-int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long int>& fastaFilePos, vector<unsigned long int>& qfileFilePos, vector<unsigned long int>& rfileFilePos) {
+int SeqErrorCommand::setLines(string filename, string qfilename, string rfilename, vector<unsigned long long>& fastaFilePos, vector<unsigned long long>& qfileFilePos, vector<unsigned long long>& rfileFilePos) {
        try {
 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                //set file positions for fasta file
@@ -1189,7 +1255,7 @@ int SeqErrorCommand::setLines(string filename, string qfilename, string rfilenam
                                        map<string, int>::iterator it = firstSeqNames.find(sname);
                                        
                                        if(it != firstSeqNames.end()) { //this is the start of a new chunk
-                                               unsigned long int pos = inQual.tellg(); 
+                                               unsigned long long pos = inQual.tellg(); 
                                                qfileFilePos.push_back(pos - input.length() - 1);       
                                                firstSeqNames.erase(it);
                                        }
@@ -1210,7 +1276,7 @@ int SeqErrorCommand::setLines(string filename, string qfilename, string rfilenam
                
                //get last file position of qfile
                FILE * pFile;
-               unsigned long int size;
+               unsigned long long size;
                
                //get num bytes in file
                pFile = fopen (qfilename.c_str(),"rb");
@@ -1248,7 +1314,7 @@ int SeqErrorCommand::setLines(string filename, string qfilename, string rfilenam
                                map<string, int>::iterator it = firstSeqNamesReport.find(sname);
                        
                                if(it != firstSeqNamesReport.end()) { //this is the start of a new chunk
-                                       unsigned long int pos = inR.tellg(); 
+                                       unsigned long long pos = inR.tellg(); 
                                        rfileFilePos.push_back(pos - input.length() - 1);       
                                        firstSeqNamesReport.erase(it);
                                }
@@ -1269,7 +1335,7 @@ int SeqErrorCommand::setLines(string filename, string qfilename, string rfilenam
                
                //get last file position of qfile
                FILE * rFile;
-               unsigned long int sizeR;
+               unsigned long long sizeR;
                
                //get num bytes in file
                rFile = fopen (rfilename.c_str(),"rb");
@@ -1289,7 +1355,7 @@ int SeqErrorCommand::setLines(string filename, string qfilename, string rfilenam
                fastaFilePos.push_back(0); qfileFilePos.push_back(0);
                //get last file position of fastafile
                FILE * pFile;
-               unsigned long int size;
+               unsigned long long size;
                
                //get num bytes in file
                pFile = fopen (filename.c_str(),"rb");