X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=screenseqscommand.cpp;h=b68a8cc8ea0042d53222da781770627c58ff3966;hb=e150b0b0664caec517485ee6d69dcdade6dcae77;hp=f67dba7223eded3e55cc104841657ee229bc0db8;hpb=8699fd2c88f10abda5fe32c89be061a89d673fd6;p=mothur.git diff --git a/screenseqscommand.cpp b/screenseqscommand.cpp index f67dba7..b68a8cc 100644 --- a/screenseqscommand.cpp +++ b/screenseqscommand.cpp @@ -10,25 +10,99 @@ #include "screenseqscommand.h" #include "sequence.hpp" +//********************************************************************************************************************** +vector ScreenSeqsCommand::setParameters(){ + try { + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname); + 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 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); + CommandParameter pmaxhomop("maxhomop", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxhomop); + CommandParameter pminlength("minlength", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pminlength); + CommandParameter pmaxlength("maxlength", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxlength); + CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors); + CommandParameter pcriteria("criteria", "Number", "", "90", "", "", "",false,false); parameters.push_back(pcriteria); + CommandParameter poptimize("optimize", "Multiple", "none-start-end-maxambig-maxhomop-minlength-maxlength", "none", "", "", "",true,false); parameters.push_back(poptimize); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +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 fasta parameter is required.\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"; + helpString += "The maxhomop parameter allows you to set a maximum homopolymer length. \n"; + helpString += "The minlength parameter allows you to set and minimum sequence length. \n"; + helpString += "The maxlength parameter allows you to set and maximum sequence length. \n"; + helpString += "The processors parameter allows you to specify the number of processors to use while running the command. The default is 1.\n"; + helpString += "The optimize and criteria parameters allow you set the start, end, maxabig, maxhomop, minlength and maxlength parameters relative to your set of sequences .\n"; + helpString += "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"; + helpString += "The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n"; + helpString += "The screen.seqs command should be in the following format: \n"; + helpString += "screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, \n"; + helpString += "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n"; + helpString += "Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n"; + helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +ScreenSeqsCommand::ScreenSeqsCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["group"] = tempOutNames; + outputTypes["alignreport"] = tempOutNames; + outputTypes["accnos"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand"); + 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 if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength", - "name", "group", "alignreport","processors","outputdir","inputdir"}; - vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); - ValidParameters validParameter; + ValidParameters validParameter("screen.seqs"); map::iterator it; //check to make sure all parameters are valid for command @@ -36,6 +110,15 @@ 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; + outputTypes["qfile"] = 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 = ""; } @@ -44,7 +127,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { it = parameters.find("fasta"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } @@ -52,7 +135,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { it = parameters.find("group"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } @@ -60,7 +143,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { it = parameters.find("name"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } @@ -68,23 +151,40 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { it = parameters.find("alignreport"); //user has given a template file if(it != parameters.end()){ - path = hasPath(it->second); + path = m->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; } } + + it = parameters.find("qfile"); + //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["qfile"] = inputDir + it->second; } + } + } //check for required parameters 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; } + if (fastafile == "not found") { + fastafile = m->getFastaFile(); + 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; } groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { abort = true; } else if (groupfile == "not found") { groupfile = ""; } + qualfile = validParameter.validFile(parameters, "qfile", true); + if (qualfile == "not open") { abort = true; } + else if (qualfile == "not found") { qualfile = ""; } + 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); @@ -94,7 +194,7 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { //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 + outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it } //check for optional parameter and set defaults @@ -118,9 +218,29 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; } convert(temp, maxLength); - temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; } - convert(temp, processors); - + temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); } + m->setProcessors(temp); + convert(temp, processors); + + temp = validParameter.validFile(parameters, "optimize", false); //optimizing trumps the optimized values original value + if (temp == "not found"){ temp = "none"; } + m->splitAtDash(temp, optimize); + + //check for invalid optimize options + set validOptimizers; + validOptimizers.insert("none"); 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--; + } + } + + if (optimize.size() == 1) { if (optimize[0] == "none") { optimize.clear(); } } + + temp = validParameter.validFile(parameters, "criteria", false); if (temp == "not found"){ temp = "90"; } + convert(temp, criteria); } } @@ -129,57 +249,38 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { exit(1); } } -//********************************************************************************************************************** - -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 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) { - m->errorOut(e, "ScreenSeqsCommand", "help"); - exit(1); - } -} - -//*************************************************************************************************************** - -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 + getRootName(getSimpleName(fastafile)) + "good" + getExtension(fastafile); - string badSeqFile = outputDir + getRootName(getSimpleName(fastafile)) + "bad" + getExtension(fastafile); - string badAccnosFile = outputDir + getRootName(getSimpleName(fastafile)) + "bad.accnos"; + string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile); + string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos"; int numFastaSeqs = 0; set badSeqNames; int start = time(NULL); #ifdef USE_MPI - int pid, end, numSeqsPerProcessor; + int pid, numSeqsPerProcessor; int tag = 2001; - vector MPIPos; + vector MPIPos; MPI_Status status; MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are @@ -187,7 +288,6 @@ int ScreenSeqsCommand::execute(){ MPI_File inMPI; MPI_File outMPIGood; - MPI_File outMPIBad; MPI_File outMPIBadAccnos; int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; @@ -196,9 +296,6 @@ int ScreenSeqsCommand::execute(){ char outGoodFilename[1024]; strcpy(outGoodFilename, goodSeqFile.c_str()); - char outBadFilename[1024]; - strcpy(outBadFilename, badSeqFile.c_str()); - char outBadAccnosFilename[1024]; strcpy(outBadAccnosFilename, badAccnosFile.c_str()); @@ -207,28 +304,29 @@ int ScreenSeqsCommand::execute(){ 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 (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; } if (pid == 0) { //you are the root process - MPIPos = setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs + MPIPos = m->setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs //send file positions to all processes - MPI_Bcast(&numFastaSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD); //send numSeqs - MPI_Bcast(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //send file pos + 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; } - + // cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl; //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; } + 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++) { @@ -249,19 +347,19 @@ int ScreenSeqsCommand::execute(){ }*/ } }else{ //you are a child process - MPI_Bcast(&numFastaSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD); //get numSeqs + MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status); MPIPos.resize(numFastaSeqs+1); - MPI_Bcast(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //get file positions - + 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; } - + //cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl; //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; } + 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 int badSize = badSeqNames.size(); @@ -283,97 +381,54 @@ int ScreenSeqsCommand::execute(){ //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)); + numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames); - driver(lines[0], goodSeqFile, badSeqFile, badAccnosFile, fastafile, badSeqNames); - - if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodSeqFile.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); + numFastaSeqs = createProcesses(goodSeqFile, 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;iappendFiles((goodSeqFile + toString(processIDS[i]) + ".temp"), goodSeqFile); remove((goodSeqFile + toString(processIDS[i]) + ".temp").c_str()); - - appendFiles((badSeqFile + toString(processIDS[i]) + ".temp"), badSeqFile); - remove((badSeqFile + toString(processIDS[i]) + ".temp").c_str()); - - appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile); + + m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile); remove((badAccnosFile + toString(processIDS[i]) + ".temp").c_str()); } - if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodSeqFile.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"); + int ableToOpen = m->openInputFile(badAccnosFile, inBad, "no error"); if (ableToOpen == 0) { badSeqNames.clear(); string tempName; while (!inBad.eof()) { - inBad >> tempName; gobble(inBad); + inBad >> tempName; m->gobble(inBad); badSeqNames.insert(tempName); } inBad.close(); } } #else - ifstream inFASTA; - openInputFile(fastafile, inFASTA); - numFastaSeqs=count(istreambuf_iterator(inFASTA),istreambuf_iterator(), '>'); - inFASTA.close(); - - lines.push_back(new linePair(0, numFastaSeqs)); + numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames); - driver(lines[0], goodSeqFile, badSeqFile, badAccnosFile, fastafile, badSeqNames); - - if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } #endif @@ -407,24 +462,25 @@ int ScreenSeqsCommand::execute(){ badSeqNames.clear(); string tempName; while (!iss.eof()) { - iss >> tempName; gobble(iss); + iss >> tempName; m->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; } + if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } }else if(namefile != "") { screenNameGroupFile(badSeqNames); - if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodSeqFile.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 (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } if(alignreport != "") { screenAlignReport(badSeqNames); } + if(qualfile != "") { screenQual(badSeqNames); } - if (m->control_pressed) { remove(goodSeqFile.c_str()); remove(badSeqFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } #ifdef USE_MPI } @@ -432,12 +488,33 @@ int ScreenSeqsCommand::execute(){ m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); - m->mothurOut(goodSeqFile); m->mothurOutEndLine(); - m->mothurOut(badSeqFile); 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); } + } + + itTypes = outputTypes.find("qfile"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); } + } m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); @@ -455,28 +532,25 @@ int ScreenSeqsCommand::execute(){ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ try { ifstream inputNames; - openInputFile(namefile, inputNames); + m->openInputFile(namefile, 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); + string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile); + outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile); - ofstream goodNameOut; openOutputFile(goodNameFile, goodNameOut); - ofstream badNameOut; openOutputFile(badNameFile, badNameOut); + ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut); while(!inputNames.eof()){ - if (m->control_pressed) { goodNameOut.close(); badNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); remove(badNameFile.c_str()); return 0; } + if (m->control_pressed) { goodNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); return 0; } inputNames >> 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;i badSeqNames){ else{ goodNameOut << seqName << '\t' << seqList << endl; } - gobble(inputNames); + m->gobble(inputNames); } inputNames.close(); goodNameOut.close(); - badNameOut.close(); - + //we were unable to remove some of the bad sequences if (badSeqNames.size() != 0) { for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) { @@ -508,45 +581,41 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ if(groupfile != ""){ ifstream inputGroups; - openInputFile(groupfile, inputGroups); + m->openInputFile(groupfile, inputGroups); - string goodGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "good" + getExtension(groupfile); - string badGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "bad" + getExtension(groupfile); + string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); - outputNames.push_back(goodGroupFile); outputNames.push_back(badGroupFile); - - ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); - ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); + ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); 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; } + 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()){ badSeqGroups.erase(it); - badGroupOut << seqName << '\t' << group << endl; } else{ goodGroupOut << seqName << '\t' << group << endl; } - gobble(inputGroups); + m->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->mothurOut("Your groupfile does not include the sequence " + *it + " please correct."); m->mothurOutEndLine(); } } } - + + return 0; } @@ -555,41 +624,225 @@ 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); + } +} //*************************************************************************************************************** int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ try { ifstream inputGroups; - openInputFile(groupfile, inputGroups); + m->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); + string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); + ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); while(!inputGroups.eof()){ - if (m->control_pressed) { goodGroupOut.close(); badGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); return 0; } + if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; } inputGroups >> seqName >> group; it = badSeqNames.find(seqName); if(it != badSeqNames.end()){ badSeqNames.erase(it); - badGroupOut << seqName << '\t' << group << endl; } else{ goodGroupOut << seqName << '\t' << group << endl; } - gobble(inputGroups); + m->gobble(inputGroups); } - if (m->control_pressed) { goodGroupOut.close(); badGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); return 0; } + if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; } //we were unable to remove some of the bad sequences if (badSeqNames.size() != 0) { @@ -601,10 +854,8 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ inputGroups.close(); goodGroupOut.close(); - badGroupOut.close(); - if (m->control_pressed) { remove(goodGroupFile.c_str()); remove(badGroupFile.c_str()); } - + if (m->control_pressed) { remove(goodGroupFile.c_str()); } return 0; @@ -620,27 +871,22 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ try { ifstream inputAlignReport; - openInputFile(alignreport, inputAlignReport); + m->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); + string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport); + outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile); + ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut); 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; } + if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; } inputAlignReport >> seqName; it = badSeqNames.find(seqName); @@ -653,29 +899,27 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ if(it != badSeqNames.end()){ badSeqNames.erase(it); - badAlignReportOut << seqName << '\t' << line; } else{ goodAlignReportOut << seqName << '\t' << line; } - gobble(inputAlignReport); + m->gobble(inputAlignReport); } - if (m->control_pressed) { goodAlignReportOut.close(); badAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); remove(badAlignReportFile.c_str()); return 0; } + if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.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->mothurOut("Your alignreport file does not include the sequence " + *it + " please correct."); m->mothurOutEndLine(); } } inputAlignReport.close(); goodAlignReportOut.close(); - badAlignReportOut.close(); - if (m->control_pressed) { remove(goodAlignReportFile.c_str()); remove(badAlignReportFile.c_str()); return 0; } + if (m->control_pressed) { remove(goodAlignReportFile.c_str()); return 0; } return 0; @@ -685,30 +929,103 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ exit(1); } +} +//*************************************************************************************************************** + +int ScreenSeqsCommand::screenQual(set badSeqNames){ + try { + ifstream in; + m->openInputFile(qualfile, in); + set::iterator it; + + string goodQualFile = outputDir + m->getRootName(m->getSimpleName(qualfile)) + "good" + m->getExtension(qualfile); + outputNames.push_back(goodQualFile); outputTypes["qfile"].push_back(goodQualFile); + ofstream goodQual; m->openOutputFile(goodQualFile, goodQual); + + while(!in.eof()){ + + if (m->control_pressed) { goodQual.close(); in.close(); remove(goodQualFile.c_str()); return 0; } + + string saveName = ""; + string name = ""; + string scores = ""; + + in >> name; + + if (name.length() != 0) { + saveName = name.substr(1); + while (!in.eof()) { + char c = in.get(); + if (c == 10 || c == 13){ break; } + else { name += c; } + } + m->gobble(in); + } + + while(in){ + char letter= in.get(); + if(letter == '>'){ in.putback(letter); break; } + else{ scores += letter; } + } + + m->gobble(in); + + it = badSeqNames.find(saveName); + + if(it != badSeqNames.end()){ + badSeqNames.erase(it); + }else{ + goodQual << name << endl << scores; + } + + m->gobble(in); + } + + in.close(); + goodQual.close(); + + //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 qual file does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } + + if (m->control_pressed) { remove(goodQualFile.c_str()); return 0; } + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenQual"); + exit(1); + } + } //********************************************************************************************************************** -int ScreenSeqsCommand::driver(linePair* line, string goodFName, string badFName, string badAccnosFName, string filename, set& badSeqNames){ +int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set& badSeqNames){ try { ofstream goodFile; - openOutputFile(goodFName, goodFile); - - ofstream badFile; - openOutputFile(badFName, badFile); + m->openOutputFile(goodFName, goodFile); ofstream badAccnosFile; - openOutputFile(badAccnosFName, badAccnosFile); + m->openOutputFile(badAccnosFName, badAccnosFile); ifstream inFASTA; - openInputFile(filename, inFASTA); + m->openInputFile(filename, inFASTA); + + inFASTA.seekg(filePos->start); - inFASTA.seekg(line->start); + bool done = false; + int count = 0; - for(int i=0;inumSeqs;i++){ - + while (!done) { + if (m->control_pressed) { return 0; } - Sequence currSeq(inFASTA); + Sequence currSeq(inFASTA); m->gobble(inFASTA); if (currSeq.getName() != "") { bool goodSeq = 1; // innocent until proven guilty if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; } @@ -722,21 +1039,31 @@ int ScreenSeqsCommand::driver(linePair* line, string goodFName, string badFName, currSeq.printSequence(goodFile); } else{ - currSeq.printSequence(badFile); badAccnosFile << currSeq.getName() << endl; badSeqNames.insert(currSeq.getName()); } + count++; } - gobble(inFASTA); + + #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(); } } + //report progress + if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); } goodFile.close(); inFASTA.close(); - badFile.close(); badAccnosFile.close(); - return 1; + return count; } catch(exception& e) { m->errorOut(e, "ScreenSeqsCommand", "driver"); @@ -745,11 +1072,10 @@ int ScreenSeqsCommand::driver(linePair* line, string goodFName, string badFName, } //********************************************************************************************************************** #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){ +int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector& MPIPos, set& badSeqNames){ try { string outputString = ""; MPI_Status statusGood; - MPI_Status statusBad; MPI_Status statusBadAccnos; MPI_Status status; int pid; @@ -795,15 +1121,6 @@ int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& 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()); @@ -818,6 +1135,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; @@ -830,11 +1150,11 @@ int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& #endif /**************************************************************************************************/ -int ScreenSeqsCommand::createProcesses(string goodFileName, string badFileName, string badAccnos, string filename, set& badSeqNames) { +int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set& badSeqNames) { try { #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) int process = 0; - int exitCommand = 1; + int num = 0; //loop through and create all the processes you want while (process != processors) { @@ -844,9 +1164,21 @@ int ScreenSeqsCommand::createProcesses(string goodFileName, string badFileName, 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); + num = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames); + + //pass numSeqs to parent + ofstream out; + string tempFile = filename + toString(getpid()) + ".num.temp"; + m->openOutputFile(tempFile, out); + out << num << 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); - }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); } + } } //force parent to wait until all the processes are done @@ -855,7 +1187,15 @@ int ScreenSeqsCommand::createProcesses(string goodFileName, string badFileName, wait(&temp); } - return exitCommand; + for (int i = 0; i < processIDS.size(); i++) { + ifstream in; + 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()); + } + + return num; #endif } catch(exception& e) {