Skip to content

Commit

Permalink
[depth] fix handling of unsorted bed (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lomereiter committed Jun 18, 2015
1 parent 93e2510 commit 287a1ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
26 changes: 8 additions & 18 deletions sambamba/depth.d
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,18 @@ class GeneralRegionStatsCollector : RegionStatsCollector {
if (bed.length == 0)
return;

trees_.length = bed_.back.ref_id + 1;

size_t start_index = 0;
size_t end_index = start_index;

intervalTreeNode[] intervals;

while (start_index < bed.length) {
while (end_index < bed.length && bed[end_index].ref_id == bed[start_index].ref_id)
++end_index;

intervals.length = end_index - start_index;
foreach (i; 0 .. intervals.length) {
auto start = bed[start_index + i].start;
auto stop = bed[start_index + i].end;
auto value = start_index + i;
intervals[i] = new intervalTreeNode(start, stop, value);
}

trees_[bed[start_index].ref_id] = new intervalTree(intervals);
start_index = end_index;
intervalTreeNode[][int] intervals;
foreach (i, line; bed) {
auto node = new intervalTreeNode(line.start, line.end, i);
intervals[line.ref_id] ~= node;
}

trees_.length = reduce!max(intervals.keys) + 1;
foreach (ref_id, ivs; intervals)
trees_[ref_id] = new intervalTree(ivs);
}

override void nextColumn(uint ref_id, pos_t position,
Expand Down
3 changes: 2 additions & 1 deletion sambamba/utils/common/bed.d
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ BamRegion[] parseBed(Reader)(string bed_filename, Reader bam, bool non_overlappi
regions ~= BamRegion(cast(uint)id,
cast(uint)interval.beg, cast(uint)interval.end);
}
std.algorithm.sort(regions);
if (bed_lines is null)
std.algorithm.sort(regions);
return regions;
}

0 comments on commit 287a1ea

Please sign in to comment.