Skip to content

Commit

Permalink
Relax implicit assumption in FeatureCache that features are grouped b…
Browse files Browse the repository at this point in the history
…y chromosome.
  • Loading branch information
jrobinso committed Jan 3, 2025
1 parent 8a80a57 commit 3a9ef80
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/broad/igv/feature/FeatureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package org.broad.igv.feature;

import com.google.common.base.Predicate;
import htsjdk.samtools.util.Locatable;
import htsjdk.tribble.Feature;

import java.util.*;
Expand All @@ -49,7 +50,7 @@ public static Predicate<Feature> getOverlapPredicate(final String chr, final int
/**
* Sort the feature list by ascending start value
*/
public static void sortFeatureList(List<? extends Feature> features) {
public static void sortFeatureList(List<? extends Locatable> features) {
Collections.sort(features, FEATURE_START_COMPARATOR);
}

Expand Down Expand Up @@ -375,7 +376,7 @@ public static List<Feature> getAllFeaturesContaining(double position,
return returnList;
}

public static final Comparator<Feature> FEATURE_START_COMPARATOR = (o1, o2) -> o1.getStart() - o2.getStart();
public static final Comparator<Locatable> FEATURE_START_COMPARATOR = (o1, o2) -> o1.getStart() - o2.getStart();
public static final Comparator<Feature> FEATURE_END_COMPARATOR = (o1, o2) -> o1.getEnd() - o2.getEnd();
public static final Comparator<Feature> FEATURE_CENTER_COMPARATOR = (o1, o2) -> o1.getStart() - o2.getStart() + o1.getEnd() - o2.getEnd();

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/broad/igv/util/FeatureCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FeatureCache(List<T> features, int batchSize) {
public List<T> getFeatures(String chr, int start, int end) {
List<T> features = new ArrayList<>();
IntervalTree<List<T>> tree = featureMap.get(chr);
if(tree != null) {
if (tree != null) {
List<Interval<List<T>>> intervals = tree.findOverlapping(start, end);
for (Interval<List<T>> interval : intervals) {
List<T> intervalFeatures = interval.getValue();
Expand All @@ -38,6 +38,10 @@ public List<T> getFeatures(String chr, int start, int end) {
}

private void init(List<T> features, int batchSize) {

// The feature list must be grouped by chromosome
Collections.sort(features, Comparator.comparing(Locatable::getContig));

String lastChr = null;

List<T> currentFeatureList = new ArrayList<>();
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/org/broad/igv/util/DataInputStreamWrapper.java

This file was deleted.

36 changes: 36 additions & 0 deletions src/test/java/org/broad/igv/util/FeatureCacheTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.broad.igv.util;

import org.broad.igv.bedpe.BedPEFeature;
import org.broad.igv.bedpe.BedPEParser;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class FeatureCacheTest {

@Test
public void testGetFeatures() throws Exception {

String p = TestUtils.DATA_DIR + "bedpe/interleaved_chrs.bedpe";

List<BedPEFeature> features = BedPEParser.parse(new ResourceLocator(p), null).features;

//chr5:135,300,000-135,399,999
String chr = "chr5";
int start = 135300000 - 1;
int end = 135399999;

// Manually count overlaps
int count = 0;
for (BedPEFeature f : features) {
if (chr.equals(f.getChr()) && f.getEnd() >= start && f.getStart() <= end) count++;
}

FeatureCache<BedPEFeature> cache = new FeatureCache<>(features);
List<BedPEFeature> subset = cache.getFeatures(chr, start, end);
assertEquals(count, subset.size());
}

}
78 changes: 78 additions & 0 deletions test/data/bedpe/interleaved_chrs.bedpe
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
chr5 135399049 135399149 chr5 135398825 135398922 VH01587:178:2223JJ7NX:1:1102:64658:6900 255 - +
chr5 135386568 135386668 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:1113:42124:15819 255 + -
chr5 135318574 135318616 chr5 135318574 135318616 VH01587:178:2223JJ7NX:1:1207:65121:56177 255 - +
chr5 135386572 135386671 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:1105:76010:20827 255 + -
chr5 135386572 135386673 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:1106:50269:28323 255 + -
chr5 135386571 135386671 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1203:34612:45526 255 + -
chr5 135340960 135340997 chr5 135454955 135454986 VH01587:178:2223JJ7NX:1:1204:22694:11994 255 + -
chr5 135386571 135386671 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1315:61056:22440 255 + -
chr5 135386572 135386673 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:1315:78360:27053 255 + -
chr5 135386572 135386673 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:1212:43564:5219 255 + -
chr5 135386572 135386673 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:1212:6934:19112 255 + -
chr5 135349855 135349954 chr5 135349888 135349986 VH01587:178:2223JJ7NX:1:1316:63869:22131 255 + -
chr5 135318936 135318976 chr5 135318936 135318976 VH01587:178:2223JJ7NX:1:1414:28679:11634 255 - +
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1301:39380:24875 255 + -
chr5 135386572 135386673 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:1301:24357:45491 255 + -
chr5 135386572 135386671 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1406:40735:21256 255 + -
chr5 135376747 135378498 chr5 135376778 135378527 VH01587:178:2223JJ7NX:1:1409:18887:21976 255 + -
chr5 135386572 135386673 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:1409:72855:27894 255 + -
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1504:57112:1823 255 + -
chr5 135378511 135378811 chr5 135378584 135378881 VH01587:178:2223JJ7NX:1:1505:62394:41752 255 + -
chr5 135386571 135386668 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1516:48263:36881 255 + -
chr5 135399109 135400136 chr5 135398865 135399075 VH01587:178:2223JJ7NX:1:1506:18646:35663 255 - +
chr5 135311209 135311309 chr5 135311253 135311350 VH01587:178:2223JJ7NX:1:1605:6865:31839 255 + -
chr5 135338649 135338746 chr5 135338725 135338822 VH01587:178:2223JJ7NX:1:1503:20842:32851 255 + -
chr5 135386568 135386668 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:2106:60113:35458 255 + -
chr5 135386572 135386673 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:2106:44524:54376 255 + -
chr5 135386553 135386654 chr5 135386576 135386671 VH01587:178:2223JJ7NX:1:2107:33309:6351 255 + -
chr5 135311207 135311305 chr5 135311236 135311334 VH01587:178:2223JJ7NX:1:1604:38745:3487 255 + -
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1612:66407:36710 255 + -
chr5 135386569 135386668 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:1615:10809:21719 255 + -
chr5 135386568 135386668 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1606:12627:23606 255 + -
chr5 135386568 135386668 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:1606:13519:23709 255 + -
chr5 135258456 135258545 chr5 135311267 135311350 VH01587:178:2223JJ7NX:1:2110:19092:13298 255 + -
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:2205:14565:7398 255 + -
chr5 135378506 135378805 chr5 135378567 135378862 VH01587:178:2223JJ7NX:1:2104:34235:30775 255 + -
chr5 135318625 135318658 chr5 135318574 135318658 VH01587:178:2223JJ7NX:1:2113:54780:8375 255 - +
chr5 135386568 135386668 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:2113:55174:27739 255 + -
chr5_GL456354v1_random 139739 139837 chr5_GL456354v1_random 178521 178615 VH01587:178:2223JJ7NX:1:2309:25575:4310 255 + -
chr5_GL456354v1_random 12993 13041 chr5_GL456354v1_random 12993 13041 VH01587:178:2223JJ7NX:1:1207:78068:27414 255 - +
chr5_GL456354v1_random 12999 13051 chr5_GL456354v1_random 12999 13051 VH01587:178:2223JJ7NX:1:2115:40666:55251 255 - +
chr5_GL456354v1_random 48531 48577 chr5_GL456354v1_random 48531 48577 VH01587:178:2223JJ7NX:1:2212:64383:30055 255 - +
chr5_GL456354v1_random 12994 13051 chr5_GL456354v1_random 12994 13051 VH01587:178:2223JJ7NX:1:2215:68773:37293 255 - +
chr5_GL456354v1_random 12998 13043 chr5_GL456354v1_random 12998 13043 VH01587:180:22255M7NX:1:2408:35813:23057 255 - +
chr5_GL456354v1_random 116075 116110 chr5_GL456354v1_random 116075 116110 VH01587:180:22255M7NX:2:1212:47800:24035 255 - +
chr5_GL456354v1_random 116075 116110 chr5_GL456354v1_random 116075 116110 VH01587:180:22255M7NX:2:1212:47817:24052 255 - +
chr5_GL456354v1_random 116075 116110 chr5_GL456354v1_random 116075 116110 VH01587:180:22255M7NX:2:1212:48589:24686 255 - +
chr5_GL456354v1_random 13000 13041 chr5_GL456354v1_random 13000 13041 VH01587:180:22255M7NX:2:2507:8254:21565 255 - +
chr5 135258456 135258545 chr5 135311267 135311350 VH01587:178:2223JJ7NX:1:2202:23808:53861 255 + -
chr5 135386568 135386668 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:2214:64349:4499 255 + -
chr5 135373537 135374957 chr5 135375026 135375124 VH01587:178:2223JJ7NX:1:2215:15045:22182 255 + -
chr5 135386568 135386668 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:2201:43701:45594 255 + -
chr5 135386568 135386668 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:2202:26175:2201 255 + -
chr5 135386573 135386671 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:2211:61262:46006 255 + -
chr5 135328512 135328611 chr5 135328578 135328671 VH01587:178:2223JJ7NX:1:2207:55431:17397 255 + -
chr5 135386574 135386675 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:2312:31217:16059 255 + -
chr5 135386568 135386668 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:2209:53545:22199 255 + -
chr5 135345904 135346004 chr5 135345930 135346023 VH01587:178:2223JJ7NX:1:2312:24803:44085 255 + -
chr5 135349855 135349954 chr5 135349888 135349986 VH01587:178:2223JJ7NX:1:2316:72529:20399 255 + -
chr5 135258452 135258545 chr5 135311264 135311350 VH01587:178:2223JJ7NX:1:2512:19178:9404 255 + -
chr5 135311209 135311309 chr5 135311253 135311350 VH01587:178:2223JJ7NX:1:2512:6436:21565 255 + -
chr5 135318518 135318554 chr5 135318958 135318984 VH01587:178:2223JJ7NX:1:2505:34441:17363 255 + -
chr5 135327845 135327945 chr5 135327884 135327982 VH01587:178:2223JJ7NX:1:2614:47148:9181 255 + -
chr5 135348373 135348473 chr5 135348400 135348498 VH01587:178:2223JJ7NX:1:2614:38694:49059 255 + -
chr5 135386568 135386668 chr5 135386578 135386676 VH01587:178:2223JJ7NX:1:2616:37510:33365 255 + -
chr5 135318920 135318958 chr5 135318920 135318958 VH01587:178:2223JJ7NX:2:1101:16554:20227 255 - +
chr5 135386572 135386673 chr5 135386581 135386679 VH01587:178:2223JJ7NX:1:2514:45056:3864 255 + -
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:1:2607:29948:3195 255 + -
chr5 135375009 135375106 chr5 135376760 135378508 VH01587:178:2223JJ7NX:2:1102:59821:12389 255 + -
chr5 135400144 135400245 chr5 135399136 135400161 VH01587:178:2223JJ7NX:1:2516:50304:43450 255 - +
chr5 135386572 135386673 chr5 135386582 135386680 VH01587:178:2223JJ7NX:2:1211:41849:51906 255 + -
chr5 135324650 135324751 chr5 135324782 135324880 VH01587:178:2223JJ7NX:2:1212:40014:7500 255 + -
chr5 135376732 135378484 chr5 135376779 135378528 VH01587:178:2223JJ7NX:2:1109:15199:38391 255 + -
chr5 135318779 135318815 chr5 135318779 135318815 VH01587:178:2223JJ7NX:2:1205:28353:4447 255 - +
chr5 135386572 135386673 chr5 135386578 135386676 VH01587:178:2223JJ7NX:2:1115:19984:19987 255 + -
chr5 135386587 135386626 chr5 135386587 135386626 VH01587:178:2223JJ7NX:2:1113:54865:4825 255 - +
chr5 135366591 135366688 chr5 135366629 135366726 VH01587:178:2223JJ7NX:2:1114:57489:12080 255 + -
chr5 135318595 135318639 chr5 135318595 135318639 VH01587:178:2223JJ7NX:2:1213:45502:19541 255 - +

0 comments on commit 3a9ef80

Please sign in to comment.