summaryrefslogtreecommitdiff
path: root/test/toxbank-investigation-sparql.rb
blob: 89f550db05f79074970a34f6925d91d8243cb94c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
require_relative "toxbank-setup.rb"

# Test API extension SPARQL templates 
class TBSPARQLTest < MiniTest::Test

  i_suck_and_my_tests_are_order_dependent!

  # login as pi and create a test investigation
  def test_00_create_investigation
    OpenTox::RestClientWrapper.subjectid = $pi[:subjectid] # set pi as the logged in user
    @@uri = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "BII-I-1-tb2_ftp.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file)}, { :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    @@uri = URI(uri)
    OpenTox::RestClientWrapper.put @@uri.to_s, { :published => "true", :summarySearchable => "true"}, { :subjectid => $pi[:subjectid] }
  end  

  # initial tests to be changed
  def test_01_nonexisting_template
    assert_raises OpenTox::ResourceNotFoundError do
      response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/not_existing_template", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    end
  end

  def test_02_existing_template
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/investigation_details", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  def test_03_camelcase_template
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/InvestigationDetails", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  # Retrieves all factors (name, value, ontology URI of the value) given an investigation URI
  def test_04_factors_by_investigation
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/factors_by_investigation", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    headvars = result["head"]["vars"]
    assert headvars.include?("factorname")
    assert headvars.include?("value")
    assert headvars.include?("ontouri")
    assert headvars.include?("unitOnto")
    assert headvars.include?("unit")
    assert headvars.include?("unitID")
    factorvalues = result["results"]["bindings"].map {|n|  "#{n["factorname"]["value"]}:::#{n["value"]["value"]}"}
    assert factorvalues.include?("limiting nutrient:::phosphorus")
    assert factorvalues.include?("limiting nutrient:::glucose")
    assert factorvalues.include?("limiting nutrient:::carbon")
    assert factorvalues.include?("limiting nutrient:::sulfur")
    assert factorvalues.include?("limiting nutrient:::nitrogen")
    assert factorvalues.include?("limiting nutrient:::ethanol")
    assert factorvalues.include?("rate:::0.1")
    assert factorvalues.include?("rate:::0.2")
    assert factorvalues.include?("rate:::0.07")
  end

  def test_05_characteristics_by_investigation
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/characteristics_by_investigation", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    propnamevalues = result["results"]["bindings"].map {|n|  "#{n["propname"]["value"]}:::#{n["value"]["value"]}:::#{n["ontouri"]["value"]}"}
    assert propnamevalues.include?("organism:::Saccharomyces cerevisiae (Baker's yeast):::http://purl.obolibrary.org/obo/NEWT_4932")
    assert propnamevalues.include?("Label:::biotin:::http://purl.obolibrary.org/chebi/15956")
    assert propnamevalues.include?("organism:::Saccharomyces cerevisiae (Baker's yeast):::http://purl.obolibrary.org/obo/NEWT_4932")
  end


  def test_06_investigation_endpoint_technology
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/investigation_endpoint_technology", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    endpointtechnologies = result["results"]["bindings"].map {|n|  "#{n["endpoint"]["value"]}:::#{n["technology"]["value"]}"}
    assert endpointtechnologies.include?("http://purl.org/obo/owl/OBI#0000424:::http://purl.org/obo/owl/OBI#0400148")
    assert endpointtechnologies.include?("http://purl.org/obo/owl/OBI#0000366:::http://purl.org/obo/owl/OBI#OBI_0000470")
    assert endpointtechnologies.include?("http://purl.org/obo/owl/OBI#OBI_0000615:::http://purl.org/obo/owl/OBI#OBI_0000470")
    assert endpointtechnologies.include?("http://purl.org/obo/owl/OBI#0000424:::http://purl.org/obo/owl/OBI#0400148")
    assert_equal endpointtechnologies.size, 4
  end

  def test_07_investigation_and_characteristics
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_and_characteristics", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    inv_chars = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["propValue"]["value"]}:::#{n["ontouri"]["value"]}"}
    assert inv_chars.include?("#{@@uri}:::Label:::#{@@uri}/CV2:::http://purl.obolibrary.org/chebi/15956")
    assert inv_chars.include?("#{@@uri}:::organism:::#{@@uri}/CV4:::http://purl.obolibrary.org/obo/NEWT_4932")
    assert_equal 200, response.code
  end

  def test_08_investigations_and_protocols
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigations_and_protocols", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    inv_protocols = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["protocol"]["value"]}:::#{n["label"]["value"]}"}
    #puts inv_protocols
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P2:::biotin labeling")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P6:::EukGE-WS4")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P1:::metabolite extraction")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P5:::mRNA extraction")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P9:::biotin labeling")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P8:::mRNA extraction")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P4:::protein extraction")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P3:::EukGE-WS4")
    assert inv_protocols.include?("#{@@uri}:::#{@@uri}/P7:::ITRAQ labeling")
    assert_equal 200, response.code
  end

  def test_09_files_by_investigation
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/files_by_investigation", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    files = result["results"]["bindings"].map{|n| "#{n["file"]["value"]}"}
    assert_equal 180, files.size
    downloaduris = result["results"]["bindings"].map{|n| "#{n["downloaduri"]["value"]}"}
    assert_match /#{@@uri}\/files\/JIC37_Ethanol_0\.07_Internal_1_3\.txt/, downloaduris.to_s
  end

  def test_13_investigation_by_characteristic_value
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_characteristic_value", {:value => "Saccharomyces cerevisiae (Baker's yeast)"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    char_value = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["ontoURI"]["value"]}"}
    assert char_value.include?("#{@@uri}:::organism:::http://purl.obolibrary.org/obo/NEWT_4932")
    assert_equal 200, response.code
  end

  def test_14_investigation_by_characteristic_name
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_characteristic_name", {:value => "organism"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    char_name = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["value"]["value"]}:::#{n["ontoURI"]["value"]}"}
    assert char_name.include?("#{@@uri}:::Saccharomyces cerevisiae (Baker's yeast):::http://purl.obolibrary.org/obo/NEWT_4932")
    assert_equal 200, response.code
  end

  def test_15_investigation_by_characteristic
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_characteristic", {:value => "http://purl.obolibrary.org/obo/NEWT_4932"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    inv_char = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["propname"]["value"]}:::#{n["value"]["value"]}"}
    assert inv_char.include?("#{@@uri}:::organism:::Saccharomyces cerevisiae (Baker's yeast)")
    assert_equal 200, response.code
  end

  def test_30_empty_factorValues_search
    assert_raises OpenTox::BadRequestError do
      response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_factors", {:factorValues => ""}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    end
  end

  def test_31_empty_value_search
    assert_raises OpenTox::BadRequestError do
      response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_characteristic", {:value => ""}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    end
  end

  # delete investigation/{id}
  # @note expect code 200
  def test_90_delete_investigation
    result = OpenTox::RestClientWrapper.delete @@uri.to_s, {}, {:subjectid => $pi[:subjectid]}
    assert_equal 200, result.code
  end

