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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["name"] = tempOutNames;
33 outputTypes["group"] = tempOutNames;
34 outputTypes["alignreport"] = tempOutNames;
35 outputTypes["accnos"] = tempOutNames;
38 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
42 //**********************************************************************************************************************
43 vector<string> ScreenSeqsCommand::getRequiredParameters(){
45 string Array[] = {"fasta"};
46 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
50 m->errorOut(e, "ScreenSeqsCommand", "getRequiredParameters");
54 //**********************************************************************************************************************
55 vector<string> ScreenSeqsCommand::getRequiredFiles(){
57 vector<string> myArray;
61 m->errorOut(e, "ScreenSeqsCommand", "getRequiredFiles");
65 //***************************************************************************************************************
67 ScreenSeqsCommand::ScreenSeqsCommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); abort = true; calledHelp = true; }
75 //valid paramters for this command
76 string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop","optimize","criteria", "minlength", "maxlength",
77 "name", "group", "alignreport","processors","outputdir","inputdir"};
78 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter("screen.seqs");
84 map<string,string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["fasta"] = tempOutNames;
94 outputTypes["name"] = tempOutNames;
95 outputTypes["group"] = tempOutNames;
96 outputTypes["alignreport"] = tempOutNames;
97 outputTypes["accnos"] = tempOutNames;
99 //if the user changes the input directory command factory will send this info to us in the output parameter
100 string inputDir = validParameter.validFile(parameters, "inputdir", false);
101 if (inputDir == "not found"){ inputDir = ""; }
104 it = parameters.find("fasta");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["fasta"] = inputDir + it->second; }
112 it = parameters.find("group");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["group"] = inputDir + it->second; }
120 it = parameters.find("name");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["name"] = inputDir + it->second; }
128 it = parameters.find("alignreport");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
137 //check for required parameters
138 fastafile = validParameter.validFile(parameters, "fasta", true);
139 if (fastafile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
140 else if (fastafile == "not open") { abort = true; }
142 groupfile = validParameter.validFile(parameters, "group", true);
143 if (groupfile == "not open") { abort = true; }
144 else if (groupfile == "not found") { groupfile = ""; }
146 namefile = validParameter.validFile(parameters, "name", true);
147 if (namefile == "not open") { namefile = ""; abort = true; }
148 else if (namefile == "not found") { namefile = ""; }
150 alignreport = validParameter.validFile(parameters, "alignreport", true);
151 if (alignreport == "not open") { abort = true; }
152 else if (alignreport == "not found") { alignreport = ""; }
154 //if the user changes the output directory command factory will send this info to us in the output parameter
155 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
157 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
160 //check for optional parameter and set defaults
161 // ...at some point should added some additional type checking...
163 temp = validParameter.validFile(parameters, "start", false); if (temp == "not found") { temp = "-1"; }
164 convert(temp, startPos);
166 temp = validParameter.validFile(parameters, "end", false); if (temp == "not found") { temp = "-1"; }
167 convert(temp, endPos);
169 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
170 convert(temp, maxAmbig);
172 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "-1"; }
173 convert(temp, maxHomoP);
175 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "-1"; }
176 convert(temp, minLength);
178 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "-1"; }
179 convert(temp, maxLength);
181 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
182 convert(temp, processors);
184 temp = validParameter.validFile(parameters, "optimize", false); //optimizing trumps the optimized values original value
185 if (temp == "not found"){ temp = ""; }
186 else { m->splitAtDash(temp, optimize); }
188 //check for invalid optimize options
189 set<string> validOptimizers;
190 validOptimizers.insert("start"); validOptimizers.insert("end"); validOptimizers.insert("maxambig"); validOptimizers.insert("maxhomop"); validOptimizers.insert("minlength"); validOptimizers.insert("maxlength");
192 for (int i = 0; i < optimize.size(); i++) {
193 if (validOptimizers.count(optimize[i]) == 0) {
194 m->mothurOut(optimize[i] + " is not a valid optimizer. Valid options are start, end, maxambig, maxhomop, minlength and maxlength."); m->mothurOutEndLine();
195 optimize.erase(optimize.begin()+i);
200 temp = validParameter.validFile(parameters, "criteria", false); if (temp == "not found"){ temp = "90"; }
201 convert(temp, criteria);
205 catch(exception& e) {
206 m->errorOut(e, "ScreenSeqsCommand", "ScreenSeqsCommand");
210 //**********************************************************************************************************************
212 void ScreenSeqsCommand::help(){
214 m->mothurOut("The screen.seqs command reads a fastafile and creates .....\n");
215 m->mothurOut("The screen.seqs command parameters are fasta, start, end, maxambig, maxhomop, minlength, maxlength, name, group, optimize, criteria and processors.\n");
216 m->mothurOut("The fasta parameter is required.\n");
217 m->mothurOut("The start parameter .... The default is -1.\n");
218 m->mothurOut("The end parameter .... The default is -1.\n");
219 m->mothurOut("The maxambig parameter .... The default is -1.\n");
220 m->mothurOut("The maxhomop parameter .... The default is -1.\n");
221 m->mothurOut("The minlength parameter .... The default is -1.\n");
222 m->mothurOut("The maxlength parameter .... The default is -1.\n");
223 m->mothurOut("The processors parameter allows you to specify the number of processors to use while running the command. The default is 1.\n");
224 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");
225 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");
226 m->mothurOut("The name parameter allows you to provide a namesfile, and the group parameter allows you to provide a groupfile.\n");
227 m->mothurOut("The screen.seqs command should be in the following format: \n");
228 m->mothurOut("screen.seqs(fasta=yourFastaFile, name=youNameFile, group=yourGroupFIle, start=yourStart, end=yourEnd, maxambig=yourMaxambig, \n");
229 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
230 m->mothurOut("Example screen.seqs(fasta=abrecovery.fasta, name=abrecovery.names, group=abrecovery.groups, start=..., end=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
231 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
234 catch(exception& e) {
235 m->errorOut(e, "ScreenSeqsCommand", "help");
240 //***************************************************************************************************************
242 ScreenSeqsCommand::~ScreenSeqsCommand(){ /* do nothing */ }
244 //***************************************************************************************************************
246 int ScreenSeqsCommand::execute(){
249 if (abort == true) { if (calledHelp) { return 0; } return 2; }
251 //if the user want to optimize we need to know the 90% mark
252 vector<unsigned long int> positions;
253 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
254 //use the namefile to optimize correctly
255 if (namefile != "") { nameMap = m->readNames(namefile); }
256 getSummary(positions);
259 positions = m->divideFile(fastafile, processors);
260 for (int i = 0; i < (positions.size()-1); i++) {
261 lines.push_back(new linePair(positions[i], positions[(i+1)]));
265 string goodSeqFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "good" + m->getExtension(fastafile);
266 string badAccnosFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "bad.accnos";
268 int numFastaSeqs = 0;
269 set<string> badSeqNames;
270 int start = time(NULL);
273 int pid, numSeqsPerProcessor;
275 vector<unsigned long int> MPIPos;
278 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
279 MPI_Comm_size(MPI_COMM_WORLD, &processors);
283 MPI_File outMPIBadAccnos;
285 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
286 int inMode=MPI_MODE_RDONLY;
288 char outGoodFilename[1024];
289 strcpy(outGoodFilename, goodSeqFile.c_str());
291 char outBadAccnosFilename[1024];
292 strcpy(outBadAccnosFilename, badAccnosFile.c_str());
294 char inFileName[1024];
295 strcpy(inFileName, fastafile.c_str());
297 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
298 MPI_File_open(MPI_COMM_WORLD, outGoodFilename, outMode, MPI_INFO_NULL, &outMPIGood);
299 MPI_File_open(MPI_COMM_WORLD, outBadAccnosFilename, outMode, MPI_INFO_NULL, &outMPIBadAccnos);
301 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
303 if (pid == 0) { //you are the root process
305 MPIPos = m->setFilePosFasta(fastafile, numFastaSeqs); //fills MPIPos, returns numSeqs
307 //send file positions to all processes
308 for(int i = 1; i < processors; i++) {
309 MPI_Send(&numFastaSeqs, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
310 MPI_Send(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
313 //figure out how many sequences you have to align
314 numSeqsPerProcessor = numFastaSeqs / processors;
315 int startIndex = pid * numSeqsPerProcessor;
316 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
317 // cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
319 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
320 //cout << pid << " done" << endl;
321 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
323 for (int i = 1; i < processors; i++) {
327 MPI_Recv(&badSize, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status);
328 /*for (int j = 0; j < badSize; j++) {
330 MPI_Recv(&length, 1, MPI_INT, i, tag, MPI_COMM_WORLD, &status); //recv the length of the name
331 char* buf2 = new char[length]; //make space to recieve it
332 MPI_Recv(buf2, length, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); //get name
334 string tempBuf = buf2;
335 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
338 badSeqNames.insert(tempBuf);
341 }else{ //you are a child process
342 MPI_Recv(&numFastaSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
343 MPIPos.resize(numFastaSeqs+1);
344 MPI_Recv(&MPIPos[0], (numFastaSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
346 //figure out how many sequences you have to align
347 numSeqsPerProcessor = numFastaSeqs / processors;
348 int startIndex = pid * numSeqsPerProcessor;
349 if(pid == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - pid * numSeqsPerProcessor; }
350 //cout << pid << '\t' << numSeqsPerProcessor << '\t' << startIndex << endl;
352 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPIGood, outMPIBadAccnos, MPIPos, badSeqNames);
353 //cout << pid << " done" << endl;
354 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPIGood); MPI_File_close(&outMPIBadAccnos); return 0; }
357 int badSize = badSeqNames.size();
358 MPI_Send(&badSize, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
361 set<string>::iterator it;
362 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
364 int length = name.length();
365 char* buf2 = new char[length];
366 memcpy(buf2, name.c_str(), length);
368 MPI_Send(&length, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
369 MPI_Send(buf2, length, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
374 MPI_File_close(&inMPI);
375 MPI_File_close(&outMPIGood);
376 MPI_File_close(&outMPIBadAccnos);
377 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
381 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
383 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
385 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
388 processIDS.resize(0);
390 numFastaSeqs = createProcesses(goodSeqFile, badAccnosFile, fastafile, badSeqNames);
392 rename((goodSeqFile + toString(processIDS[0]) + ".temp").c_str(), goodSeqFile.c_str());
393 rename((badAccnosFile + toString(processIDS[0]) + ".temp").c_str(), badAccnosFile.c_str());
395 //append alignment and report files
396 for(int i=1;i<processors;i++){
397 m->appendFiles((goodSeqFile + toString(processIDS[i]) + ".temp"), goodSeqFile);
398 remove((goodSeqFile + toString(processIDS[i]) + ".temp").c_str());
400 m->appendFiles((badAccnosFile + toString(processIDS[i]) + ".temp"), badAccnosFile);
401 remove((badAccnosFile + toString(processIDS[i]) + ".temp").c_str());
404 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
406 //read badSeqs in because root process doesnt know what other "bad" seqs the children found
408 int ableToOpen = m->openInputFile(badAccnosFile, inBad, "no error");
410 if (ableToOpen == 0) {
413 while (!inBad.eof()) {
414 inBad >> tempName; m->gobble(inBad);
415 badSeqNames.insert(tempName);
421 numFastaSeqs = driver(lines[0], goodSeqFile, badAccnosFile, fastafile, badSeqNames);
423 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
430 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
432 if (pid == 0) { //only one process should fix files
434 //read accnos file with all names in it, process 0 just has its names
435 MPI_File inMPIAccnos;
438 char inFileName[1024];
439 strcpy(inFileName, badAccnosFile.c_str());
441 MPI_File_open(MPI_COMM_SELF, inFileName, inMode, MPI_INFO_NULL, &inMPIAccnos); //comm, filename, mode, info, filepointer
442 MPI_File_get_size(inMPIAccnos, &size);
444 char* buffer = new char[size];
445 MPI_File_read(inMPIAccnos, buffer, size, MPI_CHAR, &status);
447 string tempBuf = buffer;
448 if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size); }
449 istringstream iss (tempBuf,istringstream::in);
452 MPI_File_close(&inMPIAccnos);
457 iss >> tempName; m->gobble(iss);
458 badSeqNames.insert(tempName);
462 if(namefile != "" && groupfile != "") {
463 screenNameGroupFile(badSeqNames);
464 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
465 }else if(namefile != "") {
466 screenNameGroupFile(badSeqNames);
467 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
468 }else if(groupfile != "") { screenGroupFile(badSeqNames); } // this screens just the group
470 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
472 if(alignreport != "") { screenAlignReport(badSeqNames); }
474 if (m->control_pressed) { remove(goodSeqFile.c_str()); return 0; }
480 m->mothurOutEndLine();
481 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
482 m->mothurOut(goodSeqFile); m->mothurOutEndLine(); outputTypes["fasta"].push_back(goodSeqFile);
483 m->mothurOut(badAccnosFile); m->mothurOutEndLine(); outputTypes["accnos"].push_back(badAccnosFile);
484 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
485 m->mothurOutEndLine();
486 m->mothurOutEndLine();
488 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to screen " + toString(numFastaSeqs) + " sequences.");
489 m->mothurOutEndLine();
493 catch(exception& e) {
494 m->errorOut(e, "ScreenSeqsCommand", "execute");
499 //***************************************************************************************************************
501 int ScreenSeqsCommand::screenNameGroupFile(set<string> badSeqNames){
504 m->openInputFile(namefile, inputNames);
505 set<string> badSeqGroups;
506 string seqName, seqList, group;
507 set<string>::iterator it;
509 string goodNameFile = outputDir + m->getRootName(m->getSimpleName(namefile)) + "good" + m->getExtension(namefile);
510 outputNames.push_back(goodNameFile); outputTypes["name"].push_back(goodNameFile);
512 ofstream goodNameOut; m->openOutputFile(goodNameFile, goodNameOut);
514 while(!inputNames.eof()){
515 if (m->control_pressed) { goodNameOut.close(); inputNames.close(); remove(goodNameFile.c_str()); return 0; }
517 inputNames >> seqName >> seqList;
518 it = badSeqNames.find(seqName);
520 if(it != badSeqNames.end()){
521 badSeqNames.erase(it);
525 for(int i=0;i<seqList.length();i++){
526 if(seqList[i] == ','){
527 badSeqGroups.insert(seqList.substr(start,i-start));
531 badSeqGroups.insert(seqList.substr(start,seqList.length()-start));
535 goodNameOut << seqName << '\t' << seqList << endl;
537 m->gobble(inputNames);
542 //we were unable to remove some of the bad sequences
543 if (badSeqNames.size() != 0) {
544 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
545 m->mothurOut("Your namefile does not include the sequence " + *it + " please correct.");
546 m->mothurOutEndLine();
552 ifstream inputGroups;
553 m->openInputFile(groupfile, inputGroups);
555 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
556 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
558 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
560 while(!inputGroups.eof()){
561 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodNameFile.c_str()); remove(goodGroupFile.c_str()); return 0; }
563 inputGroups >> seqName >> group;
565 it = badSeqGroups.find(seqName);
567 if(it != badSeqGroups.end()){
568 badSeqGroups.erase(it);
571 goodGroupOut << seqName << '\t' << group << endl;
573 m->gobble(inputGroups);
576 goodGroupOut.close();
578 //we were unable to remove some of the bad sequences
579 if (badSeqGroups.size() != 0) {
580 for (it = badSeqGroups.begin(); it != badSeqGroups.end(); it++) {
581 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
582 m->mothurOutEndLine();
591 catch(exception& e) {
592 m->errorOut(e, "ScreenSeqsCommand", "screenNameGroupFile");
596 //***************************************************************************************************************
597 int ScreenSeqsCommand::getSummary(vector<unsigned long int>& positions){
600 vector<int> startPosition;
601 vector<int> endPosition;
602 vector<int> seqLength;
603 vector<int> ambigBases;
604 vector<int> longHomoPolymer;
606 vector<unsigned long int> positions = m->divideFile(fastafile, processors);
608 for (int i = 0; i < (positions.size()-1); i++) {
609 lines.push_back(new linePair(positions[i], positions[(i+1)]));
613 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
615 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
617 numSeqs = createProcessesCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile);
620 if (m->control_pressed) { return 0; }
622 numSeqs = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
623 if (m->control_pressed) { return 0; }
626 sort(startPosition.begin(), startPosition.end());
627 sort(endPosition.begin(), endPosition.end());
628 sort(seqLength.begin(), seqLength.end());
629 sort(ambigBases.begin(), ambigBases.end());
630 sort(longHomoPolymer.begin(), longHomoPolymer.end());
632 //numSeqs is the number of unique seqs, startPosition.size() is the total number of seqs, we want to optimize using all seqs
633 int criteriaPercentile = int(startPosition.size() * (criteria / (float) 100));
635 for (int i = 0; i < optimize.size(); i++) {
636 if (optimize[i] == "start") { startPos = startPosition[criteriaPercentile]; m->mothurOut("Optimizing start to " + toString(startPos) + "."); m->mothurOutEndLine(); }
637 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();}
638 else if (optimize[i] == "maxambig") { maxAmbig = ambigBases[criteriaPercentile]; m->mothurOut("Optimizing maxambig to " + toString(maxAmbig) + "."); m->mothurOutEndLine(); }
639 else if (optimize[i] == "maxhomop") { maxHomoP = longHomoPolymer[criteriaPercentile]; m->mothurOut("Optimizing maxhomop to " + toString(maxHomoP) + "."); m->mothurOutEndLine(); }
640 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(); }
641 else if (optimize[i] == "maxlength") { maxLength = seqLength[criteriaPercentile]; m->mothurOut("Optimizing maxlength to " + toString(maxLength) + "."); m->mothurOutEndLine(); }
646 catch(exception& e) {
647 m->errorOut(e, "ScreenSeqsCommand", "getSummary");
651 /**************************************************************************************/
652 int ScreenSeqsCommand::driverCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename, linePair* filePos) {
656 m->openInputFile(filename, in);
658 in.seekg(filePos->start);
665 if (m->control_pressed) { in.close(); return 1; }
667 Sequence current(in); m->gobble(in);
669 if (current.getName() != "") {
671 if (namefile != "") {
672 //make sure this sequence is in the namefile, else error
673 map<string, int>::iterator it = nameMap.find(current.getName());
675 if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
676 else { num = it->second; }
679 //for each sequence this sequence represents
680 for (int i = 0; i < num; i++) {
681 startPosition.push_back(current.getStartPos());
682 endPosition.push_back(current.getEndPos());
683 seqLength.push_back(current.getNumBases());
684 ambigBases.push_back(current.getAmbigBases());
685 longHomoPolymer.push_back(current.getLongHomoPolymer());
691 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
692 unsigned long int pos = in.tellg();
693 if ((pos == -1) || (pos >= filePos->end)) { break; }
695 if (in.eof()) { break; }
704 catch(exception& e) {
705 m->errorOut(e, "ScreenSeqsCommand", "driverCreateSummary");
709 /**************************************************************************************************/
710 int ScreenSeqsCommand::createProcessesCreateSummary(vector<int>& startPosition, vector<int>& endPosition, vector<int>& seqLength, vector<int>& ambigBases, vector<int>& longHomoPolymer, string filename) {
712 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
717 //loop through and create all the processes you want
718 while (process != processors) {
722 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
725 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[process]);
727 //pass numSeqs to parent
729 string tempFile = fastafile + toString(getpid()) + ".num.temp";
730 m->openOutputFile(tempFile, out);
733 out << startPosition.size() << endl;
734 for (int k = 0; k < startPosition.size(); k++) { out << startPosition[k] << '\t'; } out << endl;
735 for (int k = 0; k < endPosition.size(); k++) { out << endPosition[k] << '\t'; } out << endl;
736 for (int k = 0; k < seqLength.size(); k++) { out << seqLength[k] << '\t'; } out << endl;
737 for (int k = 0; k < ambigBases.size(); k++) { out << ambigBases[k] << '\t'; } out << endl;
738 for (int k = 0; k < longHomoPolymer.size(); k++) { out << longHomoPolymer[k] << '\t'; } out << endl;
744 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
745 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
750 num = driverCreateSummary(startPosition, endPosition, seqLength, ambigBases, longHomoPolymer, fastafile, lines[0]);
752 //force parent to wait until all the processes are done
753 for (int i=0;i<processIDS.size();i++) {
754 int temp = processIDS[i];
758 //parent reads in and combine Filter info
759 for (int i = 0; i < processIDS.size(); i++) {
760 string tempFilename = fastafile + toString(processIDS[i]) + ".num.temp";
762 m->openInputFile(tempFilename, in);
765 in >> tempNum; m->gobble(in); num += tempNum;
766 in >> tempNum; m->gobble(in);
767 for (int k = 0; k < tempNum; k++) { in >> temp; startPosition.push_back(temp); } m->gobble(in);
768 for (int k = 0; k < tempNum; k++) { in >> temp; endPosition.push_back(temp); } m->gobble(in);
769 for (int k = 0; k < tempNum; k++) { in >> temp; seqLength.push_back(temp); } m->gobble(in);
770 for (int k = 0; k < tempNum; k++) { in >> temp; ambigBases.push_back(temp); } m->gobble(in);
771 for (int k = 0; k < tempNum; k++) { in >> temp; longHomoPolymer.push_back(temp); } m->gobble(in);
774 remove(tempFilename.c_str());
780 catch(exception& e) {
781 m->errorOut(e, "ScreenSeqsCommand", "createProcessesCreateSummary");
786 //***************************************************************************************************************
788 int ScreenSeqsCommand::screenGroupFile(set<string> badSeqNames){
790 ifstream inputGroups;
791 m->openInputFile(groupfile, inputGroups);
792 string seqName, group;
793 set<string>::iterator it;
795 string goodGroupFile = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "good" + m->getExtension(groupfile);
796 outputNames.push_back(goodGroupFile); outputTypes["group"].push_back(goodGroupFile);
797 ofstream goodGroupOut; m->openOutputFile(goodGroupFile, goodGroupOut);
799 while(!inputGroups.eof()){
800 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
802 inputGroups >> seqName >> group;
803 it = badSeqNames.find(seqName);
805 if(it != badSeqNames.end()){
806 badSeqNames.erase(it);
809 goodGroupOut << seqName << '\t' << group << endl;
811 m->gobble(inputGroups);
814 if (m->control_pressed) { goodGroupOut.close(); inputGroups.close(); remove(goodGroupFile.c_str()); return 0; }
816 //we were unable to remove some of the bad sequences
817 if (badSeqNames.size() != 0) {
818 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
819 m->mothurOut("Your groupfile does not include the sequence " + *it + " please correct.");
820 m->mothurOutEndLine();
825 goodGroupOut.close();
827 if (m->control_pressed) { remove(goodGroupFile.c_str()); }
832 catch(exception& e) {
833 m->errorOut(e, "ScreenSeqsCommand", "screenGroupFile");
838 //***************************************************************************************************************
840 int ScreenSeqsCommand::screenAlignReport(set<string> badSeqNames){
842 ifstream inputAlignReport;
843 m->openInputFile(alignreport, inputAlignReport);
844 string seqName, group;
845 set<string>::iterator it;
847 string goodAlignReportFile = outputDir + m->getRootName(m->getSimpleName(alignreport)) + "good" + m->getExtension(alignreport);
848 outputNames.push_back(goodAlignReportFile); outputTypes["alignreport"].push_back(goodAlignReportFile);
849 ofstream goodAlignReportOut; m->openOutputFile(goodAlignReportFile, goodAlignReportOut);
851 while (!inputAlignReport.eof()) { // need to copy header
852 char c = inputAlignReport.get();
853 goodAlignReportOut << c;
854 if (c == 10 || c == 13){ break; }
857 while(!inputAlignReport.eof()){
858 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
860 inputAlignReport >> seqName;
861 it = badSeqNames.find(seqName);
863 while (!inputAlignReport.eof()) { // need to copy header
864 char c = inputAlignReport.get();
866 if (c == 10 || c == 13){ break; }
869 if(it != badSeqNames.end()){
870 badSeqNames.erase(it);
873 goodAlignReportOut << seqName << '\t' << line;
875 m->gobble(inputAlignReport);
878 if (m->control_pressed) { goodAlignReportOut.close(); inputAlignReport.close(); remove(goodAlignReportFile.c_str()); return 0; }
880 //we were unable to remove some of the bad sequences
881 if (badSeqNames.size() != 0) {
882 for (it = badSeqNames.begin(); it != badSeqNames.end(); it++) {
883 m->mothurOut("Your alignreport file does not include the sequence " + *it + " please correct.");
884 m->mothurOutEndLine();
888 inputAlignReport.close();
889 goodAlignReportOut.close();
891 if (m->control_pressed) { remove(goodAlignReportFile.c_str()); return 0; }
896 catch(exception& e) {
897 m->errorOut(e, "ScreenSeqsCommand", "screenAlignReport");
902 //**********************************************************************************************************************
904 int ScreenSeqsCommand::driver(linePair* filePos, string goodFName, string badAccnosFName, string filename, set<string>& badSeqNames){
907 m->openOutputFile(goodFName, goodFile);
909 ofstream badAccnosFile;
910 m->openOutputFile(badAccnosFName, badAccnosFile);
913 m->openInputFile(filename, inFASTA);
915 inFASTA.seekg(filePos->start);
922 if (m->control_pressed) { return 0; }
924 Sequence currSeq(inFASTA); m->gobble(inFASTA);
925 if (currSeq.getName() != "") {
926 bool goodSeq = 1; // innocent until proven guilty
927 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
928 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
929 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
930 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
931 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
932 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
935 currSeq.printSequence(goodFile);
938 badAccnosFile << currSeq.getName() << endl;
939 badSeqNames.insert(currSeq.getName());
944 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
945 unsigned long int pos = inFASTA.tellg();
946 if ((pos == -1) || (pos >= filePos->end)) { break; }
948 if (inFASTA.eof()) { break; }
952 if((count) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
955 if((count) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(count)); m->mothurOutEndLine(); }
960 badAccnosFile.close();
964 catch(exception& e) {
965 m->errorOut(e, "ScreenSeqsCommand", "driver");
969 //**********************************************************************************************************************
971 int ScreenSeqsCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& goodFile, MPI_File& badAccnosFile, vector<unsigned long int>& MPIPos, set<string>& badSeqNames){
973 string outputString = "";
974 MPI_Status statusGood;
975 MPI_Status statusBadAccnos;
978 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
980 for(int i=0;i<num;i++){
982 if (m->control_pressed) { return 0; }
985 int length = MPIPos[start+i+1] - MPIPos[start+i];
987 char* buf4 = new char[length];
988 memcpy(buf4, outputString.c_str(), length);
990 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
992 string tempBuf = buf4; delete buf4;
993 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
994 istringstream iss (tempBuf,istringstream::in);
996 Sequence currSeq(iss);
999 if (currSeq.getName() != "") {
1000 bool goodSeq = 1; // innocent until proven guilty
1001 if(goodSeq == 1 && startPos != -1 && startPos < currSeq.getStartPos()) { goodSeq = 0; }
1002 if(goodSeq == 1 && endPos != -1 && endPos > currSeq.getEndPos()) { goodSeq = 0; }
1003 if(goodSeq == 1 && maxAmbig != -1 && maxAmbig < currSeq.getAmbigBases()) { goodSeq = 0; }
1004 if(goodSeq == 1 && maxHomoP != -1 && maxHomoP < currSeq.getLongHomoPolymer()) { goodSeq = 0; }
1005 if(goodSeq == 1 && minLength != -1 && minLength > currSeq.getNumBases()) { goodSeq = 0; }
1006 if(goodSeq == 1 && maxLength != -1 && maxLength < currSeq.getNumBases()) { goodSeq = 0; }
1009 outputString = ">" + currSeq.getName() + "\n" + currSeq.getAligned() + "\n";
1012 length = outputString.length();
1013 char* buf2 = new char[length];
1014 memcpy(buf2, outputString.c_str(), length);
1016 MPI_File_write_shared(goodFile, buf2, length, MPI_CHAR, &statusGood);
1021 badSeqNames.insert(currSeq.getName());
1023 //write to bad accnos file
1024 outputString = currSeq.getName() + "\n";
1026 length = outputString.length();
1027 char* buf3 = new char[length];
1028 memcpy(buf3, outputString.c_str(), length);
1030 MPI_File_write_shared(badAccnosFile, buf3, length, MPI_CHAR, &statusBadAccnos);
1036 if((i) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i)); m->mothurOutEndLine(); }
1041 catch(exception& e) {
1042 m->errorOut(e, "ScreenSeqsCommand", "driverMPI");
1047 /**************************************************************************************************/
1049 int ScreenSeqsCommand::createProcesses(string goodFileName, string badAccnos, string filename, set<string>& badSeqNames) {
1051 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1055 //loop through and create all the processes you want
1056 while (process != processors) {
1060 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1062 }else if (pid == 0){
1063 num = driver(lines[process], goodFileName + toString(getpid()) + ".temp", badAccnos + toString(getpid()) + ".temp", filename, badSeqNames);
1065 //pass numSeqs to parent
1067 string tempFile = filename + toString(getpid()) + ".num.temp";
1068 m->openOutputFile(tempFile, out);
1074 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1075 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1080 //force parent to wait until all the processes are done
1081 for (int i=0;i<processors;i++) {
1082 int temp = processIDS[i];
1086 for (int i = 0; i < processIDS.size(); i++) {
1088 string tempFile = filename + toString(processIDS[i]) + ".num.temp";
1089 m->openInputFile(tempFile, in);
1090 if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
1091 in.close(); remove(tempFile.c_str());
1097 catch(exception& e) {
1098 m->errorOut(e, "ScreenSeqsCommand", "createProcesses");
1103 //***************************************************************************************************************