Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / solGS / List.pm
blobeba00680fc82de19a3f4189bc589563ae38ff394
2 =head1 AUTHOR
4 Isaak Y Tecle <iyt2@cornell.edu>
6 =head1 LICENSE
8 This library is free software. You can redistribute it and/or modify
9 it under the same terms as Perl itself.
11 =head1 DESCRIPTION
13 SGN::Controller::solGS::List - Controller for list based training and selection populations
15 =cut
17 package SGN::Controller::solGS::List;
19 use Moose;
20 use namespace::autoclean;
21 use Carp qw/ carp confess croak /;
22 use CXGN::List::Transform;
23 use CXGN::Tools::Run;
24 use File::Path qw / mkpath /;
25 use File::Spec::Functions qw / catfile catdir/;
26 use File::Slurp qw /write_file read_file/;
27 use File::Temp qw / tempfile tempdir /;
28 use JSON;
29 use List::MoreUtils qw /uniq firstidx/;
30 use CXGN::People::Person;
31 use POSIX qw(strftime);
32 use Storable qw/ nstore retrieve /;
33 use String::CRC;
34 use Try::Tiny;
36 use solGS::queryJobs;
38 BEGIN { extends 'Catalyst::Controller' }
40 sub generate_check_value : Path('/solgs/generate/checkvalue') Args(0) {
41 my ( $self, $c ) = @_;
43 my $file_name = $c->req->param('string');
44 my $check_value = crc($file_name);
46 my $ret->{status} = 'failed';
48 if ($check_value) {
49 $ret->{status} = 'success';
50 $ret->{check_value} = $check_value;
53 $ret = to_json($ret);
55 $c->res->content_type('application/json');
56 $c->res->body($ret);
60 sub check_predicted_list_selection :
61 Path('/solgs/check/predicted/list/selection') Args(0) {
62 my ( $self, $c ) = @_;
64 my $args = $c->req->param('arguments');
65 $c->controller('solGS::Utils')->stash_json_args( $c, $args );
67 my $training_pop_id = $c->stash->{training_pop_id};
68 my $selection_pop_id = $c->stash->{selection_pop_id};
70 $c->controller('solGS::Download')
71 ->selection_prediction_download_urls( $c, $training_pop_id,
72 $selection_pop_id );
74 my $ret->{output} = $c->stash->{selection_prediction_download};
76 $ret = to_json($ret);
78 $c->res->content_type('application/json');
79 $c->res->body($ret);
83 sub get_trial_id : Path('/solgs/get/trial/id') Args(0) {
84 my ( $self, $c ) = @_;
86 my @trials_names = $c->req->param('trials_names[]');
88 my $tr_rs = $c->controller('solGS::Search')->model($c)
89 ->project_details_by_exact_name( \@trials_names );
91 my @trials_ids;
93 while ( my $rw = $tr_rs->next ) {
94 push @trials_ids, $rw->project_id;
97 my $ret->{trials_ids} = \@trials_ids;
99 $ret = to_json($ret);
101 $c->res->content_type('application/json');
102 $c->res->body($ret);
106 sub get_trial_id_plots_list {
107 my ( $self, $c, $list_id ) = @_;
109 $list_id = $list_id =~ /list/ ? $list_id : 'list_' . $list_id;
111 my $pheno_file =
112 $c->controller('solGS::Files')->phenotype_file_name( $c, $list_id );
113 my @pheno_data = read_file( $pheno_file, { binmode => ':utf8' } );
114 my @headers = split( /\t/, $pheno_data[0] );
115 my $trial_idx = firstidx { $_ eq 'studyDbId' } @headers;
116 my $trial_id = ( split( /\t/, $pheno_data[1] ) )[$trial_idx];
118 return $trial_id;
122 sub get_selection_genotypes_list_from_file {
123 my ( $self, $file ) = @_;
124 my @clones;
126 open my $fh, $file or die "Can't open file $file: $!";
128 while (<$fh>) {
129 $_ =~ s/\n//;
130 push @clones, $_;
133 return \@clones;
137 sub get_genotypes_list {
138 my ( $self, $c ) = @_;
140 my $list_id = $c->stash->{list_id};
142 my $list =
143 CXGN::List->new( { dbh => $c->dbc()->dbh(), list_id => $list_id } );
144 my @genotypes_list = @{ $list->elements };
146 $c->stash->{genotypes_list} = \@genotypes_list;
150 sub transform_genotypes_unqiueids {
151 my ( $self, $c, $genotypes ) = @_;
153 my $transform = CXGN::List::Transform->new();
154 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
155 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
156 my $genotypes_t =
157 $transform->can_transform( "accessions", "accession_ids" );
158 my $genotypes_id_hash =
159 $transform->transform( $schema, $genotypes_t, $genotypes );
160 my @genotypes_ids = @{ $genotypes_id_hash->{transform} };
162 return \@genotypes_ids;
166 sub transform_uniqueids_genotypes {
167 my ( $self, $c, $genotypes_ids ) = @_;
169 my $transform = CXGN::List::Transform->new();
170 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
171 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
172 my $genotypes_t = $transform->can_transform( "stock_ids", "stocks" );
173 my $genotypes_hash =
174 $transform->transform( $schema, $genotypes_t, $genotypes_ids );
175 my @genotypes = @{ $genotypes_hash->{transform} };
177 return \@genotypes;
181 sub get_genotypes_list_details {
182 my ( $self, $c ) = @_;
184 # my $list_id = $c->stash->{list_id};
186 $self->stash_list_metadata($c);
187 my $list_type = $c->stash->{list_type};
189 my $genotypes_names;
191 if ( $list_type =~ /accessions/ ) {
192 $self->get_list_elements_names($c);
193 $genotypes_names = $c->stash->{list_elements_names};
195 elsif ( $list_type =~ /plots/ ) {
196 $self->transform_plots_genotypes_names($c);
197 $genotypes_names = $c->stash->{genotypes_list};
200 my @genotypes_names = uniq(@$genotypes_names);
201 my $genotypes_ids =
202 $self->transform_genotypes_unqiueids( $c, \@genotypes_names );
204 $c->stash->{genotypes_list} = $genotypes_names;
205 $c->stash->{genotypes_ids} = $genotypes_ids;
209 sub create_list_pop_data_files {
210 my ( $self, $c ) = @_;
212 my $file_id;
214 if ( $c->stash->{list_id} ) {
215 $file_id = $self->list_file_id($c);
217 elsif ( $c->stash->{dataset_id} ) {
218 $file_id = $c->controller('solGS::Dataset')->dataset_file_id($c);
221 my $protocol_id = $c->stash->{genotyping_protocol_id};
223 $c->controller('solGS::Files')->phenotype_file_name( $c, $file_id );
224 my $pheno_file = $c->stash->{phenotype_file_name};
226 $c->controller('solGS::Files')
227 ->genotype_file_name( $c, $file_id, $protocol_id );
228 my $geno_file = $c->stash->{genotype_file_name};
230 my $files = {
231 pheno_file => $pheno_file,
232 geno_file => $geno_file
235 return $files;
239 sub stash_list_metadata {
240 my ( $self, $c, $list_id ) = @_;
242 $list_id = $c->stash->{list_id} if !$list_id;
243 $list_id =~ s/\w+_//g;
245 my $list =
246 CXGN::List->new( { dbh => $c->dbc()->dbh(), list_id => $list_id } );
247 $c->stash->{list_id} = $list_id;
248 $c->stash->{list_type} = $list->type;
249 $c->stash->{list_name} = $list->name;
250 $c->stash->{list_owner} = $list->owner;
254 sub create_list_population_metadata {
255 my ( $self, $c ) = @_;
256 my $metadata = 'key' . "\t" . 'value';
257 $metadata .= "\n" . 'user_id' . "\t" . $c->user->id;
258 $metadata .= "\n" . 'list_name' . "\t" . $c->{stash}->{list_name};
259 $metadata .= "\n"
260 . 'description' . "\t"
261 . 'Uploaded on: '
262 . strftime "%a %b %e %H:%M %Y", localtime;
264 $c->stash->{list_metadata} = $metadata;
268 sub create_list_population_metadata_file {
269 my ( $self, $c, $list_pop_id ) = @_;
271 my $user_id = $c->user->id;
272 my $tmp_dir = $c->stash->{solgs_lists_dir};
274 $c->controller('solGS::Files')
275 ->population_metadata_file( $c, $tmp_dir, $list_pop_id );
276 my $file = $c->stash->{population_metadata_file};
278 $self->create_list_population_metadata($c);
279 my $metadata = $c->stash->{list_metadata};
281 write_file( $file, { binmode => ':utf8' }, $metadata );
283 $c->stash->{list_metadata_file} = $file;
287 sub predict_list_selection_pop_single_pop_model {
288 my ( $self, $c ) = @_;
290 my $trait_id = $c->stash->{trait_id};
291 my $training_pop_id = $c->stash->{training_pop_id};
292 my $selection_pop_id = $c->stash->{selection_pop_id};
293 my $protocol_id = $c->stash->{genotyping_protocol_id};
295 $c->stash->{list_prediction} = 1;
297 # my $identifier = $training_pop_id . '_' . $selection_pop_id;
298 $c->controller('solGS::Files')
299 ->rrblup_selection_gebvs_file( $c, $training_pop_id, $selection_pop_id,
300 $trait_id );
301 my $rrblup_selection_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
303 if ( !-s $rrblup_selection_gebvs_file ) {
304 $c->controller('solGS::Files')
305 ->phenotype_file_name( $c, $training_pop_id );
306 $c->stash->{phenotype_file} = $c->stash->{phenotype_file_name};
308 $c->controller('solGS::Files')
309 ->genotype_file_name( $c, $training_pop_id, $protocol_id );
310 $c->stash->{genotype_file} = $c->stash->{genotype_file_name};
312 $self->user_selection_population_file( $c, $selection_pop_id,
313 $protocol_id );
315 $c->stash->{pop_id} = $c->stash->{training_pop_id};
316 $c->controller('solGS::Trait')->get_trait_details( $c, $trait_id );
317 $c->controller('solGS::solGS')->get_rrblup_output($c);
318 $c->stash->{status} = 'success';
320 else {
321 $c->stash->{status} = 'success';
326 sub predict_list_selection_pop_multi_traits {
327 my ( $self, $c ) = @_;
329 my $data_set_type = $c->stash->{data_set_type};
330 my $training_pop_id = $c->stash->{training_pop_id};
331 my $selection_pop_id = $c->stash->{selection_pop_id};
332 my $protocol_id = $c->stash->{genotyping_protocol_id};
334 $c->stash->{pop_id} = $training_pop_id;
335 $c->controller('solGS::solGS')->traits_with_valid_models($c);
336 my @traits_with_valid_models = @{ $c->stash->{traits_with_valid_models} };
338 foreach my $trait_abbr (@traits_with_valid_models) {
339 $c->stash->{trait_abbr} = $trait_abbr;
340 $c->controller('solGS::Trait')->get_trait_details_of_trait_abbr($c);
341 $self->predict_list_selection_pop_single_pop_model($c);
344 $c->controller('solGS::Download')
345 ->selection_prediction_download_urls( $c, $training_pop_id,
346 $selection_pop_id );
347 my $download_prediction = $c->stash->{selection_prediction_download};
351 sub predict_list_selection_pop_combined_pops_model {
352 my ( $self, $c ) = @_;
354 my $data_set_type = $c->stash->{data_set_type};
355 my $combo_pops_id = $c->stash->{combo_pops_id};
356 my $training_pop_id = $c->stash->{training_pop_id};
357 my $selection_pop_id = $c->stash->{selection_pop_id};
358 my $trait_id = $c->stash->{trait_id};
360 $c->stash->{pop_id} = $training_pop_id;
361 $c->stash->{list_prediction} = 1;
363 $c->controller('solGS::Files')
364 ->rrblup_selection_gebvs_file( $c, $training_pop_id, $selection_pop_id,
365 $trait_id );
366 my $rrblup_selection_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
368 if ( !-s $rrblup_selection_gebvs_file ) {
369 $c->controller('solGS::Trait')->get_trait_details( $c, $trait_id );
371 $c->controller("solGS::combinedTrials")->cache_combined_pops_data($c);
373 my $pheno_file = $c->stash->{trait_combined_pheno_file};
374 my $geno_file = $c->stash->{trait_combined_geno_file};
376 $self->user_selection_population_file( $c, $selection_pop_id );
378 $c->controller("solGS::solGS")->get_rrblup_output($c);
379 $c->stash->{status} = 'success';
381 else {
382 $c->stash->{status} = 'success';
385 $c->controller('solGS::Download')
386 ->selection_prediction_download_urls( $c, $training_pop_id,
387 $selection_pop_id );
391 sub predict_list_selection_gebvs {
392 my ( $self, $c ) = @_;
394 my $referer = $c->req->referer;
396 if ( $referer =~ /solgs\/trait\// ) {
397 $self->predict_list_selection_pop_single_pop_model($c);
399 elsif ( $referer =~ /solgs\/traits\/all\// ) {
400 $self->predict_list_selection_pop_multi_traits($c);
402 elsif ( $referer =~ /solgs\/models\/combined\/trials\// ) {
403 $c->stash->{pop_id} = $c->stash->{training_pop_id};
404 $c->controller("solGS::solGS")->traits_with_valid_models($c);
405 my @traits_with_valid_models =
406 @{ $c->stash->{traits_with_valid_models} };
408 foreach my $trait_abbr (@traits_with_valid_models) {
409 $c->stash->{trait_abbr} = $trait_abbr;
410 $c->controller('solGS::Trait')->get_trait_details_of_trait_abbr($c);
412 $self->predict_list_selection_pop_combined_pops_model($c);
415 elsif ( $referer =~ /solgs\/model\/combined\/populations\// ) {
416 $self->predict_list_selection_pop_combined_pops_model($c);
418 else {
419 $c->stash->{status} =
420 "calling predict_list_selection_gebvs..no matching type analysis.";
424 sub user_selection_population_file {
425 my ( $self, $c, $pred_pop_id, $protocol_id ) = @_;
427 my $list_dir = $c->stash->{solgs_lists_dir};
429 my ( $fh, $tempfile ) =
430 tempfile( "selection_population_${pred_pop_id}-XXXXX", DIR => $list_dir );
432 $c->controller('solGS::Files')
433 ->genotype_file_name( $c, $pred_pop_id, $protocol_id );
434 my $pred_pop_file = $c->stash->{genotype_file_name};
436 $c->stash->{genotypes_list_genotype_file} = $pred_pop_file;
438 $fh->print($pred_pop_file);
439 $fh->close;
441 $c->stash->{selection_population_file} = $tempfile;
445 sub get_list_elements_names {
446 my ( $self, $c, $list_id ) = @_;
448 $list_id = $c->stash->{list_id} if !$list_id;
450 my $list =
451 CXGN::List->new( { dbh => $c->dbc()->dbh(), list_id => $list_id } );
452 my $names = $list->elements;
454 $c->stash->{list_elements_names} = $names;
455 return $names;
459 sub get_plots_list_elements_ids {
460 my ( $self, $c, $list_id ) = @_;
462 $list_id = $c->stash->{list_id} if !$list_id;
464 my $plots;
465 if ( $c->stash->{plots_names} ) {
466 $plots = $c->stash->{plots_names};
468 else {
469 $self->get_list_elements_names($c);
470 $plots = $c->stash->{list_elements_names};
473 my $transform = CXGN::List::Transform->new();
474 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
475 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
476 my $plots_t = $transform->can_transform( "plots", "plot_ids" );
477 my $plots_id_hash = $transform->transform( $schema, $plots_t, $plots );
478 my @plots_ids = @{ $plots_id_hash->{transform} };
480 $c->stash->{list_elements_ids} = \@plots_ids;
482 return \@plots_ids;
486 sub map_plots_genotypes {
487 my ( $self, $c ) = @_;
489 my $plots = $c->stash->{plots_names};
491 if ( !@$plots ) {
492 die "No plots list provided $!\n";
494 else {
495 my $genotypes_rs = $c->controller('solGS::Search')->model($c)
496 ->get_genotypes_from_plots($plots);
498 my @genotypes;
499 my @genotypes_ids;
500 while ( my $genotype = $genotypes_rs->next ) {
501 my $name = $genotype->uniquename;
502 my $genotypes_ids = $genotype->id;
503 push @genotypes, $name;
506 @genotypes = uniq(@genotypes);
507 @genotypes_ids = uniq(@genotypes);
509 $c->stash->{genotypes_list} = \@genotypes;
510 $c->stash->{genotypes_ids} = \@genotypes_ids;
515 sub transform_plots_genotypes_names {
516 my ( $self, $c ) = @_;
518 $self->get_list_elements_names($c);
519 $c->stash->{plots_names} = $c->stash->{list_elements_names};
521 $self->get_plots_list_elements_ids($c);
522 $c->stash->{plots_ids} = $c->stash->{list_elements_ids};
524 $self->map_plots_genotypes($c);
528 sub genotypes_list_genotype_file {
529 my ( $self, $c ) = @_;
531 $self->genotypes_list_genotype_query_job($c);
532 my $args = $c->stash->{genotypes_list_genotype_query_job};
534 $c->controller('solGS::AsyncJob')->submit_job_cluster( $c, $args );
538 sub genotypes_list_genotype_query_job {
539 my ( $self, $c ) = @_;
541 my $list_id = $c->stash->{list_id};
542 my $protocol_id = $c->stash->{genotyping_protocol_id};
544 my $pop_id = 'list_' . $list_id;
545 my $data_dir = $c->stash->{solgs_lists_dir};
546 my $pop_type = "list";
548 $self->get_genotypes_list_details($c);
549 my $genotypes_ids = $c->stash->{genotypes_ids};
551 $c->controller('solGS::Files')->genotype_file_name( $c, $pop_id );
552 my $geno_file = $c->stash->{genotype_file_name};
554 my $args = {
555 'genotypes_ids' => $genotypes_ids,
556 'data_dir' => $data_dir,
557 'genotype_file' => $geno_file,
558 'genotyping_protocol_id' => $protocol_id,
559 'r_temp_file' => "genotypes-list-genotype-data-query-${pop_id}",
562 $c->stash->{r_temp_file} = $args->{r_temp_file};
563 $c->controller('solGS::AsyncJob')->create_cluster_accessible_tmp_files($c);
564 my $out_temp_file = $c->stash->{out_file_temp};
565 my $err_temp_file = $c->stash->{err_file_temp};
567 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
568 my $background_job = $c->stash->{background_job};
570 my $report_file = $c->controller('solGS::Files')
571 ->create_tempfile( $temp_dir, "geno-data-query-report-args-${pop_id}" );
572 $c->stash->{report_file} = $report_file;
574 my $config_args = {
575 'temp_dir' => $temp_dir,
576 'out_file' => $out_temp_file,
577 'err_file' => $err_temp_file,
578 'cluster_host' => 'localhost'
581 my $config = $c->controller('solGS::AsyncJob')
582 ->create_cluster_config( $c, $config_args );
584 my $args_file = $c->controller('solGS::Files')
585 ->create_tempfile( $temp_dir, "geno-data-query-job-args-file-${pop_id}" );
587 nstore $args, $args_file
588 or croak
589 "data query script: $! serializing genotype lists genotype query details to $args_file ";
591 my $dbhost = $c->config->{dbhost};
592 my $dbname = $c->config->{dbname};
593 my $dbpass = $c->config->{dbpass};
594 my $dbuser = $c->config->{dbuser};
596 my $cmd =
597 'mx-run solGS::queryJobs '
598 . ' --dbhost '
599 . $dbhost
600 . ' --dbname '
601 . $dbname
602 . ' --dbuser '
603 . $dbuser
604 . ' --dbpass '
605 . $dbpass
606 . ' --data_type genotype '
607 . ' --population_type '
608 . $pop_type
609 . ' --args_file '
610 . $args_file;
612 my $job_args = {
613 'cmd' => $cmd,
614 'config' => $config,
615 'background_job' => $background_job,
616 'temp_dir' => $temp_dir,
619 $c->stash->{genotypes_list_genotype_query_job} = $job_args;
623 sub plots_list_phenotype_query_job {
624 my ( $self, $c ) = @_;
626 my $model_id = $c->stash->{model_id};
627 my $list = $c->stash->{list};
628 my $list_id = $c->stash->{list_id};
630 my $dataset_id = $c->stash->{dataset_id};
631 my $plots_names = $c->stash->{plots_list};
632 my $plots_ids = $c->stash->{plots_ids};
634 if ( !$plots_ids ) {
635 $self->get_plots_list_elements_ids($c);
636 $plots_ids = $c->stash->{list_elements_ids};
639 $c->stash->{pop_id} =
640 $dataset_id ? 'dataset_' . $dataset_id : 'list_' . $list_id;
641 my $file_id = $c->stash->{pop_id};
642 $c->controller('solGS::Files')->traits_list_file($c);
643 my $traits_file = $c->stash->{traits_list_file};
645 my $data_dir = $c->stash->{solgs_lists_dir};
647 $c->stash->{r_temp_file} = 'plots-phenotype-data-query';
648 $c->controller('solGS::AsyncJob')->create_cluster_accessible_tmp_files($c);
649 my $out_temp_file = $c->stash->{out_file_temp};
650 my $err_temp_file = $c->stash->{err_file_temp};
652 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
653 my $background_job = $c->stash->{background_job};
655 my $temp_data_files =
656 $self->create_list_pop_data_files( $c, $data_dir, $file_id );
657 my $pheno_file = $temp_data_files->{pheno_file};
658 $c->stash->{plots_list_phenotype_file} = $pheno_file;
660 $c->controller('solGS::Files')->phenotype_metadata_file($c);
661 my $metadata_file = $c->stash->{phenotype_metadata_file};
663 my $args = {
664 'list_id' => $list_id,
665 'plots_ids' => $plots_ids,
666 'traits_file' => $traits_file,
667 'list_data_dir' => $data_dir,
668 'phenotype_file' => $pheno_file,
669 'metadata_file' => $metadata_file
672 my $args_file = $c->controller('solGS::Files')
673 ->create_tempfile( $temp_dir, 'pheno-data-query-report-args' );
674 $c->stash->{report_file} = $args_file;
676 nstore $args, $args_file
677 or croak
678 "data query script: $! serializing data query details to $args_file ";
680 my $dbhost = $c->config->{dbhost};
681 my $dbname = $c->config->{dbname};
682 my $dbpass = $c->config->{dbpass};
683 my $dbuser = $c->config->{dbuser};
685 my $cmd =
686 'mx-run solGS::queryJobs '
687 . ' --dbhost '
688 . $dbhost
689 . ' --dbname '
690 . $dbname
691 . ' --dbuser '
692 . $dbuser
693 . ' --dbpass '
694 . $dbpass
695 . ' --data_type phenotype '
696 . ' --population_type plots_list '
697 . ' --args_file '
698 . $args_file;
700 my $config_args = {
701 'temp_dir' => $temp_dir,
702 'out_file' => $out_temp_file,
703 'err_file' => $err_temp_file,
704 'cluster_host' => 'localhost'
707 my $config = $c->controller('solGS::AsyncJob')
708 ->create_cluster_config( $c, $config_args );
710 my $job_args = {
711 'cmd' => $cmd,
712 'config' => $config,
713 'background_job' => $background_job,
714 'temp_dir' => $temp_dir,
717 $c->stash->{plots_list_phenotype_query_job} = $job_args;
718 $c->stash->{phenotype_file} = $pheno_file;
721 sub create_list_pheno_data_query_jobs {
722 my ( $self, $c ) = @_;
724 $self->stash_list_metadata($c);
725 my $list_type = $c->stash->{list_type};
727 if ( $list_type =~ /plots/ ) {
728 $self->plots_list_phenotype_query_job($c);
729 $c->stash->{list_pheno_data_query_jobs} =
730 $c->stash->{plots_list_phenotype_query_job};
732 elsif ( $list_type =~ /trials/ ) {
733 $self->get_list_trials_ids($c);
734 my $trials_ids = $c->stash->{trials_ids};
735 $c->controller('solGS::combinedTrials')
736 ->multi_pops_pheno_files( $c, $trials_ids );
737 $c->stash->{phenotype_files_list} = $c->stash->{multi_pops_pheno_files};
738 $c->controller('solGS::AsyncJob')
739 ->get_trials_phenotype_query_jobs_args( $c, $trials_ids );
740 $c->stash->{list_pheno_data_query_jobs} =
741 $c->stash->{trials_phenotype_query_jobs_args};
745 sub create_list_geno_data_query_jobs {
746 my ( $self, $c ) = @_;
748 $self->stash_list_metadata($c);
749 my $list_type = $c->stash->{list_type};
751 my $protocol_id = $c->stash->{genotyping_protocol_id};
753 if ( $list_type =~ /accessions/ ) {
754 $self->genotypes_list_genotype_query_job($c);
755 $c->stash->{list_geno_data_query_jobs} =
756 $c->stash->{genotypes_list_genotype_query_job};
758 elsif ( $list_type =~ /trials/ ) {
759 $self->get_list_trials_ids($c);
760 my $trials_ids = $c->stash->{trials_ids};
762 $c->controller('solGS::combinedTrials')
763 ->multi_pops_geno_files( $c, $trials_ids, $protocol_id );
764 $c->stash->{genotype_files_list} = $c->stash->{multi_pops_geno_files};
765 $c->controller('solGS::AsyncJob')
766 ->get_trials_genotype_query_jobs_args( $c, $trials_ids,
767 $protocol_id );
768 $c->stash->{list_geno_data_query_jobs} =
769 $c->stash->{trials_genotype_query_jobs_args};
773 sub list_phenotype_data {
774 my ( $self, $c ) = @_;
776 #my $list_id = $c->stash->{list_id};
777 #$list_id =~ s/\w+_//g;
779 $self->stash_list_metadata($c);
780 my $list_type = $c->stash->{list_type};
782 if ( $list_type eq 'plots' ) {
783 $self->plots_list_phenotype_file($c);
784 $c->stash->{phenotype_file} = $c->stash->{plots_list_phenotype_file};
786 elsif ( $list_type eq 'trials' ) {
787 $self->get_list_trials_ids($c);
788 $self->get_trials_list_pheno_data($c);
792 sub plots_list_phenotype_file {
793 my ( $self, $c ) = @_;
795 $self->plots_list_phenotype_query_job($c);
796 my $args = $c->stash->{plots_list_phenotype_query_job};
797 $c->controller('solGS::AsyncJob')->submit_job_cluster( $c, $args );
801 sub get_list_training_data_query_jobs {
802 my ( $self, $c, $protocol_id ) = @_;
804 $self->plots_list_phenotype_query_job($c);
805 $self->genotypes_list_genotype_query_job($c);
807 my $pheno_job = $c->stash->{plots_list_phenotype_query_job};
808 my $geno_job = $c->stash->{genotypes_list_genotype_query_job};
810 $c->stash->{list_training_data_query_jobs} = [ $pheno_job, $geno_job ];
813 sub get_list_training_data_query_jobs_file {
814 my ( $self, $c, $protocol_id ) = @_;
816 $self->get_list_training_data_query_jobs( $c, $protocol_id );
817 my $query_jobs = $c->stash->{list_training_data_query_jobs};
819 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
820 my $queries_args_file = $c->controller('solGS::Files')
821 ->create_tempfile( $temp_dir, 'list_training_data_query_args' );
823 nstore $query_jobs, $queries_args_file
824 or croak
825 "list type training pop data query job : $! serializing selection pop data query details to $queries_args_file";
827 $c->stash->{list_training_data_query_jobs_file} = $queries_args_file;
830 sub submit_list_training_data_query {
831 my ( $self, $c ) = @_;
833 my $list_id = $c->stash->{list_id};
835 $self->stash_list_metadata($c);
836 my $list_type = $c->stash->{list_type};
838 my $protocol_id = $c->stash->{genotyping_protocol_id};
840 my $query_jobs_file;
842 if ( $list_type =~ /plots/ ) {
843 $self->get_list_training_data_query_jobs_file( $c, $protocol_id );
844 $query_jobs_file = $c->stash->{list_training_data_query_jobs_file};
846 elsif ( $list_type =~ /trials/ ) {
847 $self->get_list_trials_ids($c);
848 my $trials = $c->stash->{trials_ids};
849 $c->controller('solGS::AsyncJob')
850 ->get_training_pop_data_query_job_args_file( $c, $trials,
851 $protocol_id );
852 $query_jobs_file = $c->stash->{training_pop_data_query_job_args_file};
855 $c->stash->{dependent_jobs} = $query_jobs_file;
856 $c->controller('solGS::AsyncJob')->run_async($c);
859 sub list_population_summary {
860 my ( $self, $c ) = @_;
862 my $list_id = $c->stash->{list_id};
863 my $file_id = $self->list_file_id($c);
864 my $tmp_dir = $c->stash->{solgs_lists_dir};
865 my $protocol_id = $c->stash->{genotyping_protocol_id};
867 $self->stash_list_metadata($c);
868 my $list_name = $c->stash->{list_name};
869 my $owner_id = $c->stash->{list_owner};
871 my $person = CXGN::People::Person->new( $c->dbc()->dbh(), $owner_id );
872 my $owner = $person->get_first_name() . ' ' . $person->get_last_name();
874 my $user_role;
875 my $user_id;
877 if ( $c->user ) {
878 $user_role = $c->user->get_object->get_user_type();
879 $user_id = $c->user->get_object->get_sp_person_id();
882 if ( ( $user_role =~ /submitter|user/ && $user_id != $owner_id )
883 || !$c->user )
885 my $page = "/" . $c->req->path;
886 $c->res->redirect("/solgs/login/message?page=$page");
887 $c->detach;
889 else {
890 my $user_name = $c->user->id;
891 my $protocol_url = $c->controller('solGS::genotypingProtocol')
892 ->create_protocol_url( $c, $protocol_id );
894 if ($file_id) {
895 $c->controller('solGS::Files')
896 ->population_metadata_file( $c, $tmp_dir, $file_id );
897 my $metadata_file = $c->stash->{population_metadata_file};
898 my @metadata = read_file( $metadata_file, { binmode => ':utf8' } );
900 my ( $key, $desc );
902 ($desc) = grep { /description/ } @metadata;
903 ( $key, $desc ) = split( /\t/, $desc );
905 $c->stash(
906 project_id => $file_id,
907 project_name => $list_name,
908 selection_pop_name => $list_name,
909 project_desc => $desc,
910 owner => $owner,
911 protocol => $protocol_url,
917 sub get_list_trials_ids {
918 my ( $self, $c ) = @_;
920 my $list_id = $c->stash->{list_id};
922 my $list =
923 CXGN::List->new( { dbh => $c->dbc()->dbh(), list_id => $list_id } );
924 my @trials_names = @{ $list->elements };
926 my $list_type = $list->type();
927 my @trials_ids;
929 if ( $list_type =~ /trials/ ) {
930 foreach my $t_name (@trials_names) {
931 my $trial_id = $c->controller('solGS::Search')->model($c)
932 ->project_details_by_name($t_name)->first->project_id;
934 push @trials_ids, $trial_id;
938 $c->stash->{trials_ids} = \@trials_ids;
939 $c->stash->{pops_ids_list} = \@trials_ids;
943 sub get_trials_list_pheno_data {
944 my ( $self, $c ) = @_;
946 my $trials_ids = $c->stash->{pops_ids_list};
948 #$c->controller('solGS::combinedTrials')->multi_pops_phenotype_data($c, $trials_ids);
949 $c->controller('solGS::AsyncJob')
950 ->submit_cluster_phenotype_query( $c, $trials_ids );
952 #$c->controller('solGS::solGS')->get_trials_phenotype_query_jobs_args($c, $trials_ids);
954 $c->controller('solGS::combinedTrials')
955 ->multi_pops_pheno_files( $c, $trials_ids );
956 my @pheno_files = split( "\t", $c->stash->{multi_pops_pheno_files} );
957 $c->stash->{phenotype_files_list} = \@pheno_files;
961 sub get_trials_list_geno_data {
962 my ( $self, $c ) = @_;
964 my $trials_ids = $c->stash->{pops_ids_list};
965 my $protocol_id = $c->stash->{genotyping_protocol_id};
967 $c->controller('solGS::AsyncJob')
968 ->submit_cluster_genotype_query( $c, $trials_ids, $protocol_id );
969 $c->controller('solGS::combinedTrials')
970 ->multi_pops_geno_files( $c, $trials_ids );
971 my @geno_files = split( "\t", $c->stash->{multi_pops_geno_files} );
972 $c->stash->{genotype_files_list} = \@geno_files;
976 # sub get_trial_genotype_data {
977 # my ($self, $c) = @_;
979 # my $pop_id = $c->stash->{pop_id};
980 # my $protocol_id = $c->stash->{genotyping_protocol_id};
982 # $c->controller('solGS::Files')->genotype_file_name($c, $pop_id, $protocol_id);
983 # my $geno_file = $c->stash->{genotype_file_name};
985 # if (-s $geno_file)
987 # $c->stash->{genotype_file} = $geno_file;
989 # else
991 # $c->controller('solGS::solGS')->genotype_file($c);
996 sub register_trials_list {
997 my ( $self, $c ) = @_;
999 my $trials_ids = $c->stash->{pops_ids_list};
1001 if ($trials_ids) {
1002 $c->controller('solGS::combinedTrials')
1003 ->catalogue_combined_pops( $c, $trials_ids );
1008 sub list_file_id {
1009 my ( $self, $c ) = @_;
1011 my $list_id = $c->stash->{list_id};
1012 if ( $list_id =~ /dataset/ ) {
1013 return $list_id;
1015 else {
1016 return 'list_' . $list_id;
1021 sub begin : Private {
1022 my ( $self, $c ) = @_;
1024 $c->controller('solGS::Files')->get_solgs_dirs($c);