seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / PhenotypesDownload.pm
blobf0b7b4a7922f5c87fae37a91da85f175f9bd7ebc
2 =head1 NAME
4 SGN::Controller::AJAX::PhenotypesDownload - a REST controller class to provide the
5 backend for downloading phenotype spreadsheets
7 =head1 DESCRIPTION
9 Downloading Phenotype Spreadsheets
11 =head1 AUTHOR
13 Jeremy Edwards <jde22@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::PhenotypesDownload;
19 use Moose;
20 use Try::Tiny;
21 use DateTime;
22 use File::Slurp;
23 use File::Spec::Functions;
24 use File::Copy;
25 use List::MoreUtils qw /any /;
26 use SGN::View::ArrayElements qw/array_elements_simple_view/;
27 use CXGN::Stock::StockTemplate;
28 use JSON -support_by_pp;
29 use CXGN::Phenotypes::CreateSpreadsheet;
30 use CXGN::Trial::Download;
31 use Tie::UrlEncoder; our(%urlencode);
32 use Data::Dumper;
33 use JSON qw( decode_json );
35 BEGIN { extends 'Catalyst::Controller::REST' }
37 __PACKAGE__->config(
38 default => 'application/json',
39 stash_key => 'rest',
40 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
44 sub create_phenotype_spreadsheet : Path('/ajax/phenotype/create_spreadsheet') : ActionClass('REST') { }
46 sub create_phenotype_spreadsheet_GET : Args(0) {
47 my $self = shift;
48 my $c = shift;
49 $c->forward('create_phenotype_spreadsheet_POST');
52 sub create_phenotype_spreadsheet_POST : Args(0) {
53 print STDERR "phenotype download controller\n";
54 my ($self, $c) = @_;
55 if (!$c->user()) {
56 $c->stash->{rest} = {error => "You need to be logged in to download a phenotype spreadsheet." };
57 return;
60 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
61 my @trial_ids = @{_parse_list_from_json($c->req->param('trial_ids'))};
62 print STDERR Dumper \@trial_ids;
63 my $format = $c->req->param('format') || "ExcelBasic";
64 my $data_level = $c->req->param('data_level') || "plots";
65 my $file_format = $c->req->param('create_spreadsheet_phenotype_file_format') || "detailed";
66 my $sample_number = $c->req->param('sample_number');
67 if ($sample_number eq '') {$sample_number = undef};
68 my $predefined_columns = $c->req->param('predefined_columns') ? decode_json $c->req->param('predefined_columns') : [];
70 #print STDERR Dumper $sample_number;
71 #print STDERR Dumper $predefined_columns;
73 foreach (@trial_ids){
74 if ($data_level eq 'plants') {
75 my $trial = CXGN::Trial->new( { bcs_schema => $c->dbic_schema("Bio::Chado::Schema"), trial_id => $_ });
76 if (!$trial->has_plant_entries()) {
77 $c->stash->{rest} = { error => "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first." };
78 return;
81 if ($data_level eq 'subplots' || $data_level eq 'plants_subplots') {
82 my $trial = CXGN::Trial->new( { bcs_schema => $c->dbic_schema("Bio::Chado::Schema"), trial_id => $_ });
83 if (!$trial->has_subplot_entries()) {
84 $c->stash->{rest} = { error => "The requested trial (".$trial->get_name().") does not have subplot entries." };
85 return;
90 my @trait_list = @{_parse_list_from_json($c->req->param('trait_list'))};
91 my $dir = $c->tempfiles_subdir('/download');
92 my $rel_file = $c->tempfile( TEMPLATE => 'download/downloadXXXXX');
93 my $tempfile = $c->config->{basepath}."/".$rel_file.".xls";
95 my $create_spreadsheet = CXGN::Trial::Download->new(
97 bcs_schema => $schema,
98 trial_list => \@trial_ids,
99 trait_list => \@trait_list,
100 filename => $tempfile,
101 format => $format,
102 data_level => $data_level,
103 sample_number => $sample_number,
104 predefined_columns => $predefined_columns,
107 $create_spreadsheet->download();
109 print STDERR "DOWNLOAD FILENAME = ".$create_spreadsheet->filename()."\n";
110 print STDERR "RELATIVE = $rel_file\n";
112 #Add postcomposed terms from selected predefined_columns
113 if (scalar(@$predefined_columns)>0){
114 my @allowed_composed_cvs = split ',', $c->config->{composable_cvs};
115 my $composable_cvterm_delimiter = $c->config->{composable_cvterm_delimiter};
116 my $composable_cvterm_format = $c->config->{composable_cvterm_format};
117 my @allowed_composed_cvs_minus_trait = grep { $_ ne 'trait' } @allowed_composed_cvs;
118 my %id_hash;
119 for my $i (0 .. scalar @$predefined_columns){
120 my $cv_type = $allowed_composed_cvs_minus_trait[$i];
121 foreach my $selected_term (values %{$predefined_columns->[$i]}){
122 my $cvterm_id = SGN::Model::Cvterm->get_cvterm_row_from_trait_name($schema, $selected_term)->cvterm_id();
123 push @{$id_hash{$cv_type}}, $cvterm_id;
126 my @trait_cvterm_ids;
127 foreach (@trait_list){
128 push @trait_cvterm_ids, SGN::Model::Cvterm->get_cvterm_row_from_trait_name($schema, $_)->cvterm_id();
130 $id_hash{'trait'} = \@trait_cvterm_ids;
131 #print STDERR Dumper \%id_hash;
132 my $traits = SGN::Model::Cvterm->get_traits_from_component_categories($schema, \@allowed_composed_cvs, $composable_cvterm_delimiter, $composable_cvterm_format, \%id_hash);
133 my %new_traits;
134 foreach (@{$traits->{new_traits}}){
135 $new_traits{$_->[1]} = join ',', @{$_->[0]};
137 #print STDERR Dumper \%new_traits;
138 my $new_terms;
139 eval {
140 my $onto = CXGN::Onto->new({ schema => $schema });
141 $new_terms = $onto->store_composed_term(\%new_traits);
143 if ($@) {
144 die "An error occurred saving the new trait details: $@";
146 #print STDERR Dumper $new_terms;
149 #if ($error) {
150 #$c->stash->{rest} = { error => $error };
151 #return;
153 $c->stash->{rest} = { filename => $urlencode{$rel_file.".xls"} };
157 sub _parse_list_from_json {
158 my $list_json = shift;
159 my $json = new JSON;
160 if ($list_json) {
161 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
162 #my $decoded_list = decode_json($list_json);
163 my @array_of_list_items = @{$decoded_list};
164 return \@array_of_list_items;
166 else {
167 return;
171 #########
173 #########