]> git.donarmstrong.com Git - mothur.git/blobdiff - screenseqscommand.cpp
metastats in progress
[mothur.git] / screenseqscommand.cpp
index f751525b6b1fa6211ab6017063ba6c7090f6e484..ec2d8e003a31a712de8c32d255d164b6b13cd46c 100644 (file)
@@ -18,6 +18,7 @@ vector<string> ScreenSeqsCommand::setParameters(){
                CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
                CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pqfile);
                CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(palignreport);
+               CommandParameter ptax("taxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(ptax);
                CommandParameter pstart("start", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pstart);
                CommandParameter pend("end", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pend);
                CommandParameter pmaxambig("maxambig", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxambig);
@@ -44,8 +45,9 @@ string ScreenSeqsCommand::getHelpString(){
        try {
                string helpString = "";
                helpString += "The screen.seqs command reads a fastafile and creates .....\n";
-               helpString += "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, qfile, optimize, criteria and processors.\n";
+               helpString += "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, qfile, alignreport, taxonomy, optimize, criteria and processors.\n";
                helpString += "The fasta parameter is required.\n";
+               helpString += "The alignreport and taxonomy parameters allow you to remove bad seqs from taxonomy and alignreport files.\n";
                helpString += "The start parameter .... The default is -1.\n";
                helpString += "The end parameter .... The default is -1.\n";
                helpString += "The maxambig parameter allows you to set the maximum number of ambigious bases allowed. The default is -1.\n";
@@ -80,6 +82,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(){
                outputTypes["alignreport"] = tempOutNames;
                outputTypes["accnos"] = tempOutNames;
                outputTypes["qfile"] = tempOutNames;
+               outputTypes["taxonomy"] = tempOutNames;
        }
        catch(exception& e) {
                m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
@@ -118,6 +121,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option)  {
                        outputTypes["alignreport"] = tempOutNames;
                        outputTypes["accnos"] = tempOutNames;
                        outputTypes["qfile"] = tempOutNames;
+                       outputTypes["taxonomy"] = 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);              
@@ -164,6 +168,13 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option)  {
                                        if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
                                }
                                
+                               it = parameters.find("taxonomy");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = m->hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
+                               }
                        }
 
                        //check for required parameters
@@ -193,7 +204,11 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option)  {
                        
                        alignreport = validParameter.validFile(parameters, "alignreport", true);
                        if (alignreport == "not open") { abort = true; }
-                       else if (alignreport == "not found") { alignreport = ""; }      
+                       else if (alignreport == "not found") { alignreport = ""; }
+                       
+                       taxonomy = validParameter.validFile(parameters, "taxonomy", true);
+                       if (taxonomy == "not open") { abort = true; }
+                       else if (taxonomy == "not found") { taxonomy = ""; }    
                        
                        //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"){  
@@ -261,17 +276,22 @@ int ScreenSeqsCommand::execute(){
                if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
                
                //if the user want to optimize we need to know the 90% mark
-               vector<unsigned long int> positions;
+               vector<unsigned long long> positions;
                if (optimize.size() != 0) {  //get summary is paralellized so we need to divideFile, no need to do this step twice so I moved it here
                        //use the namefile to optimize correctly
                        if (namefile != "") { nameMap = m->readNames(namefile); }
                        getSummary(positions); 
                } 
                else { 
+                       #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        positions = m->divideFile(fastafile, processors);
                        for (int i = 0; i < (positions.size()-1); i++) {
                                lines.push_back(new linePair(positions[i], positions[(i+1)]));
-                       }       
+                       }
+                       #else 
+                               positions.push_back(0); positions.push_back(1000);
+                               lines.push_back(new linePair(0, 1000));
+                       #endif
                }
                                
                string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile);
@@ -280,16 +300,16 @@ int ScreenSeqsCommand::execute(){
                int numFastaSeqs = 0;
                set<string> badSeqNames;
                int start = time(NULL);
-               
+       
 #ifdef USE_MPI 
                        int pid, numSeqsPerProcessor; 
                        int tag = 2001;
-                       vector<unsigned long int> MPIPos;
+                       vector<unsigned long long> MPIPos;
                        
                        MPI_Status status; 
                        MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
                        MPI_Comm_size(MPI_COMM_WORLD, &processors); 
-
+       
                        MPI_File inMPI;
                        MPI_File outMPIGood;
                        MPI_File outMPIBadAccnos;
@@ -483,6 +503,7 @@ int ScreenSeqsCommand::execute(){
 
                if(alignreport != "")                                   {       screenAlignReport(badSeqNames);         }
                if(qualfile != "")                                              {       screenQual(badSeqNames);                        }
+               if(taxonomy != "")                                              {       screenTaxonomy(badSeqNames);            }
                
                if (m->control_pressed) { m->mothurRemove(goodSeqFile);  return 0; }
                
@@ -519,6 +540,11 @@ int ScreenSeqsCommand::execute(){
                if (itTypes != outputTypes.end()) {
                        if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
                }
+               
+               itTypes = outputTypes.find("taxonomy");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
+               }
 
                m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences.");
                m->mothurOutEndLine();
@@ -629,7 +655,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
        }
 }
 //***************************************************************************************************************
