+ out << '>' << seq.getName() << endl << filterSeq << endl;
+ count++;
+ }
+
+ unsigned long int pos = in.tellg();
+ if ((pos == -1) || (pos >= filePos->end)) { break; }
+
+ //report progress
+ if((count) % 100 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+ }
+ //report progress
+ if((count) % 100 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+
+
+ out.close();
+ in.close();
+
+ return count;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "FilterSeqsCommand", "driverRunFilter");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+int FilterSeqsCommand::createProcessesRunFilter(string F, string filename) {
+ try {
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+ int process = 0;
+ int num = 0;
+ processIDS.clear();
+
+ //loop through and create all the processes you want
+ while (process != processors) {
+ int pid = fork();
+
+ if (pid > 0) {
+ processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
+ process++;
+ }else if (pid == 0){
+ string filteredFasta = filename + toString(getpid()) + ".temp";
+ num = driverRunFilter(F, filteredFasta, filename, lines[process]);
+
+ //pass numSeqs to parent
+ ofstream out;
+ string tempFile = filename + toString(getpid()) + ".num.temp";
+ m->openOutputFile(tempFile, out);
+ out << num << endl;
+ out.close();
+
+ exit(0);
+ }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+ }
+
+ //force parent to wait until all the processes are done
+ for (int i=0;i<processors;i++) {
+ int temp = processIDS[i];
+ wait(&temp);
+ }
+
+ for (int i = 0; i < processIDS.size(); i++) {
+ ifstream in;
+ string tempFile = filename + toString(processIDS[i]) + ".num.temp";
+ m->openInputFile(tempFile, in);
+ if (!in.eof()) { int tempNum = 0; in >> tempNum; num += tempNum; }
+ in.close(); remove(tempFile.c_str());
+ }
+
+
+ return num;
+#endif
+ }
+ catch(exception& e) {
+ m->errorOut(e, "FilterSeqsCommand", "createProcessesRunFilter");
+ exit(1);
+ }
+}
+/**************************************************************************************/
+string FilterSeqsCommand::createFilter() {
+ try {
+ string filterString = "";
+ Filters F;
+
+ if (soft != 0) { F.setSoft(soft); }
+ if (trump != '*') { F.setTrump(trump); }
+
+ F.setLength(alignmentLength);
+
+ if(trump != '*' || m->isTrue(vertical) || soft != 0){
+ F.initialize();
+ }
+
+ if(hard.compare("") != 0) { F.doHard(hard); }
+ else { F.setFilter(string(alignmentLength, '1')); }
+
+ numSeqs = 0;
+ if(trump != '*' || m->isTrue(vertical) || soft != 0){
+ for (int s = 0; s < fastafileNames.size(); s++) {
+
+ for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
+
+#ifdef USE_MPI
+ int pid, numSeqsPerProcessor, num;
+ int tag = 2001;
+ vector<unsigned long int> MPIPos;
+
+ MPI_Status status;
+ MPI_File inMPI;
+ MPI_Comm_size(MPI_COMM_WORLD, &processors);
+ MPI_Comm_rank(MPI_COMM_WORLD, &pid);
+
+ //char* tempFileName = new char(fastafileNames[s].length());
+ //tempFileName = &(fastafileNames[s][0]);
+
+ char tempFileName[1024];
+ strcpy(tempFileName, fastafileNames[s].c_str());
+
+ MPI_File_open(MPI_COMM_WORLD, tempFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
+
+ if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
+
+ if (pid == 0) { //you are the root process
+ MPIPos = m->setFilePosFasta(fastafileNames[s], num); //fills MPIPos, returns numSeqs
+ numSeqs += num;
+
+ //send file positions to all processes
+ for(int i = 1; i < processors; i++) {
+ MPI_Send(&num, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
+ MPI_Send(&MPIPos[0], (num+1), MPI_LONG, i, tag, MPI_COMM_WORLD);
+ }
+
+ //figure out how many sequences you have to do
+ numSeqsPerProcessor = num / processors;
+ int startIndex = pid * numSeqsPerProcessor;
+ if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
+
+
+ //do your part
+ MPICreateFilter(startIndex, numSeqsPerProcessor, F, inMPI, MPIPos);
+
+ if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
+
+ }else { //i am the child process
+ MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
+ MPIPos.resize(num+1);
+ numSeqs += num;
+ MPI_Recv(&MPIPos[0], (num+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
+
+ //figure out how many sequences you have to align
+ numSeqsPerProcessor = num / processors;
+ int startIndex = pid * numSeqsPerProcessor;
+ if(pid == (processors - 1)){ numSeqsPerProcessor = num - pid * numSeqsPerProcessor; }
+
+
+ //do your part
+ MPICreateFilter(startIndex, numSeqsPerProcessor, F, inMPI, MPIPos);
+
+ if (m->control_pressed) { MPI_File_close(&inMPI); return 0; }
+ }
+
+ MPI_File_close(&inMPI);
+ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
+
+#else
+ vector<unsigned long int> positions = m->divideFile(fastafileNames[s], processors);
+
+ for (int i = 0; i < (positions.size()-1); i++) {
+ lines.push_back(new linePair(positions[i], positions[(i+1)]));
+ }
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+ if(processors == 1){
+ int numFastaSeqs = driverCreateFilter(F, fastafileNames[s], lines[0]);
+ numSeqs += numFastaSeqs;
+ }else{
+ int numFastaSeqs = createProcessesCreateFilter(F, fastafileNames[s]);
+ numSeqs += numFastaSeqs;