Merge pull request #1716 from solgenomics/topic/seedlot_upload_fix
[sgn.git] / t / unit_fixture / CXGN / Trial / TrialCreate.t
blob48a59a6833cf71fabb1c306301f5d215bbd94acc
2 use strict;
4 use Test::More;
5 use lib 't/lib';
6 use SGN::Test::Fixture;
7 use JSON::Any;
8 use Data::Dumper;
10 my $fix = SGN::Test::Fixture->new();
12 is(ref($fix->config()), "HASH", 'hashref check');
14 BEGIN {use_ok('CXGN::Trial::TrialCreate');}
15 BEGIN {use_ok('CXGN::Trial::TrialLayout');}
16 BEGIN {use_ok('CXGN::Trial::TrialDesign');}
17 BEGIN {use_ok('CXGN::Trial::TrialLookup');}
18 ok(my $chado_schema = $fix->bcs_schema);
19 ok(my $phenome_schema = $fix->phenome_schema);
20 ok(my $dbh = $fix->dbh);
22 # create a location for the trial
23 ok(my $trial_location = "test_location_for_trial");
24 ok(my $location = $chado_schema->resultset('NaturalDiversity::NdGeolocation')
25    ->new({
26     description => $trial_location,
27          }));
28 ok($location->insert());
30 # create stocks for the trial
31 ok(my $accession_cvterm = $chado_schema->resultset("Cv::Cvterm")
32    ->create_with({
33        name   => 'accession',
34        cv     => 'stock_type',
35       
36                  }));
37 my @stock_names;
38 for (my $i = 1; $i <= 10; $i++) {
39     push(@stock_names, "test_stock_for_trial".$i);
42 #create stocks for genotyping trial
43 my @genotyping_stock_names;
44 for (my $i = 1; $i <= 10; $i++) {
45     push(@genotyping_stock_names, "test_stock_for_genotyping_trial".$i);
49 ok(my $organism = $chado_schema->resultset("Organism::Organism")
50    ->find_or_create( {
51        genus => 'Test_genus',
52        species => 'Test_genus test_species',
53                      }, ));
55 # create some test stocks
56 foreach my $stock_name (@stock_names) {
57     my $accession_stock = $chado_schema->resultset('Stock::Stock')
58         ->create({
59             organism_id => $organism->organism_id,
60             name       => $stock_name,
61             uniquename => $stock_name,
62             type_id     => $accession_cvterm->cvterm_id,
63                  });
66 # create some genotyping test stocks
67 foreach my $stock_name (@genotyping_stock_names) {
68     my $accession_stock = $chado_schema->resultset('Stock::Stock')
69         ->create({
70             organism_id => $organism->organism_id,
71             name       => $stock_name,
72             uniquename => $stock_name,
73             type_id     => $accession_cvterm->cvterm_id,
74                  });
78 ok(my $trial_design = CXGN::Trial::TrialDesign->new(), "create trial design object");
79 ok($trial_design->set_trial_name("test_trial"), "set trial name");
80 ok($trial_design->set_stock_list(\@stock_names), "set stock list");
81 ok($trial_design->set_plot_start_number(1), "set plot start number");
82 ok($trial_design->set_plot_number_increment(1), "set plot increment");
83 ok($trial_design->set_number_of_blocks(2), "set block number");
84 ok($trial_design->set_design_type("RCBD"), "set design type");
85 ok($trial_design->calculate_design(), "calculate design");
86 ok(my $design = $trial_design->get_design(), "retrieve design");
88 my $ayt_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'Advanced Yield Trial', 'project_type')->cvterm_id();
90 ok(my $trial_create = CXGN::Trial::TrialCreate->new({
91     chado_schema => $chado_schema,
92     dbh => $dbh,
93     user_name => "johndoe", #not implemented
94     design => $design,  
95     program => "test",
96     trial_year => "2015",
97     trial_description => "test description",
98     trial_location => "test_location_for_trial",
99     trial_name => "new_test_trial_name",
100     trial_type=>$ayt_cvterm_id,
101     design_type => "RCBD",
102     operator => "janedoe"
103                                                     }), "create trial object");
105 my $save = $trial_create->save_trial();
106 ok($save->{'trial_id'}, "save trial");
108 ok(my $trial_lookup = CXGN::Trial::TrialLookup->new({
109     schema => $chado_schema,
110     trial_name => "new_test_trial_name",
111                                                     }), "create trial lookup object");
112 ok(my $trial = $trial_lookup->get_trial());
113 ok(my $trial_id = $trial->project_id());
114 ok(my $trial_layout = CXGN::Trial::TrialLayout->new({
115     schema => $chado_schema,
116     trial_id => $trial_id,
117     experiment_type => 'field_layout'
118                                                     }), "create trial layout object");
120 ok(my $accession_names = $trial_layout->get_accession_names(), "retrieve accession names1");
122 my %stocks = map { $_ => 1 } @stock_names;
124 foreach my $acc (@$accession_names) {
125     ok(exists($stocks{$acc->{accession_name}}), "check accession names $acc->{accession_name}");
130 #create RCBD trial with one accession
132 @stock_names;
133 push @stock_names, "test_stock_for_trial1";
135 ok(my $trial_design = CXGN::Trial::TrialDesign->new(), "create trial design object");
136 ok($trial_design->set_trial_name("new_test_trial_name_single"), "set trial name");
137 ok($trial_design->set_stock_list(\@stock_names), "set stock list");
138 ok($trial_design->set_plot_start_number(1), "set plot start number");
139 ok($trial_design->set_plot_number_increment(1), "set plot increment");
140 ok($trial_design->set_number_of_reps(2), "set rep number");
141 ok($trial_design->set_design_type("CRD"), "set design type");
142 ok($trial_design->calculate_design(), "calculate design");
143 ok(my $design = $trial_design->get_design(), "retrieve design");
145 ok(my $trial_create = CXGN::Trial::TrialCreate->new({
146     chado_schema => $chado_schema,
147     dbh => $dbh,
148     user_name => "johndoe", #not implemented
149     design => $design,  
150     program => "test",
151     trial_year => "2015",
152     trial_description => "test description",
153     trial_location => "test_location_for_trial",
154     trial_name => "new_test_trial_name_single",
155     design_type => "RCBD",
156     operator => "janedoe"
157                                                     }), "create trial object");
159 my $save = $trial_create->save_trial();
160 ok($save->{'trial_id'}, "save trial");
162 ok(my $trial_lookup = CXGN::Trial::TrialLookup->new({
163     schema => $chado_schema,
164     trial_name => "new_test_trial_name_single",
165                                                     }), "create trial lookup object");
166 ok(my $trial = $trial_lookup->get_trial());
167 ok(my $trial_id = $trial->project_id());
168 ok(my $trial_layout = CXGN::Trial::TrialLayout->new({
169     schema => $chado_schema,
170     trial_id => $trial_id,
171     experiment_type => 'field_layout'
172                                                     }), "create trial layout object");
174 ok(my $accession_names = $trial_layout->get_accession_names(), "retrieve accession names2");
176 my %stocks = map { $_ => 1 } @stock_names;
178 foreach my $acc (@$accession_names) {
179     ok(exists($stocks{$acc->{accession_name}}), "check accession names $acc->{accession_name}");
183 # layout for genotyping experiment
184 # use data structure returned by brapi call for GDF:
185 # plates:[ { 'project_id' : 'project x', 
186 #            'plate_name' : 'required',
187 #            'plate_format': 'Plate_96' | 'tubes',
188 #            'sample_type' : 'DNA' | 'RNA' | 'Tissue'
189 #            'samples':[
190 # {
191 #               'name': 'sample_name1', 
192 #               'well': 'optional'
193 #               'concentration:
194 #'              'volume': 
195 #               'taxomony_id' : 
196 #               'tissue_type' : 
197 #               }
198 # ] 
200 my $plate_info = {
201     elements => \@genotyping_stock_names,
202     plate_format => 96,
203     blank_well => 'A02',
204     name => 'test_genotyping_trial_name',
205     description => "test description",
206     year => '2015',
207     project_name => 'NextGenCassava',
208     genotyping_facility_submit => 'no',
209     genotyping_facility => 'igd',
210     sample_type => 'DNA'
213 my $gd = CXGN::Trial::TrialDesign->new( { schema => $chado_schema } );
214 $gd->set_stock_list($plate_info->{elements});
215 $gd->set_block_size($plate_info->{plate_format});
216 $gd->set_blank($plate_info->{blank_well});
217 $gd->set_trial_name($plate_info->{name});
218 $gd->set_design_type("genotyping_plate");
219 $gd->calculate_design();
220 my $geno_design = $gd->get_design();
222 print STDERR Dumper $geno_design;
223 is_deeply($geno_design, {
224           'A09' => {
225                      'row_number' => 'A',
226                      'plot_name' => 'test_genotyping_trial_name_A09',
227                      'stock_name' => 'test_stock_for_genotyping_trial8',
228                      'col_number' => 9,
229                      'is_blank' => 0,
230                      'plot_number' => 'A09'
231                    },
232           'A07' => {
233                      'is_blank' => 0,
234                      'plot_number' => 'A07',
235                      'plot_name' => 'test_genotyping_trial_name_A07',
236                      'row_number' => 'A',
237                      'col_number' => 7,
238                      'stock_name' => 'test_stock_for_genotyping_trial6'
239                    },
240           'A02' => {
241                      'is_blank' => 1,
242                      'plot_number' => 'A02',
243                      'row_number' => 'A',
244                      'plot_name' => 'test_genotyping_trial_name_A02_BLANK',
245                      'stock_name' => 'BLANK',
246                      'col_number' => 2
247                    },
248           'A05' => {
249                      'is_blank' => 0,
250                      'plot_number' => 'A05',
251                      'row_number' => 'A',
252                      'plot_name' => 'test_genotyping_trial_name_A05',
253                      'stock_name' => 'test_stock_for_genotyping_trial4',
254                      'col_number' => 5
255                    },
256           'A08' => {
257                      'plot_name' => 'test_genotyping_trial_name_A08',
258                      'row_number' => 'A',
259                      'col_number' => 8,
260                      'stock_name' => 'test_stock_for_genotyping_trial7',
261                      'is_blank' => 0,
262                      'plot_number' => 'A08'
263                    },
264           'A04' => {
265                      'plot_name' => 'test_genotyping_trial_name_A04',
266                      'row_number' => 'A',
267                      'stock_name' => 'test_stock_for_genotyping_trial3',
268                      'col_number' => 4,
269                      'is_blank' => 0,
270                      'plot_number' => 'A04'
271                    },
272           'A01' => {
273                      'is_blank' => 0,
274                      'plot_number' => 'A01',
275                      'plot_name' => 'test_genotyping_trial_name_A01',
276                      'row_number' => 'A',
277                      'stock_name' => 'test_stock_for_genotyping_trial1',
278                      'col_number' => 1
279                    },
280           'A11' => {
281                      'plot_name' => 'test_genotyping_trial_name_A11',
282                      'row_number' => 'A',
283                      'stock_name' => 'test_stock_for_genotyping_trial10',
284                      'col_number' => 11,
285                      'is_blank' => 0,
286                      'plot_number' => 'A11'
287                    },
288           'A06' => {
289                      'plot_number' => 'A06',
290                      'is_blank' => 0,
291                      'stock_name' => 'test_stock_for_genotyping_trial5',
292                      'col_number' => 6,
293                      'row_number' => 'A',
294                      'plot_name' => 'test_genotyping_trial_name_A06'
295                    },
296           'A10' => {
297                      'is_blank' => 0,
298                      'plot_number' => 'A10',
299                      'plot_name' => 'test_genotyping_trial_name_A10',
300                      'row_number' => 'A',
301                      'col_number' => 10,
302                      'stock_name' => 'test_stock_for_genotyping_trial9'
303                    },
304           'A03' => {
305                      'plot_name' => 'test_genotyping_trial_name_A03',
306                      'row_number' => 'A',
307                      'stock_name' => 'test_stock_for_genotyping_trial2',
308                      'col_number' => 3,
309                      'is_blank' => 0,
310                      'plot_number' => 'A03'
311                    }
312         }, 'check genotyping trial design');
314 my $genotyping_trial_create;
315 ok($genotyping_trial_create = CXGN::Trial::TrialCreate->new({
316     chado_schema => $chado_schema,
317     dbh => $dbh,
318     user_name => "johndoe", #not implemented
319     program => "test",
320     trial_location => "test_location_for_trial",
321     operator => "janedoe",
322     trial_year => $plate_info->{year},
323     trial_description => $plate_info->{description},
324     design_type => 'genotyping_plate',
325     design => $geno_design,
326     trial_name => $plate_info->{name},
327     is_genotyping => 1,
328     genotyping_user_id => 41,
329     genotyping_project_name => $plate_info->{project_name},
330     genotyping_facility_submitted => $plate_info->{genotyping_facility_submit},
331     genotyping_facility => $plate_info->{genotyping_facility},
332     genotyping_plate_format => $plate_info->{plate_format},
333     genotyping_plate_sample_type => $plate_info->{sample_type},
334     }), "create genotyping trial");
335                                    
336 my $save = $genotyping_trial_create->save_trial();
337 ok($save->{'trial_id'}, "save genotyping trial");
339 ok(my $genotyping_trial_lookup = CXGN::Trial::TrialLookup->new({
340     schema => $chado_schema,
341     trial_name => "test_genotyping_trial_name",
342                                                     }), "lookup genotyping trial");
343 ok(my $genotyping_trial = $genotyping_trial_lookup->get_trial(), "retrieve genotyping trial");
344 ok(my $genotyping_trial_id = $genotyping_trial->project_id(), "retrive genotyping trial id");
345 ok(my $genotyping_trial_layout = CXGN::Trial::TrialLayout->new({
346     schema => $chado_schema,
347     trial_id => $genotyping_trial_id,
348     experiment_type => 'genotyping_layout'
349                                                     }), "create trial layout object for genotyping trial");
350 ok(my $genotyping_accession_names = $genotyping_trial_layout->get_accession_names(), "retrieve accession names3");
351 my %genotyping_stocks = map { $_ => 1 } @genotyping_stock_names;
352 $genotyping_stocks{'BLANK'} = 1;
353 foreach my $acc (@$genotyping_accession_names) { 
354     ok(exists($genotyping_stocks{$acc->{accession_name}}), "check existence of accession names $acc->{accession_name}");
357 done_testing();