Commit df95985d authored by Aaron Quinlan's avatar Aaron Quinlan
Browse files

Merge pull request #13 from jakebiesinger/master

Add ignore options for features up- and downstream of A 
parents 6357a50b 4cbfb094
......@@ -23,7 +23,7 @@ const int SLOPGROWTH = 2048000;
*/
BedClosest::BedClosest(string &bedAFile, string &bedBFile, bool sameStrand, bool diffStrand,
string &tieMode, bool reportDistance, bool signDistance, string &_strandedDistMode,
bool ignoreOverlaps, bool printHeader)
bool ignoreOverlaps, bool ignoreUpstream, bool ignoreDownstream, bool printHeader)
: _bedAFile(bedAFile)
, _bedBFile(bedBFile)
, _tieMode(tieMode)
......@@ -33,6 +33,8 @@ BedClosest::BedClosest(string &bedAFile, string &bedBFile, bool sameStrand, bool
, _signDistance(signDistance)
, _strandedDistMode(_strandedDistMode)
, _ignoreOverlaps(ignoreOverlaps)
, _ignoreUpstream(ignoreUpstream)
, _ignoreDownstream(ignoreDownstream)
, _printHeader(printHeader)
{
_bedA = new BedFile(_bedAFile);
......@@ -113,7 +115,18 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
if ((_strandedDistMode == "ref")
|| (_strandedDistMode == "a" && a.strand != "-")
|| (_strandedDistMode == "b" && h->strand == "-")) {
curDistance = -curDistance;
// hit is "upstream" of A
if (_ignoreUpstream) {
numOverlaps--;
continue;
}
else {
curDistance = -curDistance;
}
}
else if (_ignoreDownstream) {
numOverlaps--;
continue;
}
}
......@@ -137,7 +150,18 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
if (_signDistance) {
if ((_strandedDistMode == "a" && a.strand == "-")
|| (_strandedDistMode == "b" && h->strand != "-")) {
curDistance = -curDistance;
// hit is "upstream" of A
if (_ignoreUpstream) {
numOverlaps--;
continue;
}
else{
curDistance = -curDistance;
}
}
else if (_ignoreDownstream){
numOverlaps--;
continue;
}
}
if (abs(curDistance) < minDistance) {
......@@ -173,7 +197,7 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
// 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") {
if (closestB.size() == 1 || (_tieMode == "first" && closestB.size() > 0)) {
_bedA->reportBedTab(a);
if (_reportDistance == true) {
_bedB->reportBedTab(closestB[0]);
......@@ -195,7 +219,7 @@ void BedClosest::FindWindowOverlaps(BED &a, vector<BED> &hits) {
_bedB->reportBedNewLine(*b);
}
}
else if (_tieMode == "last") {
else if (_tieMode == "last" && closestB.size() > 0) {
_bedA->reportBedTab(a);
if (_reportDistance == true) {
_bedB->reportBedTab(closestB[closestB.size()-1]);
......
......@@ -30,7 +30,8 @@ public:
BedClosest(string &bedAFile, string &bedBFile,
bool sameStrand, bool diffStrand, string &tieMode,
bool reportDistance, bool signDistance, string &strandedDistMode,
bool ignoreOverlaps, bool printHeader);
bool ignoreOverlaps, bool ignoreUpstream, bool ignoreDownstream,
bool printHeader);
// destructor
~BedClosest(void);
......@@ -50,6 +51,8 @@ private:
bool _signDistance;
string _strandedDistMode;
bool _ignoreOverlaps;
bool _ignoreUpstream;
bool _ignoreDownstream;
bool _printHeader;
BedFile *_bedA, *_bedB;
......
......@@ -40,6 +40,8 @@ int closest_main(int argc, char* argv[]) {
bool sameStrand = false;
bool diffStrand = false;
bool ignoreOverlaps = false;
bool ignoreUpstream = false;
bool ignoreDownstream = false;
bool reportDistance = false;
bool signDistance = false;
bool haveStrandedDistMode = false;
......@@ -100,6 +102,12 @@ int closest_main(int argc, char* argv[]) {
else if (PARAMETER_CHECK("-io", 3, parameterLength)) {
ignoreOverlaps = true;
}
else if (PARAMETER_CHECK("-iu", 3, parameterLength)) {
ignoreUpstream = true;
}
else if (PARAMETER_CHECK("-id", 3, parameterLength)) {
ignoreDownstream = true;
}
else if (PARAMETER_CHECK("-t", 2, parameterLength)) {
if ((i+1) < argc) {
haveTieMode = true;
......@@ -139,10 +147,22 @@ int closest_main(int argc, char* argv[]) {
showHelp = true;
}
if (ignoreUpstream && ignoreDownstream) {
cerr << endl << "*****" << endl << "*****ERROR: Request either -iu OR -id, not both." << endl << "*****" << endl;
showHelp = true;
}
if ((ignoreUpstream || ignoreDownstream) && ! haveStrandedDistMode) {
cerr << endl << "*****" << endl << "*****ERROR: When requesting -iu or -id, you also need to specify -D." << endl << "*****" << endl;
showHelp = true;
}
if (!showHelp) {
BedClosest *bc = new BedClosest(bedAFile, bedBFile, sameStrand,
diffStrand, tieMode, reportDistance,
signDistance, strandedDistMode, ignoreOverlaps, printHeader);
signDistance, strandedDistMode, ignoreOverlaps,
ignoreUpstream, ignoreDownstream, printHeader);
delete bc;
}
else {
......@@ -188,6 +208,13 @@ void closest_help(void) {
cerr << "\t-io\t" << "Ignore features in B that overlap A. That is, we want close," << endl;
cerr << "\t\tyet not touching features only." << endl << endl;
cerr << "\t-iu\t" << "Ignore features in B that are upstream of features in A." << endl;
cerr << "\t\tThis option requires -D and follows its orientation" << endl;
cerr << "\t\trules for determining what is \"upstream\"." << endl;
cerr << "\t-id\t" << "Ignore features in B that are upstream of features in A." << endl;
cerr << "\t\tThis option requires -D and follows its orientation" << endl;
cerr << "\t\trules for determining what is \"downstream\"." << endl;
cerr << "\t-t\t" << "How ties for closest feature are handled. This occurs when two" << endl;
cerr << "\t\tfeatures in B have exactly the same \"closeness\" with A." << endl;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment