]> git.donarmstrong.com Git - mothur.git/blobdiff - screenseqscommand.cpp
finished shhh.seqs command, fixed bug with remove.groups and get.groups that caused...
[mothur.git] / screenseqscommand.cpp
index 7318f9060cdbf5fa2aa5d23205163c1dfecc048a..cedb74a9b6c7f0a4162cdc96d9b774105e6cc1d0 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");
@@ -94,6 +97,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option)  {
                
                //allow user to run help
                if(option == "help") { help(); abort = true; calledHelp = true; }
+               else if(option == "citation") { citation(); abort = true; calledHelp = true;}
                
                else {
                        vector<string> myArray = setParameters();
@@ -117,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);              
@@ -163,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
@@ -172,23 +184,31 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option)  {
                                if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
                                else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
                        }
-                       else if (fastafile == "not open") { abort = true; }     
+                       else if (fastafile == "not open") { abort = true; }
+                       else { m->setFastaFile(fastafile); }
        
                        groupfile = validParameter.validFile(parameters, "group", true);
                        if (groupfile == "not open") { abort = true; }  
                        else if (groupfile == "not found") { groupfile = ""; }
+                       else { m->setGroupFile(groupfile); }
                        
                        qualfile = validParameter.validFile(parameters, "qfile", true);
                        if (qualfile == "not open") { abort = true; }   
                        else if (qualfile == "not found") { qualfile = ""; }
+                       else { m->setQualFile(qualfile); }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { namefile = ""; abort = true; }
                        else if (namefile == "not found") { namefile = ""; }    
-
+                       else { m->setNameFile(namefile); }
+                       
                        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"){  
@@ -256,7 +276,7 @@ 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); }
@@ -279,7 +299,7 @@ int ScreenSeqsCommand::execute(){
 #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
@@ -389,7 +409,7 @@ int ScreenSeqsCommand::execute(){
                        if(processors == 1){
                                numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
                                
-                               if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
+                               if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
                                
                        }else{
                                processIDS.resize(0);
@@ -402,13 +422,13 @@ int ScreenSeqsCommand::execute(){
                                //append alignment and report files
                                for(int i=1;i<processors;i++){
                                        m->appendFiles((goodSeqFile + toString(processIDS[i]) + ".temp"), goodSeqFile);
-                                       remove((goodSeqFile + toString(processIDS[i]) + ".temp").c_str());
+                                       m->mothurRemove((goodSeqFile + toString(processIDS[i]) + ".temp"));
                        
                                        m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile);
-                                       remove((badAccnosFile + toString(processIDS[i]) + ".temp").c_str());
+                                       m->mothurRemove((badAccnosFile + toString(processIDS[i]) + ".temp"));
                                }
                                
-                               if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
+                               if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
                                
                                //read badSeqs in because root process doesnt know what other "bad" seqs the children found
                                ifstream inBad;
@@ -427,7 +447,7 @@ int ScreenSeqsCommand::execute(){
        #else
                        numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
                        
-                       if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
+                       if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
                        
        #endif
 
@@ -468,18 +488,19 @@ int ScreenSeqsCommand::execute(){
                                                                                                                                                                        
                if(namefile != "" && groupfile != "")   {       
                        screenNameGroupFile(badSeqNames);       
-                       if (m->control_pressed) {  remove(goodSeqFile.c_str()); return 0; }
+                       if (m->control_pressed) {  m->mothurRemove(goodSeqFile); return 0; }
                }else if(namefile != "")        {       
                        screenNameGroupFile(badSeqNames);
-                       if (m->control_pressed) {  remove(goodSeqFile.c_str());  return 0; }    
+                       if (m->control_pressed) {  m->mothurRemove(goodSeqFile);  return 0; }   
                }else if(groupfile != "")                               {       screenGroupFile(badSeqNames);           }       // this screens just the group
                
-               if (m->control_pressed) { remove(goodSeqFile.c_str());  return 0; }
+               if (m->control_pressed) { m->mothurRemove(goodSeqFile);  return 0; }
 
                if(alignreport != "")                                   {       screenAlignReport(badSeqNames);         }
                if(qualfile != "")                                              {       screenQual(badSeqNames);                        }
+               if(taxonomy != "")                                              {       screenTaxonomy(badSeqNames);            }
                
-               if (m->control_pressed) { remove(goodSeqFile.c_str());  return 0; }
+               if (m->control_pressed) { m->mothurRemove(goodSeqFile);  return 0; }
                
                #ifdef USE_MPI
                        }
@@ -514,6 +535,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();
@@ -542,7 +568,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
                ofstream goodNameOut;   m->openOutputFile(goodNameFile, goodNameOut);
                
                while(!inputNames.eof()){
-                       if (m->control_pressed) { goodNameOut.close();  inputNames.close(); remove(goodNameFile.c_str());  return 0; }
+                       if (m->control_pressed) { goodNameOut.close();  inputNames.close(); m->mothurRemove(goodNameFile);  return 0; }
 
                        inputNames >> seqName >> seqList;
                        it = badSeqNames.find(seqName);
@@ -588,7 +614,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
                        ofstream goodGroupOut;  m->openOutputFile(goodGroupFile, goodGroupOut);
                        
                        while(!inputGroups.eof()){
-                               if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str());  remove(goodGroupFile.c_str()); return 0; }
+                               if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodNameFile);  m->mothurRemove(goodGroupFile); return 0; }
 
                                inputGroups >> seqName >> group;
                                
@@ -624,7 +650,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;
@@ -633,12 +659,22 @@ int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
                vector<int> ambigBases;
                vector<int> longHomoPolymer;
                
-               vector<unsigned long int> positions = m->divideFile(fastafile, processors);
+               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)]));
                }       
                
