summaryrefslogtreecommitdiff
path: root/lib/import.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2016-04-08 13:05:52 +0200
committerChristoph Helma <helma@in-silico.ch>2016-04-08 13:05:52 +0200
commit515e644423998a94f07be06bf6460bcf4f96f968 (patch)
treefa04192def8f43862490c7d6e596f311a3c78af3 /lib/import.rb
parentf3780d7507092b643216054fa3ca1e6146281e43 (diff)
enm import test
Diffstat (limited to 'lib/import.rb')
-rw-r--r--lib/import.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/import.rb b/lib/import.rb
new file mode 100644
index 0000000..86c633a
--- /dev/null
+++ b/lib/import.rb
@@ -0,0 +1,77 @@
+module OpenTox
+
+ module Import
+
+ class Enanomapper
+ include OpenTox
+
+ def self.import
+ #get list of bundle URIs
+ bundles = JSON.parse(RestClientWrapper.get('https://data.enanomapper.net/bundle?media=application%2Fjson'))["dataset"]
+ bundles.each do |bundle|
+ uri = bundle["URI"]
+ nanoparticles = JSON.parse(RestClientWrapper.get(bundle["dataset"]+"?media=application%2Fjson"))["dataEntry"]
+ features = JSON.parse(RestClientWrapper.get(bundle["property"]+"?media=application%2Fjson"))["feature"]
+ nanoparticles.each do |np|
+ nanoparticle = Nanoparticle.find_or_create_by(
+ :name => np["values"]["https://data.enanomapper.net/identifier/name"],
+ :source => np["compound"]["URI"],
+ )
+ nanoparticle.bundles << uri
+ np["composition"].each do |comp|
+ case comp["relation"]
+ when "HAS_CORE"
+ nanoparticle.core = comp["component"]["compound"]["URI"]
+ when "HAS_COATING"
+ nanoparticle.coating << comp["component"]["compound"]["URI"]
+ end
+ end if np["composition"]
+ np["values"].each do |u,v|
+ if u.match(/property/)
+ name, unit, source = nil
+ features.each do |uri,feat|
+ if u.match(/#{uri}/)
+ name = feat["title"]
+ unit = feat["units"]
+ source = uri
+ end
+ end
+ feature = Feature.find_or_create_by(
+ :name => name,
+ :unit => unit,
+ :source => source
+ )
+ end
+ v.each{|value| nanoparticle.parse_ambit_value feature, value} if v.is_a? Array
+ end
+ nanoparticle.bundles.uniq!
+ nanoparticle.physchem_descriptors.each{|f,v| v.uniq!}
+ nanoparticle.toxicities.each{|f,v| v.uniq!}
+ nanoparticle.save!
+ end
+ end
+
+ def self.dump
+ #get list of bundle URIs
+ `wget 'https://data.enanomapper.net/bundle?media=application%2Fjson' -O bundles.json`
+ json = JSON.parse File.read('./bundles.json')
+ json["dataset"].each do |dataset|
+ uri = dataset["URI"]
+ id = uri.split("/").last
+ `wget --header='accept:application/json' '#{uri}' -O 'bundle#{id}'`
+ `wget --header='accept:application/json' '#{dataset["summary"]}' -O 'summary#{id}.json'`
+ `wget --header='accept:application/json' '#{dataset["compound"]}' -O 'compound#{id}.json'`
+ `wget --header='accept:application/json' '#{dataset["substance"]}' -O 'substance#{id}.json'`
+ `wget --header='accept:application/json' '#{dataset["property"]}' -O 'property#{id}.json'`
+ `wget --header='accept:application/json' '#{dataset["dataset"]}' -O 'dataset#{id}.json'`
+ `wget --header='accept:application/json' '#{dataset["matrix"]}' -O 'matrix#{id}.json'`
+ end
+ end
+ end
+
+ end
+
+ end
+
+end
+