Skip to content

Commit

Permalink
handle bed files with track headers or incorrect numbers of Lines
Browse files Browse the repository at this point in the history
also print a warning when possible.

closes #8
  • Loading branch information
brentp committed Sep 8, 2017
1 parent 7a5ded0 commit 46d7b84
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ assert_exit_code 0
assert_in_stdout "MT"
assert_equal "2" "$(cat t.dist | wc -l)"

run track_header $exe --by tests/track.bed tests/ovl.bam
assert_exit_code 0
assert_equal "$(cat $STDOUT_FILE)" "MT 2 80 1.00"

run track_header $exe --by tests/bad.bed tests/ovl.bam
assert_exit_code 1
assert_in_stderr "skipping bad bed line:MT 2"
assert_in_stderr "invalid integer: asdf"




test -e $bam || exit

run short $exe -c chrM $bam
Expand All @@ -36,3 +48,4 @@ assert_exit_code 0
run flag $exe -c chrM -F 4 --by 20000 /data/human/NA12878.subset.bam
assert_exit_code 0


11 changes: 10 additions & 1 deletion mosdepth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ iterator regions(bam: hts.Bam, region: region_t, tid: int, targets: seq[hts.Targ
proc bed_line_to_region(line: string): region_t =
var
cse = sequtils.to_seq(line.strip().split("\t"))

if len(cse) < 3:
stderr.write_line("[mosdepth] skipping bad bed line:", line.strip())
return nil

var
s = S.parse_int(cse[1])
e = S.parse_int(cse[2])
reg = region_t(chrom: cse[0], start: uint32(s), stop: uint32(e))
Expand Down Expand Up @@ -252,6 +258,8 @@ iterator bed_gen(bed: string): region_t =
kstr.m = 0
kstr.s = nil
while hts_getline(hf, cint(10), addr kstr) > 0:
if ($kstr.s).startswith("track "):
continue
yield bed_line_to_region($kstr.s)

hts.free(kstr.s)
Expand Down Expand Up @@ -340,6 +348,7 @@ proc window_main(bam: hts.Bam, chrom: region_t, mapq: int, eflag: uint16, args:
distribution = new_seq[int32](1000)

for r in region_gen($args["--by"], sub_targets):
if r == nil: continue
if chrom != nil and r.chrom != chrom.chrom: continue
# the firs time seeing the chrom, we fill the coverage array for
# the entire chrom.
Expand Down Expand Up @@ -401,7 +410,7 @@ Other options:
-h --help show help
"""

let args = docopt(doc, version = "mosdepth 0.1.6")
let args = docopt(doc, version = "mosdepth 0.1.7")
let mapq = S.parse_int($args["--mapq"])
var window_based = false
if $args["--by"] != "nil":
Expand Down
2 changes: 1 addition & 1 deletion mosdepth.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.6"
version = "0.1.7"
author = "Brent Pedersen"
description = "fast depth"
license = "MIT"
Expand Down
3 changes: 3 additions & 0 deletions tests/bad.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MT 2
bad asdf asdf
track name='Gene panel' description='Regions of interest'
2 changes: 2 additions & 0 deletions tests/track.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
track name='Gene panel' description='Regions of interest'
MT 2 80

0 comments on commit 46d7b84

Please sign in to comment.