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::getValidParameters(){
16 string Array[] = {"fasta", "start", "end", "maxambig", "maxhomop","optimize","criteria", "minlength", "maxlength",
17 "name", "group", "alignreport","processors","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "ScreenSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 ScreenSeqsCommand::ScreenSeqsCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["fasta"] = tempOutNames;
33 outputTypes["name"] = tempOutNames;
34 outputTypes["group"] = tempOutNames;
35 outputTypes["alignreport"] = tempOutNames;
36 outputTypes["accnos"] = tempOutNames;
39 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
43 //**********************************************************************************************************************
44 vector<string> ScreenSeqsCommand::getRequiredParameters(){
46 string Array[] = {"fasta"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "ScreenSeqsCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> ScreenSeqsCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "ScreenSeqsCommand", "getRequiredFiles");
66 //***************************************************************************************************************
68 ScreenSeqsCommand::ScreenSeqsCommand(string option) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop","optimize","criteria", "minlength", "maxlength",
78 "name", "group", "alignreport","processors","outputdir","inputdir"};
79 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
84 ValidParameters validParameter("screen.seqs");
85 map<string,string>::iterator it;
87 //check to make sure all parameters are valid for command
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["fasta"] = tempOutNames;
95 outputTypes["name"] = tempOutNames;
96 outputTypes["group"] = tempOutNames;
97 outputTypes["alignreport"] = tempOutNames;
98 outputTypes["accnos"] = tempOutNames;
100 //if the user changes the input directory command factory will send this info to us in the output parameter
101 string inputDir = validParameter.validFile(parameters, "inputdir", false);
102 if (inputDir == "not found"){ inputDir = ""; }
105 it = parameters.find("fasta");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["fasta"] = inputDir + it->second; }
113 it = parameters.find("group");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["group"] = inputDir + it->second; }
121 it = parameters.find("name");
122 //user has given a template file
123 if(it != parameters.end()){
124 path = m->hasPath(it->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["name"] = inputDir + it->second; }
129 it = parameters.find("alignreport");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
138 //check for required parameters
139 fastafile = validParameter.validFile(parameters, "fasta", true);
140 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
141 else if (fastafile == "not open") { abort = true; }
143 groupfile = validParameter.validFile(parameters, "group", true);
144 if (groupfile == "not open") { abort = true; }
145 else if (groupfile == "not found") { groupfile = ""; }
147 namefile = validParameter.validFile(parameters, "name", true);
148 if (namefile == "not open") { abort = true; }
149 else if (namefile == "not found") { namefile = ""; }
151 alignreport = validParameter.validFile(parameters, "alignreport", true);
152 if (alignreport == "not open") { abort = true; }
153 else if (alignreport == "not found") { alignreport = ""; }
155 //if the user changes the output directory command factory will send this info to us in the output parameter
156 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
158 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
161 //check for optional parameter and set defaults
162 // ...at some point should added some additional type checking...
164 temp = validParameter.validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; }
165 convert(temp, startPos);
167 temp = validParameter.validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; }
168 convert(temp, endPos);
170 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
171 convert(temp, maxAmbig);
173 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; }
174 convert(temp, maxHomoP);
176 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; }
177 convert(temp, minLength);
179 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; }
180 convert(temp, maxLength);
182 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
183 convert(temp, processors);
185 temp = validParameter.validFile(parameters, "optimize", false); //optimizing trumps the optimized values original value
186 if (temp == "not found"){ temp = ""; }
187 else { m->splitAtDash(temp, optimize); }
189 //check for invalid optimize options
190 set<string> validOptimizers;
191 validOptimizers.insert("start"); validOptimizers.insert("end"); validOptimizers.insert("maxambig"); validOptimizers.insert("maxhomop"); validOptimizers.insert("minlength"); validOptimizers.insert("maxlength");
193 for (int i = 0; i < optimize.size(); i++) {
194 if (validOptimizers.count(optimize[i]) == 0) {
195 m->mothurOut(optimize[i] + " is not a valid optimizer. Valid options are start, end, maxambig, maxhomop, minlength and maxlength."); m->mothurOutEndLine();
196 optimize.erase(optimize.begin()+i);
201 temp = validParameter.validFile(parameters, "criteria", false); if (temp == "not found"){ temp = "90"; }
202 convert(temp, criteria);
206 catch(exception& e) {
207 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
211 //**********************************************************************************************************************
213 void ScreenSeqsCommand::help(){
215 m->mothurOut("The screen.seqs command reads a fastafile and creates .....\n");
216 m->mothurOut("The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, optimize, criteria and processors.\n");
217 m->mothurOut("The fasta parameter is required.\n");
218 m->mothurOut("The start parameter .... The default is -1.\n");
219 m->mothurOut("The end parameter .... The default is -1.\n");
220 m->mothurOut("The maxambig parameter .... The default is -1.\n");
221 m->mothurOut("The maxhomop parameter .... The default is -1.\n");
222 m->mothurOut("The minlength parameter .... The default is -1.\n");
223 m->mothurOut("The maxlength parameter .... The default is -1.\n");
224 m->mothurOut("The processors parameter allows you to specify the number of processors to use while running the command. The default is 1.\n");
225 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");
226 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");
227 m->mothurOut("The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n");
228 m->mothurOut("The screen.seqs command should be in the following format: \n");
229 m->mothurOut("screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, \n");
230 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
231 m->mothurOut("Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
232 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
235 catch(exception& e) {
236 m->errorOut(e, "ScreenSeqsCommand", "help");
241 //***************************************************************************************************************
243 ScreenSeqsCommand::~ScreenSeqsCommand(){ /* do nothing */ }
245 //***************************************************************************************************************
247 int ScreenSeqsCommand::execute(){
250 if (abort == true) { return 0; }
252 //if the user want to optimize we need to no the 90% mark
253 vector<unsigned long int> positions;
254 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
256 positions = m->divideFile(fastafile, processors);
257 for (int i = 0; i < (positions.size()-1); i++) {
258 lines.push_back(new linePair(positions[i], positions[(i+1)]));
262 string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile);
263 string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos";
265 int numFastaSeqs = 0;
266 set<string> badSeqNames;
267 int start = time(NULL);
270 int pid, numSeqsPerProcessor;
272 vector<unsigned long int> MPIPos;
275 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
276 MPI_Comm_size(MPI_COMM_WORLD, &processors);
280 MPI_File outMPIBadAccnos;
282 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
283 int inMode=MPI_MODE_RDONLY;
285 char outGoodFilename[1024];
286 strcpy(outGoodFilename, goodSeqFile.c_str());
288 char outBadAccnosFilename[1024];
289 strcpy(outBadAccnosFilename, badAccnosFile.c_str());
291 char inFileName[1024];
292 strcpy(inFileName, fastafile.c_str());
294 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
295 MPI_File_open(MPI_COMM_WORLD, outGoodFilename, outMode, MPI_INFO_NULL, &outMPIGood);
296 MPI_File_open(MPI_COMM_WORLD, outBadAccnosFilename, outMode, MPI_INFO_NULL, &outMPIBadAccnos);
298 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
300 if (pid == 0) { //you are the root process
302 MPIPos = m->setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs
304 //send file positions to all processes
305 for(int i = 1; i < processors; i++) {
306 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
307 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
310 //figure out how many sequences you have to align
311 numSeqsPerProcessor = numFastaSeqs / processors;
312 int startIndex = pid * numSeqsPerProcessor;
313 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
314 // cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
316 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
317 //cout << pid << " done" << endl;
318 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
320 for (int i = 1; i < processors; i++) {
324 MPI_Recv(&badSize, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
325 /*for (int j = 0; j < badSize; j++) {
327 MPI_Recv(&length, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status); //recv the length of the name
328 char* buf2 = new char[length]; //make space to recieve it
329 MPI_Recv(buf2, length, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); //get name
331 string tempBuf = buf2;
332 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
335 badSeqNames.insert(tempBuf);
338 }else{ //you are a child process
339 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
340 MPIPos.resize(numFastaSeqs+1);
341 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
343 //figure out how many sequences you have to align
344 numSeqsPerProcessor = numFastaSeqs / processors;
345 int startIndex = pid * numSeqsPerProcessor;
346 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
347 //cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
349 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
350 //cout << pid << " done" << endl;
351 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
354 int badSize = badSeqNames.size();
355 MPI_Send(&badSize, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
358 set<string>::iterator it;
359 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
361 int length = name.length();
362 char* buf2 = new char[length];
363 memcpy(buf2, name.c_str(), length);
365 MPI_Send(&length, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
366 MPI_Send(buf2, length, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
371 MPI_File_close(&inMPI);
372 MPI_File_close(&outMPIGood);
373 MPI_File_close(&outMPIBadAccnos);
374 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
378 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
380 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
382 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
385 processIDS.resize(0);
387 numFastaSeqs = createProcesses(goodSeqFile, badAccnosFile, fastafile, badSeqNames);
389 rename((goodSeqFile + toString(processIDS[0]) + ".temp").c_str(), goodSeqFile.c_str());
390 rename((badAccnosFile + toString(processIDS[0]) + ".temp").c_str(), badAccnosFile.c_str());
392 //append alignment and report files
393 for(int i=1;i<processors;i++){
394 m->appendFiles((goodSeqFile + toString(processIDS[i]) + ".temp"), goodSeqFile);
395 remove((goodSeqFile + toString(processIDS[i]) + ".temp").c_str());
397 m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile);
398 remove((badAccnosFile + toString(processIDS[i]) + ".temp").c_str());
401 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
403 //read badSeqs in because root process doesnt know what other "bad" seqs the children found
405 int ableToOpen = m->openInputFile(badAccnosFile, inBad, "no error");
407 if (ableToOpen == 0) {
410 while (!inBad.eof()) {
411 inBad >> tempName; m->gobble(inBad);
412 badSeqNames.insert(tempName);
418 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
420 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
427 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
429 if (pid == 0) { //only one process should fix files
431 //read accnos file with all names in it, process 0 just has its names
432 MPI_File inMPIAccnos;
435 char inFileName[1024];
436 strcpy(inFileName, badAccnosFile.c_str());
438 MPI_File_open(MPI_COMM_SELF, inFileName, inMode, MPI_INFO_NULL, &inMPIAccnos); //comm, filename, mode, info, filepointer
439 MPI_File_get_size(inMPIAccnos, &size);
441 char* buffer = new char[size];
442 MPI_File_read(inMPIAccnos, buffer, size, MPI_CHAR, &status);
444 string tempBuf = buffer;
445 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
446 istringstream iss (tempBuf,istringstream::in);
449 MPI_File_close(&inMPIAccnos);
454 iss >> tempName; m->gobble(iss);
455 badSeqNames.insert(tempName);
459 if(namefile != "" && groupfile != "") {
460 screenNameGroupFile(badSeqNames);
461 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
462 }else if(namefile != "") {
463 screenNameGroupFile(badSeqNames);
464 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
465 }else if(groupfile != "") { screenGroupFile(badSeqNames); } // this screens just the group
467 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
469 if(alignreport != "") { screenAlignReport(badSeqNames); }
471 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
477 m->mothurOutEndLine();
478 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
479 m->mothurOut(goodSeqFile); m->mothurOutEndLine(); outputTypes["fasta"].push_back(goodSeqFile);
480 m->mothurOut(badAccnosFile); m->mothurOutEndLine(); outputTypes["accnos"].push_back(badAccnosFile);
481 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
482 m->mothurOutEndLine();
483 m->mothurOutEndLine();
485 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences.");
486 m->mothurOutEndLine();
490 catch(exception& e) {
491 m->errorOut(e, "ScreenSeqsCommand", "execute");
496 //***************************************************************************************************************
498 int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
501 m->openInputFile(namefile, inputNames);
502 set<string> badSeqGroups;
503 string seqName, seqList, group;
504 set<string>::iterator it;
506 string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile);
507 outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile);
509 ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut);
511 while(!inputNames.eof()){
512 if (m->control_pressed) { goodNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); return 0; }
514 inputNames >> seqName >> seqList;
515 it = badSeqNames.find(seqName);
517 if(it != badSeqNames.end()){
518 badSeqNames.erase(it);
522 for(int i=0;i<seqList.length();i++){
523 if(seqList[i] == ','){
524 badSeqGroups.insert(seqList.substr(start,i-start));
528 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
532 goodNameOut << seqName << '\t' << seqList << endl;
534 m->gobble(inputNames);
539 //we were unable to remove some of the bad sequences
540 if (badSeqNames.size() != 0) {
541 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
542 m->mothurOut("Your namefile does not include the sequence " + *it + " please correct.");
543 m->mothurOutEndLine();
549 ifstream inputGroups;
550 m->openInputFile(groupfile, inputGroups);
552 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
553 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
555 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
557 while(!inputGroups.eof()){
558 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str()); remove(goodGroupFile.c_str()); return 0; }
560 inputGroups >> seqName >> group;
562 it = badSeqGroups.find(seqName);
564 if(it != badSeqGroups.end()){
565 badSeqGroups.erase(it);
568 goodGroupOut << seqName << '\t' << group << endl;
570 m->gobble(inputGroups);
573 goodGroupOut.close();
575 //we were unable to remove some of the bad sequences
576 if (badSeqGroups.size() != 0) {
577 for (it = badSeqGroups.begin(); it != badSeqGroups.end(); it++) {
578 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
579 m->mothurOutEndLine();
588 catch(exception& e) {
589 m->errorOut(e, "ScreenSeqsCommand", "screenNameGroupFile");
593 //***************************************************************************************************************
594 int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
597 vector<int> startPosition;
598 vector<int> endPosition;
599 vector<int> seqLength;
600 vector<int> ambigBases;
601 vector<int> longHomoPolymer;
603 vector<unsigned long int> positions = m->divideFile(fastafile, processors);
605 for (int i = 0; i < (positions.size()-1); i++) {
606 lines.push_back(new linePair(positions[i], positions[(i+1)]));
610 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
612 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
614 numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile);
617 if (m->control_pressed) { return 0; }
619 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
620 if (m->control_pressed) { return 0; }
623 sort(startPosition.begin(), startPosition.end());
624 sort(endPosition.begin(), endPosition.end());
625 sort(seqLength.begin(), seqLength.end());
626 sort(ambigBases.begin(), ambigBases.end());
627 sort(longHomoPolymer.begin(), longHomoPolymer.end());
629 int criteriaPercentile = int(numSeqs * (criteria / (float) 100));
631 for (int i = 0; i < optimize.size(); i++) {
632 if (optimize[i] == "start") { startPos = startPosition[criteriaPercentile]; m->mothurOut("Optimizing start to " + toString(startPos) + "."); m->mothurOutEndLine(); }
633 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();}
634 else if (optimize[i] == "maxambig") { maxAmbig = ambigBases[criteriaPercentile]; m->mothurOut("Optimizing maxambig to " + toString(maxAmbig) + "."); m->mothurOutEndLine(); }
635 else if (optimize[i] == "maxhomop") { maxHomoP = longHomoPolymer[criteriaPercentile]; m->mothurOut("Optimizing maxhomop to " + toString(maxHomoP) + "."); m->mothurOutEndLine(); }
636 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(); }
637 else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); }
642 catch(exception& e) {
643 m->errorOut(e, "ScreenSeqsCommand", "getSummary");
647 /**************************************************************************************/
648 int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, linePair* filePos) {
652 m->openInputFile(filename, in);
654 in.seekg(filePos->start);
661 if (m->control_pressed) { in.close(); return 1; }
663 Sequence current(in); m->gobble(in);
665 if (current.getName() != "") {
666 startPosition.push_back(current.getStartPos());
667 endPosition.push_back(current.getEndPos());
668 seqLength.push_back(current.getNumBases());
669 ambigBases.push_back(current.getAmbigBases());
670 longHomoPolymer.push_back(current.getLongHomoPolymer());
674 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
675 unsigned long int pos = in.tellg();
676 if ((pos == -1) || (pos >= filePos->end)) { break; }
678 if (in.eof()) { break; }
687 catch(exception& e) {
688 m->errorOut(e, "ScreenSeqsCommand", "driverCreateSummary");
692 /**************************************************************************************************/
693 int ScreenSeqsCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename) {
695 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
700 //loop through and create all the processes you want
701 while (process != processors) {
705 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
708 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[process]);
710 //pass numSeqs to parent
712 string tempFile = fastafile + toString(getpid()) + ".num.temp";
713 m->openOutputFile(tempFile, out);
716 for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
717 for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
718 for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
719 for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
720 for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
726 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
727 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
732 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
734 //force parent to wait until all the processes are done
735 for (int i=0;i<processIDS.size();i++) {
736 int temp = processIDS[i];
740 //parent reads in and combine Filter info
741 for (int i = 0; i < processIDS.size(); i++) {
742 string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
744 m->openInputFile(tempFilename, in);
747 in >> tempNum; m->gobble(in); num += tempNum;
748 for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
749 for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
750 for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
751 for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
752 for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
755 remove(tempFilename.c_str());
761 catch(exception& e) {
762 m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary");
767 //***************************************************************************************************************
769 int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
771 ifstream inputGroups;
772 m->openInputFile(groupfile, inputGroups);
773 string seqName, group;
774 set<string>::iterator it;
776 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
777 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
778 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
780 while(!inputGroups.eof()){
781 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
783 inputGroups >> seqName >> group;
784 it = badSeqNames.find(seqName);
786 if(it != badSeqNames.end()){
787 badSeqNames.erase(it);
790 goodGroupOut << seqName << '\t' << group << endl;
792 m->gobble(inputGroups);
795 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
797 //we were unable to remove some of the bad sequences
798 if (badSeqNames.size() != 0) {
799 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
800 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
801 m->mothurOutEndLine();
806 goodGroupOut.close();
808 if (m->control_pressed) { remove(goodGroupFile.c_str()); }
813 catch(exception& e) {
814 m->errorOut(e, "ScreenSeqsCommand", "screenGroupFile");
819 //***************************************************************************************************************
821 int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
823 ifstream inputAlignReport;
824 m->openInputFile(alignreport, inputAlignReport);
825 string seqName, group;
826 set<string>::iterator it;
828 string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport);
829 outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile);
830 ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut);
832 while (!inputAlignReport.eof()) { // need to copy header
833 char c = inputAlignReport.get();
834 goodAlignReportOut << c;
835 if (c == 10 || c == 13){ break; }
838 while(!inputAlignReport.eof()){
839 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
841 inputAlignReport >> seqName;
842 it = badSeqNames.find(seqName);
844 while (!inputAlignReport.eof()) { // need to copy header
845 char c = inputAlignReport.get();
847 if (c == 10 || c == 13){ break; }
850 if(it != badSeqNames.end()){
851 badSeqNames.erase(it);
854 goodAlignReportOut << seqName << '\t' << line;
856 m->gobble(inputAlignReport);
859 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
861 //we were unable to remove some of the bad sequences
862 if (badSeqNames.size() != 0) {
863 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
864 m->mothurOut("Your alignreport file does not include the sequence " + *it + " please correct.");
865 m->mothurOutEndLine();
869 inputAlignReport.close();
870 goodAlignReportOut.close();
872 if (m->control_pressed) { remove(goodAlignReportFile.c_str()); return 0; }
877 catch(exception& e) {
878 m->errorOut(e, "ScreenSeqsCommand", "screenAlignReport");
883 //**********************************************************************************************************************
885 int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set<string>& badSeqNames){
888 m->openOutputFile(goodFName, goodFile);
890 ofstream badAccnosFile;
891 m->openOutputFile(badAccnosFName, badAccnosFile);
894 m->openInputFile(filename, inFASTA);
896 inFASTA.seekg(filePos->start);
903 if (m->control_pressed) { return 0; }
905 Sequence currSeq(inFASTA); m->gobble(inFASTA);
906 if (currSeq.getName() != "") {
907 bool goodSeq = 1; // innocent until proven guilty
908 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
909 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
910 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
911 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
912 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
913 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
916 currSeq.printSequence(goodFile);
919 badAccnosFile << currSeq.getName() << endl;
920 badSeqNames.insert(currSeq.getName());
925 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
926 unsigned long int pos = inFASTA.tellg();
927 if ((pos == -1) || (pos >= filePos->end)) { break; }
929 if (inFASTA.eof()) { break; }
933 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
936 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
941 badAccnosFile.close();
945 catch(exception& e) {
946 m->errorOut(e, "ScreenSeqsCommand", "driver");
950 //**********************************************************************************************************************
952 int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long int>& MPIPos, set<string>& badSeqNames){
954 string outputString = "";
955 MPI_Status statusGood;
956 MPI_Status statusBadAccnos;
959 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
961 for(int i=0;i<num;i++){
963 if (m->control_pressed) { return 0; }
966 int length = MPIPos[start+i+1] - MPIPos[start+i];
968 char* buf4 = new char[length];
969 memcpy(buf4, outputString.c_str(), length);
971 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
973 string tempBuf = buf4; delete buf4;
974 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
975 istringstream iss (tempBuf,istringstream::in);
977 Sequence currSeq(iss);
980 if (currSeq.getName() != "") {
981 bool goodSeq = 1; // innocent until proven guilty
982 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
983 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
984 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
985 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
986 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
987 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
990 outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n";
993 length = outputString.length();
994 char* buf2 = new char[length];
995 memcpy(buf2, outputString.c_str(), length);
997 MPI_File_write_shared(goodFile, buf2, length, MPI_CHAR, &statusGood);
1002 badSeqNames.insert(currSeq.getName());
1004 //write to bad accnos file
1005 outputString = currSeq.getName() + "\n";
1007 length = outputString.length();
1008 char* buf3 = new char[length];
1009 memcpy(buf3, outputString.c_str(), length);
1011 MPI_File_write_shared(badAccnosFile, buf3, length, MPI_CHAR, &statusBadAccnos);
1017 if((i) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i)); m->mothurOutEndLine(); }
1022 catch(exception& e) {
1023 m->errorOut(e, "ScreenSeqsCommand", "driverMPI");
1028 /**************************************************************************************************/
1030 int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set<string>& badSeqNames) {
1032 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1036 //loop through and create all the processes you want
1037 while (process != processors) {
1041 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1043 }else if (pid == 0){
1044 num = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames);
1046 //pass numSeqs to parent
1048 string tempFile = filename + toString(getpid()) + ".num.temp";
1049 m->openOutputFile(tempFile, out);
1055 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1056 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1061 //force parent to wait until all the processes are done
1062 for (int i=0;i<processors;i++) {
1063 int temp = processIDS[i];
1067 for (int i = 0; i < processIDS.size(); i++) {
1069 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
1070 m->openInputFile(tempFile, in);
1071 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
1072 in.close(); remove(tempFile.c_str());
1078 catch(exception& e) {
1079 m->errorOut(e, "ScreenSeqsCommand", "createProcesses");
1084 //***************************************************************************************************************