Skip to content
Snippets Groups Projects
Commit e59f85d4 authored by Jake Biesinger's avatar Jake Biesinger
Browse files

Added a signed distance option (-D) to closestBed

parent a5b2d831
No related branches found
No related tags found
No related merge requests found
...@@ -22,13 +22,14 @@ const int SLOPGROWTH = 2048000; ...@@ -22,13 +22,14 @@ const int SLOPGROWTH = 2048000;
Constructor Constructor
*/ */
BedClosest::BedClosest(string &bedAFile, string &bedBFile, bool sameStrand, bool diffStrand, BedClosest::BedClosest(string &bedAFile, string &bedBFile, bool sameStrand, bool diffStrand,
string &tieMode, bool reportDistance, bool ignoreOverlaps) string &tieMode, bool reportDistance, bool signDistance, bool ignoreOverlaps)
: _bedAFile(bedAFile) : _bedAFile(bedAFile)
, _bedBFile(bedBFile) , _bedBFile(bedBFile)
, _tieMode(tieMode) , _tieMode(tieMode)
, _sameStrand(sameStrand) , _sameStrand(sameStrand)
, _diffStrand(diffStrand) , _diffStrand(diffStrand)
, _reportDistance(reportDistance) , _reportDistance(reportDistance)
, _signDistance(signDistance)
, _ignoreOverlaps(ignoreOverlaps) , _ignoreOverlaps(ignoreOverlaps)
{ {
_bedA = new BedFile(_bedAFile); _bedA = new BedFile(_bedAFile);
...@@ -55,8 +56,8 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) { ...@@ -55,8 +56,8 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
CHRPOS aFudgeEnd; CHRPOS aFudgeEnd;
int numOverlaps = 0; int numOverlaps = 0;
vector<BED> closestB; vector<BED> closestB;
CHRPOS minDistance = INT_MAX; int32_t minDistance = INT_MAX;
vector<CHRPOS> distances; vector<int32_t> distances;
// is there at least one feature in B on the same chrom // is there at least one feature in B on the same chrom
// as the current A feature? // as the current A feature?
...@@ -102,30 +103,71 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) { ...@@ -102,30 +103,71 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
} }
// the hit is to the "left" of A // the hit is to the "left" of A
else if (h->end <= a.start) { else if (h->end <= a.start) {
if ((a.start - h->end) < minDistance) { if ((a.start - h->end) < abs(minDistance)) {
minDistance = a.start - h->end; if (_signDistance) {
if (a.strand == "+") {
minDistance = h->end - a.start;
}
else {
minDistance = a.start - h->end;
}
}
else {
minDistance = a.start - h->end;
}
closestB.clear(); closestB.clear();
closestB.push_back(*h); closestB.push_back(*h);
distances.clear(); distances.clear();
distances.push_back(minDistance); distances.push_back(minDistance);
} }
else if ((a.start - h->end) == minDistance) { else if ((a.start - h->end) == abs(minDistance)) {
if (_signDistance) {
if (a.strand == "+") {
minDistance = h->end - a.start;
}
else {
minDistance = a.start - h->end;
}
}
else {
minDistance = h->end - a.start;
}
closestB.push_back(*h); closestB.push_back(*h);
distances.push_back(minDistance); distances.push_back(minDistance);
} }
} }
// the hit is to the "right" of A // the hit is to the "right" of A
else if (h->start >= a.end) { else if (h->start >= a.end) {
if ((h->start - a.end) < minDistance) { if ((h->start - a.end) < abs(minDistance)) {
minDistance = h->start - a.end; if (_signDistance) {
if (a.strand == "+") {
minDistance = h->start - a.end;
}
else {
minDistance = a.end - h->start;
}
}
else {
minDistance = h->start - a.end;
}
closestB.clear(); closestB.clear();
closestB.push_back(*h); closestB.push_back(*h);
distances.clear(); distances.clear();
distances.push_back(minDistance); distances.push_back(minDistance);
} }
else if ((h->start - a.end) == minDistance) { else if ((h->start - a.end) == abs(minDistance)) {
if (_signDistance) {
if (a.strand == "+") {
minDistance = h->start - a.end;
}
else {
minDistance = a.end - h->start;
}
}
else {
minDistance = h->start - a.end;
}
closestB.push_back(*h); closestB.push_back(*h);
distances.push_back(minDistance); distances.push_back(minDistance);
} }
......
...@@ -29,7 +29,7 @@ public: ...@@ -29,7 +29,7 @@ public:
// constructor // constructor
BedClosest(string &bedAFile, string &bedBFile, BedClosest(string &bedAFile, string &bedBFile,
bool sameStrand, bool diffStrand, string &tieMode, bool sameStrand, bool diffStrand, string &tieMode,
bool reportDistance, bool ignoreOverlaps); bool reportDistance, bool signDistance, bool ignoreOverlaps);
// destructor // destructor
~BedClosest(void); ~BedClosest(void);
...@@ -46,6 +46,7 @@ private: ...@@ -46,6 +46,7 @@ private:
bool _sameStrand; bool _sameStrand;
bool _diffStrand; bool _diffStrand;
bool _reportDistance; bool _reportDistance;
bool _signDistance;
bool _ignoreOverlaps; bool _ignoreOverlaps;
BedFile *_bedA, *_bedB; BedFile *_bedA, *_bedB;
......
...@@ -40,6 +40,7 @@ int main(int argc, char* argv[]) { ...@@ -40,6 +40,7 @@ int main(int argc, char* argv[]) {
bool diffStrand = false; bool diffStrand = false;
bool ignoreOverlaps = false; bool ignoreOverlaps = false;
bool reportDistance = false; bool reportDistance = false;
bool signDistance = false;
// check to see if we should print out some help // check to see if we should print out some help
...@@ -84,6 +85,10 @@ int main(int argc, char* argv[]) { ...@@ -84,6 +85,10 @@ int main(int argc, char* argv[]) {
else if (PARAMETER_CHECK("-d", 2, parameterLength)) { else if (PARAMETER_CHECK("-d", 2, parameterLength)) {
reportDistance = true; reportDistance = true;
} }
else if (PARAMETER_CHECK("-D", 2, parameterLength)) {
reportDistance = true;
signDistance = true;
}
else if (PARAMETER_CHECK("-io", 3, parameterLength)) { else if (PARAMETER_CHECK("-io", 3, parameterLength)) {
ignoreOverlaps = true; ignoreOverlaps = true;
} }
...@@ -118,7 +123,7 @@ int main(int argc, char* argv[]) { ...@@ -118,7 +123,7 @@ int main(int argc, char* argv[]) {
} }
if (!showHelp) { if (!showHelp) {
BedClosest *bc = new BedClosest(bedAFile, bedBFile, sameStrand, diffStrand, tieMode, reportDistance, ignoreOverlaps); BedClosest *bc = new BedClosest(bedAFile, bedBFile, sameStrand, diffStrand, tieMode, reportDistance, signDistance, ignoreOverlaps);
delete bc; delete bc;
return 0; return 0;
} }
...@@ -151,6 +156,11 @@ void ShowHelp(void) { ...@@ -151,6 +156,11 @@ void ShowHelp(void) {
cerr << "\t-d\t" << "In addition to the closest feature in B, " << endl; cerr << "\t-d\t" << "In addition to the closest feature in B, " << endl;
cerr << "\t\treport its distance to A as an extra column." << endl; cerr << "\t\treport its distance to A as an extra column." << endl;
cerr << "\t\t- The reported distance for overlapping features will be 0." << endl << endl; cerr << "\t\t- The reported distance for overlapping features will be 0." << endl << endl;
cerr << "\t-D\t" << "Like -d, report the closest feature in B, and its distance to A" << endl;
cerr << "\t\tas an extra column. Unlike -d, use negative distances to report" << endl;
cerr << "\t\tif feature B is upstream of A. Note: Upstream B features are" << endl;
cerr << "\"to the right\" of features on the - strand" << endl; << endl;
cerr << "\t-no\t" << "Ignore features in B that overlap A. That is, we want close, but " << endl; cerr << "\t-no\t" << "Ignore features in B that overlap A. That is, we want close, but " << endl;
cerr << "\t\tnot touching features only." << endl << endl; cerr << "\t\tnot touching features only." << endl << endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment