summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2015-11-04 17:50:32 +0100
committerChristoph Helma <helma@in-silico.ch>2015-11-04 17:50:32 +0100
commitc23f4b8a915c0df4f34b6c3787829e1f513df571 (patch)
tree298faed0b20077615357a6bc0a4b6d7e61bd8a16
parentca2bb0f90335b1f2c4ecc28ee423e85b281ffcf0 (diff)
parent2081bda2b72f34758847fe699fecf890dae1e3df (diff)
Merge branch 'development' of github.com:opentox/lazar into development
-rw-r--r--lib/compound.rb32
-rw-r--r--lib/dataset.rb2
-rw-r--r--lib/rest-client-wrapper.rb8
-rw-r--r--test/compound.rb14
4 files changed, 51 insertions, 5 deletions
diff --git a/lib/compound.rb b/lib/compound.rb
index c5e7f02..ad0eaba 100644
--- a/lib/compound.rb
+++ b/lib/compound.rb
@@ -21,6 +21,7 @@ module OpenTox
field :png_id, type: BSON::ObjectId
field :svg_id, type: BSON::ObjectId
field :sdf_id, type: BSON::ObjectId
+ field :molecular_weight, type: Float
field :fingerprints, type: Hash, default: {}
field :default_fingerprint_size, type: Integer
field :dataset_ids, type: Array, default: []
@@ -335,6 +336,37 @@ module OpenTox
#$mongo["compounds"].aggregate(aggregate).collect{ |r| [r["_id"], r["tanimoto"]] }
end
+
+ # Get mg from logmmol (for nch LOAEL/pTD50 data)
+ # @return [Float] value in mg
+ def logmmol_to_mg(value, mw)
+ mg = (10**(-1.0*value.to_f)*(mw.to_f*1000))
+ return mg
+ end
+
+ # Get mg from mmol
+ # @return [Float] value in mg
+ def mmol_to_mg(value, mw)
+ mg = (value.to_f)*(mw.to_f)
+ return mg
+ end
+
+ # Get mg from logmg
+ # @return [Float] value in mg
+ def logmg_to_mg(value)
+ mg = 10**value.to_f
+ return mg
+ end
+
+ # Calculate molecular weight of Compound with OB and store it in object
+ # @return [Float] molecular weight
+ def molecular_weight
+ if self["molecular_weight"]==0.0 || self["molecular_weight"].nil?
+ update(:molecular_weight => OpenTox::Algorithm::Descriptor.physchem(self, ["Openbabel.MW"]).first)
+ end
+ self["molecular_weight"]
+ end
+
private
diff --git a/lib/dataset.rb b/lib/dataset.rb
index af116a9..366c79f 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -173,7 +173,7 @@ module OpenTox
$logger.debug "Skipping import of #{file}, it is already in the database (id: #{dataset.id})."
else
$logger.debug "Parsing #{file}."
- table = CSV.read file, :skip_blanks => true
+ table = CSV.read file, :skip_blanks => true, :encoding => 'windows-1251:utf-8'
dataset = self.new(:source => source, :name => name)
dataset.parse_table table, bioassay
end
diff --git a/lib/rest-client-wrapper.rb b/lib/rest-client-wrapper.rb
index de1b74f..6b5d602 100644
--- a/lib/rest-client-wrapper.rb
+++ b/lib/rest-client-wrapper.rb
@@ -26,15 +26,15 @@ module OpenTox
define_singleton_method method do |uri,payload={},headers={},waiting_task=nil|
# check input
- bad_request_error "Headers are not a hash: #{headers.inspect}", uri unless headers==nil or headers.is_a?(Hash)
+ bad_request_error "Headers are not a hash: #{headers.inspect} for #{uri}." unless headers==nil or headers.is_a?(Hash)
headers[:subjectid] ||= @@subjectid
- bad_request_error "Invalid URI: '#{uri}'", uri unless URI.valid? uri
+ bad_request_error "Invalid URI: '#{uri}'" unless URI.valid? uri
#resource_not_found_error "URI '#{uri}' not found.", uri unless URI.accessible?(uri, @subjectid) unless URI.ssl?(uri)
# make sure that no header parameters are set in the payload
[:accept,:content_type,:subjectid].each do |header|
if defined? $aa || URI(uri).host == URI($aa[:uri]).host
else
- bad_request_error "#{header} should be submitted in the headers", uri if payload and payload.is_a?(Hash) and payload[header]
+ bad_request_error "#{header} should be submitted in the headers of URI: #{uri}" if payload and payload.is_a?(Hash) and payload[header]
end
end
@@ -72,7 +72,7 @@ module OpenTox
msg = "Could not parse error response from rest call '#{method}' to '#{uri}':\n#{response}"
cause = nil
end
- Object.method(error[:method]).call msg, uri, cause # call error method
+ Object.method(error[:method]).call "#{msg}, #{uri}, #{cause}" # call error method
else
response
end
diff --git a/test/compound.rb b/test/compound.rb
index ff20c1c..50cc5aa 100644
--- a/test/compound.rb
+++ b/test/compound.rb
@@ -184,4 +184,18 @@ print c.sdf
#assert_equal neighbors, neighbors2
end
end
+
+ def test_molecular_weight
+ c = OpenTox::Compound.from_smiles "CC(=O)CC(C)C"
+ assert_equal 100.15888, c.molecular_weight
+ end
+
+ def test_mg_conversions
+ c = OpenTox::Compound.from_smiles "O"
+ mw = c.molecular_weight
+ assert_equal 18.01528, mw
+ assert_equal 0.8105107141417474, c.logmmol_to_mg(4.34688225631145, mw)
+ assert_equal 9007.64, c.mmol_to_mg(500, mw)
+ assert_equal 2437.9999984148976, c.logmg_to_mg(3.387033701)
+ end
end