X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=screenseqscommand.cpp;h=5a9c0c8320b7305834cdf4bb544effd515728417;hb=1a5c2356c1b955c6ec024b2baf9f46377ee7c72e;hp=98b6681d5091ffbb30b80172c0a36d12a531d130;hpb=cbb336d5650cfe1f83a398a837794dcfbbbd3756;p=mothur.git diff --git a/screenseqscommand.cpp b/screenseqscommand.cpp index 98b6681..5a9c0c8 100644 --- a/screenseqscommand.cpp +++ b/screenseqscommand.cpp @@ -8,58 +8,108 @@ */ #include "screenseqscommand.h" -#include "sequence.hpp" +#include "counttable.h" //********************************************************************************************************************** -vector ScreenSeqsCommand::getValidParameters(){ +vector ScreenSeqsCommand::setParameters(){ 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))); + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta",false,true,true); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "NameCount", "none", "none","name",false,false,true); parameters.push_back(pname); + CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "none", "none","count",false,false,true); parameters.push_back(pcount); + CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "none", "none","group",false,false,true); parameters.push_back(pgroup); + CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none","qfile",false,false); parameters.push_back(pqfile); + CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "none", "none","alignreport",false,false); parameters.push_back(palignreport); + CommandParameter ptax("taxonomy", "InputTypes", "", "", "none", "none", "none","taxonomy",false,false); parameters.push_back(ptax); + CommandParameter pstart("start", "Number", "", "-1", "", "", "","",false,false,true); parameters.push_back(pstart); + CommandParameter pend("end", "Number", "", "-1", "", "", "","",false,false,true); 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,true); 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", "getValidParameters"); + m->errorOut(e, "ScreenSeqsCommand", "setParameters"); exit(1); } } //********************************************************************************************************************** -ScreenSeqsCommand::ScreenSeqsCommand(){ +string ScreenSeqsCommand::getHelpString(){ try { - abort = true; - //initialize outputTypes - vector tempOutNames; - outputTypes["fasta"] = tempOutNames; - outputTypes["name"] = tempOutNames; - outputTypes["group"] = tempOutNames; - outputTypes["alignreport"] = tempOutNames; - outputTypes["accnos"] = tempOutNames; + string helpString = ""; + helpString += "The screen.seqs command reads a fastafile and screens sequences.\n"; + helpString += "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, count, qfile, alignreport, taxonomy, optimize, criteria and processors.\n"; + helpString += "The fasta parameter is required.\n"; + helpString += "The alignreport and taxonomy parameters allow you to remove bad seqs from taxonomy and alignreport files.\n"; + helpString += "The start parameter is used to set a position the \"good\" sequences must start by. The default is -1.\n"; + helpString += "The end parameter is used to set a position the \"good\" sequences must end after. 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", "ScreenSeqsCommand"); + m->errorOut(e, "ScreenSeqsCommand", "getHelpString"); 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); - } +string ScreenSeqsCommand::getOutputPattern(string type) { + try { + string pattern = ""; + + if (type == "fasta") { pattern = "[filename],good,[extension]"; } + else if (type == "taxonomy") { pattern = "[filename],good,[extension]"; } + else if (type == "name") { pattern = "[filename],good,[extension]"; } + else if (type == "group") { pattern = "[filename],good,[extension]"; } + else if (type == "count") { pattern = "[filename],good,[extension]"; } + else if (type == "accnos") { pattern = "[filename],bad.accnos"; } + else if (type == "qfile") { pattern = "[filename],good,[extension]"; } + else if (type == "alignreport") { pattern = "[filename],good.align.report"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; } + + return pattern; + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "getOutputPattern"); + exit(1); + } } //********************************************************************************************************************** -vector ScreenSeqsCommand::getRequiredFiles(){ +ScreenSeqsCommand::ScreenSeqsCommand(){ try { - vector myArray; - return myArray; + 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; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["count"] = tempOutNames; } catch(exception& e) { - m->errorOut(e, "ScreenSeqsCommand", "getRequiredFiles"); + m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand"); exit(1); } } @@ -67,16 +117,14 @@ vector ScreenSeqsCommand::getRequiredFiles(){ 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","optimize","criteria", "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(); @@ -96,6 +144,9 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { outputTypes["group"] = tempOutNames; outputTypes["alignreport"] = tempOutNames; outputTypes["accnos"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + outputTypes["taxonomy"] = tempOutNames; + outputTypes["count"] = 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); @@ -133,24 +184,77 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { //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; } + } + + it = parameters.find("taxonomy"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["taxonomy"] = inputDir + it->second; } + } + + it = parameters.find("count"); + //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["count"] = 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; } - else if (fastafile == "not open") { 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; } + else { m->setFastaFile(fastafile); } groupfile = validParameter.validFile(parameters, "group", true); if (groupfile == "not open") { abort = true; } else if (groupfile == "not found") { groupfile = ""; } + else { m->setGroupFile(groupfile); } + + qualfile = validParameter.validFile(parameters, "qfile", true); + if (qualfile == "not open") { abort = true; } + else if (qualfile == "not found") { qualfile = ""; } + else { m->setQualFile(qualfile); } namefile = validParameter.validFile(parameters, "name", true); - if (namefile == "not open") { abort = true; } + if (namefile == "not open") { namefile = ""; abort = true; } else if (namefile == "not found") { namefile = ""; } - + else { m->setNameFile(namefile); } + + countfile = validParameter.validFile(parameters, "count", true); + if (countfile == "not open") { countfile = ""; abort = true; } + else if (countfile == "not found") { countfile = ""; } + else { m->setCountTableFile(countfile); } + + if ((namefile != "") && (countfile != "")) { + m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true; + } + + if ((groupfile != "") && (countfile != "")) { + m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true; + } + alignreport = validParameter.validFile(parameters, "alignreport", true); if (alignreport == "not open") { abort = true; } - else if (alignreport == "not found") { alignreport = ""; } + else if (alignreport == "not found") { alignreport = ""; } + + taxonomy = validParameter.validFile(parameters, "taxonomy", true); + if (taxonomy == "not open") { abort = true; } + else if (taxonomy == "not found") { taxonomy = ""; } //if the user changes the output directory command factory will send this info to us in the output parameter outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ @@ -162,34 +266,34 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { // ...at some point should added some additional type checking... string temp; temp = validParameter.validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; } - convert(temp, startPos); + m->mothurConvert(temp, startPos); temp = validParameter.validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; } - convert(temp, endPos); + m->mothurConvert(temp, endPos); temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; } - convert(temp, maxAmbig); + m->mothurConvert(temp, maxAmbig); temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; } - convert(temp, maxHomoP); + m->mothurConvert(temp, maxHomoP); temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; } - convert(temp, minLength); + m->mothurConvert(temp, minLength); temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; } - convert(temp, maxLength); + m->mothurConvert(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); + m->mothurConvert(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); } + if (temp == "not found"){ temp = "none"; } + 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"); - + 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(); @@ -198,8 +302,17 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option) { } } + 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); + m->mothurConvert(temp, criteria); + + if (countfile == "") { + if (namefile == "") { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } + } } } @@ -208,73 +321,67 @@ 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, 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"); - 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 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"); - 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 no the 90% mark - vector positions; - if (optimize.size() != 0) { getSummary(positions); } //get summary is paralellized so we need to divideFile, no need to do this step twice so I moved it here + //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); } + else if (countfile != "") { + CountTable ct; + ct.readTable(countfile); + nameMap = ct.getNameMap(); + } + 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)])); - } + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + positions = m->divideFile(fastafile, processors); + for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); } + #else + if(processors == 1){ lines.push_back(linePair(0, 1000)); } + else { + int numFastaSeqs = 0; + positions = m->setFilePosFasta(fastafile, numFastaSeqs); + if (positions.size() < processors) { processors = positions.size(); } + + //figure out how many sequences you have to process + int numSeqsPerProcessor = numFastaSeqs / processors; + for (int i = 0; i < processors; i++) { + int startIndex = i * numSeqsPerProcessor; + if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; } + lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor)); + } + } + #endif } - - string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile); - string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos"; + + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile)); + string badAccnosFile = getOutputFileName("accnos",variables); + variables["[extension]"] = m->getExtension(fastafile); + string goodSeqFile = getOutputFileName("fasta", variables); + 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 MPI_Comm_size(MPI_COMM_WORLD, &processors); - + MPI_File inMPI; MPI_File outMPIGood; MPI_File outMPIBadAccnos; @@ -311,29 +418,16 @@ 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++) { - //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); @@ -344,27 +438,15 @@ 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 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); - }*/ } //close files @@ -374,53 +456,10 @@ int ScreenSeqsCommand::execute(){ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case #else - - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - if(processors == 1){ - numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames); - - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } - - }else{ - processIDS.resize(0); - - numFastaSeqs = createProcesses(goodSeqFile, badAccnosFile, fastafile, badSeqNames); - - rename((goodSeqFile + toString(processIDS[0]) + ".temp").c_str(), goodSeqFile.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()); - - m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile); - remove((badAccnosFile + toString(processIDS[i]) + ".temp").c_str()); - } - - 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 = m->openInputFile(badAccnosFile, inBad, "no error"); - - if (ableToOpen == 0) { - badSeqNames.clear(); - string tempName; - while (!inBad.eof()) { - inBad >> tempName; m->gobble(inBad); - badSeqNames.insert(tempName); - } - inBad.close(); - } - } - #else - numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames); - - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } - - #endif - + if(processors == 1){ numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames); } + else{ numFastaSeqs = createProcesses(goodSeqFile, badAccnosFile, fastafile, badSeqNames); } + + if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; } #endif #ifdef USE_MPI @@ -458,17 +497,21 @@ int ScreenSeqsCommand::execute(){ if(namefile != "" && groupfile != "") { screenNameGroupFile(badSeqNames); - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } + if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; } }else if(namefile != "") { screenNameGroupFile(badSeqNames); - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } + if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; } }else if(groupfile != "") { screenGroupFile(badSeqNames); } // this screens just the group - - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } + else if (countfile != "") { screenCountFile(badSeqNames); } + + + if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; } if(alignreport != "") { screenAlignReport(badSeqNames); } + if(qualfile != "") { screenQual(badSeqNames); } + if(taxonomy != "") { screenTaxonomy(badSeqNames); } - if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; } + if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; } #ifdef USE_MPI } @@ -481,6 +524,38 @@ int ScreenSeqsCommand::execute(){ 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); } + } + + itTypes = outputTypes.find("taxonomy"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); } + } + + itTypes = outputTypes.find("count"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); } + } m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences."); m->mothurOutEndLine(); @@ -502,18 +577,20 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ set badSeqGroups; string seqName, seqList, group; set::iterator it; - - string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile); + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(namefile)); + variables["[extension]"] = m->getExtension(namefile); + string goodNameFile = getOutputFileName("name", variables); outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile); ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut); - + while(!inputNames.eof()){ - if (m->control_pressed) { goodNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); return 0; } + if (m->control_pressed) { goodNameOut.close(); inputNames.close(); m->mothurRemove(goodNameFile); return 0; } inputNames >> seqName >> seqList; it = badSeqNames.find(seqName); - + if(it != badSeqNames.end()){ badSeqNames.erase(it); @@ -548,17 +625,19 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ ifstream inputGroups; m->openInputFile(groupfile, inputGroups); - - string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(groupfile)); + variables["[extension]"] = m->getExtension(groupfile); + string goodGroupFile = getOutputFileName("group", variables); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); while(!inputGroups.eof()){ - if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str()); remove(goodGroupFile.c_str()); return 0; } + if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodNameFile); m->mothurRemove(goodGroupFile); return 0; } inputGroups >> seqName >> group; - + it = badSeqGroups.find(seqName); if(it != badSeqGroups.end()){ @@ -580,7 +659,8 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ } } } - + + return 0; } @@ -590,7 +670,7 @@ int ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ } } //*************************************************************************************************************** -int ScreenSeqsCommand::getSummary(vector& positions){ +int ScreenSeqsCommand::getSummary(vector& positions){ try { vector startPosition; @@ -599,14 +679,36 @@ int ScreenSeqsCommand::getSummary(vector& positions){ 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)])); - } + vector positions; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + positions = m->divideFile(fastafile, processors); + for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); } +#else + if(processors == 1){ lines.push_back(linePair(0, 1000)); } + else { + int numFastaSeqs = 0; + positions = m->setFilePosFasta(fastafile, numFastaSeqs); + if (positions.size() < processors) { processors = positions.size(); } + + //figure out how many sequences you have to process + int numSeqsPerProcessor = numFastaSeqs / processors; + for (int i = 0; i < processors; i++) { + int startIndex = i * numSeqsPerProcessor; + if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; } + lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor)); + } + } +#endif + +#ifdef USE_MPI + int pid; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); + if (pid == 0) { + driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); +#else int numSeqs = 0; - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + //#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) if(processors == 1){ numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); }else{ @@ -614,28 +716,56 @@ int ScreenSeqsCommand::getSummary(vector& positions){ } if (m->control_pressed) { return 0; } - #else - numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); - if (m->control_pressed) { return 0; } - #endif - + //#else + // numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]); + // if (m->control_pressed) { return 0; } + //#endif +#endif sort(startPosition.begin(), startPosition.end()); sort(endPosition.begin(), endPosition.end()); sort(seqLength.begin(), seqLength.end()); sort(ambigBases.begin(), ambigBases.end()); sort(longHomoPolymer.begin(), longHomoPolymer.end()); - int criteriaPercentile = int(numSeqs * (criteria / (float) 100)); + //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(numSeqs * ((100 - criteria) / (float) 100)); endPos = endPosition[endcriteriaPercentile]; m->mothurOut("Optimizing end to " + toString(endPos) + "."); 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(numSeqs * ((100 - criteria) / (float) 100)); minLength = seqLength[mincriteriaPercentile]; m->mothurOut("Optimizing minlength to " + toString(minLength) + "."); 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(); } } +#ifdef USE_MPI + } + + MPI_Status status; + MPI_Comm_rank(MPI_COMM_WORLD, &pid); + MPI_Comm_size(MPI_COMM_WORLD, &processors); + + if (pid == 0) { + //send file positions to all processes + for(int i = 1; i < processors; i++) { + MPI_Send(&startPos, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + MPI_Send(&endPos, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + MPI_Send(&maxAmbig, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + MPI_Send(&maxHomoP, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + MPI_Send(&minLength, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + MPI_Send(&maxLength, 1, MPI_INT, i, 2001, MPI_COMM_WORLD); + } + }else { + MPI_Recv(&startPos, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + MPI_Recv(&endPos, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + MPI_Recv(&maxAmbig, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + MPI_Recv(&maxHomoP, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + MPI_Recv(&minLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + MPI_Recv(&maxLength, 1, MPI_INT, 0, 2001, MPI_COMM_WORLD, &status); + } + MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case +#endif return 0; } catch(exception& e) { @@ -644,13 +774,13 @@ int ScreenSeqsCommand::getSummary(vector& positions){ } } /**************************************************************************************/ -int ScreenSeqsCommand::driverCreateSummary(vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename, linePair* filePos) { +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); + in.seekg(filePos.start); bool done = false; int count = 0; @@ -662,17 +792,30 @@ int ScreenSeqsCommand::driverCreateSummary(vector& startPosition, vectorgobble(in); if (current.getName() != "") { - 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()); + 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; } + //if((count) % 100 == 0){ m->mothurOut("Optimizing sequence: " + toString(count)); m->mothurOutEndLine(); } + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + unsigned long long pos = in.tellg(); + if ((pos == -1) || (pos >= filePos.end)) { break; } #else if (in.eof()) { break; } #endif @@ -691,11 +834,13 @@ int ScreenSeqsCommand::driverCreateSummary(vector& startPosition, vector& startPosition, vector& endPosition, vector& seqLength, vector& ambigBases, vector& longHomoPolymer, string filename) { try { -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - int process = 1; + + int process = 1; int num = 0; - processIDS.clear(); - + vector processIDS; + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + //loop through and create all the processes you want while (process != processors) { int pid = fork(); @@ -712,6 +857,7 @@ int ScreenSeqsCommand::createProcessesCreateSummary(vector& startPosition, 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; @@ -744,6 +890,7 @@ int ScreenSeqsCommand::createProcessesCreateSummary(vector& startPosition, 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); @@ -751,11 +898,53 @@ int ScreenSeqsCommand::createProcessesCreateSummary(vector& startPosition, for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in); in.close(); - remove(tempFilename.c_str()); + m->mothurRemove(tempFilename); } - return num; + +#else + ////////////////////////////////////////////////////////////////////////////////////////////////////// + //Windows version shared memory, so be careful when passing variables through the seqSumData struct. + //Above fork() will clone, so memory is separate, but that's not the case with windows, + //Taking advantage of shared memory to allow both threads to add info to vectors. + ////////////////////////////////////////////////////////////////////////////////////////////////////// + + vector pDataArray; + DWORD dwThreadIdArray[processors-1]; + HANDLE hThreadArray[processors-1]; + + //Create processor worker threads. + for( int i=0; icount; + for (int k = 0; k < pDataArray[i]->startPosition.size(); k++) { startPosition.push_back(pDataArray[i]->startPosition[k]); } + for (int k = 0; k < pDataArray[i]->endPosition.size(); k++) { endPosition.push_back(pDataArray[i]->endPosition[k]); } + for (int k = 0; k < pDataArray[i]->seqLength.size(); k++) { seqLength.push_back(pDataArray[i]->seqLength[k]); } + for (int k = 0; k < pDataArray[i]->ambigBases.size(); k++) { ambigBases.push_back(pDataArray[i]->ambigBases[k]); } + for (int k = 0; k < pDataArray[i]->longHomoPolymer.size(); k++) { longHomoPolymer.push_back(pDataArray[i]->longHomoPolymer[k]); } + CloseHandle(hThreadArray[i]); + delete pDataArray[i]; + } + #endif + return num; } catch(exception& e) { m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary"); @@ -771,13 +960,15 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ m->openInputFile(groupfile, inputGroups); string seqName, group; set::iterator it; - - string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile); - outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(groupfile)); + variables["[extension]"] = m->getExtension(groupfile); + string goodGroupFile = getOutputFileName("group", variables); + outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile); ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut); while(!inputGroups.eof()){ - if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; } + if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodGroupFile); return 0; } inputGroups >> seqName >> group; it = badSeqNames.find(seqName); @@ -791,7 +982,7 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ m->gobble(inputGroups); } - if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; } + if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodGroupFile); return 0; } //we were unable to remove some of the bad sequences if (badSeqNames.size() != 0) { @@ -804,7 +995,7 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ inputGroups.close(); goodGroupOut.close(); - if (m->control_pressed) { remove(goodGroupFile.c_str()); } + if (m->control_pressed) { m->mothurRemove(goodGroupFile); } return 0; @@ -814,7 +1005,72 @@ int ScreenSeqsCommand::screenGroupFile(set badSeqNames){ exit(1); } } +//*************************************************************************************************************** +int ScreenSeqsCommand::screenCountFile(set badSeqNames){ + try { + ifstream in; + m->openInputFile(countfile, in); + set::iterator it; + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(countfile)); + variables["[extension]"] = m->getExtension(countfile); + string goodCountFile = getOutputFileName("count", variables); + + outputNames.push_back(goodCountFile); outputTypes["count"].push_back(goodCountFile); + ofstream goodCountOut; m->openOutputFile(goodCountFile, goodCountOut); + + string headers = m->getline(in); m->gobble(in); + goodCountOut << headers << endl; + + string name, rest; int thisTotal; + while (!in.eof()) { + if (m->control_pressed) { goodCountOut.close(); in.close(); m->mothurRemove(goodCountFile); return 0; } + + in >> name; m->gobble(in); + in >> thisTotal; m->gobble(in); + rest = m->getline(in); m->gobble(in); + + it = badSeqNames.find(name); + + if(it != badSeqNames.end()){ + badSeqNames.erase(it); + } + else{ + goodCountOut << name << '\t' << thisTotal << '\t' << rest << endl; + } + } + + if (m->control_pressed) { goodCountOut.close(); in.close(); m->mothurRemove(goodCountFile); 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 count file does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } + + in.close(); + goodCountOut.close(); + + //check for groups that have been eliminated + CountTable ct; + if (ct.testGroups(goodCountFile)) { + ct.readTable(goodCountFile); + ct.printTable(goodCountFile); + } + + if (m->control_pressed) { m->mothurRemove(goodCountFile); } + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenCountFile"); + exit(1); + } +} //*************************************************************************************************************** int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ @@ -824,7 +1080,10 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ string seqName, group; set::iterator it; - string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport); + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(alignreport)); + string goodAlignReportFile = getOutputFileName("alignreport", variables); + outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile); ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut); @@ -835,7 +1094,7 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ } while(!inputAlignReport.eof()){ - if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; } + if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); m->mothurRemove(goodAlignReportFile); return 0; } inputAlignReport >> seqName; it = badSeqNames.find(seqName); @@ -855,7 +1114,7 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ m->gobble(inputAlignReport); } - if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; } + if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); m->mothurRemove(goodAlignReportFile); return 0; } //we were unable to remove some of the bad sequences if (badSeqNames.size() != 0) { @@ -868,7 +1127,7 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ inputAlignReport.close(); goodAlignReportOut.close(); - if (m->control_pressed) { remove(goodAlignReportFile.c_str()); return 0; } + if (m->control_pressed) { m->mothurRemove(goodAlignReportFile); return 0; } return 0; @@ -878,10 +1137,139 @@ int ScreenSeqsCommand::screenAlignReport(set badSeqNames){ exit(1); } +} +//*************************************************************************************************************** + +int ScreenSeqsCommand::screenTaxonomy(set badSeqNames){ + try { + ifstream input; + m->openInputFile(taxonomy, input); + string seqName, tax; + set::iterator it; + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(taxonomy)); + variables["[extension]"] = m->getExtension(taxonomy); + string goodTaxFile = getOutputFileName("taxonomy", variables); + + outputNames.push_back(goodTaxFile); outputTypes["taxonomy"].push_back(goodTaxFile); + ofstream goodTaxOut; m->openOutputFile(goodTaxFile, goodTaxOut); + + while(!input.eof()){ + if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; } + + input >> seqName >> tax; + it = badSeqNames.find(seqName); + + if(it != badSeqNames.end()){ badSeqNames.erase(it); } + else{ + goodTaxOut << seqName << '\t' << tax << endl; + } + m->gobble(input); + } + + if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; } + + //we were unable to remove some of the bad sequences + if (badSeqNames.size() != 0) { + for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) { + m->mothurOut("Your taxonomy file does not include the sequence " + *it + " please correct."); + m->mothurOutEndLine(); + } + } + + input.close(); + goodTaxOut.close(); + + if (m->control_pressed) { m->mothurRemove(goodTaxFile); return 0; } + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenTaxonomy"); + exit(1); + } + +} +//*************************************************************************************************************** + +int ScreenSeqsCommand::screenQual(set badSeqNames){ + try { + ifstream in; + m->openInputFile(qualfile, in); + set::iterator it; + map variables; + variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(qualfile)); + variables["[extension]"] = m->getExtension(qualfile); + string goodQualFile = getOutputFileName("qfile", variables); + + 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(); m->mothurRemove(goodQualFile); 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) { m->mothurRemove(goodQualFile); return 0; } + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "ScreenSeqsCommand", "screenQual"); + exit(1); + } + } //********************************************************************************************************************** -int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set& badSeqNames){ +int ScreenSeqsCommand::driver(linePair filePos, string goodFName, string badAccnosFName, string filename, set& badSeqNames){ try { ofstream goodFile; m->openOutputFile(goodFName, goodFile); @@ -892,7 +1280,7 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc ifstream inFASTA; m->openInputFile(filename, inFASTA); - inFASTA.seekg(filePos->start); + inFASTA.seekg(filePos.start); bool done = false; int count = 0; @@ -921,9 +1309,9 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc count++; } - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - unsigned long int pos = inFASTA.tellg(); - if ((pos == -1) || (pos >= filePos->end)) { break; } + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + unsigned long long pos = inFASTA.tellg(); + if ((pos == -1) || (pos >= filePos.end)) { break; } #else if (inFASTA.eof()) { break; } #endif @@ -948,7 +1336,7 @@ int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAcc } //********************************************************************************************************************** #ifdef USE_MPI -int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector& 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; @@ -965,7 +1353,6 @@ int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& 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); @@ -1028,10 +1415,13 @@ int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set& badSeqNames) { try { -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - int process = 0; + + vector processIDS; + int process = 1; int num = 0; - + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + //loop through and create all the processes you want while (process != processors) { int pid = fork(); @@ -1057,8 +1447,10 @@ int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, st } } + num = driver(lines[0], goodFileName, badAccnos, filename, badSeqNames); + //force parent to wait until all the processes are done - for (int i=0;iopenInputFile(tempFile, in); if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; } - in.close(); remove(tempFile.c_str()); + in.close(); m->mothurRemove(tempFile); + + m->appendFiles((goodFileName + toString(processIDS[i]) + ".temp"), goodFileName); + m->mothurRemove((goodFileName + toString(processIDS[i]) + ".temp")); + + m->appendFiles((badAccnos + toString(processIDS[i]) + ".temp"), badAccnos); + m->mothurRemove((badAccnos + toString(processIDS[i]) + ".temp")); } - return num; -#endif + //read badSeqs in because root process doesnt know what other "bad" seqs the children found + ifstream inBad; + int ableToOpen = m->openInputFile(badAccnos, inBad, "no error"); + + if (ableToOpen == 0) { + badSeqNames.clear(); + string tempName; + while (!inBad.eof()) { + inBad >> tempName; m->gobble(inBad); + badSeqNames.insert(tempName); + } + inBad.close(); + } +#else + + ////////////////////////////////////////////////////////////////////////////////////////////////////// + //Windows version shared memory, so be careful when passing variables through the sumScreenData struct. + //Above fork() will clone, so memory is separate, but that's not the case with windows, + //Taking advantage of shared memory to allow both threads to add info to badSeqNames. + ////////////////////////////////////////////////////////////////////////////////////////////////////// + + vector pDataArray; + DWORD dwThreadIdArray[processors-1]; + HANDLE hThreadArray[processors-1]; + + //Create processor worker threads. + for( int i=0; icount; + for (set::iterator it = pDataArray[i]->badSeqNames.begin(); it != pDataArray[i]->badSeqNames.end(); it++) { badSeqNames.insert(*it); } + CloseHandle(hThreadArray[i]); + delete pDataArray[i]; + } + + for (int i = 0; i < processIDS.size(); i++) { + m->appendFiles((goodFileName + toString(processIDS[i]) + ".temp"), goodFileName); + m->mothurRemove((goodFileName + toString(processIDS[i]) + ".temp")); + + m->appendFiles((badAccnos + toString(processIDS[i]) + ".temp"), badAccnos); + m->mothurRemove((badAccnos + toString(processIDS[i]) + ".temp")); + } + +#endif + + return num; + } catch(exception& e) { m->errorOut(e, "ScreenSeqsCommand", "createProcesses");