Class: Bio::Ucsc::BigWig

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/ucsc/big_wig.rb

Overview

The BigWig class interacts with bigWig files

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f = nil, opts = {}) ⇒ BigWig

Returns a new BigWig.



26
27
28
29
# File 'lib/bio/ucsc/big_wig.rb', line 26

def initialize(f=nil, opts={})
  @filename = f
  return self
end

Instance Attribute Details

#bbi_fileObject

pointer to bbiFile



20
21
22
# File 'lib/bio/ucsc/big_wig.rb', line 20

def bbi_file
  @bbi_file
end

#filenameObject

bigWig file name



18
19
20
# File 'lib/bio/ucsc/big_wig.rb', line 18

def filename
  @filename
end

Class Method Details

.open(*args) ⇒ Object

convenience method to create a new BigWig and open it.



22
23
24
# File 'lib/bio/ucsc/big_wig.rb', line 22

def self.open(*args)
  self.new(*args).open
end

Instance Method Details

#bases_covered(opts = {}) ⇒ Object

Total bases containing actual data



89
90
91
92
# File 'lib/bio/ucsc/big_wig.rb', line 89

def bases_covered(opts={})
  bwf,bbi_sum = prepare_bwf(opts)
  return bbi_sum[:validCount]
end

#chrom_length(chrom = nil) ⇒ Object

Returns size of given chromosome or the sum of all chromosomes



94
95
96
# File 'lib/bio/ucsc/big_wig.rb', line 94

def chrom_length(chrom=nil)
  chrom.nil? ? chrom_list.inject(0){|sum,chrom|sum+=chrom[:size]} : Binding::bbiChromSize(bbi_file,chrom)
end

#closeObject

closes the file



39
40
41
42
# File 'lib/bio/ucsc/big_wig.rb', line 39

def close
  Binding::bbiFileClose(bbi_file) if bbi_file
  @bbi_file = nil
end

#coverage(chrom = nil, opts = {}) ⇒ Object

Percent of bases in region containing actual data



53
54
55
56
57
58
59
60
# File 'lib/bio/ucsc/big_wig.rb', line 53

def coverage(chrom=nil,opts={})
  if(chrom)
    self.summary(chrom,0,self.chrom_length(chrom),1,{type:'coverage'}).first
  else
    bwf,bbi_sum = prepare_bwf(opts)          
    return bbi_sum[:validCount] / chrom_length.to_f
  end
end

#info(opts = {}) ⇒ Object

prints details about the file:

  • minMax/m => Only output the minimum and maximum values

  • zooms/z => Display zoom level details

  • chroms/c => Display chrom details

  • udcDir/u => /dir/to/cache - place to put cache for remote bigBed/bigWigs



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bio/ucsc/big_wig.rb', line 102

def info(opts={})
  min_max =opts[:m] ||= opts[:minMax]
  zooms =opts[:z] ||= opts[:zooms]
  chroms =opts[:c] ||= opts[:chroms]
  bwf,bbi_sum = prepare_bwf(opts)
  # print min/max
  if(min_max)
    printf "%f %f\n", bbi_sum[:minVal], bbi_sum[:maxVal]
    return
  end
  # start summary        
  printf "version: %d\n", bwf[:version]
  printf "isCompressed: %s\n", (bwf[:uncompressBufSize] > 0 ? "yes" : "no")
  printf "isSwapped: %i\n", bwf[:isSwapped] ? 1 : 0
  printf "primaryDataSize: %i\n",bwf[:unzoomedIndexOffset] - bwf[:unzoomedDataOffset]
  unless(bwf[:levelList].null?)
    list = Binding::BbiZoomLevel.new(bwf[:levelList])
    printf "primaryIndexSize: %i\n", list[:dataOffset] - bwf[:unzoomedIndexOffset]
  end
  # print zoom level details
  printf "zoomLevels: %d\n", bwf[:zoomLevels]
  if(zooms)
    zoom = Binding::BbiZoomLevel.new(bwf[:levelList])
    while !zoom.null?
      printf "\t%d\t%d\n", zoom[:reductionLevel], zoom[:indexOffset] - zoom[:dataOffset]
      zoom = zoom[:next]
    end
  end
  # print chrom details
  
  printf "chromCount: %d\n", chrom_list.size
  if(chroms)
    chrom_list.each do |chrom|
      printf "\t%s %d %d\n", chrom[:name], chrom[:id], chrom[:size]
    end
  end
  # finish summary
  printf "basesCovered: %i\n", bbi_sum[:validCount]
  printf "mean: %f\n", bbi_sum[:sumData]/bbi_sum[:validCount]
  printf "min: %f\n", bbi_sum[:minVal]
  printf "max: %f\n", bbi_sum[:maxVal]
  printf "std: %f\n", Binding::calcStdFromSums(bbi_sum[:sumData], bbi_sum[:sumSquares], bbi_sum[:validCount])
  return 
end

#max(chrom = nil, opts = {}) ⇒ Object

Returns the maximum value of items



