1 package CXGN
::Fieldbook
::DownloadTrial
;
4 use Moose
::Util
::TypeConstraints
;
6 use File
::Basename qw
| basename dirname
|;
8 use File
::Spec
::Functions
;
10 use CXGN
::List
::Validate
;
12 use CXGN
::Trial
::TrialLayout
;
13 use Spreadsheet
::WriteExcel
;
15 use CXGN
::List
::Transform
;
16 use CXGN
::People
::Person
;
20 isa
=> "Bio::Chado::Schema",
25 has
'metadata_schema' => (
26 isa
=> "CXGN::Metadata::Schema",
31 has
'phenome_schema' => (
32 isa
=> "CXGN::Phenome::Schema",
43 has
'tempfile' => (isa
=> 'Str', is
=> 'ro',
44 predicate
=> 'has_tempfile',
48 has
'archive_path' => (isa
=> 'Str', is
=> 'ro',
49 predicate
=> 'has_archive_path',
65 has
'file_metadata' => (isa
=> 'Str', is
=> 'rw', predicate
=> 'has_file_metadata');
73 my $schema = $self->bcs_schema();
74 my $trial_id = $self->trial_id();
75 my $tempfile = $self->tempfile();
76 my $wb = Spreadsheet
::WriteExcel
->new($tempfile);
78 push @error_messages, "Could not create file.";
79 $errors{'error_messages'} = \
@error_messages;
83 my $ws = $wb->add_worksheet();
85 print STDERR
"\n\nTrial id: ($trial_id)\n\n";
87 $trial_layout = CXGN
::Trial
::TrialLayout
->new({schema
=> $schema, trial_id
=> $trial_id} );
90 push @error_messages, "Trial does not have valid field design.";
91 $errors{'error_messages'} = \
@error_messages;
95 my $trial_name = $trial_layout->get_trial_name();
96 $ws->write(0, 0, 'plot_id');
97 $ws->write(0, 1, 'range');
98 $ws->write(0, 2, 'plot');
99 $ws->write(0, 3, 'rep');
100 $ws->write(0, 4, 'accession');
101 $ws->write(0, 5, 'is_a_control');
103 my %design = %{$trial_layout->get_design()};
105 foreach my $key (sort { $a <=> $b} keys %design) {
106 my %design_info = %{$design{$key}};
107 $ws->write($row_num,0,$design_info{'plot_name'});
108 $ws->write($row_num,1,$design_info{'block_number'});
109 $ws->write($row_num,2,$design_info{'plot_number'});
110 $ws->write($row_num,3,$design_info{'rep_number'});
111 $ws->write($row_num,4,$design_info{'accession_name'});
112 $ws->write($row_num,5,$design_info{'is_a_control'});
117 my $user_id = $self->user_id();
118 open(my $F, "<", $tempfile) || die "Can't open file ".$self->tempfile();
120 my $md5 = Digest
::MD5
->new();
124 my $project = $trial_layout->get_project;
126 my $time = DateTime
->now();
127 my $timestamp = $time->ymd()."_".$time->hms();
128 my $user_name = $self->user_name();
129 my $subdirectory_name = "tablet_field_layout";
130 my $archived_file_name = catfile
($user_id, $subdirectory_name,$timestamp."_".$project->name.".xls");
131 my $archive_path = $self->archive_path();
132 my $file_destination = catfile
($archive_path, $archived_file_name);
134 if (!-d
$archive_path) {
138 if (! -d catfile
($archive_path, $user_id)) {
139 mkdir (catfile
($archive_path, $user_id));
142 if (! -d catfile
($archive_path, $user_id,$subdirectory_name)) {
143 mkdir (catfile
($archive_path, $user_id, $subdirectory_name));
146 my $metadata_schema = $self->metadata_schema();
147 my $md_row = $metadata_schema->resultset("MdMetadata")->create({
148 create_person_id
=> $user_id,
152 my $file_row = $metadata_schema->resultset("MdFiles")->create({
153 basename
=> basename
($file_destination),
154 dirname
=> dirname
($file_destination),
155 filetype
=> 'tablet field layout xls',
156 md5checksum
=> $md5->hexdigest(),
157 metadata_id
=> $md_row->metadata_id(),
161 my $field_layout_cvterm = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'field_layout', 'experiment_type' );
163 my $experiment = $schema->resultset('NaturalDiversity::NdExperiment')->find(
165 'nd_experiment_projects.project_id' => $project->project_id,
166 type_id
=> $field_layout_cvterm->cvterm_id(),
169 join => 'nd_experiment_projects',
173 my $phenome_schema = $self->phenome_schema();
174 my $experiment_files = $phenome_schema->resultset("NdExperimentMdFiles")->create({
175 nd_experiment_id
=> $experiment->nd_experiment_id(),
176 file_id
=> $file_row->file_id(),
178 $experiment_files->insert();
180 move
($tempfile,$file_destination);
183 my $result = $file_row->file_id;
184 return {result
=> $result, file
=> $file_destination};