2 * screenseqscommand.cpp
5 * Created by Pat Schloss on 6/3/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "screenseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> ScreenSeqsCommand::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pqfile);
20 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(palignreport);
21 CommandParameter ptax("taxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(ptax);
22 CommandParameter pstart("start", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pstart);
23 CommandParameter pend("end", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pend);
24 CommandParameter pmaxambig("maxambig", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxambig);
25 CommandParameter pmaxhomop("maxhomop", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxhomop);
26 CommandParameter pminlength("minlength", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pminlength);
27 CommandParameter pmaxlength("maxlength", "Number", "", "-1", "", "", "",false,false); parameters.push_back(pmaxlength);
28 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
29 CommandParameter pcriteria("criteria", "Number", "", "90", "", "", "",false,false); parameters.push_back(pcriteria);
30 CommandParameter poptimize("optimize", "Multiple", "none-start-end-maxambig-maxhomop-minlength-maxlength", "none", "", "", "",true,false); parameters.push_back(poptimize);
31 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
32 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34 vector<string> myArray;
35 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
39 m->errorOut(e, "ScreenSeqsCommand", "setParameters");
43 //**********************************************************************************************************************
44 string ScreenSeqsCommand::getHelpString(){
46 string helpString = "";
47 helpString += "The screen.seqs command reads a fastafile and creates .....\n";
48 helpString += "The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, qfile, alignreport, taxonomy, optimize, criteria and processors.\n";
49 helpString += "The fasta parameter is required.\n";
50 helpString += "The alignreport and taxonomy parameters allow you to remove bad seqs from taxonomy and alignreport files.\n";
51 helpString += "The start parameter .... The default is -1.\n";
52 helpString += "The end parameter .... The default is -1.\n";
53 helpString += "The maxambig parameter allows you to set the maximum number of ambigious bases allowed. The default is -1.\n";
54 helpString += "The maxhomop parameter allows you to set a maximum homopolymer length. \n";
55 helpString += "The minlength parameter allows you to set and minimum sequence length. \n";
56 helpString += "The maxlength parameter allows you to set and maximum sequence length. \n";
57 helpString += "The processors parameter allows you to specify the number of processors to use while running the command. The default is 1.\n";
58 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";
59 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";
60 helpString += "The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n";
61 helpString += "The screen.seqs command should be in the following format: \n";
62 helpString += "screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, \n";
63 helpString += "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n";
64 helpString += "Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n";
65 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
69 m->errorOut(e, "ScreenSeqsCommand", "getHelpString");
73 //**********************************************************************************************************************
74 ScreenSeqsCommand::ScreenSeqsCommand(){
76 abort = true; calledHelp = true;
78 vector<string> tempOutNames;
79 outputTypes["fasta"] = tempOutNames;
80 outputTypes["name"] = tempOutNames;
81 outputTypes["group"] = tempOutNames;
82 outputTypes["alignreport"] = tempOutNames;
83 outputTypes["accnos"] = tempOutNames;
84 outputTypes["qfile"] = tempOutNames;
85 outputTypes["taxonomy"] = tempOutNames;
88 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
92 //***************************************************************************************************************
94 ScreenSeqsCommand::ScreenSeqsCommand(string option) {
96 abort = false; calledHelp = false;
98 //allow user to run help
99 if(option == "help") { help(); abort = true; calledHelp = true; }
100 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
103 vector<string> myArray = setParameters();
105 OptionParser parser(option);
106 map<string,string> parameters = parser.getParameters();
108 ValidParameters validParameter("screen.seqs");
109 map<string,string>::iterator it;
111 //check to make sure all parameters are valid for command
112 for (it = parameters.begin(); it != parameters.end(); it++) {
113 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
116 //initialize outputTypes
117 vector<string> tempOutNames;
118 outputTypes["fasta"] = tempOutNames;
119 outputTypes["name"] = tempOutNames;
120 outputTypes["group"] = tempOutNames;
121 outputTypes["alignreport"] = tempOutNames;
122 outputTypes["accnos"] = tempOutNames;
123 outputTypes["qfile"] = tempOutNames;
124 outputTypes["taxonomy"] = tempOutNames;
126 //if the user changes the input directory command factory will send this info to us in the output parameter
127 string inputDir = validParameter.validFile(parameters, "inputdir", false);
128 if (inputDir == "not found"){ inputDir = ""; }
131 it = parameters.find("fasta");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["fasta"] = inputDir + it->second; }
139 it = parameters.find("group");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["group"] = inputDir + it->second; }
147 it = parameters.find("name");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["name"] = inputDir + it->second; }
155 it = parameters.find("alignreport");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
163 it = parameters.find("qfile");
164 //user has given a template file
165 if(it != parameters.end()){
166 path = m->hasPath(it->second);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { parameters["qfile"] = inputDir + it->second; }
171 it = parameters.find("taxonomy");
172 //user has given a template file
173 if(it != parameters.end()){
174 path = m->hasPath(it->second);
175 //if the user has not given a path then, add inputdir. else leave path alone.
176 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
180 //check for required parameters
181 fastafile = validParameter.validFile(parameters, "fasta", true);
182 if (fastafile == "not found") {
183 fastafile = m->getFastaFile();
184 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
185 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
187 else if (fastafile == "not open") { abort = true; }
188 else { m->setFastaFile(fastafile); }
190 groupfile = validParameter.validFile(parameters, "group", true);
191 if (groupfile == "not open") { abort = true; }
192 else if (groupfile == "not found") { groupfile = ""; }
193 else { m->setGroupFile(groupfile); }
195 qualfile = validParameter.validFile(parameters, "qfile", true);
196 if (qualfile == "not open") { abort = true; }
197 else if (qualfile == "not found") { qualfile = ""; }
198 else { m->setQualFile(qualfile); }
200 namefile = validParameter.validFile(parameters, "name", true);
201 if (namefile == "not open") { namefile = ""; abort = true; }
202 else if (namefile == "not found") { namefile = ""; }
203 else { m->setNameFile(namefile); }
205 alignreport = validParameter.validFile(parameters, "alignreport", true);
206 if (alignreport == "not open") { abort = true; }
207 else if (alignreport == "not found") { alignreport = ""; }
209 taxonomy = validParameter.validFile(parameters, "taxonomy", true);
210 if (taxonomy == "not open") { abort = true; }
211 else if (taxonomy == "not found") { taxonomy = ""; }
213 //if the user changes the output directory command factory will send this info to us in the output parameter
214 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
216 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
219 //check for optional parameter and set defaults
220 // ...at some point should added some additional type checking...
222 temp = validParameter.validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; }
223 convert(temp, startPos);
225 temp = validParameter.validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; }
226 convert(temp, endPos);
228 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
229 convert(temp, maxAmbig);
231 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; }
232 convert(temp, maxHomoP);
234 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; }
235 convert(temp, minLength);
237 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; }
238 convert(temp, maxLength);
240 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
241 m->setProcessors(temp);
242 convert(temp, processors);
244 temp = validParameter.validFile(parameters, "optimize", false); //optimizing trumps the optimized values original value
245 if (temp == "not found"){ temp = "none"; }
246 m->splitAtDash(temp, optimize);
248 //check for invalid optimize options
249 set<string> validOptimizers;
250 validOptimizers.insert("none"); validOptimizers.insert("start"); validOptimizers.insert("end"); validOptimizers.insert("maxambig"); validOptimizers.insert("maxhomop"); validOptimizers.insert("minlength"); validOptimizers.insert("maxlength");
251 for (int i = 0; i < optimize.size(); i++) {
252 if (validOptimizers.count(optimize[i]) == 0) {
253 m->mothurOut(optimize[i] + " is not a valid optimizer. Valid options are start, end, maxambig, maxhomop, minlength and maxlength."); m->mothurOutEndLine();
254 optimize.erase(optimize.begin()+i);
259 if (optimize.size() == 1) { if (optimize[0] == "none") { optimize.clear(); } }
261 temp = validParameter.validFile(parameters, "criteria", false); if (temp == "not found"){ temp = "90"; }
262 convert(temp, criteria);
266 catch(exception& e) {
267 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
271 //***************************************************************************************************************
273 int ScreenSeqsCommand::execute(){
276 if (abort == true) { if (calledHelp) { return 0; } return 2; }
278 //if the user want to optimize we need to know the 90% mark
279 vector<unsigned long long> positions;
280 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
281 //use the namefile to optimize correctly
282 if (namefile != "") { nameMap = m->readNames(namefile); }
283 getSummary(positions);
286 positions = m->divideFile(fastafile, processors);
287 for (int i = 0; i < (positions.size()-1); i++) {
288 lines.push_back(new linePair(positions[i], positions[(i+1)]));
292 string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile);
293 string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos";
295 int numFastaSeqs = 0;
296 set<string> badSeqNames;
297 int start = time(NULL);
300 int pid, numSeqsPerProcessor;
302 vector<unsigned long long> MPIPos;
305 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
306 MPI_Comm_size(MPI_COMM_WORLD, &processors);
310 MPI_File outMPIBadAccnos;
312 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
313 int inMode=MPI_MODE_RDONLY;
315 char outGoodFilename[1024];
316 strcpy(outGoodFilename, goodSeqFile.c_str());
318 char outBadAccnosFilename[1024];
319 strcpy(outBadAccnosFilename, badAccnosFile.c_str());
321 char inFileName[1024];
322 strcpy(inFileName, fastafile.c_str());
324 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
325 MPI_File_open(MPI_COMM_WORLD, outGoodFilename, outMode, MPI_INFO_NULL, &outMPIGood);
326 MPI_File_open(MPI_COMM_WORLD, outBadAccnosFilename, outMode, MPI_INFO_NULL, &outMPIBadAccnos);
328 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
330 if (pid == 0) { //you are the root process
332 MPIPos = m->setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs
334 //send file positions to all processes
335 for(int i = 1; i < processors; i++) {
336 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
337 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
340 //figure out how many sequences you have to align
341 numSeqsPerProcessor = numFastaSeqs / processors;
342 int startIndex = pid * numSeqsPerProcessor;
343 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
344 // cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
346 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
347 //cout << pid << " done" << endl;
348 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
350 for (int i = 1; i < processors; i++) {
354 MPI_Recv(&badSize, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
355 /*for (int j = 0; j < badSize; j++) {
357 MPI_Recv(&length, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status); //recv the length of the name
358 char* buf2 = new char[length]; //make space to recieve it
359 MPI_Recv(buf2, length, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); //get name
361 string tempBuf = buf2;
362 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
365 badSeqNames.insert(tempBuf);
368 }else{ //you are a child process
369 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
370 MPIPos.resize(numFastaSeqs+1);
371 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
373 //figure out how many sequences you have to align
374 numSeqsPerProcessor = numFastaSeqs / processors;
375 int startIndex = pid * numSeqsPerProcessor;
376 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
377 //cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
379 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
380 //cout << pid << " done" << endl;
381 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
384 int badSize = badSeqNames.size();
385 MPI_Send(&badSize, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
388 set<string>::iterator it;
389 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
391 int length = name.length();
392 char* buf2 = new char[length];
393 memcpy(buf2, name.c_str(), length);
395 MPI_Send(&length, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
396 MPI_Send(buf2, length, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
401 MPI_File_close(&inMPI);
402 MPI_File_close(&outMPIGood);
403 MPI_File_close(&outMPIBadAccnos);
404 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
408 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
410 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
412 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
415 processIDS.resize(0);
417 numFastaSeqs = createProcesses(goodSeqFile, badAccnosFile, fastafile, badSeqNames);
419 rename((goodSeqFile + toString(processIDS[0]) + ".temp").c_str(), goodSeqFile.c_str());
420 rename((badAccnosFile + toString(processIDS[0]) + ".temp").c_str(), badAccnosFile.c_str());
422 //append alignment and report files
423 for(int i=1;i<processors;i++){
424 m->appendFiles((goodSeqFile + toString(processIDS[i]) + ".temp"), goodSeqFile);
425 m->mothurRemove((goodSeqFile + toString(processIDS[i]) + ".temp"));
427 m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile);
428 m->mothurRemove((badAccnosFile + toString(processIDS[i]) + ".temp"));
431 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
433 //read badSeqs in because root process doesnt know what other "bad" seqs the children found
435 int ableToOpen = m->openInputFile(badAccnosFile, inBad, "no error");
437 if (ableToOpen == 0) {
440 while (!inBad.eof()) {
441 inBad >> tempName; m->gobble(inBad);
442 badSeqNames.insert(tempName);
448 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
450 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
457 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
459 if (pid == 0) { //only one process should fix files
461 //read accnos file with all names in it, process 0 just has its names
462 MPI_File inMPIAccnos;
465 char inFileName[1024];
466 strcpy(inFileName, badAccnosFile.c_str());
468 MPI_File_open(MPI_COMM_SELF, inFileName, inMode, MPI_INFO_NULL, &inMPIAccnos); //comm, filename, mode, info, filepointer
469 MPI_File_get_size(inMPIAccnos, &size);
471 char* buffer = new char[size];
472 MPI_File_read(inMPIAccnos, buffer, size, MPI_CHAR, &status);
474 string tempBuf = buffer;
475 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
476 istringstream iss (tempBuf,istringstream::in);
479 MPI_File_close(&inMPIAccnos);
484 iss >> tempName; m->gobble(iss);
485 badSeqNames.insert(tempName);
489 if(namefile != "" && groupfile != "") {
490 screenNameGroupFile(badSeqNames);
491 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
492 }else if(namefile != "") {
493 screenNameGroupFile(badSeqNames);
494 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
495 }else if(groupfile != "") { screenGroupFile(badSeqNames); } // this screens just the group
497 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
499 if(alignreport != "") { screenAlignReport(badSeqNames); }
500 if(qualfile != "") { screenQual(badSeqNames); }
501 if(taxonomy != "") { screenTaxonomy(badSeqNames); }
503 if (m->control_pressed) { m->mothurRemove(goodSeqFile); return 0; }
509 m->mothurOutEndLine();
510 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
511 m->mothurOut(goodSeqFile); m->mothurOutEndLine(); outputTypes["fasta"].push_back(goodSeqFile);
512 m->mothurOut(badAccnosFile); m->mothurOutEndLine(); outputTypes["accnos"].push_back(badAccnosFile);
513 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
514 m->mothurOutEndLine();
515 m->mothurOutEndLine();
517 //set fasta file as new current fastafile
519 itTypes = outputTypes.find("fasta");
520 if (itTypes != outputTypes.end()) {
521 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
524 itTypes = outputTypes.find("name");
525 if (itTypes != outputTypes.end()) {
526 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
529 itTypes = outputTypes.find("group");
530 if (itTypes != outputTypes.end()) {
531 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
534 itTypes = outputTypes.find("qfile");
535 if (itTypes != outputTypes.end()) {
536 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
539 itTypes = outputTypes.find("taxonomy");
540 if (itTypes != outputTypes.end()) {
541 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
544 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences.");
545 m->mothurOutEndLine();
549 catch(exception& e) {
550 m->errorOut(e, "ScreenSeqsCommand", "execute");
555 //***************************************************************************************************************
557 int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
560 m->openInputFile(namefile, inputNames);
561 set<string> badSeqGroups;
562 string seqName, seqList, group;
563 set<string>::iterator it;
565 string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile);
566 outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile);
568 ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut);
570 while(!inputNames.eof()){
571 if (m->control_pressed) { goodNameOut.close(); inputNames.close(); m->mothurRemove(goodNameFile); return 0; }
573 inputNames >> seqName >> seqList;
574 it = badSeqNames.find(seqName);
576 if(it != badSeqNames.end()){
577 badSeqNames.erase(it);
581 for(int i=0;i<seqList.length();i++){
582 if(seqList[i] == ','){
583 badSeqGroups.insert(seqList.substr(start,i-start));
587 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
591 goodNameOut << seqName << '\t' << seqList << endl;
593 m->gobble(inputNames);
598 //we were unable to remove some of the bad sequences
599 if (badSeqNames.size() != 0) {
600 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
601 m->mothurOut("Your namefile does not include the sequence " + *it + " please correct.");
602 m->mothurOutEndLine();
608 ifstream inputGroups;
609 m->openInputFile(groupfile, inputGroups);
611 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
612 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
614 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
616 while(!inputGroups.eof()){
617 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodNameFile); m->mothurRemove(goodGroupFile); return 0; }
619 inputGroups >> seqName >> group;
621 it = badSeqGroups.find(seqName);
623 if(it != badSeqGroups.end()){
624 badSeqGroups.erase(it);
627 goodGroupOut << seqName << '\t' << group << endl;
629 m->gobble(inputGroups);
632 goodGroupOut.close();
634 //we were unable to remove some of the bad sequences
635 if (badSeqGroups.size() != 0) {
636 for (it = badSeqGroups.begin(); it != badSeqGroups.end(); it++) {
637 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
638 m->mothurOutEndLine();
647 catch(exception& e) {
648 m->errorOut(e, "ScreenSeqsCommand", "screenNameGroupFile");
652 //***************************************************************************************************************
653 int ScreenSeqsCommand::getSummary(vector<unsigned long long>& positions){
656 vector<int> startPosition;
657 vector<int> endPosition;
658 vector<int> seqLength;
659 vector<int> ambigBases;
660 vector<int> longHomoPolymer;
662 vector<unsigned long long> positions = m->divideFile(fastafile, processors);
664 for (int i = 0; i < (positions.size()-1); i++) {
665 lines.push_back(new linePair(positions[i], positions[(i+1)]));
670 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
672 if (pid == 0) { //only one process should fix files
673 driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
676 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait
679 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
681 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
683 numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile);
686 if (m->control_pressed) { return 0; }
688 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
689 if (m->control_pressed) { return 0; }
692 sort(startPosition.begin(), startPosition.end());
693 sort(endPosition.begin(), endPosition.end());
694 sort(seqLength.begin(), seqLength.end());
695 sort(ambigBases.begin(), ambigBases.end());
696 sort(longHomoPolymer.begin(), longHomoPolymer.end());
698 //numSeqs is the number of unique seqs, startPosition.size() is the total number of seqs, we want to optimize using all seqs
699 int criteriaPercentile = int(startPosition.size() * (criteria / (float) 100));
701 for (int i = 0; i < optimize.size(); i++) {
702 if (optimize[i] == "start") { startPos = startPosition[criteriaPercentile]; m->mothurOut("Optimizing start to " + toString(startPos) + "."); m->mothurOutEndLine(); }
703 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();}
704 else if (optimize[i] == "maxambig") { maxAmbig = ambigBases[criteriaPercentile]; m->mothurOut("Optimizing maxambig to " + toString(maxAmbig) + "."); m->mothurOutEndLine(); }
705 else if (optimize[i] == "maxhomop") { maxHomoP = longHomoPolymer[criteriaPercentile]; m->mothurOut("Optimizing maxhomop to " + toString(maxHomoP) + "."); m->mothurOutEndLine(); }
706 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(); }
707 else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); }
712 catch(exception& e) {
713 m->errorOut(e, "ScreenSeqsCommand", "getSummary");
717 /**************************************************************************************/
718 int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, linePair* filePos) {
722 m->openInputFile(filename, in);
724 in.seekg(filePos->start);
731 if (m->control_pressed) { in.close(); return 1; }
733 Sequence current(in); m->gobble(in);
735 if (current.getName() != "") {
737 if (namefile != "") {
738 //make sure this sequence is in the namefile, else error
739 map<string, int>::iterator it = nameMap.find(current.getName());
741 if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
742 else { num = it->second; }
745 //for each sequence this sequence represents
746 for (int i = 0; i < num; i++) {
747 startPosition.push_back(current.getStartPos());
748 endPosition.push_back(current.getEndPos());
749 seqLength.push_back(current.getNumBases());
750 ambigBases.push_back(current.getAmbigBases());
751 longHomoPolymer.push_back(current.getLongHomoPolymer());
757 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
758 unsigned long long pos = in.tellg();
759 if ((pos == -1) || (pos >= filePos->end)) { break; }
761 if (in.eof()) { break; }
770 catch(exception& e) {
771 m->errorOut(e, "ScreenSeqsCommand", "driverCreateSummary");
775 /**************************************************************************************************/
776 int ScreenSeqsCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename) {
778 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
783 //loop through and create all the processes you want
784 while (process != processors) {
788 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
791 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[process]);
793 //pass numSeqs to parent
795 string tempFile = fastafile + toString(getpid()) + ".num.temp";
796 m->openOutputFile(tempFile, out);
799 out << startPosition.size() << endl;
800 for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
801 for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
802 for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
803 for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
804 for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
810 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
811 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
816 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
818 //force parent to wait until all the processes are done
819 for (int i=0;i<processIDS.size();i++) {
820 int temp = processIDS[i];
824 //parent reads in and combine Filter info
825 for (int i = 0; i < processIDS.size(); i++) {
826 string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
828 m->openInputFile(tempFilename, in);
831 in >> tempNum; m->gobble(in); num += tempNum;
832 in >> tempNum; m->gobble(in);
833 for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
834 for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
835 for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
836 for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
837 for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
840 m->mothurRemove(tempFilename);
846 catch(exception& e) {
847 m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary");
852 //***************************************************************************************************************
854 int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
856 ifstream inputGroups;
857 m->openInputFile(groupfile, inputGroups);
858 string seqName, group;
859 set<string>::iterator it;
861 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
862 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
863 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
865 while(!inputGroups.eof()){
866 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodGroupFile); return 0; }
868 inputGroups >> seqName >> group;
869 it = badSeqNames.find(seqName);
871 if(it != badSeqNames.end()){
872 badSeqNames.erase(it);
875 goodGroupOut << seqName << '\t' << group << endl;
877 m->gobble(inputGroups);
880 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); m->mothurRemove(goodGroupFile); return 0; }
882 //we were unable to remove some of the bad sequences
883 if (badSeqNames.size() != 0) {
884 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
885 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
886 m->mothurOutEndLine();
891 goodGroupOut.close();
893 if (m->control_pressed) { m->mothurRemove(goodGroupFile); }
898 catch(exception& e) {
899 m->errorOut(e, "ScreenSeqsCommand", "screenGroupFile");
904 //***************************************************************************************************************
906 int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
908 ifstream inputAlignReport;
909 m->openInputFile(alignreport, inputAlignReport);
910 string seqName, group;
911 set<string>::iterator it;
913 string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport);
914 outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile);
915 ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut);
917 while (!inputAlignReport.eof()) { // need to copy header
918 char c = inputAlignReport.get();
919 goodAlignReportOut << c;
920 if (c == 10 || c == 13){ break; }
923 while(!inputAlignReport.eof()){
924 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); m->mothurRemove(goodAlignReportFile); return 0; }
926 inputAlignReport >> seqName;
927 it = badSeqNames.find(seqName);
929 while (!inputAlignReport.eof()) { // need to copy header
930 char c = inputAlignReport.get();
932 if (c == 10 || c == 13){ break; }
935 if(it != badSeqNames.end()){
936 badSeqNames.erase(it);
939 goodAlignReportOut << seqName << '\t' << line;
941 m->gobble(inputAlignReport);
944 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); m->mothurRemove(goodAlignReportFile); return 0; }
946 //we were unable to remove some of the bad sequences
947 if (badSeqNames.size() != 0) {
948 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
949 m->mothurOut("Your alignreport file does not include the sequence " + *it + " please correct.");
950 m->mothurOutEndLine();
954 inputAlignReport.close();
955 goodAlignReportOut.close();
957 if (m->control_pressed) { m->mothurRemove(goodAlignReportFile); return 0; }
962 catch(exception& e) {
963 m->errorOut(e, "ScreenSeqsCommand", "screenAlignReport");
968 //***************************************************************************************************************
970 int ScreenSeqsCommand::screenTaxonomy(set<string> badSeqNames){
973 m->openInputFile(taxonomy, input);
975 set<string>::iterator it;
977 string goodTaxFile = outputDir + m->getRootName(m->getSimpleName(taxonomy)) + "good" + m->getExtension(taxonomy);
978 outputNames.push_back(goodTaxFile); outputTypes["taxonomy"].push_back(goodTaxFile);
979 ofstream goodTaxOut; m->openOutputFile(goodTaxFile, goodTaxOut);
982 if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; }
984 input >> seqName >> tax;
985 it = badSeqNames.find(seqName);
987 if(it != badSeqNames.end()){ badSeqNames.erase(it); }
989 goodTaxOut << seqName << '\t' << tax << endl;
994 if (m->control_pressed) { goodTaxOut.close(); input.close(); m->mothurRemove(goodTaxFile); return 0; }
996 //we were unable to remove some of the bad sequences
997 if (badSeqNames.size() != 0) {
998 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
999 m->mothurOut("Your taxonomy file does not include the sequence " + *it + " please correct.");
1000 m->mothurOutEndLine();
1007 if (m->control_pressed) { m->mothurRemove(goodTaxFile); return 0; }
1012 catch(exception& e) {
1013 m->errorOut(e, "ScreenSeqsCommand", "screenTaxonomy");
1018 //***************************************************************************************************************
1020 int ScreenSeqsCommand::screenQual(set<string> badSeqNames){
1023 m->openInputFile(qualfile, in);
1024 set<string>::iterator it;
1026 string goodQualFile = outputDir + m->getRootName(m->getSimpleName(qualfile)) + "good" + m->getExtension(qualfile);
1027 outputNames.push_back(goodQualFile); outputTypes["qfile"].push_back(goodQualFile);
1028 ofstream goodQual; m->openOutputFile(goodQualFile, goodQual);
1032 if (m->control_pressed) { goodQual.close(); in.close(); m->mothurRemove(goodQualFile); return 0; }
1034 string saveName = "";
1040 if (name.length() != 0) {
1041 saveName = name.substr(1);
1044 if (c == 10 || c == 13){ break; }
1051 char letter= in.get();
1052 if(letter == '>'){ in.putback(letter); break; }
1053 else{ scores += letter; }
1058 it = badSeqNames.find(saveName);
1060 if(it != badSeqNames.end()){
1061 badSeqNames.erase(it);
1063 goodQual << name << endl << scores;
1072 //we were unable to remove some of the bad sequences
1073 if (badSeqNames.size() != 0) {
1074 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
1075 m->mothurOut("Your qual file does not include the sequence " + *it + " please correct.");
1076 m->mothurOutEndLine();
1080 if (m->control_pressed) { m->mothurRemove(goodQualFile); return 0; }
1085 catch(exception& e) {
1086 m->errorOut(e, "ScreenSeqsCommand", "screenQual");
1091 //**********************************************************************************************************************
1093 int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set<string>& badSeqNames){
1096 m->openOutputFile(goodFName, goodFile);
1098 ofstream badAccnosFile;
1099 m->openOutputFile(badAccnosFName, badAccnosFile);
1102 m->openInputFile(filename, inFASTA);
1104 inFASTA.seekg(filePos->start);
1111 if (m->control_pressed) { return 0; }
1113 Sequence currSeq(inFASTA); m->gobble(inFASTA);
1114 if (currSeq.getName() != "") {
1115 bool goodSeq = 1; // innocent until proven guilty
1116 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
1117 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
1118 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
1119 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
1120 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
1121 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
1124 currSeq.printSequence(goodFile);
1127 badAccnosFile << currSeq.getName() << endl;
1128 badSeqNames.insert(currSeq.getName());
1133 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1134 unsigned long long pos = inFASTA.tellg();
1135 if ((pos == -1) || (pos >= filePos->end)) { break; }
1137 if (inFASTA.eof()) { break; }
1141 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
1144 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
1149 badAccnosFile.close();
1153 catch(exception& e) {
1154 m->errorOut(e, "ScreenSeqsCommand", "driver");
1158 //**********************************************************************************************************************
1160 int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long long>& MPIPos, set<string>& badSeqNames){
1162 string outputString = "";
1163 MPI_Status statusGood;
1164 MPI_Status statusBadAccnos;
1167 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
1169 for(int i=0;i<num;i++){
1171 if (m->control_pressed) { return 0; }
1173 //read next sequence
1174 int length = MPIPos[start+i+1] - MPIPos[start+i];
1176 char* buf4 = new char[length];
1177 memcpy(buf4, outputString.c_str(), length);
1179 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
1181 string tempBuf = buf4; delete buf4;
1182 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
1183 istringstream iss (tempBuf,istringstream::in);
1185 Sequence currSeq(iss);
1188 if (currSeq.getName() != "") {
1189 bool goodSeq = 1; // innocent until proven guilty
1190 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
1191 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
1192 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
1193 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
1194 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
1195 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
1198 outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n";
1201 length = outputString.length();
1202 char* buf2 = new char[length];
1203 memcpy(buf2, outputString.c_str(), length);
1205 MPI_File_write_shared(goodFile, buf2, length, MPI_CHAR, &statusGood);
1210 badSeqNames.insert(currSeq.getName());
1212 //write to bad accnos file
1213 outputString = currSeq.getName() + "\n";
1215 length = outputString.length();
1216 char* buf3 = new char[length];
1217 memcpy(buf3, outputString.c_str(), length);
1219 MPI_File_write_shared(badAccnosFile, buf3, length, MPI_CHAR, &statusBadAccnos);
1225 if((i) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i)); m->mothurOutEndLine(); }
1230 catch(exception& e) {
1231 m->errorOut(e, "ScreenSeqsCommand", "driverMPI");
1236 /**************************************************************************************************/
1238 int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set<string>& badSeqNames) {
1240 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1244 //loop through and create all the processes you want
1245 while (process != processors) {
1249 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1251 }else if (pid == 0){
1252 num = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames);
1254 //pass numSeqs to parent
1256 string tempFile = filename + toString(getpid()) + ".num.temp";
1257 m->openOutputFile(tempFile, out);
1263 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1264 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1269 //force parent to wait until all the processes are done
1270 for (int i=0;i<processors;i++) {
1271 int temp = processIDS[i];
1275 for (int i = 0; i < processIDS.size(); i++) {
1277 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
1278 m->openInputFile(tempFile, in);
1279 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
1280 in.close(); m->mothurRemove(tempFile);
1286 catch(exception& e) {
1287 m->errorOut(e, "ScreenSeqsCommand", "createProcesses");
1292 //***************************************************************************************************************