6 use SGN::Test::Fixture;
8 use CXGN::Phenotypes::StorePhenotypes;
9 use CXGN::BreedersToolbox::DeriveTrait;
10 use SGN::Model::Cvterm;
12 my $fix = SGN::Test::Fixture->new();
14 is(ref($fix->config()), "HASH", 'hashref check');
16 BEGIN {use_ok('CXGN::Trial::TrialCreate');}
17 BEGIN {use_ok('CXGN::Trial::TrialLayout');}
18 BEGIN {use_ok('CXGN::Trial::TrialDesign');}
19 BEGIN {use_ok('CXGN::Trial::TrialLookup');}
20 ok(my $chado_schema = $fix->bcs_schema);
21 ok(my $phenome_schema = $fix->phenome_schema);
22 ok(my $dbh = $fix->dbh);
24 # create a location for the trial
25 ok(my $trial_location = "test_location_for_trial_derive_trait");
26 ok(my $location = $chado_schema->resultset('NaturalDiversity::NdGeolocation')
28 description => $trial_location,
30 ok($location->insert());
32 # create stocks for the trial
33 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
39 for (my $i = 1; $i <= 10; $i++) {
40 push(@stock_names, "test_stock_for_trial_derive_trait".$i);
43 ok(my $organism = $chado_schema->resultset("Organism::Organism")
45 genus => 'Test_genus',
46 species => 'Test_genus test_species',
49 # create some test stocks
50 foreach my $stock_name (@stock_names) {
51 my $accession_stock = $chado_schema->resultset('Stock::Stock')
53 organism_id => $organism->organism_id,
55 uniquename => $stock_name,
56 type_id => $accession_cvterm->cvterm_id,
61 ok(my $trial_design = CXGN::Trial::TrialDesign->new(), "create trial design object");
62 ok($trial_design->set_trial_name("test_trial_derive_trait"), "set trial name");
63 ok($trial_design->set_stock_list(\@stock_names), "set stock list");
64 ok($trial_design->set_plot_start_number(1), "set plot start number");
65 ok($trial_design->set_plot_number_increment(1), "set plot increment");
66 ok($trial_design->set_number_of_blocks(2), "set block number");
67 ok($trial_design->set_design_type("RCBD"), "set design type");
68 ok($trial_design->calculate_design(), "calculate design");
69 ok(my $design = $trial_design->get_design(), "retrieve design");
71 print STDERR Dumper $design;
73 ok(my $trial_create = CXGN::Trial::TrialCreate->new({
74 chado_schema => $chado_schema,
80 trial_description => "test_trial_derive_trait description",
81 trial_location => "test_location_for_trial_derive_trait",
82 trial_name => "test_trial_derive_trait",
83 design_type => "RCBD",
85 }), "create trial object");
87 ok(my $save = $trial_create->save_trial(), "save trial");
88 my $trial_id = $save->{'trial_id'};
89 my $trial = CXGN::Trial->new({ bcs_schema => $fix->bcs_schema(), trial_id => $trial_id });
91 my $trial_plots = $trial->get_plots();
93 foreach (@$trial_plots){
94 push @trial_plot_names, $_->[1];
96 @trial_plot_names = sort @trial_plot_names;
97 is(scalar(@trial_plot_names), 20, "check num plots saved");
99 my $num_plants_add = 2;
100 $trial->create_plant_entities($num_plants_add);
102 my %phenotype_metadata;
103 $phenotype_metadata{'operator'}="janedoe";
104 $phenotype_metadata{'date'}="2017-02-16_01:10:56";
105 my @plots = sort ( $trial_plot_names[0], $trial_plot_names[1], $trial_plot_names[2] );
106 my @plants = sort ( $trial_plot_names[0]."_plant_2", $trial_plot_names[1]."_plant_2", $trial_plot_names[2]."_plant_2" );
107 print STDERR Dumper \@plots;
111 'dry matter content|CO_334:0000092' => [
113 '2017-02-11 11:12:20-0500'
117 'dry matter content|CO_334:0000092' => [
119 '2017-02-11 11:13:20-0500'
123 'dry matter content|CO_334:0000092' => [
125 '2017-02-11 11:15:20-0500'
130 my @traits = ( 'dry matter content|CO_334:0000092' );
132 my $store_phenotypes = CXGN::Phenotypes::StorePhenotypes->new(
133 basepath=>$fix->config->{basepath},
134 dbhost=>$fix->config->{dbhost},
135 dbname=>$fix->config->{dbname},
136 dbuser=>$fix->config->{dbuser},
137 dbpass=>$fix->config->{dbpass},
138 temp_file_nd_experiment_id=>$fix->config->{cluster_shared_tempdir}."/test_temp_nd_experiment_id_delete",
139 bcs_schema=>$fix->bcs_schema,
140 metadata_schema=>$fix->metadata_schema,
141 phenome_schema=>$fix->phenome_schema,
143 stock_list=>\@plants,
144 trait_list=>\@traits,
145 values_hash=>$parsed_data,
148 metadata_hash=>\%phenotype_metadata,
149 composable_validation_check_name=>$fix->config->{composable_validation_check_name}
152 my ($stored_phenotype_error_msg, $stored_phenotype_success) = $store_phenotypes->store();
153 ok(!$stored_phenotype_error_msg, "check that store pheno spreadsheet works");
155 my $tn = CXGN::Trial->new( { bcs_schema => $fix->bcs_schema(),
156 trial_id => $trial_id });
157 my $traits_assayed = $tn->get_traits_assayed();
158 my @traits_assayed_sorted = sort {$a->[0] cmp $b->[0]} @$traits_assayed;
159 print STDERR Dumper \@traits_assayed_sorted;
160 is_deeply(\@traits_assayed_sorted, [
163 'dry matter content percentage|CO_334:0000092', [], 3, undef, undef
165 ], "check upload worked");
168 my $method = 'arithmetic_mean';
169 my $rounding = 'round';
170 my $trait_name = 'dry matter content|CO_334:0000092';
171 my $derive_trait = CXGN::BreedersToolbox::DeriveTrait->new({bcs_schema=>$fix->bcs_schema, trait_name=>$trait_name, trial_id=>$trial_id, method=>$method, rounding=>$rounding});
172 my ($info, $plots_ret, $traits, $store_hash) = $derive_trait->generate_plot_phenotypes();
173 #print STDERR Dumper $info;
175 my @sorted_plots_ret = sort @$plots_ret;
176 is_deeply(\@plots, \@sorted_plots_ret, 'check generated plots');
179 foreach my $info_n (@$info) {
180 push @values_to_store, $info_n->{'value_to_store'};
182 @values_to_store = sort @values_to_store;
183 print STDERR Dumper \@values_to_store;
184 is_deeply(\@values_to_store, [23,28,30], "check returned values");
186 print STDERR Dumper $store_hash;
188 my %phenotype_metadata;
189 $phenotype_metadata{'operator'}='janedoe';
190 $phenotype_metadata{'date'}="2017-02-16_03:10:59";
191 my $store_phenotypes = CXGN::Phenotypes::StorePhenotypes->new(
192 basepath=>$fix->config->{basepath},
193 dbhost=>$fix->config->{dbhost},
194 dbname=>$fix->config->{dbname},
195 dbuser=>$fix->config->{dbuser},
196 dbpass=>$fix->config->{dbpass},
197 temp_file_nd_experiment_id=>$fix->config->{cluster_shared_tempdir}."/test_temp_nd_experiment_id_delete",
198 bcs_schema=>$fix->bcs_schema,
199 metadata_schema=>$fix->metadata_schema,
200 phenome_schema=>$fix->phenome_schema,
202 stock_list=>$plots_ret,
204 values_hash=>$store_hash,
207 metadata_hash=>\%phenotype_metadata,
208 composable_validation_check_name=>$fix->config->{composable_validation_check_name}
211 my ($store_error, $store_success) = $store_phenotypes->store();
212 ok(!$store_error, "check that store pheno spreadsheet works");
214 my $trait_id = SGN::Model::Cvterm->get_cvterm_row_from_trait_name($fix->bcs_schema, 'dry matter content|CO_334:0000092')->cvterm_id();
215 my $all_stock_phenotypes_for_dry_matter_content = $tn->get_stock_phenotypes_for_traits([$trait_id], 'all', ['plot_of','plant_of'], 'accession', 'subject');
216 #print STDERR Dumper $all_stock_phenotypes_for_dry_matter_content;
217 ok(scalar(@$all_stock_phenotypes_for_dry_matter_content) == 6, "check if num phenotype saved is correct");
218 my $plant_phenotypes_for_dry_matter_content = $tn->get_stock_phenotypes_for_traits([$trait_id], 'plant', ['plant_of'], 'accession', 'subject');
219 #print STDERR Dumper $plant_phenotypes_for_dry_matter_content;
220 ok(scalar(@$plant_phenotypes_for_dry_matter_content) == 3, "check num phenotype for plant is correct");
221 my $plot_phenotypes_for_dry_matter_content = $tn->get_stock_phenotypes_for_traits([$trait_id], 'plot', ['plot_of'], 'accession', 'subject');
222 #print STDERR Dumper $plot_phenotypes_for_dry_matter_content;
223 ok(scalar(@$plot_phenotypes_for_dry_matter_content) == 3, "check num phenotype for plot is correct");