5 use SGN::Test::Fixture;
9 use CXGN::Trial::TrialCreate;
10 use CXGN::Trial::ParseUpload;
11 use SGN::Model::Cvterm;
13 use CXGN::Location::LocationLookup;
14 use CXGN::Stock::StockLookup;
16 use CXGN::Trial::TrialDesign;
17 use CXGN::BreedersToolbox::Projects;
18 use CXGN::Genotype::StoreGenotypingProject;
20 use Test::WWW::Mechanize;
23 use Spreadsheet::Read;
26 my $f = SGN::Test::Fixture->new();
28 for my $extension ("xls", "xlsx") {
30 my $c = SimulateC->new({ dbh => $f->dbh(),
31 bcs_schema => $f->bcs_schema(),
32 metadata_schema => $f->metadata_schema(),
33 phenome_schema => $f->phenome_schema(),
34 sp_person_id => 41 });
36 #######################################
37 #Find out table counts before adding anything, so that changes can be compared
39 my $pre_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
40 my $pre_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
41 my $pre_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
42 my $pre_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
43 my $pre_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
44 my $pre_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
45 my $pre_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
46 my $pre_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
47 my $pre_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
48 my $pre_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
51 #First Upload Excel Trial File
55 my $file_name = "t/data/trial/trial_layout_example.$extension";
56 my $time = DateTime->now();
57 my $timestamp = $time->ymd() . "_" . $time->hms();
59 #Test archive upload file
60 my $uploader = CXGN::UploadFile->new({
61 tempfile => $file_name,
62 subdirectory => 'temp_trial_upload',
63 archive_path => '/tmp',
64 archive_filename => "trial_layout_example.$extension",
65 timestamp => $timestamp,
66 user_id => 41, #janedoe in fixture
67 user_role => 'curator'
70 ## Store uploaded temporary file in archive
71 my $archived_filename_with_path = $uploader->archive();
72 my $md5 = $uploader->get_md5($archived_filename_with_path);
73 ok($archived_filename_with_path);
76 $upload_metadata{'archived_file'} = $archived_filename_with_path;
77 $upload_metadata{'archived_file_type'} = "trial upload file";
78 $upload_metadata{'user_id'} = $c->sp_person_id;
79 $upload_metadata{'date'} = "2014-02-14_09:10:11";
82 #parse uploaded file with wrong plugin
83 my $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
84 $parser->load_plugin('ParseIGDFile');
85 my $parsed_data = $parser->parse();
86 ok(!$parsed_data, "Check if parse validate igd file fails for excel");
87 ok($parser->has_parse_errors(), "Check that parser errors occur");
89 #parse uploaded file with appropriate plugin
90 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
91 $parser->load_plugin('TrialExcelFormat');
92 $parsed_data = $parser->parse()->{'design'};
93 ok($parsed_data, "Check if parse validate excel file works");
94 ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
96 #print STDERR Dumper $parsed_data;
98 my $parsed_data_check = {
100 'plot_name' => 'plot_name1',
101 'stock_name' => 'test_accession1',
105 'block_number' => '1',
106 'range_number' => '1',
113 'block_number' => '2',
114 'plot_name' => 'plot_name6',
115 'stock_name' => 'test_accession3',
117 'range_number' => '2',
122 'range_number' => '2',
124 'plot_number' => '7',
125 'plot_name' => 'plot_name7',
126 'stock_name' => 'test_accession4',
130 'block_number' => '2'
133 'range_number' => '1',
134 'plot_number' => '4',
138 'block_number' => '1',
139 'plot_name' => 'plot_name4',
141 'stock_name' => 'test_accession2'
144 'range_number' => '2',
146 'plot_number' => '8',
147 'plot_name' => 'plot_name8',
148 'stock_name' => 'test_accession4',
152 'block_number' => '2'
155 'range_number' => '1',
156 'plot_number' => '2',
158 'plot_name' => 'plot_name2',
160 'stock_name' => 'test_accession1',
163 'block_number' => '1'
166 'range_number' => '2',
168 'plot_number' => '5',
169 'plot_name' => 'plot_name5',
170 'stock_name' => 'test_accession3',
174 'block_number' => '2'
177 'stock_name' => 'test_accession2',
179 'plot_name' => 'plot_name3',
180 'block_number' => '1',
184 'plot_number' => '3',
185 'range_number' => '1'
189 is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
191 my $trial_create = CXGN::Trial::TrialCreate
193 chado_schema => $c->bcs_schema(),
196 trial_year => "2016",
197 trial_description => "Trial Upload Test",
198 trial_location => "test_location",
199 trial_name => "Trial_upload_test",
200 design_type => "RCBD",
201 design => $parsed_data,
203 upload_trial_file => $archived_filename_with_path,
204 operator => "janedoe"
207 my $save = $trial_create->save_trial();
209 ok($save->{'trial_id'}, "check that trial_create worked");
210 my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
211 ok($project_name == "Trial_upload_test", "check that trial_create really worked");
213 my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
214 ok($project_desc == "Trial Upload Test", "check that trial_create really worked");
216 my $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
217 my $post1_project_diff = $post_project_count - $pre_project_count;
218 print STDERR "Project: " . $post1_project_diff . "\n";
219 ok($post1_project_diff == 1, "check project table after upload excel trial");
221 my $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
222 my $post1_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
223 print STDERR "NdExperiment: " . $post1_nd_experiment_diff . "\n";
224 ok($post1_nd_experiment_diff == 1, "check ndexperiment table after upload excel trial");
226 my $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
227 my $post1_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
228 print STDERR "NdExperimentProject: " . $post1_nd_experiment_proj_diff . "\n";
229 ok($post1_nd_experiment_proj_diff == 1, "check ndexperimentproject table after upload excel trial");
231 my $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
232 my $post1_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
233 print STDERR "NdExperimentprop: " . $post1_nd_experimentprop_diff . "\n";
234 ok($post1_nd_experimentprop_diff == 0, "check ndexperimentprop table after upload excel trial");
236 my $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
237 my $post1_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
238 print STDERR "Projectprop: " . $post1_project_prop_diff . "\n";
239 ok($post1_project_prop_diff == 4, "check projectprop table after upload excel trial");
241 my $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
242 my $post1_stock_diff = $post_stock_count - $pre_stock_count;
243 print STDERR "Stock: " . $post1_stock_diff . "\n";
244 ok($post1_stock_diff == 8, "check stock table after upload excel trial");
246 my $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
247 my $post1_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
248 print STDERR "Stockprop: " . $post1_stock_prop_diff . "\n";
249 ok($post1_stock_prop_diff == 48, "check stockprop table after upload excel trial");
251 my $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
252 my $post1_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
253 print STDERR "StockRelationship: " . $post1_stock_relationship_diff . "\n";
254 ok($post1_stock_relationship_diff == 8, "check stockrelationship table after upload excel trial");
256 my $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
257 my $post1_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
258 print STDERR "NdExperimentStock: " . $post1_nd_experiment_stock_diff . "\n";
259 ok($post1_nd_experiment_stock_diff == 8, "check ndexperimentstock table after upload excel trial");
261 my $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
262 my $post1_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
263 print STDERR "ProjectRelationship: " . $post1_project_relationship_diff . "\n";
264 ok($post1_project_relationship_diff == 1, "check projectrelationship table after upload excel trial");
267 #Upload IGD Trial File
269 $file_name = 't/data/genotype_trial_upload/CASSAVA_GS_74Template.csv';
270 #parse uploaded file with wrong plugin
271 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $file_name);
272 $parser->load_plugin('TrialExcelFormat');
273 my $rtn = $parser->parse();
274 $parsed_data = $rtn->{'design'};
275 ok(!$parsed_data, "Check if parse validate excel fails for igd parser");
276 ok($parser->has_parse_errors(), "Check that parser errors occur");
278 #parse uploaded file with appropriate plugin
279 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $file_name);
280 $parser->load_plugin('ParseIGDFile');
281 my $meta = $parser->parse();
282 ok($meta, "Check if parse validate excel file works");
284 print STDERR "CHECK =" . Dumper($meta) . "\n";
286 my $parsed_data_check = {
287 'blank_well' => 'F05',
288 'trial_name' => 'CASSAVA_GS_74',
289 'user_id' => 'I.Rabbi@cgiar.org',
290 'project_name' => 'NEXTGENCASSAVA'
293 is_deeply($meta, $parsed_data_check, 'check igd file parse data');
296 my $list = CXGN::List->new({ dbh => $c->dbh(), list_id => $list_id });
297 my $elements = $list->elements();
299 my $slu = CXGN::Stock::StockLookup->new({ schema => $c->bcs_schema });
301 # remove non-word characters from names as required by
302 # IGD naming conventions. Store new names as synonyms.
305 foreach my $e (@$elements) {
306 my $submission_name = $e;
307 $submission_name =~ s/\W/\_/g;
309 print STDERR "Replacing element $e with $submission_name\n";
310 $slu->set_stock_name($e);
311 my $s = $slu->get_stock();
312 $slu->set_stock_name($submission_name);
314 print STDERR "Storing synonym $submission_name for $e\n";
315 $slu->set_stock_name($e);
317 #my $rs = $slu->_get_stock_resultset();
318 $s->create_stockprops(
319 { igd_synonym => $submission_name },
321 'cv.name' => 'local',
326 my $td = CXGN::Trial::TrialDesign->new({ schema => $c->bcs_schema });
328 $td->set_stock_list($elements);
329 $td->set_block_size(96);
330 $td->set_blank($meta->{blank_well});
331 $td->set_design_type("genotyping_plate");
332 $td->set_trial_name($meta->{trial_name});
335 $td->calculate_design();
336 $design = $td->get_design();
338 #print STDERR Dumper $design;
340 my $igd_design_check = {
342 'stock_name' => 'test_accession5',
346 'plot_number' => 'A05',
347 'plot_name' => 'CASSAVA_GS_74_A05'
350 'plot_number' => 'A04',
351 'plot_name' => 'CASSAVA_GS_74_A04',
353 'stock_name' => 'test_accession4',
361 'stock_name' => 'test_accession2',
362 'plot_name' => 'CASSAVA_GS_74_A02',
363 'plot_number' => 'A02'
366 'stock_name' => 'test_accession1',
370 'plot_name' => 'CASSAVA_GS_74_A01',
371 'plot_number' => 'A01'
374 'plot_name' => 'CASSAVA_GS_74_F05_BLANK',
375 'plot_number' => 'F05',
378 'stock_name' => 'BLANK',
382 'plot_name' => 'CASSAVA_GS_74_A03',
383 'plot_number' => 'A03',
384 'stock_name' => 'test_accession3',
391 is_deeply($design, $igd_design_check, "check igd design");
393 #genotyping project for igd
394 my $chado_schema = $c->bcs_schema;
395 my $location_rs = $chado_schema->resultset('NaturalDiversity::NdGeolocation')->search({ description => 'Cornell Biotech' });
396 my $location_id = $location_rs->first->nd_geolocation_id;
398 my $bp_rs = $chado_schema->resultset('Project::Project')->find({ name => 'test' });
399 my $breeding_program_id = $bp_rs->project_id();
401 my $add_genotyping_project = CXGN::Genotype::StoreGenotypingProject->new({
402 chado_schema => $chado_schema,
404 project_name => 'test_genotyping_project_2',
405 breeding_program_id => $breeding_program_id,
406 project_facility => 'igd',
409 project_description => 'genotyping project for test',
410 nd_geolocation_id => $location_id,
413 ok(my $store_return = $add_genotyping_project->store_genotyping_project(), "store genotyping project");
415 my $gp_rs = $chado_schema->resultset('Project::Project')->find({ name => 'test_genotyping_project_2' });
416 my $genotyping_project_id = $gp_rs->project_id();
417 my $trial = CXGN::Trial->new({ bcs_schema => $chado_schema, trial_id => $genotyping_project_id });
419 #editing genotyping project details
420 my $new_year = '2021';
421 my $new_description = 'new genotyping project for test';
422 $trial->set_year($new_year);
423 $trial->set_description($new_description);
425 my $location_data = $trial->get_location();
426 my $location_name = $location_data->[1];
427 my $description = $trial->get_description();
428 my $genotyping_facility = $trial->get_genotyping_facility();
429 my $plate_year = $trial->get_year();
430 is($plate_year, '2021');
431 is($description, 'new genotyping project for test');
433 my $program_object = CXGN::BreedersToolbox::Projects->new({ schema => $chado_schema });
434 my $breeding_program_data = $program_object->get_breeding_programs_by_trial($genotyping_project_id);
435 my $breeding_program_name = $breeding_program_data->[0]->[1];
437 my $trial_create = CXGN::Trial::TrialCreate
439 chado_schema => $chado_schema,
442 trial_year => $plate_year,
443 trial_location => $location_name,
444 program => $breeding_program_name,
445 trial_description => "Test Genotyping Plate Upload",
446 design_type => 'genotyping_plate',
448 trial_name => "test_genotyping_trial_upload",
450 genotyping_user_id => $meta->{user_id} || "unknown",
451 genotyping_project_id => $genotyping_project_id,
452 genotyping_facility_submitted => 'no',
453 genotyping_facility => $genotyping_facility,
454 genotyping_plate_format => '96',
455 genotyping_plate_sample_type => 'DNA',
456 operator => "janedoe"
459 my $save = $trial_create->save_trial();
461 ok($save->{'trial_id'}, "check that trial_create worked");
462 my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
463 ok($project_name == "test_genotyping_trial_upload", "check that trial_create really worked for igd trial");
465 my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
466 ok($project_desc == "Test Genotyping Plate Upload", "check that trial_create really worked for igd trial");
468 $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
469 my $post2_project_diff = $post_project_count - $pre_project_count;
470 print STDERR "Project: " . $post2_project_diff . "\n";
471 ok($post2_project_diff == 3, "check project table after upload igd trial");
473 $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
474 my $post2_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
475 print STDERR "NdExperiment: " . $post2_nd_experiment_diff . "\n";
476 ok($post2_nd_experiment_diff == 2, "check ndexperiment table after upload igd trial");
478 $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
479 my $post2_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
480 print STDERR "NdExperimentProject: " . $post2_nd_experiment_proj_diff . "\n";
481 ok($post2_nd_experiment_proj_diff == 2, "check ndexperimentproject table after upload igd trial");
483 $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
484 my $post2_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
485 print STDERR "NdExperimentprop: " . $post2_nd_experimentprop_diff . "\n";
486 ok($post2_nd_experimentprop_diff == 1, "check ndexperimentprop table after upload igd trial");
488 $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
489 my $post2_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
490 print STDERR "Projectprop: " . $post2_project_prop_diff . "\n";
491 ok($post2_project_prop_diff == 15, "check projectprop table after adding genotyping project and uploading igd trial");
493 $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
494 my $post2_stock_diff = $post_stock_count - $pre_stock_count;
495 print STDERR "Stock: " . $post2_stock_diff . "\n";
496 ok($post2_stock_diff == 14, "check stock table after upload igd trial");
498 $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
499 my $post2_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
500 print STDERR "Stockprop: " . $post2_stock_prop_diff . "\n";
501 ok($post2_stock_prop_diff == 84, "check stockprop table after upload igd trial");
503 $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
504 my $post2_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
505 print STDERR "StockRelationship: " . $post2_stock_relationship_diff . "\n";
506 ok($post2_stock_relationship_diff == 14, "check stockrelationship table after upload igd trial");
508 $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
509 my $post2_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
510 print STDERR "NdExperimentStock: " . $post2_nd_experiment_stock_diff . "\n";
511 ok($post2_nd_experiment_stock_diff == 14, "check ndexperimentstock table after upload igd trial");
513 $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
514 my $post2_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
515 print STDERR "ProjectRelationship: " . $post2_project_relationship_diff . "\n";
516 ok($post2_project_relationship_diff == 4, "check projectrelationship table after adding genotyping project and uploading igd trial");
519 #############################
520 # Upload a trial with seedlot info filled
523 my $file_name = "t/data/trial/trial_layout_with_seedlot_example.$extension";
524 my $time = DateTime->now();
525 my $timestamp = $time->ymd() . "_" . $time->hms();
527 #Test archive upload file
528 my $uploader = CXGN::UploadFile->new({
529 tempfile => $file_name,
530 subdirectory => 'temp_trial_upload',
531 archive_path => '/tmp',
532 archive_filename => "trial_layout_with_seedlot_example.$extension",
533 timestamp => $timestamp,
534 user_id => 41, #janedoe in fixture
535 user_role => 'curator'
538 ## Store uploaded temporary file in archive
539 my $archived_filename_with_path = $uploader->archive();
540 my $md5 = $uploader->get_md5($archived_filename_with_path);
541 ok($archived_filename_with_path);
544 $upload_metadata{'archived_file'} = $archived_filename_with_path;
545 $upload_metadata{'archived_file_type'} = "trial upload file";
546 $upload_metadata{'user_id'} = $c->sp_person_id;
547 $upload_metadata{'date'} = "2014-02-14_09:10:11";
550 #parse uploaded file with appropriate plugin
551 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path);
552 $parser->load_plugin('TrialExcelFormat');
553 $rtn = $parser->parse();
554 $parsed_data = $rtn->{'design'};
555 ok($parsed_data, "Check if parse validate excel file works");
556 ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
558 #print STDERR Dumper $parsed_data;
560 my $parsed_data_check = {
563 'num_seed_per_plot' => '12',
564 'block_number' => '2',
567 'plot_name' => 'plot_with_seedlot_name7',
568 'stock_name' => 'test_accession4',
569 'seedlot_name' => 'test_accession4_001',
570 'plot_number' => '7',
571 'range_number' => '2',
572 'weight_gram_seed_per_plot' => 0,
577 'weight_gram_seed_per_plot' => '5',
578 'range_number' => '1',
579 'seedlot_name' => 'test_accession2_001',
580 'plot_number' => '4',
582 'num_seed_per_plot' => '12',
583 'block_number' => '1',
585 'stock_name' => 'test_accession2',
586 'plot_name' => 'plot_with_seedlot_name4',
591 'weight_gram_seed_per_plot' => 0,
592 'range_number' => '1',
593 'seedlot_name' => 'test_accession1_001',
594 'plot_number' => '1',
596 'num_seed_per_plot' => '12',
597 'block_number' => '1',
599 'stock_name' => 'test_accession1',
600 'plot_name' => 'plot_with_seedlot_name1',
604 'range_number' => '2',
606 'weight_gram_seed_per_plot' => 0,
607 'plot_number' => '5',
608 'seedlot_name' => 'test_accession3_001',
609 'plot_name' => 'plot_with_seedlot_name5',
611 'stock_name' => 'test_accession3',
614 'num_seed_per_plot' => '12',
615 'block_number' => '2'
618 'block_number' => '1',
619 'num_seed_per_plot' => '12',
622 'stock_name' => 'test_accession1',
624 'plot_name' => 'plot_with_seedlot_name2',
625 'plot_number' => '2',
626 'seedlot_name' => 'test_accession1_001',
628 'weight_gram_seed_per_plot' => 0,
629 'range_number' => '1'
632 'weight_gram_seed_per_plot' => '4',
634 'range_number' => '1',
635 'plot_number' => '3',
636 'seedlot_name' => 'test_accession2_001',
638 'block_number' => '1',
639 'num_seed_per_plot' => '12',
641 'stock_name' => 'test_accession2',
642 'plot_name' => 'plot_with_seedlot_name3',
647 'plot_name' => 'plot_with_seedlot_name6',
648 'stock_name' => 'test_accession3',
650 'num_seed_per_plot' => '12',
651 'block_number' => '2',
653 'seedlot_name' => 'test_accession3_001',
654 'plot_number' => '6',
655 'range_number' => '2',
657 'weight_gram_seed_per_plot' => 0
660 'seedlot_name' => 'test_accession4_001',
661 'plot_number' => '8',
662 'weight_gram_seed_per_plot' => 0,
664 'range_number' => '2',
665 'block_number' => '2',
666 'num_seed_per_plot' => '12',
669 'stock_name' => 'test_accession4',
671 'plot_name' => 'plot_with_seedlot_name8'
675 is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
677 my $trial_create = CXGN::Trial::TrialCreate
679 chado_schema => $c->bcs_schema(),
682 trial_year => "2016",
683 trial_description => "Trial Upload Test",
684 trial_location => "test_location",
685 trial_name => "Trial_upload_with_seedlot_test",
686 design_type => "RCBD",
687 design => $parsed_data,
689 upload_trial_file => $archived_filename_with_path,
690 operator => "janedoe"
693 $trial_create->save_trial();
695 ok($trial_create, "check that trial_create worked");
696 my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
697 ok($project_name == "Trial_upload_test", "check that trial_create really worked");
699 my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
700 ok($project_desc == "Trial Upload Test", "check that trial_create really worked");
702 my $post_project_count = $c->bcs_schema->resultset('Project::Project')->search({})->count();
703 my $post1_project_diff = $post_project_count - $pre_project_count;
704 print STDERR "Project: " . $post1_project_diff . "\n";
705 ok($post1_project_diff == 4, "check project table after third upload excel trial");
707 my $post_nd_experiment_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search({})->count();
708 my $post1_nd_experiment_diff = $post_nd_experiment_count - $pre_nd_experiment_count;
709 print STDERR "NdExperiment: " . $post1_nd_experiment_diff . "\n";
710 ok($post1_nd_experiment_diff == 3, "check ndexperiment table after upload excel trial");
712 my $post_nd_experiment_proj_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentProject')->search({})->count();
713 my $post1_nd_experiment_proj_diff = $post_nd_experiment_proj_count - $pre_nd_experiment_proj_count;
714 print STDERR "NdExperimentProject: " . $post1_nd_experiment_proj_diff . "\n";
715 ok($post1_nd_experiment_proj_diff == 3, "check ndexperimentproject table after upload excel trial");
717 my $post_nd_experimentprop_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentprop')->search({})->count();
718 my $post1_nd_experimentprop_diff = $post_nd_experimentprop_count - $pre_nd_experimentprop_count;
719 print STDERR "NdExperimentprop: " . $post1_nd_experimentprop_diff . "\n";
720 ok($post1_nd_experimentprop_diff == 1, "check ndexperimentprop table after upload excel trial");
722 my $post_project_prop_count = $c->bcs_schema->resultset('Project::Projectprop')->search({})->count();
723 my $post1_project_prop_diff = $post_project_prop_count - $pre_project_prop_count;
724 print STDERR "Projectprop: " . $post1_project_prop_diff . "\n";
725 ok($post1_project_prop_diff == 19, "check projectprop table after upload excel trial");
727 my $post_stock_count = $c->bcs_schema->resultset('Stock::Stock')->search({})->count();
728 my $post1_stock_diff = $post_stock_count - $pre_stock_count;
729 print STDERR "Stock: " . $post1_stock_diff . "\n";
730 ok($post1_stock_diff == 22, "check stock table after upload excel trial");
732 my $post_stock_prop_count = $c->bcs_schema->resultset('Stock::Stockprop')->search({})->count();
733 my $post1_stock_prop_diff = $post_stock_prop_count - $pre_stock_prop_count;
734 print STDERR "Stockprop: " . $post1_stock_prop_diff . "\n";
735 #ok($post1_stock_prop_diff == 133, "check stockprop table after upload excel trial");
737 my $post_stock_relationship_count = $c->bcs_schema->resultset('Stock::StockRelationship')->search({})->count();
738 my $post1_stock_relationship_diff = $post_stock_relationship_count - $pre_stock_relationship_count;
739 print STDERR "StockRelationship: " . $post1_stock_relationship_diff . "\n";
740 ok($post1_stock_relationship_diff == 30, "check stockrelationship table after upload excel trial");
742 my $post_nd_experiment_stock_count = $c->bcs_schema->resultset('NaturalDiversity::NdExperimentStock')->search({})->count();
743 my $post1_nd_experiment_stock_diff = $post_nd_experiment_stock_count - $pre_nd_experiment_stock_count;
744 print STDERR "NdExperimentStock: " . $post1_nd_experiment_stock_diff . "\n";
745 ok($post1_nd_experiment_stock_diff == 22, "check ndexperimentstock table after upload excel trial");
747 my $post_project_relationship_count = $c->bcs_schema->resultset('Project::ProjectRelationship')->search({})->count();
748 my $post1_project_relationship_diff = $post_project_relationship_count - $pre_project_relationship_count;
749 print STDERR "ProjectRelationship: " . $post1_project_relationship_diff . "\n";
750 ok($post1_project_relationship_diff == 5, "check projectrelationship table after upload excel trial");
752 #adding new genotyping project
753 my $add_genotyping_project_2 = CXGN::Genotype::StoreGenotypingProject->new({
754 chado_schema => $chado_schema,
756 project_name => 'test_genotyping_project_4',
757 breeding_program_id => $breeding_program_id,
758 project_facility => 'igd',
761 project_description => 'genotyping project for test',
762 nd_geolocation_id => $location_id,
765 ok(my $store_return_2 = $add_genotyping_project_2->store_genotyping_project(), "store genotyping project");
767 my $gp_rs_2 = $chado_schema->resultset('Project::Project')->find({ name => 'test_genotyping_project_4' });
768 my $genotyping_project_id_2 = $gp_rs_2->project_id();
770 my $mech = Test::WWW::Mechanize->new;
771 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username" => "janedoe", "password" => "secretpw", "grant_type" => "password" ]);
772 my $response = decode_json $mech->content;
773 print STDERR Dumper $response;
774 my $sgn_session_id = $response->{access_token};
775 print STDERR $sgn_session_id . "\n";
777 my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/genotype_trial_upload.$extension";
778 my $ua = LWP::UserAgent->new;
779 $response = $ua->post(
780 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
781 Content_Type => 'form-data',
783 genotyping_trial_layout_upload => [
785 "genotype_trial_upload.$extension",
786 Content_Type => ($extension eq "xls") ? 'application/vnd.ms-excel' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
788 "sgn_session_id" => $sgn_session_id,
789 "genotyping_trial_name" => '2018TestPlate02'
793 ok($response->is_success);
794 my $message = $response->decoded_content;
795 my $message_hash = decode_json $message;
797 is_deeply($message_hash, {
801 'concentration' => '5',
802 'acquisition_date' => '2018/02/16',
803 'dna_person' => 'nmorales',
806 'plot_name' => '2018TestPlate02_A01',
807 'ncbi_taxonomy_id' => '9001',
808 'stock_name' => 'KASESE_TP2013_885',
809 'notes' => 'test well A01',
811 'extraction' => 'CTAB',
812 'plot_number' => 'A01',
814 'tissue_type' => 'leaf',
815 'facility_identifier' => undef
818 'notes' => 'test well A03',
820 'stock_name' => 'KASESE_TP2013_1671',
821 'ncbi_taxonomy_id' => '9001',
822 'plot_name' => '2018TestPlate02_A03',
823 'tissue_type' => 'leaf',
825 'plot_number' => 'A03',
826 'extraction' => 'CTAB',
828 'dna_person' => 'nmorales',
829 'concentration' => '5',
830 'acquisition_date' => '2018/02/16',
832 'facility_identifier' => undef
835 'extraction' => undef,
836 'plot_number' => 'A02',
838 'tissue_type' => 'stem',
839 'stock_name' => 'BLANK',
840 'notes' => 'test blank',
842 'ncbi_taxonomy_id' => undef,
843 'plot_name' => '2018TestPlate02_A02',
846 'acquisition_date' => '2018/02/16',
847 'concentration' => undef,
848 'dna_person' => 'nmorales',
849 'facility_identifier' => undef
854 my $project = $c->bcs_schema()->resultset("Project::Project")->find({ name => 'test' });
855 my $location = $c->bcs_schema()->resultset("NaturalDiversity::NdGeolocation")->find({ description => 'test_location' });
858 design => $message_hash->{design},
859 genotyping_facility_submit => 'yes',
860 name => 'test_genotype_upload_trial1',
861 genotyping_project_id => $genotyping_project_id_2,
862 sample_type => 'DNA',
866 $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
867 $response = decode_json $mech->content;
868 #print STDERR Dumper $response;
870 ok($response->{trial_id});
872 my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/CoordinateTemplate.csv";
873 my $ua = LWP::UserAgent->new;
874 $response = $ua->post(
875 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
876 Content_Type => 'form-data',
878 genotyping_trial_layout_upload_coordinate_template => [ $file, 'genotype_trial_upload', Content_Type => 'application/vnd.ms-excel', ],
879 "sgn_session_id" => $sgn_session_id,
880 "genotyping_trial_name" => "18DNA00101"
884 #print STDERR Dumper $response;
885 ok($response->is_success);
886 my $message = $response->decoded_content;
887 my $message_hash = decode_json $message;
888 #print STDERR Dumper $message_hash;
890 is_deeply($message_hash, {
894 'notes' => 'newplate',
895 'ncbi_taxonomy_id' => 'NA',
896 'dna_person' => 'gbauchet',
898 'concentration' => 'NA',
899 'plot_number' => 'B12',
901 'tissue_type' => 'leaf',
902 'plot_name' => '18DNA00101_B12',
903 'extraction' => 'NA',
905 'col_number' => '12',
906 'acquisition_date' => '8/23/2018',
907 'stock_name' => 'BLANK',
908 'facility_identifier' => undef
911 'stock_name' => 'KASESE_TP2013_1671',
912 'acquisition_date' => '8/23/2018',
913 'col_number' => '01',
915 'extraction' => 'NA',
916 'plot_name' => '18DNA00101_A01',
917 'tissue_type' => 'leaf',
918 'plot_number' => 'A01',
920 'dna_person' => 'gbauchet',
922 'concentration' => 'NA',
923 'ncbi_taxonomy_id' => 'NA',
924 'notes' => 'newplate',
925 'facility_identifier' => undef
928 'plot_name' => '18DNA00101_B01',
929 'extraction' => 'NA',
931 'col_number' => '01',
932 'stock_name' => 'KASESE_TP2013_1671',
933 'acquisition_date' => '8/23/2018',
934 'ncbi_taxonomy_id' => 'NA',
935 'notes' => 'newplate',
936 'dna_person' => 'gbauchet',
938 'concentration' => 'NA',
939 'plot_number' => 'B01',
941 'tissue_type' => 'leaf',
942 'facility_identifier' => undef
945 'col_number' => '01',
946 'acquisition_date' => '8/23/2018',
947 'stock_name' => 'KASESE_TP2013_885',
948 'extraction' => 'NA',
950 'plot_name' => '18DNA00101_C01',
951 'tissue_type' => 'leaf',
953 'dna_person' => 'gbauchet',
954 'concentration' => 'NA',
955 'plot_number' => 'C01',
957 'ncbi_taxonomy_id' => 'NA',
958 'notes' => 'newplate',
959 'facility_identifier' => undef
962 'ncbi_taxonomy_id' => 'NA',
963 'notes' => 'newplate',
964 'plot_number' => 'D01',
966 'dna_person' => 'gbauchet',
968 'concentration' => 'NA',
969 'tissue_type' => 'leaf',
970 'plot_name' => '18DNA00101_D01',
972 'extraction' => 'NA',
973 'acquisition_date' => '8/23/2018',
974 'stock_name' => 'KASESE_TP2013_885',
975 'col_number' => '01',
976 'facility_identifier' => undef
979 }, 'test upload parse of coordinate genotyping plate');
982 design => $message_hash->{design},
983 genotyping_facility_submit => 'no',
984 name => 'test_genotype_upload_coordinate_trial101',
985 genotyping_project_id => $genotyping_project_id_2,
986 sample_type => 'DNA',
991 $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
992 $response = decode_json $mech->content;
993 #print STDERR Dumper $response;
995 ok($response->{trial_id});
997 my $file = $f->config->{basepath} . "/t/data/genotype_trial_upload/CoordinatePlateUpload.csv";
998 my $ua = LWP::UserAgent->new;
999 $response = $ua->post(
1000 'http://localhost:3010/ajax/breeders/parsegenotypetrial',
1001 Content_Type => 'form-data',
1003 genotyping_trial_layout_upload_coordinate => [ $file, 'genotype_trial_upload', Content_Type => 'application/vnd.ms-excel', ],
1004 "sgn_session_id" => $sgn_session_id,
1005 "genotyping_trial_name" => "18DNA00001"
1009 #print STDERR Dumper $response;
1010 ok($response->is_success);
1011 my $message = $response->decoded_content;
1012 my $message_hash = decode_json $message;
1013 #print STDERR Dumper $message_hash;
1015 is_deeply($message_hash, {
1018 'ncbi_taxonomy_id' => 'NA',
1020 'acquisition_date' => '2018-02-06',
1021 'plot_name' => '18DNA00001_B01',
1022 'col_number' => '01',
1024 'extraction' => 'CTAB',
1025 'tissue_type' => 'leaf',
1027 'concentration' => 'NA',
1028 'stock_name' => 'test_accession1',
1029 'plot_number' => 'B01',
1030 'row_number' => 'B',
1031 'dna_person' => 'Trevor_Rife',
1032 'facility_identifier' => undef
1035 'tissue_type' => 'leaf',
1036 'extraction' => 'CTAB',
1038 'col_number' => '04',
1039 'acquisition_date' => '2018-02-06',
1040 'plot_name' => '18DNA00001_B04',
1041 'ncbi_taxonomy_id' => 'NA',
1043 'row_number' => 'B',
1044 'dna_person' => 'Trevor_Rife',
1045 'plot_number' => 'B04',
1046 'stock_name' => 'BLANK',
1047 'concentration' => 'NA',
1049 'facility_identifier' => undef
1053 'ncbi_taxonomy_id' => 'NA',
1054 'plot_name' => '18DNA00001_C01',
1055 'acquisition_date' => '2018-02-06',
1057 'col_number' => '01',
1058 'extraction' => 'CTAB',
1059 'tissue_type' => 'leaf',
1061 'concentration' => 'NA',
1062 'stock_name' => 'test_accession2',
1063 'plot_number' => 'C01',
1064 'dna_person' => 'Trevor_Rife',
1065 'row_number' => 'C',
1066 'facility_identifier' => undef
1069 'ncbi_taxonomy_id' => 'NA',
1071 'plot_name' => '18DNA00001_C04',
1072 'acquisition_date' => '2018-02-06',
1074 'col_number' => '04',
1075 'tissue_type' => 'leaf',
1076 'extraction' => 'CTAB',
1078 'stock_name' => 'BLANK',
1079 'concentration' => 'NA',
1080 'plot_number' => 'C04',
1081 'dna_person' => 'Trevor_Rife',
1082 'row_number' => 'C',
1083 'facility_identifier' => undef
1087 'ncbi_taxonomy_id' => 'NA',
1088 'acquisition_date' => '2018-02-06',
1089 'plot_name' => '18DNA00001_A01',
1091 'col_number' => '01',
1092 'extraction' => 'CTAB',
1093 'tissue_type' => 'leaf',
1095 'concentration' => 'NA',
1096 'stock_name' => 'test_accession1',
1097 'plot_number' => 'A01',
1098 'dna_person' => 'Trevor_Rife',
1099 'row_number' => 'A',
1100 'facility_identifier' => undef
1103 'dna_person' => 'Trevor_Rife',
1104 'row_number' => 'D',
1105 'plot_number' => 'D01',
1106 'stock_name' => 'test_accession2',
1107 'concentration' => 'NA',
1109 'tissue_type' => 'leaf',
1110 'extraction' => 'CTAB',
1111 'col_number' => '01',
1113 'acquisition_date' => '2018-02-06',
1114 'plot_name' => '18DNA00001_D01',
1115 'ncbi_taxonomy_id' => 'NA',
1117 'facility_identifier' => undef
1121 }, 'test upload parse of coordinate genotyping plate');
1124 design => $message_hash->{design},
1125 genotyping_facility_submit => 'no',
1126 name => 'test_genotype_upload_coordinate_trial1',
1127 genotyping_project_id => $genotyping_project_id,
1128 sample_type => 'DNA',
1129 plate_format => '96'
1132 $mech->post_ok('http://localhost:3010/ajax/breeders/storegenotypetrial', [ "sgn_session_id" => $sgn_session_id, plate_data => encode_json($plate_data) ]);
1133 $response = decode_json $mech->content;
1134 #print STDERR "RESPONSE: ".Dumper $response;
1136 ok($response->{trial_id});
1137 my $geno_trial_id = $response->{trial_id};
1138 $mech->get_ok("http://localhost:3010/breeders/trial/$geno_trial_id/download/layout?format=intertekxls&dataLevel=plate");
1139 my $intertek_download = $mech->content;
1140 my $contents = ReadData $intertek_download;
1141 #print STDERR Dumper $contents;
1142 is($contents->[0]->{'type'}, 'xls', "check that type of file is correct #1");
1143 is($contents->[0]->{'sheets'}, '1', "check that type of file is correct #2");
1145 my $columns = $contents->[1]->{'cell'};
1146 #print STDERR Dumper scalar(@$columns);
1147 ok(scalar(@$columns) == 7, "check number of col in created file.");
1149 #print STDERR Dumper $columns;
1150 is_deeply($columns, [
1155 '18DNA00001_A01|||test_accession1',
1156 '18DNA00001_B01|||test_accession1',
1157 '18DNA00001_B04|||BLANK',
1158 '18DNA00001_C01|||test_accession2',
1159 '18DNA00001_C04|||BLANK',
1160 '18DNA00001_D01|||test_accession2'
1165 'test_genotype_upload_coordinate_trial1',
1166 'test_genotype_upload_coordinate_trial1',
1167 'test_genotype_upload_coordinate_trial1',
1168 'test_genotype_upload_coordinate_trial1',
1169 'test_genotype_upload_coordinate_trial1',
1170 'test_genotype_upload_coordinate_trial1'
1195 'test_genotype_upload_coordinate_trial1',
1196 'test_genotype_upload_coordinate_trial1',
1197 'test_genotype_upload_coordinate_trial1',
1198 'test_genotype_upload_coordinate_trial1',
1199 'test_genotype_upload_coordinate_trial1',
1200 'test_genotype_upload_coordinate_trial1'
1205 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1206 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1207 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1208 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1209 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: ',
1210 'Notes: AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA TissueType: leaf Person: Trevor_Rife Extraction: CTAB Facility Identifier: '
1212 ], 'test intertek genotyping plate download');
1214 $mech->get_ok("http://localhost:3010/breeders/trial/$geno_trial_id/download/layout?format=dartseqcsv&dataLevel=plate");
1215 my $intertek_download = $mech->content;
1216 #print STDERR Dumper $intertek_download;
1217 my @intertek_download = split "\n", $intertek_download;
1218 #print STDERR Dumper \@intertek_download;
1220 is_deeply(\@intertek_download, [
1221 'PlateID,Row,Column,Organism,Species,Genotype,Tissue,Comments',
1222 'test_genotype_upload_coordinate_trial1,A,01,tomato,"Solanum lycopersicum",18DNA00001_A01|||test_accession1,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1223 'test_genotype_upload_coordinate_trial1,B,01,tomato,"Solanum lycopersicum",18DNA00001_B01|||test_accession1,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1224 'test_genotype_upload_coordinate_trial1,C,01,tomato,"Solanum lycopersicum",18DNA00001_C01|||test_accession2,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"',
1225 'test_genotype_upload_coordinate_trial1,D,01,tomato,"Solanum lycopersicum",18DNA00001_D01|||test_accession2,leaf,"Notes: NA AcquisitionDate: 2018-02-06 Concentration: NA Volume: NA Person: Trevor_Rife Extraction: CTAB Facility Identifier: NA"'
1229 #Upload trial with management factors
1231 my $file_name_with_managementfactors = "t/data/trial/trial_layout_example_with_management_factor.$extension";
1233 #Test archive upload file
1234 my $uploader = CXGN::UploadFile->new({
1235 tempfile => $file_name_with_managementfactors,
1236 subdirectory => 'temp_trial_upload',
1237 archive_path => '/tmp',
1238 archive_filename => "trial_layout_example_with_management_factor.$extension",
1239 timestamp => $timestamp,
1240 user_id => 41, #janedoe in fixture
1241 user_role => 'curator'
1244 ## Store uploaded temporary file in archive
1245 my $management_factor_archived_filename_with_path = $uploader->archive();
1246 my $md5_management_factor = $uploader->get_md5($management_factor_archived_filename_with_path);
1247 ok($management_factor_archived_filename_with_path);
1248 ok($md5_management_factor);
1250 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $management_factor_archived_filename_with_path);
1251 $parser->load_plugin('TrialExcelFormat');
1252 $rtn = $parser->parse();
1253 $parsed_data = $rtn->{'design'};
1254 ok($parsed_data, "Check if parse validate excel file works");
1255 ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
1257 #print STDERR Dumper $parsed_data;
1259 my $parsed_data_check_with_management_factor = {
1261 'plot_number' => '7',
1262 'col_number' => '2',
1263 'block_number' => '2',
1264 'rep_number' => '1',
1265 'is_a_control' => 0,
1266 'stock_name' => 'test_accession4',
1267 'row_number' => '3',
1268 'range_number' => '2',
1269 'plot_name' => 'trial_management_factor_plot_name7'
1272 'col_number' => '2',
1273 'plot_number' => '5',
1274 'rep_number' => '1',
1275 'block_number' => '2',
1276 'is_a_control' => 0,
1277 'stock_name' => 'test_accession3',
1278 'row_number' => '1',
1279 'range_number' => '2',
1280 'plot_name' => 'trial_management_factor_plot_name5'
1283 'block_number' => '1',
1284 'rep_number' => '1',
1285 'plot_number' => '3',
1286 'col_number' => '1',
1287 'range_number' => '1',
1288 'plot_name' => 'trial_management_factor_plot_name3',
1289 'row_number' => '3',
1290 'stock_name' => 'test_accession2',
1294 'stock_name' => 'test_accession2',
1295 'is_a_control' => 0,
1296 'row_number' => '4',
1297 'plot_name' => 'trial_management_factor_plot_name4',
1298 'range_number' => '1',
1299 'col_number' => '1',
1300 'plot_number' => '4',
1301 'block_number' => '1',
1305 'rep_number' => '2',
1306 'block_number' => '2',
1307 'plot_number' => '8',
1308 'col_number' => '2',
1309 'row_number' => '4',
1310 'range_number' => '2',
1311 'plot_name' => 'trial_management_factor_plot_name8',
1312 'stock_name' => 'test_accession4',
1316 'block_number' => '1',
1317 'rep_number' => '1',
1318 'plot_number' => '1',
1319 'col_number' => '1',
1320 'is_a_control' => 0,
1321 'stock_name' => 'test_accession1',
1322 'plot_name' => 'trial_management_factor_plot_name1',
1323 'range_number' => '1',
1327 'plot_name' => 'trial_management_factor_plot_name2',
1328 'range_number' => '1',
1329 'row_number' => '2',
1330 'stock_name' => 'test_accession1',
1331 'is_a_control' => 0,
1332 'plot_number' => '2',
1333 'col_number' => '1',
1334 'block_number' => '1',
1338 'manage_factor2' => {
1339 'new_treatment_stocks' => [
1340 'trial_management_factor_plot_name3',
1341 'trial_management_factor_plot_name4',
1342 'trial_management_factor_plot_name5'
1346 'new_treatment_stocks' => [
1347 'trial_management_factor_plot_name1',
1348 'trial_management_factor_plot_name2',
1349 'trial_management_factor_plot_name3',
1350 'trial_management_factor_plot_name6',
1351 'trial_management_factor_plot_name7',
1352 'trial_management_factor_plot_name8'
1357 'row_number' => '2',
1358 'range_number' => '2',
1359 'plot_name' => 'trial_management_factor_plot_name6',
1360 'stock_name' => 'test_accession3',
1361 'is_a_control' => 0,
1362 'plot_number' => '6',
1363 'col_number' => '2',
1364 'rep_number' => '2',
1365 'block_number' => '2'
1369 is_deeply($parsed_data, $parsed_data_check_with_management_factor, 'check trial excel parse data');
1371 my $trial_create_with_management_factor = CXGN::Trial::TrialCreate
1373 chado_schema => $c->bcs_schema(),
1375 trial_year => "2016",
1376 trial_description => "Trial Upload Test with Management Factors",
1377 trial_location => "test_location",
1378 trial_name => "Trial_upload_test_with_management_factor",
1379 design_type => "RCBD",
1380 design => $parsed_data,
1382 upload_trial_file => $management_factor_archived_filename_with_path,
1383 operator => "janedoe",
1387 my $save_with_management_factor = $trial_create_with_management_factor->save_trial();
1389 ok($save_with_management_factor->{'trial_id'}, "check that trial_create worked with management factor");
1390 my $project_name_with_management_factor = $c->bcs_schema()->resultset('Project::Project')->find({ project_id => $save_with_management_factor->{'trial_id'} })->name();
1391 ok($project_name_with_management_factor == "Trial_upload_test_with_management_factor", "check that trial_create really worked");
1393 my $trial_with_management_factor = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $save_with_management_factor->{'trial_id'} });
1394 my $management_factors = $trial_with_management_factor->get_treatments();
1395 #print STDERR Dumper $management_factors;
1396 is(scalar(@$management_factors), 2);
1398 my $trial_management_factor1 = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $management_factors->[0]->[0] });
1399 my $management_factor_name1 = $trial_management_factor1->name();
1400 #print STDERR Dumper $management_factor_name1;
1401 is($management_factor_name1, "Trial_upload_test_with_management_factor_fert_factor1");
1402 my $management_factor_plots1 = $trial_management_factor1->get_plots();
1403 #print STDERR Dumper $management_factor_plots1;
1404 is(scalar(@$management_factor_plots1), 6);
1406 my $trial_management_factor2 = CXGN::Trial->new({ bcs_schema => $c->bcs_schema, trial_id => $management_factors->[1]->[0] });
1407 my $management_factor_name2 = $trial_management_factor2->name();
1408 #print STDERR Dumper $management_factor_name2;
1409 is($management_factor_name2, "Trial_upload_test_with_management_factor_manage_factor2");
1410 my $management_factor_plots2 = $trial_management_factor2->get_plots();
1411 #print STDERR Dumper $management_factor_plots2;
1412 is(scalar(@$management_factor_plots2), 3);
1414 #test deleting genotyping project with genotyping plate
1415 my $schema = $f->bcs_schema();
1416 my $before_deleting_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1418 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_project_id . '/delete/genotyping_project');
1419 $response = decode_json $mech->content;
1420 is($response->{'error'}, 'Cannot delete genotyping project with associated genotyping plates.');
1422 my $after_deleting_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1423 is($after_deleting_genotyping_project, $before_deleting_genotyping_project);
1425 #test deleting empty genotyping project
1426 #first deleting associated genotyping plates
1427 my $genotyping_plate_id_1 = $schema->resultset("Project::Project")->find({ name => 'test_genotyping_trial_upload' })->project_id;
1428 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_plate_id_1 . '/delete/layout');
1429 $response = decode_json $mech->content;
1430 is($response->{'success'}, '1');
1432 my $genotyping_plate_id_4 = $schema->resultset("Project::Project")->find({ name => 'test_genotype_upload_coordinate_trial1' })->project_id;
1433 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_plate_id_4 . '/delete/layout');
1434 $response = decode_json $mech->content;
1435 is($response->{'success'}, '1');
1437 #delete empty genotyping_project
1438 $mech->get_ok('http://localhost:3010/ajax/breeders/trial/' . $genotyping_project_id . '/delete/genotyping_project');
1439 $response = decode_json $mech->content;
1440 is($response->{'success'}, '1');
1442 my $after_deleting_empty_genotyping_project = $schema->resultset("Project::Project")->search({})->count();
1443 #deleting 2 associated genotyping plates and genotyping project
1444 is($after_deleting_empty_genotyping_project, $before_deleting_genotyping_project - 3);
1447 #Upload Trial with flexible column headers, entry Numbers, and auto generating plot names
1448 my %upload_metadata;
1449 my $file_name = "t/data/trial/trial_layout_example_flexible.$extension";
1450 my $time = DateTime->now();
1451 my $timestamp = $time->ymd() . "_" . $time->hms();
1452 my $trial_name = "Trial_upload_test_flexible";
1454 #Test archive upload file
1455 my $uploader = CXGN::UploadFile->new({
1456 tempfile => $file_name,
1457 subdirectory => 'temp_trial_upload',
1458 archive_path => '/tmp',
1459 archive_filename => "trial_layout_example_flexible.$extension",
1460 timestamp => $timestamp,
1461 user_id => 41, #janedoe in fixture
1462 user_role => 'curator'
1465 ## Store uploaded temporary file in archive
1466 my $archived_filename_with_path = $uploader->archive();
1467 my $md5 = $uploader->get_md5($archived_filename_with_path);
1468 ok($archived_filename_with_path);
1471 $upload_metadata{'archived_file'} = $archived_filename_with_path;
1472 $upload_metadata{'archived_file_type'} = "trial upload file";
1473 $upload_metadata{'user_id'} = $c->sp_person_id;
1474 $upload_metadata{'date'} = "2014-02-14_09:10:11";
1476 #parse uploaded file with appropriate plugin
1477 $parser = CXGN::Trial::ParseUpload->new(chado_schema => $f->bcs_schema(), filename => $archived_filename_with_path, trial_name => $trial_name);
1478 $parser->load_plugin('TrialExcelFormat');
1479 my $p = $parser->parse();
1480 $parsed_data = $p->{'design'};
1481 my $entry_numbers = $p->{'entry_numbers'};
1482 ok($parsed_data, "Check if parse validate excel file works");
1483 ok(!$parser->has_parse_errors(), "Check that parse returns no errors");
1485 my $parsed_data_check = {
1487 'plot_name' => 'Trial_upload_test_flexible-PLOT_1',
1488 'stock_name' => 'test_accession1',
1489 'col_number' => '1',
1490 'is_a_control' => 0,
1491 'rep_number' => '1',
1492 'block_number' => '1',
1493 'range_number' => '1',
1494 'row_number' => '1',
1495 'plot_number' => '1'
1498 'rep_number' => '2',
1499 'is_a_control' => 0,
1500 'block_number' => '2',
1501 'plot_name' => 'Trial_upload_test_flexible-PLOT_6',
1502 'stock_name' => 'test_accession3',
1503 'col_number' => '2',
1504 'range_number' => '2',
1505 'row_number' => '2',
1506 'plot_number' => '6'
1509 'range_number' => '2',
1510 'row_number' => '3',
1511 'plot_number' => '7',
1512 'plot_name' => 'Trial_upload_test_flexible-PLOT_7',
1513 'stock_name' => 'test_accession4',
1514 'col_number' => '2',
1515 'rep_number' => '1',
1516 'is_a_control' => 0,
1517 'block_number' => '2'
1520 'range_number' => '1',
1521 'plot_number' => '4',
1522 'row_number' => '4',
1523 'is_a_control' => 0,
1524 'rep_number' => '2',
1525 'block_number' => '1',
1526 'plot_name' => 'Trial_upload_test_flexible-PLOT_4',
1527 'col_number' => '1',
1528 'stock_name' => 'test_accession2'
1531 'range_number' => '2',
1532 'row_number' => '4',
1533 'plot_number' => '8',
1534 'plot_name' => 'Trial_upload_test_flexible-PLOT_8',
1535 'stock_name' => 'test_accession4',
1536 'col_number' => '2',
1537 'rep_number' => '2',
1538 'is_a_control' => 0,
1539 'block_number' => '2'
1542 'range_number' => '1',
1543 'plot_number' => '2',
1544 'row_number' => '2',
1545 'plot_name' => 'Trial_upload_test_flexible-PLOT_2',
1546 'col_number' => '1',
1547 'stock_name' => 'test_accession1',
1548 'is_a_control' => 0,
1549 'rep_number' => '2',
1550 'block_number' => '1'
1553 'range_number' => '2',
1554 'row_number' => '1',
1555 'plot_number' => '5',
1556 'plot_name' => 'Trial_upload_test_flexible-PLOT_5',
1557 'stock_name' => 'test_accession3',
1558 'col_number' => '2',
1559 'is_a_control' => 0,
1560 'rep_number' => '1',
1561 'block_number' => '2'
1564 'stock_name' => 'test_accession2',
1565 'col_number' => '1',
1566 'plot_name' => 'Trial_upload_test_flexible-PLOT_3',
1567 'block_number' => '1',
1568 'is_a_control' => 0,
1569 'rep_number' => '1',
1570 'row_number' => '3',
1571 'plot_number' => '3',
1572 'range_number' => '1'
1575 my $entry_numbers_check = {
1576 'test_accession4' => '4',
1577 'test_accession2' => '2',
1578 'test_accession3' => '3',
1579 'test_accession1' => '1'
1582 is_deeply($parsed_data, $parsed_data_check, 'check trial excel parse data');
1583 is_deeply($entry_numbers, $entry_numbers_check, 'check trial excel entry numbers');
1585 my $trial_create = CXGN::Trial::TrialCreate
1587 chado_schema => $c->bcs_schema(),
1590 trial_year => "2016",
1591 trial_description => "Trial Upload Test Flexible",
1592 trial_location => "test_location",
1593 trial_name => $trial_name,
1594 design_type => "RCBD",
1595 design => $parsed_data,
1597 upload_trial_file => $archived_filename_with_path,
1598 operator => "janedoe"
1601 my $save = $trial_create->save_trial();
1603 ok($save->{'trial_id'}, "check that trial_create worked");
1604 my $project_name = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->name();
1605 ok($project_name == $trial_name, "check that trial_create really worked");
1607 my $project_desc = $c->bcs_schema()->resultset('Project::Project')->search({}, { order_by => { -desc => 'project_id' } })->first()->description();
1608 ok($project_desc == "Trial Upload Test Flexible", "check that trial_create really worked");