drugbank 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in drugbank.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eric Harrison
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # Drugbank
2
+
3
+ Simple Gem (for now) that allows you to import the drugbank.ca xml
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'drugbank'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install drugbank
18
+
19
+ ## Usage
20
+
21
+ importer = Drugbank::Import.new('http://www.drugbank.ca/system/downloads/current/drugbank.xml.zip')
22
+ importer.import(){|drugs| #do something }
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'drugbank/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "drugbank"
8
+ gem.version = Drugbank::VERSION
9
+ gem.authors = ["Eric Harrison"]
10
+ gem.email = ["eharrison@smashtankapps.com"]
11
+ gem.description = %q{Download the xml from Drugbank.ca and import}
12
+ gem.summary = %q{Importer for Drugbank.ca}
13
+ gem.homepage = "http://github.com/smashtank/drugbank"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "sax_stream", "1.0.3"
21
+ gem.add_dependency "rubyzip", "0.9.9"
22
+ end
Binary file
@@ -0,0 +1,11 @@
1
+ require "drugbank/version"
2
+
3
+ require 'net/http'
4
+
5
+ require 'sax_stream/mapper'
6
+ require 'sax_stream/parser'
7
+
8
+ Dir["#{File.dirname(__FILE__)}/drugbank/*.rb"].each { |f| require(f) }
9
+
10
+ module Drugbank
11
+ end
Binary file
@@ -0,0 +1,9 @@
1
+ require 'drugbank/base'
2
+
3
+ module Drugbank
4
+ class AtcCode < Drugbank::Base
5
+ include SaxStream::Mapper
6
+ node 'atc-code'
7
+ map :code, :to => ''
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module Drugbank
2
+ class Base
3
+ def method_missing(method, *args, &block)
4
+ if self.attributes.keys.include?(method.to_s)
5
+ self.class.send(:define_method, method,lambda{ self[method.to_s]})
6
+ return self[method.to_s]
7
+ elsif self.relations.keys.include?(method.to_s)
8
+ self.class.send(:define_method, method,lambda{ self.relations[method.to_s]})
9
+ return self.relations[method.to_s]
10
+ else
11
+ super
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Brand < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ node 'brand'
6
+ map :name, :to => ''
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Category < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ node 'category'
6
+ map :name, :to => ''
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ module Drugbank
2
+ class Collector
3
+ def initialize(batch_size, &block)
4
+ @batch_size = batch_size
5
+ @counter = 0
6
+ @batch = []
7
+ self.block = block
8
+ end
9
+
10
+ def <<(value)
11
+ @batch << value
12
+ @counter += 1
13
+ collect_batch if @counter % @batch_size == 0
14
+ end
15
+
16
+ def collect_batch
17
+ process_batch unless @batch.empty?
18
+ @batch = []
19
+ puts " #{@counter}"
20
+ end
21
+
22
+ def block=(block)
23
+ @block = block
24
+ end
25
+
26
+ def process_batch
27
+ puts "Processing batch .."
28
+ errors = []
29
+ begin
30
+ block.call(@batch, errors)
31
+ rescue Exception => e
32
+ errors << "Error: BATCH FAILED - #{e.message}"
33
+ ensure
34
+ puts errors.join("\n")
35
+ puts "..done"
36
+ end
37
+ end
38
+
39
+ private
40
+ def block
41
+ @block
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Dosage < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ node 'dosage'
7
+
8
+ map :form, :to => 'form'
9
+ map :route, :to => 'route'
10
+ map :strength, :to => 'strength'
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+ require 'drugbank/base'
2
+ require 'drugbank/secondary_accession_number'
3
+ require 'drugbank/group'
4
+ require 'drugbank/brand'
5
+ require 'drugbank/synonym'
6
+ require 'drugbank/packager'
7
+ require 'drugbank/manufacturer'
8
+ require 'drugbank/price'
9
+ require 'drugbank/category'
10
+ require 'drugbank/dosage'
11
+ require 'drugbank/atc_code'
12
+ require 'drugbank/patent'
13
+ require 'drugbank/drug_interaction'
14
+ require 'drugbank/external_id'
15
+ require 'drugbank/external_link'
16
+
17
+ module Drugbank
18
+ class Drug < Drugbank::Base
19
+ include SaxStream::Mapper
20
+
21
+ node 'drug'
22
+ map :id, :to => 'drugbank-id'
23
+ map :type, :to => '@type'
24
+ map :name, :to => 'name'
25
+ map :description, :to => 'description'
26
+ map :cas_registry_number, :to => 'cas-number'
27
+ map :general_references, :to => 'general-references'
28
+ map :synthesis_references, :to => 'synthesis-references'
29
+ map :indication, :to => 'indication'
30
+ map :pharmacology, :to => 'pharmacology'
31
+ map :mechanism_of_action, :to => 'mechanism-of-action'
32
+ map :toxicity, :to => 'toxicity'
33
+ map :biotransformation, :to => 'biotransformation'
34
+ map :absorption, :to => 'absorption'
35
+ map :half_life, :to => 'half-life'
36
+ map :protein_binding, :to => 'protein-binding'
37
+ map :route_of_elimination, :to => 'route-of-elimination'
38
+ map :volume_of_distribution, :to => 'volume-of-distribution'
39
+ map :clearance, :to => 'clearance'
40
+
41
+ relate :secondary_accession_numbers, :to => 'secondary-accession-numbers/secondary_accession_number', :as => [Drugbank::SecondaryAccessionNumber], :parent_collects => true
42
+ relate :brands, :to => 'brands/brand', :as => [Drugbank::Brand], :parent_collects => true
43
+ relate :groups, :to => 'groups/group', :as => [Drugbank::Group], :parent_collects => true
44
+ relate :synonyms, :to => 'synonyms/synonym', :as => [Drugbank::Synonym], :parent_collects => true
45
+ relate :packagers, :to => 'packagers/packager', :as => [Drugbank::Packager], :parent_collects => true
46
+ relate :manufacturers, :to => 'manufacturers/manufacturer', :as => [Drugbank::Manufacturer], :parent_collects => true
47
+ relate :prices, :to => 'prices/price', :as => [Drugbank::Price], :parent_collects => true
48
+ relate :categories, :to => 'categories/category', :as => [Drugbank::Category], :parent_collects => true
49
+ relate :dosages, :to => 'dosages/dosage', :as => [Drugbank::Dosage], :parent_collects => true
50
+ relate :atc_codes, :to => 'atc-codes/atc-code', :as => [Drugbank::AtcCode], :parent_collects => true
51
+ relate :patents, :to => 'patents/patent', :as => [Drugbank::Patent], :parent_collects => true
52
+ relate :drug_interactions, :to => 'drug-interactions/drug-interaction', :as => [Drugbank::DrugInteraction], :parent_collects => true
53
+ relate :external_ids, :to => 'external-identifiers/external-identifier', :as => [Drugbank::ExternalId], :parent_collects => true
54
+ relate :external_links, :to => 'external-links/external-link', :as => [Drugbank::ExternalLink], :parent_collects => true
55
+
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class DrugInteraction < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ node 'drug-interaction'
7
+ map :interaction_drug_id, :to => 'drug'
8
+ map :name, :to => 'name'
9
+ map :description, :to => 'description'
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'drugbank/base'
2
+ require 'drugbank/drug'
3
+
4
+ module Drugbank
5
+ class Drugs < Drugbank::Base
6
+ include SaxStream::Mapper
7
+ node 'drugs', :collect => false
8
+
9
+ relate :drugs, :to => 'drug', :as => [Drugbank::Drug]
10
+ end
11
+ end
12
+
@@ -0,0 +1,9 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class ExternalId < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ map :resource, :to => 'resource'
7
+ map :id, :to => 'identifier'
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class ExternalLink < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ map :resource, :to => 'resource'
6
+ map :url, :to => 'url'
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Group < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ map :name, :to => ''
7
+ end
8
+ end
@@ -0,0 +1,82 @@
1
+ require 'zip/zip'
2
+ require 'net/http'
3
+ require 'open-uri' #incase we can
4
+ require 'uri'
5
+ require 'tempfile'
6
+ require 'digest/md5'
7
+ require 'zip/zip'
8
+
9
+ require 'drugbank/drugs'
10
+ require 'drugbank/collector'
11
+
12
+ module Drugbank
13
+ class Import
14
+ include ObjectSpace
15
+
16
+ def initialize(url, batch_size = 500)
17
+ @batch_size = batch_size
18
+ @url = url
19
+ end
20
+
21
+ def parser
22
+ @parser ||= SaxStream::Parser.new(collector, [Drugbank::Drugs])
23
+ end
24
+
25
+ def collector
26
+ @collector ||= Drugbank::Collector.new(@batch_size)
27
+ end
28
+
29
+ def import(&block)
30
+ set_collector_block(block)
31
+ parser.parse_stream(fetch_url(@url))
32
+ collector.collect_batch
33
+ end
34
+
35
+ private
36
+ def set_collector_block(block)
37
+ collector.block = block
38
+ end
39
+
40
+ def add_file_for_cleanup(file)
41
+ define_finalizer(self, lambda{|id| file.respond_to?(:unlink) ? file.unlink : File.unlink(file) })
42
+ end
43
+
44
+ def file_name
45
+ "drugbank_import-#{Digest::MD5.hexdigest(@url)}.zip"
46
+ end
47
+
48
+ def fetch_url(url)
49
+ case url.split(/\./).last
50
+ when 'zip'
51
+ zip_file = open(file_name, 'wb')
52
+ add_file_for_cleanup(file_name)
53
+ begin
54
+ uri = URI.parse(url)
55
+ Net::HTTP.start(uri.host, uri.port) do |http|
56
+ http.request_get(uri.request_uri) do |resp|
57
+ resp.read_body do |segment|
58
+ zip_file.write(segment)
59
+ end
60
+ end
61
+ end
62
+ ensure
63
+ zip_file.close()
64
+ end
65
+ #unzip the file first
66
+ Zip::ZipInputStream::open(file_name) do |io|
67
+ while (entry = io.get_next_entry)
68
+ if entry.name.split(/\./).last == 'xml'
69
+ unziped_file = Tempfile.new(Digest::MD5.hexdigest(entry.name))
70
+ add_file_for_cleanup(unziped_file)
71
+ unziped_file.write(io.read)
72
+ unziped_file.close()
73
+ return open(unziped_file.path)
74
+ end
75
+ end
76
+ end
77
+ else
78
+ return open(url)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,9 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Manufacturer < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ map :generic, :to => '@generic'
7
+ map :name, :to => ''
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Packager < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ map :name, :to => 'name'
6
+ map :url, :to => 'url'
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Patent < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ map :number, :to => 'number'
7
+ map :country, :to => 'country'
8
+ map :expires_at, :to => 'expires'
9
+ map :approved_at, :to => 'approved'
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Price < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ map :description, :to => 'description'
6
+ map :cost, :to => 'cost'
7
+ map :currency, :to => 'cost@currency'
8
+ map :unit, :to => 'unit'
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class SecondaryAccessionNumber < Drugbank::Base
4
+ include SaxStream::Mapper
5
+ node 'secondary-accession-number'
6
+
7
+ map :number, :to => ''
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'drugbank/base'
2
+ module Drugbank
3
+ class Synonym < Drugbank::Base
4
+ include SaxStream::Mapper
5
+
6
+ node 'synonym'
7
+ map :name, :to => ''
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Drugbank
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drugbank
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Harrison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sax_stream
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: rubyzip
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.9
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.9
46
+ description: Download the xml from Drugbank.ca and import
47
+ email:
48
+ - eharrison@smashtankapps.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - drugbank.gemspec
59
+ - lib/.DS_Store
60
+ - lib/drugbank.rb
61
+ - lib/drugbank/.DS_Store
62
+ - lib/drugbank/atc_code.rb
63
+ - lib/drugbank/base.rb
64
+ - lib/drugbank/brand.rb
65
+ - lib/drugbank/category.rb
66
+ - lib/drugbank/collector.rb
67
+ - lib/drugbank/dosage.rb
68
+ - lib/drugbank/drug.rb
69
+ - lib/drugbank/drug_interaction.rb
70
+ - lib/drugbank/drugs.rb
71
+ - lib/drugbank/external_id.rb
72
+ - lib/drugbank/external_link.rb
73
+ - lib/drugbank/group.rb
74
+ - lib/drugbank/import.rb
75
+ - lib/drugbank/manufacturer.rb
76
+ - lib/drugbank/packager.rb
77
+ - lib/drugbank/patent.rb
78
+ - lib/drugbank/price.rb
79
+ - lib/drugbank/secondary_accession_number.rb
80
+ - lib/drugbank/synonym.rb
81
+ - lib/drugbank/version.rb
82
+ homepage: http://github.com/smashtank/drugbank
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Importer for Drugbank.ca
106
+ test_files: []