1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
#
# 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
#
# $Id:$
#
require 'yaml'
require 'bio/reference'
module Bio
#
# 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
#
# These files are the same as the "emboss_?.???" files located at:
#
# * ftp://ftp.neb.com/pub/rebase/
#
# To easily get started with the data you can simply type this command
# at your shell prompt:
#
# % wget "ftp://ftp.neb.com/pub/rebase/emboss_*"
#
#
# = Usage
#
# require 'bio'
# require 'pp'
#
# enz = File.read('emboss_e')
# ref = File.read('emboss_r')
# sup = File.read('emboss_s')
#
# # When creating a new instance of Bio::REBASE
# # the contents of the enzyme file must be passed.
# # The references and suppiers file contents
# # may also be passed.
# rebase = Bio::REBASE.new( enz )
# rebase = Bio::REBASE.new( enz, ref )
# rebase = Bio::REBASE.new( enz, ref, sup )
#
# # The 'read' class method allows you to read in files
# # that are REBASE EMBOSS formatted
# rebase = Bio::REBASE.read( 'emboss_e' )
# rebase = Bio::REBASE.read( 'emboss_e', 'emboss_r' )
# rebase = Bio::REBASE.read( 'emboss_e', 'emboss_r', 'emboss_s' )
#
# # The data loaded may be saved in YAML format
# rebase.save_yaml( 'enz.yaml' )
# rebase.save_yaml( 'enz.yaml', 'ref.yaml' )
# rebase.save_yaml( 'enz.yaml', 'ref.yaml', 'sup.yaml' )
#
# # YAML formatted files can also be read with the
# # class method 'load_yaml'
# 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
# pp rebase['AarI'].primary_strand_cut2 # 0
# pp rebase['AarI'].complementary_strand_cut1 # 15
# pp rebase['AarI'].complementary_strand_cut2 # 0
# pp rebase['AarI'].suppliers # ["F"]
# pp rebase['AarI'].supplier_names # ["Fermentas International Inc."]
#
# pp rebase['AarI'].isoschizomers # Currently none stored in the references file
# pp rebase['AarI'].methylation # ""
#
# pp rebase['EcoRII'].methylation # "2(5)"
# pp rebase['EcoRII'].suppliers # ["F", "J", "M", "O", "S"]
# pp rebase['EcoRII'].supplier_names # ["Fermentas International Inc.", "Nippon Gene Co., Ltd.",
# # "Roche Applied Science", "Toyobo Biochemicals",
# # "Sigma Chemical Corporation"]
#
# # Number of enzymes in the database
# pp rebase.size # 673
# pp rebase.enzymes.size # 673
#
# rebase.each do |name, info|
# pp "#{name}: #{info.methylation}" unless info.methylation.empty?
# end
#
class REBASE
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
if method_id.to_s[-1].chr == '='
k.class_eval do
define_method(method_id) { |s| self[ method_id.to_s[0..-2].to_sym ] = s }
end
k.instance_method(method_id).bind(self).call(args[0])
else
k.class_eval do
define_method(method_id) { self[method_id] }
end
k.instance_method(method_id).bind(self).call
end
end
end
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 { |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) #: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
# 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
@enzyme_data = parse_enzymes(enzyme_lines)
@reference_data = parse_references(reference_lines)
@supplier_data = parse_suppliers(supplier_lines)
end
EnzymeEntry.supplier_data = @supplier_data
setup_enzyme_data
end
# List the enzymes available
#
# ---
# *Arguments*
# * _none_
# *Returns*:: +Array+ sorted enzyme names
def enzymes
@enzyme_names
end
# Check if supplied name is the name of an available enzyme
#
# ---
# *Arguments*
# * +name+: Enzyme name
# *Returns*:: +true/false+
def enzyme_name?(name)
@enzyme_names_downcased.include?(name.downcase)
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)
end
# 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)
end
#########
protected
#########
def setup_enzyme_data
@data = {}
@enzyme_data.each do |name, hash|
@data[name] = EnzymeEntry.new
d = @data[name]
d.pattern = hash[:pattern]
# d.blunt?= is a syntax error
d[:blunt?] = (hash[:blunt].to_i == 1 ? true : false)
d.primary_strand_cut1 = hash[:c1].to_i
d.complementary_strand_cut1 = hash[:c2].to_i
d.primary_strand_cut2 = hash[:c3].to_i
d.complementary_strand_cut2 = hash[:c4].to_i
# Set up keys just in case there's no reference data supplied
[:organism, :isoschizomers,
:methylation, :source].each { |k| d[k] = '' }
d.suppliers = []
d.references = []
end
@enzyme_names = @data.keys.sort
@enzyme_names_downcased = @enzyme_names.map{|a| a.downcase}
setup_enzyme_and_reference_association
end
def setup_enzyme_and_reference_association
return unless @reference_data
@reference_data.each do |name, hash|
d = @data[name]
[:organism, :isoschizomers,
:methylation, :source].each { |k| d[k] = hash[k] }
d.suppliers = hash[:suppliers].split('')
d.references = []
hash[:references].each { |k| d.references << raw_to_reference(k) }
end
end
# data is a hash indexed by the :name of each entry which is also a hash
# * data[enzyme_name] has the following keys:
# :name, :pattern, :len, :ncuts, :blunt, :c1, :c2, :c3, :c4
# :c1 => First 5' cut
# :c2 => First 3' cut
# :c3 => Second 5' cut
# :c4 => Seocnd 3' cut
def parse_enzymes( lines )
data = {}
return data if lines == nil
lines.each_line do |line|
next if line[0].chr == '#'
line.chomp!
a = line.split("\s")
data[ a[0] ] = {
:name => a[0],
:pattern => a[1],
:len => a[2],
:ncuts => a[3],
:blunt => a[4],
:c1 => a[5],
:c2 => a[6],
:c3 => a[7],
:c4 => a[8]
}
end # lines.each
data
end
# data is a hash indexed by the :name of each entry which is also a hash
# * data[enzyme_name] has the following keys:
# :organism, :isoschizomers, :references, :source, :methylation, :suppliers, :name, :number_of_references
def parse_references( lines )
data = {}
return data if lines == nil
index = 1
h = {}
references_left = 0
lines.each_line do |line|
next if line[0].chr == '#' # Comment
next if line[0..1] == '//' # End of entry marker
line.chomp!
if (1..7).include?( index )
h[index] = line
references_left = h[index].to_i if index == 7
index += 1
next
end
if index == 8
h[index] ||= []
h[index] << line
references_left -= 1
end
if references_left == 0
data[ h[1] ] = {
:name => h[1],
:organism => h[2],
:isoschizomers => h[3],
:methylation => h[4],
:source => h[5],
:suppliers => h[6],
:number_of_references => h[7],
:references => h[8]
}
index = 1
h = {}
end
end # lines.each
data
end
# data is a hash indexed by the supplier code
# data[supplier_code]
# returns the suppliers name
def parse_suppliers( lines )
data = {}
return data if lines == nil
lines.each_line do |line|
next if line[0].chr == '#'
data[$1] = $2 if line =~ %r{(.+?)\s(.+)}
end
data
end
# Takes a string in one of the three formats listed below and returns a
# Bio::Reference object
# * Possible input styles:
# a = 'Inagaki, K., Hikita, T., Yanagidani, S., Nomura, Y., Kishimoto, N., Tano, T., Tanaka, H., (1993) Biosci. Biotechnol. Biochem., vol. 57, pp. 1716-1721.'
# b = 'Nekrasiene, D., Lapcinskaja, S., Kiuduliene, L., Vitkute, J., Janulaitis, A., Unpublished observations.'
# c = "Grigaite, R., Maneliene, Z., Janulaitis, A., (2002) Nucleic Acids Res., vol. 30."
def raw_to_reference( line )
a = line.split(', ')
if a[-1] == 'Unpublished observations.'
title = a.pop.chop
pages = volume = year = journal = ''
else
title = ''
pages_or_volume = a.pop.chop
if pages_or_volume =~ %r{pp\.\s}
pages = pages_or_volume
pages.gsub!('pp. ', '')
volume = a.pop
else
pages = ''
volume = pages_or_volume
end
volume.gsub!('vol. ', '')
year_and_journal = a.pop
year_and_journal =~ %r{\((\d+)\)\s(.+)}
year = $1
journal = $2
end
authors = []
last_name = nil
a.each do |e|
if last_name
authors << "#{last_name}, #{e}"
last_name = nil
else
last_name = e
end
end
ref = {
'title' => title,
'pages' => pages,
'volume' => volume,
'year' => year,
'journal' => journal,
'authors' => authors,
}
Bio::Reference.new(ref)
end
end # REBASE
end # Bio
|