+ out << '>' << seq.getName() << endl << filterSeq << endl;
+ count++;
+ }
+
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ unsigned long long pos = in.tellg();
+ if ((pos == -1) || (pos >= filePos->end)) { break; }
+ #else
+ if (in.eof()) { break; }
+ #endif
+
+ //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, string filteredFastaName) {
+ try {
+
+ int process = 1;
+ int num = 0;
+ processIDS.clear();
+
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+
+
+ //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("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
+ for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+ exit(0);
+ }
+ }
+
+ num = driverRunFilter(F, filteredFastaName, filename, lines[0]);
+
+ //force parent to wait until all the processes are done
+ for (int i=0;i<processIDS.size();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(); m->mothurRemove(tempFile);
+
+ m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filteredFastaName);
+ m->mothurRemove((filename + toString(processIDS[i]) + ".temp"));
+ }
+
+#else
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ //Windows version shared memory, so be careful when passing variables through the filterData struct.
+ //Above fork() will clone, so memory is separate, but that's not the case with windows,
+ //Taking advantage of shared memory to allow both threads to add info to F.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ vector<filterRunData*> pDataArray;
+ DWORD dwThreadIdArray[processors-1];
+ HANDLE hThreadArray[processors-1];
+
+ //Create processor worker threads.
+ for( int i=0; i<processors-1; i++){
+
+ string extension = "";
+ if (i != 0) { extension = toString(i) + ".temp"; }
+
+ filterRunData* tempFilter = new filterRunData(filter, filename, (filteredFastaName + extension), m, lines[i]->start, lines[i]->end, alignmentLength, i);
+ pDataArray.push_back(tempFilter);
+ processIDS.push_back(i);
+
+ hThreadArray[i] = CreateThread(NULL, 0, MyRunFilterThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
+ }
+
+ num = driverRunFilter(F, (filteredFastaName + toString(processors-1) + ".temp"), filename, lines[processors-1]);
+
+ //Wait until all threads have terminated.
+ WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
+
+ //Close all thread handles and free memory allocations.
+ for(int i=0; i < pDataArray.size(); i++){
+ num += pDataArray[i]->count;
+ CloseHandle(hThreadArray[i]);
+ delete pDataArray[i];
+ }
+
+ for (int i = 1; i < processors; i++) {
+ m->appendFiles((filteredFastaName + toString(i) + ".temp"), filteredFastaName);
+ m->mothurRemove((filteredFastaName + toString(i) + ".temp"));
+ }
+#endif
+
+ return num;
+
+ }
+ 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 long> 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 long> positions;
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ 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(processors == 1){
+ int numFastaSeqs = driverCreateFilter(F, fastafileNames[s], lines[0]);
+ numSeqs += numFastaSeqs;
+ }else{
+ int numFastaSeqs = createProcessesCreateFilter(F, fastafileNames[s]);
+ numSeqs += numFastaSeqs;