Skip to content

Commit

Permalink
rename option. support (b)gzipped input
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Aug 17, 2019
1 parent cf3d9d0 commit 298140e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

[![badge](https://img.shields.io/badge/docs-latest-blue.svg)](https://brentp.github.io/libbigwig-nim/bigwig.html)

## Command Line

bigwig-nim includes a command-line tool distributed as a static binary. It supports converting
bed to bigwig and bigwig to bed and extracting stats (mean, coverage, etc) for regions in a bigwig.

There are other tools to do this, including [kentTools](https://hgwdev.gi.ucsc.edu/~kent/src/) which has a more restrictive license and does not supported (b)gzipped input and [bwtools](https://github.com/CRG-Barcelona/bwtool) which seems to provide similar functionality (but I am not able to build it).


### view

To convert a bed with the value in the 4th column to bigwig, use:

```Shell
bigwig view $bed_in --value-column 4 --chrom-sizes $fai -O bigwig -o $bigwig_out
```
`bigwig` will automatically determine the best data format for each block (fixed span and step or per-base) most of the
CPU time is spent parsing the input bed file.

### stats

To get the mean value for a given region (in this case on chromosome 22)

```Shell
bigwig stats --stat mean $bigwig 22:145000-155000
```

The supported stats are `mean`, `min`, `max`, `coverage`, `sum` with a special-case for the stat of `header` which
shows the chromosomes, lengths and mean coverages for each chromosome in the bigwig file.


## Reading

```Nim
Expand Down
2 changes: 1 addition & 1 deletion bigwig.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = "MIT"

# Dependencies

requires "nimbigwig", "argparse"
requires "nimbigwig", "argparse", "hts >= 0.2.20"
srcDir = "src"
installExt = @["nim"]

Expand Down
12 changes: 6 additions & 6 deletions src/bigwigpkg/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import strutils
import tables
import strformat
import ./lib
import hts/files
import argparse

type region = tuple[chrom: string, start: int, stop: int]
Expand Down Expand Up @@ -54,8 +55,7 @@ iterator chunks(bed_path: string, chrom: var string, n:int=2048, value_column: i
let col = value_column - 1

var cache = newSeqOfCap[tuple[start: int, stop:int, value:float32]](n)
# TODO: support gzipped as well.
for l in bed_path.lines:
for l in bed_path.hts_lines:
let toks = l.strip.split('\t')
if toks[0] != chrom and cache.len > 0:
yield cache
Expand Down Expand Up @@ -215,7 +215,7 @@ proc view_main*() =

var p = newParser("bigwig view"):
option("-r", "--region", help="optional chromosome, or chrom:start-stop region to view")
option("-f", "--fai", help="path to fai, only used for converting BED->BigWig")
option("-c", "--chrom-sizes", help="file indicating chromosome sizes (can be .fai), only used for converting BED->BigWig")
option("-i", "--value-column", help="column-number (1-based) of the value to encode in to BigWig, only used for encoding BED->BigWig", default="4")
option("-O", "--output-fmt", choices= @["bed", "bigwig"], default="bed", help="output format")
option("-o", "--output-file", default="/dev/stdout", help="output bed or bigwig file")
Expand Down Expand Up @@ -283,11 +283,11 @@ proc view_main*() =
ofh.close
bw.close
else:
if opts.fai == "":
quit "[bigwig] --fai is required when input is not bigwig."
if opts.chrom_sizes == "":
quit "[bigwig] --chrom-sizes is required when input is not bigwig."
if opts.region != "":
quit "[bigwig] --region is not supported for BED input"
var h = opts.fai.from_fai
var h = opts.chrom_sizes.from_fai
var ofh: BigWig
if not ofh.open(opts.output_file, fmWrite):
quit "[bigwig] couldn't open output bigwig file:" & opts.output_file
Expand Down

0 comments on commit 298140e

Please sign in to comment.