diff --git a/src/closestBed/closestBed.cpp b/src/closestBed/closestBed.cpp index f04e530c9e1e5c580e130a2c9c51abfe1485740c..12279c3922643cee83fa082ccdd564881debafa3 100644 --- a/src/closestBed/closestBed.cpp +++ b/src/closestBed/closestBed.cpp @@ -53,6 +53,16 @@ BedClosest::BedClosest(string &bedAFile, string &bedBFile, bool sameStrand, BedClosest::~BedClosest(void) { } +void BedClosest::ReportClosestNotFound(const BED &a) { + _bedA->reportBedTab(a); + if (_reportDistance == true) { + _bedB->reportNullBedTab(); + cout << -1 << endl; + } + else + _bedB->reportNullBedNewLine(); +} + void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) { @@ -194,58 +204,58 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) { // if no overlaps were found, we'll widen the range // by SLOPGROWTH in each direction and search again. slop += SLOPGROWTH; - } - } - // there is no feature in B on the same chromosome as A - else { - _bedA->reportBedTab(a); - if (_reportDistance == true) { - _bedB->reportNullBedTab(); - cout << -1 << endl; - } - else - _bedB->reportNullBedNewLine(); - } - - // report the closest feature(s) in B to the current A feature. - // obey the user's reporting request (_tieMode) - if (numOverlaps > 0) { - if (closestB.size() == 1 || - (_tieMode == "first" && closestB.size() > 0)) - { - _bedA->reportBedTab(a); - if (_reportDistance == true) { - _bedB->reportBedTab(closestB[0]); - cout << distances[0] << endl; + } // while ((numOverlaps == 0) && (slop <= MAXSLOP)) + + // report the closest feature(s) in B to the current A feature. + // obey the user's reporting request (_tieMode) + if (numOverlaps > 0) { + if (closestB.size() == 1 || + (_tieMode == "first" && closestB.size() > 0)) + { + _bedA->reportBedTab(a); + if (_reportDistance == true) { + _bedB->reportBedTab(closestB[0]); + cout << distances[0] << endl; + } + else + _bedB->reportBedNewLine(closestB[0]); } - else - _bedB->reportBedNewLine(closestB[0]); - } - else { - if (_tieMode == "all") { - size_t i = 0; - vector<BED>::iterator b = closestB.begin(); - for (; b != closestB.end(); ++b) - { + else { + if (_tieMode == "all") { + size_t i = 0; + vector<BED>::iterator b = closestB.begin(); + for (; b != closestB.end(); ++b) + { + _bedA->reportBedTab(a); + if (_reportDistance == true) { + _bedB->reportBedTab(*b); + cout << distances[i++] <<endl; + } + else + _bedB->reportBedNewLine(*b); + } + } + else if (_tieMode == "last" && closestB.size() > 0) { _bedA->reportBedTab(a); if (_reportDistance == true) { - _bedB->reportBedTab(*b); - cout << distances[i++] <<endl; + _bedB->reportBedTab(closestB[closestB.size()-1]); + cout << distances[distances.size() - 1]<<endl; } else - _bedB->reportBedNewLine(*b); - } - } - else if (_tieMode == "last" && closestB.size() > 0) { - _bedA->reportBedTab(a); - if (_reportDistance == true) { - _bedB->reportBedTab(closestB[closestB.size()-1]); - cout << distances[distances.size() - 1]<<endl; + _bedB->reportBedNewLine(closestB[closestB.size()-1]); } - else - _bedB->reportBedNewLine(closestB[closestB.size()-1]); } } + // there were B features on the same chrom as A, but there were + // none that met the user's criteria (e.g., that it be on the + // same chrom) + else if (numOverlaps == 0) { + ReportClosestNotFound(a); + } + } + // there is no feature in B on the same chromosome as A + else { + ReportClosestNotFound(a); } } diff --git a/src/closestBed/closestBed.h b/src/closestBed/closestBed.h index dae9d561a2d07af85756ef4c54ed5edcab38f363..59051869872fb11da2a94a3b648750e220d7564b 100644 --- a/src/closestBed/closestBed.h +++ b/src/closestBed/closestBed.h @@ -59,8 +59,8 @@ private: BedFile *_bedA, *_bedB; // methods - void reportNullB(); void FindWindowOverlaps(BED &, vector<BED> &); + void ReportClosestNotFound(const BED &a); }; #endif /* CLOSEST_H */ diff --git a/test/closest/strand-test-a.bed b/test/closest/strand-test-a.bed new file mode 100644 index 0000000000000000000000000000000000000000..3d31b6208a86f877b2ee5e58bbab2c028896afe9 --- /dev/null +++ b/test/closest/strand-test-a.bed @@ -0,0 +1,2 @@ +chr1 100 200 a 10 + + diff --git a/test/closest/strand-test-b.bed b/test/closest/strand-test-b.bed new file mode 100644 index 0000000000000000000000000000000000000000..1957ceb8f4d0d6f6817aad451ed5d35fb224478a --- /dev/null +++ b/test/closest/strand-test-b.bed @@ -0,0 +1,3 @@ +chr1 90 120 b 1 - + + diff --git a/test/closest/test-closest.sh b/test/closest/test-closest.sh index 0c094d0488edbdc5c16ccab02c23ab1e382c7ee4..58269cf86010e7c7bbda161cb404b0648d9d5215 100644 --- a/test/closest/test-closest.sh +++ b/test/closest/test-closest.sh @@ -81,3 +81,23 @@ chr1 55 58 break2 chr1 40 50 break1 6" > exp $BT closest -a a.names.bed -b b.names.bed -d -N > obs check obs exp rm obs exp + +########################################################### +# test closest forcing -s yet no matching strands on chrom +########################################################### +echo " closest.t7...\c" +echo \ +"chr1 100 200 a 10 + . -1 -1 . -1 ." > exp +$BT closest -a strand-test-a.bed -b strand-test-b.bed -s > obs +check obs exp +rm obs exp + +########################################################### +# test closest forcing -S with only an opp strands on chrom +########################################################### +echo " closest.t8...\c" +echo \ +"chr1 100 200 a 10 + chr1 90 120 b 1 -" > exp +$BT closest -a strand-test-a.bed -b strand-test-b.bed -S > obs +check obs exp +rm obs exp