From fdee9c46a4268c7aeb4087203e692e6ad392e6ea Mon Sep 17 00:00:00 2001 From: Aaron <aaronquinlan@gmail.com> Date: Wed, 19 Jan 2011 12:54:38 -0500 Subject: [PATCH] Issue #50. SlopBed now adds percent-based slop to features with the -pct option. --- src/slopBed/slopBed.cpp | 30 +++++++++++++++++++----------- src/slopBed/slopBed.h | 11 ++++++----- src/slopBed/slopBedMain.cpp | 32 ++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/slopBed/slopBed.cpp b/src/slopBed/slopBed.cpp index 0403200f..c4cd1151 100644 --- a/src/slopBed/slopBed.cpp +++ b/src/slopBed/slopBed.cpp @@ -7,24 +7,25 @@ University of Virginia aaronquinlan@gmail.com - Licenced under the GNU General Public License 2.0 license. + Licensed under the GNU General Public License 2.0 license. ******************************************************************************/ #include "lineFileUtilities.h" #include "slopBed.h" -BedSlop::BedSlop(string &bedFile, string &genomeFile, bool &forceStrand, int &leftSlop, int &rightSlop) { +BedSlop::BedSlop(string &bedFile, string &genomeFile, bool forceStrand, float leftSlop, float rightSlop, bool fractional) { _bedFile = bedFile; _genomeFile = genomeFile; _forceStrand = forceStrand; - - _leftSlop = leftSlop; - _rightSlop = rightSlop; + _leftSlop = leftSlop; + _rightSlop = rightSlop; + _fractional = fractional; _bed = new BedFile(bedFile); _genome = new GenomeFile(genomeFile); + // get going, slop it up. SlopBed(); } @@ -44,7 +45,14 @@ void BedSlop::SlopBed() { bedStatus = _bed->GetNextBed(bedEntry, lineNum); while (bedStatus != BED_INVALID) { if (bedStatus == BED_VALID) { - AddSlop(bedEntry); + if (_fractional == false) { + AddSlop(bedEntry, _leftSlop, _rightSlop); + } + else { + int leftSlop = (int) (_leftSlop * bedEntry.size()); + int rightSlop = (int) (_rightSlop * bedEntry.size()); + AddSlop(bedEntry, leftSlop, rightSlop); + } _bed->reportBedNewLine(bedEntry); bedEntry = nullBed; } @@ -54,7 +62,7 @@ void BedSlop::SlopBed() { } -void BedSlop::AddSlop(BED &bed) { +void BedSlop::AddSlop(BED &bed, int leftSlop, int rightSlop) { // special handling if the BED entry is on the negative // strand and the user cares about strandedness. @@ -62,20 +70,20 @@ void BedSlop::AddSlop(BED &bed) { if ( (_forceStrand) && (bed.strand == "-") ) { // inspect the start - if ( (static_cast<int>(bed.start) - _rightSlop) > 0 ) bed.start -= _rightSlop; + if ( (static_cast<int>(bed.start) - rightSlop) > 0 ) bed.start -= rightSlop; else bed.start = 0; // inspect the start - if ( (static_cast<int>(bed.end) + _leftSlop) <= static_cast<int>(chromSize)) bed.end += _leftSlop; + if ( (static_cast<int>(bed.end) + leftSlop) <= static_cast<int>(chromSize)) bed.end += leftSlop; else bed.end = chromSize; } else { // inspect the start - if ( (static_cast<int>(bed.start) - _leftSlop) > 0) bed.start -= _leftSlop; + if ( (static_cast<int>(bed.start) - leftSlop) > 0) bed.start -= leftSlop; else bed.start = 0; // inspect the end - if ( (static_cast<int>(bed.end) + _rightSlop) <= static_cast<int>(chromSize)) bed.end += _rightSlop; + if ( (static_cast<int>(bed.end) + rightSlop) <= static_cast<int>(chromSize)) bed.end += rightSlop; else bed.end = chromSize; } } diff --git a/src/slopBed/slopBed.h b/src/slopBed/slopBed.h index 48d36f4f..d22590c7 100644 --- a/src/slopBed/slopBed.h +++ b/src/slopBed/slopBed.h @@ -30,7 +30,7 @@ class BedSlop { public: // constructor - BedSlop(string &bedFile, string &genomeFile, bool &forceStrand, int &leftSlop, int &rightSlop) ; + BedSlop(string &bedFile, string &genomeFile, bool forceStrand, float leftSlop, float rightSlop, bool fractional); // destructor ~BedSlop(void); @@ -42,9 +42,10 @@ private: string _bedFile; string _genomeFile; - bool _forceStrand; - int _leftSlop; - int _rightSlop; + bool _forceStrand; + float _leftSlop; + float _rightSlop; + bool _fractional; BedFile *_bed; GenomeFile *_genome; @@ -54,5 +55,5 @@ private: void SlopBed(); // method to add requested "slop" to a single BED entry - void AddSlop(BED &bed); + void AddSlop(BED &bed, int leftSlop, int rightSlop); }; diff --git a/src/slopBed/slopBedMain.cpp b/src/slopBed/slopBedMain.cpp index 3e490de0..149cc377 100644 --- a/src/slopBed/slopBedMain.cpp +++ b/src/slopBed/slopBedMain.cpp @@ -40,8 +40,9 @@ int main(int argc, char* argv[]) { bool haveBoth = false; bool forceStrand = false; - int leftSlop = 0; - int rightSlop = 0; + float leftSlop = 0.0; + float rightSlop = 0.0; + bool fractional = false; for(int i = 1; i < argc; i++) { int parameterLength = (int)strlen(argv[i]); @@ -75,28 +76,31 @@ int main(int argc, char* argv[]) { else if(PARAMETER_CHECK("-l", 2, parameterLength)) { if ((i+1) < argc) { haveLeft = true; - leftSlop = atoi(argv[i + 1]); + leftSlop = atof(argv[i + 1]); i++; } } else if(PARAMETER_CHECK("-r", 2, parameterLength)) { if ((i+1) < argc) { haveRight = true; - rightSlop = atoi(argv[i + 1]); + rightSlop = atof(argv[i + 1]); i++; } } else if(PARAMETER_CHECK("-b", 2, parameterLength)) { if ((i+1) < argc) { haveBoth = true; - leftSlop = atoi(argv[i + 1]); - rightSlop = atoi(argv[i + 1]); + leftSlop = atof(argv[i + 1]); + rightSlop = atof(argv[i + 1]); i++; } } else if(PARAMETER_CHECK("-s", 2, parameterLength)) { forceStrand = true; } + else if(PARAMETER_CHECK("-pct", 4, parameterLength)) { + fractional = true; + } else { cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl; showHelp = true; @@ -122,7 +126,7 @@ int main(int argc, char* argv[]) { } if (!showHelp) { - BedSlop *bc = new BedSlop(bedFile, genomeFile, forceStrand, leftSlop, rightSlop); + BedSlop *bc = new BedSlop(bedFile, genomeFile, forceStrand, leftSlop, rightSlop, fractional); delete bc; return 0; @@ -144,18 +148,22 @@ void ShowHelp(void) { cerr << "Options: " << endl; cerr << "\t-b\t" << "Increase the BED/GFF/VCF entry by -b base pairs in each direction." << endl; - cerr << "\t\t- (Integer)" << endl; + cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl; cerr << "\t-l\t" << "The number of base pairs to subtract from the start coordinate." << endl; - cerr << "\t\t- (Integer)" << endl; - + cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl; + cerr << "\t-r\t" << "The number of base pairs to add to the end coordinate." << endl; - cerr << "\t\t- (Integer)" << endl; - + cerr << "\t\t- (Integer) or (Float, e.g. 0.1) if used with -pct." << endl << endl; + cerr << "\t-s\t" << "Define -l and -r based on strand." << endl; cerr << "\t\tE.g. if used, -l 500 for a negative-stranded feature, " << endl; cerr << "\t\tit will add 500 bp downstream. Default = false." << endl << endl; + cerr << "\t-pct\t" << "Define -l and -r as a fraction of the feature's length." << endl; + cerr << "\t\tE.g. if used on a 1000bp feature, -l 0.50, " << endl; + cerr << "\t\twill add 500 bp \"upstream\". Default = false." << endl << endl; + cerr << "Notes: " << endl; cerr << "\t(1) Starts will be set to 0 if options would force it below 0." << endl; cerr << "\t(2) Ends will be set to the chromosome length if requested slop would" << endl; -- GitLab