Merge pull request #2383 from solgenomics/dauglyon-patch-1
[sgn.git] / lib / CXGN / Fieldbook / DownloadTrial.pm
blob3705196e8ec769c92a6cb0f5f87ff5eaa51728e2
1 package CXGN::Fieldbook::DownloadTrial;
3 =head1 NAME
5 CXGN::Fieldbook::DownloadTrial - an object to handle creating a Fieldbook Trial Layout xls file.
7 =head1 SYNOPSIS
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],
24 });
26 my $create_fieldbook_return = $create_fieldbook->download();
27 my $error;
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'};
34 =head1 AUTHORS
36 =cut
38 use Moose;
39 use Moose::Util::TypeConstraints;
40 use Try::Tiny;
41 use File::Basename qw | basename dirname|;
42 use File::Copy;
43 use File::Spec::Functions;
44 use Digest::MD5;
45 use CXGN::List::Validate;
46 use Data::Dumper;
47 use CXGN::Trial::TrialLayout;
48 use Spreadsheet::WriteExcel;
49 use CXGN::Trait;
50 use CXGN::List::Transform;
51 use CXGN::People::Person;
52 use DateTime;
53 use CXGN::Stock::Accession;
54 use CXGN::Stock;
55 use CXGN::Phenotypes::Summary;
56 use CXGN::Trial::TrialLayoutDownload;
58 has 'bcs_schema' => (
59 isa => "Bio::Chado::Schema",
60 is => 'ro',
61 required => 1,
64 has 'metadata_schema' => (
65 isa => "CXGN::Metadata::Schema",
66 is => 'ro',
67 required => 1,
70 has 'phenome_schema' => (
71 isa => "CXGN::Phenome::Schema",
72 is => 'ro',
73 required => 1,
76 has 'trial_id' => (
77 isa => "Int",
78 is => 'ro',
79 required => 1,
82 has 'tempfile' => (isa => 'Str', is => 'ro',
83 predicate => 'has_tempfile',
84 required => 1,
87 has 'archive_path' => (isa => 'Str', is => 'ro',
88 predicate => 'has_archive_path',
89 required => 1,
92 has 'user_id' => (
93 is => 'ro',
94 isa => 'Int',
95 required => 1,
98 has 'user_name' => (
99 is => 'ro',
100 isa => 'Str',
101 required => 1,
104 has 'data_level' => (
105 is => 'ro',
106 isa => 'Str',
107 default => 'plots',
110 has 'file_metadata' => (isa => 'Str', is => 'rw', predicate => 'has_file_metadata');
112 has 'treatment_project_ids' => (
113 isa => 'ArrayRef[Int]|Undef',
114 is => 'rw'
117 has 'selected_columns' => (
118 is => 'ro',
119 isa => 'HashRef',
120 default => sub { {"plot_name"=>1, "plot_number"=>1} }
123 has 'selected_trait_ids'=> (
124 is => 'ro',
125 isa => 'ArrayRef[Int]|Undef',
128 sub download {
129 my $self = shift;
130 my %errors;
131 my @error_messages;
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);
137 if (!$wb) {
138 push @error_messages, "Could not create file.";
139 $errors{'error_messages'} = \@error_messages;
140 return \%errors;
143 my $ws = $wb->add_worksheet();
144 my $trial_layout_download = CXGN::Trial::TrialLayoutDownload->new({
145 schema => $schema,
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}){
154 return $output;
156 my @output_array = @{$output->{output}};
157 my $row_num = 0;
158 foreach my $l (@output_array){
159 my $col_num = 0;
160 foreach my $c (@$l){
161 $ws->write($row_num, $col_num, $c);
162 $col_num++;
164 $row_num++;
166 $wb->close();
168 my $user_id = $self->user_id();
169 open(my $F, "<", $tempfile) || die "Can't open file ".$self->tempfile();
170 binmode $F;
171 my $md5 = Digest::MD5->new();
172 $md5->addfile($F);
173 close($F);
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) {
187 mkdir $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,
202 $md_row->insert();
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(),
211 $file_row->insert();
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);
233 unlink $tempfile;
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() };