Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / t / unit_fixture / CXGN / Trial.t
blob89be687f32d71f60fcfa249137fdb8c2bb778a67
1 #This script should test all functions in CXGN::Trial, CXGN::Trial::TrialLayout, CXGN::Trial::TrialDesign, CXGN::Trial::TrialCreate
3 use strict;
4 use lib 't/lib';
6 use Test::More;
7 use SGN::Test::Fixture;
8 use SimulateC;
10 use Data::Dumper;
12 use CXGN::Trial;
13 use CXGN::Trial::TrialLayout;
14 use CXGN::Trial::TrialDesign;
15 use CXGN::Trial::TrialCreate;
16 use CXGN::Trial::Folder;
17 use CXGN::Phenotypes::StorePhenotypes;
18 use CXGN::Trial::Search;
19 use SGN::Model::Cvterm;
21 my $f = SGN::Test::Fixture->new();
22 my $schema = $f->bcs_schema;
24 my $trial_search = CXGN::Trial::Search->new({
25     bcs_schema=>$schema,
26 });
27 my ($result, $total_count) = $trial_search->search();
28 print STDERR "ALL TRIAL =".Dumper($result)."\n";
29 is_deeply($result, [
30           {
31             'design' => 'RCBD',
32             'breeding_program_id' => 134,
33             'genotyping_facility_status' => undef,
34             'genotyping_plate_sample_type' => undef,
35             'genotyping_facility_submitted' => undef,
36             'project_harvest_date' => '',
37             'location_id' => '23',
38             'breeding_program_description' => 'test',
39             'location_name' => 'test_location',
40             'breeding_program_name' => 'test',
41             'genotyping_plate_format' => undef,
42             'year' => '2017',
43             'description' => 'Copy of trial with postcomposed phenotypes from cassbase.',
44             'trial_name' => 'CASS_6Genotypes_Sampling_2015',
45                 'trial_type' => 'Preliminary Yield Trial',
46                 'trial_type_value' => 'Preliminary Yield Trial',
47             'trial_type_name' => 'Preliminary Yield Trial',
48             'trial_type_id' => 76515,
49             'folder_description' => undef,
50             'genotyping_facility_plate_id' => undef,
51             'folder_id' => undef,
52             'genotyping_facility' => undef,
53             'project_planting_date' => '',
54             'folder_name' => undef,
55             'trial_id' => 165,
56             'sampling_facility' => undef,
57                 'sampling_trial_sample_type' => undef,
58                 'additional_info' => undef
60           },
61           {
62             'genotyping_facility_status' => undef,
63             'genotyping_plate_sample_type' => undef,
64             'genotyping_facility_submitted' => undef,
65             'breeding_program_id' => 134,
66             'design' => 'Alpha',
67             'description' => 'This trial was loaded into the fixture to test solgs.',
68             'year' => '2014',
69             'genotyping_plate_format' => undef,
70             'trial_type_name' => 'Clonal Evaluation',
71             'trial_name' => 'Kasese solgs trial',
72                 'trial_type' => 'Clonal Evaluation',
73                 'trial_type_value' => '1',
74             'folder_description' => undef,
75             'trial_type_id' => 77106,
76             'project_planting_date' => '',
77             'genotyping_facility' => undef,
78             'folder_name' => undef,
79             'folder_id' => undef,
80             'trial_id' => 139,
81             'genotyping_facility_plate_id' => undef,
82             'breeding_program_description' => 'test',
83             'location_name' => 'test_location',
84             'location_id' => '23',
85             'project_harvest_date' => '',
86             'breeding_program_name' => 'test',
87             'sampling_facility' => undef,
88                 'sampling_trial_sample_type' => undef,
89                 'additional_info' => undef
91           },
92           {
93             'description' => 'new_test_cross',
94             'year' => undef,
95             'genotyping_plate_format' => undef,
96             'trial_type_name' => undef,
97                 'trial_type' => undef,
98                 'trial_type_value' => undef,
99             'trial_name' => 'new_test_cross',
100             'folder_description' => undef,
101             'trial_type_id' => undef,
102             'trial_id' => 135,
103             'genotyping_facility' => undef,
104             'project_planting_date' => '',
105             'folder_name' => undef,
106             'folder_id' => undef,
107             'genotyping_facility_plate_id' => undef,
108             'breeding_program_description' => 'test',
109             'location_name' => '',
110             'project_harvest_date' => '',
111             'location_id' => undef,
112             'breeding_program_name' => 'test',
113             'genotyping_plate_sample_type' => undef,
114             'genotyping_facility_status' => undef,
115             'genotyping_facility_submitted' => undef,
116             'breeding_program_id' => 134,
117             'design' => undef,
118             'sampling_facility' => undef,
119                 'sampling_trial_sample_type' => undef,
120                 'additional_info' => undef
122           },
123           {
124             'genotyping_facility_plate_id' => undef,
125             'genotyping_facility' => undef,
126             'folder_name' => undef,
127             'project_planting_date' => '',
128             'folder_id' => undef,
129             'trial_id' => 144,
130             'trial_type_id' => undef,
131             'folder_description' => undef,
132             'trial_name' => 'test_t',
133                 'trial_type' => undef,
134                 'trial_type_value' => undef,
135             'trial_type_name' => undef,
136             'year' => '2016',
137             'genotyping_plate_format' => undef,
138             'description' => 'test tets',
139             'breeding_program_name' => 'test',
140             'project_harvest_date' => '',
141             'location_id' => '23',
142             'location_name' => 'test_location',
143             'breeding_program_description' => 'test',
144             'genotyping_facility_submitted' => undef,
145             'genotyping_plate_sample_type' => undef,
146             'genotyping_facility_status' => undef,
147             'design' => 'CRD',
148             'breeding_program_id' => 134,
149             'sampling_facility' => undef,
150                 'sampling_trial_sample_type' => undef,
151                 'additional_info' => undef
153           },
154           {
155             'genotyping_facility_status' => undef,
156             'genotyping_plate_sample_type' => undef,
157             'genotyping_facility_submitted' => undef,
158             'breeding_program_id' => 134,
159             'design' => 'CRD',
160             'trial_type_name' => undef,
161             'trial_name' => 'test_trial',
162                 'trial_type' => undef,
163                 'trial_type_value' => undef,
164             'description' => 'test trial',
165             'year' => '2014',
166             'genotyping_plate_format' => undef,
167             'genotyping_facility' => undef,
168             'folder_name' => undef,
169             'project_planting_date' => '2017-July-04',
170             'folder_id' => undef,
171             'trial_id' => 137,
172             'genotyping_facility_plate_id' => undef,
173             'folder_description' => undef,
174             'trial_type_id' => undef,
175             'breeding_program_name' => 'test',
176             'breeding_program_description' => 'test',
177             'location_name' => 'test_location',
178             'project_harvest_date' => '2017-July-21',
179             'location_id' => '23',
180             'sampling_facility' => undef,
181                 'sampling_trial_sample_type' => undef,
182                 'additional_info' => undef
184           },
185           {
186             'genotyping_facility_submitted' => undef,
187             'genotyping_facility_status' => undef,
188             'genotyping_plate_sample_type' => undef,
189             'breeding_program_id' => 134,
190             'design' => 'CRD',
191             'folder_description' => undef,
192                 'trial_type_id' => undef,
193                 'trial_type_value' => undef,
194             'folder_name' => undef,
195             'genotyping_facility' => undef,
196             'project_planting_date' => '',
197             'folder_id' => undef,
198             'trial_id' => 141,
199             'genotyping_facility_plate_id' => undef,
200             'description' => 'another trial for solGS',
201             'genotyping_plate_format' => undef,
202             'year' => '2014',
203             'trial_type_name' => undef,
204             'trial_name' => 'trial2 NaCRRI',
205             'trial_type' => undef,
206             'breeding_program_description' => 'test',
207             'location_name' => 'test_location',
208             'location_id' => '23',
209             'project_harvest_date' => '',
210             'breeding_program_name' => 'test',
211             'sampling_facility' => undef,
212                 'sampling_trial_sample_type' => undef,
213                 'additional_info' => undef
215           }
216         ], 'trial search test 1');
218 $trial_search = CXGN::Trial::Search->new({
219     bcs_schema=>$schema,
220     location_list=>['test_location'],
221     program_list=>['test'],
223 ($result, $total_count) = $trial_search->search();
224 print STDERR "SELECTED TRIAL =".Dumper($result)."\n";
225 is_deeply($result, [
226           {
227             'genotyping_facility_status' => undef,
228             'genotyping_plate_sample_type' => undef,
229             'genotyping_facility_submitted' => undef,
230             'design' => 'RCBD',
231             'breeding_program_id' => 134,
232             'trial_type_name' => 'Preliminary Yield Trial',
233             'trial_type' => 'Preliminary Yield Trial',
234             'trial_name' => 'CASS_6Genotypes_Sampling_2015',
235             'description' => 'Copy of trial with postcomposed phenotypes from cassbase.',
236             'year' => '2017',
237             'genotyping_plate_format' => undef,
238             'project_planting_date' => '',
239             'genotyping_facility' => undef,
240             'folder_id' => undef,
241             'folder_name' => undef,
242             'trial_id' => 165,
243             'genotyping_facility_plate_id' => undef,
244             'folder_description' => undef,
245             'trial_type_id' => 76515,
246             'breeding_program_name' => 'test',
247             'breeding_program_description' => 'test',
248                 'location_name' => 'test_location',
249                 'trial_type_value' => 'Preliminary Yield Trial',
250             'location_id' => '23',
251             'project_harvest_date' => '',
252             'sampling_facility' => undef,
253                 'sampling_trial_sample_type' => undef,
254                 'additional_info' => undef
255           },
256           {
257             'project_harvest_date' => '',
258             'location_id' => '23',
259             'location_name' => 'test_location',
260             'breeding_program_description' => 'test',
261             'breeding_program_name' => 'test',
262             'genotyping_plate_format' => undef,
263             'year' => '2014',
264             'description' => 'This trial was loaded into the fixture to test solgs.',
265             'trial_type' => 'Clonal Evaluation',
266             'trial_name' => 'Kasese solgs trial',
267                 'trial_type_name' => 'Clonal Evaluation',
268                 'trial_type_value' => '1',
269             'trial_type_id' => 77106,
270             'folder_description' => undef,
271             'genotyping_facility_plate_id' => undef,
272             'project_planting_date' => '',
273             'genotyping_facility' => undef,
274             'trial_id' => 139,
275             'folder_id' => undef,
276             'folder_name' => undef,
277             'design' => 'Alpha',
278             'breeding_program_id' => 134,
279             'genotyping_facility_status' => undef,
280             'genotyping_plate_sample_type' => undef,
281             'genotyping_facility_submitted' => undef,
282             'sampling_facility' => undef,
283                 'sampling_trial_sample_type' => undef,
284                 'additional_info' => undef
285           },
286           {
287             'genotyping_facility_plate_id' => undef,
288             'folder_id' => undef,
289             'genotyping_facility' => undef,
290             'project_planting_date' => '',
291             'trial_id' => 135,
292             'folder_name' => undef,
293             'trial_type_id' => undef,
294             'folder_description' => undef,
295             'trial_type' => undef,
296             'trial_name' => 'new_test_cross',
297             'trial_type_name' => undef,
298             'year' => undef,
299             'genotyping_plate_format' => undef,
300             'description' => 'new_test_cross',
301             'breeding_program_name' => 'test',
302             'project_harvest_date' => '',
303             'location_id' => undef,
304             'location_name' => '',
305             'breeding_program_description' => 'test',
306             'genotyping_facility_submitted' => undef,
307             'genotyping_plate_sample_type' => undef,
308             'genotyping_facility_status' => undef,
309             'breeding_program_id' => 134,
310             'design' => undef,
311             'sampling_facility' => undef,
312                 'sampling_trial_sample_type' => undef,
313                 'trial_type_value' => undef,
314                 'additional_info' => undef
315           },
316           {
317             'design' => 'CRD',
318             'breeding_program_id' => 134,
319             'genotyping_plate_sample_type' => undef,
320             'genotyping_facility_status' => undef,
321             'genotyping_facility_submitted' => undef,
322             'breeding_program_name' => 'test',
323             'location_id' => '23',
324             'project_harvest_date' => '',
325             'breeding_program_description' => 'test',
326             'location_name' => 'test_location',
327             'trial_type' => undef,
328             'trial_name' => 'test_t',
329                 'trial_type_name' => undef,
330                 'trial_type_value' => undef,
331             'genotyping_plate_format' => undef,
332             'year' => '2016',
333             'description' => 'test tets',
334             'genotyping_facility_plate_id' => undef,
335             'folder_id' => undef,
336             'genotyping_facility' => undef,
337             'project_planting_date' => '',
338             'trial_id' => 144,
339             'folder_name' => undef,
340             'trial_type_id' => undef,
341             'folder_description' => undef,
342             'sampling_facility' => undef,
343                 'sampling_trial_sample_type' => undef,
344                 'additional_info' => undef
345           },
346           {
347             'design' => 'CRD',
348             'breeding_program_id' => 134,
349             'genotyping_facility_submitted' => undef,
350             'genotyping_facility_status' => undef,
351             'genotyping_plate_sample_type' => undef,
352                 'breeding_program_name' => 'test',
353                 'trial_type_value' => undef,
354             'breeding_program_description' => 'test',
355             'location_name' => 'test_location',
356             'location_id' => '23',
357             'project_harvest_date' => '2017-July-21',
358             'genotyping_facility' => undef,
359             'project_planting_date' => '2017-July-04',
360             'folder_id' => undef,
361             'folder_name' => undef,
362             'trial_id' => 137,
363             'genotyping_facility_plate_id' => undef,
364             'folder_description' => undef,
365             'trial_type_id' => undef,
366             'trial_type_name' => undef,
367             'trial_name' => 'test_trial',
368             'trial_type' => undef,
369             'description' => 'test trial',
370             'genotyping_plate_format' => undef,
371             'year' => '2014',
372             'sampling_facility' => undef,
373                 'sampling_trial_sample_type' => undef,
374                 'additional_info' => undef
375           },
376           {
377             'breeding_program_description' => 'test',
378             'location_name' => 'test_location',
379             'location_id' => '23',
380             'project_harvest_date' => '',
381                 'breeding_program_name' => 'test',
382                  'trial_type_value' => undef,
383             'description' => 'another trial for solGS',
384             'year' => '2014',
385             'genotyping_plate_format' => undef,
386             'trial_type_name' => undef,
387             'trial_name' => 'trial2 NaCRRI',
388             'trial_type' => undef,
389             'folder_description' => undef,
390             'trial_type_id' => undef,
391             'genotyping_facility' => undef,
392             'project_planting_date' => '',
393             'trial_id' => 141,
394             'folder_name' => undef,
395             'folder_id' => undef,
396             'genotyping_facility_plate_id' => undef,
397             'design' => 'CRD',
398             'breeding_program_id' => 134,
399             'genotyping_facility_status' => undef,
400             'genotyping_plate_sample_type' => undef,
401             'genotyping_facility_submitted' => undef,
402             'sampling_facility' => undef,
403                 'sampling_trial_sample_type' => undef,
404                 'additional_info' => undef
405           }
406         ], 'trial search test 2');
410 #CXGN::Trial Class METHODS
411 my $locations = CXGN::Trial::get_all_locations($f->bcs_schema());
412 #print STDERR Dumper $locations;
413 my @all_location_names;
414 foreach (@$locations) {
415     push @all_location_names, $_->[1];
417 @all_location_names = sort @all_location_names;
418 #print STDERR Dumper \@all_location_names;
419 my %all_location_names = map {$_=>1} @all_location_names;
420 ok(exists($all_location_names{'Cornell Biotech'}));
421 ok(exists($all_location_names{'test_location'}));
423 my @project_types = CXGN::Trial::get_all_project_types($f->bcs_schema());
424 my @all_project_types;
425 foreach (@project_types) {
426     push @all_project_types, $_->[1];
428 @all_project_types = sort @all_project_types;
429 print STDERR Dumper \@all_project_types;
430 is_deeply(\@all_project_types, [
431           'Advanced Yield Trial',
432           'Clonal Evaluation',
433           'Preliminary Yield Trial',
434           'Screen House',
435           'Seed Multiplication',
436           'Seedling Nursery',
437           'Sensory Trial',
438           'Specialty Trial',
439           'Uniform Yield Trial',
440           'Variety Release Trial',
441           'activity_record',
442           'crossing_block_trial',
443           'crossing_trial',
444           'genetic_gain_trial',
445           'genotyping_project',
446           'genotyping_trial',
447           'grafting_trial',
448           'health_status_trial',
449           'heterosis_trial',
450           'misc_trial',
451           'phenotyping_trial',
452           'pollinating_trial',
453           'storage_trial',
454           'transformation_project'
455       ], "check get_all_project_types");
458 my $stock_count_rs = $f->bcs_schema()->resultset("Stock::Stock")->search( { } );
459 my $initial_stock_count = $stock_count_rs->count();
461 my $number_of_reps = 3;
462 my $stock_list = [ 'test_accession1', 'test_accession2', 'test_accession3' ];
463 # print STDERR "\n\n Before creating trial design\n\n";
464 my $td = CXGN::Trial::TrialDesign->new(
465     {
466         schema => $f->bcs_schema(),
467         trial_name => "anothertrial",
468         stock_list => $stock_list,
469         number_of_reps => $number_of_reps,
470         block_size => 2,
471         design_type => 'RCBD',
472         number_of_blocks => 3,
473     });
475 # print STDERR "\n\n After creating trial design\n\n";
477 my $number_of_plots = $number_of_reps * scalar(@$stock_list);
478 # print STDERR "\n\n before calculating design! \n\n";
480 $td->calculate_design();
481 # print STDERR "\n\nGot passed calculating design! \n\n";
483 my $trial_design = $td->get_design();
484 # print STDERR "\n\nTrial Design  :".Dumper($trial_design)."\n\n";
487 my $breeding_program_row = $f->bcs_schema->resultset("Project::Project")->find( { name => 'test' });
488 my $trial_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($f->bcs_schema, 'Advanced Yield Trial', 'project_type')->cvterm_id();
490 my $new_trial = CXGN::Trial::TrialCreate->new(
491   {
492     dbh => $f->dbh(),
493     chado_schema => $f->bcs_schema(),
494     user_name => 'janedoe', #not implemented
495     program => 'test',
496     trial_year => 2014,
498     trial_description => 'another test trial...',
499     design_type => 'RCBD',
500     trial_type => $trial_type_cvterm_id,
501     trial_location => 'test_location',
502     trial_name => "anothertrial",
503     design => $trial_design,
504     owner_id => 41,
505     operator => 'janedoe'
506   });
508 my $save = $new_trial->save_trial();
510 my $after_design_creation_count = $stock_count_rs->count();
512 is($number_of_plots + $initial_stock_count, $after_design_creation_count, "check stock table count after trial creation.");
514 my $trial_rs = $f->bcs_schema->resultset("Project::Project")->search( { name => 'anothertrial' });
516 my $trial_id = 0;
518 if ($trial_rs->count() > 0) {
519     $trial_id = $trial_rs->first()->project_id();
522 if (!$trial_id) { die "Test failed... could not retrieve trial\n"; }
524 my $trial = CXGN::Trial->new( { bcs_schema => $f->bcs_schema(),
525                                 trial_id => $trial_id });
527 my $breeding_programs = $trial->get_breeding_programs();
528 #print STDERR Dumper $breeding_programs;
529 my @breeding_program_names;
530 foreach (@$breeding_programs){
531     push @breeding_program_names, $_->[1];
533 @breeding_program_names = sort @breeding_program_names;
534 #print STDERR Dumper \@breeding_program_names;
535 is_deeply(\@breeding_program_names, ['test'], "check breeding_program_names");
537 print STDERR Dumper($trial_design);
538 my $plot_name = $trial_design->{9}->{plot_name};
539 print STDERR "PLOT NAME = $plot_name\n";
540 my $rs = $f->bcs_schema()->resultset("Stock::Stock")->search( { name => $plot_name });
541 is($rs->count(), 1, "check that a single plot was saved for a single name.");
542 is($rs->first->name(), $plot_name, 'check that plot name was saved correctly');
544 if ($rs->count() > 0) {
545     print STDERR "antohertrial1 has id ".$rs->first()->stock_id()."\n";
547 else {
548     print STDERR "anothertrial1 does not exist!\n";
551 # Test addition and deletion of phenotypic data
553 my $phenotype_count_before_store = $trial->phenotype_count();
555 ok($trial->phenotype_count() == 0, "trial has no phenotype data");
556 my $plotlist_ref = [ $trial_design->{7}->{plot_name}, $trial_design->{8}->{plot_name}, $trial_design->{9}->{plot_name} ];
558 my $traitlist_ref = [ 'root number|CO_334:0000011', 'dry yield|CO_334:0000014' ];
560 my %plot_trait_value = ( $trial_design->{7}->{plot_name} => { 'root number|CO_334:0000011'  => [0,''], 'dry yield|CO_334:0000014' => [30,''] },
561                            $trial_design->{8}->{plot_name} => { 'root number|CO_334:0000011'  => [10,''], 'dry yield|CO_334:0000014' => [40,''] },
562                            $trial_design->{9}->{plot_name} => { 'root number|CO_334:0000011'  => [20,''], 'dry yield|CO_334:0000014' => [50,''] },
563     );
566 my %metadata = ( operator => 'johndoe', date => '20141223' );
568 my $total_phenotypes_before_store = $trial->total_phenotypes();
570 my $lp = CXGN::Phenotypes::StorePhenotypes->new(
571     basepath=>$f->config->{basepath},
572     dbhost=>$f->config->{dbhost},
573     dbname=>$f->config->{dbname},
574     dbuser=>$f->config->{dbuser},
575     dbpass=>$f->config->{dbpass},
576     temp_file_nd_experiment_id=>$f->config->{cluster_shared_tempdir}."/test_temp_nd_experiment_id_delete",
577     bcs_schema=>$f->bcs_schema,
578     metadata_schema=>$f->metadata_schema,
579     phenome_schema=>$f->phenome_schema,
580     user_id=>41,
581     stock_list=>$plotlist_ref,
582     trait_list=>$traitlist_ref,
583     values_hash=>\%plot_trait_value,
584     has_timestamps=>0,
585     overwrite_values=>0,
586     metadata_hash=>\%metadata,
587     composable_validation_check_name=>$f->config->{composable_validation_check_name}
590 $lp->store();
592 my $total_phenotypes = $trial->total_phenotypes();
594 my $trial_phenotype_count = $trial->phenotype_count();
596 #print STDERR "Total phentoypes: $total_phenotypes\n";
597 #print STDERR "Trial phentoypes: $trial_phenotype_count\n";
598 is($total_phenotypes, $total_phenotypes_before_store + 6, "total phenotype data");
599 is($trial_phenotype_count, 6, "trial has phenotype data");
601 my $tn = CXGN::Trial->new( { bcs_schema => $f->bcs_schema(),
602                                 trial_id => $trial_id });
604 my $traits_assayed  = $tn->get_traits_assayed();
605 my @traits_assayed_names;
606 #print STDERR Dumper $traits_assayed;
607 foreach (@$traits_assayed) {
608     push @traits_assayed_names, $_->[1];
610 @traits_assayed_names = sort @traits_assayed_names;
611 #print STDERR Dumper \@traits_assayed_names;
612 is_deeply(\@traits_assayed_names, ['dry yield|CO_334:0000014', 'root number counting|CO_334:0000011'], 'check traits assayed' );
614 my @pheno_for_trait = $tn->get_phenotypes_for_trait(70727);
615 my @pheno_for_trait_sorted = sort {$a <=> $b} @pheno_for_trait;
616 #print STDERR Dumper \@pheno_for_trait_sorted;
617 is_deeply(\@pheno_for_trait_sorted, ['30','40','50'], 'check traits assayed' );
619 my $plot_pheno_for_trait = $tn->get_stock_phenotypes_for_traits([70727], 'all', ['plot_of','plant_of'], 'accession', 'subject');
620 print STDERR Dumper "PHENO FOR TRAIT: $plot_pheno_for_trait\n";
621 my @phenotyped_stocks;
622 my @phenotyped_stocks_values;
623 foreach (@$plot_pheno_for_trait) {
624     push @phenotyped_stocks, $_->[1];
625     push @phenotyped_stocks_values, $_->[7];
627 @phenotyped_stocks = sort @phenotyped_stocks;
628 @phenotyped_stocks_values = sort @phenotyped_stocks_values;
629 my @expected_sorted_stocks = sort ($trial_design->{7}->{plot_name}, $trial_design->{8}->{plot_name}, $trial_design->{9}->{plot_name});
630 print STDERR Dumper \@phenotyped_stocks;
631 print STDERR Dumper \@expected_sorted_stocks;
632 is_deeply(\@phenotyped_stocks, \@expected_sorted_stocks, "check phenotyped stocks");
633 is_deeply(\@phenotyped_stocks_values, ['30', '40', '50'], "check phenotyped stocks 2");
635 my $trial_experiment_count = $trial->get_experiment_count();
636 print STDERR $trial_experiment_count."\n";
637 is($trial_experiment_count, 4, "check get_experiment_count");
639 my $location_type_id = $trial->get_location_type_id();
640 #print STDERR $location_type_id."\n";
641 is($location_type_id, 76462, "check get_location_type_id");
643 my $year_type_id = $trial->get_year_type_id();
644 #print STDERR $year_type_id."\n";
645 is($year_type_id, 76395, "check get_year_type_id");
647 my $bp_trial_rel_cvterm_id = $trial->get_breeding_program_trial_relationship_cvterm_id();
648 #print STDERR $bp_trial_rel_cvterm_id,"\n";
649 is($bp_trial_rel_cvterm_id, 76448, "check get_breeding_program_trial_relationship_cvterm_id");
651 my $bp_cvterm_id = $trial->get_breeding_program_cvterm_id();
652 #print STDERR $bp_cvterm_id."\n";
653 is($bp_cvterm_id, 76440, "check get_breeding_program_cvterm_id");
655 my $folder = $trial->get_folder();
656 #print STDERR $folder->name."\n";
657 is($folder->name, 'test', 'check get_folder when no folder associated. should return bp name');
659 my $folder = CXGN::Trial::Folder->create({
660   bcs_schema => $f->bcs_schema(),
661   parent_folder_id => 0,
662   name => 'F1',
663   breeding_program_id => $breeding_program_row->project_id(),
665 my $folder_id = $folder->folder_id();
667 my $folder = CXGN::Trial::Folder->new({
668     bcs_schema => $f->bcs_schema(),
669     folder_id => $trial_id
672 $folder->associate_parent($folder_id);
674 my $folder = $trial->get_folder();
675 #print STDERR $folder->name."\n";
676 is($folder->name, 'F1', 'check get_folder after folder associated');
678 my $harvest_date_cvterm_id = $trial->get_harvest_date_cvterm_id();
679 #print STDERR $harvest_date_cvterm_id."\n";
680 is($harvest_date_cvterm_id, 76495, "check get_harvest_date_cvterm_id");
682 my $planting_date_cvterm_id = $trial->get_planting_date_cvterm_id();
683 #print STDERR $planting_date_cvterm_id."\n";
684 is($planting_date_cvterm_id, 76496, "check get_planting_date_cvterm_id");
686 my $design_type = $trial->get_design_type();
687 #print STDERR $design_type."\n";
688 is($design_type, 'RCBD', 'check get_design_type');
690 my $trial_accessions = $trial->get_accessions();
691 #print STDERR Dumper $trial_accessions;
692 my @trial_accession_names;
693 foreach (@$trial_accessions) {
694     push @trial_accession_names, $_->{'accession_name'};
696 @trial_accession_names = sort @trial_accession_names;
697 is_deeply(\@trial_accession_names, ['test_accession1', 'test_accession2', 'test_accession3'], "check get_accessions");
699 my $trial_plots = $trial->get_plots();
700 my @trial_plot_names;
701 foreach (@$trial_plots){
702     push @trial_plot_names, $_->[1];
704 @trial_plot_names = sort @trial_plot_names;
705 print STDERR "Num plots: ".scalar(@trial_plot_names)."\n";
706 is(scalar(@trial_plot_names), 9, 'check number of plots');
708 print STDERR "DESIGN NOW: ".Dumper($trial_design);
711 #my @expected_sorted_plots = sort ($trial_design->{11}->{plot_name}, $trial_design->{12}->{plot_name}, $trial_design->{13}->{plot_name}, $trial_design->{21}->{plot_name}, $trial_design->{22}->{plot_name}, $trial_design->{23}->{plot_name}, $trial_design->{31}->{plot_name}, $trial_design->{32}->{plot_name}, $trial_design->{33}->{plot_name});
712 my @expected_sorted_plots = sort ($trial_design->{1}->{plot_name}, $trial_design->{2}->{plot_name}, $trial_design->{3}->{plot_name}, $trial_design->{4}->{plot_name}, $trial_design->{5}->{plot_name}, $trial_design->{6}->{plot_name}, $trial_design->{7}->{plot_name}, $trial_design->{8}->{plot_name}, $trial_design->{9}->{plot_name});
713 print STDERR "TRIAL PLOT NAMES: ". Dumper \@trial_plot_names;
714 print STDERR "TRIAL SORTED PLOTS: ".Dumper \@expected_sorted_plots;
715 is_deeply(\@trial_plot_names, \@expected_sorted_plots, 'Check get_plots 1');
717 my $trial_controls = $trial->get_controls();
718 #print STDERR Dumper $trial_controls;
719 is_deeply($trial_controls, [], "check get_controls");
721 #add plant entries
722 my $num_plants_add = 3;
723 $trial->create_plant_entities($num_plants_add);
724 #print STDERR Dumper($trial);
725 ok($trial->has_plant_entries(), "check if plant entries created.");
727 my $trial = CXGN::Trial->new( { bcs_schema => $f->bcs_schema(), trial_id => $trial_id });
728 my $plants = $trial->get_plants();
729 #print STDERR Dumper $plants;
730 is(scalar(@$plants), $number_of_plots*3, "check if the right number of plants was created");
732 my $plantlist_ref = [ $trial_design->{7}->{plot_name}.'_plant_2', $trial_design->{8}->{plot_name}.'_plant_2', $trial_design->{9}->{plot_name}.'_plant_1' ];
734 my $traitlist_ref = [ 'root number|CO_334:0000011', 'dry yield|CO_334:0000014', 'harvest index|CO_334:0000015' ];
736 my %plant_trait_value = ( $trial_design->{7}->{plot_name}.'_plant_2' => { 'root number|CO_334:0000011'  => [12,''], 'dry yield|CO_334:0000014' => [30,''], 'harvest index|CO_334:0000015' => [2,''] },
737     $trial_design->{8}->{plot_name}.'_plant_2' => { 'root number|CO_334:0000011'  => [10,''], 'dry yield|CO_334:0000014' => [40,''], 'harvest index|CO_334:0000015' => [3,''] },
738     $trial_design->{9}->{plot_name}.'_plant_1' => { 'root number|CO_334:0000011'  => [20,''], 'dry yield|CO_334:0000014' => [50,''], 'harvest index|CO_334:0000015' => [7,''] },
741 my %metadata = ( operator => 'johndoe', date => '20141225' );
743 my $total_phenotype_count_before_save2 = $trial->total_phenotypes();
745 my $lp = CXGN::Phenotypes::StorePhenotypes->new({
746     basepath=>$f->config->{basepath},
747     dbhost=>$f->config->{dbhost},
748     dbname=>$f->config->{dbname},
749     dbuser=>$f->config->{dbuser},
750     dbpass=>$f->config->{dbpass},
751     temp_file_nd_experiment_id=>$f->config->{cluster_shared_tempdir}."/test_temp_nd_experiment_id_delete",
752     bcs_schema=>$f->bcs_schema,
753     metadata_schema=>$f->metadata_schema,
754     phenome_schema=>$f->phenome_schema,
755     user_id=>41,
756     stock_list=>$plantlist_ref,
757     trait_list=>$traitlist_ref,
758     values_hash=>\%plant_trait_value,
759     has_timestamps=>0,
760     overwrite_values=>0,
761     metadata_hash=>\%metadata,
762     composable_validation_check_name=>$f->config->{composable_validation_check_name}
764 $lp->store();
766 my $total_phenotypes = $trial->total_phenotypes();
768 my $trial_phenotype_count = $trial->phenotype_count();
770 print STDERR "Total phentoypes: $total_phenotypes\n";
771 print STDERR "Trial phentoypes: $trial_phenotype_count\n";
772 is($total_phenotypes, $total_phenotype_count_before_save2 + 9, "total phenotype data");
773 is($trial_phenotype_count, 15, "trial has phenotype data");
775 my $tn = CXGN::Trial->new( { bcs_schema => $f->bcs_schema(),
776                                 trial_id => $trial_id });
778 my $traits_assayed  = $tn->get_traits_assayed();
779 my @traits_assayed_sorted = sort {$a->[0] cmp $b->[0]} @$traits_assayed;
780 #print STDERR Dumper \@traits_assayed_sorted;
782 my @traits_assayed_check = (['70668','Harvest index variable'],['70706','Root number counting'],['70727','Dry yield']);
784 #is_deeply(\@traits_assayed_sorted, \@traits_assayed_check, 'check traits assayed' );
786 my @pheno_for_trait = $tn->get_phenotypes_for_trait(70706);
787 my @pheno_for_trait_sorted = sort {$a <=> $b} @pheno_for_trait;
788 #print STDERR Dumper \@pheno_for_trait_sorted;
789 my @pheno_for_trait_check = (0, 10, 10, 12, 20, 20);
790 is_deeply(\@pheno_for_trait_sorted, \@pheno_for_trait_check, 'check traits assayed' );
792 my @pheno_for_trait = $tn->get_phenotypes_for_trait(70668);
793 my @pheno_for_trait_sorted = sort {$a <=> $b} @pheno_for_trait;
794 #print STDERR Dumper \@pheno_for_trait_sorted;
795 my @pheno_for_trait_check = (2,3,7);
796 is_deeply(\@pheno_for_trait_sorted, \@pheno_for_trait_check, 'check traits assayed' );
798 my @pheno_for_trait = $tn->get_phenotypes_for_trait(70727);
799 my @pheno_for_trait_sorted = sort {$a <=> $b} @pheno_for_trait;
800 #print STDERR Dumper \@pheno_for_trait_sorted;
801 my @pheno_for_trait_check = (30,30,40,40,50,50);
802 is_deeply(\@pheno_for_trait_sorted, \@pheno_for_trait_check, 'check traits assayed' );
805 my $retrieve_accessions = $trial->get_accessions();
806 #print STDERR Dumper $retrieve_accessions;
807 my @get_accessions_names;
808 foreach (@$retrieve_accessions){
809     push @get_accessions_names, $_->{'accession_name'};
811 @get_accessions_names = sort @get_accessions_names;
812 #print STDERR Dumper \@get_accessions_names;
813 is_deeply(\@get_accessions_names, [
814           'test_accession1',
815           'test_accession2',
816           'test_accession3'
817         ], 'check get_accessions');
819 my $retrieve_plots = $trial->get_plots();
820 #print STDERR Dumper $retrieve_plots;
821 my @get_plot_names;
822 foreach (@$retrieve_plots){
823     push @get_plot_names, $_->[1];
825 @get_plot_names = sort @get_plot_names;
826 print STDERR "PLOT NAMES NOW: ". Dumper \@get_plot_names;
827 print STDERR "EXPECTED PLOT NAMES NOW: ".Dumper \@expected_sorted_plots;
828 is_deeply(\@get_plot_names, \@expected_sorted_plots, "check get_plots 2");
830 my @expected_plants;
831 foreach (@expected_sorted_plots) {
832     for my $i (1..$num_plants_add){
833         push @expected_plants, $_."_plant_".$i;
834     }
836 my @expected_sorted_plants = sort @expected_plants;
838 my $retrieve_plants = $trial->get_plants();
839 #print STDERR Dumper $retrieve_plants;
840 my @get_plant_names;
841 foreach (@$retrieve_plants){
842     push @get_plant_names, $_->[1];
844 @get_plant_names = sort @get_plant_names;
845 print STDERR Dumper \@get_plant_names;
846 is_deeply(\@get_plant_names, \@expected_sorted_plants, "check get_plants()");
849 # check trial deletion - first, delete associated phenotypes
851 my $del_ret = $trial->delete_phenotype_data($f->config->{basepath}, $f->config->{dbhost}, $f->config->{dbname}, $f->config->{dbuser}, $f->config->{dbpass}, $f->config->{cluster_shared_tempdir}."/test_temp_nd_experiment_id_delete");
852 ok(!$del_ret);
853 print STDERR Dumper $del_ret;
855 ok($trial->phenotype_count() ==0, "phenotype data deleted");
857 is($trial->total_phenotypes(), $total_phenotypes - $trial_phenotype_count, "check total phenotypes");
859 # check trial layout deletion
861 my $error = $trial->delete_field_layout();
863 ok(! $error, "no error upon layout deletion");
865 my $after_design_deletion_count = $stock_count_rs->count();
867 is( $after_design_deletion_count, $initial_stock_count, "check that stock counts before layout creation and after deletion match");
869 # test name accessors
871 is($trial->get_name(), "anothertrial");
872 $trial->set_name("anothertrial modified");
873 is($trial->get_name(), "anothertrial modified");
875 # test description accessors
877 my $desc = $trial->get_description();
879 ok($desc == "test_trial", "another test trial...");
881 $trial->set_description("blablabla");
883 is($trial->get_description(), "blablabla", "description setter test");
885 # test harvest_date accessors
887 $trial->set_harvest_date('2016/01/01 12:20:10');
888 my $harvest_date = $trial->get_harvest_date();
889 #print STDERR Dumper $harvest_date;
890 is($harvest_date, '2016-January-01 12:20:10', "set harvest_date test");
891 $trial->remove_harvest_date('2016/01/01 12:20:10');
892 $harvest_date = $trial->get_harvest_date();
893 ok(!$harvest_date, "test remove harvest_date");
895 # test planting_date accessors
897 $trial->set_planting_date('2016/01/01 12:20:10');
898 my $planting_date = $trial->get_planting_date();
899 #print STDERR Dumper $planting_date;
900 is($planting_date, '2016-January-01 12:20:10', "set harvest_date test");
901 $trial->remove_planting_date('2016/01/01 12:20:10');
902 $planting_date = $trial->get_planting_date();
903 ok(!$planting_date, "test remove planting_date");
905 # test transplanting_date accessors
906 $trial->set_transplanting_date('2016/01/02 12:21:11');
907 my $transplanting_date = $trial->get_transplanting_date();
908 #print STDERR Dumper $transplanting_date;
909 is($transplanting_date, '2016-January-02 12:21:11', "set transplanting_date test");
910 $trial->set_transplanting_date('2016/01/03 12:21:11');
911 $transplanting_date = $trial->get_transplanting_date();
912 is($transplanting_date, '2016-January-03 12:21:11', "update/set transplanting_date test");
913 $trial->remove_transplanting_date('2016/01/03 12:21:11');
914 $transplanting_date = $trial->get_transplanting_date();
915 ok(!$transplanting_date, "test remove transplanting_date");
917 # test transplantnig date accessors visibility
918 $trial->set_transplanting_date('2023/06/15 12:21:14');
919 my $transplanting_date = $trial->get_transplanting_date();
920 is($transplanting_date, '2023-June-15 12:21:14', "set transplanting_date test");
921 $trial->set_transplanting_date('2023/06/16 12:21:14');
922 $transplanting_date = $trial->get_transplanting_date();
923 is($transplanting_date, '2023-June-16 12:21:14', "update/set transplanting_date test");
924 $trial->remove_transplanting_date('2023/06/16 12:21:14');
925 $transplanting_date = $trial->get_transplanting_date();
926 ok(!$transplanting_date, "remove transplanting_date test");
928 #test transplanting_date accessors
929 $trial->set_transplanting_date('2017/02/03 12:21:12');
930 my $transplanting_date = $trial->get_transplanting_date();
931 #print STDERR Dumper $transplanting_date;
932 is($transplanting_date, '2017-February-03 12:21:12', "set transplanting_date test");
933 $trial->set_transplanting_date('2017/02/04 12:21:13');
934 $transplanting_date = $trial->get_transplanting_date();
935 is($transplanting_date, '2017-February-04 12:21:13', "update/set transplanting_date test");
936 $trial->remove_transplanting_date('2017/02/04 12:21:13');
937 $transplanting_date = $trial->get_transplanting_date();
938 ok(!$transplanting_date, "test remove transplanting_date");
940 # test year accessors
942 is($trial->get_year(), 2014, "get year test");
944 $trial->set_year(2013);
945 is($trial->get_year(), 2013, "set year test");
947 # test planting date accessors
949 #$trial-> set_planting_date ('2016/01/02 12:21:12');
950 #my $planting_date = $trial -> get_planting_date();
951 #print STDERR Dumper $planting_date;
952 #is($planting_date, '2016-January-02 12:21:12', "set planting_date test");
953 #$trial-> set_planting_date('2016/01/04 12:21:12');
954 #$planting_date = $trial-> get_planting_date();
955 #is($planting_date, '2016-January-04 12:21:12', "update/set planting_date test");
956 #$trial-> remove_planting_date ('2016/01/04 12:21:12');
957 #$planting_date = $trial-> get_planting_date();
958 #ok(!$planting_date, "test remove planting_date");
960 # test breeding program accessors
962 is($trial->get_breeding_program(), 'test', "get breeding program test");
964 $trial->set_breeding_program($breeding_program_row->project_id());
965 is($trial->get_breeding_program(), 'test', "set breeding program test");
967 # test location accessors
969 is_deeply($trial->get_location(), [ 23, 'test_location' ], "get location");
971 $trial->set_location(23);
972 is_deeply($trial->get_location(), [ 23, 'test_location' ], "set location");
974 # test project type accessors
976 is($trial->get_project_type()->[1], "Advanced Yield Trial", "get type test");
978 my $error = $trial->set_project_type("77106");
980 is($trial->get_project_type()->[1], "Clonal Evaluation", "set type test");
982 print STDERR "DELETING PROJECT ENTRY... ";
983 $trial->delete_project_entry();
984 print STDERR "Done.\n";
986 my $deleted_trial;
987 eval {
988      $deleted_trial = CXGN::Trial->new( { bcs_schema => $f->bcs_schema, trial_id=>$trial_id });
991 if ($@) { print "An error occurred: $@\n"; }
992 ok($@, "deleted trial id (".$@.")");
995 done_testing();