Class: BioVcf::BedFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-vcf/bedfilter.rb

Instance Method Summary collapse

Constructor Details

#initialize(bedfilen) ⇒ BedFilter

Returns a new instance of BedFilter.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bio-vcf/bedfilter.rb', line 4

def initialize bedfilen
  require 'binary_search/native'

  # Parse Bed file and build up search array
  chrs = {}
  info = {}
  File.open(bedfilen).each_line { | line |
    (chr,start,stop,gene) = line.strip.split(/\t/)[0..3]
    chrs[chr] ||= []
    chrs[chr].push(stop.to_i)
    info[chr+':'+stop] = [chr,start.to_i,stop.to_i,gene]
  }
  # Make sure chrs is sorted
  @chrs = {}
  chrs.each { | k,list |
    @chrs[k] = list.sort
  }
  @info = info
end

Instance Method Details

#contains(rec) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bio-vcf/bedfilter.rb', line 24

def contains(rec)
  stop_list = @chrs[rec.chrom]
  if stop_list
    pos = rec.pos
    stop = stop_list.bsearch { |bedstop| bedstop >= pos }
    if stop
      rinfo = @info[rec.chrom+':'+stop.to_s]
      raise "Unexpected error in BED record for #{rec.chrom}:#{stop} position" if rinfo == nil
      start = rinfo[1]
      if pos >= start
        # p [rec.chrom,rec.pos,rinfo]
        return rinfo
      end
    end
  end
  nil
end