Sha256: c1834c35fdcba6f76236a4c9ce40564bb728ce4a06a6ce8a044647ef12167ad7

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

module BioVcf

  class BedFilter
    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

    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
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bio-vcf-0.9.5 lib/bio-vcf/bedfilter.rb
bio-vcf-0.9.4 lib/bio-vcf/bedfilter.rb
bio-vcf-0.9.2 lib/bio-vcf/bedfilter.rb
bio-vcf-0.9.0 lib/bio-vcf/bedfilter.rb
bio-vcf-0.8.2 lib/bio-vcf/bedfilter.rb
bio-vcf-0.8.1 lib/bio-vcf/bedfilter.rb