lib/bio/db/rebase.rb in bio-1.0.0 vs lib/bio/db/rebase.rb in bio-1.1.0

- old
+ new

@@ -1,21 +1,35 @@ # -# = bio/db/rebase.rb - Interface for EMBOSS formatted REBASE files +# bio/db/rebase.rb - Interface for EMBOSS formatted REBASE files # -# Copyright:: Copyright (C) 2005 Trevor Wennblom <trevor@corevx.com> -# License:: LGPL +# Author:: Trevor Wennblom <mailto:trevor@corevx.com> +# Copyright:: Copyright (c) 2005-2007 Midwinter Laboratories, LLC (http://midwinterlabs.com) +# License:: The Ruby License # -# $Id: rebase.rb,v 1.3 2006/02/27 13:22:05 k Exp $ +# $Id: rebase.rb,v 1.8 2007/04/05 23:35:40 trevor Exp $ # + +autoload :YAML, 'yaml' + +module Bio #:nodoc: + +autoload :Reference, 'bio/reference' + # -# == Synopsis +# bio/db/rebase.rb - Interface for EMBOSS formatted REBASE files +# +# Author:: Trevor Wennblom <mailto:trevor@corevx.com> +# Copyright:: Copyright (c) 2005-2007 Midwinter Laboratories, LLC (http://midwinterlabs.com) +# License:: The Ruby License +# +# +# = Description # # Bio::REBASE provides utilties for interacting with REBASE data in EMBOSS # format. REBASE is the Restriction Enzyme Database, more information # can be found here: # - # * http://rebase.neb.com # # EMBOSS formatted files located at: # # * http://rebase.neb.com/rebase/rebase.f37.html @@ -28,13 +42,13 @@ # at your shell prompt: # # % wget ftp://ftp.neb.com/pub/rebase/emboss* # # -# == Usage +# = Usage # -# require 'bio/db/rebase' +# require 'bio' # require 'pp' # # enz = File.read('emboss_e') # ref = File.read('emboss_r') # sup = File.read('emboss_s') @@ -63,10 +77,11 @@ # rebase = Bio::REBASE.load_yaml( 'enz.yaml' ) # rebase = Bio::REBASE.load_yaml( 'enz.yaml', 'ref.yaml' ) # rebase = Bio::REBASE.load_yaml( 'enz.yaml', 'ref.yaml', 'sup.yaml' ) # # pp rebase.enzymes[0..4] # ["AarI", "AasI", "AatI", "AatII", "Acc16I"] +# pp rebase.enzyme_name?('aasi') # true # pp rebase['AarI'].pattern # "CACCTGC" # pp rebase['AarI'].blunt? # false # pp rebase['AarI'].organism # "Arthrobacter aurescens SS2-322" # pp rebase['AarI'].source # "A. Janulaitis" # pp rebase['AarI'].primary_strand_cut1 # 11 @@ -90,41 +105,15 @@ # pp rebase.enzymes.size # 673 # # rebase.each do |name, info| # pp "#{name}: #{info.methylation}" unless info.methylation.empty? # end -# -# -#-- # -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -#++ -# -autoload :YAML, 'yaml' - -module Bio - - autoload :Reference, 'reference' - - class REBASE - class DynamicMethod_Hash < Hash + class DynamicMethod_Hash < Hash #:nodoc: # Define a writer or reader # * Allows hash[:kay]= to be accessed like hash.key= # * Allows hash[:key] to be accessed like hash.key def method_missing(method_id, *args) k = self.class @@ -140,38 +129,54 @@ k.instance_method(method_id).bind(self).call end end end - class EnzymeEntry < DynamicMethod_Hash + class EnzymeEntry < DynamicMethod_Hash #:nodoc: @@supplier_data = {} def self.supplier_data=(d); @@supplier_data = d; end def supplier_names ret = [] self.suppliers.each { |s| ret << @@supplier_data[s] } ret end end + # Calls _block_ once for each element in <tt>@data</tt> hash, passing that element as a parameter. + # + # --- + # *Arguments* + # * Accepts a block + # *Returns*:: results of _block_ operations def each - @data.each { |v| yield v } + @data.each { |item| yield item } end # Make the instantiated class act like a Hash on @data # Does the equivalent and more of this: # def []( key ); @data[ key ]; end # def size; @data.size; end - def method_missing(method_id, *args) + def method_missing(method_id, *args) #:nodoc: self.class.class_eval do define_method(method_id) { |a| Hash.instance_method(method_id).bind(@data).call(a) } end Hash.instance_method(method_id).bind(@data).call(*args) end - # All your REBASE are belong to us. + # Constructor + # + # --- + # *Arguments* + # * +enzyme_lines+: (_required_) contents of EMBOSS formatted enzymes file + # * +reference_lines+: (_optional_) contents of EMBOSS formatted references file + # * +supplier_lines+: (_optional_) contents of EMBOSS formatted suppliers files + # * +yaml+: (_optional_, _default_ +false+) enzyme_lines, reference_lines, and supplier_lines are read as YAML if set to true + # *Returns*:: Bio::REBASE def initialize( enzyme_lines, reference_lines = nil, supplier_lines = nil, yaml = false ) + # All your REBASE are belong to us. + if yaml @enzyme_data = enzyme_lines @reference_data = reference_lines @supplier_data = supplier_lines else @@ -183,28 +188,61 @@ EnzymeEntry.supplier_data = @supplier_data setup_enzyme_data end # List the enzymes available + # + # --- + # *Arguments* + # * _none_ + # *Returns*:: +Array+ sorted enzyme names def enzymes @data.keys.sort end + + # Check if supplied name is the name of an available enzyme + # + # --- + # *Arguments* + # * +name+: Enzyme name + # *Returns*:: +true/false+ + def enzyme_name?(name) + enzymes.each do |e| + return true if e.downcase == name.downcase + end + return false + end # Save the current data # rebase.save_yaml( 'enz.yaml' ) # rebase.save_yaml( 'enz.yaml', 'ref.yaml' ) # rebase.save_yaml( 'enz.yaml', 'ref.yaml', 'sup.yaml' ) + # + # --- + # *Arguments* + # * +f_enzyme+: (_required_) Filename to save YAML formatted output of enzyme data + # * +f_reference+: (_optional_) Filename to save YAML formatted output of reference data + # * +f_supplier+: (_optional_) Filename to save YAML formatted output of supplier data + # *Returns*:: nothing def save_yaml( f_enzyme, f_reference=nil, f_supplier=nil ) File.open(f_enzyme, 'w') { |f| f.puts YAML.dump(@enzyme_data) } File.open(f_reference, 'w') { |f| f.puts YAML.dump(@reference_data) } if f_reference File.open(f_supplier, 'w') { |f| f.puts YAML.dump(@supplier_data) } if f_supplier + return end # Read REBASE EMBOSS-formatted files # rebase = Bio::REBASE.read( 'emboss_e' ) # rebase = Bio::REBASE.read( 'emboss_e', 'emboss_r' ) # rebase = Bio::REBASE.read( 'emboss_e', 'emboss_r', 'emboss_s' ) + # + # --- + # *Arguments* + # * +f_enzyme+: (_required_) Filename to read enzyme data + # * +f_reference+: (_optional_) Filename to read reference data + # * +f_supplier+: (_optional_) Filename to read supplier data + # *Returns*:: Bio::REBASE object def self.read( f_enzyme, f_reference=nil, f_supplier=nil ) e = IO.readlines(f_enzyme) r = f_reference ? IO.readlines(f_reference) : nil s = f_supplier ? IO.readlines(f_supplier) : nil self.new(e,r,s) @@ -212,10 +250,17 @@ # Read YAML formatted files # rebase = Bio::REBASE.load_yaml( 'enz.yaml' ) # rebase = Bio::REBASE.load_yaml( 'enz.yaml', 'ref.yaml' ) # rebase = Bio::REBASE.load_yaml( 'enz.yaml', 'ref.yaml', 'sup.yaml' ) + # + # --- + # *Arguments* + # * +f_enzyme+: (_required_) Filename to read YAML-formatted enzyme data + # * +f_reference+: (_optional_) Filename to read YAML-formatted reference data + # * +f_supplier+: (_optional_) Filename to read YAML-formatted supplier data + # *Returns*:: Bio::REBASE object def self.load_yaml( f_enzyme, f_reference=nil, f_supplier=nil ) e = YAML.load_file(f_enzyme) r = f_reference ? YAML.load_file(f_reference) : nil s = f_supplier ? YAML.load_file(f_supplier) : nil self.new(e,r,s,true) @@ -407,7 +452,6 @@ Bio::Reference.new(ref) end end # REBASE - end # Bio