use cxgn::trial get_asssayed_traits func
[sgn.git] / lib / SGN / Controller / solGS / solGS.pm
blob96ad056fdd6537ee26a5e1b330eb466eb27b8453
1 package SGN::Controller::solGS::solGS;
3 use Moose;
4 use namespace::autoclean;
6 use String::CRC;
7 use URI::FromHash 'uri';
8 use File::Path qw / mkpath /;
9 use File::Spec::Functions qw / catfile catdir/;
10 use File::Temp qw / tempfile tempdir /;
11 use File::Slurp qw /write_file read_file/;
12 use File::Copy;
13 use File::Basename;
14 use Cache::File;
15 use Try::Tiny;
16 use List::MoreUtils qw /uniq/;
17 use Scalar::Util qw /weaken reftype/;
18 use Statistics::Descriptive;
19 use Math::Round::Var;
20 use Algorithm::Combinatorics qw /combinations/;
21 use Array::Utils qw(:all);
22 use CXGN::Tools::Run;
23 use JSON;
24 use Storable qw/ nstore retrieve /;
25 use Carp qw/ carp confess croak /;
26 use SGN::Controller::solGS::Utils;
27 use solGS::queryJobs;
28 use solGS::asyncJob;
29 use CXGN::Genotype::Search;
31 BEGIN { extends 'Catalyst::Controller' }
34 # Sets the actions in this controller to be registered with no prefix
35 # so they function identically to actions created in MyApp.pm
38 #__PACKAGE__->config(namespace => '');
40 =head1 NAME
42 solGS::Controller::Root - Root Controller for solGS
44 =head1 DESCRIPTION
46 [enter your description here]
48 =head1 METHODS
50 =head2 index
52 The root page (/)
54 =cut
57 # sub index :Path :Args(0) {
58 # my ($self, $c) = @_;
59 # $c->forward('search');
60 # }
62 sub solgs : Path('/solgs'){
63 my ($self, $c) = @_;
64 $c->forward('search');
68 sub solgs_breeder_search :Path('/solgs/breeder_search') Args(0) {
69 my ($self, $c) = @_;
70 $c->stash->{referer} = $c->req->referer();
71 $c->stash->{template} = '/solgs/breeder_search_solgs.mas';
75 sub submit :Path('/solgs/submit/intro') Args(0) {
76 my ($self, $c) = @_;
78 $c->stash->{template} = $c->controller('solGS::Files')->template('/submit/intro.mas');
82 sub solgs_login_message :Path('/solgs/login/message') Args(0) {
83 my ($self, $c) = @_;
85 my $page = $c->req->param('page');
87 my $message = "This is a private data. If you are the owner, "
88 . "please <a href=\"/user/login?goto_url=$page\">login</a> to view it.";
90 $c->stash->{message} = $message;
92 $c->stash->{template} = "/generic_message.mas";
97 sub search : Path('/solgs/search') Args() {
98 my ($self, $c) = @_;
100 #$self->gs_traits_index($c);
101 #my $gs_traits_index = $c->stash->{gs_traits_index};
103 $c->stash(template => $c->controller('solGS::Files')->template('/search/solgs.mas'),
104 # gs_traits_index => $gs_traits_index,
110 sub search_trials : Path('/solgs/search/trials') Args() {
111 my ($self, $c) = @_;
113 my $show_result = $c->req->param('show_result');
114 my $limit = $show_result =~ /all/ ? undef : 10;
116 my $projects_ids = $c->model('solGS::solGS')->all_gs_projects($limit);
118 my $ret->{status} = 'failed';
120 my $formatted_trials = [];
122 if (@$projects_ids)
124 my $projects_rs = $c->model('solGS::solGS')->project_details($projects_ids);
126 $self->get_projects_details($c, $projects_rs);
127 my $projects = $c->stash->{projects_details};
129 $self->format_gs_projects($c, $projects);
130 $formatted_trials = $c->stash->{formatted_gs_projects};
132 $ret->{status} = 'success';
135 $ret->{trials} = $formatted_trials;
136 $ret = to_json($ret);
138 $c->res->content_type('application/json');
139 $c->res->body($ret);
144 sub projects_links {
145 my ($self, $c, $pr_rs) = @_;
147 $self->get_projects_details($c, $pr_rs);
148 my $projects = $c->stash->{projects_details};
150 my @projects_pages;
151 my $update_marker_count;
153 foreach my $pr_id (keys %$projects)
155 my $pr_name = $projects->{$pr_id}{project_name};
156 my $pr_desc = $projects->{$pr_id}{project_desc};
157 my $pr_year = $projects->{$pr_id}{project_year};
158 my $pr_location = $projects->{$pr_id}{project_location};
160 my $dummy_name = $pr_name =~ /test\w*/ig;
161 #my $dummy_desc = $pr_desc =~ /test\w*/ig;
163 $self->check_population_has_genotype($c);
164 my $has_genotype = $c->stash->{population_has_genotype};
166 no warnings 'uninitialized';
168 unless ($dummy_name || !$pr_name )
170 #$self->trial_compatibility_table($c, $has_genotype);
171 #my $match_code = $c->stash->{trial_compatibility_code};
173 my $checkbox = qq |<form> <input type="checkbox" name="project" value="$pr_id" onclick="solGS.combinedTrials.getPopIds()"/> </form> |;
175 #$match_code = qq | <div class=trial_code style="color: $match_code; background-color: $match_code; height: 100%; width:30px">code</div> |;
177 push @projects_pages, [$checkbox, qq|<a href="/solgs/population/$pr_id" onclick="solGS.waitPage(this.href); return false;">$pr_name</a>|,
178 $pr_desc, $pr_location, $pr_year
186 $c->stash->{projects_pages} = \@projects_pages;
190 sub search_trials_trait : Path('/solgs/search/trials/trait') Args(1) {
191 my ($self, $c, $trait_id) = @_;
193 $self->get_trait_details($c, $trait_id);
195 $c->stash->{template} = $c->controller('solGS::Files')->template('/search/trials/trait.mas');
200 sub show_search_result_pops : Path('/solgs/search/result/populations') Args(1) {
201 my ($self, $c, $trait_id) = @_;
203 my $combine = $c->req->param('combine');
204 my $page = $c->req->param('page') || 1;
206 my $projects_ids = $c->model('solGS::solGS')->search_trait_trials($trait_id);
208 my $ret->{status} = 'failed';
209 my $formatted_projects = [];
211 if (@$projects_ids)
213 my $projects_rs = $c->model('solGS::solGS')->project_details($projects_ids);
214 my $trait = $c->model('solGS::solGS')->trait_name($trait_id);
216 $self->get_projects_details($c, $projects_rs);
217 my $projects = $c->stash->{projects_details};
219 $self->format_trait_gs_projects($c, $trait_id, $projects);
220 $formatted_projects = $c->stash->{formatted_gs_projects};
222 $ret->{status} = 'success';
225 $ret->{trials} = $formatted_projects;
227 $ret = to_json($ret);
229 $c->res->content_type('application/json');
230 $c->res->body($ret);
235 sub format_trait_gs_projects {
236 my ($self, $c, $trait_id, $projects) = @_;
238 my @formatted_projects;
240 foreach my $pr_id (keys %$projects)
242 my $pr_name = $projects->{$pr_id}{project_name};
243 my $pr_desc = $projects->{$pr_id}{project_desc};
244 my $pr_year = $projects->{$pr_id}{project_year};
245 my $pr_location = $projects->{$pr_id}{project_location};
247 $c->stash->{pop_id} = $pr_id;
248 $self->check_population_has_genotype($c);
249 my $has_genotype = $c->stash->{population_has_genotype};
251 if ($has_genotype)
253 my $trial_compatibility_file = $self->trial_compatibility_file($c);
255 $self->trial_compatibility_table($c, $has_genotype);
256 my $match_code = $c->stash->{trial_compatibility_code};
258 my $checkbox = qq |<form> <input type="checkbox" name="project" value="$pr_id" onclick="solGS.combinedTrials.getPopIds()"/> </form> |;
259 $match_code = qq | <div class=trial_code style="color: $match_code; background-color: $match_code; height: 100%; width:100%">code</div> |;
261 push @formatted_projects, [ $checkbox, qq|<a href="/solgs/trait/$trait_id/population/$pr_id" onclick="solGS.waitPage(this.href); return false;">$pr_name</a>|, $pr_desc, $pr_location, $pr_year, $match_code];
265 $c->stash->{formatted_gs_projects} = \@formatted_projects;
270 sub format_gs_projects {
271 my ($self, $c, $projects) = @_;
273 my @formatted_projects;
275 foreach my $pr_id (keys %$projects)
277 my $pr_name = $projects->{$pr_id}{project_name};
278 my $pr_desc = $projects->{$pr_id}{project_desc};
279 my $pr_year = $projects->{$pr_id}{project_year};
280 my $pr_location = $projects->{$pr_id}{project_location};
282 # $c->stash->{pop_id} = $pr_id;
283 # $self->check_population_has_genotype($c);
284 # my $has_genotype = $c->stash->{population_has_genotype};
285 my $has_genotype = $c->config->{default_genotyping_protocol};
287 if ($has_genotype)
289 my $trial_compatibility_file = $self->trial_compatibility_file($c);
291 $self->trial_compatibility_table($c, $has_genotype);
292 my $match_code = $c->stash->{trial_compatibility_code};
294 my $checkbox = qq |<form> <input type="checkbox" name="project" value="$pr_id" onclick="solGS.combinedTrials.getPopIds()"/> </form> |;
295 $match_code = qq | <div class=trial_code style="color: $match_code; background-color: $match_code; height: 100%; width:100%">code</div> |;
297 push @formatted_projects, [ $checkbox, qq|<a href="/solgs/population/$pr_id" onclick="solGS.waitPage(this.href); return false;">$pr_name</a>|, $pr_desc, $pr_location, $pr_year, $match_code];
301 $c->stash->{formatted_gs_projects} = \@formatted_projects;
306 sub trial_compatibility_table {
307 my ($self, $c, $markers) = @_;
309 $self->trial_compatibility_file($c);
310 my $compatibility_file = $c->stash->{trial_compatibility_file};
312 my $color;
314 if (-s $compatibility_file)
316 my @line = read_file($compatibility_file);
317 my ($entry) = grep(/$markers/, @line);
318 chomp($entry);
320 if($entry)
322 ($markers, $color) = split(/\t/, $entry);
323 $c->stash->{trial_compatibility_code} = $color;
327 if (!$color)
329 my ($red, $blue, $green) = map { int(rand(255)) } 1..3;
330 $color = 'rgb' . '(' . "$red,$blue,$green" . ')';
332 my $color_code = $markers . "\t" . $color . "\n";
334 $c->stash->{trial_compatibility_code} = $color;
335 write_file($compatibility_file,{append => 1}, $color_code);
340 sub trial_compatibility_file {
341 my ($self, $c) = @_;
343 my $cache_data = {key => 'trial_compatibility',
344 file => 'trial_compatibility_codes',
345 stash_key => 'trial_compatibility_file'
348 $c->controller('solGS::Files')->cache_file($c, $cache_data);
353 sub get_projects_details {
354 my ($self, $c, $pr_rs) = @_;
356 my ($year, $location, $pr_id, $pr_name, $pr_desc);
357 my %projects_details = ();
359 while (my $pr = $pr_rs->next)
361 $pr_id = $pr->get_column('project_id');
362 $pr_name = $pr->get_column('name');
363 $pr_desc = $pr->get_column('description');
365 my $pr_yr_rs = $c->model('solGS::solGS')->project_year($pr_id);
367 while (my $pr = $pr_yr_rs->next)
369 $year = $pr->value;
372 my $location = $c->model('solGS::solGS')->project_location($pr_id);
374 $projects_details{$pr_id} = {
375 project_name => $pr_name,
376 project_desc => $pr_desc,
377 project_year => $year,
378 project_location => $location,
382 $c->stash->{projects_details} = \%projects_details;
387 sub store_project_marker_count {
388 my ($self, $c) = @_;
390 my $pop_id = $c->stash->{pop_id};
391 my $marker_count = $c->stash->{marker_count};
393 unless ($marker_count)
395 my $markers = $c->model("solGS::solGS")->get_project_genotyping_markers($pop_id);
396 my @markers = split('\t', $markers);
397 $marker_count = scalar(@markers);
400 my $genoprop = {'project_id' => $pop_id, 'marker_count' => $marker_count};
401 $c->model("solGS::solGS")->set_project_genotypeprop($genoprop);
406 sub search_traits : Path('/solgs/search/traits/') Args(1) {
407 my ($self, $c, $query) = @_;
409 my $traits = $c->model('solGS::solGS')->search_trait($query);
410 my $result = $c->model('solGS::solGS')->trait_details($traits);
412 my $ret->{status} = 0;
413 if ($result->first)
415 $ret->{status} = 1;
418 $ret = to_json($ret);
420 $c->res->content_type('application/json');
421 $c->res->body($ret);
426 sub show_search_result_traits : Path('/solgs/search/result/traits') Args(1) {
427 my ($self, $c, $query) = @_;
429 my $traits = $c->model('solGS::solGS')->search_trait($query);
430 my $result = $c->model('solGS::solGS')->trait_details($traits);
432 my @rows;
433 while (my $row = $result->next)
435 my $id = $row->cvterm_id;
436 my $name = $row->name;
437 my $def = $row->definition;
439 push @rows, [ qq |<a href="/solgs/search/trials/trait/$id" onclick="solGS.waitPage()">$name</a>|, $def];
442 if (@rows)
444 $c->stash(template => $c->controller('solGS::Files')->template('/search/result/traits.mas'),
445 result => \@rows,
446 query => $query,
453 sub population : Path('/solgs/population') Args(1) {
454 my ($self, $c, $pop_id) = @_;
456 if (!$pop_id)
458 $c->stash->{message} = "You can not access this page with out population id.";
459 $c->stash->{template} = "/generic_message.mas";
462 $c->stash->{pop_id} = $pop_id;
464 if ($pop_id =~ /dataset/)
466 $c->stash->{dataset_id} = $pop_id =~ s/\w+_//r;
468 elsif ($pop_id =~ /list/)
470 $c->stash->{list_id} = $pop_id =~ s/\w+_//r;
473 $c->controller('solGS::Files')->phenotype_file_name($c, $pop_id);
474 $c->stash->{phenotype_file} = $c->stash->{phenotype_file_name};
476 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
477 $c->stash->{genotype_file} = $c->stash->{genotype_file_name};
479 if (!-s $c->stash->{phenotype_file} || !-s $c->stash->{genotype_file})
481 $c->stash->{message} = "Cached output for this training population does not exist anymore.\n"
482 . "Please go to <a href=\"/solgs/search/\">the search page</a>"
483 . " and create the training population data.";
485 $c->stash->{template} = "/generic_message.mas";
487 else
489 $self->get_all_traits($c);
490 $self->project_description($c, $pop_id);
492 $c->stash->{template} = $c->controller('solGS::Files')->template('/population.mas');
494 my $acronym = $self->get_acronym_pairs($c, $pop_id);
495 $c->stash->{acronym} = $acronym;
501 sub get_project_details {
502 my ($self, $c, $pr_id) = @_;
504 my $pr_rs = $c->model('solGS::solGS')->project_details($pr_id);
506 while (my $row = $pr_rs->next)
508 $c->stash(project_id => $row->id,
509 project_name => $row->name,
510 project_desc => $row->description
517 sub get_markers_count {
518 my ($self, $c, $pop_hash) = @_;
520 my $filtered_geno_file;
521 my $markers_cnt;
523 if ($pop_hash->{training_pop})
525 my $training_pop_id = $pop_hash->{training_pop_id};
526 $c->stash->{pop_id} = $training_pop_id;
527 $c->controller('solGS::Files')->filtered_training_genotype_file($c);
528 $filtered_geno_file = $c->stash->{filtered_training_genotype_file};
530 if (-s $filtered_geno_file) {
531 my @geno_lines = read_file($filtered_geno_file);
532 $markers_cnt = scalar(split('\t', $geno_lines[0])) - 1;
534 else
536 $c->controller('solGS::Files')->genotype_file_name($c, $training_pop_id);
537 my $geno_file = $c->stash->{genotype_file_name};
538 my @geno_lines = read_file($geno_file);
539 $markers_cnt= scalar(split ('\t', $geno_lines[0])) - 1;
543 elsif ($pop_hash->{selection_pop})
545 my $selection_pop_id = $pop_hash->{selection_pop_id};
546 $c->stash->{pop_id} = $selection_pop_id;
547 $c->controller('solGS::Files')->filtered_selection_genotype_file($c);
548 $filtered_geno_file = $c->stash->{filtered_selection_genotype_file};
550 if (-s $filtered_geno_file) {
551 my @geno_lines = read_file($filtered_geno_file);
552 $markers_cnt = scalar(split('\t', $geno_lines[0])) - 1;
554 else
556 $c->controller('solGS::Files')->genotype_file_name($c, $selection_pop_id);
557 my $geno_file = $c->stash->{genotype_file_name};
558 my @geno_lines = read_file($geno_file);
559 $markers_cnt= scalar(split ('\t', $geno_lines[0])) - 1;
563 return $markers_cnt;
568 sub create_protocol_url {
569 my ($self, $c, $protocol) = @_;
571 $protocol = $c->config->{default_genotyping_protocol} if !$protocol;
573 my $protocol_url;
574 if ($protocol)
576 my $protocol_id = $c->model('solGS::solGS')->protocol_id($protocol);
577 $protocol_url = '<a href="/breeders_toolbox/protocol/' . $protocol_id . '">' . $protocol . '</a>';
579 else
581 $protocol_url = 'N/A';
584 return $protocol_url;
588 sub project_description {
589 my ($self, $c, $pr_id) = @_;
591 $c->stash->{pop_id} = $pr_id;
593 my $protocol = $self->create_protocol_url($c);
595 if ($c->stash->{list_id})
597 $c->controller('solGS::List')->list_population_summary($c);
599 elsif ($c->stash->{dataset_id})
601 $c->controller('solGS::Dataset')->dataset_population_summary($c);
603 else
605 my $pr_rs = $c->model('solGS::solGS')->project_details($pr_id);
607 while (my $row = $pr_rs->next)
609 $c->stash(project_id => $row->id,
610 project_name => $row->name,
611 project_desc => $row->description
615 $self->get_project_owners($c, $pr_id);
616 $c->stash->{owner} = $c->stash->{project_owners};
619 $c->controller('solGS::Files')->filtered_training_genotype_file($c);
620 my $filtered_geno_file = $c->stash->{filtered_training_genotype_file};
622 my $markers_no;
623 my @geno_lines;
625 if (-s $filtered_geno_file) {
626 @geno_lines = read_file($filtered_geno_file);
627 $markers_no = scalar(split('\t', $geno_lines[0])) - 1;
629 else
631 $c->controller('solGS::Files')->genotype_file_name($c, $pr_id);
632 my $geno_file = $c->stash->{genotype_file_name};
633 @geno_lines = read_file($geno_file);
634 $markers_no = scalar(split ('\t', $geno_lines[0])) - 1;
637 my $stocks_no = $self->training_pop_member_count($c, $pr_id);
639 $c->controller('solGS::Files')->traits_acronym_file($c, $pr_id);
640 my $traits_file = $c->stash->{traits_acronym_file};
641 my @traits_lines = read_file($traits_file);
642 my $traits_no = scalar(@traits_lines) - 1;
644 $c->stash(markers_no => $markers_no,
645 traits_no => $traits_no,
646 stocks_no => $stocks_no,
647 protocol => $protocol,
653 sub training_pop_member_count {
654 my ($self, $c, $pop_id) = @_;
656 $c->stash->{pop_id} = $pop_id if $pop_id;
658 $c->controller("solGS::Files")->trait_phenodata_file($c);
659 my $trait_pheno_file = $c->stash->{trait_phenodata_file};
660 my @trait_pheno_lines = read_file($trait_pheno_file) if $trait_pheno_file;
662 my @geno_lines;
663 if (!@trait_pheno_lines)
665 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
666 my $geno_file = $c->stash->{genotype_file_name};
667 @geno_lines = read_file($geno_file);
670 my $count = @trait_pheno_lines ? scalar(@trait_pheno_lines) - 1 : scalar(@geno_lines) - 1;
672 return $count;
676 sub check_training_pop_size : Path('/solgs/check/training/pop/size') Args(0) {
677 my ($self, $c) = @_;
679 my $pop_id = $c->req->param('training_pop_id');
680 my $type = $c->req->param('data_set_type');
682 my $count;
683 if ($type =~ /single/)
685 $count = $self->training_pop_member_count($c, $pop_id);
687 elsif ($type =~ /combined/)
689 $count = $c->controller('solGS::combinedTrials')->count_combined_trials_members($c, $pop_id);
692 my $ret->{status} = 'failed';
694 if ($count)
696 $ret->{status} = 'success';
697 $ret->{member_count} = $count;
700 $ret = to_json($ret);
702 $c->res->content_type('application/json');
703 $c->res->body($ret);
709 sub selection_trait :Path('/solgs/selection/') Args(5) {
710 my ($self, $c, $selection_pop_id,
711 $model_key, $training_pop_id,
712 $trait_key, $trait_id) = @_;
714 $self->get_trait_details($c, $trait_id);
715 $c->stash->{training_pop_id} = $training_pop_id;
716 $c->stash->{selection_pop_id} = $selection_pop_id;
717 $c->stash->{data_set_type} = 'single population';
719 if ($training_pop_id =~ /list/)
721 $c->stash->{list_id} = $training_pop_id =~ s/\w+_//r;
722 $c->controller('solGS::List')->list_population_summary($c);
723 $c->stash->{training_pop_id} = $c->stash->{project_id};
724 $c->stash->{training_pop_name} = $c->stash->{project_name};
725 $c->stash->{training_pop_desc} = $c->stash->{project_desc};
726 $c->stash->{training_pop_owner} = $c->stash->{owner};
728 elsif ($training_pop_id =~ /dataset/)
730 $c->stash->{dataset_id} = $training_pop_id =~ s/\w+_//r;
731 $c->controller('solGS::Dataset')->dataset_population_summary($c);
732 $c->stash->{training_pop_id} = $c->stash->{project_id};
733 $c->stash->{training_pop_name} = $c->stash->{project_name};
734 $c->stash->{training_pop_desc} = $c->stash->{project_desc};
735 $c->stash->{training_pop_owner} = $c->stash->{owner};
737 else
739 $self->get_project_details($c, $training_pop_id);
740 $c->stash->{training_pop_id} = $c->stash->{project_id};
741 $c->stash->{training_pop_name} = $c->stash->{project_name};
742 $c->stash->{training_pop_desc} = $c->stash->{project_desc};
744 $self->get_project_owners($c, $training_pop_id);
745 $c->stash->{training_pop_owner} = $c->stash->{project_owners};
748 if ($selection_pop_id =~ /list/)
750 $c->stash->{list_id} = $selection_pop_id =~ s/\w+_//r;
752 $c->controller('solGS::List')->list_population_summary($c);
753 $c->stash->{selection_pop_id} = $c->stash->{project_id};
754 $c->stash->{selection_pop_name} = $c->stash->{project_name};
755 $c->stash->{selection_pop_desc} = $c->stash->{project_desc};
756 $c->stash->{selection_pop_owner} = $c->stash->{owner};
758 elsif ($selection_pop_id =~ /dataset/)
760 $c->stash->{dataset_id} = $selection_pop_id =~ s/\w+_//r;
761 $c->controller('solGS::Dataset')->dataset_population_summary($c);
762 $c->stash->{selection_pop_id} = $c->stash->{project_id};
763 $c->stash->{selection_pop_name} = $c->stash->{project_name};
764 $c->stash->{selection_pop_desc} = $c->stash->{project_desc};
765 $c->stash->{selection_pop_owner} = $c->stash->{owner};
767 else
769 $self->get_project_details($c, $selection_pop_id);
770 $c->stash->{selection_pop_id} = $c->stash->{project_id};
771 $c->stash->{selection_pop_name} = $c->stash->{project_name};
772 $c->stash->{selection_pop_desc} = $c->stash->{project_desc};
774 $self->get_project_owners($c, $selection_pop_id);
775 $c->stash->{selection_pop_owner} = $c->stash->{project_owners};
778 my $tr_pop_mr_cnt = $self->get_markers_count($c, {'training_pop' => 1, 'training_pop_id' => $training_pop_id});
779 my $sel_pop_mr_cnt = $self->get_markers_count($c, {'selection_pop' => 1, 'selection_pop_id' => $selection_pop_id});
781 $c->stash->{training_markers_cnt} = $tr_pop_mr_cnt;
782 $c->stash->{selection_markers_cnt} = $sel_pop_mr_cnt;
784 my $protocol = $self->create_protocol_url($c);
785 $c->stash->{protocol} = $protocol;
787 my $identifier = $training_pop_id . '_' . $selection_pop_id;
789 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
790 my $gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
792 my @stock_rows = read_file($gebvs_file);
793 $c->stash->{selection_stocks_cnt} = scalar(@stock_rows) - 1;
795 $self->top_blups($c, $gebvs_file);
797 $c->stash->{blups_download_url} = qq | <a href="/solgs/download/prediction/model/$training_pop_id/prediction/$selection_pop_id/$trait_id">Download all GEBVs</a>|;
799 $c->stash->{template} = $c->controller('solGS::Files')->template('/population/selection_trait.mas');
804 sub build_single_trait_model {
805 my ($self, $c) = @_;
807 my $trait_id = $c->stash->{trait_id};
808 $self->get_trait_details($c, $trait_id);
810 $self->get_rrblup_output($c);
815 sub trait :Path('/solgs/trait') Args(3) {
816 my ($self, $c, $trait_id, $key, $pop_id) = @_;
818 if ($pop_id =~ /dataset/)
820 $c->stash->{dataset_id} = $pop_id =~ s/\w+_//r;
822 elsif ($pop_id =~ /list/)
824 $c->stash->{list_id} = $pop_id =~ s/\w+_//r;
827 $c->stash->{pop_id} = $pop_id;
828 $c->stash->{trait_id} = $trait_id;
830 if ($pop_id && $trait_id)
832 $c->controller('solGS::Files')->rrblup_training_gebvs_file($c);
833 my $gebv_file = $c->stash->{rrblup_training_gebvs_file};
835 $self->project_description($c, $pop_id);
836 my $training_pop_name = $c->stash->{project_name};
837 my $training_pop_desc = $c->stash->{project_desc};
838 my $training_pop_page = qq | <a href="/solgs/population/$pop_id">$training_pop_name</a> |;
840 if (!-s $gebv_file)
842 $c->stash->{message} = "Cached output for this model does not exist anymore.\n" .
843 " Please go to $training_pop_page and run the analysis.";
845 $c->stash->{template} = "/generic_message.mas";
847 else
849 $self->get_trait_details($c, $trait_id);
850 $self->gs_modeling_files($c);
852 $c->controller('solGS::Files')->traits_acronym_file($c, $pop_id);
853 my $acronym_file = $c->stash->{traits_acronym_file};
855 if (!-e $acronym_file || !-s $acronym_file)
857 $self->get_all_traits($c);
860 $self->trait_phenotype_stat($c);
861 $c->stash->{template} = $c->controller('solGS::Files')->template("/population/trait.mas");
868 sub gs_modeling_files {
869 my ($self, $c) = @_;
871 $self->output_files($c);
872 $self->input_files($c);
873 $self->model_accuracy($c);
874 $self->top_blups($c, $c->stash->{rrblup_training_gebvs_file});
875 $self->download_urls($c);
876 $self->top_markers($c, $c->stash->{marker_effects_file});
877 $self->model_parameters($c);
882 sub trait_info_file {
883 my ($self, $c) = @_;
885 my $pop_id = $c->stash->{pop_id} || $c->stash->{combo_pops_id};
886 my $trait_id = $c->stash->{trait_id};
887 my $trait_abbr = $c->stash->{trait_abbr};
888 my $name = "trait_info_${trait_id}_pop_${pop_id}";
889 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
890 my $trait_info = $trait_id . "\t" . $trait_abbr;
891 my $file = $c->controller('solGS::Files')->create_tempfile($temp_dir, $name);
893 write_file($file, $trait_info);
895 $c->stash->{trait_info_file} = $file;
899 sub input_files {
900 my ($self, $c) = @_;
902 if ($c->stash->{data_set_type} =~ /combined populations/i)
904 $c->controller('solGS::combinedTrials')->combined_pops_gs_input_files($c);
905 my $input_file = $c->stash->{combined_pops_gs_input_files};
906 $c->stash->{input_files} = $input_file;
908 else
910 my $pop_id = $c->stash->{pop_id};
911 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
912 $c->controller('solGS::Files')->phenotype_file_name($c, $pop_id);
913 $self->trait_info_file($c);
915 $c->controller('solGS::Files')->formatted_phenotype_file($c);
916 my $formatted_phenotype_file = $c->stash->{formatted_phenotype_file};
918 my $selection_pop_id = $c->stash->{prediction_pop_id} ||$c->stash->{selection_pop_id} ;
919 my ($selection_population_file, $filtered_pred_geno_file);
921 if ($selection_pop_id)
923 $selection_population_file = $c->stash->{selection_population_file};
926 my $pheno_file = $c->stash->{phenotype_file_name};
927 my $geno_file = $c->stash->{genotype_file_name};
928 my $traits_file = $c->stash->{selected_traits_file};
929 my $trait_file = $c->stash->{trait_info_file};
931 no warnings 'uninitialized';
933 my $input_files = join ("\t",
934 $pheno_file,
935 $formatted_phenotype_file,
936 $geno_file,
937 $traits_file,
938 $trait_file,
939 $selection_population_file,
942 my $name = "input_files_${pop_id}";
943 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
944 my $tempfile = $c->controller('solGS::Files')->create_tempfile($temp_dir, $name);
945 write_file($tempfile, $input_files);
946 $c->stash->{input_files} = $tempfile;
951 sub output_files {
952 my ($self, $c) = @_;
954 my $pop_id = $c->stash->{pop_id};
955 my $trait = $c->stash->{trait_abbr};
956 my $trait_id = $c->stash->{trait_id};
958 $c->controller('solGS::Files')->marker_effects_file($c);
959 $c->controller('solGS::Files')->rrblup_training_gebvs_file($c);
960 $c->controller('solGS::Files')->validation_file($c);
961 $c->controller("solGS::Files")->trait_phenodata_file($c);
962 $c->controller("solGS::Files")->variance_components_file($c);
963 $c->controller('solGS::Files')->relationship_matrix_file($c);
964 $c->controller('solGS::Files')->filtered_training_genotype_file($c);
966 my $selection_pop_id = $c->stash->{prediction_pop_id} || $c->stash->{selection_pop_id};
967 if (!$pop_id) {$pop_id = $c->stash->{model_id};}
969 no warnings 'uninitialized';
971 if ($selection_pop_id)
973 my $identifier = $pop_id . '_' . $selection_pop_id;
974 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
977 my $file_list = join ("\t",
978 $c->stash->{rrblup_training_gebvs_file},
979 $c->stash->{marker_effects_file},
980 $c->stash->{validation_file},
981 $c->stash->{trait_phenodata_file},
982 $c->stash->{selected_traits_gebv_file},
983 $c->stash->{variance_components_file},
984 $c->stash->{relationship_matrix_file},
985 $c->stash->{filtered_training_genotype_file},
986 $c->stash->{rrblup_selection_gebvs_file}
989 my $name = "output_files_${trait}_$pop_id";
990 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
991 my $tempfile = $c->controller('solGS::Files')->create_tempfile($temp_dir, $name);
992 write_file($tempfile, $file_list);
994 $c->stash->{output_files} = $tempfile;
999 sub download_blups :Path('/solgs/download/blups/pop') Args(3) {
1000 my ($self, $c, $pop_id, $trait, $trait_id) = @_;
1002 $c->stash->{pop_id} = $pop_id;
1003 $self->get_trait_details($c, $trait_id);
1004 my $trait_abbr = $c->stash->{trait_abbr};
1006 my $referer = $c->req->referer;
1007 if ($referer =~ /combined\/populations\//)
1009 $c->stash->{data_set_type} = 'combined populations';
1010 $c->stash->{combo_pops_id} = $pop_id;
1013 $c->controller('solGS::Files')->rrblup_training_gebvs_file($c);
1014 my $blups_file = $c->stash->{rrblup_training_gebvs_file};
1016 unless (!-e $blups_file || -s $blups_file == 0)
1018 my @blups = map { [ split(/\t/) ] } read_file($blups_file);
1020 $c->res->content_type("text/plain");
1021 $c->res->body(join "", map { $_->[0] . "\t" . $_->[1] } @blups);
1027 sub download_marker_effects :Path('/solgs/download/marker/pop') Args(3) {
1028 my ($self, $c, $pop_id, $trait, $trait_id) = @_;
1030 $c->stash->{pop_id} = $pop_id;
1031 $self->get_trait_details($c, $trait_id);
1032 my $trait_abbr = $c->stash->{trait_abbr};
1034 $c->controller('solGS::Files')->marker_effects_file($c);
1035 my $markers_file = $c->stash->{marker_effects_file};
1037 unless (!-e $markers_file || -s $markers_file == 0)
1039 my @effects = map { [ split(/\t/) ] } read_file($markers_file);
1041 $c->res->content_type("text/plain");
1042 $c->res->body(join "", map { $_->[0] . "\t" . $_->[1] } @effects);
1048 sub download_urls {
1049 my ($self, $c) = @_;
1050 my $data_set_type = $c->stash->{data_set_type};
1051 my $pop_id;
1053 no warnings 'uninitialized';
1055 if ($data_set_type =~ /combined populations/)
1057 $pop_id = $c->stash->{combo_pops_id};
1059 else
1061 $pop_id = $c->stash->{pop_id};
1064 my $trait_id = $c->stash->{trait_id};
1066 my $blups_url = qq | <a href="/solgs/download/blups/pop/$pop_id/trait/$trait_id">Download all GEBVs</a> |;
1067 my $marker_url = qq | <a href="/solgs/download/marker/pop/$pop_id/trait/$trait_id">Download all marker effects</a> |;
1068 my $validation_url = qq | <a href="/solgs/download/validation/pop/$pop_id/trait/$trait_id">Download model accuracy report</a> |;
1070 $c->stash(blups_download_url => $blups_url,
1071 marker_effects_download_url => $marker_url,
1072 validation_download_url => $validation_url);
1078 sub top_markers {
1079 my ($self, $c, $markers_file) = @_;
1081 $c->stash->{top_marker_effects} = $c->controller('solGS::Utils')->top_10($markers_file);
1085 sub top_blups {
1086 my ($self, $c, $gebv_file) = @_;
1088 $c->stash->{top_blups} = $c->controller('solGS::Utils')->top_10($gebv_file);
1092 sub download_validation :Path('/solgs/download/validation/pop') Args(3) {
1093 my ($self, $c, $pop_id, $trait, $trait_id) = @_;
1095 $c->stash->{pop_id} = $pop_id;
1096 $self->get_trait_details($c, $trait_id);
1097 my $trait_abbr = $c->stash->{trait_abbr};
1099 $c->controller('solGS::Files')->validation_file($c);
1100 my $validation_file = $c->stash->{validation_file};
1102 unless (!-e $validation_file || -s $validation_file == 0)
1104 my @validation = map { [ split(/\t/) ] } read_file($validation_file);
1106 $c->res->content_type("text/plain");
1107 $c->res->body(join "", map { $_->[0] . "\t" . $_->[1] } @validation);
1113 sub predict_selection_pop_single_trait {
1114 my ($self, $c) = @_;
1116 if ($c->stash->{data_set_type} =~ /single population/)
1118 $self->predict_selection_pop_single_pop_model($c)
1120 else
1122 $c->controller('solGS::combinedTrials')->predict_selection_pop_combined_pops_model($c);
1128 sub predict_selection_pop_multi_traits {
1129 my ($self, $c) = @_;
1131 my $data_set_type = $c->stash->{data_set_type};
1132 my $training_pop_id = $c->stash->{training_pop_id};
1133 my $selection_pop_id = $c->stash->{selection_pop_id};
1135 $c->stash->{pop_id} = $training_pop_id;
1137 my @traits = @{$c->stash->{training_traits_ids}} if $c->stash->{training_traits_ids};
1139 $self->traits_with_valid_models($c);
1140 my @traits_with_valid_models = @{$c->stash->{traits_ids_with_valid_models}};
1142 $c->stash->{training_traits_ids} = \@traits_with_valid_models;
1144 my @unpredicted_traits;
1145 foreach my $trait_id (@{$c->stash->{training_traits_ids}})
1147 my $identifier = $training_pop_id .'_' . $selection_pop_id;
1148 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
1150 push @unpredicted_traits, $trait_id if !-s $c->stash->{rrblup_selection_gebvs_file};
1153 if (@unpredicted_traits)
1155 $c->stash->{training_traits_ids} = \@unpredicted_traits;
1157 $c->controller('solGS::Files')->genotype_file_name($c, $selection_pop_id);
1159 if (!-s $c->stash->{genotype_file_name})
1161 $self->get_selection_pop_query_args_file($c);
1162 $c->stash->{prerequisite_jobs} = $c->stash->{selection_pop_query_args_file};
1165 $c->controller('solGS::Files')->selection_population_file($c, $selection_pop_id);
1167 $self->get_gs_modeling_jobs_args_file($c);
1168 $c->stash->{dependent_jobs} = $c->stash->{gs_modeling_jobs_args_file};
1171 #$c->stash->{prerequisite_type} = 'selection_pop_download_data';
1173 $self->run_async($c);
1175 else
1177 croak "No traits to predict: $!\n";
1183 sub predict_selection_pop_single_pop_model {
1184 my ($self, $c) = @_;
1186 my $trait_id = $c->stash->{trait_id};
1187 my $training_pop_id = $c->stash->{training_pop_id};
1188 my $prediction_pop_id = $c->stash->{prediction_pop_id} || $c->stash->{selection_pop_id};
1190 $self->get_trait_details($c, $trait_id);
1191 my $trait_abbr = $c->stash->{trait_abbr};
1193 my $identifier = $training_pop_id . '_' . $prediction_pop_id;
1194 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
1196 my $rrblup_selection_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
1197 $c->stash->{selection_pop_id} = $prediction_pop_id;
1199 if (!-s $rrblup_selection_gebvs_file)
1201 $c->stash->{pop_id} = $training_pop_id;
1202 $c->controller('solGS::Files')->phenotype_file_name($c, $training_pop_id);
1203 my $pheno_file = $c->stash->{phenotype_file_name};
1205 $c->controller('solGS::Files')->genotype_file_name($c, $training_pop_id);
1206 my $geno_file = $c->stash->{genotype_file_name};
1208 $c->stash->{pheno_file} = $pheno_file;
1209 $c->stash->{geno_file} = $geno_file;
1211 $c->controller('solGS::Files')->selection_population_file($c, $prediction_pop_id);
1213 $self->get_rrblup_output($c);
1219 sub selection_prediction :Path('/solgs/model') Args(3) {
1220 my ($self, $c, $training_pop_id, $pop, $selection_pop_id) = @_;
1222 my $referer = $c->req->referer;
1223 my $path = $c->req->path;
1224 my $base = $c->req->base;
1225 $referer =~ s/$base//;
1227 $c->stash->{training_pop_id} = $training_pop_id;
1228 $c->stash->{model_id} = $training_pop_id;
1229 $c->stash->{pop_id} = $training_pop_id;
1230 $c->stash->{prediction_pop_id} = $selection_pop_id;
1231 $c->stash->{selection_pop_id} = $selection_pop_id;
1233 if ($referer =~ /solgs\/model\/combined\/populations\//)
1235 my ($combo_pops_id, $trait_id) = $referer =~ m/(\d+)/g;
1237 $c->stash->{data_set_type} = "combined populations";
1238 $c->stash->{combo_pops_id} = $combo_pops_id;
1239 $c->stash->{trait_id} = $trait_id;
1241 $c->controller('solGS::combinedTrials')->predict_selection_pop_combined_pops_model($c);
1243 $c->controller('solGS::combinedTrials')->combined_pops_summary($c);
1244 $self->trait_phenotype_stat($c);
1245 $self->gs_modeling_files($c);
1247 $c->res->redirect("/solgs/model/combined/populations/$combo_pops_id/trait/$trait_id");
1248 $c->detach();
1250 elsif ($referer =~ /solgs\/trait\//)
1252 my ($trait_id, $pop_id) = $referer =~ m/(\d+)/g;
1254 $c->stash->{data_set_type} = "single population";
1255 $c->stash->{trait_id} = $trait_id;
1257 $self->predict_selection_pop_single_pop_model($c);
1259 $self->trait_phenotype_stat($c);
1260 $self->gs_modeling_files($c);
1262 $c->res->redirect("/solgs/trait/$trait_id/population/$training_pop_id");
1263 $c->detach();
1265 elsif ($referer =~ /solgs\/models\/combined\/trials/)
1267 $c->stash->{data_set_type} = "combined populations";
1268 $c->stash->{combo_pops_id} = $training_pop_id;
1270 $self->traits_with_valid_models($c);
1271 my @traits_abbrs = @ {$c->stash->{traits_with_valid_models}};
1273 foreach my $trait_abbr (@traits_abbrs)
1275 $c->stash->{trait_abbr} = $trait_abbr;
1276 $self->get_trait_details_of_trait_abbr($c);
1277 $c->controller('solGS::combinedTrials')->predict_selection_pop_combined_pops_model($c);
1280 $c->res->redirect("/solgs/models/combined/trials/$training_pop_id");
1281 $c->detach();
1283 elsif ($referer =~ /solgs\/traits\/all\/population\//)
1285 $c->stash->{data_set_type} = "single population";
1287 $self->predict_selection_pop_multi_traits($c);
1289 $c->res->redirect("/solgs/traits/all/population/$training_pop_id");
1290 $c->detach();
1296 sub list_predicted_selection_pops {
1297 my ($self, $c, $model_id) = @_;
1299 my $dir = $c->stash->{solgs_cache_dir};
1301 opendir my $dh, $dir or die "can't open $dir: $!\n";
1303 my @files = grep { /rrblup_selection_gebvs_\w+_${model_id}_/ && -f "$dir/$_" }
1304 readdir($dh);
1306 closedir $dh;
1308 my @pred_pops;
1310 foreach (@files)
1312 unless ($_ =~ /list/) {
1313 my ($model_id2, $pred_pop_id) = $_ =~ m/\d+/g;
1315 push @pred_pops, $pred_pop_id;
1319 @pred_pops = uniq(@pred_pops);
1321 $c->stash->{list_of_predicted_selection_pops} = \@pred_pops;
1326 sub download_prediction_GEBVs :Path('/solgs/download/prediction/model') Args(4) {
1327 my ($self, $c, $pop_id, $prediction, $prediction_id, $trait_id) = @_;
1329 $self->get_trait_details($c, $trait_id);
1330 $c->stash->{pop_id} = $pop_id;
1332 my $identifier = $pop_id . "_" . $prediction_id;
1333 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
1334 my $prediction_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
1336 unless (!-e $prediction_gebvs_file || -s $prediction_gebvs_file == 0)
1338 my @prediction_gebvs = map { [ split(/\t/) ] } read_file($prediction_gebvs_file);
1340 $c->res->content_type("text/plain");
1341 $c->res->body(join "", map { $_->[0] . "\t" . $_->[1] } @prediction_gebvs);
1347 sub prediction_pop_analyzed_traits {
1348 my ($self, $c, $training_pop_id, $selection_pop_id) = @_;
1350 my @selected_analyzed_traits = @{$c->stash->{training_traits_ids}} if $c->stash->{training_traits_ids};
1352 no warnings 'uninitialized';
1354 my $dir = $c->stash->{solgs_cache_dir};
1355 opendir my $dh, $dir or die "can't open $dir: $!\n";
1357 my @files;
1358 my @trait_ids;
1359 my @trait_abbrs;
1360 my @selected_trait_abbrs;
1361 my @selected_files;
1362 my $identifier = $training_pop_id . '_' . $selection_pop_id;
1364 if (@selected_analyzed_traits)
1366 @trait_ids;
1368 foreach my $trait_id (@selected_analyzed_traits)
1370 $c->stash->{trait_id} = $trait_id;
1371 $self->get_trait_details($c);
1372 push @selected_trait_abbrs, $c->stash->{trait_abbr};
1374 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
1375 my $file = $c->stash->{rrblup_selection_gebvs_file};
1377 if ( -s $c->stash->{rrblup_selection_gebvs_file})
1379 push @selected_files, $c->stash->{rrblup_selection_gebvs_file};
1380 push @trait_ids, $trait_id;
1385 @trait_abbrs = @selected_trait_abbrs if @selected_trait_abbrs;
1386 @files = @selected_files if @selected_files;
1388 $c->stash->{prediction_pop_analyzed_traits} = \@trait_abbrs;
1389 $c->stash->{prediction_pop_analyzed_traits_ids} = \@trait_ids;
1390 $c->stash->{prediction_pop_analyzed_traits_files} = \@files;
1395 sub download_prediction_urls {
1396 my ($self, $c, $training_pop_id, $prediction_pop_id) = @_;
1398 my $selection_traits_ids;
1399 my $download_url;
1401 my $selected_model_traits = $c->stash->{training_traits_ids};
1403 no warnings 'uninitialized';
1405 if ($prediction_pop_id)
1407 $self->prediction_pop_analyzed_traits($c, $training_pop_id, $prediction_pop_id);
1408 $selection_traits_ids = $c->stash->{prediction_pop_analyzed_traits_ids};
1411 my @selection_traits_ids = sort(@$selection_traits_ids) if $selection_traits_ids->[0];
1412 my @selected_model_traits = sort(@$selected_model_traits) if $selected_model_traits->[0];
1414 if (@selected_model_traits ~~ @selection_traits_ids)
1416 foreach my $trait_id (@selection_traits_ids)
1418 $self->get_trait_details($c, $trait_id);
1419 my $trait_abbr = $c->stash->{trait_abbr};
1421 my $page = $c->req->referer;
1422 if ($page =~ /solgs\/traits\/all\/|solgs\/models\/combined\//)
1424 $download_url .= " | " if $download_url;
1427 if ($page =~ /combined/)
1429 $download_url .= qq |<a href="/solgs/selection/$prediction_pop_id/model/combined/$training_pop_id/trait/$trait_id">$trait_abbr</a> |;
1431 else
1433 $download_url .= qq |<a href="/solgs/selection/$prediction_pop_id/model/$training_pop_id/trait/$trait_id">$trait_abbr</a> |;
1438 if ($download_url)
1440 $c->stash->{download_prediction} = $download_url;
1442 else
1444 $c->stash->{download_prediction} = qq | <a href ="/solgs/model/$training_pop_id/prediction/$prediction_pop_id" onclick="solGS.waitPage(this.href); return false;">[ Predict ]</a> |;
1451 sub model_accuracy {
1452 my ($self, $c) = @_;
1453 my $file = $c->stash->{validation_file};
1454 my @report =();
1456 if ( !-e $file) { @report = (["Validation file doesn't exist.", "None"]);}
1457 if ( -s $file == 0) { @report = (["There is no cross-validation output report.", "None"]);}
1459 if (!@report)
1461 @report = map { [ split(/\t/, $_) ]} read_file($file);
1464 shift(@report); #add condition
1466 $c->stash->{accuracy_report} = \@report;
1471 sub model_parameters {
1472 my ($self, $c) = @_;
1474 $c->controller("solGS::Files")->variance_components_file($c);
1475 my $file = $c->stash->{variance_components_file};
1477 my @params = map { [ split(/\t/, $_) ]} read_file($file);
1479 shift(@params); #add condition
1481 $c->stash->{model_parameters} = \@params;
1486 sub solgs_details_trait :Path('/solgs/details/trait/') Args(1) {
1487 my ($self, $c, $trait_id) = @_;
1489 $trait_id = $c->req->param('trait_id') if !$trait_id;
1491 my $ret->{status} = undef;
1493 if ($trait_id)
1495 $self->get_trait_details($c, $trait_id);
1496 $ret->{name} = $c->stash->{trait_name};
1497 $ret->{def} = $c->stash->{trait_def};
1498 $ret->{abbr} = $c->stash->{trait_abbr};
1499 $ret->{id} = $c->stash->{trait_id};
1500 $ret->{status} = 1;
1503 $ret = to_json($ret);
1505 $c->res->content_type('application/json');
1506 $c->res->body($ret);
1511 sub get_trait_details {
1512 my ($self, $c, $trait) = @_;
1514 $trait = $c->stash->{trait_id} if !$trait;
1516 die "Can't get trait details with out trait id or name: $!\n" if !$trait;
1518 my ($trait_name, $trait_def, $trait_id, $trait_abbr);
1520 if ($trait =~ /^\d+$/)
1522 $trait = $c->model('solGS::solGS')->trait_name($trait);
1525 if ($trait)
1527 my $rs = $c->model('solGS::solGS')->trait_details($trait);
1529 while (my $row = $rs->next)
1531 $trait_id = $row->id;
1532 $trait_name = $row->name;
1533 $trait_def = $row->definition;
1534 $trait_abbr = $c->controller('solGS::Utils')->abbreviate_term($trait_name);
1538 my $abbr = $c->controller('solGS::Utils')->abbreviate_term($trait_name);
1540 $c->stash->{trait_id} = $trait_id;
1541 $c->stash->{trait_name} = $trait_name;
1542 $c->stash->{trait_def} = $trait_def;
1543 $c->stash->{trait_abbr} = $abbr;
1548 sub check_selection_pops_list :Path('/solgs/check/selection/populations') Args(1) {
1549 my ($self, $c, $tr_pop_id) = @_;
1551 my @traits_ids = $c->req->param('training_traits_ids[]');
1552 $c->stash->{training_traits_ids} = \@traits_ids;
1554 $c->stash->{training_pop_id} = $tr_pop_id;
1556 $c->controller('solGS::Files')->list_of_prediction_pops_file($c, $tr_pop_id);
1557 my $pred_pops_file = $c->stash->{list_of_prediction_pops_file};
1559 my $ret->{result} = 0;
1561 if (-s $pred_pops_file)
1563 $self->list_of_prediction_pops($c, $tr_pop_id);
1564 my $selection_pops_ids = $c->stash->{selection_pops_ids};
1565 my $formatted_selection_pops = $c->stash->{list_of_prediction_pops};
1567 $self->prediction_pop_analyzed_traits($c, $tr_pop_id, $selection_pops_ids->[0]);
1568 my $selection_pop_traits = $c->stash->{prediction_pop_analyzed_traits_ids};
1570 $ret->{selection_traits} = $selection_pop_traits;
1571 $ret->{data} = $formatted_selection_pops;
1574 $ret = to_json($ret);
1576 $c->res->content_type('application/json');
1577 $c->res->body($ret);
1582 sub selection_population_predicted_traits :Path('/solgs/selection/population/predicted/traits/') Args(0) {
1583 my ($self, $c) = @_;
1585 my $training_pop_id = $c->req->param('training_pop_id');
1586 my $selection_pop_id = $c->req->param('selection_pop_id');
1588 my $ret->{selection_traits} = undef;
1589 if ($training_pop_id && $selection_pop_id)
1591 $self->prediction_pop_analyzed_traits($c, $training_pop_id, $selection_pop_id);
1592 my $selection_pop_traits = $c->stash->{prediction_pop_analyzed_traits_ids};
1593 $ret->{selection_traits} = $selection_pop_traits;
1596 $ret = to_json($ret);
1598 $c->res->content_type('application/json');
1599 $c->res->body($ret);
1604 sub check_genotype_data_population :Path('/solgs/check/genotype/data/population/') Args(1) {
1605 my ($self, $c, $pop_id) = @_;
1607 $c->stash->{pop_id} = $pop_id;
1608 $self->check_population_has_genotype($c);
1610 my $ret->{has_genotype} = $c->stash->{population_has_genotype};
1611 $ret = to_json($ret);
1613 $c->res->content_type('application/json');
1614 $c->res->body($ret);
1619 sub check_phenotype_data_population :Path('/solgs/check/phenotype/data/population/') Args(1) {
1620 my ($self, $c, $pop_id) = @_;
1622 $c->stash->{pop_id} = $pop_id;
1623 $self->check_population_has_phenotype($c);
1625 my $ret->{has_phenotype} = $c->stash->{population_has_phenotype};
1626 $ret = to_json($ret);
1628 $c->res->content_type('application/json');
1629 $c->res->body($ret);
1634 sub check_population_exists :Path('/solgs/check/population/exists/') Args(0) {
1635 my ($self, $c) = @_;
1637 my $name = $c->req->param('name');
1639 my $rs = $c->model("solGS::solGS")->project_details_by_name($name);
1641 my $pop_id;
1642 while (my $row = $rs->next)
1644 $pop_id = $row->id;
1647 my $ret->{population_id} = $pop_id;
1648 $ret = to_json($ret);
1650 $c->res->content_type('application/json');
1651 $c->res->body($ret);
1656 sub check_training_population :Path('/solgs/check/training/population/') Args(1) {
1657 my ($self, $c, $pop_id) = @_;
1659 $c->stash->{pop_id} = $pop_id;
1661 $self->check_population_is_training_population($c);
1662 my $is_training_pop = $c->stash->{is_training_population};
1664 my $training_pop_data;
1665 if ($is_training_pop)
1667 my $pr_rs = $c->model('solGS::solGS')->project_details($pop_id);
1668 $self->projects_links($c, $pr_rs);
1669 $training_pop_data = $c->stash->{projects_pages};
1672 my $ret->{is_training_population} = $is_training_pop;
1673 $ret->{training_pop_data} = $training_pop_data;
1674 $ret = to_json($ret);
1676 $c->res->content_type('application/json');
1677 $c->res->body($ret);
1682 sub check_population_is_training_population {
1683 my ($self, $c) = @_;
1685 my $pr_id = $c->stash->{pop_id};
1686 my $is_gs = $c->model("solGS::solGS")->get_project_type($pr_id);
1688 my $has_phenotype;
1689 my $has_genotype;
1691 if ($is_gs !~ /genomic selection/)
1693 $self->check_population_has_phenotype($c);
1694 $has_phenotype = $c->stash->{population_has_phenotype};
1696 if ($has_phenotype)
1698 $self->check_population_has_genotype($c);
1699 $has_genotype = $c->stash->{population_has_genotype};
1703 if ($is_gs || ($has_phenotype && $has_genotype))
1705 $c->stash->{is_training_population} = 1;
1711 sub check_population_has_phenotype {
1712 my ($self, $c) = @_;
1714 my $pr_id = $c->stash->{pop_id};
1715 my $is_gs = $c->model("solGS::solGS")->get_project_type($pr_id);
1716 my $has_phenotype = 1 if $is_gs;
1718 if ($is_gs !~ /genomic selection/)
1720 $c->controller('solGS::Files')->phenotype_file_name($c, $pr_id);
1721 my $pheno_file = $c->stash->{genotype_file_name};
1723 if (!-s $pheno_file)
1725 $has_phenotype = $c->model("solGS::solGS")->has_phenotype($pr_id);
1727 else
1729 $has_phenotype = 1;
1733 $c->stash->{population_has_phenotype} = $has_phenotype;
1738 sub check_population_has_genotype {
1739 my ($self, $c) = @_;
1741 my $pop_id = $c->stash->{pop_id};
1743 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
1744 my $geno_file = $c->stash->{genotype_file_name};
1746 my $has_genotype;
1748 if (-s $geno_file)
1750 $has_genotype = 1;
1752 else
1754 $c->controller('solGS::Files')->first_stock_genotype_file($c, $pop_id);
1755 my $first_stock_file = $c->stash->{first_stock_genotype_file};
1757 $has_genotype = 1 if -s $first_stock_file;
1760 if (!$has_genotype)
1762 $has_genotype = $c->model('solGS::solGS')->has_genotype($pop_id);
1765 $c->stash->{population_has_genotype} = $has_genotype;
1769 sub check_selection_population_relevance :Path('/solgs/check/selection/population/relevance') Args() {
1770 my ($self, $c) = @_;
1772 #my $data_set_type = $c->req->param('data_set_type');
1773 my $training_pop_id = $c->req->param('training_pop_id');
1774 my $selection_pop_name = $c->req->param('selection_pop_name');
1775 my $trait_id = $c->req->param('trait_id');
1777 my $referer = $c->req->referer;
1779 if ($referer =~ /combined\//)
1781 $c->stash->{data_set_type} = 'combined populations';
1782 $c->stash->{combo_pops_id} = $training_pop_id;
1785 my $pr_rs = $c->model("solGS::solGS")->project_details_by_exact_name($selection_pop_name);
1787 my $selection_pop_id;
1788 while (my $row = $pr_rs->next) {
1789 $selection_pop_id = $row->project_id;
1792 my $ret = {};
1794 if ($selection_pop_id !~ /$training_pop_id/)
1796 my $has_genotype;
1797 if ($selection_pop_id)
1799 $c->stash->{pop_id} = $selection_pop_id;
1800 $self->check_population_has_genotype($c);
1801 $has_genotype = $c->stash->{population_has_genotype};
1804 my $similarity;
1805 if ($has_genotype)
1807 $self->first_stock_genotype_data($c, $selection_pop_id);
1809 $c->controller('solGS::Files')->first_stock_genotype_file($c, $selection_pop_id);
1810 my $selection_geno_file = $c->stash->{first_stock_genotype_file};
1812 $c->controller('solGS::Files')->genotype_file_name($c, $training_pop_id);
1813 my $training_geno_file = $c->stash->{genotype_file_name};
1815 $similarity = $self->compare_marker_set_similarity([$selection_geno_file, $training_geno_file]);
1818 my $selection_pop_data;
1819 unless ($similarity < 0.5 )
1821 $c->stash->{training_pop_id} = $training_pop_id;
1822 $self->format_selection_pops($c, [$selection_pop_id]);
1823 $selection_pop_data = $c->stash->{selection_pops_list};
1824 $self->save_selection_pops($c, [$selection_pop_id]);
1827 $ret->{selection_pop_data} = $selection_pop_data;
1828 $ret->{similarity} = $similarity;
1829 $ret->{has_genotype} = $has_genotype;
1830 $ret->{selection_pop_id} = $selection_pop_id;
1832 else
1834 $ret->{selection_pop_id} = $selection_pop_id;
1837 $ret = to_json($ret);
1839 $c->res->content_type('application/json');
1840 $c->res->body($ret);
1845 sub save_selection_pops {
1846 my ($self, $c, $selection_pop_id) = @_;
1848 my $training_pop_id = $c->stash->{training_pop_id};
1850 $c->controller('solGS::Files')->list_of_prediction_pops_file($c, $training_pop_id);
1851 my $selection_pops_file = $c->stash->{list_of_prediction_pops_file};
1853 my @existing_pops_ids = read_file($selection_pops_file);
1855 my @uniq_ids = unique(@existing_pops_ids, @$selection_pop_id);
1856 my $formatted_ids = join("\n", @uniq_ids);
1858 write_file($selection_pops_file, $formatted_ids);
1863 sub search_selection_pops :Path('/solgs/search/selection/populations/') {
1864 my ($self, $c, $tr_pop_id) = @_;
1866 $c->stash->{training_pop_id} = $tr_pop_id;
1868 $self->search_all_relevant_selection_pops($c, $tr_pop_id);
1869 my $selection_pops_list = $c->stash->{all_relevant_selection_pops};
1871 my $ret->{selection_pops_list} = 0;
1872 if ($selection_pops_list)
1874 $ret->{data} = $selection_pops_list;
1877 $ret = to_json($ret);
1879 $c->res->content_type('application/json');
1880 $c->res->body($ret);
1885 sub list_of_prediction_pops {
1886 my ($self, $c, $training_pop_id) = @_;
1888 $c->controller('solGS::Files')->list_of_prediction_pops_file($c, $training_pop_id);
1889 my $pred_pops_file = $c->stash->{list_of_prediction_pops_file};
1891 my @pred_pops_ids = read_file($pred_pops_file);
1892 grep(s/\s//g, @pred_pops_ids);
1894 $c->stash->{selection_pops_ids} = \@pred_pops_ids;
1896 $self->format_selection_pops($c, \@pred_pops_ids);
1897 $c->stash->{list_of_prediction_pops} = $c->stash->{selection_pops_list};
1902 sub search_all_relevant_selection_pops {
1903 my ($self, $c, $training_pop_id) = @_;
1905 my @pred_pops_ids = @{$c->model('solGS::solGS')->prediction_pops($training_pop_id)};
1907 $self->save_selection_pops($c, \@pred_pops_ids);
1909 $self->format_selection_pops($c, \@pred_pops_ids);
1911 $c->stash->{all_relevant_selection_pops} = $c->stash->{selection_pops_list};
1916 sub format_selection_pops {
1917 my ($self, $c, $pred_pops_ids) = @_;
1919 my $training_pop_id = $c->stash->{training_pop_id};
1921 my @pred_pops_ids = @{$pred_pops_ids};
1922 my @data;
1924 if (@pred_pops_ids) {
1926 foreach my $prediction_pop_id (@pred_pops_ids)
1928 my $pred_pop_rs = $c->model('solGS::solGS')->project_details($prediction_pop_id);
1929 my $pred_pop_link;
1931 while (my $row = $pred_pop_rs->next)
1933 my $name = $row->name;
1934 my $desc = $row->description;
1936 # unless ($name =~ /test/ || $desc =~ /test/)
1938 my $id_pop_name->{id} = $prediction_pop_id;
1939 $id_pop_name->{name} = $name;
1940 $id_pop_name->{pop_type} = 'selection';
1941 $id_pop_name = to_json($id_pop_name);
1943 # $pred_pop_link = qq | <a href="/solgs/model/$training_pop_id/prediction/$prediction_pop_id"
1944 # onclick="solGS.waitPage(this.href); return false;"><input type="hidden" value=\'$id_pop_name\'>$name</data>
1945 # </a>
1946 # |;
1948 $pred_pop_link = qq | <data><input type="hidden" value=\'$id_pop_name\'>$name</data>|;
1951 my $pr_yr_rs = $c->model('solGS::solGS')->project_year($prediction_pop_id);
1952 my $project_yr;
1954 while ( my $yr_r = $pr_yr_rs->next )
1956 $project_yr = $yr_r->value;
1959 $self->download_prediction_urls($c, $training_pop_id, $prediction_pop_id);
1960 my $download_prediction = $c->stash->{download_prediction};
1962 push @data, [$pred_pop_link, $desc, $project_yr, $download_prediction];
1967 $c->stash->{selection_pops_list} = \@data;
1972 sub get_trait_details_of_trait_abbr {
1973 my ($self, $c) = @_;
1975 my $trait_abbr = $c->stash->{trait_abbr};
1977 # if (!$c->stash->{pop_id})
1978 # {
1979 # $c->stash->{pop_id} = $c->stash->{training_pop_id} || $c->stash->{combo_pops_id};
1982 my $trait_id;
1984 my $acronym_pairs = $self->get_acronym_pairs($c, $c->stash->{training_pop_id});
1986 if ($acronym_pairs)
1988 foreach my $r (@$acronym_pairs)
1990 if ($r->[0] eq $trait_abbr)
1992 my $trait_name = $r->[1];
1993 $trait_name =~ s/^\s+|\s+$//g;
1995 $trait_id = $c->model('solGS::solGS')->get_trait_id($trait_name);
1996 $self->get_trait_details($c, $trait_id);
2004 sub build_multiple_traits_models {
2005 my ($self, $c) = @_;
2007 my $pop_id = $c->stash->{pop_id} || $c->stash->{training_pop_id};
2008 my @selected_traits = @{$c->stash->{training_traits_ids}};
2009 my $trait_id = $selected_traits[0] if scalar(@selected_traits) == 1;
2011 my $traits;
2013 for (my $i = 0; $i <= $#selected_traits; $i++)
2015 my $tr = $c->model('solGS::solGS')->trait_name($selected_traits[$i]);
2016 my $abbr = $c->controller('solGS::Utils')->abbreviate_term($tr);
2017 $traits .= $abbr;
2018 $traits .= "\t" unless ($i == $#selected_traits);
2022 my $name = "selected_traits_pop_${pop_id}";
2023 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
2024 my $file = $c->controller('solGS::Files')->create_tempfile($temp_dir, $name);
2026 write_file($file, $traits);
2027 $c->stash->{selected_traits_file} = $file;
2029 $name = "trait_info_${trait_id}_pop_${pop_id}";
2030 my $file2 = $c->controller('solGS::Files')->create_tempfile($temp_dir, $name);
2032 $c->stash->{trait_file} = $file2;
2034 $self->get_gs_modeling_jobs_args_file($c);
2035 $c->stash->{dependent_jobs} = $c->stash->{gs_modeling_jobs_args_file};
2036 $self->run_async($c);
2041 sub all_traits_output :Path('/solgs/traits/all/population') Args(3) {
2042 my ($self, $c, $training_pop_id, $tr_txt, $traits_selection_id) = @_;
2044 my @traits_ids;
2046 if ($traits_selection_id =~ /^\d+$/)
2048 $c->controller('solGS::TraitsGebvs')->get_traits_selection_list($c, $traits_selection_id);
2049 @traits_ids = @{$c->stash->{traits_selection_list}} if $c->stash->{traits_selection_list};
2052 if ($training_pop_id =~ /list/)
2054 $c->stash->{list_id} = $training_pop_id =~ s/list_//r;
2057 $self->project_description($c, $training_pop_id);
2058 my $training_pop_name = $c->stash->{project_name};
2059 my $training_pop_desc = $c->stash->{project_desc};
2060 my $training_pop_page = qq | <a href="/solgs/population/$training_pop_id">$training_pop_name</a> |;
2062 my @select_analysed_traits;
2064 if(!@traits_ids)
2066 $c->stash->{message} = "Cached output for this page does not exist anymore.\n" .
2067 " Please go to $training_pop_page and run the analysis.";
2069 $c->stash->{template} = "/generic_message.mas";
2071 else
2073 my @traits_pages;
2074 if (scalar(@traits_ids) == 1)
2076 my $trait_id = $traits_ids[0];
2077 $c->res->redirect("/solgs/trait/$trait_id/population/$training_pop_id");
2078 $c->detach();
2080 else
2082 foreach my $trait_id (@traits_ids)
2084 $c->stash->{trait_id} = $trait_id;
2085 $c->stash->{model_id} = $training_pop_id;
2086 $self->create_model_summary($c);
2087 my $model_summary = $c->stash->{model_summary};
2089 push @traits_pages, $model_summary;
2093 $c->stash->{training_traits_ids} = \@traits_ids;
2094 $c->controller('solGS::solGS')->analyzed_traits($c);
2095 my $analyzed_traits = $c->stash->{analyzed_traits};
2097 $c->stash->{trait_pages} = \@traits_pages;
2099 my @training_pop_data = ([$training_pop_page, $training_pop_desc, \@traits_pages]);
2101 $c->stash->{model_data} = \@training_pop_data;
2102 $c->stash->{pop_id} = $training_pop_id;
2103 $c->controller('solGS::solGS')->get_acronym_pairs($c, $training_pop_id);
2105 $c->stash->{template} = '/solgs/population/multiple_traits_output.mas';
2111 sub create_model_summary {
2112 my ($self, $c) = @_;
2114 my $trait_id = $c->stash->{trait_id};
2115 my $model_id = $c->stash->{model_id};
2117 $c->controller("solGS::solGS")->get_trait_details($c, $trait_id);
2118 my $tr_abbr = $c->stash->{trait_abbr};
2120 my $path = $c->req->path;
2121 my $trait_page;
2123 if ($path =~ /solgs\/traits\/all\/population\//)
2125 $trait_page = qq | <a href="/solgs/trait/$trait_id/population/$model_id" onclick="solGS.waitPage()">$tr_abbr</a>|;
2127 elsif ($path =~ /solgs\/models\/combined\/trials\//)
2129 $trait_page = qq | <a href="/solgs/model/combined/populations/$model_id/trait/$trait_id" onclick="solGS.waitPage()">$tr_abbr</a>|;
2132 $c->controller("solGS::solGS")->get_model_accuracy_value($c, $model_id, $tr_abbr);
2133 my $accuracy_value = $c->stash->{accuracy_value};
2135 $c->controller("solGS::Heritability")->get_heritability($c);
2136 my $heritability = $c->stash->{heritability};
2138 my $model_summary = [$trait_page, $accuracy_value, $heritability];
2140 $c->stash->{model_summary} = $model_summary;
2146 sub traits_with_valid_models {
2147 my ($self, $c) = @_;
2149 my $pop_id = $c->stash->{pop_id} || $c->stash->{training_pop_id};
2151 $self->analyzed_traits($c);
2153 my @analyzed_traits = @{$c->stash->{analyzed_traits}};
2154 my @filtered_analyzed_traits;
2155 my @valid_traits_ids;
2157 foreach my $analyzed_trait (@analyzed_traits)
2159 $self->get_model_accuracy_value($c, $pop_id, $analyzed_trait);
2160 my $av = $c->stash->{accuracy_value};
2161 if ($av && $av =~ m/\d+/ && $av > 0)
2163 push @filtered_analyzed_traits, $analyzed_trait;
2166 $c->stash->{trait_abbr} = $analyzed_trait;
2167 $self->get_trait_details_of_trait_abbr($c);
2168 push @valid_traits_ids, $c->stash->{trait_id};
2172 @filtered_analyzed_traits = uniq(@filtered_analyzed_traits);
2173 @valid_traits_ids = uniq(@valid_traits_ids);
2175 $c->stash->{traits_with_valid_models} = \@filtered_analyzed_traits;
2176 $c->stash->{traits_ids_with_valid_models} = \@valid_traits_ids;
2181 sub get_model_accuracy_value {
2182 my ($self, $c, $model_id, $trait_abbr) = @_;
2184 my $dir = $c->stash->{solgs_cache_dir};
2185 opendir my $dh, $dir or die "can't open $dir: $!\n";
2187 my ($validation_file) = grep { /cross_validation_${trait_abbr}_${model_id}/ && -f "$dir/$_" }
2188 readdir($dh);
2190 closedir $dh;
2192 $validation_file = catfile($dir, $validation_file);
2193 my ($row) = grep {/Average/} read_file($validation_file);
2194 my ($text, $accuracy_value) = split(/\t/, $row);
2196 $accuracy_value =~ s/\s+//g;
2197 $c->stash->{accuracy_value} = $accuracy_value;
2202 sub get_project_owners {
2203 my ($self, $c, $pr_id) = @_;
2205 my $owners = $c->model("solGS::solGS")->get_stock_owners($pr_id);
2206 my $owners_names;
2208 if ($owners)
2210 for (my $i=0; $i < scalar(@$owners); $i++)
2212 my $owner_name = $owners->[$i]->{'first_name'} . "\t" . $owners->[$i]->{'last_name'} if $owners->[$i];
2214 unless (!$owner_name)
2216 $owners_names .= $owners_names ? ', ' . $owner_name : $owner_name;
2221 $c->stash->{project_owners} = $owners_names;
2225 sub compare_marker_set_similarity {
2226 my ($self, $marker_file_pair) = @_;
2228 my $file_1 = $marker_file_pair->[0];
2229 my $file_2 = $marker_file_pair->[1];
2231 my $first_markers = (read_file($marker_file_pair->[0]))[0];
2232 my $sec_markers = (read_file($marker_file_pair->[1]))[0];
2234 my @first_geno_markers = split(/\t/, $first_markers);
2235 my @sec_geno_markers = split(/\t/, $sec_markers);
2237 if ( @first_geno_markers && @sec_geno_markers)
2239 my $common_markers = scalar(intersect(@first_geno_markers, @sec_geno_markers));
2240 my $similarity = $common_markers / scalar(@first_geno_markers);
2242 return $similarity;
2244 else
2246 return 0;
2252 sub compare_genotyping_platforms {
2253 my ($self, $c, $g_files) = @_;
2255 my $combinations = combinations($g_files, 2);
2256 my $combo_cnt = combinations($g_files, 2);
2258 my $not_matching_pops;
2259 my $cnt = 0;
2260 my $cnt_pairs = 0;
2262 while ($combo_cnt->next)
2264 $cnt_pairs++;
2267 while (my $pair = $combinations->next)
2269 $cnt++;
2270 my $similarity = $self->compare_marker_set_similarity($pair);
2272 unless ($similarity > 0.5 )
2274 no warnings 'uninitialized';
2275 my $pop_id_1 = fileparse($pair->[0]);
2276 my $pop_id_2 = fileparse($pair->[1]);
2278 map { s/genotype_data_|\.txt//g } $pop_id_1, $pop_id_2;
2280 my $list_type_pop = $c->stash->{list_prediction};
2282 unless ($list_type_pop)
2284 my @pop_names;
2285 foreach ($pop_id_1, $pop_id_2)
2287 my $pr_rs = $c->model('solGS::solGS')->project_details($_);
2289 while (my $row = $pr_rs->next)
2291 push @pop_names, $row->name;
2295 $not_matching_pops .= '[ ' . $pop_names[0]. ' and ' . $pop_names[1] . ' ]';
2296 $not_matching_pops .= ', ' if $cnt != $cnt_pairs;
2298 # else
2299 # {
2300 # $not_matching_pops = 'not_matching';
2305 $c->stash->{pops_with_no_genotype_match} = $not_matching_pops;
2310 sub submit_cluster_compare_trials_markers {
2311 my ($self, $c, $geno_files) = @_;
2313 $c->stash->{r_temp_file} = 'compare-trials-markers';
2314 $self->create_cluster_accesible_tmp_files($c);
2315 my $out_temp_file = $c->stash->{out_file_temp};
2316 my $err_temp_file = $c->stash->{err_file_temp};
2318 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
2319 my $background_job = $c->stash->{background_job};
2321 my $status;
2323 try
2325 my $compare_trials_job = CXGN::Tools::Run->run_cluster_perl({
2327 method => ["SGN::Controller::solGS::solGS" => "compare_genotyping_platforms"],
2328 args => ['SGN::Context', $geno_files],
2329 load_packages => ['SGN::Controller::solGS::solGS', 'SGN::Context'],
2330 run_opts => {
2331 out_file => $out_temp_file,
2332 err_file => $err_temp_file,
2333 working_dir => $temp_dir,
2334 max_cluster_jobs => 1_000_000_000,
2339 $c->stash->{r_job_tempdir} = $compare_trials_job->tempdir();
2341 $c->stash->{r_job_id} = $compare_trials_job->job_id();
2342 $c->stash->{cluster_job} = $compare_trials_job;
2344 unless ($background_job)
2346 $compare_trials_job->wait();
2350 catch
2352 $status = $_;
2353 $status =~ s/\n at .+//s;
2359 sub phenotype_graph :Path('/solgs/phenotype/graph') Args(0) {
2360 my ($self, $c) = @_;
2362 my $pop_id = $c->req->param('pop_id');
2363 my $trait_id = $c->req->param('trait_id');
2364 my $combo_pops_id = $c->req->param('combo_pops_id');
2366 $self->get_trait_details($c, $trait_id);
2368 $c->stash->{pop_id} = $pop_id;
2369 $c->stash->{combo_pops_id} = $combo_pops_id;
2371 $c->stash->{data_set_type} = 'combined populations' if $combo_pops_id;
2373 $c->controller("solGS::Files")->trait_phenodata_file($c);
2375 my $trait_pheno_file = $c->{stash}->{trait_phenodata_file};
2376 my $trait_data = $c->controller("solGS::Utils")->read_file_data($trait_pheno_file);
2378 my $ret->{status} = 'failed';
2380 if (@$trait_data)
2382 $ret->{status} = 'success';
2383 $ret->{trait_data} = $trait_data;
2386 $ret = to_json($ret);
2388 $c->res->content_type('application/json');
2389 $c->res->body($ret);
2394 #generates descriptive stat for a trait phenotype data
2395 sub trait_phenotype_stat {
2396 my ($self, $c) = @_;
2398 $c->controller("solGS::Files")->trait_phenodata_file($c);
2400 my $trait_pheno_file = $c->{stash}->{trait_phenodata_file};
2402 my $trait_data = $c->controller("solGS::Utils")->read_file_data($trait_pheno_file);
2404 my @desc_stat;
2405 my $background_job = $c->stash->{background_job};
2407 if ($trait_data && !$background_job)
2409 my @pheno_data;
2410 foreach (@$trait_data)
2412 unless (!$_->[0])
2414 my $d = $_->[1];
2415 chomp($d);
2417 if ($d =~ /\d+/)
2419 push @pheno_data, $d;
2424 my $stat = Statistics::Descriptive::Full->new();
2425 $stat->add_data(@pheno_data);
2427 my $min = $stat->min;
2428 my $max = $stat->max;
2429 my $mean = $stat->mean;
2430 my $med = $stat->median;
2431 my $std = $stat->standard_deviation;
2432 my $cnt = scalar(@$trait_data);
2433 my $cv = ($std / $mean) * 100;
2434 my $na = scalar(@$trait_data) - scalar(@pheno_data);
2436 if ($na == 0) { $na = '--'; }
2438 my $round = Math::Round::Var->new(0.01);
2439 $std = $round->round($std);
2440 $mean = $round->round($mean);
2441 $cv = $round->round($cv);
2442 $cv = $cv . '%';
2444 @desc_stat = ( [ 'Total no. of genotypes', $cnt ],
2445 [ 'Genotypes missing data', $na ],
2446 [ 'Minimum', $min ],
2447 [ 'Maximum', $max ],
2448 [ 'Arithmetic mean', $mean ],
2449 [ 'Median', $med ],
2450 [ 'Standard deviation', $std ],
2451 [ 'Coefficient of variation', $cv ]
2456 else
2458 @desc_stat = ( [ 'Total no. of genotypes', 'None' ],
2459 [ 'Genotypes missing data', 'None' ],
2460 [ 'Minimum', 'None' ],
2461 [ 'Maximum', 'None' ],
2462 [ 'Arithmetic mean', 'None' ],
2463 [ 'Median', 'None'],
2464 [ 'Standard deviation', 'None' ],
2465 [ 'Coefficient of variation', 'None' ]
2470 $c->stash->{descriptive_stat} = \@desc_stat;
2472 #sends an array of trait gebv data to an ajax request
2473 #with a population id and trait id parameters
2474 sub gebv_graph :Path('/solgs/trait/gebv/graph') Args(0) {
2475 my ($self, $c) = @_;
2477 my $pop_id = $c->req->param('pop_id');
2478 my $trait_id = $c->req->param('trait_id');
2479 my $prediction_pop_id = $c->req->param('selection_pop_id');
2480 my $combo_pops_id = $c->req->param('combo_pops_id');
2482 if ($combo_pops_id)
2484 $c->controller('solGS::combinedTrials')->get_combined_pops_list($c, $combo_pops_id);
2485 $c->stash->{data_set_type} = 'combined populations';
2486 $pop_id = $combo_pops_id;
2489 $c->stash->{pop_id} = $pop_id;
2490 $c->stash->{combo_pops_id} = $combo_pops_id;
2491 $c->stash->{prediction_pop_id} = $prediction_pop_id;
2493 $self->get_trait_details($c, $trait_id);
2495 my $page = $c->req->referer();
2496 my $gebv_file;
2498 if ($page =~ /solgs\/selection\//)
2500 my $identifier = $pop_id . '_' . $prediction_pop_id;
2501 $c->controller('solGS::Files')->rrblup_selection_gebvs_file($c, $identifier, $trait_id);
2502 $gebv_file = $c->stash->{rrblup_selection_gebvs_file};
2504 else
2506 $c->controller('solGS::Files')->rrblup_training_gebvs_file($c);
2507 $gebv_file = $c->stash->{rrblup_training_gebvs_file};
2510 my $gebv_data = $c->controller("solGS::Utils")->read_file_data($gebv_file);
2512 my $ret->{status} = 'failed';
2514 if (@$gebv_data)
2516 $ret->{status} = 'success';
2517 $ret->{gebv_data} = $gebv_data;
2520 $ret = to_json($ret);
2522 $c->res->content_type('application/json');
2523 $c->res->body($ret);
2528 sub get_single_trial_traits {
2529 my ($self, $c) = @_;
2531 my $pop_id = $c->stash->{pop_id};
2533 $c->controller('solGS::Files')->traits_list_file($c);
2534 my $traits_file = $c->stash->{traits_list_file};
2536 if (!-s $traits_file)
2538 my $traits = $c->model('solGS::solGS')->trial_traits($pop_id);
2540 $traits = join("\t", @$traits);
2541 write_file($traits_file, $traits);
2547 sub get_all_traits {
2548 my ($self, $c) = @_;
2550 my $pop_id = $c->stash->{pop_id};
2552 $c->controller('solGS::Files')->traits_list_file($c);
2553 my $traits_file = $c->stash->{traits_list_file};
2555 if (!-s $traits_file)
2557 my $page = $c->req->path;
2559 if ($page =~ /solgs\/population\// && $pop_id !~ /\w+/)
2561 $self->get_single_trial_traits($c);
2565 my $traits = read_file($traits_file);
2567 $c->controller('solGS::Files')->traits_acronym_file($c, $pop_id);
2568 my $acronym_file = $c->stash->{traits_acronym_file};
2570 unless (-s $acronym_file)
2572 my @filtered_traits = split(/\t/, $traits);
2573 my $acronymized_traits = $c->controller('solGS::Utils')->acronymize_traits(\@filtered_traits);
2574 my $acronym_table = $acronymized_traits->{acronym_table};
2576 $self->traits_acronym_table($c, $acronym_table);
2579 $self->create_trait_data($c);
2583 sub create_trait_data {
2584 my ($self, $c) = @_;
2586 my $acronym_pairs = $self->get_acronym_pairs($c);
2588 if (@$acronym_pairs)
2590 my $table = 'trait_id' . "\t" . 'trait_name' . "\t" . 'acronym' . "\n";
2591 foreach (@$acronym_pairs)
2593 my $trait_name = $_->[1];
2594 $trait_name =~ s/\n//g;
2596 my $trait_id = $c->model('solGS::solGS')->get_trait_id($trait_name);
2598 if ($trait_id)
2600 $table .= $trait_id . "\t" . $trait_name . "\t" . $_->[0] . "\n";
2604 $c->controller('solGS::Files')->all_traits_file($c);
2605 my $traits_file = $c->stash->{all_traits_file};
2606 write_file($traits_file, $table);
2611 sub get_acronym_pairs {
2612 my ($self, $c, $pop_id) = @_;
2614 my $pop_id = $c->stash->{training_pop_id} if !$pop_id;
2615 #$pop_id = $c->stash->{combo_pops_id} if !$pop_id;
2617 my $dir = $c->stash->{solgs_cache_dir};
2618 opendir my $dh, $dir
2619 or die "can't open $dir: $!\n";
2621 no warnings 'uninitialized';
2623 my ($file) = grep(/traits_acronym_pop_${pop_id}/, readdir($dh));
2624 $dh->close;
2626 my $acronyms_file = catfile($dir, $file);
2628 my @acronym_pairs;
2629 if (-f $acronyms_file)
2631 @acronym_pairs = map { [ split(/\t/) ] } read_file($acronyms_file);
2632 shift(@acronym_pairs); # remove header;
2635 @acronym_pairs = sort {uc $a->[0] cmp uc $b->[0] } @acronym_pairs;
2637 $c->stash->{acronym} = \@acronym_pairs;
2639 return \@acronym_pairs;
2644 sub traits_acronym_table {
2645 my ($self, $c, $acronym_table) = @_;
2647 my $pop_id = $c->stash->{pop_id};
2649 if (keys %$acronym_table)
2651 my $table = 'Acronym' . "\t" . 'Trait name' . "\n";
2653 foreach (keys %$acronym_table)
2655 $table .= $_ . "\t" . $acronym_table->{$_} . "\n";
2658 $c->controller('solGS::Files')->traits_acronym_file($c, $pop_id);
2659 my $acronym_file = $c->stash->{traits_acronym_file};
2661 write_file($acronym_file, $table);
2667 sub analyzed_traits {
2668 my ($self, $c) = @_;
2670 my $training_pop_id = $c->stash->{model_id} || $c->stash->{training_pop_id};
2671 my @selected_analyzed_traits = @{$c->stash->{training_traits_ids}} if $c->stash->{training_traits_ids};
2673 my $dir = $c->stash->{solgs_cache_dir};
2674 opendir my $dh, $dir or die "can't open $dir: $!\n";
2676 my @all_files = grep { /rrblup_training_gebvs_[a-zA-Z0-9]/ && -f "$dir/$_" }
2677 readdir($dh);
2679 closedir $dh;
2681 my @traits_files = map { catfile($dir, $_)}
2682 grep {/($training_pop_id)/}
2683 @all_files;
2685 my @traits;
2686 my @traits_ids;
2687 my @si_traits;
2688 my @valid_traits_files;
2689 my @analyzed_traits_files;
2691 foreach my $trait_file (@traits_files)
2693 if (-s $trait_file)
2695 my $trait = basename($trait_file);
2696 $trait =~ s/rrblup_training_gebvs_//;
2697 $trait =~ s/$training_pop_id|_|combined_pops//g;
2698 $trait =~ s/$dir|\///g;
2699 $trait =~ s/\.txt//;
2701 my $trait_id;
2703 my $acronym_pairs = $self->get_acronym_pairs($c, $training_pop_id);
2705 if ($acronym_pairs)
2707 foreach my $r (@$acronym_pairs)
2709 if ($r->[0] eq $trait)
2711 my $trait_name = $r->[1];
2712 $trait_name =~ s/\n//g;
2713 $trait_id = $c->model('solGS::solGS')->get_trait_id($trait_name);
2715 if (@selected_analyzed_traits)
2717 if (grep($trait_id == $_, @selected_analyzed_traits))
2719 push @traits_ids, $trait_id;
2722 else
2724 push @traits_ids, $trait_id;
2730 $self->get_model_accuracy_value($c, $training_pop_id, $trait);
2731 my $av = $c->stash->{accuracy_value};
2733 if ($av && $av =~ m/\d+/ && $av > 0)
2735 if (@selected_analyzed_traits)
2737 if (grep($trait_id == $_, @selected_analyzed_traits))
2739 push @si_traits, $trait;
2740 push @valid_traits_files, $trait_file;
2743 else
2745 push @si_traits, $trait;
2746 push @valid_traits_files, $trait_file;
2750 if (@selected_analyzed_traits) {
2751 if (grep($trait_id == $_, @selected_analyzed_traits))
2753 push @traits, $trait;
2754 push @analyzed_traits_files, $trait_file;
2757 else
2759 push @traits, $trait;
2760 push @analyzed_traits_files, $trait_file;
2765 $c->stash->{analyzed_traits} = \@traits;
2766 $c->stash->{analyzed_traits_ids} = \@traits_ids;
2767 $c->stash->{analyzed_traits_files} = \@analyzed_traits_files;
2768 $c->stash->{selection_index_traits} = \@si_traits;
2769 $c->stash->{analyzed_valid_traits_files} = \@valid_traits_files;
2773 sub all_gs_traits_list {
2774 my ($self, $c) = @_;
2776 $self->trial_compatibility_file($c);
2777 my $file = $c->stash->{trial_compatibility_file};
2779 my $traits;
2780 my $mv_name = 'all_gs_traits';
2782 my $matview = $c->model('solGS::solGS')->check_matview_exists($mv_name);
2784 if (!$matview)
2786 $c->model('solGS::solGS')->materialized_view_all_gs_traits();
2787 $c->model('solGS::solGS')->insert_matview_public($mv_name);
2789 else
2791 if (!-s $file)
2793 $c->model('solGS::solGS')->refresh_materialized_view_all_gs_traits();
2794 $c->model('solGS::solGS')->update_matview_public($mv_name);
2800 $traits = $c->model('solGS::solGS')->all_gs_traits();
2802 catch
2805 if ($_ =~ /materialized view \"all_gs_traits\" has not been populated/)
2809 $c->model('solGS::solGS')->refresh_materialized_view_all_gs_traits();
2810 $c->model('solGS::solGS')->update_matview_public($mv_name);
2811 $traits = $c->model('solGS::solGS')->all_gs_traits();
2816 $c->stash->{all_gs_traits} = $traits;
2821 sub gs_traits_index {
2822 my ($self, $c) = @_;
2824 $self->all_gs_traits_list($c);
2825 my $all_traits = $c->stash->{all_gs_traits};
2826 my @all_traits = sort{$a cmp $b} @$all_traits;
2828 my @indices = ('A'..'Z');
2829 my %traits_hash;
2830 my @valid_indices;
2832 foreach my $index (@indices)
2834 my @index_traits;
2835 foreach my $trait (@all_traits)
2837 if ($trait =~ /^$index/i)
2839 push @index_traits, $trait;
2842 if (@index_traits)
2844 $traits_hash{$index}=[ @index_traits ];
2848 foreach my $k ( keys(%traits_hash))
2850 push @valid_indices, $k;
2853 @valid_indices = sort( @valid_indices );
2855 my $trait_index;
2856 foreach my $v_i (@valid_indices)
2858 $trait_index .= qq | <a href=/solgs/traits/$v_i>$v_i</a> |;
2859 unless ($v_i eq $valid_indices[-1])
2861 $trait_index .= " | ";
2865 $c->stash->{gs_traits_index} = $trait_index;
2870 sub traits_starting_with {
2871 my ($self, $c, $index) = @_;
2873 $self->all_gs_traits_list($c);
2874 my $all_traits = $c->stash->{all_gs_traits};
2876 my $trait_gr = [
2877 sort { $a cmp $b }
2878 grep { /^$index/i }
2879 uniq @$all_traits
2882 $c->stash->{trait_subgroup} = $trait_gr;
2886 sub hyperlink_traits {
2887 my ($self, $c, $traits) = @_;
2889 if (ref($traits) eq 'ARRAY')
2891 my @traits_urls;
2892 foreach my $tr (@$traits)
2894 push @traits_urls, [ qq | <a href="/solgs/search/result/traits/$tr">$tr</a> | ];
2897 $c->stash->{traits_urls} = \@traits_urls;
2899 else
2901 $c->stash->{traits_urls} = qq | <a href="/solgs/search/result/traits/$traits">$traits</a> |;
2906 sub gs_traits : Path('/solgs/traits') Args(1) {
2907 my ($self, $c, $index) = @_;
2909 my @traits_list;
2911 if ($index =~ /^\w{1}$/)
2913 $self->traits_starting_with($c, $index);
2914 my $traits_gr = $c->stash->{trait_subgroup};
2916 foreach my $trait (@$traits_gr)
2918 $self->hyperlink_traits($c, $trait);
2919 my $trait_url = $c->stash->{traits_urls};
2921 $self->get_trait_details($c, $trait);
2922 push @traits_list, [$trait_url, $c->stash->{trait_def}];
2925 $c->stash( template => $c->controller('solGS::Files')->template('/search/traits/list.mas'),
2926 index => $index,
2927 traits_list => \@traits_list
2930 else
2932 $c->forward('search');
2937 sub get_cluster_phenotype_query_job_args {
2938 my ($self, $c, $trials) = @_;
2940 my @queries;
2942 $c->controller('solGS::combinedTrials')->multi_pops_pheno_files($c, $trials);
2943 $c->stash->{phenotype_files_list} = $c->stash->{multi_pops_pheno_files};
2945 foreach my $trial_id (@$trials)
2947 $c->controller('solGS::Files')->phenotype_file_name($c, $trial_id);
2949 if (!-s $c->stash->{phenotype_file_name})
2951 my $args = $self->phenotype_trial_query_args($c, $trial_id);
2953 $c->stash->{r_temp_file} = "phenotype-data-query-${trial_id}";
2954 $self->create_cluster_accesible_tmp_files($c);
2955 my $out_temp_file = $c->stash->{out_file_temp};
2956 my $err_temp_file = $c->stash->{err_file_temp};
2958 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
2959 my $background_job = $c->stash->{background_job};
2961 my $args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "pheno-data-args_file-${trial_id}");
2963 nstore $args, $args_file
2964 or croak "data query script: $! serializing phenotype data query details to $args_file ";
2966 my $cmd = 'mx-run solGS::queryJobs '
2967 . ' --data_type phenotype '
2968 . ' --population_type trial '
2969 . ' --args_file ' . $args_file;
2972 my $config_args = {
2973 'temp_dir' => $temp_dir,
2974 'out_file' => $out_temp_file,
2975 'err_file' => $err_temp_file,
2976 'cluster_host' => 'localhost'
2979 my $config = $self->create_cluster_config($c, $config_args);
2981 my $job_args = {
2982 'cmd' => $cmd,
2983 'config' => $config,
2984 'background_job'=> $background_job,
2985 'temp_dir' => $temp_dir,
2988 push @queries, $job_args;
2992 $c->stash->{cluster_phenotype_query_job_args} = \@queries;
2997 sub get_pheno_data_query_job_args_file {
2998 my ($self, $c, $trials) = @_;
3000 $self->get_cluster_phenotype_query_job_args($c, $trials);
3001 my $pheno_query_args = $c->stash->{cluster_phenotype_query_job_args};
3003 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3004 my $pheno_query_args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'phenotype_data_query_args_file');
3006 nstore $pheno_query_args, $pheno_query_args_file
3007 or croak "pheno data query job : $! serializing selection pop data query details to $pheno_query_args_file";
3009 $c->stash->{pheno_data_query_job_args_file} = $pheno_query_args_file;
3013 sub get_geno_data_query_job_args_file {
3014 my ($self, $c, $trials) = @_;
3016 $self->get_cluster_genotype_query_job_args($c, $trials);
3017 my $geno_query_args = $c->stash->{cluster_genotype_query_job_args};
3019 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3020 my $geno_query_args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'genotype_data_query_args_file');
3022 nstore $geno_query_args, $geno_query_args_file
3023 or croak "geno data query job : $! serializing selection pop data query details to $geno_query_args_file";
3025 $c->stash->{geno_data_query_job_args_file} = $geno_query_args_file;
3029 sub submit_cluster_phenotype_query {
3030 my ($self, $c, $trials) = @_;
3032 $self->get_pheno_data_query_job_args_file($c, $trials);
3033 $c->stash->{dependent_jobs} = $c->stash->{pheno_data_query_job_args_file};
3034 $self->run_async($c);
3038 sub submit_cluster_genotype_query {
3039 my ($self, $c, $trials) = @_;
3041 $self->get_geno_data_query_job_args_file($c, $trials);
3042 $c->stash->{dependent_jobs} = $c->stash->{geno_data_query_job_args_file};
3043 $self->run_async($c);
3047 sub submit_cluster_training_pop_data_query {
3048 my ($self, $c, $trials) = @_;
3050 $self->get_training_pop_data_query_job_args_file($c, $trials);
3051 $c->stash->{dependent_jobs} = $c->stash->{training_pop_data_query_job_args_file};
3052 $self->run_async($c);
3056 sub training_pop_data_query_job_args {
3057 my ($self, $c, $trials) = @_;
3059 my @queries;
3061 foreach my $trial (@$trials)
3063 $c->controller('solGS::Files')->phenotype_file_name($c, $trial);
3065 if (!-s $c->stash->{phenotype_file_name})
3067 $self->get_cluster_phenotype_query_job_args($c, [$trial]);
3068 my $pheno_query = $c->stash->{cluster_phenotype_query_job_args};
3069 push @queries, @$pheno_query if $pheno_query;
3072 $c->controller('solGS::Files')->genotype_file_name($c, $trial);
3074 if (!-s $c->stash->{genotype_file_name})
3076 $self->get_cluster_genotype_query_job_args($c, [$trial]);
3077 my $geno_query = $c->stash->{cluster_genotype_query_job_args};
3078 push @queries, @$geno_query if $geno_query;
3083 $c->stash->{training_pop_data_query_job_args} = \@queries;
3087 sub get_training_pop_data_query_job_args_file {
3088 my ($self, $c, $trials) = @_;
3090 $self->training_pop_data_query_job_args($c, $trials);
3091 my $training_query_args = $c->stash->{training_pop_data_query_job_args};
3093 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3094 my $training_query_args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'training_pop_data_query_args');
3096 nstore $training_query_args, $training_query_args_file
3097 or croak "training pop data query job : $! serializing selection pop data query details to $training_query_args_file";
3099 $c->stash->{training_pop_data_query_job_args_file} = $training_query_args_file;
3103 sub get_cluster_genotype_query_job_args {
3104 my ($self, $c, $trials) = @_;
3106 my @queries;
3108 foreach my $trial_id (@$trials)
3110 my $geno_file;
3111 if ($c->stash->{check_data_exists})
3113 $c->controller('solGS::Files')->first_stock_genotype_file($c, $trial_id);
3114 $geno_file = $c->stash->{first_stock_genotype_file};
3116 else
3118 $c->controller('solGS::Files')->genotype_file_name($c, $trial_id);
3119 $geno_file = $c->stash->{genotype_file_name};
3122 if (!-s $geno_file)
3124 #my $pop_id = $args->{selection_pop_id} || $args->{selection_pop_id} || $args->{training_pop_id};
3125 my $args = $self->genotype_trial_query_args($c, $trial_id);
3127 $c->stash->{r_temp_file} = "genotype-data-query-${trial_id}";
3128 $self->create_cluster_accesible_tmp_files($c);
3129 my $out_temp_file = $c->stash->{out_file_temp};
3130 my $err_temp_file = $c->stash->{err_file_temp};
3132 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3133 my $background_job = $c->stash->{background_job};
3135 my $args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "geno-data-args_file-${trial_id}");
3137 nstore $args, $args_file
3138 or croak "data queryscript: $! serializing model details to $args_file ";
3140 my $check_data_exists = $c->stash->{check_data_exists} ? 1 : 0;
3142 my $cmd = 'mx-run solGS::queryJobs '
3143 . ' --data_type genotype '
3144 . ' --population_type trial '
3145 . ' --args_file ' . $args_file
3146 . ' --check_data_exists ' . $check_data_exists;
3148 my $config_args = {
3149 'temp_dir' => $temp_dir,
3150 'out_file' => $out_temp_file,
3151 'err_file' => $err_temp_file,
3152 'cluster_host' => 'localhost'
3155 my $config = $self->create_cluster_config($c, $config_args);
3157 my $job_args = {
3158 'cmd' => $cmd,
3159 'config' => $config,
3160 'background_job'=> $background_job,
3161 'temp_dir' => $temp_dir,
3164 push @queries, $job_args;
3168 $c->stash->{cluster_genotype_query_job_args} = \@queries;
3172 sub first_stock_genotype_data {
3173 my ($self, $c, $pr_id) = @_;
3175 $c->stash->{check_data_exists} = 1;
3176 $self->submit_cluster_genotype_query($c, [$pr_id]);
3180 sub phenotype_file {
3181 my ($self, $c, $pop_id) = @_;
3183 if (!$pop_id) {
3184 $pop_id = $c->stash->{pop_id}
3185 || $c->stash->{training_pop_id}
3186 || $c->stash->{trial_id};
3189 $c->stash->{pop_id} = $pop_id;
3190 die "Population id must be provided to get the phenotype data set." if !$pop_id;
3191 $pop_id =~ s/combined_//;
3193 if ($c->stash->{list_reference} || $pop_id =~ /list/) {
3194 if (!$c->user) {
3196 my $page = "/" . $c->req->path;
3198 $c->res->redirect("/solgs/login/message?page=$page");
3199 $c->detach;
3203 $c->controller('solGS::Files')->phenotype_file_name($c, $pop_id);
3204 my $pheno_file = $c->stash->{phenotype_file_name};
3206 no warnings 'uninitialized';
3208 unless ( -s $pheno_file)
3210 if ($pop_id !~ /list/)
3212 #my $args = $self->phenotype_trial_query_args($c);
3213 $self->submit_cluster_phenotype_query($c, [$pop_id]);
3217 $self->get_all_traits($c);
3219 $c->stash->{phenotype_file} = $pheno_file;
3224 sub genotype_trial_query_args {
3225 my ($self, $c, $pop_id) = @_;
3227 #$pop_id = $c->stash->{pop_id} if !$pop_id;
3228 #my $training_pop_id = $c->stash->{training_pop_id};
3229 #my $selection_pop_id = $c->stash->{selection_pop_id};
3231 # $pop_id = $training_pop_id || $selection_pop_id if !$pop_id;
3233 my $geno_file;
3234 my $check_data_exists = $c->stash->{check_data_exists};
3236 if ($c->stash->{check_data_exists})
3238 $c->controller('solGS::Files')->first_stock_genotype_file($c, $pop_id);
3239 $geno_file = $c->stash->{first_stock_genotype_file};
3241 else
3243 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
3244 $geno_file = $c->stash->{genotype_file_name};
3247 # my $referer = $c->req->referer;
3249 # my $tr_pop_id;
3250 # if ($referer =~ /models\/combined\/trials\/|solgs\/populations\/combined\//)
3252 # $training_pop_id = $c->stash->{combo_pops_id};
3253 # $tr_pop_id = "${training_pop_id}_combined";
3254 # }
3255 # else
3257 # $tr_pop_id = $training_pop_id ? $training_pop_id : $pop_id;
3260 #$c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
3261 #my $training_geno_file = $c->stash->{genotype_file_name};
3262 # print STDERR "\n NO check data exisits genotype_trial_query_args: --training geno file: $training_geno_file\n";
3263 # my $args = {
3264 # 'training_pop_id' => $pop_id,
3265 # 'selection_pop_id' => $selection_pop_id,
3266 # 'training_geno_file' => $training_geno_file,
3267 # 'genotype_file' => $geno_file,
3268 # 'cache_dir' => $c->stash->{solgs_cache_dir},
3269 # };
3271 my $args = {
3272 'trial_id' => $pop_id,
3273 'genotype_file' => $geno_file,
3274 'cache_dir' => $c->stash->{solgs_cache_dir},
3277 return $args;
3282 sub phenotype_trial_query_args {
3283 my ($self, $c, $pop_id) = @_;
3285 $pop_id = $c->stash->{pop_id} if !$pop_id;
3287 $c->controller('solGS::Files')->phenotype_file_name($c, $pop_id);
3288 my $pheno_file = $c->stash->{phenotype_file_name};
3290 $c->controller('solGS::Files')->phenotype_metadata_file($c);
3291 my $metadata_file = $c->stash->{phenotype_metadata_file};
3293 no warnings 'uninitialized';
3295 $c->controller('solGS::Files')->traits_list_file($c);
3296 my $traits_file = $c->stash->{traits_list_file};
3298 my $args = {
3299 'population_id' => $pop_id,
3300 'phenotype_file' => $pheno_file,
3301 'traits_list_file' => $traits_file,
3302 'metadata_file' => $metadata_file,
3305 return $args;
3309 sub format_phenotype_dataset {
3310 my ($self, $data_ref, $metadata, $traits_file) = @_;
3312 my $data = $$data_ref;
3313 my @rows = split (/\n/, $data);
3315 my $formatted_headers = $self->format_phenotype_dataset_headers($rows[0], $metadata, $traits_file);
3316 $rows[0] = $formatted_headers;
3318 my $formatted_dataset = $self->format_phenotype_dataset_rows(\@rows);
3320 return $formatted_dataset;
3324 sub format_phenotype_dataset_rows {
3325 my ($self, $data_rows) = @_;
3327 my $data = join("\n", @$data_rows);
3329 return $data;
3333 sub clean_traits {
3334 my ($self, $terms) = @_;
3336 $terms =~ s/(\|\w+:\d+)//g;
3337 $terms =~ s/\|/ /g;
3338 $terms =~ s/^\s+|\s+$//g;
3340 return $terms;
3344 sub format_phenotype_dataset_headers {
3345 my ($self, $all_headers, $meta_headers, $traits_file) = @_;
3347 $all_headers = $self->clean_traits($all_headers);
3349 my $traits = $all_headers;
3351 foreach my $mh (@$meta_headers) {
3352 $traits =~ s/($mh)//g;
3355 write_file($traits_file, $traits) if $traits_file;
3356 my @filtered_traits = split(/\t/, $traits);
3358 my $acronymized_traits = SGN::Controller::solGS::Utils->acronymize_traits(\@filtered_traits);
3359 my $acronym_table = $acronymized_traits->{acronym_table};
3361 my $formatted_headers;
3362 my @headers = split("\t", $all_headers);
3364 foreach my $hd (@headers)
3366 my $acronym;
3367 foreach my $acr (keys %$acronym_table)
3369 $acronym = $acr if $acronym_table->{$acr} =~ /$hd/;
3370 last if $acronym;
3373 $formatted_headers .= $acronym ? $acronym : $hd;
3374 $formatted_headers .= "\t" unless ($headers[-1] eq $hd);
3377 return $formatted_headers;
3382 sub genotype_file {
3383 my ($self, $c, $pop_id) = @_;
3385 $pop_id = $c->stash->{pop_id} if !$pop_id;
3387 my $training_pop_id = $c->stash->{training_pop_id};
3388 my $selection_pop_id = $c->stash->{selection_pop_id};
3390 $pop_id = $training_pop_id || $selection_pop_id if !$pop_id;
3391 die "Population id must be provided to get the genotype data set." if !$pop_id;
3393 if ($pop_id =~ /list/)
3395 if (!$c->user)
3397 my $path = "/" . $c->req->path;
3398 $c->res->redirect("/solgs/login/message?page=$path");
3399 $c->detach;
3403 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
3404 my $geno_file = $c->stash->{genotype_file_name};
3406 no warnings 'uninitialized';
3407 unless (-s $geno_file)
3409 my $args = $self->genotype_trial_query_args($c, $pop_id);
3410 $self->submit_cluster_genotype_query($c, $args);
3413 $c->stash->{genotype_file} = $geno_file;
3418 sub get_rrblup_output {
3419 my ($self, $c) = @_;
3421 $c->stash->{pop_id} = $c->stash->{combo_pops_id} if $c->stash->{combo_pops_id};
3423 my $pop_id = $c->stash->{pop_id} || $c->stash->{training_pop_id};
3424 my $trait_abbr = $c->stash->{trait_abbr};
3425 my $trait_name = $c->stash->{trait_name};
3426 my $trait_id = $c->stash->{trait_id};
3428 my $data_set_type = $c->stash->{data_set_type};
3429 my $selection_pop_id = $c->stash->{prediction_pop_id} || $c->stash->{selection_pop_id};
3431 my ($traits_file, @traits, @trait_pages);
3433 $c->stash->{selection_pop_id} = $selection_pop_id;
3434 if ($trait_id)
3436 $self->run_rrblup_trait($c, $trait_id);
3438 else
3440 $traits_file = $c->stash->{selected_traits_file};
3441 my $content = read_file($traits_file);
3443 if ($content =~ /\t/)
3445 @traits = split(/\t/, $content);
3447 else
3449 push @traits, $content;
3452 no warnings 'uninitialized';
3454 foreach my $tr (@traits)
3456 my $acronym_pairs = $self->get_acronym_pairs($c);
3457 my $trait_name;
3458 if ($acronym_pairs)
3460 foreach my $r (@$acronym_pairs)
3462 if ($r->[0] eq $tr)
3464 $trait_name = $r->[1];
3465 $trait_name =~ s/\n//g;
3466 $c->stash->{trait_name} = $trait_name;
3467 $c->stash->{trait_abbr} = $r->[0];
3472 my $trait_id = $c->model('solGS::solGS')->get_trait_id($trait_name);
3473 $self->run_rrblup_trait($c, $trait_id);
3476 push @trait_pages, [ qq | <a href="/solgs/trait/$trait_id/population/$pop_id" onclick="solGS.waitPage()">$tr</a>| ];
3480 $c->stash->{combo_pops_analysis_result} = 0;
3482 no warnings 'uninitialized';
3484 if ($data_set_type !~ /combined populations/)
3486 if (scalar(@traits) == 1)
3488 $self->gs_modeling_files($c);
3489 $c->stash->{template} = $c->controller('solGS::Files')->template('population/trait.mas');
3492 if (scalar(@traits) > 1)
3494 $c->stash->{model_id} = $pop_id;
3495 $self->analyzed_traits($c);
3496 $c->stash->{template} = $c->controller('solGS::Files')->template('/population/multiple_traits_output.mas');
3497 $c->stash->{trait_pages} = \@trait_pages;
3500 else
3502 $c->stash->{combo_pops_analysis_result} = 1;
3508 sub run_rrblup_trait {
3509 my ($self, $c, $trait_id) = @_;
3511 $trait_id = $c->stash->{trait_id} if !$trait_id;
3513 $c->stash->{trait_id} = $trait_id;
3514 $self->get_trait_details($c, $trait_id);
3516 my $training_pop_id = $c->stash->{training_pop_id} || $c->stash->{pop_id};
3517 my $selection_pop_id = $c->stash->{prediction_pop_id} || $c->stash->{selection_pop_id};
3519 $self->input_files($c);
3520 $self->output_files($c);
3521 $c->stash->{r_script} = 'R/solGS/gs.r';
3523 my $training_pop_gebvs_file = $c->stash->{rrblup_training_gebvs_file};
3524 my $selection_pop_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
3526 if ($training_pop_id && !-s $training_pop_gebvs_file)
3528 $self->run_r_script($c);
3530 elsif (($selection_pop_id && !-s $selection_pop_gebvs_file))
3533 $self->get_selection_pop_query_args_file($c);
3534 my $pre_req = $c->stash->{selection_pop_query_args_file};
3536 $self->get_gs_modeling_jobs_args_file($c);
3537 my $dependent_job = $c->stash->{gs_modeling_jobs_args_file};
3539 $c->stash->{prerequisite_jobs} = $pre_req;
3540 $c->stash->{dependent_jobs} = $dependent_job;
3542 $self->run_async($c);
3548 sub create_cluster_accesible_tmp_files {
3549 my ($self, $c, $template) = @_;
3551 my $temp_file_template = $template || $c->stash->{r_temp_file};
3553 my $temp_dir = $c->stash->{analysis_tempfiles_dir} || $c->stash->{solgs_tempfiles_dir};
3555 my $in_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "${temp_file_template}-in");
3556 my $out_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "${temp_file_template}-out");
3557 my $err_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "${temp_file_template}-err");
3559 $c->stash(
3560 in_file_temp => $in_file,
3561 out_file_temp => $out_file,
3562 err_file_temp => $err_file,
3568 sub run_async {
3569 my ($self, $c) = @_;
3571 my $prerequisite_jobs = $c->stash->{prerequisite_jobs} || 'none';
3572 my $background_job = $c->stash->{background_job};
3573 my $dependent_jobs = $c->stash->{dependent_jobs};
3575 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3577 $c->stash->{r_temp_file} = 'run-async';
3578 $self->create_cluster_accesible_tmp_files($c);
3579 my $err_temp_file = $c->stash->{err_file_temp};
3580 my $out_temp_file = $c->stash->{out_file_temp};
3582 my $referer = $c->req->referer;
3584 my $report_file = 'none';
3586 if ($background_job)
3588 $c->stash->{async} = 1;
3589 $c->controller('solGS::AnalysisQueue')->get_analysis_report_job_args_file($c, 2);
3590 $report_file = $c->stash->{analysis_report_job_args_file};
3593 my $config_args = {
3594 'temp_dir' => $temp_dir,
3595 'out_file' => $out_temp_file,
3596 'err_file' => $err_temp_file,
3597 'cluster_host' => 'localhost'
3600 my $job_config = $self->create_cluster_config($c, $config_args);
3601 my $job_config_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'job_config_file');
3603 nstore $job_config, $job_config_file
3604 or croak "job config file: $! serializing job config to $job_config_file ";
3607 # my $jobs = solGS::asyncJob->new({prerequisite_jobs => $prerequisite_jobs,
3608 # dependent_jobs => $dependent_jobs,
3609 # analysis_report_job => $report_file,
3610 # config_file => $job_config_file}
3611 # );
3612 # print STDERR "\ncalling async job run\n";
3613 # $jobs->run;
3615 my $cmd = 'mx-run solGS::asyncJob'
3616 . ' --prerequisite_jobs ' . $prerequisite_jobs
3617 . ' --dependent_jobs ' . $dependent_jobs
3618 . ' --analysis_report_job ' . $report_file
3619 . ' --config_file ' . $job_config_file;
3622 print STDERR "\nDONE callg async job run\n";
3623 my $cluster_job_args = {
3624 'cmd' => $cmd,
3625 'config' => $job_config,
3626 'background_job' => $background_job,
3627 'temp_dir' => $temp_dir,
3628 'async' => $c->stash->{async},
3631 my $job = $self->submit_job_cluster($c, $cluster_job_args);
3636 sub get_gs_r_temp_file {
3637 my ($self, $c) = @_;
3639 my $pop_id = $c->stash->{pop_id};
3640 my $trait_id = $c->stash->{trait_id};
3642 my $data_set_type = $c->stash->{data_set_type};
3644 my $selection_pop_id = $c->stash->{prediction_pop_id} || $c->stash->{selection_pop_id};
3645 $c->stash->{selection_pop_id} = $selection_pop_id;
3647 $pop_id = $c->stash->{combo_pops_id} if !$pop_id;
3648 my $identifier = $selection_pop_id ? $pop_id . '-' . $selection_pop_id : $pop_id;
3650 if ($data_set_type =~ /combined populations/)
3652 my $combo_identifier = $c->stash->{combo_pops_id};
3653 $c->stash->{r_temp_file} = "gs-rrblup-combo-${identifier}-${trait_id}";
3655 else
3657 $c->stash->{r_temp_file} = "gs-rrblup-${identifier}-${trait_id}";
3663 sub get_selection_pop_query_args {
3664 my ($self, $c) = @_;
3666 my $selection_pop_id = $c->stash->{selection_pop_id} || $c->stash->{prediction_pop_id};
3668 my $selection_pop_geno_file;
3669 my $pop_type;
3671 if ($selection_pop_id)
3673 $c->controller('solGS::Files')->genotype_file_name($c, $selection_pop_id);
3674 $selection_pop_geno_file = $c->stash->{genotype_file_name};
3677 my $genotypes_ids;
3678 if ($selection_pop_id =~ /list/)
3680 $c->controller('solGS::List')->get_genotypes_list_details($c);
3681 $genotypes_ids = $c->stash->{genotypes_ids};
3682 $pop_type = 'list';
3684 elsif ($selection_pop_id =~ /dataset/)
3686 #$c->controller('solGS::Dataset')->get_dataset_genotypes_list($c);
3687 #$genotypes_ids = $c->stash->{genotypes_ids};
3689 $pop_type = 'dataset';
3691 else
3693 $pop_type = 'trial';
3696 $c->stash->{population_type} = $pop_type;
3697 my $temp_file_template = "genotype-data-query-${selection_pop_id}";
3698 $self->create_cluster_accesible_tmp_files($c, $temp_file_template);
3699 my $in_file = $c->stash->{in_file_temp};
3700 my $out_temp_file = $c->stash->{out_file_temp};
3701 my $err_temp_file = $c->stash->{err_file_temp};
3703 my $selection_pop_query_args = {
3704 'trial_id' => $selection_pop_id,
3705 'genotype_file' => $selection_pop_geno_file,
3706 'genotypes_ids' => $genotypes_ids,
3707 'dataset_id' => $c->stash->{dataset_id},
3708 'out_file' => $out_temp_file,
3709 'err_file' => $err_temp_file,
3710 'population_type' => $pop_type
3713 $c->stash->{selection_pop_query_args} = $selection_pop_query_args;
3718 sub get_cluster_query_job_args {
3719 my ($self, $c) = @_;
3721 my $pop_id = $c->stash->{selection_pop_id} || $c->stash->{prediction_pop_id};
3723 $c->controller('solGS::Files')->genotype_file_name($c, $pop_id);
3724 my $geno_file = $c->stash->{genotype_file_name};
3726 my @queries;
3728 if (!-s $geno_file)
3730 $c->stash->{r_temp_file} = "genotype-data-query-${pop_id}";
3731 $self->create_cluster_accesible_tmp_files($c);
3732 my $out_temp_file = $c->stash->{out_file_temp};
3733 my $err_temp_file = $c->stash->{err_file_temp};
3735 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3736 my $background_job = $c->stash->{background_job};
3738 $self->get_selection_pop_query_args($c);
3739 my $query_args = $c->stash->{selection_pop_query_args};
3740 my $genotype_file = $query_args->{genotype_file};
3741 my $args_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, "geno-data-args_file-${pop_id}");
3743 my $pop_type = $query_args->{population_type};
3744 my $data_type = 'genotype';
3746 nstore $query_args, $args_file
3747 or croak "data query script: $! serializing model details to $args_file ";
3749 my $cmd = 'mx-run solGS::queryJobs '
3750 . ' --data_type ' . $data_type
3751 . ' --population_type ' . $pop_type
3752 . ' --args_file ' . $args_file;
3754 my $config_args = {
3755 'temp_dir' => $temp_dir,
3756 'out_file' => $out_temp_file,
3757 'err_file' => $err_temp_file,
3758 'cluster_host' => 'localhost'
3761 my $config = $self->create_cluster_config($c, $config_args);
3763 my $job_args = {
3764 'cmd' => $cmd,
3765 'config' => $config,
3766 'background_job'=> $background_job,
3767 'temp_dir' => $temp_dir,
3768 'genotype_file' => $genotype_file
3771 push @queries, $job_args;
3775 $c->stash->{cluster_query_job_args} = \@queries;
3779 sub get_selection_pop_query_args_file {
3780 my ($self, $c) = @_;
3782 $self->get_cluster_query_job_args($c);
3783 my $selection_pop_query_args = $c->stash->{cluster_query_job_args};
3785 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3786 my $selection_pop_query_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'selection_pop_query_args');
3788 nstore $selection_pop_query_args, $selection_pop_query_file
3789 or croak "selection pop query job : $! serializing selection pop data query details to $selection_pop_query_file";
3791 $c->stash->{selection_pop_query_args_file} = $selection_pop_query_file;
3795 sub modeling_jobs {
3796 my ($self, $c) = @_;
3798 my $modeling_traits = $c->stash->{training_traits_ids} || [$c->stash->{trait_id}];
3799 my $training_pop_id = $c->stash->{training_pop_id};
3800 my $selection_pop_id = $c->stash->{selection_pop_id};
3802 my @modeling_jobs;
3804 if ($modeling_traits) {
3806 foreach my $trait_id (@$modeling_traits)
3808 $c->stash->{trait_id} = $trait_id;
3809 $self->get_trait_details($c);
3811 $self->input_files($c);
3812 $self->output_files($c);
3814 my $selection_pop_gebvs_file = $c->stash->{rrblup_selection_gebvs_file};
3815 my $training_pop_gebvs_file = $c->stash->{rrblup_training_gebvs_file};
3817 if (($training_pop_id && !-s $training_pop_gebvs_file) ||
3818 ($selection_pop_id && !-s $selection_pop_gebvs_file))
3820 $self->get_gs_r_temp_file($c);
3821 $c->stash->{r_script} = 'R/solGS/gs.r';
3822 $self->get_cluster_r_job_args($c);
3824 push @modeling_jobs, $c->stash->{cluster_r_job_args};
3829 return \@modeling_jobs;
3833 sub get_gs_modeling_jobs_args_file {
3834 my ($self, $c) = @_;
3836 my $modeling_jobs = [];
3838 if ($c->stash->{training_traits_ids})
3840 $modeling_jobs = $self->modeling_jobs($c);
3843 if ($modeling_jobs)
3845 my $temp_dir = $c->stash->{solgs_tempfiles_dir};
3846 my $model_file = $c->controller('solGS::Files')->create_tempfile($temp_dir, 'gs_model_args');
3848 nstore $modeling_jobs, $model_file
3849 or croak "gs r script: $! serializing model details to $model_file";
3851 $c->stash->{gs_modeling_jobs_args_file} = $model_file;
3857 sub run_r_script {
3858 my ($self, $c) = @_;
3860 if ($c->stash->{background_job})
3862 $self->get_gs_modeling_jobs_args_file($c);
3863 $c->stash->{dependent_jobs} = $c->stash->{gs_modeling_jobs_args_file};
3864 $self->run_async($c);
3866 else
3868 $self->get_cluster_r_job_args($c);
3869 my $cluster_job_args = $c->stash->{cluster_r_job_args};
3870 $self->submit_job_cluster($c, $cluster_job_args);
3876 sub get_cluster_r_job_args {
3877 my ($self, $c) = @_;
3879 my $r_script = $c->stash->{r_script};
3880 my $input_files = $c->stash->{input_files};
3881 my $output_files = $c->stash->{output_files};
3883 if ($r_script =~ /gs/)
3885 $self->get_gs_r_temp_file($c);
3888 $self->create_cluster_accesible_tmp_files($c);
3889 my $in_file = $c->stash->{in_file_temp};
3890 my $out_temp_file = $c->stash->{out_file_temp};
3891 my $err_temp_file = $c->stash->{err_file_temp};
3893 my $temp_dir = $c->stash->{analysis_tempfiles_dir} || $c->stash->{solgs_tempfiles_dir};
3896 my $r_cmd_file = $c->path_to($r_script);
3897 copy($r_cmd_file, $in_file)
3898 or die "could not copy '$r_cmd_file' to '$in_file'";
3901 my $config_args = {
3902 'temp_dir' => $temp_dir,
3903 'out_file' => $out_temp_file,
3904 'err_file' => $err_temp_file
3907 my $config = $self->create_cluster_config($c, $config_args);
3909 my $cmd = 'Rscript --slave '
3910 . "$in_file $out_temp_file "
3911 . '--args ' . $input_files
3912 . ' ' . $output_files;
3914 my $job_args = {
3915 'cmd' => $cmd,
3916 'background_job' => $c->stash->{background_job},
3917 'config' => $config,
3920 $c->stash->{cluster_r_job_args} = $job_args;
3925 sub create_cluster_config {
3926 my ($self, $c, $args) = @_;
3928 my $config = {
3929 temp_base => $args->{temp_dir},
3930 queue => $c->config->{'web_cluster_queue'},
3931 max_cluster_jobs => 1_000_000_000,
3932 out_file => $args->{out_file},
3933 err_file => $args->{err_file},
3934 is_async => 0,
3935 do_cleanup => 0,
3936 sleep => $args->{sleep}
3939 if ($args->{cluster_host} =~ /localhost/) {
3940 $config->{backend} = 'Slurm';
3941 } else {
3942 my $backend = $c->config->{backend};
3943 my $cluster_host = $c->config->{cluster_host};
3944 my $error_file = $config->{err_file};
3945 print STDERR "\n\nsubmit job to remote cluster: backend - $backend : submit_host - $cluster_host\n\n";
3946 $config->{backend} = $c->config->{backend};
3947 $config->{submit_host} = $c->config->{cluster_host};
3950 return $config;
3954 sub submit_job_cluster {
3955 my ($self, $c, $args) = @_;
3957 my $job;
3959 my $cmd = $args->{cmd};
3960 print STDERR "\n submit_job_cluster cmd: $cmd\n";
3961 eval
3963 $job = CXGN::Tools::Run->new($args->{config});
3964 $job->do_not_cleanup(1);
3967 if ($args->{background_job})
3969 print STDERR "\n background submit_job_cluster async job\n";
3970 $job->is_async(1);
3971 $job->run_async($args->{cmd});
3973 $c->stash->{r_job_tempdir} = $job->job_tempdir();
3974 $c->stash->{r_job_id} = $job->jobid();
3975 $c->stash->{cluster_job_id} = $job->cluster_job_id();
3976 $c->stash->{cluster_job} = $job;
3978 else
3980 print STDERR "\n WAIT submit_job_cluster async job\n";
3981 $job->run_async($args->{cmd});
3982 $job->wait();
3986 if ($@)
3988 $c->stash->{Error} = 'Error occured submitting the job ' . $@ . "\nJob: " . $args->{cmd};
3989 $c->stash->{status} = 'Error occured submitting the job ' . $@ . "\nJob: " . $args->{cmd};
3992 return $job;
3996 # sub default :Path {
3997 # my ( $self, $c ) = @_;
3998 # $c->forward('search');
4003 =head2 end
4005 Attempt to render a view, if needed.
4007 =cut
4009 #sub render : ActionClass('RenderView') {}
4010 sub begin : Private {
4011 my ($self, $c) = @_;
4013 $c->controller('solGS::Files')->get_solgs_dirs($c);
4019 =head1 AUTHOR
4021 Isaak Y Tecle <iyt2@cornell.edu>
4023 =head1 LICENSE
4025 This library is free software. You can redistribute it and/or modify
4026 it under the same terms as Perl itself.
4028 =cut
4030 __PACKAGE__->meta->make_immutable;