71
72
73
74
75
76
77
78
# File 'lib/bio/ucsc/big_wig.rb', line 71

def max(chrom=nil,opts={})
  if(chrom)
    self.summary(chrom,0,self.chrom_length(chrom),1,{type:'max'}).first
  else
    bwf,bbi_sum = prepare_bwf(opts)
    return bbi_sum[:maxVal]
  end
end

#mean(chrom = nil, opts = {}) ⇒ Object

Returns the mean value of items



80
81
82
83
84
85
86
87
# File 'lib/bio/ucsc/big_wig.rb', line 80

def mean(chrom=nil,opts={})
  if(chrom)
    self.summary(chrom,0,self.chrom_length(chrom),1,{type:'mean'}).first
  else
    bwf,bbi_sum = prepare_bwf(opts)
    return bbi_sum[:sumData]/bbi_sum[:validCount].to_f
  end
end

#min(chrom = nil, opts = {}) ⇒ Object

Returns the minimum value of items



62
63
64
65
66
67
68
69
# File 'lib/bio/ucsc/big_wig.rb', line 62

def min(chrom=nil,opts={})
  if(chrom)
    self.summary(chrom,0,self.chrom_length(chrom),1,{type:'min'}).first
  else
    bwf,bbi_sum = prepare_bwf(opts)
    return bbi_sum[:minVal]
  end
end

#openObject

opens the file

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/bio/ucsc/big_wig.rb', line 31

def open
  raise ArgumentError, "filename undefined" unless filename
  raise NameError, "#{filename} not found" unless File.exist?(filename)
  raise LoadError, "#{filename} bad format" unless Binding::isBigWig(filename)
  @bbi_file = Binding::bigWigFileOpen(filename)
  return self
end

#smooth(out_file, opts = {}) ⇒ Object

creates a new smoothed bigWig file at the supplied location. Smoothing options:

  • chrom => restrict smoothing to a given chromosome

  • cutoff => probe count cutoff

  • window => rolling window size

  • type => smoothing algorithm [avg]

    • ‘avg’ - average depth in window

    • ‘probe’ - count of regions (probes) crossing ‘cutoff’ in window

Big Wig options:

  • :blockSize => Number of items to bundle in r-tree [256]

  • :itemsPerSlot => Number of data points bundled at lowest level [1024]

  • :unc => If set, do not use compression

  • :udcDir => /dir/to/cache - place to put cache for remote bigBed/bigWigs



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bio/ucsc/big_wig.rb', line 183

def smooth(out_file,opts={})
  verb = opts[:v] || 0
  window = opts[:window] || 250
  cutoff = opts[:cutoff] || self.mean
  block_size = opts[:block_size]||256
  chrom = opts[:chrom]||nil
  items_per_slot = opts[:items_per_slot]||1024
  unc = opts[:unc]||false
  do_compress = !unc
  type = opts[:type]||'avg'
  udc_dir = opts[:u] ||= opts[:udcDir] ||= Binding::udcDefaultDir()
  Binding::bigWigFileSmooth(filename, chrom, block_size, items_per_slot, do_compress, window, verb, out_file, type, cutoff)
end

#std_dev(chrom = nil, opts = {}) ⇒ Object

returns the caclulated standard deviation



44
45
46
47
48
49
50
51
# File 'lib/bio/ucsc/big_wig.rb', line 44

def std_dev(chrom=nil,opts={})
  if(chrom)
    self.summary(chrom,0,self.chrom_length(chrom),1,{type:'std'}).first
  else
    bwf,bbi_sum = prepare_bwf(opts)
    return Binding::calcStdFromSums(bbi_sum[:sumData], bbi_sum[:sumSquares], bbi_sum[:validCount])
  end
end

#summary(chrom, start, stop, count, opts = {}) ⇒ Object

retrieves summary information from the bigWig for the given range.

  • chrom => Sequence name for summary

  • start => Start of range (0 based)

  • stop => End of range

  • count => Number of datapoints to compute (1 for simple summary)

hash Options:

  • :udcDir - /dir/to/cache - place to put cache for remote bigBed/bigWigs

  • :type => Summary type string

    • mean - average value in region (default)

    • min - minimum value in region

    • max - maximum value in region

    • std - standard deviation in region

    • coverage - %% of region that is covered



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bio/ucsc/big_wig.rb', line 159

def summary(chrom, start, stop, count, opts={})
  type = opts[:type] || opts[:t] || 'mean'
  udc_dir = opts[:u] ||= opts[:udcDir] ||= Binding::udcDefaultDir()
  Binding::udcSetDefaultDir(udc_dir)
  # allocate the array
  summaryValues = FFI::MemoryPointer.new(:double,count)
  # initialize to all 'NaN'
  summaryValues.write_array_of_type(:double,:write_string,["NaN"]*count)
  # fill in with Summary Data
  Binding::bigWigSummaryArray(bbi_file, chrom, start, stop, Binding::bbiSummaryTypeFromString(type),count,summaryValues)
  return summaryValues.read_array_of_double(count)
end