4 SGN::Controller::AJAX::PhenotypesDownload - a REST controller class to provide the
5 backend for downloading phenotype spreadsheets
9 Downloading Phenotype Spreadsheets
13 Jeremy Edwards <jde22@cornell.edu>
17 package SGN
::Controller
::AJAX
::PhenotypesDownload
;
23 use File
::Spec
::Functions
;
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);
33 use JSON
qw( decode_json );
35 BEGIN { extends
'Catalyst::Controller::REST' }
38 default => 'application/json',
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) {
49 $c->forward('create_phenotype_spreadsheet_POST');
52 sub create_phenotype_spreadsheet_POST
: Args
(0) {
53 print STDERR
"phenotype download controller\n";
56 $c->stash->{rest
} = {error
=> "You need to be logged in to download a phenotype spreadsheet." };
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;
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." };
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." };
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,
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;
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);
134 foreach (@
{$traits->{new_traits
}}){
135 $new_traits{$_->[1]} = join ',', @
{$_->[0]};
137 #print STDERR Dumper \%new_traits;
140 my $onto = CXGN
::Onto
->new({ schema
=> $schema });
141 $new_terms = $onto->store_composed_term(\
%new_traits);
144 die "An error occurred saving the new trait details: $@";
146 #print STDERR Dumper $new_terms;
150 #$c->stash->{rest} = { error => $error };
153 $c->stash->{rest
} = { filename
=> $urlencode{$rel_file.".xls"} };
157 sub _parse_list_from_json
{
158 my $list_json = shift;
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;