start fixing test for multi cat phenotype upload.
[sgn.git] / t / unit_fixture / CXGN / Trial / DeriveTrait.t
blob677851853e52e4a85e53d958833040dfc4392839
2 use strict;
4 use Test::More;
5 use lib 't/lib';
6 use SGN::Test::Fixture;
7 use Data::Dumper;
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')
27    ->new({
28     description => $trial_location,
29          }));
30 ok($location->insert());
32 # create stocks for the trial
33 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
34    ->create_with({
35        name   => 'accession',
36        cv     => 'stock_type',
37 }));
38 my @stock_names;
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")
44    ->find_or_create( {
45        genus => 'Test_genus',
46        species => 'Test_genus test_species',
47 }, ));
49 # create some test stocks
50 foreach my $stock_name (@stock_names) {
51     my $accession_stock = $chado_schema->resultset('Stock::Stock')
52         ->create({
53             organism_id => $organism->organism_id,
54             name       => $stock_name,
55             uniquename => $stock_name,
56             type_id     => $accession_cvterm->cvterm_id,
57                  });
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,
75     dbh => $dbh,
76     owner_id => 41,
77     design => $design,
78     program => "test",
79     trial_year => "2016",
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",
84     operator => "janedoe"
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();
92 my @trial_plot_names;
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;
109 my $parsed_data = {
110             $plants[0] => {
111                                 'dry matter content|CO_334:0000092' => [
112                                                                      '23',
113                                                                      '2017-02-11 11:12:20-0500'
114                                                                    ]
115                                                    },
116             $plants[1] => {
117                                'dry matter content|CO_334:0000092' => [
118                                                                     '28',
119                                                                     '2017-02-11 11:13:20-0500'
120                                                                   ]
121                                                   },
122             $plants[2] => {
123                               'dry matter content|CO_334:0000092' => [
124                                                                    '30',
125                                                                    '2017-02-11 11:15:20-0500'
126                                                                  ]
127                                                  },
128             };
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,
142     user_id=>41,
143     stock_list=>\@plants,
144     trait_list=>\@traits,
145     values_hash=>$parsed_data,
146     has_timestamps=>1,
147     overwrite_values=>0,
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, [
161           [
162             70741,
163             'dry matter content percentage|CO_334:0000092', [], 3, undef, undef
164           ]
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');
178 my @values_to_store;
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,
201     user_id=>41,
202     stock_list=>$plots_ret,
203     trait_list=>$traits,
204     values_hash=>$store_hash,
205     has_timestamps=>0,
206     overwrite_values=>1,
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");
225 done_testing();