5 * Created by Pat Schloss on 12/22/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "trimflowscommand.h"
11 #include "needlemanoverlap.hpp"
14 //**********************************************************************************************************************
15 vector<string> TrimFlowsCommand::setParameters(){
17 CommandParameter pflow("flow", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pflow);
18 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(poligos);
19 CommandParameter pmaxhomop("maxhomop", "Number", "", "9", "", "", "",false,false); parameters.push_back(pmaxhomop);
20 CommandParameter pmaxflows("maxflows", "Number", "", "720", "", "", "",false,false); parameters.push_back(pmaxflows);
21 CommandParameter pminflows("minflows", "Number", "", "360", "", "", "",false,false); parameters.push_back(pminflows);
22 CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
23 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
24 CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
25 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
26 CommandParameter psignal("signal", "Number", "", "0.50", "", "", "",false,false); parameters.push_back(psignal);
27 CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "",false,false); parameters.push_back(pnoise);
28 CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "",false,false); parameters.push_back(pallfiles);
29 CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
30 CommandParameter pfasta("fasta", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pfasta);
31 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
32 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
34 vector<string> myArray;
35 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
39 m->errorOut(e, "TrimFlowsCommand", "setParameters");
43 //**********************************************************************************************************************
44 string TrimFlowsCommand::getHelpString(){
46 string helpString = "";
47 helpString += "The trim.flows command reads a flowgram file and creates .....\n";
48 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
49 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n";
53 m->errorOut(e, "TrimFlowsCommand", "getHelpString");
58 //**********************************************************************************************************************
60 TrimFlowsCommand::TrimFlowsCommand(){
62 abort = true; calledHelp = true;
64 vector<string> tempOutNames;
65 outputTypes["flow"] = tempOutNames;
66 outputTypes["fasta"] = tempOutNames;
69 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
73 //**********************************************************************************************************************
75 TrimFlowsCommand::TrimFlowsCommand(string option) {
78 abort = false; calledHelp = false;
81 //allow user to run help
82 if(option == "help") { help(); abort = true; calledHelp = true; }
83 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string,string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["flow"] = tempOutNames;
103 outputTypes["fasta"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("flow");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["flow"] = inputDir + it->second; }
118 it = parameters.find("oligos");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["oligos"] = inputDir + it->second; }
129 //check for required parameters
130 flowFileName = validParameter.validFile(parameters, "flow", true);
131 if (flowFileName == "not found") {
132 flowFileName = m->getFlowFile();
133 if (flowFileName != "") { m->mothurOut("Using " + flowFileName + " as input file for the flow parameter."); m->mothurOutEndLine(); }
135 m->mothurOut("No valid current flow file. You must provide a flow file."); m->mothurOutEndLine();
138 }else if (flowFileName == "not open") { flowFileName = ""; abort = true; }
140 //if the user changes the output directory command factory will send this info to us in the output parameter
141 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
143 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
147 //check for optional parameter and set defaults
148 // ...at some point should added some additional type checking...
151 temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "450"; }
152 convert(temp, minFlows);
154 temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "450"; }
155 convert(temp, maxFlows);
158 temp = validParameter.validFile(parameters, "oligos", true);
159 if (temp == "not found") { oligoFileName = ""; }
160 else if(temp == "not open") { abort = true; }
161 else { oligoFileName = temp; m->setOligosFile(oligoFileName); }
163 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
164 else if(m->isTrue(temp)) { fasta = 1; }
166 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
167 convert(temp, maxHomoP);
169 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
170 convert(temp, signal);
172 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
173 convert(temp, noise);
175 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
176 convert(temp, bdiffs);
178 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
179 convert(temp, pdiffs);
181 temp = validParameter.validFile(parameters, "tdiffs", false);
182 if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
183 convert(temp, tdiffs);
184 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
186 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
187 m->setProcessors(temp);
188 convert(temp, processors);
190 flowOrder = validParameter.validFile(parameters, "order", false);
191 if (flowOrder == "not found"){ flowOrder = "TACG"; }
192 else if(flowOrder.length() != 4){
193 m->mothurOut("The value of the order option must be four bases long\n");
196 if(oligoFileName == "") { allFiles = 0; }
197 else { allFiles = 1; }
204 catch(exception& e) {
205 m->errorOut(e, "TrimFlowsCommand", "TrimSeqsCommand");
210 //***************************************************************************************************************
212 int TrimFlowsCommand::execute(){
215 if (abort == true) { if (calledHelp) { return 0; } return 2; }
217 string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
218 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
220 string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
221 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
223 string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
225 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
228 vector<unsigned long long> flowFilePos;
229 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
230 flowFilePos = getFlowFileBreaks();
231 for (int i = 0; i < (flowFilePos.size()-1); i++) {
232 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
235 ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
236 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
238 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
239 if (processors == 1) {
240 lines.push_back(new linePair(0, 1000));
243 flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
244 flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
246 //figure out how many sequences you have to process
247 int numSeqsPerProcessor = numFlowLines / processors;
248 cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
249 for (int i = 0; i < processors; i++) {
250 int startIndex = i * numSeqsPerProcessor;
251 if(i == (processors - 1)){ numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor; }
252 lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
253 cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
258 vector<vector<string> > barcodePrimerComboFileNames;
259 if(oligoFileName != ""){
260 getOligos(barcodePrimerComboFileNames);
264 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
266 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
269 if (m->control_pressed) { return 0; }
271 string flowFilesFileName;
276 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
277 m->openOutputFile(flowFilesFileName, output);
279 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
280 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
283 unsigned long long size;
285 //get num bytes in file
286 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
287 if (pFile==NULL) perror ("Error opening file");
289 fseek (pFile, 0, SEEK_END);
295 m->mothurRemove(barcodePrimerComboFileNames[i][j]);
298 output << barcodePrimerComboFileNames[i][j] << endl;
299 outputNames.push_back(barcodePrimerComboFileNames[i][j]);
300 outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
307 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
308 m->openOutputFile(flowFilesFileName, output);
310 output << trimFlowFileName << endl;
314 outputTypes["flow.files"].push_back(flowFilesFileName);
315 outputNames.push_back(flowFilesFileName);
317 // set fasta file as new current fastafile
318 // string current = "";
319 // itTypes = outputTypes.find("fasta");
320 // if (itTypes != outputTypes.end()) {
321 // if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
324 m->mothurOutEndLine();
325 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
326 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
327 m->mothurOutEndLine();
331 catch(exception& e) {
332 m->errorOut(e, "TrimSeqsCommand", "execute");
337 //***************************************************************************************************************
339 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
342 ofstream trimFlowFile;
343 m->openOutputFile(trimFlowFileName, trimFlowFile);
344 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
346 ofstream scrapFlowFile;
347 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
348 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
351 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
354 m->openInputFile(flowFileName, flowFile);
356 flowFile.seekg(line->start);
358 if(line->start == 0){
359 flowFile >> numFlows; m->gobble(flowFile);
360 scrapFlowFile << maxFlows << endl;
361 trimFlowFile << maxFlows << endl;
363 for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
364 for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
366 m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
367 temp << maxFlows << endl;
374 FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
375 //cout << " driver flowdata address " << &flowData << &flowFile << endl;
379 TrimOligos trimOligos(pdiffs, bdiffs, primers, barcodes, revPrimer);
382 //cout << "driver " << count << endl;
385 if (m->control_pressed) { break; }
388 int currentSeqDiffs = 0;
389 string trashCode = "";
391 flowData.getNext(flowFile);
392 //cout << "driver good bit " << flowFile.good() << endl;
393 flowData.capFlows(maxFlows);
395 Sequence currSeq = flowData.getSequence();
397 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
403 int barcodeIndex = 0;
405 if(barcodes.size() != 0){
406 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
407 if(success > bdiffs) { trashCode += 'b'; }
408 else{ currentSeqDiffs += success; }
411 if(numFPrimers != 0){
412 success = trimOligos.stripForward(currSeq, primerIndex);
413 if(success > pdiffs) { trashCode += 'f'; }
414 else{ currentSeqDiffs += success; }
417 if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
419 if(numRPrimers != 0){
420 success = trimOligos.stripReverse(currSeq);
421 if(!success) { trashCode += 'r'; }
424 if(trashCode.length() == 0){
426 flowData.printFlows(trimFlowFile);
428 if(fasta) { currSeq.printSequence(fastaFile); }
432 m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
433 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
435 flowData.printFlows(output);
440 flowData.printFlows(scrapFlowFile, trashCode);
444 //cout << "driver" << '\t' << currSeq.getName() << endl;
446 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
448 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
449 unsigned long long pos = flowFile.tellg();
451 if ((pos == -1) || (pos >= line->end)) { break; }
453 if (flowFile.eof()) { break; }
458 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
460 trimFlowFile.close();
461 scrapFlowFile.close();
463 if(fasta){ fastaFile.close(); }
467 catch(exception& e) {
468 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
473 //***************************************************************************************************************
475 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
478 m->openInputFile(oligoFileName, oligosFile);
480 string type, oligo, group;
483 int indexBarcode = 0;
485 while(!oligosFile.eof()){
487 oligosFile >> type; m->gobble(oligosFile); //get the first column value of the row - is it a comment or a feature we are interested in?
489 if(type[0] == '#'){ //igore the line because there's a comment
490 while (!oligosFile.eof()) { char c = oligosFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
492 else{ //there's a feature we're interested in
494 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } //make type case insensitive
496 oligosFile >> oligo; //get the DNA sequence for the feature
498 for(int i=0;i<oligo.length();i++){ //make type case insensitive and change any U's to T's
499 oligo[i] = toupper(oligo[i]);
500 if(oligo[i] == 'U') { oligo[i] = 'T'; }
503 if(type == "FORWARD"){ //if the feature is a forward primer...
506 while (!oligosFile.eof()) { // get rest of line in case there is a primer name = will have the name of the primer
507 char c = oligosFile.get();
508 if (c == 10 || c == 13){ break; }
509 else if (c == 32 || c == 9){;} //space or tab
513 //have we seen this primer already?
514 map<string, int>::iterator itPrimer = primers.find(oligo);
515 if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
517 primers[oligo]=indexPrimer; indexPrimer++;
518 primerNameVector.push_back(group);
521 else if(type == "REVERSE"){
522 Sequence oligoRC("reverse", oligo);
523 oligoRC.reverseComplement();
524 revPrimer.push_back(oligoRC.getUnaligned());
526 else if(type == "BARCODE"){
529 //check for repeat barcodes
530 map<string, int>::iterator itBar = barcodes.find(oligo);
531 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
533 barcodes[oligo]=indexBarcode; indexBarcode++;
534 barcodeNameVector.push_back(group);
537 m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
541 m->gobble(oligosFile);
545 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
547 //add in potential combos
548 if(barcodeNameVector.size() == 0){
550 barcodeNameVector.push_back("");
553 if(primerNameVector.size() == 0){
555 primerNameVector.push_back("");
559 outFlowFileNames.resize(barcodeNameVector.size());
560 for(int i=0;i<outFlowFileNames.size();i++){
561 outFlowFileNames[i].assign(primerNameVector.size(), "");
566 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
567 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
569 string primerName = primerNameVector[itPrimer->second];
570 string barcodeName = barcodeNameVector[itBar->second];
572 string comboGroupName = "";
573 string fileName = "";
575 if(primerName == ""){
576 comboGroupName = barcodeNameVector[itBar->second];
577 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
580 if(barcodeName == ""){
581 comboGroupName = primerNameVector[itPrimer->second];
584 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
586 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
589 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
592 m->openOutputFile(fileName, temp);
598 numFPrimers = primers.size();
599 numRPrimers = revPrimer.size();
602 catch(exception& e) {
603 m->errorOut(e, "TrimSeqsCommand", "getOligos");
607 /**************************************************************************************************/
608 vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
612 vector<unsigned long long> filePos;
613 filePos.push_back(0);
616 unsigned long long size;
618 //get num bytes in file
619 pFile = fopen (flowFileName.c_str(),"rb");
620 if (pFile==NULL) perror ("Error opening file");
622 fseek (pFile, 0, SEEK_END);
627 //estimate file breaks
628 unsigned long long chunkSize = 0;
629 chunkSize = size / processors;
631 //file too small to divide by processors
632 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
634 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
635 for (int i = 0; i < processors; i++) {
636 unsigned long long spot = (i+1) * chunkSize;
639 m->openInputFile(flowFileName, in);
642 string dummy = m->getline(in);
644 //there was not another sequence before the end of the file
645 unsigned long long sanityPos = in.tellg();
647 // if (sanityPos == -1) { break; }
648 // else { filePos.push_back(newSpot); }
649 if (sanityPos == -1) { break; }
650 else { filePos.push_back(sanityPos); }
656 filePos.push_back(size);
658 //sanity check filePos
659 for (int i = 0; i < (filePos.size()-1); i++) {
660 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
664 m->openInputFile(flowFileName, in);
667 //unsigned long long spot = in.tellg();
671 processors = (filePos.size() - 1);
675 catch(exception& e) {
676 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
681 /**************************************************************************************************/
683 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
689 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
692 //loop through and create all the processes you want
693 while (process != processors) {
697 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
701 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
703 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
704 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
705 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
707 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
713 driverCreateTrim(flowFileName,
714 (trimFlowFileName + toString(getpid()) + ".temp"),
715 (scrapFlowFileName + toString(getpid()) + ".temp"),
716 (fastaFileName + toString(getpid()) + ".temp"),
717 tempBarcodePrimerComboFileNames, lines[process]);
721 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
722 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
729 m->openOutputFile(trimFlowFileName, temp);
732 m->openOutputFile(scrapFlowFileName, temp);
736 m->openOutputFile(fastaFileName, temp);
740 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
742 //force parent to wait until all the processes are done
743 for (int i=0;i<processIDS.size();i++) {
744 int temp = processIDS[i];
748 //////////////////////////////////////////////////////////////////////////////////////////////////////
749 //Windows version shared memory, so be careful when passing variables through the trimFlowData struct.
750 //Above fork() will clone, so memory is separate, but that's not the case with windows,
751 //////////////////////////////////////////////////////////////////////////////////////////////////////
753 vector<trimFlowData*> pDataArray;
754 DWORD dwThreadIdArray[processors-1];
755 HANDLE hThreadArray[processors-1];
757 //Create processor worker threads.
758 for( int i=0; i<processors-1; i++ ){
759 // Allocate memory for thread data.
760 string extension = "";
761 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
763 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
765 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
766 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
767 tempBarcodePrimerComboFileNames[i][j] += extension;
769 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
776 trimFlowData* tempflow = new trimFlowData(flowFileName, (trimFlowFileName + extension), (scrapFlowFileName + extension), fastaFileName, flowOrder, tempBarcodePrimerComboFileNames, barcodes, primers, revPrimer, fasta, allFiles, lines[i]->start, lines[i]->end, m, signal, noise, numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, i);
777 pDataArray.push_back(tempflow);
779 //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
780 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
781 hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
784 //using the main process as a worker saves time and memory
786 m->openOutputFile(trimFlowFileName, temp);
789 m->openOutputFile(scrapFlowFileName, temp);
793 m->openOutputFile(fastaFileName, temp);
797 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
799 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
800 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
801 tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
803 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
810 //do my part - do last piece because windows is looking for eof
811 int num = driverCreateTrim(flowFileName, (trimFlowFileName + toString(processors-1) + ".temp"), (scrapFlowFileName + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
812 processIDS.push_back((processors-1));
814 //Wait until all threads have terminated.
815 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
817 //Close all thread handles and free memory allocations.
818 for(int i=0; i < pDataArray.size(); i++){
819 num += pDataArray[i]->count;
820 CloseHandle(hThreadArray[i]);
821 delete pDataArray[i];
827 m->mothurOutEndLine();
828 for(int i=0;i<processIDS.size();i++){
830 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
832 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
833 m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
834 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
836 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
837 m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
838 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
841 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
842 m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
843 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
846 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
847 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
848 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
849 m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
858 catch(exception& e) {
859 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
864 //***************************************************************************************************************