2 * filterseqscommand.cpp
5 * Created by Thomas Ryabin on 5/4/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "filterseqscommand.h"
11 #include "sequence.hpp"
13 /**************************************************************************************/
15 FilterSeqsCommand::FilterSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta", "trump", "soft", "hard", "vertical", "outputdir","inputdir", "processors"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
52 it = parameters.find("hard");
53 //user has given a template file
54 if(it != parameters.end()){
55 path = hasPath(it->second);
56 //if the user has not given a path then, add inputdir. else leave path alone.
57 if (path == "") { parameters["hard"] = inputDir + it->second; }
61 //check for required parameters
62 fasta = validParameter.validFile(parameters, "fasta", false);
63 if (fasta == "not found") { m->mothurOut("fasta is a required parameter for the filter.seqs command."); m->mothurOutEndLine(); abort = true; }
65 splitAtDash(fasta, fastafileNames);
67 //go through files and make sure they are good, if not, then disregard them
68 for (int i = 0; i < fastafileNames.size(); i++) {
70 string path = hasPath(fastafileNames[i]);
71 //if the user has not given a path then, add inputdir. else leave path alone.
72 if (path == "") { fastafileNames[i] = inputDir + fastafileNames[i]; }
77 ableToOpen = openInputFile(fastafileNames[i], in);
78 if (ableToOpen == 1) {
79 m->mothurOut(fastafileNames[i] + " will be disregarded."); m->mothurOutEndLine();
80 //erase from file list
81 fastafileNames.erase(fastafileNames.begin()+i);
84 string simpleName = getSimpleName(fastafileNames[i]);
85 filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
90 //make sure there is at least one valid file left
91 if (fastafileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
95 //if the user changes the output directory command factory will send this info to us in the output parameter
96 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
98 outputDir += hasPath(fastafileNames[0]); //if user entered a file with a path then preserve it
101 //check for optional parameter and set defaults
102 // ...at some point should added some additional type checking...
105 temp = validParameter.validFile(parameters, "trump", false); if (temp == "not found") { temp = "*"; }
108 temp = validParameter.validFile(parameters, "soft", false); if (temp == "not found") { soft = 0; }
109 else { soft = (float)atoi(temp.c_str()) / 100.0; }
111 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
112 convert(temp, processors);
114 hard = validParameter.validFile(parameters, "hard", true); if (hard == "not found") { hard = ""; }
115 else if (hard == "not open") { abort = true; }
117 vertical = validParameter.validFile(parameters, "vertical", false); if (vertical == "not found") { vertical = "T"; }
124 catch(exception& e) {
125 m->errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
130 //**********************************************************************************************************************
132 void FilterSeqsCommand::help(){
135 m->mothurOut("The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n");
136 m->mothurOut("The filter.seqs command parameters are fasta, trump, soft, hard and vertical. \n");
137 m->mothurOut("The fasta parameter is required. You may enter several fasta files to build the filter from and filter, by separating their names with -'s.\n");
138 m->mothurOut("For example: fasta=abrecovery.fasta-amazon.fasta \n");
139 m->mothurOut("The trump parameter .... The default is ...\n");
140 m->mothurOut("The soft parameter .... The default is ....\n");
141 m->mothurOut("The hard parameter .... The default is ....\n");
142 m->mothurOut("The vertical parameter .... The default is T.\n");
143 m->mothurOut("The filter.seqs command should be in the following format: \n");
144 m->mothurOut("filter.seqs(fasta=yourFastaFile, trump=yourTrump, soft=yourSoft, hard=yourHard, vertical=yourVertical) \n");
145 m->mothurOut("Example filter.seqs(fasta=abrecovery.fasta, trump=..., soft=..., hard=..., vertical=T).\n");
146 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
149 catch(exception& e) {
150 m->errorOut(e, "FilterSeqsCommand", "help");
155 /**************************************************************************************/
157 int FilterSeqsCommand::execute() {
160 if (abort == true) { return 0; }
163 openInputFile(fastafileNames[0], inFASTA);
165 Sequence testSeq(inFASTA);
166 alignmentLength = testSeq.getAlignLength();
169 ////////////create filter/////////////////
170 m->mothurOut("Creating Filter... "); m->mothurOutEndLine();
172 filter = createFilter();
174 m->mothurOutEndLine(); m->mothurOutEndLine();
176 if (m->control_pressed) { return 0; }
180 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
182 if (pid == 0) { //only one process should output the filter
187 string filterFile = outputDir + filterFileName + ".filter";
188 openOutputFile(filterFile, outFilter);
189 outFilter << filter << endl;
191 outputNames.push_back(filterFile);
197 ////////////run filter/////////////////
199 m->mothurOut("Running Filter... "); m->mothurOutEndLine();
203 m->mothurOutEndLine(); m->mothurOutEndLine();
205 int filteredLength = 0;
206 for(int i=0;i<alignmentLength;i++){
207 if(filter[i] == '1'){ filteredLength++; }
210 if (m->control_pressed) { for(int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
213 m->mothurOutEndLine();
214 m->mothurOut("Length of filtered alignment: " + toString(filteredLength)); m->mothurOutEndLine();
215 m->mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); m->mothurOutEndLine();
216 m->mothurOut("Length of the original alignment: " + toString(alignmentLength)); m->mothurOutEndLine();
217 m->mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); m->mothurOutEndLine();
220 m->mothurOutEndLine();
221 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
222 for(int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
223 m->mothurOutEndLine();
228 catch(exception& e) {
229 m->errorOut(e, "FilterSeqsCommand", "execute");
233 /**************************************************************************************/
234 int FilterSeqsCommand::filterSequences() {
239 for (int s = 0; s < fastafileNames.size(); s++) {
241 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
243 string filteredFasta = outputDir + getRootName(getSimpleName(fastafileNames[s])) + "filter.fasta";
245 int pid, start, end, numSeqsPerProcessor, num;
250 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
251 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
256 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
257 int inMode=MPI_MODE_RDONLY;
259 //char* outFilename = new char[filteredFasta.length()];
260 //memcpy(outFilename, filteredFasta.c_str(), filteredFasta.length());
262 char outFilename[1024];
263 strcpy(outFilename, filteredFasta.c_str());
265 //char* inFileName = new char[fastafileNames[s].length()];
266 //memcpy(inFileName, fastafileNames[s].c_str(), fastafileNames[s].length());
268 char inFileName[1024];
269 strcpy(inFileName, fastafileNames[s].c_str());
271 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
272 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
275 //delete outFilename;
277 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); return 0; }
279 if (pid == 0) { //you are the root process
281 MPIPos = setFilePosFasta(fastafileNames[s], num); //fills MPIPos, returns numSeqs
284 //send file positions to all processes
285 for(int i = 1; i < processors; i++) {
286 MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
287 MPI_Send(&MPIPos[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
290 //figure out how many sequences you have to do
291 numSeqsPerProcessor = num / processors;
292 int startIndex = pid * numSeqsPerProcessor;
293 if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
297 driverMPIRun(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
299 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); return 0; }
302 for(int i = 1; i < processors; i++) {
304 MPI_Recv(buf, 4, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
307 }else { //you are a child process
308 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
309 MPIPos.resize(num+1);
311 MPI_Recv(&MPIPos[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
313 //figure out how many sequences you have to align
314 numSeqsPerProcessor = num / processors;
315 int startIndex = pid * numSeqsPerProcessor;
316 if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
320 driverMPIRun(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
322 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); return 0; }
327 //tell parent you are done.
328 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
331 MPI_File_close(&outMPI);
332 MPI_File_close(&inMPI);
333 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
336 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
340 openInputFile(fastafileNames[s], inFASTA);
341 getNumSeqs(inFASTA, numFastaSeqs);
344 lines.push_back(new linePair(0, numFastaSeqs));
346 numSeqs += numFastaSeqs;
348 driverRunFilter(filter, filteredFasta, fastafileNames[s], lines[0]);
350 setLines(fastafileNames[s]);
351 createProcessesRunFilter(filter, fastafileNames[s]);
353 rename((fastafileNames[s] + toString(processIDS[0]) + ".temp").c_str(), filteredFasta.c_str());
356 for(int i=1;i<processors;i++){
357 appendFiles((fastafileNames[s] + toString(processIDS[i]) + ".temp"), filteredFasta);
358 remove((fastafileNames[s] + toString(processIDS[i]) + ".temp").c_str());
362 if (m->control_pressed) { return 1; }
366 openInputFile(fastafileNames[s], inFASTA);
367 getNumSeqs(inFASTA, numFastaSeqs);
370 lines.push_back(new linePair(0, numFastaSeqs));
372 numSeqs += numFastaSeqs;
374 driverRunFilter(filter, filteredFasta, fastafileNames[s], lines[0]);
376 if (m->control_pressed) { return 1; }
379 outputNames.push_back(filteredFasta);
384 catch(exception& e) {
385 m->errorOut(e, "FilterSeqsCommand", "filterSequences");
390 /**************************************************************************************/
391 int FilterSeqsCommand::driverMPIRun(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<long>& MPIPos) {
393 string outputString = "";
397 for(int i=0;i<num;i++){
399 if (m->control_pressed) { return 0; }
402 int length = MPIPos[start+i+1] - MPIPos[start+i];
403 char* buf4 = new char[length];
404 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
406 string tempBuf = buf4;
407 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
408 istringstream iss (tempBuf,istringstream::in);
411 Sequence seq(iss); gobble(iss);
413 if (seq.getName() != "") {
414 string align = seq.getAligned();
415 string filterSeq = "";
417 for(int j=0;j<alignmentLength;j++){
418 if(filter[j] == '1'){
419 filterSeq += align[j];
424 outputString += ">" + seq.getName() + "\n" + filterSeq + "\n";
426 if(count % 10 == 0){ //output to file
427 //send results to parent
428 int length = outputString.length();
429 char* buf = new char[length];
430 memcpy(buf, outputString.c_str(), length);
432 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
439 if((i+1) % 100 == 0){ cout << (i+1) << endl; m->mothurOutJustToLog(toString(i+1) + "\n"); }
442 if(outputString != ""){ //output to file
443 //send results to parent
444 int length = outputString.length();
445 char* buf = new char[length];
446 memcpy(buf, outputString.c_str(), length);
448 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
453 if((num) % 100 != 0){ cout << (num) << endl; m->mothurOutJustToLog(toString(num) + "\n"); }
457 catch(exception& e) {
458 m->errorOut(e, "FilterSeqsCommand", "driverRunFilter");
463 /**************************************************************************************/
464 int FilterSeqsCommand::driverRunFilter(string F, string outputFilename, string inputFilename, linePair* line) {
467 openOutputFile(outputFilename, out);
470 openInputFile(inputFilename, in);
472 in.seekg(line->start);
474 for(int i=0;i<line->num;i++){
476 if (m->control_pressed) { in.close(); out.close(); return 0; }
479 if (seq.getName() != "") {
480 string align = seq.getAligned();
481 string filterSeq = "";
483 for(int j=0;j<alignmentLength;j++){
484 if(filter[j] == '1'){
485 filterSeq += align[j];
489 out << '>' << seq.getName() << endl << filterSeq << endl;
498 catch(exception& e) {
499 m->errorOut(e, "FilterSeqsCommand", "driverRunFilter");
503 /**************************************************************************************************/
505 int FilterSeqsCommand::createProcessesRunFilter(string F, string filename) {
507 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
512 //loop through and create all the processes you want
513 while (process != processors) {
517 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
520 string filteredFasta = filename + toString(getpid()) + ".temp";
521 driverRunFilter(F, filteredFasta, filename, lines[process]);
523 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
526 //force parent to wait until all the processes are done
527 for (int i=0;i<processors;i++) {
528 int temp = processIDS[i];
535 catch(exception& e) {
536 m->errorOut(e, "FilterSeqsCommand", "createProcessesRunFilter");
540 /**************************************************************************************/
541 string FilterSeqsCommand::createFilter() {
543 string filterString = "";
546 if (soft != 0) { F.setSoft(soft); }
547 if (trump != '*') { F.setTrump(trump); }
549 F.setLength(alignmentLength);
551 if(trump != '*' || isTrue(vertical) || soft != 0){
555 if(hard.compare("") != 0) { F.doHard(hard); }
556 else { F.setFilter(string(alignmentLength, '1')); }
559 if(trump != '*' || isTrue(vertical) || soft != 0){
560 for (int s = 0; s < fastafileNames.size(); s++) {
562 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
565 int pid, numSeqsPerProcessor, num;
571 MPI_Comm_size(MPI_COMM_WORLD, &processors);
572 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
574 //char* tempFileName = new char(fastafileNames[s].length());
575 //tempFileName = &(fastafileNames[s][0]);
577 char tempFileName[1024];
578 strcpy(tempFileName, fastafileNames[s].c_str());
580 MPI_File_open(MPI_COMM_WORLD, tempFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
582 if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
584 if (pid == 0) { //you are the root process
585 MPIPos = setFilePosFasta(fastafileNames[s], num); //fills MPIPos, returns numSeqs
588 //send file positions to all processes
589 for(int i = 1; i < processors; i++) {
590 MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
591 MPI_Send(&MPIPos[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
594 //figure out how many sequences you have to do
595 numSeqsPerProcessor = num / processors;
596 int startIndex = pid * numSeqsPerProcessor;
597 if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
601 MPICreateFilter(startIndex, numSeqsPerProcessor, F, inMPI, MPIPos);
603 if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
605 }else { //i am the child process
606 MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
607 MPIPos.resize(num+1);
609 MPI_Recv(&MPIPos[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
611 //figure out how many sequences you have to align
612 numSeqsPerProcessor = num / processors;
613 int startIndex = pid * numSeqsPerProcessor;
614 if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
618 MPICreateFilter(startIndex, numSeqsPerProcessor, F, inMPI, MPIPos);
620 if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
623 MPI_File_close(&inMPI);
624 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
627 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
631 openInputFile(fastafileNames[s], inFASTA);
632 getNumSeqs(inFASTA, numFastaSeqs);
635 numSeqs += numFastaSeqs;
637 lines.push_back(new linePair(0, numFastaSeqs));
639 driverCreateFilter(F, fastafileNames[s], lines[0]);
641 setLines(fastafileNames[s]);
642 createProcessesCreateFilter(F, fastafileNames[s]);
645 if (m->control_pressed) { return filterString; }
649 openInputFile(fastafileNames[s], inFASTA);
650 getNumSeqs(inFASTA, numFastaSeqs);
653 numSeqs += numFastaSeqs;
655 lines.push_back(new linePair(0, numFastaSeqs));
657 driverCreateFilter(F, fastafileNames[s], lines[0]);
658 if (m->control_pressed) { return filterString; }
668 int Atag = 1; int Ttag = 2; int Ctag = 3; int Gtag = 4; int Gaptag = 5;
671 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
673 if(trump != '*' || isTrue(vertical) || soft != 0){
675 if (pid == 0) { //only one process should output the filter
677 vector<int> temp; temp.resize(alignmentLength+1);
679 //get the frequencies from the child processes
680 for(int i = 1; i < processors; i++) {
682 for (int j = 0; j < 5; j++) {
684 MPI_Recv(&temp[0], (alignmentLength+1), MPI_INT, i, 2001, MPI_COMM_WORLD, &status);
685 int receiveTag = temp[temp.size()-1]; //child process added a int to the end to indicate what letter count this is for
687 if (receiveTag == Atag) { //you are recieveing the A frequencies
688 for (int k = 0; k < alignmentLength; k++) { F.a[k] += temp[k]; }
689 }else if (receiveTag == Ttag) { //you are recieveing the T frequencies
690 for (int k = 0; k < alignmentLength; k++) { F.t[k] += temp[k]; }
691 }else if (receiveTag == Ctag) { //you are recieveing the C frequencies
692 for (int k = 0; k < alignmentLength; k++) { F.c[k] += temp[k]; }
693 }else if (receiveTag == Gtag) { //you are recieveing the G frequencies
694 for (int k = 0; k < alignmentLength; k++) { F.g[k] += temp[k]; }
695 }else if (receiveTag == Gaptag) { //you are recieveing the gap frequencies
696 for (int k = 0; k < alignmentLength; k++) { F.gap[k] += temp[k]; }
702 //send my fequency counts
704 int ierr = MPI_Send(&(F.a[0]), (alignmentLength+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
706 ierr = MPI_Send (&(F.t[0]), (alignmentLength+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
708 ierr = MPI_Send(&(F.c[0]), (alignmentLength+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
710 ierr = MPI_Send(&(F.g[0]), (alignmentLength+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
711 F.gap.push_back(Gaptag);
712 ierr = MPI_Send(&(F.gap[0]), (alignmentLength+1), MPI_INT, 0, 2001, MPI_COMM_WORLD);
717 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
719 if (pid == 0) { //only one process should output the filter
721 F.setNumSeqs(numSeqs);
723 if(isTrue(vertical) == 1) { F.doVertical(); }
724 if(soft != 0) { F.doSoft(); }
726 filterString = F.getFilter();
729 //send filter string to kids
730 //for(int i = 1; i < processors; i++) {
731 // MPI_Send(&filterString[0], alignmentLength, MPI_CHAR, i, 2001, MPI_COMM_WORLD);
733 MPI_Bcast(&filterString[0], alignmentLength, MPI_CHAR, 0, MPI_COMM_WORLD);
735 //recieve filterString
736 char* tempBuf = new char[alignmentLength];
737 //MPI_Recv(&tempBuf[0], alignmentLength, MPI_CHAR, 0, 2001, MPI_COMM_WORLD, &status);
738 MPI_Bcast(tempBuf, alignmentLength, MPI_CHAR, 0, MPI_COMM_WORLD);
740 filterString = tempBuf;
741 if (filterString.length() > alignmentLength) { filterString = filterString.substr(0, alignmentLength); }
745 MPI_Barrier(MPI_COMM_WORLD);
750 catch(exception& e) {
751 m->errorOut(e, "FilterSeqsCommand", "createFilter");
755 /**************************************************************************************/
756 int FilterSeqsCommand::driverCreateFilter(Filters& F, string filename, linePair* line) {
760 openInputFile(filename, in);
762 in.seekg(line->start);
764 for(int i=0;i<line->num;i++){
766 if (m->control_pressed) { in.close(); return 1; }
769 if (seq.getName() != "") {
770 if (seq.getAligned().length() != alignmentLength) { m->mothurOut("Sequences are not all the same length, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
772 if(trump != '*'){ F.doTrump(seq); }
773 if(isTrue(vertical) || soft != 0){ F.getFreqs(seq); }
778 if((i+1) % 100 == 0){ m->mothurOut(toString(i+1)); m->mothurOutEndLine(); }
782 if((line->num) % 100 != 0){ m->mothurOut(toString(line->num)); m->mothurOutEndLine(); }
788 catch(exception& e) {
789 m->errorOut(e, "FilterSeqsCommand", "driverCreateFilter");
794 /**************************************************************************************/
795 int FilterSeqsCommand::MPICreateFilter(int start, int num, Filters& F, MPI_File& inMPI, vector<long>& MPIPos) {
800 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
802 for(int i=0;i<num;i++){
804 if (m->control_pressed) { return 0; }
807 int length = MPIPos[start+i+1] - MPIPos[start+i];
809 char* buf4 = new char[length];
810 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
812 string tempBuf = buf4;
813 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
814 istringstream iss (tempBuf,istringstream::in);
819 if (seq.getAligned().length() != alignmentLength) { cout << "Alignment length is " << alignmentLength << " and sequence " << seq.getName() << " has length " << seq.getAligned().length() << ", please correct." << endl; exit(1); }
821 if(trump != '*'){ F.doTrump(seq); }
822 if(isTrue(vertical) || soft != 0){ F.getFreqs(seq); }
826 if((i+1) % 100 == 0){ cout << (i+1) << endl; m->mothurOutJustToLog(toString(i+1) + "\n"); }
830 if((num) % 100 != 0){ cout << num << endl; m->mothurOutJustToLog(toString(num) + "\n"); }
834 catch(exception& e) {
835 m->errorOut(e, "FilterSeqsCommand", "MPICreateFilter");
840 /**************************************************************************************************/
842 int FilterSeqsCommand::createProcessesCreateFilter(Filters& F, string filename) {
844 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
849 //loop through and create all the processes you want
850 while (process != processors) {
854 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
857 driverCreateFilter(F, filename, lines[process]);
859 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
862 //force parent to wait until all the processes are done
863 for (int i=0;i<processors;i++) {
864 int temp = processIDS[i];
871 catch(exception& e) {
872 m->errorOut(e, "FilterSeqsCommand", "createProcessesCreateFilter");
876 /**************************************************************************************************/
878 int FilterSeqsCommand::setLines(string filename) {
881 vector<long int> positions;
885 openInputFile(filename, inFASTA);
888 while(!inFASTA.eof()){
889 input = getline(inFASTA);
891 if (input.length() != 0) {
892 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
897 int numFastaSeqs = positions.size();
902 //get num bytes in file
903 pFile = fopen (filename.c_str(),"rb");
904 if (pFile==NULL) perror ("Error opening file");
906 fseek (pFile, 0, SEEK_END);
911 numSeqs += numFastaSeqs;
913 int numSeqsPerProcessor = numFastaSeqs / processors;
915 for (int i = 0; i < processors; i++) {
917 long int startPos = positions[ i * numSeqsPerProcessor ];
918 if(i == processors - 1){
919 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
920 bufferSizes.push_back(size - startPos);
922 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
923 bufferSizes.push_back(myEnd-startPos);
925 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
930 catch(exception& e) {
931 m->errorOut(e, "FilterSeqsCommand", "setLines");
935 /**************************************************************************************/