Skip to content

Commit

Permalink
Fix bug in IntervalList.getUniqueIntervals that caused missing interv…
Browse files Browse the repository at this point in the history
…al names (#1265)

* Fixes a bug in IntervalList.getUniqueIntervals() where two intervals with identical coordinates didn't correctly keep both interval names in the merged interval.  This was because the intervals were being stored in a Set and Interval doesn't examine name in it's equals() method.  Now both names will be correctly concatenated.
  • Loading branch information
Yossi Farjoun authored and lbergelson committed Jan 24, 2019
1 parent d0b1a74 commit 3c48018
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/htsjdk/samtools/util/IntervalList.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static List<Interval> getUniqueIntervals(final IntervalList list, final b
}

final List<Interval> unique = new ArrayList<>();
final TreeSet<Interval> toBeMerged = new TreeSet<>();
final List<Interval> toBeMerged = new ArrayList<>();
Interval current = null;

for (final Interval next : intervals) {
Expand Down Expand Up @@ -383,11 +383,12 @@ private static List<Interval> breakIntervalAtBandMultiples(final Interval interv
/**
* Merges a sorted collection of intervals and optionally concatenates unique names or takes the first name.
*/
static Interval merge(final SortedSet<Interval> intervals, final boolean concatenateNames) {
final String chrom = intervals.first().getContig();
int start = intervals.first().getStart();
int end = intervals.last().getEnd();
final boolean neg = intervals.first().isNegativeStrand();
static Interval merge(final Iterable<Interval> intervals, final boolean concatenateNames) {
final Interval first = intervals.iterator().next();
final String chrom = first.getContig();
int start = first.getStart();
int end = start;
final boolean neg = first.isNegativeStrand();
final LinkedHashSet<String> names = new LinkedHashSet<>();
final String name;

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/htsjdk/samtools/util/IntervalListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ public void testUnionIntervalLists(final List<IntervalList> lists, final Interva
CollectionUtil.makeCollection(list.iterator()));
}


@Test
public void testUnionSamePosition() {
final IntervalList iList= new IntervalList(fileHeader);

final List<Interval> intervals = Arrays.asList(
new Interval("1", 2, 100, true, "test1"),
new Interval("1", 2, 100, true, "test2")
);
iList.addall(intervals);
final List<Interval> uniqued = iList.uniqued().getIntervals();
Assert.assertEquals(uniqued.size(),1);
Assert.assertEquals(uniqued.get(0).getName(),"test1|test2");
}

@DataProvider(name = "invertData")
public Object[][] invertData() {
final IntervalList invert1 = new IntervalList(fileHeader);
Expand Down

0 comments on commit 3c48018

Please sign in to comment.