X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=screenseqscommand.cpp;h=8c334cb217b00d709f7bfa35220dce6a8d6c5dad;hb=348de0f8b17d84ede77081dcf67bd6ef43496677;hp=d98a4cfc9f5bd27847f342434a7fda7ad817741a;hpb=260ae19c36cb11a53ddc5a75b5e507f8dd8b31d6;p=mothur.git diff --git a/screenseqscommand.cpp b/screenseqscommand.cpp index d98a4cf..8c334cb 100644 --- a/screenseqscommand.cpp +++ b/screenseqscommand.cpp @@ -10,18 +10,70 @@ #include "screenseqscommand.h" #include "sequence.hpp" +//********************************************************************************************************************** +vector ScreenSeqsCommand::getValidParameters(){ + try { + string Array[] = {"fasta", "start", "end", "maxambig", "maxhomop","optimize","criteria", "minlength", "maxlength", + "name", "group", "alignreport","processors","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getValidParameters"); + exit(1); + } +} +//********************************************************************************************************************** +ScreenSeqsCommand::ScreenSeqsCommand(){ + try { + abort = true; calledHelp = true; + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["alignreport"] = tempOutNames; + outputTypes["accnos"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand"); + exit(1); + } +} +//********************************************************************************************************************** +vector ScreenSeqsCommand::getRequiredParameters(){ + try { + string Array[] = {"fasta"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getRequiredParameters"); + exit(1); + } +} +//********************************************************************************************************************** +vector ScreenSeqsCommand::getRequiredFiles(){ + try { + vector myArray; + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getRequiredFiles"); + exit(1); + } +} //*************************************************************************************************************** ScreenSeqsCommand::ScreenSeqsCommand(string option) { try { - abort = false; + abort = false; calledHelp = false; //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } else { //valid paramters for this command - string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength", + string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop","optimize","criteria", "minlength", "maxlength", "name", "group", "alignreport","processors","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); @@ -36,6 +88,14 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["alignreport"] = tempOutNames; + outputTypes["accnos"] = 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 = ""; } @@ -84,7 +144,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { else if (groupfile == "not found") { groupfile = ""; } namefile = validParameter.validFile(parameters, "name", true); - if (namefile == "not open") { abort = true; } + if (namefile == "not open") { namefile = ""; abort = true; } else if (namefile == "not found") { namefile = ""; } alignreport = validParameter.validFile(parameters, "alignreport", true); @@ -120,7 +180,25 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; } convert(temp, processors); - + + temp = validParameter.validFile(parameters, "optimize", false); //optimizing trumps the optimized values original value + if (temp == "not found"){ temp = ""; } + else { m->splitAtDash(temp, optimize); } + + //check for invalid optimize options + set validOptimizers; + validOptimizers.insert("start"); validOptimizers.insert("end"); validOptimizers.insert("maxambig"); validOptimizers.insert("maxhomop"); validOptimizers.insert("minlength"); validOptimizers.insert("maxlength"); + + for (int i = 0; i < optimize.size(); i++) { + if (validOptimizers.count(optimize[i]) == 0) { + m->mothurOut(optimize[i] + " is not a valid optimizer. Valid options are start, end, maxambig, maxhomop, minlength and maxlength."); m->mothurOutEndLine(); + optimize.erase(optimize.begin()+i); + i--; + } + } + + temp = validParameter.validFile(parameters, "criteria", false); if (temp == "not found"){ temp = "90"; } + convert(temp, criteria); } } @@ -134,7 +212,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { void ScreenSeqsCommand::help(){ try { m->mothurOut("The screen.seqs command reads a fastafile and creates .....\n"); - m->mothurOut("The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group and processors.\n"); + m->mothurOut("The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, optimize, criteria and processors.\n"); m->mothurOut("The fasta parameter is required.\n"); m->mothurOut("The start parameter .... The default is -1.\n"); m->mothurOut("The end parameter .... The default is -1.\n"); @@ -143,6 +221,8 @@ void ScreenSeqsCommand::help(){ m->mothurOut("The minlength parameter .... The default is -1.\n"); m->mothurOut("The maxlength parameter .... The default is -1.\n"); m->mothurOut("The processors parameter allows you to specify the number of processors to use while running the command. The default is 1.\n"); + m->mothurOut("The optimize and criteria parameters allow you set the start, end, maxabig, maxhomop, minlength and maxlength parameters relative to your set of sequences .\n"); + m->mothurOut("For example optimize=start-end, criteria=90, would set the start and end values to the position 90% of your sequences started and ended.\n"); m->mothurOut("The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n"); m->mothurOut("The screen.seqs command should be in the following format: \n"); m->mothurOut("screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, \n"); @@ -166,7 +246,21 @@ ScreenSeqsCommand::~ScreenSeqsCommand(){ /* do nothing */ } int ScreenSeqsCommand::execute(){ try{ - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + //if the user want to optimize we need to know the 90% mark + vector 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 { + positions = m->divideFile(fastafile, processors); + for (int i = 0; i < (positions.size()-1); i++) { + lines.push_back(new linePair(positions[i], positions[(i+1)])); + } + } string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile); string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos"; @@ -176,7 +270,7 @@ int ScreenSeqsCommand::execute(){ int start = time(NULL); #ifdef USE_MPI - int pid, end, numSeqsPerProcessor; + int pid, numSeqsPerProcessor; int tag = 2001; vector MPIPos; @@ -220,10 +314,10 @@ int ScreenSeqsCommand::execute(){ numSeqsPerProcessor = numFastaSeqs / processors; int startIndex = pid * numSeqsPerProcessor; if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; } - + // cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl; //align your part driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames); - + //cout << pid << " done" << endl; if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; } for (int i = 1; i < processors; i++) { @@ -253,10 +347,10 @@ int ScreenSeqsCommand::execute(){ numSeqsPerProcessor = numFastaSeqs / processors; int startIndex = pid * numSeqsPerProcessor; if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; } - + //cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl; //align your part driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames); - +//cout << pid << " done" << endl; if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; } //send bad list @@ -283,11 +377,6 @@ int ScreenSeqsCommand::execute(){ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case #else - vector positions = m->divideFile(fastafile, processors); - - for (int i = 0; i < (positions.size()-1); i++) { - lines.push_back(new linePair(positions[i], positions[(i+1)])); - } #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) if(processors == 1){ @@ -390,11 +479,28 @@ int ScreenSeqsCommand::execute(){ m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); - m->mothurOut(goodSeqFile); m->mothurOutEndLine(); - m->mothurOut(badAccnosFile); m->mothurOutEndLine(); + m->mothurOut(goodSeqFile); m->mothurOutEndLine(); outputTypes["fasta"].push_back(goodSeqFile); + m->mothurOut(badAccnosFile); m->mothurOutEndLine(); outputTypes["accnos"].push_back(badAccnosFile); for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } m->mothurOutEndLine(); m->mothurOutEndLine(); + + //set fasta file as new current fastafile + string current = ""; + itTypes = outputTypes.find("fasta"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); } + } + + itTypes = outputTypes.find("name"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); } + } + + itTypes = outputTypes.find("group"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); } + } m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); @@ -418,7 +524,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ set::iterator it; string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile); - outputNames.push_back(goodNameFile); + outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile); ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut); @@ -427,7 +533,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ inputNames >> seqName >> seqList; it = badSeqNames.find(seqName); - + if(it != badSeqNames.end()){ badSeqNames.erase(it); @@ -449,7 +555,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ } inputNames.close(); goodNameOut.close(); - + //we were unable to remove some of the bad sequences if (badSeqNames.size() != 0) { for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) { @@ -464,7 +570,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ m->openInputFile(groupfile, inputGroups); string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); - outputNames.push_back(goodGroupFile); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); @@ -472,7 +578,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str()); remove(goodGroupFile.c_str()); return 0; } inputGroups >> seqName >> group; - + it = badSeqGroups.find(seqName); if(it != badSeqGroups.end()){ @@ -494,7 +600,8 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ } } } - + + return 0; } @@ -503,6 +610,195 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ exit(1); } } +//*************************************************************************************************************** +int ScreenSeqsCommand::getSummary(vector& positions){ + try { + + vector startPosition; + vector endPosition; + vector seqLength; + vector ambigBases; + vector longHomoPolymer; + + vector positions = m->divideFile(fastafile, processors); + + for (int i = 0; i < (positions.size()-1); i++) { + lines.push_back(new linePair(positions[i], positions[(i+1)])); + } + + int numSeqs = 0; + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + if(processors == 1){ + numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); + }else{ + numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile); + } + + if (m->control_pressed) { return 0; } + #else + numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); + if (m->control_pressed) { return 0; } + #endif + + sort(startPosition.begin(), startPosition.end()); + sort(endPosition.begin(), endPosition.end()); + sort(seqLength.begin(), seqLength.end()); + sort(ambigBases.begin(), ambigBases.end()); + sort(longHomoPolymer.begin(), longHomoPolymer.end()); + + //numSeqs is the number of unique seqs, startPosition.size() is the total number of seqs, we want to optimize using all seqs + int criteriaPercentile = int(startPosition.size() * (criteria / (float) 100)); + + for (int i = 0; i < optimize.size(); i++) { + if (optimize[i] == "start") { startPos = startPosition[criteriaPercentile]; m->mothurOut("Optimizing start to " + toString(startPos) + "."); m->mothurOutEndLine(); } + else if (optimize[i] == "end") { int endcriteriaPercentile = int(endPosition.size() * ((100 - criteria) / (float) 100)); endPos = endPosition[endcriteriaPercentile]; m->mothurOut("Optimizing end to " + toString(endPos) + "."); m->mothurOutEndLine();} + else if (optimize[i] == "maxambig") { maxAmbig = ambigBases[criteriaPercentile]; m->mothurOut("Optimizing maxambig to " + toString(maxAmbig) + "."); m->mothurOutEndLine(); } + else if (optimize[i] == "maxhomop") { maxHomoP = longHomoPolymer[criteriaPercentile]; m->mothurOut("Optimizing maxhomop to " + toString(maxHomoP) + "."); m->mothurOutEndLine(); } + else if (optimize[i] == "minlength") { int mincriteriaPercentile = int(seqLength.size() * ((100 - criteria) / (float) 100)); minLength = seqLength[mincriteriaPercentile]; m->mothurOut("Optimizing minlength to " + toString(minLength) + "."); m->mothurOutEndLine(); } + else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); } + } + + return 0; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getSummary"); + exit(1); + } +} +/**************************************************************************************/ +int ScreenSeqsCommand::driverCreateSummary(vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename, linePair* filePos) { + try { + + ifstream in; + m->openInputFile(filename, in); + + in.seekg(filePos->start); + + bool done = false; + int count = 0; + + while (!done) { + + if (m->control_pressed) { in.close(); return 1; } + + Sequence current(in); m->gobble(in); + + if (current.getName() != "") { + int num = 1; + if (namefile != "") { + //make sure this sequence is in the namefile, else error + map::iterator it = nameMap.find(current.getName()); + + if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; } + else { num = it->second; } + } + + //for each sequence this sequence represents + for (int i = 0; i < num; i++) { + startPosition.push_back(current.getStartPos()); + endPosition.push_back(current.getEndPos()); + seqLength.push_back(current.getNumBases()); + ambigBases.push_back(current.getAmbigBases()); + longHomoPolymer.push_back(current.getLongHomoPolymer()); + } + + count++; + } + + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + unsigned long int pos = in.tellg(); + if ((pos == -1) || (pos >= filePos->end)) { break; } + #else + if (in.eof()) { break; } + #endif + + } + + in.close(); + + return count; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "driverCreateSummary"); + exit(1); + } +} +/**************************************************************************************************/ +int ScreenSeqsCommand::createProcessesCreateSummary(vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename) { + try { +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + int process = 1; + int num = 0; + processIDS.clear(); + + //loop through and create all the processes you want + while (process != processors) { + int pid = fork(); + + if (pid > 0) { + processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later + process++; + }else if (pid == 0){ + num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[process]); + + //pass numSeqs to parent + ofstream out; + string tempFile = fastafile + toString(getpid()) + ".num.temp"; + m->openOutputFile(tempFile, out); + + out << num << endl; + out << startPosition.size() << endl; + for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl; + for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl; + for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl; + for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl; + for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl; + + out.close(); + + exit(0); + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } + } + + num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); + + //force parent to wait until all the processes are done + for (int i=0;iopenInputFile(tempFilename, in); + + int temp, tempNum; + in >> tempNum; m->gobble(in); num += tempNum; + in >> tempNum; m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in); + for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in); + + in.close(); + remove(tempFilename.c_str()); + } + + return num; +#endif + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary"); + exit(1); + } +} //*************************************************************************************************************** @@ -514,7 +810,7 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ set::iterator it; string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); - outputNames.push_back(goodGroupFile); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); while(!inputGroups.eof()){ @@ -566,7 +862,7 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ set::iterator it; string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport); - outputNames.push_back(goodAlignReportFile); + outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile); ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut); while (!inputAlignReport.eof()) { // need to copy header @@ -662,8 +958,12 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc count++; } - unsigned long int pos = inFASTA.tellg(); - if ((pos == -1) || (pos >= filePos->end)) { break; } + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + unsigned long int pos = inFASTA.tellg(); + if ((pos == -1) || (pos >= filePos->end)) { break; } + #else + if (inFASTA.eof()) { break; } + #endif //report progress if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); } @@ -748,6 +1048,9 @@ int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& delete buf3; } } + + //report progress + if((i) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i)); m->mothurOutEndLine(); } } return 1; @@ -784,7 +1087,11 @@ int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, st out.close(); exit(0); - }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); } + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } } //force parent to wait until all the processes are done