lib/bio-ucsc/hg18/gap.rb in bio-ucsc-api-0.1.0 vs lib/bio-ucsc/hg18/gap.rb in bio-ucsc-api-0.2.0

- old
+ new

@@ -1,49 +1,79 @@ -# -*- coding: utf-8 -*- # = hg18/gap.rb # Copyright:: # Copyright (C) 2011 MISHIMA, Hiroyuki <missy at be.to / hmishima at nagasaki-u.ac.jp> # License:: The Ruby licence (Ryby's / GPLv2 dual) # -# = Table description in UCSC Table Browser This track depicts gaps in -# the assembly. These gaps - with the exception of intractable -# heterochromatic gaps - will be closed during the finishing process. -# -# = ommitted dynamic method(s) due to the method name collision -# none -# -# = Note -# In the hg18 database, the Gap table is actually separated -# into "chr1_gap", "chr2_gap", etc. The Gap class dynamically -# define Gap::Chr1_Gap, Gap::Chr2_Gap, etc. The -# Gap.find_by_interval calls an appropreate class automatically. +# In the hg18 database, this table is actually separated +# into "chr1_*", "chr2_*", etc. This class dynamically +# define *::Chr1_*, *::Chr2_*, etc. The +# Rmsk.find_by_interval calls an appropreate class automatically. module Bio module Ucsc module Hg18 + class Gap - %w( -Chr1 Chr2 Chr3 Chr4 Chr5 Chr6 Chr7 Chr8 Chr9 -Chr10 Chr11 Chr12 Chr13 Chr14 Chr15 Chr16 Chr17 Chr18 Chr19 -Chr20 Chr21 Chr22 ).each do |chr| - klass = Class.new(DBConnection) do - extend Bio::Ucsc::Hg18::QueryUsingChromBin - set_table_name "#{chr.downcase}_gap" - set_primary_key nil - columns_hash.delete("type") - end - self.const_set("#{chr}_Gap", klass) - end + Bio::Ucsc::Hg18::CHROMS.each do |chr| + class_eval %! + class #{chr[0..0].upcase + chr[1..-1]}_Gap < DBConnection + set_table_name "#{chr[0..0].downcase + chr[1..-1]}_gap" + set_primary_key nil + set_inheritance_column nil + def self.find_by_interval(interval, opt = {:partial => true}) + find_first_or_all_by_interval(interval, :first, opt) + end + + def self.find_all_by_interval(interval, opt = {:partial => true}) + find_first_or_all_by_interval(interval, :all, opt) + end + + def self.find_first_or_all_by_interval(interval, first_all, opt) + zstart = interval.zero_start + zend = interval.zero_end + if opt[:partial] == true + where = <<-SQL + chrom = :chrom +AND bin in (:bins) +AND ((chromStart BETWEEN :zstart AND :zend) + OR (chromEnd BETWEEN :zstart AND :zend) + OR (chromStart <= :zstart AND chromEnd >= :zend)) + SQL + else + where = <<-SQL + chrom = :chrom +AND bin in (:bins) +AND ((chromStart BETWEEN :zstart AND :zend) +AND (chromEnd BETWEEN :zstart AND :zend)) + SQL + end + cond = { + :chrom => interval.chrom, + :bins => Bio::Ucsc::UcscBin.bin_all(zstart, zend), + :zstart => zstart, + :zend => zend, + } + self.find(first_all, + :select => "*", + :conditions => [where, cond],) + end + end + ! + end # each chromosome + def self.find_by_interval(interval, opt = {:partial => true}) - chr_klass = self.const_get("#{interval.chrom.capitalize}_Gap") + chrom = interval.chrom[0..0].upcase + interval.chrom[1..-1] + chr_klass = self.const_get("#{chrom}_Gap") chr_klass.__send__(:find_by_interval, interval, opt) end def self.find_all_by_interval(interval, opt = {:partial => true}) - chr_klass = self.const_get("#{interval.chrom.capitalize}_Gap") + chrom = interval.chrom[0..0].upcase + interval.chrom[1..-1] + chr_klass = self.const_get("#{chrom}_Gap") chr_klass.__send__(:find_all_by_interval, interval, opt) end - end - end - end -end + end # class + + end # module Hg18 + end # module Ucsc +end # module Bio