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, end, 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();
587 catch(exception& e) {
588 m->errorOut(e, "ScreenSeqsCommand", "screenNameGroupFile");
592 //***************************************************************************************************************
593 int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
596 vector<int> startPosition;
597 vector<int> endPosition;
598 vector<int> seqLength;
599 vector<int> ambigBases;
600 vector<int> longHomoPolymer;
602 vector<unsigned long int> positions = m->divideFile(fastafile, processors);
604 for (int i = 0; i < (positions.size()-1); i++) {
605 lines.push_back(new linePair(positions[i], positions[(i+1)]));
609 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
611 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
613 numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile);
616 if (m->control_pressed) { return 0; }
618 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
619 if (m->control_pressed) { return 0; }
622 sort(startPosition.begin(), startPosition.end());
623 sort(endPosition.begin(), endPosition.end());
624 sort(seqLength.begin(), seqLength.end());
625 sort(ambigBases.begin(), ambigBases.end());
626 sort(longHomoPolymer.begin(), longHomoPolymer.end());
628 int criteriaPercentile = int(numSeqs * (criteria / (float) 100));
630 for (int i = 0; i < optimize.size(); i++) {
631 if (optimize[i] == "start") { startPos = startPosition[criteriaPercentile]; m->mothurOut("Optimizing start to " + toString(startPos) + "."); m->mothurOutEndLine(); }
632 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();}
633 else if (optimize[i] == "maxambig") { maxAmbig = ambigBases[criteriaPercentile]; m->mothurOut("Optimizing maxambig to " + toString(maxAmbig) + "."); m->mothurOutEndLine(); }
634 else if (optimize[i] == "maxhomop") { maxHomoP = longHomoPolymer[criteriaPercentile]; m->mothurOut("Optimizing maxhomop to " + toString(maxHomoP) + "."); m->mothurOutEndLine(); }
635 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(); }
636 else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); }
641 catch(exception& e) {
642 m->errorOut(e, "ScreenSeqsCommand", "getSummary");
646 /**************************************************************************************/
647 int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, linePair* filePos) {
651 m->openInputFile(filename, in);
653 in.seekg(filePos->start);
660 if (m->control_pressed) { in.close(); return 1; }
662 Sequence current(in); m->gobble(in);
664 if (current.getName() != "") {
665 startPosition.push_back(current.getStartPos());
666 endPosition.push_back(current.getEndPos());
667 seqLength.push_back(current.getNumBases());
668 ambigBases.push_back(current.getAmbigBases());
669 longHomoPolymer.push_back(current.getLongHomoPolymer());
673 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
674 unsigned long int pos = in.tellg();
675 if ((pos == -1) || (pos >= filePos->end)) { break; }
677 if (in.eof()) { break; }
686 catch(exception& e) {
687 m->errorOut(e, "ScreenSeqsCommand", "driverCreateSummary");
691 /**************************************************************************************************/
692 int ScreenSeqsCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename) {
694 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
699 //loop through and create all the processes you want
700 while (process != processors) {
704 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
707 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[process]);
709 //pass numSeqs to parent
711 string tempFile = fastafile + toString(getpid()) + ".num.temp";
712 m->openOutputFile(tempFile, out);
715 for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
716 for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
717 for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
718 for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
719 for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
724 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
727 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
729 //force parent to wait until all the processes are done
730 for (int i=0;i<processIDS.size();i++) {
731 int temp = processIDS[i];
735 //parent reads in and combine Filter info
736 for (int i = 0; i < processIDS.size(); i++) {
737 string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
739 m->openInputFile(tempFilename, in);
742 in >> tempNum; m->gobble(in); num += tempNum;
743 for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
744 for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
745 for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
746 for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
747 for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
750 remove(tempFilename.c_str());
756 catch(exception& e) {
757 m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary");
762 //***************************************************************************************************************
764 int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
766 ifstream inputGroups;
767 m->openInputFile(groupfile, inputGroups);
768 string seqName, group;
769 set<string>::iterator it;
771 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
772 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
773 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
775 while(!inputGroups.eof()){
776 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
778 inputGroups >> seqName >> group;
779 it = badSeqNames.find(seqName);
781 if(it != badSeqNames.end()){
782 badSeqNames.erase(it);
785 goodGroupOut << seqName << '\t' << group << endl;
787 m->gobble(inputGroups);
790 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
792 //we were unable to remove some of the bad sequences
793 if (badSeqNames.size() != 0) {
794 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
795 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
796 m->mothurOutEndLine();
801 goodGroupOut.close();
803 if (m->control_pressed) { remove(goodGroupFile.c_str()); }
808 catch(exception& e) {
809 m->errorOut(e, "ScreenSeqsCommand", "screenGroupFile");
814 //***************************************************************************************************************
816 int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
818 ifstream inputAlignReport;
819 m->openInputFile(alignreport, inputAlignReport);
820 string seqName, group;
821 set<string>::iterator it;
823 string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport);
824 outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile);
825 ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut);
827 while (!inputAlignReport.eof()) { // need to copy header
828 char c = inputAlignReport.get();
829 goodAlignReportOut << c;
830 if (c == 10 || c == 13){ break; }
833 while(!inputAlignReport.eof()){
834 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
836 inputAlignReport >> seqName;
837 it = badSeqNames.find(seqName);
839 while (!inputAlignReport.eof()) { // need to copy header
840 char c = inputAlignReport.get();
842 if (c == 10 || c == 13){ break; }
845 if(it != badSeqNames.end()){
846 badSeqNames.erase(it);
849 goodAlignReportOut << seqName << '\t' << line;
851 m->gobble(inputAlignReport);
854 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
856 //we were unable to remove some of the bad sequences
857 if (badSeqNames.size() != 0) {
858 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
859 m->mothurOut("Your alignreport file does not include the sequence " + *it + " please correct.");
860 m->mothurOutEndLine();
864 inputAlignReport.close();
865 goodAlignReportOut.close();
867 if (m->control_pressed) { remove(goodAlignReportFile.c_str()); return 0; }
872 catch(exception& e) {
873 m->errorOut(e, "ScreenSeqsCommand", "screenAlignReport");
878 //**********************************************************************************************************************
880 int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set<string>& badSeqNames){
883 m->openOutputFile(goodFName, goodFile);
885 ofstream badAccnosFile;
886 m->openOutputFile(badAccnosFName, badAccnosFile);
889 m->openInputFile(filename, inFASTA);
891 inFASTA.seekg(filePos->start);
898 if (m->control_pressed) { return 0; }
900 Sequence currSeq(inFASTA); m->gobble(inFASTA);
901 if (currSeq.getName() != "") {
902 bool goodSeq = 1; // innocent until proven guilty
903 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
904 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
905 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
906 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
907 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
908 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
911 currSeq.printSequence(goodFile);
914 badAccnosFile << currSeq.getName() << endl;
915 badSeqNames.insert(currSeq.getName());
920 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
921 unsigned long int pos = inFASTA.tellg();
922 if ((pos == -1) || (pos >= filePos->end)) { break; }
924 if (inFASTA.eof()) { break; }
928 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
931 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
936 badAccnosFile.close();
940 catch(exception& e) {
941 m->errorOut(e, "ScreenSeqsCommand", "driver");
945 //**********************************************************************************************************************
947 int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long int>& MPIPos, set<string>& badSeqNames){
949 string outputString = "";
950 MPI_Status statusGood;
951 MPI_Status statusBadAccnos;
954 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
956 for(int i=0;i<num;i++){
958 if (m->control_pressed) { return 0; }
961 int length = MPIPos[start+i+1] - MPIPos[start+i];
963 char* buf4 = new char[length];
964 memcpy(buf4, outputString.c_str(), length);
966 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
968 string tempBuf = buf4; delete buf4;
969 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
970 istringstream iss (tempBuf,istringstream::in);
972 Sequence currSeq(iss);
975 if (currSeq.getName() != "") {
976 bool goodSeq = 1; // innocent until proven guilty
977 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
978 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
979 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
980 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
981 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
982 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
985 outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n";
988 length = outputString.length();
989 char* buf2 = new char[length];
990 memcpy(buf2, outputString.c_str(), length);
992 MPI_File_write_shared(goodFile, buf2, length, MPI_CHAR, &statusGood);
997 badSeqNames.insert(currSeq.getName());
999 //write to bad accnos file
1000 outputString = currSeq.getName() + "\n";
1002 length = outputString.length();
1003 char* buf3 = new char[length];
1004 memcpy(buf3, outputString.c_str(), length);
1006 MPI_File_write_shared(badAccnosFile, buf3, length, MPI_CHAR, &statusBadAccnos);
1014 catch(exception& e) {
1015 m->errorOut(e, "ScreenSeqsCommand", "driverMPI");
1020 /**************************************************************************************************/
1022 int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set<string>& badSeqNames) {
1024 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1028 //loop through and create all the processes you want
1029 while (process != processors) {
1033 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1035 }else if (pid == 0){
1036 num = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames);
1038 //pass numSeqs to parent
1040 string tempFile = filename + toString(getpid()) + ".num.temp";
1041 m->openOutputFile(tempFile, out);
1046 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
1049 //force parent to wait until all the processes are done
1050 for (int i=0;i<processors;i++) {
1051 int temp = processIDS[i];
1055 for (int i = 0; i < processIDS.size(); i++) {
1057 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
1058 m->openInputFile(tempFile, in);
1059 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
1060 in.close(); remove(tempFile.c_str());
1066 catch(exception& e) {
1067 m->errorOut(e, "ScreenSeqsCommand", "createProcesses");
1072 //***************************************************************************************************************