8 use SGN::Test::Fixture;
10 use SGN::Model::Cvterm;
11 use Test::WWW::Mechanize;
14 my $mech = Test::WWW::Mechanize->new;
15 my $json = JSON->new->allow_nonref;
17 my $t = SGN::Test::Fixture->new();
18 my $schema = $t->bcs_schema();
20 my $onto = CXGN::Onto->new( { schema => $t->bcs_schema() });
22 my $cv_type = 'attribute_ontology';
23 my @results = $onto->get_root_nodes($cv_type);
24 #print STDERR "test 1 results " .Dumper @results;
28 'CHEBI:00000 chebi_compounds'
29 ], 'onto get root nodes test');
32 my $cv_id = 58; # CASSTISS ontology with cvprop 'object'
33 @results = $onto->get_terms($cv_id);
34 print STDERR "test 2 results: @results";
35 my @cass_tissues = ["[77181,'cass fibrous root|CASSTISS:0000011']",
36 "[77176,'cass lower leaf|CASSTISS:0000006']",
37 "[77171,'cass lower stem bark|CASSTISS:0000010']",
38 "[77174,'cass lower stem whole|CASSTISS:0000009']",
39 "[77178,'cass pre-storage root|CASSTISS:0000012']",
40 "[77180,'cass sink leaf|CASSTISS:0000004']",
41 "[77170,'cass source leaf|CASSTISS:0000005']",
42 "[77168,'cass storage root|CASSTISS:0000013']",
43 "[77172,'cass upper stem|CASSTISS:0000007']"];
44 # problem with this data structure
45 is_deeply(@results, @cass_tissues, 'onto get terms test');
48 my @allowed_composed_cvs = ('trait','toy');
49 my $composable_cvterm_delimiter = '|';
50 my $composable_cvterm_format = 'concise';
51 my $traits1 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
56 trait => [70775,76555],
61 #print STDERR Dumper $traits1;
63 'existing_traits' => [],
70 'anthocyanin pigmentation visual rating 0-3|month 1'
77 'anthocyanin pigmentation visual rating 0-3|month 10'
84 'ease of harvest assessment 1-3|month 1'
91 'ease of harvest assessment 1-3|month 10'
94 }, 'check composed trait creation 1');
96 @allowed_composed_cvs = ('trait','toy','tod','object');
97 $composable_cvterm_delimiter = '||';
98 $composable_cvterm_format = 'concise';
99 my $traits2 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
100 object => [77176, 77180],
104 trait => [70775,76555],
106 toy => [77526,77499],
109 #print STDERR Dumper $traits2;
110 is_deeply($traits2, {
111 'existing_traits' => [],
120 'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass lower leaf'
129 'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass sink leaf'
138 'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass lower leaf'
147 'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass sink leaf'
156 'ease of harvest assessment 1-3||month 10||end of day||cass lower leaf'
165 'ease of harvest assessment 1-3||month 10||end of day||cass sink leaf'
174 'ease of harvest assessment 1-3||month 1||end of day||cass lower leaf'
183 'ease of harvest assessment 1-3||month 1||end of day||cass sink leaf'
186 }, 'check composed trait creation 2');
188 @allowed_composed_cvs = ('trait','toy','tod','object');
189 $composable_cvterm_delimiter = '||';
190 $composable_cvterm_format = 'extended';
191 my $traits3 = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
192 object => [77171, 77174],
196 trait => [70775,76555],
198 toy => [77526,77499],
201 #print STDERR Dumper $traits3;
202 is_deeply($traits3, {
203 'existing_traits' => [],
212 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
221 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
230 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
239 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
248 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
257 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
266 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010'
275 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009'
278 }, 'check composed trait creation 3');
280 my $new_traits2 = $traits2->{new_traits};
281 my %new_composed_terms2;
282 foreach (@$new_traits2){
283 $new_composed_terms2{$_->[1]} = join ',', @{$_->[0]};
285 my $results2 = $onto->store_composed_term(\%new_composed_terms2);
286 #print STDERR Dumper $results2;
288 foreach (@$results2){
289 ok($_->[0], 'check that cvterm_id saved');
290 push @results2_names, $_->[1];
292 print STDERR Dumper \@results2_names;
293 is_deeply(\@results2_names, [
294 'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass lower leaf|COMP:0000014',
295 'anthocyanin pigmentation visual rating 0-3||month 10||end of day||cass sink leaf|COMP:0000015',
296 'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass lower leaf|COMP:0000016',
297 'anthocyanin pigmentation visual rating 0-3||month 1||end of day||cass sink leaf|COMP:0000017',
298 'ease of harvest assessment 1-3||month 10||end of day||cass lower leaf|COMP:0000018',
299 'ease of harvest assessment 1-3||month 10||end of day||cass sink leaf|COMP:0000019',
300 'ease of harvest assessment 1-3||month 1||end of day||cass lower leaf|COMP:0000020',
301 'ease of harvest assessment 1-3||month 1||end of day||cass sink leaf|COMP:0000021'
302 ], 'check store composed terms 2');
304 my $new_traits3 = $traits3->{new_traits};
305 my %new_composed_terms3;
306 foreach (@$new_traits3){
307 $new_composed_terms3{$_->[1]} = join ',', @{$_->[0]};
309 my $results3 = $onto->store_composed_term(\%new_composed_terms3);
310 #print STDERR Dumper $results3;
312 foreach (@$results3){
313 ok($_->[0], 'check that cvterm_id saved');
314 push @results3_names, $_->[1];
316 print STDERR Dumper \@results3_names;
317 is_deeply(\@results3_names, [
318 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000022',
319 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000023',
320 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000024',
321 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000025',
322 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000026',
323 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000027',
324 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000028',
325 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000029'
326 ], 'test save composed terms 3');
328 my $traits3_duplicate = SGN::Model::Cvterm->get_traits_from_component_categories($t->bcs_schema(), \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, {
329 object => [77171, 77174],
333 trait => [70775,76555],
335 toy => [77526,77499],
338 #print STDERR Dumper $traits3_duplicate;
339 my $new_traits = $traits3_duplicate->{new_traits};
340 my $existing_traits = $traits3_duplicate->{existing_traits};
342 foreach (@$existing_traits){
343 ok($_->[0], 'check that cvterm_id saved');
344 push @check_names, $_->[1];
346 is_deeply($new_traits, [], 'check that duplicate traits are not added');
347 print STDERR Dumper \@check_names;
348 is_deeply(\@check_names, [
349 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000022',
350 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000023',
351 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000024',
352 'anthocyanin pigmentation visual rating 0-3|CO_334:0000103||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000025',
353 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000026',
354 'ease of harvest assessment 1-3|CO_334:0000225||month 10|TIME:0000069||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000027',
355 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem bark|CASSTISS:0000010|COMP:0000028',
356 'ease of harvest assessment 1-3|CO_334:0000225||month 1|TIME:0000060||end of day|TIME:0000003||cass lower stem whole|CASSTISS:0000009|COMP:0000029'
357 ], 'check that duplicate traits are separated from new_traits');
360 ## Test adding observation variables, traits, methods, scales
363 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ]);
364 $response = decode_json $mech->content;
365 print STDERR Dumper $response;
366 my $sgn_session_id = $response->{access_token};
367 print STDERR $sgn_session_id."\n";
369 my $q = "Select db_id FROM db where name = 'CO_334';";
370 my $sth = $schema->storage->dbh->prepare($q);
372 my ($db_id) = $sth->fetchrow_array();
374 $mech->post_ok('http://localhost:3010/ajax/onto/store_trait_method_scale_observation_variable',
376 "sgn_session_id"=>$sgn_session_id,
377 "selected_observation_variable_db_id"=> $db_id,
378 "new_observation_variable_name"=> "new observation variable name",
379 "new_observation_variable_definition"=> "new observation variable definition",
380 "selected_trait_db_id"=> $db_id,
381 "selected_trait_cvterm_id"=> undef,
382 "new_trait_name"=> "new trait name",
383 "new_trait_definition"=> "new trait definition",
384 "selected_method_db_id"=> $db_id,
385 "selected_method_cvterm_id"=> undef,
386 "new_method_name"=> "new method name",
387 "new_method_definition"=> "new method definition",
388 "selected_scale_db_id"=> $db_id,
389 "selected_scale_cvterm_id"=> undef,
390 "new_scale_name"=> "new scale name",
391 "new_scale_definition"=> "new scale definition",
392 "new_scale_format"=> "",
393 "new_scale_minimum"=> "",
394 "new_scale_maximum"=> "",
395 "new_scale_default"=> "",
396 "new_scale_categories"=> ""
399 $response = decode_json $mech->content;
400 print STDERR Dumper $response;
401 ok($response->{success});
403 $q = "Select cvterm_id FROM cvterm where name = 'new trait name';";
404 $sth = $schema->storage->dbh->prepare($q);
406 my ($trait_cvterm_id) = $sth->fetchrow_array();
408 $mech->post_ok('http://localhost:3010/ajax/onto/store_trait_method_scale_observation_variable',
410 "selected_observation_variable_db_id"=> $db_id,
411 "new_observation_variable_name"=> "new observation variable name 2",
412 "new_observation_variable_definition"=> "new observation variable definition 2",
413 "selected_trait_db_id"=> $db_id,
414 "selected_trait_cvterm_id"=> $trait_cvterm_id,
415 "new_trait_name"=> "",
416 "new_trait_definition"=> "",
417 "selected_method_db_id"=> $db_id,
418 "selected_method_cvterm_id"=> "",
419 "new_method_name"=> "new method name 2",
420 "new_method_definition"=> "new method definition 2",
421 "selected_scale_db_id"=> $db_id,
422 "selected_scale_cvterm_id"=> "",
423 "new_scale_name"=> "new scale name 2",
424 "new_scale_definition"=> "new scale definition 2",
425 "new_scale_format"=> "",
426 "new_scale_minimum"=> "",
427 "new_scale_maximum"=> "",
428 "new_scale_default"=> "",
429 "new_scale_categories"=> ""
432 $response = decode_json $mech->content;
433 print STDERR Dumper $response;
434 ok($response->{success});
436 $mech->post_ok('http://localhost:3010/ajax/onto/store_ontology_identifier',
438 "sgn_session_id"=>$sgn_session_id,
439 "ontology_name"=> "NewOntology1",
440 "ontology_description"=> "new ontology",
441 "ontology_identifier"=> "NOO1",
442 "ontology_type"=> "method_ontology",
445 $response = decode_json $mech->content;
446 print STDERR Dumper $response;
447 ok($response->{success});