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", "", "450", "", "", "",false,false); parameters.push_back(pmaxflows);
21 CommandParameter pminflows("minflows", "Number", "", "450", "", "", "",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 m->mothurConvert(temp, minFlows);
154 temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "450"; }
155 m->mothurConvert(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 m->mothurConvert(temp, maxHomoP);
169 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
170 m->mothurConvert(temp, signal);
172 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
173 m->mothurConvert(temp, noise);
175 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
176 m->mothurConvert(temp, bdiffs);
178 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
179 m->mothurConvert(temp, pdiffs);
181 temp = validParameter.validFile(parameters, "tdiffs", false);
182 if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
183 m->mothurConvert(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 m->mothurConvert(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; }
206 catch(exception& e) {
207 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
212 //***************************************************************************************************************
214 int TrimFlowsCommand::execute(){
217 if (abort == true) { if (calledHelp) { return 0; } return 2; }
219 string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
220 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
222 string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
223 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
225 string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
227 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
230 vector<unsigned long long> flowFilePos;
231 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
232 flowFilePos = getFlowFileBreaks();
233 for (int i = 0; i < (flowFilePos.size()-1); i++) {
234 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
237 ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
238 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
240 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
241 if (processors == 1) {
242 lines.push_back(new linePair(0, 1000));
245 flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
246 flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
248 //figure out how many sequences you have to process
249 int numSeqsPerProcessor = numFlowLines / processors;
250 cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
251 for (int i = 0; i < processors; i++) {
252 int startIndex = i * numSeqsPerProcessor;
253 if(i == (processors - 1)){ numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor; }
254 lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
255 cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
260 vector<vector<string> > barcodePrimerComboFileNames;
261 if(oligoFileName != ""){
262 getOligos(barcodePrimerComboFileNames);
266 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
268 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
271 if (m->control_pressed) { return 0; }
273 string flowFilesFileName;
277 set<string> namesAlreadyProcessed;
278 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
279 m->openOutputFile(flowFilesFileName, output);
281 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
282 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
283 if (namesAlreadyProcessed.count(barcodePrimerComboFileNames[i][j]) == 0) {
285 unsigned long long size;
287 //get num bytes in file
288 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
289 if (pFile==NULL) perror ("Error opening file");
291 fseek (pFile, 0, SEEK_END);
297 m->mothurRemove(barcodePrimerComboFileNames[i][j]);
300 output << barcodePrimerComboFileNames[i][j] << endl;
301 outputNames.push_back(barcodePrimerComboFileNames[i][j]);
302 outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
304 namesAlreadyProcessed.insert(barcodePrimerComboFileNames[i][j]);
311 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
312 m->openOutputFile(flowFilesFileName, output);
314 output << trimFlowFileName << endl;
318 outputTypes["flow.files"].push_back(flowFilesFileName);
319 outputNames.push_back(flowFilesFileName);
321 // set fasta file as new current fastafile
322 // string current = "";
323 // itTypes = outputTypes.find("fasta");
324 // if (itTypes != outputTypes.end()) {
325 // if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
328 m->mothurOutEndLine();
329 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
330 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
331 m->mothurOutEndLine();
335 catch(exception& e) {
336 m->errorOut(e, "TrimSeqsCommand", "execute");
341 //***************************************************************************************************************
343 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
346 ofstream trimFlowFile;
347 m->openOutputFile(trimFlowFileName, trimFlowFile);
348 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
350 ofstream scrapFlowFile;
351 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
352 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
355 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
358 m->openInputFile(flowFileName, flowFile);
360 flowFile.seekg(line->start);
362 if(line->start == 0){
363 flowFile >> numFlows; m->gobble(flowFile);
364 scrapFlowFile << maxFlows << endl;
365 trimFlowFile << maxFlows << endl;
367 for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
368 for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
370 m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
371 temp << maxFlows << endl;
378 FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
379 //cout << " driver flowdata address " << &flowData << &flowFile << endl;
383 TrimOligos trimOligos(pdiffs, bdiffs, primers, barcodes, revPrimer);
386 //cout << "driver " << count << endl;
388 if (m->control_pressed) { break; }
391 int currentSeqDiffs = 0;
392 string trashCode = "";
394 flowData.getNext(flowFile);
395 //cout << "driver good bit " << flowFile.good() << endl;
396 flowData.capFlows(maxFlows);
398 Sequence currSeq = flowData.getSequence();
400 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
406 int barcodeIndex = 0;
408 if(barcodes.size() != 0){
409 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
410 if(success > bdiffs) { trashCode += 'b'; }
411 else{ currentSeqDiffs += success; }
414 if(numFPrimers != 0){
415 success = trimOligos.stripForward(currSeq, primerIndex);
416 if(success > pdiffs) { trashCode += 'f'; }
417 else{ currentSeqDiffs += success; }
420 if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
422 if(numRPrimers != 0){
423 success = trimOligos.stripReverse(currSeq);
424 if(!success) { trashCode += 'r'; }
427 if(trashCode.length() == 0){
429 flowData.printFlows(trimFlowFile);
431 if(fasta) { currSeq.printSequence(fastaFile); }
435 m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
436 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
438 flowData.printFlows(output);
443 flowData.printFlows(scrapFlowFile, trashCode);
447 //cout << "driver" << '\t' << currSeq.getName() << endl;
449 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
451 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
452 unsigned long long pos = flowFile.tellg();
454 if ((pos == -1) || (pos >= line->end)) { break; }
456 if (flowFile.eof()) { break; }
461 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
463 trimFlowFile.close();
464 scrapFlowFile.close();
466 if(fasta){ fastaFile.close(); }
470 catch(exception& e) {
471 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
476 //***************************************************************************************************************
478 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
481 m->openInputFile(oligoFileName, oligosFile);
483 string type, oligo, group;
486 int indexBarcode = 0;
488 while(!oligosFile.eof()){
490 oligosFile >> type; m->gobble(oligosFile); //get the first column value of the row - is it a comment or a feature we are interested in?
492 if(type[0] == '#'){ //igore the line because there's a comment
493 while (!oligosFile.eof()) { char c = oligosFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
495 else{ //there's a feature we're interested in
497 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } //make type case insensitive
499 oligosFile >> oligo; //get the DNA sequence for the feature
501 for(int i=0;i<oligo.length();i++){ //make type case insensitive and change any U's to T's
502 oligo[i] = toupper(oligo[i]);
503 if(oligo[i] == 'U') { oligo[i] = 'T'; }
506 if(type == "FORWARD"){ //if the feature is a forward primer...
509 while (!oligosFile.eof()) { // get rest of line in case there is a primer name = will have the name of the primer
510 char c = oligosFile.get();
511 if (c == 10 || c == 13){ break; }
512 else if (c == 32 || c == 9){;} //space or tab
516 //have we seen this primer already?
517 map<string, int>::iterator itPrimer = primers.find(oligo);
518 if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
520 primers[oligo]=indexPrimer; indexPrimer++;
521 primerNameVector.push_back(group);
524 else if(type == "REVERSE"){
525 Sequence oligoRC("reverse", oligo);
526 oligoRC.reverseComplement();
527 revPrimer.push_back(oligoRC.getUnaligned());
529 else if(type == "BARCODE"){
532 //check for repeat barcodes
533 map<string, int>::iterator itBar = barcodes.find(oligo);
534 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
536 barcodes[oligo]=indexBarcode; indexBarcode++;
537 barcodeNameVector.push_back(group);
540 m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
544 m->gobble(oligosFile);
548 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
550 //add in potential combos
551 if(barcodeNameVector.size() == 0){
553 barcodeNameVector.push_back("");
556 if(primerNameVector.size() == 0){
558 primerNameVector.push_back("");
562 outFlowFileNames.resize(barcodeNameVector.size());
563 for(int i=0;i<outFlowFileNames.size();i++){
564 outFlowFileNames[i].assign(primerNameVector.size(), "");
569 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
570 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
572 string primerName = primerNameVector[itPrimer->second];
573 string barcodeName = barcodeNameVector[itBar->second];
575 string comboGroupName = "";
576 string fileName = "";
578 if(primerName == ""){
579 comboGroupName = barcodeNameVector[itBar->second];
580 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
583 if(barcodeName == ""){
584 comboGroupName = primerNameVector[itPrimer->second];
587 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
589 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
592 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
595 m->openOutputFile(fileName, temp);
601 numFPrimers = primers.size();
602 numRPrimers = revPrimer.size();
605 catch(exception& e) {
606 m->errorOut(e, "TrimSeqsCommand", "getOligos");
610 /**************************************************************************************************/
611 vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
615 vector<unsigned long long> filePos;
616 filePos.push_back(0);
619 unsigned long long size;
621 //get num bytes in file
622 pFile = fopen (flowFileName.c_str(),"rb");
623 if (pFile==NULL) perror ("Error opening file");
625 fseek (pFile, 0, SEEK_END);
630 //estimate file breaks
631 unsigned long long chunkSize = 0;
632 chunkSize = size / processors;
634 //file too small to divide by processors
635 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
637 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
638 for (int i = 0; i < processors; i++) {
639 unsigned long long spot = (i+1) * chunkSize;
642 m->openInputFile(flowFileName, in);
645 string dummy = m->getline(in);
647 //there was not another sequence before the end of the file
648 unsigned long long sanityPos = in.tellg();
650 // if (sanityPos == -1) { break; }
651 // else { filePos.push_back(newSpot); }
652 if (sanityPos == -1) { break; }
653 else { filePos.push_back(sanityPos); }
659 filePos.push_back(size);
661 //sanity check filePos
662 for (int i = 0; i < (filePos.size()-1); i++) {
663 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
667 m->openInputFile(flowFileName, in);
670 //unsigned long long spot = in.tellg();
674 processors = (filePos.size() - 1);
678 catch(exception& e) {
679 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
684 /**************************************************************************************************/
686 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
692 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
695 //loop through and create all the processes you want
696 while (process != processors) {
700 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
704 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
706 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
707 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
708 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
710 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
716 driverCreateTrim(flowFileName,
717 (trimFlowFileName + toString(getpid()) + ".temp"),
718 (scrapFlowFileName + toString(getpid()) + ".temp"),
719 (fastaFileName + toString(getpid()) + ".temp"),
720 tempBarcodePrimerComboFileNames, lines[process]);
724 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
725 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
732 m->openOutputFile(trimFlowFileName, temp);
735 m->openOutputFile(scrapFlowFileName, temp);
739 m->openOutputFile(fastaFileName, temp);
743 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
745 //force parent to wait until all the processes are done
746 for (int i=0;i<processIDS.size();i++) {
747 int temp = processIDS[i];
751 //////////////////////////////////////////////////////////////////////////////////////////////////////
752 //Windows version shared memory, so be careful when passing variables through the trimFlowData struct.
753 //Above fork() will clone, so memory is separate, but that's not the case with windows,
754 //////////////////////////////////////////////////////////////////////////////////////////////////////
756 vector<trimFlowData*> pDataArray;
757 DWORD dwThreadIdArray[processors-1];
758 HANDLE hThreadArray[processors-1];
760 //Create processor worker threads.
761 for( int i=0; i<processors-1; i++ ){
762 // Allocate memory for thread data.
763 string extension = "";
764 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
766 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
768 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
769 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
770 tempBarcodePrimerComboFileNames[i][j] += extension;
772 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
779 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);
780 pDataArray.push_back(tempflow);
782 //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
783 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
784 hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
787 //using the main process as a worker saves time and memory
789 m->openOutputFile(trimFlowFileName, temp);
792 m->openOutputFile(scrapFlowFileName, temp);
796 m->openOutputFile(fastaFileName, temp);
800 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
802 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
803 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
804 tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
806 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
813 //do my part - do last piece because windows is looking for eof
814 int num = driverCreateTrim(flowFileName, (trimFlowFileName + toString(processors-1) + ".temp"), (scrapFlowFileName + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
815 processIDS.push_back((processors-1));
817 //Wait until all threads have terminated.
818 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
820 //Close all thread handles and free memory allocations.
821 for(int i=0; i < pDataArray.size(); i++){
822 num += pDataArray[i]->count;
823 CloseHandle(hThreadArray[i]);
824 delete pDataArray[i];
830 m->mothurOutEndLine();
831 for(int i=0;i<processIDS.size();i++){
833 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
835 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
836 m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
837 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
839 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
840 m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
841 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
844 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
845 m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
846 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
849 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
850 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
851 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
852 m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
861 catch(exception& e) {
862 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
867 //***************************************************************************************************************