X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=screenseqscommand.cpp;h=878a06bfb3dd91262eccce5197c54e5324e46452;hb=725a3d4ff2442c79bfde0a75ed3e0904edcf03b7;hp=31b6bb7137579796c3a6652b283858bff435731a;hpb=0470f6d037aacb3563c3f7010708120a4a67d4e6;p=mothur.git diff --git a/screenseqscommand.cpp b/screenseqscommand.cpp index 31b6bb7..878a06b 100644 --- a/screenseqscommand.cpp +++ b/screenseqscommand.cpp @@ -12,9 +12,8 @@ //*************************************************************************************************************** -ScreenSeqsCommand::ScreenSeqsCommand(string option){ +ScreenSeqsCommand::ScreenSeqsCommand(string option) { try { - globaldata = GlobalData::getInstance(); abort = false; //allow user to run help @@ -22,103 +21,140 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength", "name", "group"}; + string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength", + "name", "group", "alignreport","processors","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); - parser = new OptionParser(); - parser->parse(option, parameters); delete parser; + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; - ValidParameters* validParameter = new ValidParameters(); - //check to make sure all parameters are valid for command for (it = parameters.begin(); it != parameters.end(); it++) { - if (validParameter->isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + if (validParameter.isValidParameter(it->first, myArray, it->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("group"); + //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["group"] = 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; } + } + + it = parameters.find("alignreport"); + //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["alignreport"] = inputDir + it->second; } + } + } + //check for required parameters - fastafile = validParameter->validFile(parameters, "fasta", true); - if (fastafile == "not found") { cout << "fasta is a required parameter for the screen.seqs command." << endl; abort = true; } + fastafile = validParameter.validFile(parameters, "fasta", true); + if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; } else if (fastafile == "not open") { abort = true; } - else { globaldata->setFastaFile(fastafile); } - - groupfile = validParameter->validFile(parameters, "group", true); + + groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { abort = true; } else if (groupfile == "not found") { groupfile = ""; } - else { - globaldata->setGroupFile(groupfile); - } - namefile = validParameter->validFile(parameters, "name", true); + namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not open") { abort = true; } else if (namefile == "not found") { namefile = ""; } - else { - globaldata->setNameFile(namefile); + + alignreport = validParameter.validFile(parameters, "alignreport", true); + if (alignreport == "not open") { abort = true; } + else if (alignreport == "not found") { alignreport = ""; } + + //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... string temp; - temp = validParameter->validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; } convert(temp, startPos); - temp = validParameter->validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; } convert(temp, endPos); - temp = validParameter->validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; } convert(temp, maxAmbig); - temp = validParameter->validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; } convert(temp, maxHomoP); - temp = validParameter->validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; } convert(temp, minLength); - temp = validParameter->validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; } + temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; } convert(temp, maxLength); - - delete validParameter; + + temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; } + convert(temp, processors); + } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function ScreenSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the ScreenSeqsCommand class function ScreenSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } } //********************************************************************************************************************** void ScreenSeqsCommand::help(){ try { - cout << "The screen.seqs command reads a fastafile and creates ....." << "\n"; - cout << "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, and group." << "\n"; - cout << "The fasta parameter is required." << "\n"; - cout << "The start parameter .... The default is -1." << "\n"; - cout << "The end parameter .... The default is -1." << "\n"; - cout << "The maxambig parameter .... The default is -1." << "\n"; - cout << "The maxhomop parameter .... The default is -1." << "\n"; - cout << "The minlength parameter .... The default is -1." << "\n"; - cout << "The maxlength parameter .... The default is -1." << "\n"; - cout << "The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile." << "\n"; - cout << "The screen.seqs command should be in the following format: " << "\n"; - cout << "screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, " << "\n"; - cout << "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) " << "\n"; - cout << "Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...)." << "\n"; - cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta)." << "\n" << "\n"; + 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 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"); + m->mothurOut("The maxambig parameter .... The default is -1.\n"); + m->mothurOut("The maxhomop parameter .... The default is -1.\n"); + 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 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"); + m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n"); + m->mothurOut("Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ScreenSeqsCommand", "help"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the ScreenSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } } //*************************************************************************************************************** @@ -132,117 +168,422 @@ int ScreenSeqsCommand::execute(){ if (abort == true) { return 0; } - ifstream inFASTA; - openInputFile(fastafile, inFASTA); + string goodSeqFile = outputDir + getRootName(getSimpleName(fastafile)) + "good" + getExtension(fastafile); + string badSeqFile = outputDir + getRootName(getSimpleName(fastafile)) + "bad" + getExtension(fastafile); + string badAccnosFile = outputDir + getRootName(getSimpleName(fastafile)) + "bad.accnos"; + int numFastaSeqs = 0; set badSeqNames; + int start = time(NULL); - string goodSeqFile = getRootName(fastafile) + "good" + getExtension(fastafile); - string badSeqFile = getRootName(fastafile) + "bad" + getExtension(fastafile); - - ofstream goodSeqOut; openOutputFile(goodSeqFile, goodSeqOut); - ofstream badSeqOut; openOutputFile(badSeqFile, badSeqOut); - - while(!inFASTA.eof()){ - Sequence currSeq(inFASTA); - bool goodSeq = 1; // innocent until proven guilty - if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; } - if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; } - if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; } - if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; } - if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; } - if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; } - - if(goodSeq == 1){ - currSeq.printSequence(goodSeqOut); +#ifdef USE_MPI + int pid, end, numSeqsPerProcessor; + int tag = 2001; + vector 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 outMPIBad; + MPI_File outMPIBadAccnos; + + int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; + int inMode=MPI_MODE_RDONLY; + + char outGoodFilename[1024]; + strcpy(outGoodFilename, goodSeqFile.c_str()); + + char outBadFilename[1024]; + strcpy(outBadFilename, badSeqFile.c_str()); + + char outBadAccnosFilename[1024]; + strcpy(outBadAccnosFilename, badAccnosFile.c_str()); + + char inFileName[1024]; + strcpy(inFileName, fastafile.c_str()); + + MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer + MPI_File_open(MPI_COMM_WORLD, outGoodFilename, outMode, MPI_INFO_NULL, &outMPIGood); + MPI_File_open(MPI_COMM_WORLD, outBadFilename, outMode, MPI_INFO_NULL, &outMPIBad); + MPI_File_open(MPI_COMM_WORLD, outBadAccnosFilename, outMode, MPI_INFO_NULL, &outMPIBadAccnos); + + if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBad); MPI_File_close(&outMPIBadAccnos); return 0; } + + if (pid == 0) { //you are the root process + + MPIPos = setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs + + //send file positions to all processes + for(int i = 1; i < processors; i++) { + MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD); + MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD); + } + + //figure out how many sequences you have to align + numSeqsPerProcessor = numFastaSeqs / processors; + int startIndex = pid * numSeqsPerProcessor; + if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; } + + //align your part + driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBad, outMPIBadAccnos, MPIPos, badSeqNames); + + if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); MPI_File_close(&outMPIBad); return 0; } + + for (int i = 1; i < processors; i++) { + + //get bad lists + int badSize; + MPI_Recv(&badSize, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status); + /*for (int j = 0; j < badSize; j++) { + int length; + MPI_Recv(&length, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status); //recv the length of the name + char* buf2 = new char[length]; //make space to recieve it + MPI_Recv(buf2, length, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); //get name + + string tempBuf = buf2; + if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); } + delete buf2; + + badSeqNames.insert(tempBuf); + }*/ + } + }else{ //you are a child process + MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); + MPIPos.resize(numFastaSeqs+1); + MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status); + + //figure out how many sequences you have to align + numSeqsPerProcessor = numFastaSeqs / processors; + int startIndex = pid * numSeqsPerProcessor; + if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; } + + //align your part + driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBad, outMPIBadAccnos, MPIPos, badSeqNames); + + if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBad); MPI_File_close(&outMPIBadAccnos); return 0; } + + //send bad list + int badSize = badSeqNames.size(); + MPI_Send(&badSize, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); + + /* + set::iterator it; + for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) { + string name = *it; + int length = name.length(); + char* buf2 = new char[length]; + memcpy(buf2, name.c_str(), length); + + MPI_Send(&length, 1, MPI_INT, 0, tag, MPI_COMM_WORLD); + MPI_Send(buf2, length, MPI_CHAR, 0, tag, MPI_COMM_WORLD); + }*/ } - else{ - currSeq.printSequence(badSeqOut); - badSeqNames.insert(currSeq.getName()); + + //close files + MPI_File_close(&inMPI); + MPI_File_close(&outMPIGood); + MPI_File_close(&outMPIBad); + MPI_File_close(&outMPIBadAccnos); + MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case + +#else + + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + if(processors == 1){ + ifstream inFASTA; + openInputFile(fastafile, inFASTA); + numFastaSeqs=count(istreambuf_iterator(inFASTA),istreambuf_iterator(), '>'); + inFASTA.close(); + + lines.push_back(new linePair(0, numFastaSeqs)); + + driver(lines[0], goodSeqFile, badSeqFile, badAccnosFile, fastafile, badSeqNames); + + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + + }else{ + vector positions; + processIDS.resize(0); + + ifstream inFASTA; + openInputFile(fastafile, inFASTA); + + string input; + while(!inFASTA.eof()){ + input = getline(inFASTA); + if (input.length() != 0) { + if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); } + } + } + inFASTA.close(); + + numFastaSeqs = positions.size(); + + int numSeqsPerProcessor = numFastaSeqs / processors; + + for (int i = 0; i < processors; i++) { + long int startPos = positions[ i * numSeqsPerProcessor ]; + if(i == processors - 1){ + numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; + } + lines.push_back(new linePair(startPos, numSeqsPerProcessor)); + } + + createProcesses(goodSeqFile, badSeqFile, badAccnosFile, fastafile, badSeqNames); + + rename((goodSeqFile + toString(processIDS[0]) + ".temp").c_str(), goodSeqFile.c_str()); + rename((badSeqFile + toString(processIDS[0]) + ".temp").c_str(), badSeqFile.c_str()); + rename((badAccnosFile + toString(processIDS[0]) + ".temp").c_str(), badAccnosFile.c_str()); + + //append alignment and report files + for(int i=1;icontrol_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + + //read badSeqs in because root process doesnt know what other "bad" seqs the children found + ifstream inBad; + int ableToOpen = openInputFile(badAccnosFile, inBad, "no error"); + + if (ableToOpen == 0) { + badSeqNames.clear(); + string tempName; + while (!inBad.eof()) { + inBad >> tempName; gobble(inBad); + badSeqNames.insert(tempName); + } + inBad.close(); + } } - gobble(inFASTA); - } - if(namefile != ""){ + #else + ifstream inFASTA; + openInputFile(fastafile, inFASTA); + numFastaSeqs=count(istreambuf_iterator(inFASTA),istreambuf_iterator(), '>'); + inFASTA.close(); + + lines.push_back(new linePair(0, numFastaSeqs)); + + driver(lines[0], goodSeqFile, badSeqFile, badAccnosFile, fastafile, badSeqNames); + + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + + #endif + +#endif + + #ifdef USE_MPI + MPI_Comm_rank(MPI_COMM_WORLD, &pid); + + if (pid == 0) { //only one process should fix files + + //read accnos file with all names in it, process 0 just has its names + MPI_File inMPIAccnos; + MPI_Offset size; + + char inFileName[1024]; + strcpy(inFileName, badAccnosFile.c_str()); + + MPI_File_open(MPI_COMM_SELF, inFileName, inMode, MPI_INFO_NULL, &inMPIAccnos); //comm, filename, mode, info, filepointer + MPI_File_get_size(inMPIAccnos, &size); + + char* buffer = new char[size]; + MPI_File_read(inMPIAccnos, buffer, size, MPI_CHAR, &status); + + string tempBuf = buffer; + if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); } + istringstream iss (tempBuf,istringstream::in); + + delete buffer; + MPI_File_close(&inMPIAccnos); + + badSeqNames.clear(); + string tempName; + while (!iss.eof()) { + iss >> tempName; gobble(iss); + badSeqNames.insert(tempName); + } + #endif + + if(namefile != "" && groupfile != "") { + screenNameGroupFile(badSeqNames); + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + }else if(namefile != "") { screenNameGroupFile(badSeqNames); - } - else if(groupfile != ""){ - screenGroupFile(badSeqNames); - } + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + }else if(groupfile != "") { screenGroupFile(badSeqNames); } // this screens just the group + + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + + if(alignreport != "") { screenAlignReport(badSeqNames); } + + if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + #ifdef USE_MPI + } + #endif + + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + m->mothurOut(goodSeqFile); m->mothurOutEndLine(); + m->mothurOut(badSeqFile); m->mothurOutEndLine(); + m->mothurOut(badAccnosFile); m->mothurOutEndLine(); + for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + m->mothurOutEndLine(); + + m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences."); + m->mothurOutEndLine(); + return 0; } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the ScreenSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "ScreenSeqsCommand", "execute"); exit(1); } - catch(...) { - cout << "An unknown error has occurred in the ScreenSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - } //*************************************************************************************************************** -void ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ +int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ + try { + ifstream inputNames; + openInputFile(namefile, inputNames); + set badSeqGroups; + string seqName, seqList, group; + set::iterator it; - ifstream inputNames; - openInputFile(globaldata->getNameFile(), inputNames); - set badSeqGroups; - string seqName, seqList, group; - set::iterator it; + string goodNameFile = outputDir + getRootName(getSimpleName(namefile)) + "good" + getExtension(namefile); + string badNameFile = outputDir + getRootName(getSimpleName(namefile)) + "bad" + getExtension(namefile); + + outputNames.push_back(goodNameFile); outputNames.push_back(badNameFile); + + ofstream goodNameOut; openOutputFile(goodNameFile, goodNameOut); + ofstream badNameOut; openOutputFile(badNameFile, badNameOut); + + while(!inputNames.eof()){ + if (m->control_pressed) { goodNameOut.close(); badNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); remove(badNameFile.c_str()); return 0; } - string goodNameFile = getRootName(globaldata->getNameFile()) + "good" + getExtension(globaldata->getNameFile()); - string badNameFile = getRootName(globaldata->getNameFile()) + "bad" + getExtension(globaldata->getNameFile()); - - ofstream goodNameOut; openOutputFile(goodNameFile, goodNameOut); - ofstream badNameOut; openOutputFile(badNameFile, badNameOut); - - while(!inputNames.eof()){ - inputNames >> seqName >> seqList; - it = badSeqNames.find(seqName); - - if(it != badSeqNames.end()){ - badSeqNames.erase(it); - badNameOut << seqName << '\t' << seqList << endl; - if(globaldata->getNameFile() != ""){ - int start = 0; - for(int i=0;i> seqName >> seqList; + it = badSeqNames.find(seqName); + + if(it != badSeqNames.end()){ + badSeqNames.erase(it); + badNameOut << seqName << '\t' << seqList << endl; + if(namefile != ""){ + int start = 0; + for(int i=0;imothurOut("Your namefile does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } } - gobble(inputNames); - } - inputNames.close(); - goodNameOut.close(); - badNameOut.close(); + + if(groupfile != ""){ + + ifstream inputGroups; + openInputFile(groupfile, inputGroups); + + string goodGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "good" + getExtension(groupfile); + string badGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "bad" + getExtension(groupfile); + + outputNames.push_back(goodGroupFile); outputNames.push_back(badGroupFile); + + ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); + ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); + + while(!inputGroups.eof()){ + if (m->control_pressed) { goodGroupOut.close(); badGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str()); remove(badNameFile.c_str()); remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); return 0; } + + inputGroups >> seqName >> group; + + it = badSeqGroups.find(seqName); + + if(it != badSeqGroups.end()){ + badSeqGroups.erase(it); + badGroupOut << seqName << '\t' << group << endl; + } + else{ + goodGroupOut << seqName << '\t' << group << endl; + } + gobble(inputGroups); + } + inputGroups.close(); + goodGroupOut.close(); + badGroupOut.close(); + + //we were unable to remove some of the bad sequences + if (badSeqGroups.size() != 0) { + for (it = badSeqGroups.begin(); it != badSeqGroups.end(); it++) { + m->mothurOut("Your namefile does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } + } + + return 0; - if(globaldata->getGroupFile() != ""){ - - ifstream inputGroups; - openInputFile(globaldata->getGroupFile(), inputGroups); + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenNameGroupFile"); + exit(1); + } +} + +//*************************************************************************************************************** - string goodGroupFile = getRootName(globaldata->getGroupFile()) + "good" + getExtension(globaldata->getGroupFile()); - string badGroupFile = getRootName(globaldata->getGroupFile()) + "bad" + getExtension(globaldata->getGroupFile()); +int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ + try { + ifstream inputGroups; + openInputFile(groupfile, inputGroups); + string seqName, group; + set::iterator it; + + string goodGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "good" + getExtension(groupfile); + string badGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "bad" + getExtension(groupfile); + + outputNames.push_back(goodGroupFile); outputNames.push_back(badGroupFile); ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); while(!inputGroups.eof()){ - inputGroups >> seqName >> group; + if (m->control_pressed) { goodGroupOut.close(); badGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); return 0; } - it = badSeqGroups.find(seqName); + inputGroups >> seqName >> group; + it = badSeqNames.find(seqName); - if(it != badSeqGroups.end()){ - badSeqGroups.erase(it); + if(it != badSeqNames.end()){ + badSeqNames.erase(it); badGroupOut << seqName << '\t' << group << endl; } else{ @@ -250,44 +591,280 @@ void ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ } gobble(inputGroups); } + + if (m->control_pressed) { goodGroupOut.close(); badGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); 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 groupfile does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } + inputGroups.close(); goodGroupOut.close(); badGroupOut.close(); + + if (m->control_pressed) { remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); } + + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenGroupFile"); + exit(1); } } //*************************************************************************************************************** -void ScreenSeqsCommand::screenGroupFile(set badSeqNames){ +int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ + try { + ifstream inputAlignReport; + openInputFile(alignreport, inputAlignReport); + string seqName, group; + set::iterator it; + + string goodAlignReportFile = outputDir + getRootName(getSimpleName(alignreport)) + "good" + getExtension(alignreport); + string badAlignReportFile = outputDir + getRootName(getSimpleName(alignreport)) + "bad" + getExtension(alignreport); + + outputNames.push_back(goodAlignReportFile); outputNames.push_back(badAlignReportFile); + + ofstream goodAlignReportOut; openOutputFile(goodAlignReportFile, goodAlignReportOut); + ofstream badAlignReportOut; openOutputFile(badAlignReportFile, badAlignReportOut); + + while (!inputAlignReport.eof()) { // need to copy header + char c = inputAlignReport.get(); + goodAlignReportOut << c; + badAlignReportOut << c; + if (c == 10 || c == 13){ break; } + } + + while(!inputAlignReport.eof()){ + if (m->control_pressed) { goodAlignReportOut.close(); badAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); remove(badAlignReportFile.c_str()); return 0; } + + inputAlignReport >> seqName; + it = badSeqNames.find(seqName); + string line; + while (!inputAlignReport.eof()) { // need to copy header + char c = inputAlignReport.get(); + line += c; + if (c == 10 || c == 13){ break; } + } + + if(it != badSeqNames.end()){ + badSeqNames.erase(it); + badAlignReportOut << seqName << '\t' << line; + } + else{ + goodAlignReportOut << seqName << '\t' << line; + } + gobble(inputAlignReport); + } + + if (m->control_pressed) { goodAlignReportOut.close(); badAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); remove(badAlignReportFile.c_str()); 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 file does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } - ifstream inputGroups; - openInputFile(globaldata->getGroupFile(), inputGroups); - string seqName, group; - set::iterator it; + inputAlignReport.close(); + goodAlignReportOut.close(); + badAlignReportOut.close(); + + if (m->control_pressed) { remove(goodAlignReportFile.c_str()); remove(badAlignReportFile.c_str()); return 0; } + + return 0; - string goodGroupFile = getRootName(globaldata->getGroupFile()) + "good" + getExtension(globaldata->getGroupFile()); - string badGroupFile = getRootName(globaldata->getGroupFile()) + "bad" + getExtension(globaldata->getGroupFile()); + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenAlignReport"); + exit(1); + } - ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); - ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); +} +//********************************************************************************************************************** + +int ScreenSeqsCommand::driver(linePair* line, string goodFName, string badFName, string badAccnosFName, string filename, set& badSeqNames){ + try { + ofstream goodFile; + openOutputFile(goodFName, goodFile); + + ofstream badFile; + openOutputFile(badFName, badFile); + + ofstream badAccnosFile; + openOutputFile(badAccnosFName, badAccnosFile); + + ifstream inFASTA; + openInputFile(filename, inFASTA); + + inFASTA.seekg(line->start); - while(!inputGroups.eof()){ - inputGroups >> seqName >> group; - it = badSeqNames.find(seqName); + for(int i=0;inumSeqs;i++){ + + if (m->control_pressed) { return 0; } + + Sequence currSeq(inFASTA); + if (currSeq.getName() != "") { + bool goodSeq = 1; // innocent until proven guilty + if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; } + if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; } + if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; } + if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; } + if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; } + if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; } + + if(goodSeq == 1){ + currSeq.printSequence(goodFile); + } + else{ + currSeq.printSequence(badFile); + badAccnosFile << currSeq.getName() << endl; + badSeqNames.insert(currSeq.getName()); + } + } + gobble(inFASTA); + } + + + goodFile.close(); + inFASTA.close(); + badFile.close(); + badAccnosFile.close(); + + return 1; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "driver"); + exit(1); + } +} +//********************************************************************************************************************** +#ifdef USE_MPI +int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badFile, MPI_File& badAccnosFile, vector& MPIPos, set& badSeqNames){ + try { + string outputString = ""; + MPI_Status statusGood; + MPI_Status statusBad; + MPI_Status statusBadAccnos; + MPI_Status status; + int pid; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are + + for(int i=0;icontrol_pressed) { return 0; } + + //read next sequence + int length = MPIPos[start+i+1] - MPIPos[start+i]; + + char* buf4 = new char[length]; + memcpy(buf4, outputString.c_str(), length); + + MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status); + + string tempBuf = buf4; delete buf4; + if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); } + istringstream iss (tempBuf,istringstream::in); + + Sequence currSeq(iss); + + //process seq + if (currSeq.getName() != "") { + bool goodSeq = 1; // innocent until proven guilty + if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; } + if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; } + if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; } + if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; } + if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; } + if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; } + + if(goodSeq == 1){ + outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n"; + + //print good seq + length = outputString.length(); + char* buf2 = new char[length]; + memcpy(buf2, outputString.c_str(), length); + + MPI_File_write_shared(goodFile, buf2, length, MPI_CHAR, &statusGood); + delete buf2; + } + else{ + outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n"; + + //print bad seq to fasta + length = outputString.length(); + char* buf2 = new char[length]; + memcpy(buf2, outputString.c_str(), length); + + MPI_File_write_shared(badFile, buf2, length, MPI_CHAR, &statusBad); + delete buf2; + + badSeqNames.insert(currSeq.getName()); + + //write to bad accnos file + outputString = currSeq.getName() + "\n"; + + length = outputString.length(); + char* buf3 = new char[length]; + memcpy(buf3, outputString.c_str(), length); + + MPI_File_write_shared(badAccnosFile, buf3, length, MPI_CHAR, &statusBadAccnos); + delete buf3; + } + } + } + + return 1; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "driverMPI"); + exit(1); + } +} +#endif +/**************************************************************************************************/ + +int ScreenSeqsCommand::createProcesses(string goodFileName, string badFileName, string badAccnos, string filename, set& badSeqNames) { + try { +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + int process = 0; + int exitCommand = 1; - if(it != badSeqNames.end()){ - badSeqNames.erase(it); - badGroupOut << seqName << '\t' << group << endl; + //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){ + exitCommand = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames); + exit(0); + }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); } } - else{ - goodGroupOut << seqName << '\t' << group << endl; + + //force parent to wait until all the processes are done + for (int i=0;ierrorOut(e, "ScreenSeqsCommand", "createProcesses"); + exit(1); } - inputGroups.close(); - goodGroupOut.close(); - badGroupOut.close(); - } //***************************************************************************************************************