Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related to issue #81 Bed file output using the 5th column (score) for counts #83

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.2.7

+ Improvement #84 Making sure bed file output always outputs count as the socre column (6 column), independently is input bed region has 3, 4 or more columns
duartemolha marked this conversation as resolved.
Show resolved Hide resolved
retaining the extra columns from the input file. (Only score column gets overwritten with counts)

0.2.6 (dev)
===========
+ fix #54. for quantize
Expand Down
42 changes: 41 additions & 1 deletion functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,54 @@ run unordered_bed $exe --by tests/unordered.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed3_n_entries $exe --by tests/bed3.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed4_n_entries $exe --by tests/bed4.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed5_n_entries $exe --by tests/bed5.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed6_n_entries $exe --by tests/bed6.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed12_n_entries $exe --by tests/bed12.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | wc -l) 2

run bed3_n_columns $exe --by tests/bed3.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | awk 'BEGIN{FS="\t"}{print NF}' | sort | uniq) 5

run bed4_n_columns $exe --by tests/bed4.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | awk 'BEGIN{FS="\t"}{print NF}' | sort | uniq) 5

run bed5_n_columns $exe --by tests/bed5.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | awk 'BEGIN{FS="\t"}{print NF}' | sort | uniq) 5

run bed6_n_columns $exe --by tests/bed6.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | awk 'BEGIN{FS="\t"}{print NF}' | sort | uniq) 6

run bed12_n_columns $exe --by tests/bed12.bed t tests/ovl.bam
assert_exit_code 0
assert_equal $(zcat t.regions.bed.gz | awk 'BEGIN{FS="\t"}{print NF}' | sort | uniq) 12

# theres data left in the bam but the region tree is empty...
run missing_bed_chrom $exe --by tests/missing.bed t tests/ovl.bam
assert_exit_code 0

run big_window $exe t tests/ovl.bam --by 100000000
assert_exit_code 0
assert_equal $(zgrep -c "MT" t.per-base.bed.gz) 2
assert_equal "MT 0 16569 0.00" "$(zgrep ^MT t.regions.bed.gz)"
assert_equal "MT 0 16569 MT:0-16569 0.00" "$(zgrep ^MT t.regions.bed.gz)"
duartemolha marked this conversation as resolved.
Show resolved Hide resolved

unset MOSDEPTH_Q0
unset MOSDEPTH_Q1
Expand Down
40 changes: 23 additions & 17 deletions mosdepth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type
start: uint32
stop: uint32
name: string
other_fields: string

coverage_t = seq[int32]

Expand Down Expand Up @@ -174,19 +175,20 @@ iterator regions(bam: hts.Bam, region: region_t, tid: int, targets: seq[hts.Targ
stderr.write_line("[mosdepth]", region.chrom, " not found")

proc bed_line_to_region(line: string): region_t =
var
cse = line.strip().split('\t', 5)

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))
if len(cse) > 3:
reg.name = cse[3]
return reg
var
cse = 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))
if len(cse) > 3:
reg.name = cse[3]
if len(cse) >= 5:
reg.other_fields = join(cse[5..high(cse)], "\t")
return reg

proc region_line_to_region(region: string): region_t =
if region == "" or region == "nil":
Expand Down Expand Up @@ -577,11 +579,15 @@ proc main(bam: hts.Bam, chrom: region_t, mapq: int, eflag: uint16, iflag: uint16
if tid != -2:
me = imean(arr, r.start, r.stop)
var m = su.format_float(me, ffDecimal, precision=precision)

var target_region_start = intToStr(int(r.start))
var target_region_stop = intToStr(int(r.stop))
var target_region_name = r.name
if r.name == "":
line.add(starget & intToStr(int(r.start)) & "\t" & intToStr(int(r.stop)) & "\t" & m)
target_region_name = target.name & ":" & target_region_start & "-" & target_region_stop
if r.other_fields == "":
line.add(starget & target_region_start & "\t" & target_region_stop & "\t" & target_region_name & "\t" & m)
else:
line.add(starget & intToStr(int(r.start)) & "\t" & intToStr(int(r.stop)) & "\t" & r.name & "\t" & m)
line.add(starget & target_region_start & "\t" & target_region_stop & "\t" & target_region_name & "\t" & m & "\t" & r.other_fields)
discard fregion.write_interval(line, target.name, int(r.start), int(r.stop))
line = line[0..<0]
if tid != -2:
Expand Down Expand Up @@ -667,7 +673,7 @@ when(isMainModule):
when not defined(release) and not defined(lto):
stderr.write_line "[mosdepth] WARNING: built in debug mode; will be slow"

let version = "mosdepth 0.2.6"
let version = "mosdepth 0.2.7"
let env_fasta = getEnv("REF_PATH")
let doc = format("""
$version
Expand Down
Empty file modified scripts/install.sh
100755 → 100644
Empty file.
2 changes: 2 additions & 0 deletions tests/bed12.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MT 1 10 MT_region1 0 + 1 10 255,0,0 1 10 0
MT 2 11 MT_region2 0 - 2 10 255,0,0 1 10 0
2 changes: 2 additions & 0 deletions tests/bed3.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MT 1 10
MT 2 11
2 changes: 2 additions & 0 deletions tests/bed4.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MT 1 10 MT_region1
MT 2 11 MT_region2
2 changes: 2 additions & 0 deletions tests/bed5.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MT 1 10 MT_region1 0
MT 2 11 MT_region2 0
2 changes: 2 additions & 0 deletions tests/bed6.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MT 1 10 MT_region1 0 +
MT 2 11 MT_region2 0 -