2 package CXGN
::Trial
::Download
;
10 This module loads one of the plugins in /CXGN/Trial/Download/Plugin/
11 This module is used differently depending on the plugin.
12 Example usage for all plugins is listed here.
14 ------------------------------------------------------------------
16 For downloading trial(s) xls spreadsheet for collecting phenotypes (as used
17 from SGN::Controller::AJAX::PhenotypesDownload->create_phenotype_spreadsheet):
19 my $rel_file = $c->tempfile( TEMPLATE => 'download/downloadXXXXX');
20 my $tempfile = $c->config->{basepath}."/".$rel_file.".xls";
21 my $create_spreadsheet = CXGN::Trial::Download->new({
22 bcs_schema => $schema,
23 trial_list => \@trial_id_list,
24 trait_list => \@trait_list,
25 filename => $tempfile,
26 format => "ExcelBasic",
27 data_level => $data_level,
28 sample_number => $sample_number,
29 predefined_columns => $predefined_columns,
31 $create_spreadsheet->download();
32 $c->stash->{rest} = { filename => $urlencode{$rel_file.".xls"} };
34 ------------------------------------------------------------------
36 For downloading phenotypes in a matrix where columns contain the phenotypes
37 and rows contain the observation unit (as used from
38 SGN::Controller::BreedersToolbox::Download->download_phenotypes_action which
39 is used from the wizard, trial detail page, and manage trials page for
40 downlading phenotypes):
42 There a number of optional keys for filtering down the phenotypes
43 (trait_list, year_list, location_list, etc). Keys can be entirely ignored
44 if you don't need to filter by them.
47 my $plugin = 'TrialPhenotypeCSV';
50 my $plugin = 'TrialPhenotypeExcel';
52 my $download = CXGN::Trial::Download->new({
53 bcs_schema => $schema,
54 trait_list => \@trait_list_int,
55 year_list => \@year_list,
56 location_list => \@location_list_int,
57 trial_list => \@trial_list_int,
58 accession_list => \@accession_list_int,
59 plot_list => \@plot_list_int,
60 plant_list => \@plant_list_int,
61 filename => $tempfile,
63 data_level => $data_level,
64 include_timestamp => $timestamp_option,
65 exclude_phenotype_outlier => $exclude_phenotype_outlier,
66 trait_contains => \@trait_contains_list,
67 phenotype_min_value => $phenotype_min_value,
68 phenotype_max_value => $phenotype_max_value,
69 has_header=>$has_header
71 my $error = $download->download();
72 my $file_name = "phenotype.$format";
73 $c->res->content_type('Application/'.$format);
74 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
75 my $output = read_file($tempfile);
76 $c->res->body($output);
78 --------------------------------------------------------------------------
80 For downloading a "DataCollector Spreadsheet" for collecting phenotypes (as
81 used in SGN::Controller::AJAX::DataCollectorDownload->create_DataCollector_spreadsheet_POST):
83 my $create_spreadsheet = CXGN::Trial::Download->new({
84 bcs_schema => $schema,
85 trial_id => $trial_id,
86 trait_list => \@trait_list,
87 filename => $file_path,
88 format => 'DataCollectorExcel',
89 data_level => $data_level,
91 my $spreadsheet_response = $create_spreadsheet->download();
92 if ($spreadsheet_response->{error}) {
93 $c->stash->{rest} = { error => $spreadsheet_response->{error} };
96 my $file_name = basename($file_path);
97 $c->stash->{rest} = { filename => $urlencode{$tempfile.".xls"} };
99 -------------------------------------------------------------------------------
101 For downloading a trial's layout (as used from CXGN::Trial::Download->trial_download):
103 A trial's layout can optionally include treatment and phenotype summary
104 information, mapping to treatment_project_ids and trait_list.
105 These keys can be ignored if you don't need them in the layout.
108 my $plugin = "TrialLayoutExcel";
111 my $plugin = "TrialLayoutCSV";
113 my $download = CXGN::Trial::Download->new({
114 bcs_schema => $schema,
115 trial_id => $c->stash->{trial_id},
116 trait_list => \@trait_list,
117 filename => $tempfile,
119 data_level => $data_level,
120 treatment_project_ids => \@treatment_project_ids,
121 selected_columns => $selected_cols,
123 my $error = $download->download();
124 my $file_name = $trial_id . "_" . "$what" . ".$format";
125 $c->res->content_type('Application/'.$format);
126 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
127 my $output = read_file($tempfile);
128 $c->res->body($output);
130 ------------------------------------------------------------------------------
132 For downloading the IGD sequencing facility spreadsheet (as used from
133 SGN::Controller::BreedersToolbox::Download->download_sequencing_facility_spreadsheet):
135 my $td = CXGN::Trial::Download->new({
136 bcs_schema => $schema,
137 trial_id => $trial_id,
138 format => "IGDFacilitySpreadsheet",
139 filename => $file_path,
140 user_id => $c->user->get_object()->get_sp_person_id(),
141 trial_download_logfile => $c->config->{trial_download_logfile},
144 my $file_name = basename($file_path);
145 $c->res->content_type('Application/xls');
146 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
147 my $output = read_file($file_path, binmode=>':raw');
148 $c->res->body($output);
151 -------------------------------------------------------------------------------
160 use Moose
::Util
::TypeConstraints
;
162 use File
::Basename qw
| basename dirname
|;
164 use CXGN
::List
::Validate
;
167 use CXGN
::Trial
::TrialLayout
;
168 use Spreadsheet
::WriteExcel
;
170 use CXGN
::List
::Transform
;
171 use CXGN
::People
::Person
;
174 with
'MooseX::Object::Pluggable';
177 has
'bcs_schema' => (
178 isa
=> "Bio::Chado::Schema",
188 # can be provided for logging purposes
194 has
'trial_download_logfile' => (
199 ## defines the plugin with which the download will be processed
200 has
'format' => (isa
=> 'Str', is
=> 'ro', required
=> 1);
201 has
'data_level' => (isa
=> 'Str | Undef', is
=> 'ro', default => 'plots');
202 has
'sample_number' => (isa
=> 'Int | Undef', is
=> 'ro', default => 0);
203 has
'predefined_columns' => (isa
=> 'ArrayRef[HashRef] | Undef', is
=> 'ro');
204 has
'trait_list' => (isa
=> 'ArrayRef[Int|Str]|Undef', is
=> 'rw', predicate
=> 'has_trait_list' );
205 has
'trait_component_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
206 has
'trial_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
207 has
'accession_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
208 has
'plot_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
209 has
'plant_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
210 has
'location_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
211 has
'year_list' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw' );
212 has
'include_timestamp' => (isa
=> 'Bool', is
=> 'ro', default => 0);
213 has
'exclude_phenotype_outlier' => (isa
=> 'Bool', is
=> 'ro', default => 0);
214 has
'has_header' => (isa
=> 'Bool', is
=> 'ro', default => 1);
215 has
'trait_contains' => (isa
=> 'ArrayRef[Str]|Undef', is
=> 'rw');
216 has
'phenotype_min_value' => (isa
=> 'Str', is
=> 'rw');
217 has
'phenotype_max_value' => (isa
=> 'Str', is
=> 'rw');
218 has
'search_type' => (isa
=> 'Str', is
=> 'rw');
219 has
'treatment_project_ids' => (isa
=> 'ArrayRef[Int]|Undef', is
=> 'rw');
220 has
'selected_columns' => (isa
=> 'HashRef|Undef', is
=> 'rw');
221 has
'include_notes' => (isa
=> 'Str', is
=> 'rw');
222 has
'filename' => (isa
=> 'Str', is
=> 'ro',
223 predicate
=> 'has_filename',
227 has
'file_metadata' => (isa
=> 'Str', is
=> 'rw', predicate
=> 'has_file_metadata');
228 has
'trial_stock_type' => (isa
=> 'Str', is
=> 'rw', predicate
=> 'has_trial_stock_type', required
=> 0);
232 $self->load_plugin($self->format());
238 # $self->load_plugin($self->format());
240 # return $self->plugin_verify();
246 # print STDERR "Format: ".$self->format()."\n";
248 # $self->load_plugin($self->format());
251 # die "The plugin specified (".$self->format().") for the download does not exist";
254 # my $error = $self->plugin_download();
259 sub trial_download_log
{
261 my $trial_id = shift;
264 if (! $self->user_id && !$self->trial_download_logfile()) {
268 print STDERR
"Note: set config variable trial_download_logfile to obtain a log of downloaded trials.\n";
271 my $now = DateTime
->now();
273 open (my $F, ">>", $self->trial_download_logfile()) || die "Can't open ".$self->trial_download_logfile();
274 my $username = CXGN
::People
::Person
->new($self->bcs_schema->storage->dbh(), $self->user_id())->get_username();
275 print $F join("\t", (
279 $now->year()."-".$now->month()."-".$now->day()." ".$now->hour().":".$now->minute()));