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"
13 //**********************************************************************************************************************
15 vector<string> TrimFlowsCommand::getValidParameters(){
17 string Array[] = {"flow", "maxflows", "minflows",
18 "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise"
19 "oligos", "pdiffs", "bdiffs", "tdiffs",
20 "allfiles", "processors",
21 "outputdir","inputdir"
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 m->errorOut(e, "TrimFlowsCommand", "getValidParameters");
33 //**********************************************************************************************************************
35 vector<string> TrimFlowsCommand::getRequiredParameters(){
37 string Array[] = {"flow"};
38 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
42 m->errorOut(e, "TrimFlowsCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
49 vector<string> TrimFlowsCommand::getRequiredFiles(){
51 vector<string> myArray;
55 m->errorOut(e, "TrimFlowsCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
62 TrimFlowsCommand::TrimFlowsCommand(){
64 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["flow"] = tempOutNames;
67 outputTypes["fasta"] = tempOutNames;
70 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
75 //***************************************************************************************************************
77 TrimFlowsCommand::~TrimFlowsCommand(){ /* do nothing */ }
79 //***************************************************************************************************************
81 void TrimFlowsCommand::help(){
83 m->mothurOut("The trim.flows command reads a flowgram file and creates .....\n");
84 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
85 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n\n");
89 m->errorOut(e, "TrimFlowsCommand", "help");
94 //**********************************************************************************************************************
96 TrimFlowsCommand::TrimFlowsCommand(string option) {
99 abort = false; calledHelp = false;
102 //allow user to run help
103 if(option == "help") { help(); abort = true; calledHelp = true; }
106 //valid paramters for this command
107 string AlignArray[] = {"flow", "maxflows", "minflows",
108 "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise",
109 "oligos", "pdiffs", "bdiffs", "tdiffs",
110 "allfiles", "processors",
113 "outputdir","inputdir"
117 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
119 OptionParser parser(option);
120 map<string,string> parameters = parser.getParameters();
122 ValidParameters validParameter;
123 map<string,string>::iterator it;
125 //check to make sure all parameters are valid for command
126 for (it = parameters.begin(); it != parameters.end(); it++) {
127 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
130 //initialize outputTypes
131 vector<string> tempOutNames;
132 outputTypes["flow"] = tempOutNames;
133 outputTypes["fasta"] = tempOutNames;
135 //if the user changes the input directory command factory will send this info to us in the output parameter
136 string inputDir = validParameter.validFile(parameters, "inputdir", false);
137 if (inputDir == "not found"){ inputDir = ""; }
140 it = parameters.find("flow");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["flow"] = inputDir + it->second; }
148 it = parameters.find("oligos");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["oligos"] = inputDir + it->second; }
157 // it = parameters.find("group");
158 // //user has given a template file
159 // if(it != parameters.end()){
160 // path = m->hasPath(it->second);
161 // //if the user has not given a path then, add inputdir. else leave path alone.
162 // if (path == "") { parameters["group"] = inputDir + it->second; }
167 //check for required parameters
168 flowFileName = validParameter.validFile(parameters, "flow", true);
169 if (flowFileName == "not found") { m->mothurOut("flow is a required parameter for the trim.flows command."); m->mothurOutEndLine(); abort = true; }
170 else if (flowFileName == "not open") { abort = true; }
172 //if the user changes the output directory command factory will send this info to us in the output parameter
173 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
175 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
179 //check for optional parameter and set defaults
180 // ...at some point should added some additional type checking...
183 temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "360"; }
184 convert(temp, minFlows);
186 temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "720"; }
187 convert(temp, maxFlows);
190 temp = validParameter.validFile(parameters, "oligos", true);
191 if (temp == "not found") { oligoFileName = ""; }
192 else if(temp == "not open") { abort = true; }
193 else { oligoFileName = temp; }
195 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
196 else if(m->isTrue(temp)) { fasta = 1; }
198 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
199 convert(temp, maxHomoP);
201 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
202 convert(temp, signal);
204 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
205 convert(temp, noise);
207 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found"){ temp = "0"; }
208 convert(temp, minLength);
210 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found"){ temp = "0"; }
211 convert(temp, maxLength);
213 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
214 convert(temp, bdiffs);
216 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
217 convert(temp, pdiffs);
219 temp = validParameter.validFile(parameters, "tdiffs", false);
220 if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
221 convert(temp, tdiffs);
222 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
224 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "T"; }
225 allFiles = m->isTrue(temp);
227 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
228 convert(temp, processors);
230 if(oligoFileName == ""){ allFiles = 0; }
237 catch(exception& e) {
238 m->errorOut(e, "TrimFlowsCommand", "TrimSeqsCommand");
243 //***************************************************************************************************************
245 int TrimFlowsCommand::execute(){
248 if (abort == true) { if (calledHelp) { return 0; } return 2; }
250 string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
251 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
253 string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
254 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
256 string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
258 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
261 vector<unsigned long int> flowFilePos = getFlowFileBreaks();
262 for (int i = 0; i < (flowFilePos.size()-1); i++) {
263 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
266 vector<vector<string> > barcodePrimerComboFileNames;
267 if(oligoFileName != ""){
268 getOligos(barcodePrimerComboFileNames);
271 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
273 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
275 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
278 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
281 if (m->control_pressed) { return 0; }
283 string flowFilesFileName;
288 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
289 m->openOutputFile(flowFilesFileName, output);
291 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
292 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
295 unsigned long int size;
297 //get num bytes in file
298 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
299 if (pFile==NULL) perror ("Error opening file");
301 fseek (pFile, 0, SEEK_END);
307 remove(barcodePrimerComboFileNames[i][j].c_str());
310 output << barcodePrimerComboFileNames[i][j] << endl;
317 flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
318 m->openOutputFile(flowFilesFileName, output);
320 output << trimFlowFileName << endl;
324 outputTypes["flow.files"].push_back(flowFilesFileName);
326 m->mothurOutEndLine();
327 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
328 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
329 m->mothurOutEndLine();
333 catch(exception& e) {
334 m->errorOut(e, "TrimSeqsCommand", "execute");
339 //***************************************************************************************************************
341 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames, linePair* line){
345 ofstream trimFlowFile;
346 m->openOutputFile(trimFlowFileName, trimFlowFile);
347 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
349 ofstream scrapFlowFile;
350 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
351 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
353 if(line->start == 4){
354 scrapFlowFile << maxFlows << endl;
355 trimFlowFile << maxFlows << endl;
357 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
358 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
359 // barcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
361 m->openOutputFile(barcodePrimerComboFileNames[i][j], temp);
362 temp << maxFlows << endl;
369 FlowData flowData(numFlows, signal, noise, maxHomoP);
372 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
375 m->openInputFile(flowFileName, flowFile);
377 flowFile.seekg(line->start);
385 int currentSeqDiffs = 0;
386 string trashCode = "";
388 flowData.getNext(flowFile);
389 flowData.capFlows(maxFlows);
391 Sequence currSeq = flowData.getSequence();
392 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
397 if(minLength > 0 || maxLength > 0){ //screen to see if sequence is above and below a specific number of bases
398 int seqLength = currSeq.getNumBases();
399 if(seqLength < minLength || seqLength > maxLength){
406 int barcodeIndex = 0;
408 if(barcodes.size() != 0){
409 success = stripBarcode(currSeq, barcodeIndex);
410 if(success > bdiffs) { trashCode += 'b'; }
411 else{ currentSeqDiffs += success; }
414 if(numFPrimers != 0){
415 success = 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 = stripReverse(currSeq);
424 if(!success) { trashCode += 'r'; }
427 if(trashCode.length() == 0){
429 flowData.printFlows(trimFlowFile);
431 if(fasta) { currSeq.printSequence(fastaFile); }
434 // string fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + barcodePrimerCombos[barcodeIndex][primerIndex] + ".flow";
436 m->openOutputFileAppend(barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
437 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
439 flowData.printFlows(output);
445 flowData.printFlows(scrapFlowFile, trashCode);
451 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
453 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
454 unsigned long int pos = flowFile.tellg();
456 if ((pos == -1) || (pos >= line->end)) { break; }
458 if (flowFile.eof()) { break; }
463 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
465 trimFlowFile.close();
466 scrapFlowFile.close();
468 if(fasta){ fastaFile.close(); }
472 catch(exception& e) {
473 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
478 //***************************************************************************************************************
480 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
483 m->openInputFile(oligoFileName, oligosFile);
485 string type, oligo, group;
488 int indexBarcode = 0;
490 while(!oligosFile.eof()){
492 oligosFile >> type; m->gobble(oligosFile); //get the first column value of the row - is it a comment or a feature we are interested in?
494 if(type[0] == '#'){ //igore the line because there's a comment
495 while (!oligosFile.eof()) { char c = oligosFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
497 else{ //there's a feature we're interested in
499 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } //make type case insensitive
501 oligosFile >> oligo; //get the DNA sequence for the feature
503 for(int i=0;i<oligo.length();i++){ //make type case insensitive and change any U's to T's
504 oligo[i] = toupper(oligo[i]);
505 if(oligo[i] == 'U') { oligo[i] = 'T'; }
508 if(type == "FORWARD"){ //if the feature is a forward primer...
511 while (!oligosFile.eof()) { // get rest of line in case there is a primer name = will have the name of the primer
512 char c = oligosFile.get();
513 if (c == 10 || c == 13){ break; }
514 else if (c == 32 || c == 9){;} //space or tab
518 //have we seen this primer already?
519 map<string, int>::iterator itPrimer = primers.find(oligo);
520 if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
522 primers[oligo]=indexPrimer; indexPrimer++;
523 primerNameVector.push_back(group);
526 else if(type == "REVERSE"){
527 Sequence oligoRC("reverse", oligo);
528 oligoRC.reverseComplement();
529 revPrimer.push_back(oligoRC.getUnaligned());
531 else if(type == "BARCODE"){
534 //check for repeat barcodes
535 map<string, int>::iterator itBar = barcodes.find(oligo);
536 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
538 barcodes[oligo]=indexBarcode; indexBarcode++;
539 barcodeNameVector.push_back(group);
542 m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
546 m->gobble(oligosFile);
550 if(barcodeNameVector.size() == 0 && primerNameVector[0] == ""){ allFiles = 0; }
552 //add in potential combos
553 if(barcodeNameVector.size() == 0){
555 barcodeNameVector.push_back("");
558 if(primerNameVector.size() == 0){
560 primerNameVector.push_back("");
564 outFlowFileNames.resize(barcodeNameVector.size());
565 for(int i=0;i<outFlowFileNames.size();i++){
566 outFlowFileNames[i].assign(primerNameVector.size(), "");
571 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
572 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
574 string primerName = primerNameVector[itPrimer->second];
575 string barcodeName = barcodeNameVector[itBar->second];
577 string comboGroupName = "";
578 string fileName = "";
580 if(primerName == ""){
581 comboGroupName = barcodeNameVector[itBar->second];
582 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
585 if(barcodeName == ""){
586 comboGroupName = primerNameVector[itPrimer->second];
589 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
591 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
594 outputNames.push_back(fileName);
595 outputTypes["flow"].push_back(fileName);
596 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
599 m->openOutputFile(fileName, temp);
605 numFPrimers = primers.size();
606 numRPrimers = revPrimer.size();
609 catch(exception& e) {
610 m->errorOut(e, "TrimSeqsCommand", "getOligos");
615 //***************************************************************************************************************
617 int TrimFlowsCommand::stripBarcode(Sequence& seq, int& group){
620 string rawSequence = seq.getUnaligned();
621 int success = bdiffs + 1; //guilty until proven innocent
623 //can you find the barcode
624 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
625 string oligo = it->first;
626 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
627 success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
631 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
633 seq.setUnaligned(rawSequence.substr(oligo.length()));
640 //if you found the barcode or if you don't want to allow for diffs
641 if ((bdiffs == 0) || (success == 0)) { return success; }
643 else { //try aligning and see if you can find it
647 Alignment* alignment;
648 if (barcodes.size() > 0) {
649 map<string,int>::iterator it=barcodes.begin();
651 for(it;it!=barcodes.end();it++){
652 if(it->first.length() > maxLength){
653 maxLength = it->first.length();
656 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+bdiffs+1));
658 }else{ alignment = NULL; }
660 //can you find the barcode
666 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
667 string oligo = it->first;
668 // int length = oligo.length();
670 if(rawSequence.length() < maxLength){ //let's just assume that the barcodes are the same length
671 success = bdiffs + 10;
675 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
676 alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
677 oligo = alignment->getSeqAAln();
678 string temp = alignment->getSeqBAln();
680 int alnLength = oligo.length();
682 for(int i=oligo.length()-1;i>=0;i--){
683 if(oligo[i] != '-'){ alnLength = i+1; break; }
685 oligo = oligo.substr(0,alnLength);
686 temp = temp.substr(0,alnLength);
688 int numDiff = countDiffs(oligo, temp);
690 if(numDiff < minDiff){
693 minGroup = it->second;
695 for(int i=0;i<alnLength;i++){
701 else if(numDiff == minDiff){
707 if(minDiff > bdiffs) { success = minDiff; } //no good matches
708 else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
709 else{ //use the best match
711 seq.setUnaligned(rawSequence.substr(minPos));
715 if (alignment != NULL) { delete alignment; }
722 catch(exception& e) {
723 m->errorOut(e, "TrimFlowsCommand", "stripBarcode");
729 //***************************************************************************************************************
731 int TrimFlowsCommand::stripForward(Sequence& seq, int& group){
734 string rawSequence = seq.getUnaligned();
735 int success = pdiffs + 1; //guilty until proven innocent
737 //can you find the primer
738 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
739 string oligo = it->first;
740 if(rawSequence.length() < oligo.length()){ //let's just assume that the primers are the same length
741 success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
745 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
747 seq.setUnaligned(rawSequence.substr(oligo.length()));
753 //if you found the barcode or if you don't want to allow for diffs
754 if ((pdiffs == 0) || (success == 0)) { return success; }
756 else { //try aligning and see if you can find it
760 Alignment* alignment;
761 if (primers.size() > 0) {
762 map<string,int>::iterator it=primers.begin();
764 for(it;it!=primers.end();it++){
765 if(it->first.length() > maxLength){
766 maxLength = it->first.length();
769 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+pdiffs+1));
771 }else{ alignment = NULL; }
773 //can you find the barcode
779 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
780 string oligo = it->first;
781 // int length = oligo.length();
783 if(rawSequence.length() < maxLength){
784 success = pdiffs + 100;
788 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
789 alignment->align(oligo, rawSequence.substr(0,oligo.length()+pdiffs));
790 oligo = alignment->getSeqAAln();
791 string temp = alignment->getSeqBAln();
793 int alnLength = oligo.length();
795 for(int i=oligo.length()-1;i>=0;i--){
796 if(oligo[i] != '-'){ alnLength = i+1; break; }
798 oligo = oligo.substr(0,alnLength);
799 temp = temp.substr(0,alnLength);
801 int numDiff = countDiffs(oligo, temp);
803 if(numDiff < minDiff){
806 minGroup = it->second;
808 for(int i=0;i<alnLength;i++){
814 else if(numDiff == minDiff){
820 if(minDiff > pdiffs) { success = minDiff; } //no good matches
821 else if(minCount > 1) { success = pdiffs + 10; } //can't tell the difference between multiple primers
822 else{ //use the best match
824 seq.setUnaligned(rawSequence.substr(minPos));
828 if (alignment != NULL) { delete alignment; }
835 catch(exception& e) {
836 m->errorOut(e, "TrimFlowsCommand", "stripForward");
841 //***************************************************************************************************************
843 bool TrimFlowsCommand::stripReverse(Sequence& seq){
846 string rawSequence = seq.getUnaligned();
847 bool success = 0; //guilty until proven innocent
849 for(int i=0;i<numRPrimers;i++){
850 string oligo = revPrimer[i];
852 if(rawSequence.length() < oligo.length()){
857 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
858 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
867 catch(exception& e) {
868 m->errorOut(e, "TrimFlowsCommand", "stripReverse");
874 //***************************************************************************************************************
876 bool TrimFlowsCommand::compareDNASeq(string oligo, string seq){
879 int length = oligo.length();
881 for(int i=0;i<length;i++){
883 if(oligo[i] != seq[i]){
884 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
885 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
886 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
887 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
888 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
889 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
890 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
891 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
892 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
893 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
894 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
895 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
897 if(success == 0) { break; }
906 catch(exception& e) {
907 m->errorOut(e, "TrimFlowsCommand", "compareDNASeq");
913 //***************************************************************************************************************
915 int TrimFlowsCommand::countDiffs(string oligo, string seq){
918 int length = oligo.length();
921 for(int i=0;i<length;i++){
923 if(oligo[i] != seq[i]){
924 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
925 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
926 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
927 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
928 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
929 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
930 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
931 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
932 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
933 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
934 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
935 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
942 catch(exception& e) {
943 m->errorOut(e, "TrimFlowsCommand", "countDiffs");
949 /**************************************************************************************************/
951 vector<unsigned long int> TrimFlowsCommand::getFlowFileBreaks() {
955 vector<unsigned long int> filePos;
956 filePos.push_back(0);
959 unsigned long int size;
961 //get num bytes in file
962 pFile = fopen (flowFileName.c_str(),"rb");
963 if (pFile==NULL) perror ("Error opening file");
965 fseek (pFile, 0, SEEK_END);
970 //estimate file breaks
971 unsigned long int chunkSize = 0;
972 chunkSize = size / processors;
974 //file too small to divide by processors
975 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
977 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
978 for (int i = 0; i < processors; i++) {
979 unsigned long int spot = (i+1) * chunkSize;
982 m->openInputFile(flowFileName, in);
985 string dummy = m->getline(in);
987 //there was not another sequence before the end of the file
988 unsigned long int sanityPos = in.tellg();
990 // if (sanityPos == -1) { break; }
991 // else { filePos.push_back(newSpot); }
992 if (sanityPos == -1) { break; }
993 else { filePos.push_back(sanityPos); }
999 filePos.push_back(size);
1001 //sanity check filePos
1002 for (int i = 0; i < (filePos.size()-1); i++) {
1003 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
1007 m->openInputFile(flowFileName, in);
1010 unsigned long int spot = in.tellg();
1014 processors = (filePos.size() - 1);
1018 catch(exception& e) {
1019 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
1024 /**************************************************************************************************/
1026 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
1029 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1031 int exitCommand = 1;
1034 //loop through and create all the processes you want
1035 while (process != processors) {
1039 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1041 }else if (pid == 0){
1043 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
1045 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
1046 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
1047 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
1049 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
1055 driverCreateTrim(flowFileName,
1056 (trimFlowFileName + toString(getpid()) + ".temp"),
1057 (scrapFlowFileName + toString(getpid()) + ".temp"),
1058 (fastaFileName + toString(getpid()) + ".temp"),
1059 tempBarcodePrimerComboFileNames, lines[process]);
1063 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1064 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1071 m->openOutputFile(trimFlowFileName, temp);
1074 m->openOutputFile(scrapFlowFileName, temp);
1078 m->openOutputFile(fastaFileName, temp);
1082 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
1084 //force parent to wait until all the processes are done
1085 for (int i=0;i<processIDS.size();i++) {
1086 int temp = processIDS[i];
1091 m->mothurOutEndLine();
1092 for(int i=0;i<processIDS.size();i++){
1094 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
1096 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
1097 remove((trimFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1098 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
1100 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
1101 remove((scrapFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1102 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
1105 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
1106 remove((fastaFileName + toString(processIDS[i]) + ".temp").c_str());
1107 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
1110 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
1111 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
1112 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
1113 remove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp").c_str());
1122 catch(exception& e) {
1123 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
1128 //***************************************************************************************************************