+               
+#ifdef USE_MPI
+               MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
+               
+               if (pid == 0) { //only one process should fix files
+                       driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
+               }
+               
+               MPI_Barrier(MPI_COMM_WORLD); //make everyone wait
+#else
                int numSeqs = 0;
                #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
                        if(processors == 1){
@@ -652,7 +688,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());
@@ -719,7 +755,7 @@ int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<in
                        }
                        
                        #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; }
@@ -801,7 +837,7 @@ int ScreenSeqsCommand::createProcessesCreateSummary(vector<int>& startPosition,
                        for (int k = 0; k < tempNum; k++)                       {               in >> temp; longHomoPolymer.push_back(temp);    }               m->gobble(in);
                                
                        in.close();
-                       remove(tempFilename.c_str());
+                       m->mothurRemove(tempFilename);
                }
                
                return num;
@@ -827,7 +863,7 @@ int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
                ofstream goodGroupOut;  m->openOutputFile(goodGroupFile, goodGroupOut);
                
                while(!inputGroups.eof()){
-                       if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
+                       if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodGroupFile); return 0; }
 
                        inputGroups >> seqName >> group;
                        it = badSeqNames.find(seqName);
@@ -841,7 +877,7 @@ int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
                        m->gobble(inputGroups);
                }
                
-               if (m->control_pressed) { goodGroupOut.close();  inputGroups.close(); remove(goodGroupFile.c_str());  return 0; }
+               if (m->control_pressed) { goodGroupOut.close();  inputGroups.close(); m->mothurRemove(goodGroupFile);  return 0; }
 
                //we were unable to remove some of the bad sequences
                if (badSeqNames.size() != 0) {
@@ -854,7 +890,7 @@ int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
                inputGroups.close();
                goodGroupOut.close();
                
-               if (m->control_pressed) { remove(goodGroupFile.c_str());   }
+               if (m->control_pressed) { m->mothurRemove(goodGroupFile);   }
                
                return 0;
        
@@ -885,7 +921,7 @@ int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
                }
 
                while(!inputAlignReport.eof()){
-                       if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
+                       if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); m->mothurRemove(goodAlignReportFile); return 0; }
 
                        inputAlignReport >> seqName;
                        it = badSeqNames.find(seqName);
@@ -905,7 +941,7 @@ int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
                        m->gobble(inputAlignReport);
                }
                
-               if (m->control_pressed) { goodAlignReportOut.close();  inputAlignReport.close(); remove(goodAlignReportFile.c_str());  return 0; }
+               if (m->control_pressed) { goodAlignReportOut.close();  inputAlignReport.close(); m->mothurRemove(goodAlignReportFile);  return 0; }
 
                //we were unable to remove some of the bad sequences
                if (badSeqNames.size() != 0) {
@@ -918,7 +954,7 @@ int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
                inputAlignReport.close();
                goodAlignReportOut.close();
                                
-               if (m->control_pressed) {  remove(goodAlignReportFile.c_str());  return 0; }
+               if (m->control_pressed) {  m->mothurRemove(goodAlignReportFile);  return 0; }
                
                return 0;
        
@@ -931,6 +967,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;
@@ -943,7 +1029,7 @@ int ScreenSeqsCommand::screenQual(set<string> badSeqNames){
                
                while(!in.eof()){       
                        
-                       if (m->control_pressed) { goodQual.close(); in.close(); remove(goodQualFile.c_str()); return 0; }
+                       if (m->control_pressed) { goodQual.close(); in.close(); m->mothurRemove(goodQualFile); return 0; }
 
                        string saveName = "";
                        string name = "";
@@ -991,7 +1077,7 @@ int ScreenSeqsCommand::screenQual(set<string> badSeqNames){
                        }
                }
                
-               if (m->control_pressed) {  remove(goodQualFile.c_str());  return 0; }
+               if (m->control_pressed) {  m->mothurRemove(goodQualFile);  return 0; }
                
                return 0;
                
@@ -1045,7 +1131,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; }
@@ -1071,7 +1157,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; 
@@ -1191,7 +1277,7 @@ int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, st
                        string tempFile =  filename + toString(processIDS[i]) + ".num.temp";
                        m->openInputFile(tempFile, in);
                        if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
-                       in.close(); remove(tempFile.c_str());
+                       in.close(); m->mothurRemove(tempFile);
                }
                
                return num;