-int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
+int ScreenSeqsCommand::getSummary(vector<unsigned long long>& positions){
        try {
                
                vector<int> startPosition;
@@ -638,12 +664,23 @@ int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
                vector<int> ambigBases;
                vector<int> longHomoPolymer;
                
-               vector<unsigned long int> positions = m->divideFile(fastafile, processors);
-                               
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+               vector<unsigned long long> positions = m->divideFile(fastafile, processors);
+               
                for (int i = 0; i < (positions.size()-1); i++) {
                        lines.push_back(new linePair(positions[i], positions[(i+1)]));
                }       
+#else
+               lines.push_back(new linePair(0, 1000));
+#endif
                
+#ifdef USE_MPI
+               int pid;
+               MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
+               
+               if (pid == 0) { 
+                       driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
+#else
                int numSeqs = 0;
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        if(processors == 1){
@@ -657,7 +694,7 @@ int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
                        numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
                        if (m->control_pressed) {  return 0; }
                #endif
-
+#endif
                sort(startPosition.begin(), startPosition.end());
                sort(endPosition.begin(), endPosition.end());
                sort(seqLength.begin(), seqLength.end());
@@ -676,6 +713,33 @@ int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
                        else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); }
                }
 
+#ifdef USE_MPI
+               }
+               
+               MPI_Status status; 
+               MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
+               MPI_Comm_size(MPI_COMM_WORLD, &processors); 
+                       
+               if (pid == 0) { 
+                       //send file positions to all processes
+                       for(int i = 1; i < processors; i++) { 
+                               MPI_Send(&startPos, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                               MPI_Send(&endPos, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                               MPI_Send(&maxAmbig, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                               MPI_Send(&maxHomoP, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                               MPI_Send(&minLength, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                               MPI_Send(&maxLength, 1, MPI_INT, i, 2001, MPI_COMM_WORLD);
+                       }
+               }else {
+                       MPI_Recv(&startPos, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+                       MPI_Recv(&endPos, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+                       MPI_Recv(&maxAmbig, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+                       MPI_Recv(&maxHomoP, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+                       MPI_Recv(&minLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+                       MPI_Recv(&maxLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status);
+               }
+               MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
+#endif
                return 0;
        }
        catch(exception& e) {
@@ -722,9 +786,9 @@ int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<in
                                
                                count++;
                        }
-                       
+                       //if((count) % 100 == 0){       m->mothurOut("Optimizing sequence: " + toString(count)); m->mothurOutEndLine();         }
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                               unsigned long int pos = in.tellg();
+                               unsigned long long pos = in.tellg();
                                if ((pos == -1) || (pos >= filePos->end)) { break; }
                        #else
                                if (in.eof()) { break; }
@@ -936,6 +1000,56 @@ int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
 }
 //***************************************************************************************************************
 
+int ScreenSeqsCommand::screenTaxonomy(set<string> badSeqNames){
+       try {
+               ifstream input;
+               m->openInputFile(taxonomy, input);
+               string seqName, tax;
+               set<string>::iterator it;
+               
+               string goodTaxFile = outputDir + m->getRootName(m->getSimpleName(taxonomy)) + "good" + m->getExtension(taxonomy);
+               outputNames.push_back(goodTaxFile);  outputTypes["taxonomy"].push_back(goodTaxFile);
+               ofstream goodTaxOut;    m->openOutputFile(goodTaxFile, goodTaxOut);
+                               
+               while(!input.eof()){
+                       if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; }
+                       
+                       input >> seqName >> tax;
+                       it = badSeqNames.find(seqName);
+                       
+                       if(it != badSeqNames.end()){ badSeqNames.erase(it); }
+                       else{
+                               goodTaxOut << seqName << '\t' << tax << endl;
+                       }
+                       m->gobble(input);
+               }
+               
+               if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; }
+               
+               //we were unable to remove some of the bad sequences
+               if (badSeqNames.size() != 0) {
+                       for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {  
+                               m->mothurOut("Your taxonomy file does not include the sequence " + *it + " please correct."); 
+                               m->mothurOutEndLine();
+                       }
+               }
+               
+               input.close();
+               goodTaxOut.close();
+               
+               if (m->control_pressed) {  m->mothurRemove(goodTaxFile);  return 0; }
+               
+               return 0;
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ScreenSeqsCommand", "screenTaxonomy");
+               exit(1);
+       }
+       
+}
+//***************************************************************************************************************
+
 int ScreenSeqsCommand::screenQual(set<string> badSeqNames){
        try {
                ifstream in;
@@ -1050,7 +1164,7 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc
                        }
                        
                        #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-                               unsigned long int pos = inFASTA.tellg();
+                               unsigned long long pos = inFASTA.tellg();
                                if ((pos == -1) || (pos >= filePos->end)) { break; }
                        #else
                                if (inFASTA.eof()) { break; }
@@ -1076,7 +1190,7 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc
 }
 //**********************************************************************************************************************
 #ifdef USE_MPI
-int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long int>& MPIPos, set<string>& badSeqNames){
+int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long long>& MPIPos, set<string>& badSeqNames){
        try {
                string outputString = "";
                MPI_Status statusGood;