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", "totalflows", "minflows",
18 "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise"
19 "oligos", "pdiffs", "bdiffs", "tdiffs",
20 "allfiles", "processors",
25 "outputdir","inputdir"
28 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32 m->errorOut(e, "TrimFlowsCommand", "getValidParameters");
37 //**********************************************************************************************************************
39 vector<string> TrimFlowsCommand::getRequiredParameters(){
41 string Array[] = {"flow"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "TrimFlowsCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
53 vector<string> TrimFlowsCommand::getRequiredFiles(){
55 vector<string> myArray;
59 m->errorOut(e, "TrimFlowsCommand", "getRequiredFiles");
64 //**********************************************************************************************************************
66 TrimFlowsCommand::TrimFlowsCommand(){
69 //initialize outputTypes
70 vector<string> tempOutNames;
71 outputTypes["flow"] = tempOutNames;
72 outputTypes["fasta"] = tempOutNames;
75 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
80 //***************************************************************************************************************
82 TrimFlowsCommand::~TrimFlowsCommand(){ /* do nothing */ }
84 //***************************************************************************************************************
86 void TrimFlowsCommand::help(){
88 m->mothurOut("The trim.flows command reads a flowgram file and creates .....\n");
89 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
90 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n\n");
94 m->errorOut(e, "TrimFlowsCommand", "help");
99 //**********************************************************************************************************************
101 TrimFlowsCommand::TrimFlowsCommand(string option) {
107 //allow user to run help
108 if(option == "help") { help(); abort = true; }
111 //valid paramters for this command
112 string AlignArray[] = {"flow", "totalflows", "minflows",
113 "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise",
114 "oligos", "pdiffs", "bdiffs", "tdiffs",
115 "allfiles", "processors",
118 "outputdir","inputdir"
122 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
124 OptionParser parser(option);
125 map<string,string> parameters = parser.getParameters();
127 ValidParameters validParameter;
128 map<string,string>::iterator it;
130 //check to make sure all parameters are valid for command
131 for (it = parameters.begin(); it != parameters.end(); it++) {
132 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
135 //initialize outputTypes
136 vector<string> tempOutNames;
137 outputTypes["flow"] = tempOutNames;
138 outputTypes["fasta"] = tempOutNames;
140 //if the user changes the input directory command factory will send this info to us in the output parameter
141 string inputDir = validParameter.validFile(parameters, "inputdir", false);
142 if (inputDir == "not found"){ inputDir = ""; }
145 it = parameters.find("flow");
146 //user has given a template file
147 if(it != parameters.end()){
148 path = m->hasPath(it->second);
149 //if the user has not given a path then, add inputdir. else leave path alone.
150 if (path == "") { parameters["flow"] = inputDir + it->second; }
153 it = parameters.find("oligos");
154 //user has given a template file
155 if(it != parameters.end()){
156 path = m->hasPath(it->second);
157 //if the user has not given a path then, add inputdir. else leave path alone.
158 if (path == "") { parameters["oligos"] = inputDir + it->second; }
162 // it = parameters.find("group");
163 // //user has given a template file
164 // if(it != parameters.end()){
165 // path = m->hasPath(it->second);
166 // //if the user has not given a path then, add inputdir. else leave path alone.
167 // if (path == "") { parameters["group"] = inputDir + it->second; }
172 //check for required parameters
173 flowFileName = validParameter.validFile(parameters, "flow", true);
174 if (flowFileName == "not found") { m->mothurOut("flow is a required parameter for the trim.flows command."); m->mothurOutEndLine(); abort = true; }
175 else if (flowFileName == "not open") { abort = true; }
177 //if the user changes the output directory command factory will send this info to us in the output parameter
178 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
180 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
184 //check for optional parameter and set defaults
185 // ...at some point should added some additional type checking...
188 temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "360"; }
189 convert(temp, minFlows);
191 temp = validParameter.validFile(parameters, "totalflows", false); if (temp == "not found") { temp = "720"; }
192 convert(temp, totalFlows);
195 temp = validParameter.validFile(parameters, "oligos", true);
196 if (temp == "not found") { oligoFileName = ""; }
197 else if(temp == "not open") { abort = true; }
198 else { oligoFileName = temp; }
200 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
201 else if(m->isTrue(temp)) { fasta = 1; }
203 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
204 convert(temp, maxHomoP);
206 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
207 convert(temp, signal);
209 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
210 convert(temp, noise);
212 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found"){ temp = "0"; }
213 convert(temp, minLength);
215 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found"){ temp = "0"; }
216 convert(temp, maxLength);
218 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
219 convert(temp, bdiffs);
221 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
222 convert(temp, pdiffs);
224 temp = validParameter.validFile(parameters, "tdiffs", false);
225 if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
226 convert(temp, tdiffs);
227 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
229 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "T"; }
230 allFiles = m->isTrue(temp);
232 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
233 convert(temp, processors);
235 if(oligoFileName == ""){ allFiles = 0; }
242 catch(exception& e) {
243 m->errorOut(e, "TrimFlowsCommand", "TrimSeqsCommand");
248 //***************************************************************************************************************
250 int TrimFlowsCommand::execute(){
253 if (abort == true) { return 0; }
255 string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
256 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
258 string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
259 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
261 string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
263 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
266 vector<unsigned long int> flowFilePos = getFlowFileBreaks();
267 for (int i = 0; i < (flowFilePos.size()-1); i++) {
268 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
271 vector<vector<string> > barcodePrimerComboFileNames;
272 if(oligoFileName != ""){
273 getOligos(barcodePrimerComboFileNames);
276 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
278 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
280 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
283 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
286 if (m->control_pressed) { return 0; }
291 string flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
292 m->openOutputFile(flowFilesFileName, output);
294 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
295 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
298 unsigned long int size;
300 //get num bytes in file
301 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
302 if (pFile==NULL) perror ("Error opening file");
304 fseek (pFile, 0, SEEK_END);
310 // m->mothurOut("deleting: " + barcodePrimerComboFileNames[i][j] + '\n');
311 remove(barcodePrimerComboFileNames[i][j].c_str());
314 // m->mothurOut("saving: " + barcodePrimerComboFileNames[i][j] + '\n');
315 output << barcodePrimerComboFileNames[i][j] << endl;
322 m->mothurOutEndLine();
323 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
324 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
325 m->mothurOutEndLine();
329 catch(exception& e) {
330 m->errorOut(e, "TrimSeqsCommand", "execute");
335 //***************************************************************************************************************
337 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames, linePair* line){
341 ofstream trimFlowFile;
342 m->openOutputFile(trimFlowFileName, trimFlowFile);
343 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
345 ofstream scrapFlowFile;
346 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
347 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
349 if(line->start == 4){
350 scrapFlowFile << totalFlows << endl;
351 trimFlowFile << totalFlows << endl;
352 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
353 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
354 // barcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
356 m->openOutputFile(barcodePrimerComboFileNames[i][j], temp);
357 temp << totalFlows << endl;
363 FlowData flowData(numFlows, signal, noise, maxHomoP);
366 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
369 m->openInputFile(flowFileName, flowFile);
371 flowFile.seekg(line->start);
379 int currentSeqDiffs = 0;
380 string trashCode = "";
382 flowData.getNext(flowFile);
383 flowData.capFlows(totalFlows);
385 Sequence currSeq = flowData.getSequence();
386 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
391 if(minLength > 0 || maxLength > 0){ //screen to see if sequence is above and below a specific number of bases
392 int seqLength = currSeq.getNumBases();
393 if(seqLength < minLength || seqLength > maxLength){
399 int primerIndex, barcodeIndex;
401 if(barcodes.size() != 0){
402 success = stripBarcode(currSeq, barcodeIndex);
403 if(success > bdiffs) { trashCode += 'b'; }
404 else{ currentSeqDiffs += success; }
407 if(numFPrimers != 0){
408 success = stripForward(currSeq, primerIndex);
409 if(success > pdiffs) { trashCode += 'f'; }
410 else{ currentSeqDiffs += success; }
413 if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
415 if(numRPrimers != 0){
416 success = stripReverse(currSeq);
417 if(!success) { trashCode += 'r'; }
421 if(trashCode.length() == 0){
422 flowData.printFlows(trimFlowFile);
425 currSeq.printSequence(fastaFile);
428 if(barcodes.size() != 0 || primers.size() != 0){
429 // string fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + barcodePrimerCombos[barcodeIndex][primerIndex] + ".flow";
431 m->openOutputFileAppend(barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
432 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
434 flowData.printFlows(output);
440 flowData.printFlows(scrapFlowFile, trashCode);
446 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
448 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
449 unsigned long int 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 //add in potential combos
546 outFlowFileNames.resize(barcodeNameVector.size());
547 for(int i=0;i<outFlowFileNames.size();i++){
548 outFlowFileNames[i].assign(primerNameVector.size(), "");
552 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
553 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
555 string primerName = primerNameVector[itPrimer->second];
556 string barcodeName = barcodeNameVector[itBar->second];
558 string comboGroupName = "";
559 string fileName = "";
561 if(primerName == ""){
562 comboGroupName = barcodeNameVector[itBar->second];
563 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
566 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
567 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
570 outputNames.push_back(fileName);
571 outputTypes["flow"].push_back(fileName);
572 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
575 m->openOutputFile(fileName, temp);
581 numFPrimers = primers.size();
582 numRPrimers = revPrimer.size();
585 catch(exception& e) {
586 m->errorOut(e, "TrimSeqsCommand", "getOligos");
591 //***************************************************************************************************************
593 int TrimFlowsCommand::stripBarcode(Sequence& seq, int& group){
596 string rawSequence = seq.getUnaligned();
597 int success = bdiffs + 1; //guilty until proven innocent
599 //can you find the barcode
600 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
601 string oligo = it->first;
602 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
603 success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
607 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
609 seq.setUnaligned(rawSequence.substr(oligo.length()));
616 //if you found the barcode or if you don't want to allow for diffs
617 if ((bdiffs == 0) || (success == 0)) { return success; }
619 else { //try aligning and see if you can find it
623 Alignment* alignment;
624 if (barcodes.size() > 0) {
625 map<string,int>::iterator it=barcodes.begin();
627 for(it;it!=barcodes.end();it++){
628 if(it->first.length() > maxLength){
629 maxLength = it->first.length();
632 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+bdiffs+1));
634 }else{ alignment = NULL; }
636 //can you find the barcode
642 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
643 string oligo = it->first;
644 // int length = oligo.length();
646 if(rawSequence.length() < maxLength){ //let's just assume that the barcodes are the same length
647 success = bdiffs + 10;
651 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
652 alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
653 oligo = alignment->getSeqAAln();
654 string temp = alignment->getSeqBAln();
656 int alnLength = oligo.length();
658 for(int i=oligo.length()-1;i>=0;i--){
659 if(oligo[i] != '-'){ alnLength = i+1; break; }
661 oligo = oligo.substr(0,alnLength);
662 temp = temp.substr(0,alnLength);
665 int numDiff = countDiffs(oligo, temp);
667 if(numDiff < minDiff){
670 minGroup = it->second;
672 for(int i=0;i<alnLength;i++){
678 else if(numDiff == minDiff){
684 if(minDiff > bdiffs) { success = minDiff; } //no good matches
685 else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
686 else{ //use the best match
688 seq.setUnaligned(rawSequence.substr(minPos));
692 if (alignment != NULL) { delete alignment; }
699 catch(exception& e) {
700 m->errorOut(e, "TrimFlowsCommand", "stripBarcode");
706 //***************************************************************************************************************
708 int TrimFlowsCommand::stripForward(Sequence& seq, int& group){
711 string rawSequence = seq.getUnaligned();
712 int success = pdiffs + 1; //guilty until proven innocent
714 //can you find the primer
715 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
716 string oligo = it->first;
717 if(rawSequence.length() < oligo.length()){ //let's just assume that the primers are the same length
718 success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
722 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
724 seq.setUnaligned(rawSequence.substr(oligo.length()));
730 //if you found the barcode or if you don't want to allow for diffs
731 if ((pdiffs == 0) || (success == 0)) { return success; }
733 else { //try aligning and see if you can find it
737 Alignment* alignment;
738 if (primers.size() > 0) {
739 map<string,int>::iterator it=primers.begin();
741 for(it;it!=primers.end();it++){
742 if(it->first.length() > maxLength){
743 maxLength = it->first.length();
746 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+pdiffs+1));
748 }else{ alignment = NULL; }
750 //can you find the barcode
756 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
757 string oligo = it->first;
758 // int length = oligo.length();
760 if(rawSequence.length() < maxLength){
761 success = pdiffs + 100;
765 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
766 alignment->align(oligo, rawSequence.substr(0,oligo.length()+pdiffs));
767 oligo = alignment->getSeqAAln();
768 string temp = alignment->getSeqBAln();
770 int alnLength = oligo.length();
772 for(int i=oligo.length()-1;i>=0;i--){
773 if(oligo[i] != '-'){ alnLength = i+1; break; }
775 oligo = oligo.substr(0,alnLength);
776 temp = temp.substr(0,alnLength);
779 int numDiff = countDiffs(oligo, temp);
781 if(numDiff < minDiff){
784 minGroup = it->second;
786 for(int i=0;i<alnLength;i++){
792 else if(numDiff == minDiff){
798 if(minDiff > pdiffs) { success = minDiff; } //no good matches
799 else if(minCount > 1) { success = pdiffs + 10; } //can't tell the difference between multiple primers
800 else{ //use the best match
802 seq.setUnaligned(rawSequence.substr(minPos));
806 if (alignment != NULL) { delete alignment; }
813 catch(exception& e) {
814 m->errorOut(e, "TrimFlowsCommand", "stripForward");
819 //***************************************************************************************************************
821 bool TrimFlowsCommand::stripReverse(Sequence& seq){
824 string rawSequence = seq.getUnaligned();
825 bool success = 0; //guilty until proven innocent
827 for(int i=0;i<numRPrimers;i++){
828 string oligo = revPrimer[i];
830 if(rawSequence.length() < oligo.length()){
835 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
836 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
845 catch(exception& e) {
846 m->errorOut(e, "TrimFlowsCommand", "stripReverse");
852 //***************************************************************************************************************
854 bool TrimFlowsCommand::compareDNASeq(string oligo, string seq){
857 int length = oligo.length();
859 for(int i=0;i<length;i++){
861 if(oligo[i] != seq[i]){
862 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
863 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
864 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
865 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
866 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
867 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
868 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
869 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
870 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
871 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
872 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
873 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
875 if(success == 0) { break; }
884 catch(exception& e) {
885 m->errorOut(e, "TrimFlowsCommand", "compareDNASeq");
891 //***************************************************************************************************************
893 int TrimFlowsCommand::countDiffs(string oligo, string seq){
896 int length = oligo.length();
899 for(int i=0;i<length;i++){
901 if(oligo[i] != seq[i]){
902 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
903 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
904 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
905 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
906 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
907 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
908 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
909 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
910 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
911 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
912 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
913 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
920 catch(exception& e) {
921 m->errorOut(e, "TrimFlowsCommand", "countDiffs");
927 /**************************************************************************************************/
929 vector<unsigned long int> TrimFlowsCommand::getFlowFileBreaks() {
933 vector<unsigned long int> filePos;
934 filePos.push_back(0);
937 unsigned long int size;
939 //get num bytes in file
940 pFile = fopen (flowFileName.c_str(),"rb");
941 if (pFile==NULL) perror ("Error opening file");
943 fseek (pFile, 0, SEEK_END);
948 //estimate file breaks
949 unsigned long int chunkSize = 0;
950 chunkSize = size / processors;
952 //file too small to divide by processors
953 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
955 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
956 for (int i = 0; i < processors; i++) {
957 unsigned long int spot = (i+1) * chunkSize;
960 m->openInputFile(flowFileName, in);
963 unsigned long int newSpot = spot;
964 string dummy = m->getline(in);
967 //there was not another sequence before the end of the file
968 unsigned long int sanityPos = in.tellg();
970 // if (sanityPos == -1) { break; }
971 // else { filePos.push_back(newSpot); }
972 if (sanityPos == -1) { break; }
973 else { filePos.push_back(sanityPos); }
979 filePos.push_back(size);
981 //sanity check filePos
982 for (int i = 0; i < (filePos.size()-1); i++) {
983 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
987 m->openInputFile(flowFileName, in);
990 unsigned long int spot = in.tellg();
994 processors = (filePos.size() - 1);
998 catch(exception& e) {
999 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
1004 /**************************************************************************************************/
1006 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
1009 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1011 int exitCommand = 1;
1014 //loop through and create all the processes you want
1015 while (process != processors) {
1019 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1021 }else if (pid == 0){
1023 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
1024 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
1025 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
1026 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
1028 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
1034 driverCreateTrim(flowFileName,
1035 (trimFlowFileName + toString(getpid()) + ".temp"),
1036 (scrapFlowFileName + toString(getpid()) + ".temp"),
1037 (fastaFileName + toString(getpid()) + ".temp"),
1038 tempBarcodePrimerComboFileNames, lines[process]);
1042 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1043 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1050 m->openOutputFile(trimFlowFileName, temp);
1053 m->openOutputFile(scrapFlowFileName, temp);
1057 m->openOutputFile(fastaFileName, temp);
1061 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
1063 //force parent to wait until all the processes are done
1064 for (int i=0;i<processIDS.size();i++) {
1065 int temp = processIDS[i];
1070 m->mothurOutEndLine();
1071 for(int i=0;i<processIDS.size();i++){
1073 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
1075 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
1076 remove((trimFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1077 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
1079 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
1080 remove((scrapFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1081 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
1084 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
1085 remove((fastaFileName + toString(processIDS[i]) + ".temp").c_str());
1086 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
1089 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
1090 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
1091 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
1092 remove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp").c_str());
1101 catch(exception& e) {
1102 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
1107 //***************************************************************************************************************