Skip to content

Commit

Permalink
Fix logic searching for features containing a position. Fixes #1541
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Aug 14, 2024
1 parent 1a426ee commit 71e111a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 66 deletions.
37 changes: 4 additions & 33 deletions src/main/java/org/broad/igv/feature/FeatureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,6 @@ public static Feature getFeatureStartsAfter(double position, List<? extends Feat
return null;
}

public static Feature getFeatureEndsBefore(double position, List<? extends Feature> features) {

int index = getIndexBefore(position, features);
Feature prevFeature = null;
while (index >= 0) {
Feature f = features.get(index);
if (f.getEnd() < position) {
if (prevFeature == null) {
prevFeature = f;
} else {
if (f.getStart() == prevFeature.getStart()) {
// Prefer smallest feature
if (f.getEnd() < prevFeature.getEnd()) {
prevFeature = f;
}
} else {
// Done
break;
}
}
}
index--;
}
return prevFeature;

}

/**
* Return the first feature whose center is > the given position. If no features satisfy the criteria
* return null;
Expand Down Expand Up @@ -374,15 +347,13 @@ public static int getIndexBeforeOld(double position, List<? extends Feature> fea
* @param features
* @return
*/
public static List<Feature> getAllFeaturesAt(double position,
double flanking,
List<? extends htsjdk.tribble.Feature> features) {
public static List<Feature> getAllFeaturesContaining(double position,
double flanking,
List<? extends htsjdk.tribble.Feature> features) {

List<Feature> returnList = null;

int startIdx = Math.max(0, getIndexBefore(position, features));
for (int idx = startIdx; idx < features.size(); idx++) {
Feature feature = features.get(idx);
for (Feature feature : features) {
if (feature.getStart() > position + flanking) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/feature/dsi/DSITrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DSITrack(ResourceLocator locator, FeatureSource src) {
@Override
public String getValueStringAt(String chr, double position, int mouseX, int mouseY, ReferenceFrame frame) {

List<Feature> allFeatures = getAllFeatureAt(position, mouseY, frame);
List<Feature> allFeatures = getAllFeaturesContaining(position, mouseY, frame);
if (allFeatures == null) {
return null;
}
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/broad/igv/track/FeatureTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public String getValueStringAt(String chr, double position, int mouseX, int mous

if (showFeatures) {

List<Feature> allFeatures = getAllFeatureAt(position, mouseY, frame);
List<Feature> allFeatures = getAllFeaturesContaining(position, mouseY, frame);
if (allFeatures == null) {
return null;
}
Expand Down Expand Up @@ -495,7 +495,7 @@ public List<Feature> getFeatures(String chr, int start, int end) {
* @param frame
* @return
*/
protected List<Feature> getAllFeatureAt(double position, int y, ReferenceFrame frame) {
protected List<Feature> getAllFeaturesContaining(double position, int y, ReferenceFrame frame) {
// Determine the level number (for expanded tracks)
int featureRow = getFeatureRow(y);
return getFeaturesAtPositionInFeatureRow(position, featureRow, frame);
Expand Down Expand Up @@ -549,19 +549,15 @@ public List<Feature> getFeaturesAtPositionInFeatureRow(double position, int feat

//If features are stacked we look at only the row.
//If they are collapsed on top of each other, we get all features in all rows
List<Feature> possFeatures;
if (getDisplayMode() == DisplayMode.COLLAPSED) {
possFeatures = packedFeatures.getFeatures();
} else {
possFeatures = rows.get(featureRow).getFeatures();
}
List<Feature> possFeatures = rows.get(featureRow).getFeatures();


List<Feature> featureList = null;
if (possFeatures != null) {
// give a minum 2 pixel or 1/2 bp window, otherwise very narrow features will be missed.
double bpPerPixel = frame.getScale();
double flanking = 4 * bpPerPixel;
featureList = FeatureUtils.getAllFeaturesAt(position, flanking, possFeatures);
featureList = FeatureUtils.getAllFeaturesContaining(position, flanking, possFeatures);
}
return featureList;
}
Expand Down Expand Up @@ -631,7 +627,7 @@ public Feature getFeatureAtMousePosition(TrackClickEvent te) {
final ReferenceFrame referenceFrame = te.getFrame();
if (referenceFrame != null) {
double location = referenceFrame.getChromosomePosition(e);
List<Feature> features = getAllFeatureAt(location, e.getY(), referenceFrame);
List<Feature> features = getAllFeaturesContaining(location, e.getY(), referenceFrame);
return (features != null && features.size() > 0) ? features.get(0) : null;
} else {
return null;
Expand Down
23 changes: 1 addition & 22 deletions src/test/java/org/broad/igv/feature/FeatureUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -129,10 +128,8 @@ public void testGetFeatureClosestWithIndels(){
public void testGetAllFeaturesAt() {

int position = 56078756;
int maxLength = 100000;


List<Feature> result = FeatureUtils.getAllFeaturesAt(position, 0, featureList);
List<Feature> result = FeatureUtils.getAllFeaturesContaining(position, 0, featureList);
assertEquals(21, result.size());
for (Feature f : result) {
assertTrue(position >= f.getStart() && position <= f.getEnd());
Expand Down Expand Up @@ -192,24 +189,6 @@ public void testGetFeatureCenterAfter() {
assertNull(f);
}

@Test
public void testGetFeatureBefore() {

// feature
// chr7 56078756 56119137
Feature f = FeatureUtils.getFeatureEndsBefore(56119137 + 1, featureList);
assertEquals(56078756, f.getStart());

// chr7 55086709 55236328
f = FeatureUtils.getFeatureStartsAfter(0, featureList);
assertEquals(55086709, f.getStart());

// last feature
// chr7 56182373 56184110
f = FeatureUtils.getFeatureStartsAfter(56182373, featureList);
assertNull(f);
}

@Test
public void testGetFeatureCenterBefore() {

Expand Down

0 comments on commit 71e111a

Please sign in to comment.