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","flow-file",false,true,true); parameters.push_back(pflow);
18 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none","",false,false,true); 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,true); parameters.push_back(ppdiffs);
23 CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "","",false,false,true); parameters.push_back(pbdiffs);
24 CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pldiffs);
25 CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psdiffs);
26 CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "","",false,false); parameters.push_back(ptdiffs);
27 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false,true); parameters.push_back(pprocessors);
28 CommandParameter psignal("signal", "Number", "", "0.50", "", "", "","",false,false); parameters.push_back(psignal);
29 CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "","",false,false); parameters.push_back(pnoise);
30 CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "","",false,false); parameters.push_back(pallfiles);
31 CommandParameter porder("order", "String", "", "TACG", "", "", "","",false,false); parameters.push_back(porder);
32 CommandParameter pfasta("fasta", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pfasta);
33 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
34 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
36 vector<string> myArray;
37 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
41 m->errorOut(e, "TrimFlowsCommand", "setParameters");
45 //**********************************************************************************************************************
46 string TrimFlowsCommand::getHelpString(){
48 string helpString = "";
49 helpString += "The trim.flows command reads a flowgram file and creates .....\n";
50 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
51 helpString += "For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n";
55 m->errorOut(e, "TrimFlowsCommand", "getHelpString");
59 //**********************************************************************************************************************
60 string TrimFlowsCommand::getOutputPattern(string type) {
64 if (type == "flow") { pattern = "[filename],[tag],flow"; }
65 else if (type == "fasta") { pattern = "[filename],flow.fasta"; }
66 else if (type == "file") { pattern = "[filename],flow.files"; }
67 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
72 m->errorOut(e, "TrimFlowsCommand", "getOutputPattern");
76 //**********************************************************************************************************************
78 TrimFlowsCommand::TrimFlowsCommand(){
80 abort = true; calledHelp = true;
82 vector<string> tempOutNames;
83 outputTypes["flow"] = tempOutNames;
84 outputTypes["fasta"] = tempOutNames;
85 outputTypes["file"] = tempOutNames;
88 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
92 //**********************************************************************************************************************
94 TrimFlowsCommand::TrimFlowsCommand(string option) {
97 abort = false; calledHelp = false;
100 //allow user to run help
101 if(option == "help") { help(); abort = true; calledHelp = true; }
102 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
106 vector<string> myArray = setParameters();
108 OptionParser parser(option);
109 map<string,string> parameters = parser.getParameters();
111 ValidParameters validParameter;
112 map<string,string>::iterator it;
114 //check to make sure all parameters are valid for command
115 for (it = parameters.begin(); it != parameters.end(); it++) {
116 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
119 //initialize outputTypes
120 vector<string> tempOutNames;
121 outputTypes["flow"] = tempOutNames;
122 outputTypes["fasta"] = tempOutNames;
123 outputTypes["file"] = tempOutNames;
125 //if the user changes the input directory command factory will send this info to us in the output parameter
126 string inputDir = validParameter.validFile(parameters, "inputdir", false);
127 if (inputDir == "not found"){ inputDir = ""; }
130 it = parameters.find("flow");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["flow"] = inputDir + it->second; }
138 it = parameters.find("oligos");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["oligos"] = inputDir + it->second; }
149 //check for required parameters
150 flowFileName = validParameter.validFile(parameters, "flow", true);
151 if (flowFileName == "not found") {
152 flowFileName = m->getFlowFile();
153 if (flowFileName != "") { m->mothurOut("Using " + flowFileName + " as input file for the flow parameter."); m->mothurOutEndLine(); }
155 m->mothurOut("No valid current flow file. You must provide a flow file."); m->mothurOutEndLine();
158 }else if (flowFileName == "not open") { flowFileName = ""; abort = true; }
160 //if the user changes the output directory command factory will send this info to us in the output parameter
161 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
163 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
167 //check for optional parameter and set defaults
168 // ...at some point should added some additional type checking...
171 temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "450"; }
172 m->mothurConvert(temp, minFlows);
174 temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "450"; }
175 m->mothurConvert(temp, maxFlows);
178 temp = validParameter.validFile(parameters, "oligos", true);
179 if (temp == "not found") { oligoFileName = ""; }
180 else if(temp == "not open") { abort = true; }
181 else { oligoFileName = temp; m->setOligosFile(oligoFileName); }
183 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
184 else if(m->isTrue(temp)) { fasta = 1; }
186 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
187 m->mothurConvert(temp, maxHomoP);
189 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
190 m->mothurConvert(temp, signal);
192 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
193 m->mothurConvert(temp, noise);
195 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
196 m->mothurConvert(temp, bdiffs);
198 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
199 m->mothurConvert(temp, pdiffs);
201 temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
202 m->mothurConvert(temp, ldiffs);
204 temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
205 m->mothurConvert(temp, sdiffs);
207 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs; temp = toString(tempTotal); }
208 m->mothurConvert(temp, tdiffs);
210 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
213 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
214 m->setProcessors(temp);
215 m->mothurConvert(temp, processors);
217 flowOrder = validParameter.validFile(parameters, "order", false);
218 if (flowOrder == "not found"){ flowOrder = "TACG"; }
219 else if(flowOrder.length() != 4){
220 m->mothurOut("The value of the order option must be four bases long\n");
223 if(oligoFileName == "") { allFiles = 0; }
224 else { allFiles = 1; }
233 catch(exception& e) {
234 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
239 //***************************************************************************************************************
241 int TrimFlowsCommand::execute(){
244 if (abort == true) { if (calledHelp) { return 0; } return 2; }
246 map<string, string> variables;
247 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
248 string fastaFileName = getOutputFileName("fasta",variables);
249 if(fasta){ outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName); }
251 variables["[tag]"] = "trim";
252 string trimFlowFileName = getOutputFileName("flow",variables);
253 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
255 variables["[tag]"] = "scrap";
256 string scrapFlowFileName = getOutputFileName("flow",variables);
257 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
261 vector<unsigned long long> flowFilePos;
262 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
263 flowFilePos = getFlowFileBreaks();
264 for (int i = 0; i < (flowFilePos.size()-1); i++) {
265 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
268 ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
269 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
271 ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
272 if (processors == 1) {
273 lines.push_back(new linePair(0, 1000));
276 flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
277 flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
279 //figure out how many sequences you have to process
280 int numSeqsPerProcessor = numFlowLines / processors;
281 cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
282 for (int i = 0; i < processors; i++) {
283 int startIndex = i * numSeqsPerProcessor;
284 if(i == (processors - 1)){ numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor; }
285 lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
286 cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
291 vector<vector<string> > barcodePrimerComboFileNames;
292 if(oligoFileName != ""){
293 getOligos(barcodePrimerComboFileNames);
297 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
299 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
302 if (m->control_pressed) { return 0; }
304 string flowFilesFileName;
308 set<string> namesAlreadyProcessed;
309 flowFilesFileName = getOutputFileName("file",variables);
310 m->openOutputFile(flowFilesFileName, output);
312 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
313 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
314 if (namesAlreadyProcessed.count(barcodePrimerComboFileNames[i][j]) == 0) {
315 if (barcodePrimerComboFileNames[i][j] != "") {
317 unsigned long long size;
319 //get num bytes in file
320 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
321 if (pFile==NULL) perror ("Error opening file");
323 fseek (pFile, 0, SEEK_END);
329 m->mothurRemove(barcodePrimerComboFileNames[i][j]);
332 output << m->getFullPathName(barcodePrimerComboFileNames[i][j]) << endl;
333 outputNames.push_back(barcodePrimerComboFileNames[i][j]);
334 outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
336 namesAlreadyProcessed.insert(barcodePrimerComboFileNames[i][j]);
344 flowFilesFileName = getOutputFileName("file",variables);
345 m->openOutputFile(flowFilesFileName, output);
347 output << m->getFullPathName(trimFlowFileName) << endl;
351 outputTypes["file"].push_back(flowFilesFileName);
352 outputNames.push_back(flowFilesFileName);
354 m->mothurOutEndLine();
355 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
356 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
357 m->mothurOutEndLine();
361 catch(exception& e) {
362 m->errorOut(e, "TrimSeqsCommand", "execute");
367 //***************************************************************************************************************
369 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
372 ofstream trimFlowFile;
373 m->openOutputFile(trimFlowFileName, trimFlowFile);
374 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
376 ofstream scrapFlowFile;
377 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
378 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
381 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
384 m->openInputFile(flowFileName, flowFile);
386 flowFile.seekg(line->start);
388 if(line->start == 0){
389 flowFile >> numFlows; m->gobble(flowFile);
390 scrapFlowFile << maxFlows << endl;
391 trimFlowFile << maxFlows << endl;
393 for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
394 for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
395 if (thisBarcodePrimerComboFileNames[i][j] != "") {
397 m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
398 temp << maxFlows << endl;
406 FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
407 //cout << " driver flowdata address " << &flowData << &flowFile << endl;
411 TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
415 if (m->control_pressed) { break; }
418 int currentSeqDiffs = 0;
419 string trashCode = "";
421 flowData.getNext(flowFile);
422 flowData.capFlows(maxFlows);
424 Sequence currSeq = flowData.getSequence();
425 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
431 int barcodeIndex = 0;
434 success = trimOligos.stripLinker(currSeq);
435 if(success > ldiffs) { trashCode += 'k'; }
436 else{ currentSeqDiffs += success; }
440 if (m->debug) { m->mothurOut("[DEBUG]: " + currSeq.getName() + " " + currSeq.getUnaligned() + "\n"); }
442 if(barcodes.size() != 0){
443 success = trimOligos.stripBarcode(currSeq, barcodeIndex);
444 if(success > bdiffs) { trashCode += 'b'; }
445 else{ currentSeqDiffs += success; }
449 success = trimOligos.stripSpacer(currSeq);
450 if(success > sdiffs) { trashCode += 's'; }
451 else{ currentSeqDiffs += success; }
455 if(numFPrimers != 0){
456 success = trimOligos.stripForward(currSeq, primerIndex);
457 if(success > pdiffs) { trashCode += 'f'; }
458 else{ currentSeqDiffs += success; }
461 if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
463 if(numRPrimers != 0){
464 success = trimOligos.stripReverse(currSeq);
465 if(!success) { trashCode += 'r'; }
468 if(trashCode.length() == 0){
469 string thisGroup = "";
470 if(barcodes.size() != 0){
471 thisGroup = barcodeNameVector[barcodeIndex];
472 if (primers.size() != 0) {
473 if (primerNameVector[primerIndex] != "") {
474 if(thisGroup != "") {
475 thisGroup += "." + primerNameVector[primerIndex];
477 thisGroup = primerNameVector[primerIndex];
483 int pos = thisGroup.find("ignore");
484 if (pos == string::npos) {
485 flowData.printFlows(trimFlowFile);
487 if(fasta) { currSeq.printSequence(fastaFile); }
491 m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
492 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
494 flowData.printFlows(output);
500 flowData.printFlows(scrapFlowFile, trashCode);
504 //cout << "driver" << '\t' << currSeq.getName() << endl;
506 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
508 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
509 unsigned long long pos = flowFile.tellg();
511 if ((pos == -1) || (pos >= line->end)) { break; }
513 if (flowFile.eof()) { break; }
518 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
520 trimFlowFile.close();
521 scrapFlowFile.close();
523 if(fasta){ fastaFile.close(); }
527 catch(exception& e) {
528 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
533 //***************************************************************************************************************
535 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
538 m->openInputFile(oligoFileName, oligosFile);
540 string type, oligo, group;
543 int indexBarcode = 0;
545 while(!oligosFile.eof()){
547 oligosFile >> type; m->gobble(oligosFile); //get the first column value of the row - is it a comment or a feature we are interested in?
549 if(type[0] == '#'){ //igore the line because there's a comment
550 while (!oligosFile.eof()) { char c = oligosFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
552 else{ //there's a feature we're interested in
554 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } //make type case insensitive
556 oligosFile >> oligo; //get the DNA sequence for the feature
558 for(int i=0;i<oligo.length();i++){ //make type case insensitive and change any U's to T's
559 oligo[i] = toupper(oligo[i]);
560 if(oligo[i] == 'U') { oligo[i] = 'T'; }
563 if(type == "FORWARD"){ //if the feature is a forward primer...
566 while (!oligosFile.eof()) { // get rest of line in case there is a primer name = will have the name of the primer
567 char c = oligosFile.get();
568 if (c == 10 || c == 13){ break; }
569 else if (c == 32 || c == 9){;} //space or tab
573 //have we seen this primer already?
574 map<string, int>::iterator itPrimer = primers.find(oligo);
575 if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
577 primers[oligo]=indexPrimer; indexPrimer++;
578 primerNameVector.push_back(group);
581 else if(type == "REVERSE"){
582 string oligoRC = reverseOligo(oligo);
583 revPrimer.push_back(oligoRC);
585 else if(type == "BARCODE"){
588 //check for repeat barcodes
589 map<string, int>::iterator itBar = barcodes.find(oligo);
590 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
592 barcodes[oligo]=indexBarcode; indexBarcode++;
593 barcodeNameVector.push_back(group);
594 }else if(type == "LINKER"){
595 linker.push_back(oligo);
596 }else if(type == "SPACER"){
597 spacer.push_back(oligo);
600 m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
604 m->gobble(oligosFile);
608 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
610 //add in potential combos
611 if(barcodeNameVector.size() == 0){
613 barcodeNameVector.push_back("");
616 if(primerNameVector.size() == 0){
618 primerNameVector.push_back("");
622 outFlowFileNames.resize(barcodeNameVector.size());
623 for(int i=0;i<outFlowFileNames.size();i++){
624 outFlowFileNames[i].assign(primerNameVector.size(), "");
629 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
630 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
632 string primerName = primerNameVector[itPrimer->second];
633 string barcodeName = barcodeNameVector[itBar->second];
635 if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
637 string comboGroupName = "";
638 string fileName = "";
640 map<string, string> variables;
641 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(flowFileName));
643 if(primerName == ""){
644 comboGroupName = barcodeNameVector[itBar->second];
645 variables["[tag]"] = comboGroupName;
646 fileName = getOutputFileName("flow", variables);
649 if(barcodeName == ""){
650 comboGroupName = primerNameVector[itPrimer->second];
653 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
655 variables["[tag]"] = comboGroupName;
656 fileName = getOutputFileName("flow", variables);
659 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
662 m->openOutputFile(fileName, temp);
669 numFPrimers = primers.size();
670 numRPrimers = revPrimer.size();
671 numLinkers = linker.size();
672 numSpacers = spacer.size();
675 catch(exception& e) {
676 m->errorOut(e, "TrimSeqsCommand", "getOligos");
680 //********************************************************************/
681 string TrimFlowsCommand::reverseOligo(string oligo){
685 for(int i=oligo.length()-1;i>=0;i--){
687 if(oligo[i] == 'A') { reverse += 'T'; }
688 else if(oligo[i] == 'T'){ reverse += 'A'; }
689 else if(oligo[i] == 'U'){ reverse += 'A'; }
691 else if(oligo[i] == 'G'){ reverse += 'C'; }
692 else if(oligo[i] == 'C'){ reverse += 'G'; }
694 else if(oligo[i] == 'R'){ reverse += 'Y'; }
695 else if(oligo[i] == 'Y'){ reverse += 'R'; }
697 else if(oligo[i] == 'M'){ reverse += 'K'; }
698 else if(oligo[i] == 'K'){ reverse += 'M'; }
700 else if(oligo[i] == 'W'){ reverse += 'W'; }
701 else if(oligo[i] == 'S'){ reverse += 'S'; }
703 else if(oligo[i] == 'B'){ reverse += 'V'; }
704 else if(oligo[i] == 'V'){ reverse += 'B'; }
706 else if(oligo[i] == 'D'){ reverse += 'H'; }
707 else if(oligo[i] == 'H'){ reverse += 'D'; }
709 else { reverse += 'N'; }
715 catch(exception& e) {
716 m->errorOut(e, "TrimFlowsCommand", "reverseOligo");
721 /**************************************************************************************************/
722 vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
726 vector<unsigned long long> filePos;
727 filePos.push_back(0);
730 unsigned long long size;
732 //get num bytes in file
733 pFile = fopen (flowFileName.c_str(),"rb");
734 if (pFile==NULL) perror ("Error opening file");
736 fseek (pFile, 0, SEEK_END);
741 //estimate file breaks
742 unsigned long long chunkSize = 0;
743 chunkSize = size / processors;
745 //file too small to divide by processors
746 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
748 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
749 for (int i = 0; i < processors; i++) {
750 unsigned long long spot = (i+1) * chunkSize;
753 m->openInputFile(flowFileName, in);
756 string dummy = m->getline(in);
758 //there was not another sequence before the end of the file
759 unsigned long long sanityPos = in.tellg();
761 // if (sanityPos == -1) { break; }
762 // else { filePos.push_back(newSpot); }
763 if (sanityPos == -1) { break; }
764 else { filePos.push_back(sanityPos); }
770 filePos.push_back(size);
772 //sanity check filePos
773 for (int i = 0; i < (filePos.size()-1); i++) {
774 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
778 m->openInputFile(flowFileName, in);
781 //unsigned long long spot = in.tellg();
785 processors = (filePos.size() - 1);
789 catch(exception& e) {
790 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
795 /**************************************************************************************************/
797 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
803 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
806 //loop through and create all the processes you want
807 while (process != processors) {
811 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
815 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
817 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
818 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
819 if (tempBarcodePrimerComboFileNames[i][j] != "") {
820 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
822 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
828 driverCreateTrim(flowFileName,
829 (trimFlowFileName + toString(getpid()) + ".temp"),
830 (scrapFlowFileName + toString(getpid()) + ".temp"),
831 (fastaFileName + toString(getpid()) + ".temp"),
832 tempBarcodePrimerComboFileNames, lines[process]);
836 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
837 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
844 m->openOutputFile(trimFlowFileName, temp);
847 m->openOutputFile(scrapFlowFileName, temp);
851 m->openOutputFile(fastaFileName, temp);
855 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
857 //force parent to wait until all the processes are done
858 for (int i=0;i<processIDS.size();i++) {
859 int temp = processIDS[i];
863 //////////////////////////////////////////////////////////////////////////////////////////////////////
864 //Windows version shared memory, so be careful when passing variables through the trimFlowData struct.
865 //Above fork() will clone, so memory is separate, but that's not the case with windows,
866 //////////////////////////////////////////////////////////////////////////////////////////////////////
868 vector<trimFlowData*> pDataArray;
869 DWORD dwThreadIdArray[processors-1];
870 HANDLE hThreadArray[processors-1];
872 //Create processor worker threads.
873 for( int i=0; i<processors-1; i++ ){
874 // Allocate memory for thread data.
875 string extension = "";
876 if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
878 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
880 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
881 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
882 if (tempBarcodePrimerComboFileNames[i][j] != "") {
883 tempBarcodePrimerComboFileNames[i][j] += extension;
885 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
892 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);
893 pDataArray.push_back(tempflow);
895 //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
896 //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
897 hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
900 //using the main process as a worker saves time and memory
902 m->openOutputFile(trimFlowFileName, temp);
905 m->openOutputFile(scrapFlowFileName, temp);
909 m->openOutputFile(fastaFileName, temp);
913 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
915 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
916 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
917 if (tempBarcodePrimerComboFileNames[i][j] != "") {
918 tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
920 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
928 //do my part - do last piece because windows is looking for eof
929 int num = driverCreateTrim(flowFileName, (trimFlowFileName + toString(processors-1) + ".temp"), (scrapFlowFileName + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
930 processIDS.push_back((processors-1));
932 //Wait until all threads have terminated.
933 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
935 //Close all thread handles and free memory allocations.
936 for(int i=0; i < pDataArray.size(); i++){
937 num += pDataArray[i]->count;
938 CloseHandle(hThreadArray[i]);
939 delete pDataArray[i];
945 m->mothurOutEndLine();
946 for(int i=0;i<processIDS.size();i++){
948 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
950 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
951 m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
952 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
954 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
955 m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
956 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
959 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
960 m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
961 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
964 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
965 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
966 if (barcodePrimerComboFileNames[j][k] != "") {
967 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
968 m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
978 catch(exception& e) {
979 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
984 //***************************************************************************************************************