Browse files

Updated BioChEMBL::ChEMBLID.

  • Loading branch information...
1 parent f609ba0 commit c3b0a5d460874d10380f45c16873481b1b0f3aa4 @nakao committed Jul 23, 2012
Showing with 89 additions and 41 deletions.
  1. +41 −33 lib/bio-chembl/chemblid.rb
  2. +9 −3 lib/bio-chembl/rest_api_server.rb
  3. +1 −0 lib/bio-chembl/rest_client.rb
  4. +38 −5 test/test_bio-chembl-chemblid.rb
View
74 lib/bio-chembl/chemblid.rb
@@ -9,7 +9,7 @@
module BioChEMBL
- # ChEMBL ID Utility
+ # ChEMBL ID utility String
#
# Format
#
@@ -22,49 +22,51 @@ module BioChEMBL
# chemblId.is_target? #=> false
# compound = chemblId.resolve
#
+ # BioChEMBL::ChEMBLID.validate("CHEMBL1")
+ #
class ChEMBLID < String
attr_accessor :data_type
- # ChEMBL ID Validater
- def self.validate_chemblId(str)
- validate_chemblId(ste)
+
+ # Checking the format of the ChEMBL ID
+ def self.valid_format?(str)
+ begin
+ self.new(str)
+ true
+ rescue Exception
+ false
+ end
end
# ChEMBL ID Validater
def validate_chemblId(str)
- unless str =~ /^CHEMBL\d+$/
+ if str =~ /^CHEMBL\d+$/
+ true
+ else
raise Exception, "Invalid ChEMBL ID, '#{str}'"
end
end
-
+ private :validate_chemblId
+
def initialize(str)
@data_type = nil
validate_chemblId(str)
super(str)
end
- # Get the data of the ChEMBL ID
+ # Get the data of the ChEMBL ID (Slow)
def resolve
- case @data_type
- when Compound
- Compound.find(self.to_s)
- when Target
- Target.find(self.to_s)
- when Assay
- Assay.find(self.to_s)
+ if @data_type == Compound
+ return Compound.find(self.to_s)
+ elsif @data_type == Target
+ return Target.find(self.to_s)
+ elsif @data_type == Assay
+ return Assay.find(self.to_s)
else
- begin
- is_compound?
- rescue
- end
- begin
- is_target?
- rescue
- end
- begin
- is_assay?
- rescue
+ if is_compound?
+ elsif is_target?
+ elsif is_assay?
end
if @data_type == nil
raise ArgumentError, "This ChEMBL ID is not exist, #{self.to_s}"
@@ -74,43 +76,49 @@ def resolve
end
end
+
# Is the ChEMBL ID of Compound ?
def is_compound?
if @data_type == Compound
return true
else
- if Compound.find(self.to_s)
+ begin
+ Compound.find(self)
@data_type = Compound
return true
- else
+ rescue Exception
return false
end
end
end
+
# Is the ChEMBL ID of Target ?
def is_target?
- if @data_type == Assay
+ if @data_type == Target
return true
else
- if Assay.find(self.to_s)
- @data_type = Assay
+ begin
+ Target.find(self)
+ @data_type = Target
return true
- else
+ rescue Exception
return false
end
end
end
+
# Is the ChEMBL ID of Assay ?
def is_assay?
if @data_type == Assay
return true
else
- if Assay.find(self.to_s)
+ begin
+ Assay.find(self)
@data_type = Assay
return true
- else
+ rescue Exception
return false
end
end
View
12 lib/bio-chembl/rest_api_server.rb
@@ -5,11 +5,17 @@
module BioChEMBL
class REST
- # serv = BioChEMBL::REST::Server.new
- #
+
+ # Local API server
+ #
+ # Usage
+ #
+ # serv = BioChEMBL::REST::Server.new(:hostname => 'localhost', :port => 3306)
+ # serv.start
+ #
class Server
def initialize
-
+ raise NotImplementedError
end
# serv.query(:status)
View
1 lib/bio-chembl/rest_client.rb
@@ -206,6 +206,7 @@ def get(url)
easy
end
+ # HTTP responce handling
def prepare_return_value(response)
if @debug then
$stderr.puts "ChEMBL: #{response.inspect}"
View
43 test/test_bio-chembl-chemblid.rb
@@ -17,20 +17,53 @@ def test_chemblid
def test_invalid_chembl_id
assert_raise(Exception) {
- BioChEMBL::CHEMBLID.new("CHEMBLCHEMBL1")
+ BioChEMBL::ChEMBLID.new("CHEMBLCHEMBL1")
}
end
+ def test_chemblid_validate
+ assert_equal(true, BioChEMBL::ChEMBLID.valid_format?(@str))
+ assert_equal(false, BioChEMBL::ChEMBLID.valid_format?("CHEMBLCHEMBL1"))
+ end
+
+ def test_resolve
+ compound = @chemblid.resolve
+ assert_equal(BioChEMBL::Compound, @chemblid.data_type)
+ assert_equal(@chemblid, compound.chemblId)
+ assert_equal("COc1ccc2[C@@H]3[C@H](COc2c1)C(C)(C)OC4=C3C(=O)C(=O)C5=C4OC(C)(C)[C@@H]6COc7cc(OC)ccc7[C@H]56", compound.smiles)
+ end
+
+ def test_resolve_target
+ target = BioChEMBL::ChEMBLID.new("CHEMBL1785").resolve
+ assert_equal(BioChEMBL::Target, target.class)
+ assert_equal("CHEMBL1785", target.chemblId)
+ assert_equal("PROTEIN", target.targetType)
+ end
+
+ def test_resolve_assay
+ assay = BioChEMBL::ChEMBLID.new("CHEMBL1217643").resolve
+ assert_equal(BioChEMBL::Assay, assay.class)
+ assert_equal("CHEMBL1217643", assay.chemblId)
+ assert_equal("B", assay.assayType)
+ end
+
+ def test_resolve_invalid
+ assert_raise(ArgumentError) {
+ dummy = BioChEMBL::ChEMBLID.new("CHEMBL11111111111111217643").resolve
+ }
+ end
+
+
def test_chemblid_is_compound?
- assert_equal(@chemblid.is_compound?, true)
- assert_equal(@chemblid.data_type, Compound)
+ assert_equal(true, @chemblid.is_compound?)
+ assert_equal(BioChEMBL::Compound, @chemblid.data_type)
end
def test_chemblid_is_target?
- assert_equal(@chemblid.is_target?, false)
+ assert_equal(false, @chemblid.is_target?)
end
def test_chemblid_is_assay?
- assert_equal(@chemblid.is_assay?, false)
+ assert_equal(false, @chemblid.is_assay?)
end
end

0 comments on commit c3b0a5d

Please sign in to comment.