Class: Bio::BigWig

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/bigwig.rb,
lib/bio/bigwig/version.rb,
ext/bio/bigwig/bigwigext.c

Overview

BigWig/BigBed file reader for Ruby

Examples:

Reading a local BigWig file

Bio::BigWig.open("file.bw") do |bw|
  puts bw.header
  puts bw.chroms
end

Reading a remote BigWig file

Bio::BigWig.open("https://example.com/file.bw") do |bw|
  values = bw.values("chr1", 0, 1000)
end

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname, mode = "r") ⇒ BigWig

Initialize a BigWig/BigBed file handle

Parameters:

  • fname (String)

    file path or URL

  • mode (String) (defaults to: "r")

    file mode (default: “r”)



42
43
44
45
46
47
# File 'lib/bio/bigwig.rb', line 42

def initialize(fname, mode = "r")
  raise "BigWig::new() does not take block; use BigWig::open() instead" if block_given?

  @fname = fname
  initialize_raw(fname, mode)
end

Class Method Details

.open(*args, **kwargs) {|BigWig| ... } ⇒ BigWig

Open a BigWig/BigBed file

Parameters:

  • args (Array)

    file path or URL

  • kwargs (Hash)

    options

Yields:

  • (BigWig)

    file handle for block usage

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bio/bigwig.rb', line 26

def self.open(*args, **kwargs)
  file = new(*args, **kwargs)
  return file unless block_given?

  begin
    yield file
  ensure
    file.close
  end
  file
end

Instance Method Details

#chroms(*args) ⇒ Hash, ...

Get chromosome information

Parameters:

  • chrom (String, nil)

    specific chromosome name (optional)

Returns:

  • (Hash, Integer, nil)

    all chromosomes as Hash, or length of specific chromosome, or nil if not found



# File 'lib/bio/bigwig.rb', line 108

#closenil

Close the file handle

Returns:

  • (nil)


# File 'lib/bio/bigwig.rb', line 100

#closed?Boolean

Check if the file is closed

Returns:

  • (Boolean)

    true if closed, false if open



# File 'lib/bio/bigwig.rb', line 104

#entries(chrom, start = 0, stop = -1,, with_string: true) ⇒ Array<Array>

Get entries for a genomic region (BigBed files only)

Parameters:

  • chrom (String)

    chromosome name

  • start (Integer) (defaults to: 0)

    start position (0-based, default: 0)

  • stop (Integer) (defaults to: -1,)

    stop position (0-based, default: -1 for end of chromosome)

  • with_string (Boolean) (defaults to: true)

    include string data (default: true)

Returns:

  • (Array<Array>)

    entries as [start, end, string] or [start, end]



96
97
98
# File 'lib/bio/bigwig.rb', line 96

def entries(chrom, start = 0, stop = -1, with_string: true)
  entries_raw(chrom, start, stop, with_string)
end

#file_typeString

Get file type

Returns:

  • (String)

    “BigWig” or “BigBed”



# File 'lib/bio/bigwig.rb', line 117

#headerHash

Get file header information

Returns:

  • (Hash)

    header information including version, levels, bases_covered, min_val, max_val, sum_data, sum_squared



# File 'lib/bio/bigwig.rb', line 113

#intervals(chrom, start = 0, stop = -1)) ⇒ Array<Array>

Get intervals for a genomic region

Parameters:

  • chrom (String)

    chromosome name

  • start (Integer) (defaults to: 0)

    start position (0-based, default: 0)

  • stop (Integer) (defaults to: -1))

    stop position (0-based, default: -1 for end of chromosome)

Returns:

  • (Array<Array>)

    intervals as [start, end, value]



85
86
87
# File 'lib/bio/bigwig.rb', line 85

def intervals(chrom, start = 0, stop = -1)
  intervals_raw(chrom, start, stop)
end

#is_bigbed?Boolean

0 = BigWig

Returns:

  • (Boolean)


# File 'lib/bio/bigwig.rb', line 125

#is_bigwig?Boolean

Check if file is BigWig format

Returns:

  • (Boolean)

    true if BigWig, false otherwise



# File 'lib/bio/bigwig.rb', line 121

#pathString

Get the file path

Returns:

  • (String)

    file path or URL



52
53
54
# File 'lib/bio/bigwig.rb', line 52

def path
  @fname
end

#sqlString?

Get SQL schema (BigBed files only)

Returns:

  • (String, nil)

    SQL schema string, or nil if not available



# File 'lib/bio/bigwig.rb', line 129

#stats(chrom, start = nil, stop = nil, nbins: nil, type: nil, exact: nil) ⇒ Array<Float>

Get statistics for a genomic region

Parameters:

  • chrom (String)

    chromosome name

  • start (Integer, nil) (defaults to: nil)

    start position (0-based)

  • stop (Integer, nil) (defaults to: nil)

    stop position (0-based)

  • nbins (Integer, nil) (defaults to: nil)

    number of bins

  • type (String, Symbol, nil) (defaults to: nil)

    statistic type (“mean”, “max”, “min”, “sum”, “coverage”, “std”)

  • exact (Boolean, nil) (defaults to: nil)

    use exact calculation

Returns:

  • (Array<Float>)

    statistics values



65
66
67
# File 'lib/bio/bigwig.rb', line 65

def stats(chrom, start = nil, stop = nil, nbins: nil, type: nil, exact: nil)
  stats_raw(chrom, start, stop, nbins, type&.to_s, exact)
end

#values(chrom, start, stop) ⇒ Array<Float>

Get values for a genomic region

Parameters:

  • chrom (String)

    chromosome name

  • start (Integer)

    start position (0-based)

  • stop (Integer)

    stop position (0-based)

Returns:

  • (Array<Float>)

    values



75
76
77
# File 'lib/bio/bigwig.rb', line 75

def values(chrom, start, stop)
  values_raw(chrom, start, stop)
end