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(){
68 abort = true; calledHelp = true;
69 vector<string> tempOutNames;
70 outputTypes["flow"] = tempOutNames;
71 outputTypes["fasta"] = tempOutNames;
74 m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
79 //***************************************************************************************************************
81 TrimFlowsCommand::~TrimFlowsCommand(){ /* do nothing */ }
83 //***************************************************************************************************************
85 void TrimFlowsCommand::help(){
87 m->mothurOut("The trim.flows command reads a flowgram file and creates .....\n");
88 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
89 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n\n");
93 m->errorOut(e, "TrimFlowsCommand", "help");
98 //**********************************************************************************************************************
100 TrimFlowsCommand::TrimFlowsCommand(string option) {
103 abort = false; calledHelp = false;
106 //allow user to run help
107 if(option == "help") { help(); abort = true; calledHelp = true; }
110 //valid paramters for this command
111 string AlignArray[] = {"flow", "totalflows", "minflows",
112 "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise",
113 "oligos", "pdiffs", "bdiffs", "tdiffs",
114 "allfiles", "processors",
117 "outputdir","inputdir"
121 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
123 OptionParser parser(option);
124 map<string,string> parameters = parser.getParameters();
126 ValidParameters validParameter;
127 map<string,string>::iterator it;
129 //check to make sure all parameters are valid for command
130 for (it = parameters.begin(); it != parameters.end(); it++) {
131 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
134 //initialize outputTypes
135 vector<string> tempOutNames;
136 outputTypes["flow"] = tempOutNames;
137 outputTypes["fasta"] = tempOutNames;
139 //if the user changes the input directory command factory will send this info to us in the output parameter
140 string inputDir = validParameter.validFile(parameters, "inputdir", false);
141 if (inputDir == "not found"){ inputDir = ""; }
144 it = parameters.find("flow");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["flow"] = inputDir + it->second; }
152 it = parameters.find("oligos");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["oligos"] = inputDir + it->second; }
161 // it = parameters.find("group");
162 // //user has given a template file
163 // if(it != parameters.end()){
164 // path = m->hasPath(it->second);
165 // //if the user has not given a path then, add inputdir. else leave path alone.
166 // if (path == "") { parameters["group"] = inputDir + it->second; }
171 //check for required parameters
172 flowFileName = validParameter.validFile(parameters, "flow", true);
173 if (flowFileName == "not found") { m->mothurOut("flow is a required parameter for the trim.flows command."); m->mothurOutEndLine(); abort = true; }
174 else if (flowFileName == "not open") { abort = true; }
176 //if the user changes the output directory command factory will send this info to us in the output parameter
177 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
179 outputDir += m->hasPath(flowFileName); //if user entered a file with a path then preserve it
183 //check for optional parameter and set defaults
184 // ...at some point should added some additional type checking...
187 temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "360"; }
188 convert(temp, minFlows);
190 temp = validParameter.validFile(parameters, "totalflows", false); if (temp == "not found") { temp = "720"; }
191 convert(temp, totalFlows);
194 temp = validParameter.validFile(parameters, "oligos", true);
195 if (temp == "not found") { oligoFileName = ""; }
196 else if(temp == "not open") { abort = true; }
197 else { oligoFileName = temp; }
199 temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
200 else if(m->isTrue(temp)) { fasta = 1; }
202 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
203 convert(temp, maxHomoP);
205 temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
206 convert(temp, signal);
208 temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
209 convert(temp, noise);
211 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found"){ temp = "0"; }
212 convert(temp, minLength);
214 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found"){ temp = "0"; }
215 convert(temp, maxLength);
217 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
218 convert(temp, bdiffs);
220 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
221 convert(temp, pdiffs);
223 temp = validParameter.validFile(parameters, "tdiffs", false);
224 if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
225 convert(temp, tdiffs);
226 if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
228 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "T"; }
229 allFiles = m->isTrue(temp);
231 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
232 convert(temp, processors);
234 if(oligoFileName == ""){ allFiles = 0; }
241 catch(exception& e) {
242 m->errorOut(e, "TrimFlowsCommand", "TrimSeqsCommand");
247 //***************************************************************************************************************
249 int TrimFlowsCommand::execute(){
252 if (abort == true) { if (calledHelp) { return 0; } return 2; }
254 string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
255 outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
257 string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
258 outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
260 string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
262 outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
265 vector<unsigned long int> flowFilePos = getFlowFileBreaks();
266 for (int i = 0; i < (flowFilePos.size()-1); i++) {
267 lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
270 vector<vector<string> > barcodePrimerComboFileNames;
271 if(oligoFileName != ""){
272 getOligos(barcodePrimerComboFileNames);
275 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
277 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
279 createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
282 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
285 if (m->control_pressed) { return 0; }
290 string flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
291 m->openOutputFile(flowFilesFileName, output);
293 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
294 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
297 unsigned long int size;
299 //get num bytes in file
300 pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
301 if (pFile==NULL) perror ("Error opening file");
303 fseek (pFile, 0, SEEK_END);
309 // m->mothurOut("deleting: " + barcodePrimerComboFileNames[i][j] + '\n');
310 remove(barcodePrimerComboFileNames[i][j].c_str());
313 // m->mothurOut("saving: " + barcodePrimerComboFileNames[i][j] + '\n');
314 output << barcodePrimerComboFileNames[i][j] << endl;
321 m->mothurOutEndLine();
322 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
323 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
324 m->mothurOutEndLine();
328 catch(exception& e) {
329 m->errorOut(e, "TrimSeqsCommand", "execute");
334 //***************************************************************************************************************
336 int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames, linePair* line){
340 ofstream trimFlowFile;
341 m->openOutputFile(trimFlowFileName, trimFlowFile);
342 trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
344 ofstream scrapFlowFile;
345 m->openOutputFile(scrapFlowFileName, scrapFlowFile);
346 scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
348 if(line->start == 4){
349 scrapFlowFile << totalFlows << endl;
350 trimFlowFile << totalFlows << endl;
351 for(int i=0;i<barcodePrimerComboFileNames.size();i++){
352 for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
353 // barcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
355 m->openOutputFile(barcodePrimerComboFileNames[i][j], temp);
356 temp << totalFlows << endl;
362 FlowData flowData(numFlows, signal, noise, maxHomoP);
365 if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
368 m->openInputFile(flowFileName, flowFile);
370 flowFile.seekg(line->start);
378 int currentSeqDiffs = 0;
379 string trashCode = "";
381 flowData.getNext(flowFile);
382 flowData.capFlows(totalFlows);
384 Sequence currSeq = flowData.getSequence();
385 if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
390 if(minLength > 0 || maxLength > 0){ //screen to see if sequence is above and below a specific number of bases
391 int seqLength = currSeq.getNumBases();
392 if(seqLength < minLength || seqLength > maxLength){
398 int primerIndex, barcodeIndex;
400 if(barcodes.size() != 0){
401 success = stripBarcode(currSeq, barcodeIndex);
402 if(success > bdiffs) { trashCode += 'b'; }
403 else{ currentSeqDiffs += success; }
406 if(numFPrimers != 0){
407 success = stripForward(currSeq, primerIndex);
408 if(success > pdiffs) { trashCode += 'f'; }
409 else{ currentSeqDiffs += success; }
412 if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
414 if(numRPrimers != 0){
415 success = stripReverse(currSeq);
416 if(!success) { trashCode += 'r'; }
420 if(trashCode.length() == 0){
421 flowData.printFlows(trimFlowFile);
424 currSeq.printSequence(fastaFile);
427 if(barcodes.size() != 0 || primers.size() != 0){
428 // string fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + barcodePrimerCombos[barcodeIndex][primerIndex] + ".flow";
430 m->openOutputFileAppend(barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
431 output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
433 flowData.printFlows(output);
439 flowData.printFlows(scrapFlowFile, trashCode);
445 if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
447 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
448 unsigned long int pos = flowFile.tellg();
450 if ((pos == -1) || (pos >= line->end)) { break; }
452 if (flowFile.eof()) { break; }
457 if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
459 trimFlowFile.close();
460 scrapFlowFile.close();
462 if(fasta){ fastaFile.close(); }
466 catch(exception& e) {
467 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
472 //***************************************************************************************************************
474 void TrimFlowsCommand::getOligos(vector<vector<string> >& outFlowFileNames){
477 m->openInputFile(oligoFileName, oligosFile);
479 string type, oligo, group;
482 int indexBarcode = 0;
484 while(!oligosFile.eof()){
486 oligosFile >> type; m->gobble(oligosFile); //get the first column value of the row - is it a comment or a feature we are interested in?
488 if(type[0] == '#'){ //igore the line because there's a comment
489 while (!oligosFile.eof()) { char c = oligosFile.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
491 else{ //there's a feature we're interested in
493 for(int i=0;i<type.length();i++){ type[i] = toupper(type[i]); } //make type case insensitive
495 oligosFile >> oligo; //get the DNA sequence for the feature
497 for(int i=0;i<oligo.length();i++){ //make type case insensitive and change any U's to T's
498 oligo[i] = toupper(oligo[i]);
499 if(oligo[i] == 'U') { oligo[i] = 'T'; }
502 if(type == "FORWARD"){ //if the feature is a forward primer...
505 while (!oligosFile.eof()) { // get rest of line in case there is a primer name = will have the name of the primer
506 char c = oligosFile.get();
507 if (c == 10 || c == 13){ break; }
508 else if (c == 32 || c == 9){;} //space or tab
512 //have we seen this primer already?
513 map<string, int>::iterator itPrimer = primers.find(oligo);
514 if (itPrimer != primers.end()) { m->mothurOut("primer " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
516 primers[oligo]=indexPrimer; indexPrimer++;
517 primerNameVector.push_back(group);
520 else if(type == "REVERSE"){
521 Sequence oligoRC("reverse", oligo);
522 oligoRC.reverseComplement();
523 revPrimer.push_back(oligoRC.getUnaligned());
525 else if(type == "BARCODE"){
528 //check for repeat barcodes
529 map<string, int>::iterator itBar = barcodes.find(oligo);
530 if (itBar != barcodes.end()) { m->mothurOut("barcode " + oligo + " is in your oligos file already."); m->mothurOutEndLine(); }
532 barcodes[oligo]=indexBarcode; indexBarcode++;
533 barcodeNameVector.push_back(group);
536 m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
540 m->gobble(oligosFile);
544 //add in potential combos
545 outFlowFileNames.resize(barcodeNameVector.size());
546 for(int i=0;i<outFlowFileNames.size();i++){
547 outFlowFileNames[i].assign(primerNameVector.size(), "");
551 for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
552 for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
554 string primerName = primerNameVector[itPrimer->second];
555 string barcodeName = barcodeNameVector[itBar->second];
557 string comboGroupName = "";
558 string fileName = "";
560 if(primerName == ""){
561 comboGroupName = barcodeNameVector[itBar->second];
562 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
565 comboGroupName = barcodeNameVector[itBar->second] + "." + primerNameVector[itPrimer->second];
566 fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
569 outputNames.push_back(fileName);
570 outputTypes["flow"].push_back(fileName);
571 outFlowFileNames[itBar->second][itPrimer->second] = fileName;
574 m->openOutputFile(fileName, temp);
580 numFPrimers = primers.size();
581 numRPrimers = revPrimer.size();
584 catch(exception& e) {
585 m->errorOut(e, "TrimSeqsCommand", "getOligos");
590 //***************************************************************************************************************
592 int TrimFlowsCommand::stripBarcode(Sequence& seq, int& group){
595 string rawSequence = seq.getUnaligned();
596 int success = bdiffs + 1; //guilty until proven innocent
598 //can you find the barcode
599 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
600 string oligo = it->first;
601 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
602 success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
606 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
608 seq.setUnaligned(rawSequence.substr(oligo.length()));
615 //if you found the barcode or if you don't want to allow for diffs
616 if ((bdiffs == 0) || (success == 0)) { return success; }
618 else { //try aligning and see if you can find it
622 Alignment* alignment;
623 if (barcodes.size() > 0) {
624 map<string,int>::iterator it=barcodes.begin();
626 for(it;it!=barcodes.end();it++){
627 if(it->first.length() > maxLength){
628 maxLength = it->first.length();
631 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+bdiffs+1));
633 }else{ alignment = NULL; }
635 //can you find the barcode
641 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
642 string oligo = it->first;
643 // int length = oligo.length();
645 if(rawSequence.length() < maxLength){ //let's just assume that the barcodes are the same length
646 success = bdiffs + 10;
650 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
651 alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
652 oligo = alignment->getSeqAAln();
653 string temp = alignment->getSeqBAln();
655 int alnLength = oligo.length();
657 for(int i=oligo.length()-1;i>=0;i--){
658 if(oligo[i] != '-'){ alnLength = i+1; break; }
660 oligo = oligo.substr(0,alnLength);
661 temp = temp.substr(0,alnLength);
663 int numDiff = countDiffs(oligo, temp);
665 if(numDiff < minDiff){
668 minGroup = it->second;
670 for(int i=0;i<alnLength;i++){
676 else if(numDiff == minDiff){
682 if(minDiff > bdiffs) { success = minDiff; } //no good matches
683 else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
684 else{ //use the best match
686 seq.setUnaligned(rawSequence.substr(minPos));
690 if (alignment != NULL) { delete alignment; }
697 catch(exception& e) {
698 m->errorOut(e, "TrimFlowsCommand", "stripBarcode");
704 //***************************************************************************************************************
706 int TrimFlowsCommand::stripForward(Sequence& seq, int& group){
709 string rawSequence = seq.getUnaligned();
710 int success = pdiffs + 1; //guilty until proven innocent
712 //can you find the primer
713 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
714 string oligo = it->first;
715 if(rawSequence.length() < oligo.length()){ //let's just assume that the primers are the same length
716 success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
720 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
722 seq.setUnaligned(rawSequence.substr(oligo.length()));
728 //if you found the barcode or if you don't want to allow for diffs
729 if ((pdiffs == 0) || (success == 0)) { return success; }
731 else { //try aligning and see if you can find it
735 Alignment* alignment;
736 if (primers.size() > 0) {
737 map<string,int>::iterator it=primers.begin();
739 for(it;it!=primers.end();it++){
740 if(it->first.length() > maxLength){
741 maxLength = it->first.length();
744 alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+pdiffs+1));
746 }else{ alignment = NULL; }
748 //can you find the barcode
754 for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
755 string oligo = it->first;
756 // int length = oligo.length();
758 if(rawSequence.length() < maxLength){
759 success = pdiffs + 100;
763 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
764 alignment->align(oligo, rawSequence.substr(0,oligo.length()+pdiffs));
765 oligo = alignment->getSeqAAln();
766 string temp = alignment->getSeqBAln();
768 int alnLength = oligo.length();
770 for(int i=oligo.length()-1;i>=0;i--){
771 if(oligo[i] != '-'){ alnLength = i+1; break; }
773 oligo = oligo.substr(0,alnLength);
774 temp = temp.substr(0,alnLength);
776 int numDiff = countDiffs(oligo, temp);
778 if(numDiff < minDiff){
781 minGroup = it->second;
783 for(int i=0;i<alnLength;i++){
789 else if(numDiff == minDiff){
795 if(minDiff > pdiffs) { success = minDiff; } //no good matches
796 else if(minCount > 1) { success = pdiffs + 10; } //can't tell the difference between multiple primers
797 else{ //use the best match
799 seq.setUnaligned(rawSequence.substr(minPos));
803 if (alignment != NULL) { delete alignment; }
810 catch(exception& e) {
811 m->errorOut(e, "TrimFlowsCommand", "stripForward");
816 //***************************************************************************************************************
818 bool TrimFlowsCommand::stripReverse(Sequence& seq){
821 string rawSequence = seq.getUnaligned();
822 bool success = 0; //guilty until proven innocent
824 for(int i=0;i<numRPrimers;i++){
825 string oligo = revPrimer[i];
827 if(rawSequence.length() < oligo.length()){
832 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
833 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
842 catch(exception& e) {
843 m->errorOut(e, "TrimFlowsCommand", "stripReverse");
849 //***************************************************************************************************************
851 bool TrimFlowsCommand::compareDNASeq(string oligo, string seq){
854 int length = oligo.length();
856 for(int i=0;i<length;i++){
858 if(oligo[i] != seq[i]){
859 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
860 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
861 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
862 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
863 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
864 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
865 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
866 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
867 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
868 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
869 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
870 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
872 if(success == 0) { break; }
881 catch(exception& e) {
882 m->errorOut(e, "TrimFlowsCommand", "compareDNASeq");
888 //***************************************************************************************************************
890 int TrimFlowsCommand::countDiffs(string oligo, string seq){
893 int length = oligo.length();
896 for(int i=0;i<length;i++){
898 if(oligo[i] != seq[i]){
899 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
900 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
901 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
902 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
903 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
904 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
905 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
906 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
907 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
908 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
909 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
910 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
917 catch(exception& e) {
918 m->errorOut(e, "TrimFlowsCommand", "countDiffs");
924 /**************************************************************************************************/
926 vector<unsigned long int> TrimFlowsCommand::getFlowFileBreaks() {
930 vector<unsigned long int> filePos;
931 filePos.push_back(0);
934 unsigned long int size;
936 //get num bytes in file
937 pFile = fopen (flowFileName.c_str(),"rb");
938 if (pFile==NULL) perror ("Error opening file");
940 fseek (pFile, 0, SEEK_END);
945 //estimate file breaks
946 unsigned long int chunkSize = 0;
947 chunkSize = size / processors;
949 //file too small to divide by processors
950 if (chunkSize == 0) { processors = 1; filePos.push_back(size); return filePos; }
952 //for each process seekg to closest file break and search for next '>' char. make that the filebreak
953 for (int i = 0; i < processors; i++) {
954 unsigned long int spot = (i+1) * chunkSize;
957 m->openInputFile(flowFileName, in);
960 string dummy = m->getline(in);
962 //there was not another sequence before the end of the file
963 unsigned long int sanityPos = in.tellg();
965 // if (sanityPos == -1) { break; }
966 // else { filePos.push_back(newSpot); }
967 if (sanityPos == -1) { break; }
968 else { filePos.push_back(sanityPos); }
974 filePos.push_back(size);
976 //sanity check filePos
977 for (int i = 0; i < (filePos.size()-1); i++) {
978 if (filePos[(i+1)] <= filePos[i]) { filePos.erase(filePos.begin()+(i+1)); i--; }
982 m->openInputFile(flowFileName, in);
985 unsigned long int spot = in.tellg();
989 processors = (filePos.size() - 1);
993 catch(exception& e) {
994 m->errorOut(e, "TrimSeqsCommand", "getFlowFileBreaks");
999 /**************************************************************************************************/
1001 int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
1004 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
1006 int exitCommand = 1;
1009 //loop through and create all the processes you want
1010 while (process != processors) {
1014 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
1016 }else if (pid == 0){
1018 vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
1019 for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
1020 for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
1021 tempBarcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
1023 m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
1029 driverCreateTrim(flowFileName,
1030 (trimFlowFileName + toString(getpid()) + ".temp"),
1031 (scrapFlowFileName + toString(getpid()) + ".temp"),
1032 (fastaFileName + toString(getpid()) + ".temp"),
1033 tempBarcodePrimerComboFileNames, lines[process]);
1037 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
1038 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
1045 m->openOutputFile(trimFlowFileName, temp);
1048 m->openOutputFile(scrapFlowFileName, temp);
1052 m->openOutputFile(fastaFileName, temp);
1056 driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
1058 //force parent to wait until all the processes are done
1059 for (int i=0;i<processIDS.size();i++) {
1060 int temp = processIDS[i];
1065 m->mothurOutEndLine();
1066 for(int i=0;i<processIDS.size();i++){
1068 m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
1070 m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
1071 remove((trimFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1072 // m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
1074 m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
1075 remove((scrapFlowFileName + toString(processIDS[i]) + ".temp").c_str());
1076 // m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
1079 m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
1080 remove((fastaFileName + toString(processIDS[i]) + ".temp").c_str());
1081 // m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
1084 for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
1085 for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
1086 m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
1087 remove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp").c_str());
1096 catch(exception& e) {
1097 m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");
1102 //***************************************************************************************************************