lazar-rest 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ # Get all substances
2
+ get "/substance/?" do
3
+ substances = Substance.all
4
+ case @accept
5
+ when "text/uri-list"
6
+ uri_list = substances.collect{|substance| uri("/substance/#{substance.id}")}
7
+ return uri_list.join("\n") + "\n"
8
+ when "application/json"
9
+ substances = JSON.parse substances.to_json
10
+ substances.each_index do |idx|
11
+ substances[idx][:URI] = uri("/substance/#{substances[idx]["_id"]["$oid"]}")
12
+ end
13
+ return substances.to_json
14
+ else
15
+ bad_request_error "Mime type #{@accept} is not supported."
16
+ end
17
+ end
18
+
19
+ # Get a substance
20
+ get "/substance/:id/?" do
21
+ substance = Substance.find :id => params[:id]
22
+ resource_not_found_error "Substance with id: #{params[:id]} not found." unless substance
23
+ substance[:URI] = uri("/substance/#{substance.id}")
24
+ return substance.to_json
25
+ end
@@ -0,0 +1,71 @@
1
+ # All available validation types
2
+ VALIDATION_TYPES = ["repeatedcrossvalidation", "leaveoneout", "crossvalidation", "regressioncrossvalidation"]
3
+
4
+ # Get a list of ayll possible validation types
5
+ # @param [Header] Accept one of text/uri-list, application/json
6
+ # @return [text/uri-list] URI list of all validation types
7
+ get "/validation/?" do
8
+ uri_list = VALIDATION_TYPES.collect{|validationtype| uri("/validation/#{validationtype}")}
9
+ case @accept
10
+ when "text/uri-list"
11
+ return uri_list.join("\n") + "\n"
12
+ when "application/json"
13
+ return uri_list.to_json
14
+ else
15
+ bad_request_error "Mime type #{@accept} is not supported."
16
+ end
17
+ end
18
+
19
+ # Get a list of all validations
20
+ # @param [Header] Accept one of text/uri-list, application/json
21
+ # @param [Path] Validationtype One of "repeatedcrossvalidation", "leaveoneout", "crossvalidation", "regressioncrossvalidation"
22
+ # @return [text/uri-list] list of all validations of a validation type
23
+ get "/validation/:validationtype/?" do
24
+ bad_request_error "There is no such validation type as: #{params[:validationtype]}" unless VALIDATION_TYPES.include? params[:validationtype]
25
+ case params[:validationtype]
26
+ when "repeatedcrossvalidation"
27
+ validations = Validation::RepeatedCrossValidation.all
28
+ when "leaveoneout"
29
+ validations = Validation::LeaveOneOut.all
30
+ when "crossvalidation"
31
+ validations = Validation::CrossValidation.all
32
+ when "regressioncrossvalidation"
33
+ validations = Validation::RegressionCrossValidation.all
34
+ end
35
+
36
+ case @accept
37
+ when "text/uri-list"
38
+ uri_list = validations.collect{|validation| uri("/validation/#{params[:validationtype]}/#{validation.id}")}
39
+ return uri_list.join("\n") + "\n"
40
+ when "application/json"
41
+ validations = JSON.parse validations.to_json
42
+ validations.each_index do |idx|
43
+ validations[idx][:URI] = uri("/validation/#{params[:validationtype]}/#{validations[idx]["_id"]["$oid"]}")
44
+ end
45
+ return validations.to_json
46
+ else
47
+ bad_request_error "Mime type #{@accept} is not supported."
48
+ end
49
+ end
50
+
51
+ # Get validation representation
52
+ get "/validation/:validationtype/:id/?" do
53
+ bad_request_error "There is no such validation type as: #{params[:validationtype]}" unless VALIDATION_TYPES.include? params[:validationtype]
54
+ case params[:validationtype]
55
+ when "repeatedcrossvalidation"
56
+ validation = Validation::RepeatedCrossValidation.find params[:id]
57
+ when "leaveoneout"
58
+ validation = Validation::LeaveOneOut.find params[:id]
59
+ when "crossvalidation"
60
+ validation = Validation::CrossValidation.find params[:id]
61
+ when "regressioncrossvalidation"
62
+ validation = Validation::RegressionCrossValidation.find params[:id]
63
+ end
64
+
65
+ resource_not_found_error "#{params[:validationtype]} with id: #{params[:id]} not found." unless validation
66
+ #model[:URI] = uri("/model/#{model.id}")
67
+ #model[:neighbor_algorithm_parameters][:feature_dataset_uri] = uri("/dataset/#{model[:neighbor_algorithm_parameters][:feature_dataset_id]}") if model[:neighbor_algorithm_parameters][:feature_dataset_id]
68
+ #model[:training_dataset_uri] = uri("/dataset/#{model.training_dataset_id}") if model.training_dataset_id
69
+ #model[:prediction_feature_uri] = uri("/dataset/#{model.prediction_feature_id}") if model.prediction_feature_id
70
+ return validation.to_json
71
+ end
@@ -0,0 +1,23 @@
1
+ require_relative "setup.rb"
2
+
3
+ class AATest < MiniTest::Test
4
+
5
+ def test_0_login
6
+ res = RestClientWrapper.post(File.join($host,"aa/authenticate"),{:username=>"guest", :password => "guest"},{:Accept => "text/plain"})
7
+ assert_equal res.code, 200
8
+ assert_equal res.size, 62
9
+ @@token = res
10
+ end
11
+
12
+ def test_1_logout
13
+ assert @@token
14
+ assert_equal @@token.size, 62
15
+ res = RestClientWrapper.post(File.join($host,"aa/logout"),{:subjectid=>@@token},{:Accept => "text/plain"})
16
+ assert_equal res.code, 200
17
+ assert_equal res, "Successfully logged out. \n"
18
+ res = RestClientWrapper.post(File.join($host,"aa/logout"),{:subjectid=>@@token},{:Accept => "text/plain"})
19
+ assert_equal res.code, 200
20
+ assert_equal res, "Logout failed.\n"
21
+ end
22
+
23
+ end
@@ -0,0 +1,5 @@
1
+ exclude = ["./setup.rb","./all.rb"]
2
+ (Dir[File.join(File.dirname(__FILE__),"*.rb")]-exclude).each do |test|
3
+ p test
4
+ require_relative test
5
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "setup.rb"
2
+ $api_uri = "#{$host}/api/api.json"
3
+
4
+
5
+ class ApiTest < MiniTest::Test
6
+
7
+ def test_0_api_get
8
+ res = RestClientWrapper.get $api_uri
9
+ assert_equal res.code, 200
10
+ assert JSON.parse "#{res.body}"
11
+ end
12
+
13
+ end
@@ -0,0 +1,54 @@
1
+ require_relative "setup.rb"
2
+
3
+ $compound_uri = "#{$host}/compound"
4
+ $compound = ["InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H"]
5
+
6
+ class CompoundTest < MiniTest::Test
7
+
8
+ def test_00_get_inchi
9
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => 'chemical/x-inchi'}
10
+ assert_equal res.code, 200
11
+ assert_equal res, "InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H"
12
+ end
13
+
14
+ def test_01_get_smiles
15
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "chemical/x-daylight-smiles"}
16
+ assert_equal res.code, 200
17
+ assert_equal res, "c1ccccc1"
18
+ end
19
+
20
+ def test_02_get_sdf
21
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "chemical/x-mdl-sdfile"}
22
+ assert_equal res.code, 200
23
+ assert res.include?(" 6 12 1 0 0 0 0\nM END\n$$$")
24
+ end
25
+
26
+ def test_03_get_png
27
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "image/png"}
28
+ assert_equal res.code, 200
29
+ assert_equal "image/png", res.headers[:content_type]
30
+ end
31
+
32
+ def test_04_get_svg
33
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "image/svg+xml"}
34
+ assert_equal res.code, 200
35
+ assert res.include?("<svg version=")
36
+ end
37
+
38
+ def test_05_get_json
39
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "application/json"}
40
+ assert_equal res.code, 200
41
+ js = JSON.parse res
42
+ assert_equal js["chemblid"], "CHEMBL277500"
43
+ assert_equal js["names"].first, "BENZENE"
44
+ assert_equal js["names"][6], "71-43-2"
45
+ end
46
+
47
+ def test_06_get_names
48
+ res = RestClientWrapper.get File.join($compound_uri, $compound[0]), {}, {:accept => "text/plain"}
49
+ assert_equal res.code, 200
50
+ assert res.include?("Benzene")
51
+ assert res.include?("401765_ALDRICH")
52
+ end
53
+
54
+ end
@@ -0,0 +1,152 @@
1
+ [
2
+ {
3
+ "SMILES": "CC=O",
4
+ "Openbabel.logP": "0.2052",
5
+ "Cdk.AtomCount.nAtom": "7",
6
+ "Cdk.CarbonTypes.C1SP1": "0",
7
+ "Cdk.CarbonTypes.C2SP1": "0",
8
+ "Cdk.CarbonTypes.C1SP2": "1",
9
+ "Cdk.CarbonTypes.C2SP2": "0",
10
+ "Cdk.CarbonTypes.C3SP2": "0",
11
+ "Cdk.CarbonTypes.C1SP3": "1",
12
+ "Cdk.CarbonTypes.C2SP3": "0",
13
+ "Cdk.CarbonTypes.C3SP3": "0",
14
+ "Cdk.CarbonTypes.C4SP3": "0",
15
+ "Joelib.LogP": "0.7679"
16
+ },
17
+ {
18
+ "SMILES": "CC(=O)Nc1ccc2c(c1)Cc1c2cccc1",
19
+ "Openbabel.logP": "3.2892",
20
+ "Cdk.AtomCount.nAtom": "30",
21
+ "Cdk.CarbonTypes.C1SP1": "0",
22
+ "Cdk.CarbonTypes.C2SP1": "0",
23
+ "Cdk.CarbonTypes.C1SP2": "1",
24
+ "Cdk.CarbonTypes.C2SP2": "8",
25
+ "Cdk.CarbonTypes.C3SP2": "4",
26
+ "Cdk.CarbonTypes.C1SP3": "1",
27
+ "Cdk.CarbonTypes.C2SP3": "1",
28
+ "Cdk.CarbonTypes.C3SP3": "0",
29
+ "Cdk.CarbonTypes.C4SP3": "0",
30
+ "Joelib.LogP": "5.3165"
31
+ },
32
+ {
33
+ "SMILES": "NC(=O)/C(=C\\c1ccc(o1)[N+](=O)[O-])/c1ccco1",
34
+ "Openbabel.logP": "3.0302",
35
+ "Cdk.AtomCount.nAtom": "26",
36
+ "Cdk.CarbonTypes.C1SP1": "0",
37
+ "Cdk.CarbonTypes.C2SP1": "0",
38
+ "Cdk.CarbonTypes.C1SP2": "3",
39
+ "Cdk.CarbonTypes.C2SP2": "7",
40
+ "Cdk.CarbonTypes.C3SP2": "1",
41
+ "Cdk.CarbonTypes.C1SP3": "0",
42
+ "Cdk.CarbonTypes.C2SP3": "0",
43
+ "Cdk.CarbonTypes.C3SP3": "0",
44
+ "Cdk.CarbonTypes.C4SP3": "0",
45
+ "Joelib.LogP": "3.6734"
46
+ },
47
+ {
48
+ "SMILES": "Nc1n[nH]cn1",
49
+ "Openbabel.logP": "-0.0319",
50
+ "Cdk.AtomCount.nAtom": "10",
51
+ "Cdk.CarbonTypes.C1SP1": "0",
52
+ "Cdk.CarbonTypes.C2SP1": "0",
53
+ "Cdk.CarbonTypes.C1SP2": "0",
54
+ "Cdk.CarbonTypes.C2SP2": "0",
55
+ "Cdk.CarbonTypes.C3SP2": "0",
56
+ "Cdk.CarbonTypes.C1SP3": "0",
57
+ "Cdk.CarbonTypes.C2SP3": "0",
58
+ "Cdk.CarbonTypes.C3SP3": "0",
59
+ "Cdk.CarbonTypes.C4SP3": "0",
60
+ "Joelib.LogP": "0.7337"
61
+ },
62
+ {
63
+ "SMILES": "[O-][Br](=O)=O.[K+]",
64
+ "Openbabel.logP": "0.4892",
65
+ "Cdk.AtomCount.nAtom": "2",
66
+ "Cdk.CarbonTypes.C1SP1": "0",
67
+ "Cdk.CarbonTypes.C2SP1": "0",
68
+ "Cdk.CarbonTypes.C1SP2": "0",
69
+ "Cdk.CarbonTypes.C2SP2": "0",
70
+ "Cdk.CarbonTypes.C3SP2": "0",
71
+ "Cdk.CarbonTypes.C1SP3": "0",
72
+ "Cdk.CarbonTypes.C2SP3": "0",
73
+ "Cdk.CarbonTypes.C3SP3": "0",
74
+ "Cdk.CarbonTypes.C4SP3": "0",
75
+ "Joelib.LogP": "-5.9945"
76
+ },
77
+ {
78
+ "SMILES": "[Cl-].[Cl-].[Cd+2]",
79
+ "Openbabel.logP": "-5.9945",
80
+ "Cdk.AtomCount.nAtom": "3",
81
+ "Cdk.CarbonTypes.C1SP1": "0",
82
+ "Cdk.CarbonTypes.C2SP1": "0",
83
+ "Cdk.CarbonTypes.C1SP2": "0",
84
+ "Cdk.CarbonTypes.C2SP2": "0",
85
+ "Cdk.CarbonTypes.C3SP2": "0",
86
+ "Cdk.CarbonTypes.C1SP3": "0",
87
+ "Cdk.CarbonTypes.C2SP3": "0",
88
+ "Cdk.CarbonTypes.C3SP3": "0",
89
+ "Cdk.CarbonTypes.C4SP3": "0",
90
+ "Joelib.LogP": "-0.2597"
91
+ },
92
+ {
93
+ "SMILES": "[O-]S(=O)(=O)[O-].[Cd+2]",
94
+ "Openbabel.logP": "-0.2597",
95
+ "Cdk.AtomCount.nAtom": "6",
96
+ "Cdk.CarbonTypes.C1SP1": "0",
97
+ "Cdk.CarbonTypes.C2SP1": "0",
98
+ "Cdk.CarbonTypes.C1SP2": "0",
99
+ "Cdk.CarbonTypes.C2SP2": "0",
100
+ "Cdk.CarbonTypes.C3SP2": "0",
101
+ "Cdk.CarbonTypes.C1SP3": "0",
102
+ "Cdk.CarbonTypes.C2SP3": "0",
103
+ "Cdk.CarbonTypes.C3SP3": "0",
104
+ "Cdk.CarbonTypes.C4SP3": "0",
105
+ "Joelib.LogP": "5.0725"
106
+ },
107
+ {
108
+ "SMILES": "OC(=O)CSc1nc(cc(n1)Cl)Nc1cccc(c1C)C",
109
+ "Openbabel.logP": "3.7401",
110
+ "Cdk.AtomCount.nAtom": "35",
111
+ "Cdk.CarbonTypes.C1SP1": "0",
112
+ "Cdk.CarbonTypes.C2SP1": "0",
113
+ "Cdk.CarbonTypes.C1SP2": "3",
114
+ "Cdk.CarbonTypes.C2SP2": "5",
115
+ "Cdk.CarbonTypes.C3SP2": "2",
116
+ "Cdk.CarbonTypes.C1SP3": "3",
117
+ "Cdk.CarbonTypes.C2SP3": "0",
118
+ "Cdk.CarbonTypes.C3SP3": "0",
119
+ "Cdk.CarbonTypes.C4SP3": "0",
120
+ "Joelib.LogP": "2.2807"
121
+ },
122
+ {
123
+ "SMILES": "COCCl",
124
+ "Openbabel.logP": "0.8291",
125
+ "Cdk.AtomCount.nAtom": "9",
126
+ "Cdk.CarbonTypes.C1SP1": "0",
127
+ "Cdk.CarbonTypes.C2SP1": "0",
128
+ "Cdk.CarbonTypes.C1SP2": "0",
129
+ "Cdk.CarbonTypes.C2SP2": "0",
130
+ "Cdk.CarbonTypes.C3SP2": "0",
131
+ "Cdk.CarbonTypes.C1SP3": "0",
132
+ "Cdk.CarbonTypes.C2SP3": "0",
133
+ "Cdk.CarbonTypes.C3SP3": "0",
134
+ "Cdk.CarbonTypes.C4SP3": "0",
135
+ "Joelib.LogP": "2.5399"
136
+ },
137
+ {
138
+ "SMILES": "C=CC(=C)Cl",
139
+ "Openbabel.logP": "1.9249",
140
+ "Cdk.AtomCount.nAtom": "10",
141
+ "Cdk.CarbonTypes.C1SP1": "0",
142
+ "Cdk.CarbonTypes.C2SP1": "0",
143
+ "Cdk.CarbonTypes.C1SP2": "2",
144
+ "Cdk.CarbonTypes.C2SP2": "2",
145
+ "Cdk.CarbonTypes.C3SP2": "0",
146
+ "Cdk.CarbonTypes.C1SP3": "0",
147
+ "Cdk.CarbonTypes.C2SP3": "0",
148
+ "Cdk.CarbonTypes.C3SP3": "0",
149
+ "Cdk.CarbonTypes.C4SP3": "0",
150
+ "Joelib.LogP": null
151
+ }
152
+ ]
@@ -0,0 +1,48 @@
1
+ require_relative "setup.rb"
2
+
3
+ $host = "#{$host}"
4
+
5
+
6
+ class DescriptorTest < MiniTest::Test
7
+
8
+ def test_00_get_descriptors
9
+ result = RestClientWrapper.get File.join($host, "compound/descriptor"), {}, {:accept => "text/plain"}
10
+ assert_equal result.code, 200
11
+ assert result.include?("Joelib.KierShape1: JOELIb does not provide meaningful descriptions, see java/JoelibDescriptors.java for details.\nJoelib.KierShape2: JOELIb does not provide meaningful descriptions, see java/JoelibDescriptors.java for details."), "Descriptor list is not complete."
12
+ assert_equal 355, result.lines.count
13
+ end
14
+
15
+ def test_01_get_descriptor
16
+ result = RestClientWrapper.get File.join($host, "compound/descriptor", "Openbabel.MW"), {}, {:accept => "text/plain"}
17
+ assert_equal result.code, 200
18
+ assert_equal result, "Molecular Weight filter"
19
+ end
20
+
21
+ def test_03_get_descriptor_id
22
+ result = RestClientWrapper.get File.join($host, "compound/descriptor", "Openbabel.HBA1"), {}, {:accept => "application/json"}
23
+ assert_equal result.code, 200
24
+ json = JSON.parse(result)
25
+ assert_equal json["description"], "Number of Hydrogen Bond Acceptors 1 (JoelLib)"
26
+ bsonid = json["_id"]["$oid"]
27
+ result = RestClientWrapper.get File.join($host, "compound/descriptor", bsonid), {}, {:accept => "application/json"}
28
+ json = JSON.parse(result)
29
+ assert_equal json["name"], "Openbabel.HBA1"
30
+ assert_equal json["numeric"], true
31
+ end
32
+
33
+ def test_04_post_descriptor
34
+ result = RestClientWrapper.post File.join($host, "compound/descriptor"), {:identifier => "CC(=O)CC(C)C#N", :descriptor => "Joelib.LogP"}, {:accept => "application/csv"}
35
+ assert_equal result.code, 200
36
+ assert_equal "SMILES,CC(=O)CC(C)C#N\n\"Joelib.LogP\",2.65908", result
37
+ end
38
+
39
+ # currently not applicable
40
+ #def test_05_post_descriptor_file
41
+ # file = File.join(DATA_DIR, "hamster_carcinogenicity.mini.csv")
42
+ # result = RestClientWrapper.post File.join($host, "compound/descriptor"), {:file => File.open(file), :descriptor => "Openbabel.logP,Cdk.AtomCount,Cdk.CarbonTypes,Joelib.LogP"}, {:accept => "application/json"}
43
+ # assert_equal result.code, 200
44
+ # proof_result = File.read(File.join(REST_DATA_DIR, "test_03_post_descriptor_file.result"))
45
+ # assert_equal result, proof_result.strip
46
+ #end
47
+
48
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "setup.rb"
2
+
3
+ $model_uri = "#{$host}/model"
4
+ class ModelTest < MiniTest::Test
5
+
6
+ def test_00_get_urilist
7
+ res = RestClientWrapper.get $model_uri, {}, {:accept => "text/uri-list"}
8
+ assert_equal res.code, 200
9
+ end
10
+
11
+ def test_01_get_400
12
+ assert_raises OpenTox::BadRequestError do
13
+ res = OpenTox::RestClientWrapper.get $model_uri, {}, {:accept => "text/notimplemented-type"}
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative '../../lazar/lib/lazar.rb'
4
+ require_relative '../../lazar/test/setup.rb'
5
+ $host = "https://enm.in-silico.ch"
6
+ include OpenTox
7
+ REST_TEST_DIR ||= File.expand_path(File.dirname(__FILE__))
8
+ REST_DATA_DIR ||= File.join(REST_TEST_DIR,"data")
@@ -0,0 +1,17 @@
1
+ require_relative "setup.rb"
2
+
3
+ $validation_uri = "#{$host}/validation"
4
+ class ModelTest < MiniTest::Test
5
+
6
+ def test_00_get_urilist
7
+ res = RestClientWrapper.get $validation_uri, {}, {:accept => "text/uri-list"}
8
+ assert_equal res.code, 200
9
+ end
10
+
11
+ def test_01_get_400
12
+ assert_raises OpenTox::BadRequestError do
13
+ res = OpenTox::RestClientWrapper.get $validation_uri, {}, {:accept => "text/notimplemented-type"}
14
+ end
15
+ end
16
+
17
+ end