1 package CXGN
::Fieldbook
::DownloadTrial
;
5 CXGN::Fieldbook::DownloadTrial - an object to handle creating a Fieldbook Trial Layout xls file.
9 this module is used to create a Fieldbook layout file that can be imported into Fieldbook App. it stores the file on fileserver and saves the file to a user, allowing them to access it later on.
11 my $create_fieldbook = CXGN::Fieldbook::DownloadTrial->new({
12 bcs_schema => $schema,
13 metadata_schema => $metadata_schema,
14 phenome_schema => $phenome_schema,
15 trial_id => $trial_id,
16 tempfile => '/tmp/fieldbook_file1.xls',
17 archive_path => /archive/path/,
18 user_id => $c->user()->get_object()->get_sp_person_id(),
19 user_name => $c->user()->get_object()->get_username(),
20 data_level => 'plots',
21 treatment_project_ids => [1],
22 selected_columns => {"plot_name"=>1,"block_number"=>1,"plot_number"=>1},
23 selected_trait_ids => [2,3],
26 my $create_fieldbook_return = $create_fieldbook->download();
28 if ($create_fieldbook_return->{'error_messages'}){
29 $error = join ',', @{$create_fieldbook_return->{'error_messages'}};
31 my $file_name = $create_fieldbook_return->{'file'};
32 my $file_id = $create_fieldbook_return->{'file_id'};
39 use Moose
::Util
::TypeConstraints
;
41 use File
::Basename qw
| basename dirname
|;
43 use File
::Spec
::Functions
;
45 use CXGN
::List
::Validate
;
47 use CXGN
::Trial
::TrialLayout
;
48 use Spreadsheet
::WriteExcel
;
50 use CXGN
::List
::Transform
;
51 use CXGN
::People
::Person
;
53 use CXGN
::Stock
::Accession
;
55 use CXGN
::Phenotypes
::Summary
;
56 use CXGN
::Trial
::TrialLayoutDownload
;
59 isa
=> "Bio::Chado::Schema",
64 has
'metadata_schema' => (
65 isa
=> "CXGN::Metadata::Schema",
70 has
'phenome_schema' => (
71 isa
=> "CXGN::Phenome::Schema",
82 has
'tempfile' => (isa
=> 'Str', is
=> 'ro',
83 predicate
=> 'has_tempfile',
87 has
'archive_path' => (isa
=> 'Str', is
=> 'ro',
88 predicate
=> 'has_archive_path',
104 has
'data_level' => (
110 has
'file_metadata' => (isa
=> 'Str', is
=> 'rw', predicate
=> 'has_file_metadata');
112 has
'treatment_project_ids' => (
113 isa
=> 'ArrayRef[Int]|Undef',
117 has
'selected_columns' => (
120 default => sub { {"plot_name"=>1, "plot_number"=>1} }
123 has
'selected_trait_ids'=> (
125 isa
=> 'ArrayRef[Int]|Undef',
133 my $schema = $self->bcs_schema();
134 my $trial_id = $self->trial_id();
135 my $tempfile = $self->tempfile();
136 my $wb = Spreadsheet
::WriteExcel
->new($tempfile);
138 push @error_messages, "Could not create file.";
139 $errors{'error_messages'} = \
@error_messages;
143 my $ws = $wb->add_worksheet();
144 my $trial_layout_download = CXGN
::Trial
::TrialLayoutDownload
->new({
146 trial_id
=> $trial_id,
147 data_level
=> $self->data_level,
148 treatment_project_ids
=> $self->treatment_project_ids,
149 selected_columns
=> $self->selected_columns,
150 selected_trait_ids
=> $self->selected_trait_ids
152 my $output = $trial_layout_download->get_layout_output();
153 if ($output->{error_messages
}){
156 my @output_array = @
{$output->{output
}};
158 foreach my $l (@output_array){
161 $ws->write($row_num, $col_num, $c);
168 my $user_id = $self->user_id();
169 open(my $F, "<", $tempfile) || die "Can't open file ".$self->tempfile();
171 my $md5 = Digest
::MD5
->new();
175 my $selected_trial = CXGN
::Trial
->new({bcs_schema
=> $schema, trial_id
=> $trial_id});
176 my $trial_name = $selected_trial->get_name();
178 my $time = DateTime
->now();
179 my $timestamp = $time->ymd()."_".$time->hms();
180 my $user_name = $self->user_name();
181 my $subdirectory_name = "tablet_field_layout";
182 my $archived_file_name = catfile
($user_id, $subdirectory_name,$timestamp."_".$trial_name.".xls");
183 my $archive_path = $self->archive_path();
184 my $file_destination = catfile
($archive_path, $archived_file_name);
186 if (!-d
$archive_path) {
190 if (! -d catfile
($archive_path, $user_id)) {
191 mkdir (catfile
($archive_path, $user_id));
194 if (! -d catfile
($archive_path, $user_id,$subdirectory_name)) {
195 mkdir (catfile
($archive_path, $user_id, $subdirectory_name));
198 my $metadata_schema = $self->metadata_schema();
199 my $md_row = $metadata_schema->resultset("MdMetadata")->create({
200 create_person_id
=> $user_id,
204 my $file_row = $metadata_schema->resultset("MdFiles")->create({
205 basename
=> basename
($file_destination),
206 dirname
=> dirname
($file_destination),
207 filetype
=> 'tablet field layout xls',
208 md5checksum
=> $md5->hexdigest(),
209 metadata_id
=> $md_row->metadata_id(),
213 my $field_layout_cvterm = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'field_layout', 'experiment_type' );
215 my $experiment = $schema->resultset('NaturalDiversity::NdExperiment')->find(
217 'nd_experiment_projects.project_id' => $trial_id,
218 type_id
=> $field_layout_cvterm->cvterm_id(),
221 join => 'nd_experiment_projects',
225 my $phenome_schema = $self->phenome_schema();
226 my $experiment_files = $phenome_schema->resultset("NdExperimentMdFiles")->create({
227 nd_experiment_id
=> $experiment->nd_experiment_id(),
228 file_id
=> $file_row->file_id(),
230 $experiment_files->insert();
232 move
($tempfile,$file_destination);
235 my $result = $file_row->file_id;
236 print STDERR
"FIeldbook file generated $file_destination ".localtime()."\n";
237 return {result
=> $result, file
=> $file_destination, file_id
=>$file_row->file_id() };