+/**************************************************************************************************/
+bool ChopSeqsCommand::createProcesses(vector<linePair> lines, string filename, string outFasta, string outAccnos) {
+ try {
+ int process = 1;
+ bool wroteAccnos = false;
+ vector<int> processIDS;
+ vector<string> nonBlankAccnosFiles;
+
+#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){
+ wroteAccnos = driver(lines[process], filename, outFasta + toString(getpid()) + ".temp", outAccnos + toString(getpid()) + ".temp");
+
+ //pass numSeqs to parent
+ ofstream out;
+ string tempFile = fastafile + toString(getpid()) + ".bool.temp";
+ m->openOutputFile(tempFile, out);
+ out << wroteAccnos << 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);
+ }
+ }
+
+ //do your part
+ wroteAccnos = driver(lines[0], filename, outFasta, outAccnos);
+
+ //force parent to wait until all the processes are done
+ for (int i=0;i<processIDS.size();i++) {
+ int temp = processIDS[i];
+ wait(&temp);
+ }
+
+
+ if (wroteAccnos) { nonBlankAccnosFiles.push_back(outAccnos); }
+ else { m->mothurRemove(outAccnos); } //remove so other files can be renamed to it
+
+ //parent reads in and combine Filter info
+ for (int i = 0; i < processIDS.size(); i++) {
+ string tempFilename = fastafile + toString(processIDS[i]) + ".bool.temp";
+ ifstream in;
+ m->openInputFile(tempFilename, in);
+
+ bool temp;
+ in >> temp; m->gobble(in);
+ if (temp) { wroteAccnos = temp; nonBlankAccnosFiles.push_back(outAccnos + toString(processIDS[i]) + ".temp"); }
+ else { m->mothurRemove((outAccnos + toString(processIDS[i]) + ".temp")); }
+
+ in.close();
+ m->mothurRemove(tempFilename);
+ }
+#else
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ //Windows version shared memory, so be careful when passing variables through the seqSumData 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 vectors.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ vector<chopData*> 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"; processIDS.push_back(i); }
+ // Allocate memory for thread data.
+ chopData* tempChop = new chopData(filename, (outFasta+extension), (outAccnos+extension), m, lines[i].start, lines[i].end, keep, countGaps, numbases, Short);
+ pDataArray.push_back(tempChop);
+
+ //MyChopThreadFunction is in header. It must be global or static to work with the threads.
+ //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
+ hThreadArray[i] = CreateThread(NULL, 0, MyChopThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
+ }
+
+ //do your part
+ wroteAccnos = driver(lines[processors-1], filename, (outFasta + toString(processors-1) + ".temp"), (outAccnos + toString(processors-1) + ".temp"));
+ processIDS.push_back(processors-1);
+
+ //Wait until all threads have terminated.
+ WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
+
+ if (wroteAccnos) { nonBlankAccnosFiles.push_back(outAccnos); }
+ else { m->mothurRemove(outAccnos); } //remove so other files can be renamed to it
+
+ //Close all thread handles and free memory allocations.
+ for(int i=0; i < pDataArray.size(); i++){
+ if (pDataArray[i]->wroteAccnos) { wroteAccnos = pDataArray[i]->wroteAccnos; nonBlankAccnosFiles.push_back(outAccnos + toString(processIDS[i]) + ".temp"); }
+ else { m->mothurRemove((outAccnos + toString(processIDS[i]) + ".temp")); }
+ //check to make sure the process finished
+ if (pDataArray[i]->count != pDataArray[i]->end) {
+ m->mothurOut("[ERROR]: process " + toString(i) + " only processed " + toString(pDataArray[i]->count) + " of " + toString(pDataArray[i]->end) + " sequences assigned to it, quitting. \n"); m->control_pressed = true;
+ }
+ CloseHandle(hThreadArray[i]);
+ delete pDataArray[i];
+ }
+#endif
+
+ for (int i = 0; i < processIDS.size(); i++) {
+ m->appendFiles((outFasta + toString(processIDS[i]) + ".temp"), outFasta);
+ m->mothurRemove((outFasta + toString(processIDS[i]) + ".temp"));
+ }
+
+ if (nonBlankAccnosFiles.size() != 0) {
+ m->renameFile(nonBlankAccnosFiles[0], outAccnos);
+
+ for (int h=1; h < nonBlankAccnosFiles.size(); h++) {
+ m->appendFiles(nonBlankAccnosFiles[h], outAccnos);
+ m->mothurRemove(nonBlankAccnosFiles[h]);
+ }
+ }else { //recreate the accnosfile if needed
+ ofstream out;
+ m->openOutputFile(outAccnos, out);
+ out.close();
+ }
+
+ return wroteAccnos;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ChopSeqsCommand", "createProcesses");
+ exit(1);
+ }
+}
+/**************************************************************************************/
+bool ChopSeqsCommand::driver(linePair filePos, string filename, string outFasta, string outAccnos) {
+ try {
+
+ ofstream out;
+ m->openOutputFile(outFasta, out);
+
+ ofstream outAcc;
+ m->openOutputFile(outAccnos, outAcc);
+
+ ifstream in;
+ m->openInputFile(filename, in);
+
+ in.seekg(filePos.start);
+
+ bool done = false;
+ bool wroteAccnos = false;
+ int count = 0;
+
+ while (!done) {
+
+ if (m->control_pressed) { in.close(); out.close(); return 1; }
+
+ Sequence seq(in); m->gobble(in);
+
+ if (m->control_pressed) { in.close(); out.close(); outAcc.close(); m->mothurRemove(outFasta); m->mothurRemove(outAccnos); return 0; }
+
+ if (seq.getName() != "") {
+ string newSeqString = getChopped(seq);
+
+ //output trimmed sequence
+ if (newSeqString != "") {
+ out << ">" << seq.getName() << endl << newSeqString << endl;
+ }else{
+ outAcc << seq.getName() << endl;
+ wroteAccnos = true;
+ }
+ 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) % 1000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+
+ }
+ //report progress
+ if((count) % 1000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+
+
+ in.close();
+ out.close();
+ outAcc.close();
+
+ return wroteAccnos;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ChopSeqsCommand", "driver");
+ exit(1);
+ }
+}