end

class TBSPARQLTestExtended < MiniTest::Test

  i_suck_and_my_tests_are_order_dependent!

  # login as pi and create a test investigation
  def test_00_create_investigation
    OpenTox::RestClientWrapper.subjectid = $pi[:subjectid] # set pi as the logged in user
    @@uri = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "IST_test_sparql.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file)}, { :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    @@uri = URI(uri)
    OpenTox::RestClientWrapper.put @@uri.to_s, { :published => "true", :summarySearchable => "true"}, { :subjectid => $pi[:subjectid] }
  end  
  
  # Retrieves all factors (name, value, ontology URI of the value) given an investigation URI
  def test_04_factors_by_investigation
    response = OpenTox::RestClientWrapper.get "#{@@uri}/sparql/factors_by_investigation", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    headvars = result["head"]["vars"]
    assert headvars.include?("factorname")
    assert headvars.include?("value")
    assert headvars.include?("ontouri")
    assert headvars.include?("unitOnto")
    assert headvars.include?("unit")
    assert headvars.include?("unitID")
    factors = result["results"]["bindings"].map{|n| "#{n["factorname"]["value"]}:::#{n["value"]["value"]}:::#{n["ontouri"]["value"]}:::#{n["unitOnto"]["value"]}:::#{n["unit"]["value"]}:::#{n["unitID"]["value"]}"}
    #puts factors
    assert factors.include?("sample TimePoint:::8::::::::::::")
    assert factors.include?("dose:::0::::::http://purl.obolibrary.org/obo/UO_0000064:::micromolar:::UO:0000064")
    assert factors.include?("compound:::DOXORUBICIN:::http://purl.obolibrary.org/chebi/CHEBI:28748:::::::::")
  end
  
  # Retrieves investigation URI and factors (name, value, ontology URI of the value)
  def test_09_investigations_and_factors
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigations_and_factors", {}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    inv_factors = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["factorname"]["value"]}:::#{n["ontouri"]["value"]}:::#{n["value"]["value"]}"}
    #puts inv_factors
    assert_equal 200, response.code
    assert inv_factors.include?("#{@@uri}:::compound:::http://purl.obolibrary.org/chebi/CHEBI:28748:::DOXORUBICIN")
  end

  # Retrieves protocol URI containing any of the factor value URI (e.g. two compound URIs)
  def test_10_protocols_by_factors
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/protocols_by_factors", {:factorValues => "['http://purl.obolibrary.org/chebi/CHEBI:28748']"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    protocol_factors = result["results"]["bindings"].map{|n| "#{n["protocol"]["value"]}:::#{n["label"]["value"]}:::#{n["factorname"]["value"]}:::#{n["value"]["value"]}"}
    #puts protocol_factors
    assert_equal 200, response.code
    assert protocol_factors.include?("#{@@uri}/P4:::labeling:::compound:::DOXORUBICIN")
    assert protocol_factors.include?("#{@@uri}/P3:::RNA extraction:::compound:::DOXORUBICIN")
    assert protocol_factors.include?("#{@@uri}/P2:::normalization data transformation:::compound:::DOXORUBICIN")
    assert protocol_factors.include?("#{@@uri}/P1:::data transformation:::compound:::DOXORUBICIN")
    assert protocol_factors.include?("#{@@uri}/P5:::data collection:::compound:::DOXORUBICIN")
    assert protocol_factors.include?("#{@@uri}/P6:::nucleic acid hybridization:::compound:::DOXORUBICIN")
  end

  # Retrieves investigation URI containing any of the factor value URI (e.g. two compound URIs)
  def test_11_investigation_by_factors
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_factors", {:factorValues => "['http://purl.obolibrary.org/chebi/CHEBI:28748']"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    #puts response
    result = JSON.parse(response)
    inv_factors = result["results"]["bindings"].map{|n| "#{n["inv"]["value"]}:::#{n["factorname"]["value"]}:::#{n["value"]["value"]}"}
    assert_equal 200, response.code
    assert inv_factors.include?("#{@@uri}:::compound:::DOXORUBICIN")
  end

  # Retrieves investigation URI given a factor value URI (e.g. compound URI)
  def test_12_investigation_by_factor
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_factor", {:value => "http://purl.obolibrary.org/chebi/CHEBI:28748"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    #puts response
    result = JSON.parse(response)
    inv_factor = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["factorname"]["value"]}:::#{n["value"]["value"]}"}
    assert_equal 200, response.code
    assert inv_factor.include?("#{@@uri}:::compound:::DOXORUBICIN")
  end
  
  def test_16_investigation_by_pvalue
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_pvalue", {:value => "0.65614"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    #puts response
    result = JSON.parse(response)
    inv_pvalue = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["gene"]["value"]}"}
    assert_equal 200, response.code
    assert inv_pvalue.include?("#{@@uri}:::http://onto.toxbank.net/isa/Entrez/3075")
  end
  
  def test_17_investigation_by_qvalue
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_qvalue", {:value => "0.805517"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    #puts response
    result = JSON.parse(response)
    inv_qvalue = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["gene"]["value"]}"}
    assert_equal 200, response.code
    assert inv_qvalue.include?("#{@@uri}:::http://onto.toxbank.net/isa/Entrez/3075")
  end
  
  def test_18_investigation_by_foldchange
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_foldchange", {:value => "0.035"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    #puts response
    inv_foldchange = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["gene"]["value"]}"}
    assert_equal 200, response.code
    assert inv_foldchange.include?("#{@@uri}:::http://onto.toxbank.net/isa/Entrez/3075")
  end
  
  def test_19_investigation_by_genes
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_genes", {:geneIdentifiers => "['entrez:3075']"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    #puts response
    inv_genes = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}:::#{n["datatype"]["value"]}:::#{n["title"]["value"]}:::#{n["valuetype"]["value"]}:::#{n["value"]["value"]}"}
    assert_equal 200, response.code
    assert inv_genes.include?("#{@@uri}:::http://onto.toxbank.net/isa/bii/data_types/microarray_derived_data:::q-value[Low.8hr-Control.8hr]:::http://onto.toxbank.net/isa/qvalue:::0.911237")
  end

  def test_20_investigation_by_gene_and_pvalue
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_gene_and_pvalue", {:geneIdentifiers => "['entrez:3075', 'uniprot:P10809']", :value => "0.7"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    #puts response
    inv = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}"}
    assert_equal 200, response.code
    assert inv.include?("#{@@uri}")
  end

  def test_21_investigation_by_gene_and_pvalue
    response = OpenTox::RestClientWrapper.get "#{$investigation[:uri]}/sparql/investigation_by_gene_and_pvalue", {:geneIdentifiers => "['entrez:3075']", :value => "0.7"}, {:accept => "application/json", :subjectid => $pi[:subjectid]}
    result = JSON.parse(response)
    #puts response
    inv = result["results"]["bindings"].map{|n| "#{n["investigation"]["value"]}"}
    assert_equal 200, response.code
    assert inv.include?("#{@@uri}")
  end
  
  # delete investigation/{id}
  # @note expect code 200
  def test_90_delete_investigation
    result = OpenTox::RestClientWrapper.delete @@uri.to_s, {}, {:subjectid => $pi[:subjectid]}
    assert_equal 200, result.code
  end
end