Merge pull request #5163 from solgenomics/audit-error-checking
[sgn.git] / lib / CXGN / Trial / TrialLayoutDownload / SamplingTrialLayout.pm
blob79caa8f7077d57a41a77caf4e7c2d0819b73bbdd
1 package CXGN::Trial::TrialLayoutDownload::SamplingTrialLayout;
3 =head1 NAME
5 CXGN::Trial::TrialLayoutDownload::SamplingTrialLayout - an object to handle downloading a sampling trial layout. this should only be called from CXGN::Trial::TrialLayoutDownload
7 =head1 USAGE
9 my $plate_layout = CXGN::Trial::TrialLayoutDownload::SamplingTrialLayout->new({
10 schema => $schema,
11 trial_id => $trial_id,
12 data_level => $data_level,
13 selected_columns => \%selected_cols,
14 design => $design,
15 trial => $selected_trial,
16 });
17 my $result = $plate_layout->retrieve();
19 =head1 DESCRIPTION
21 Will output an array of arrays, where each row is a tissue sample in the sampling trial. the columns are based on the supplied selected_cols. this should only be called from CXGN::Trial::TrialLayoutDownload
23 =head1 AUTHORS
25 =cut
27 use strict;
28 use warnings;
29 use Moose;
30 use Try::Tiny;
31 use Data::Dumper;
32 use SGN::Model::Cvterm;
33 use CXGN::Stock;
34 use CXGN::Stock::Accession;
36 extends 'CXGN::Trial::TrialLayoutDownload';
38 sub retrieve {
39 my $self = shift;
40 my $schema = $self->schema();
41 my %selected_cols = %{$self->selected_columns};
42 my %design = %{$self->design};
43 my $trial = $self->trial;
44 my @output;
46 my @possible_cols = ('trial_name', 'year', 'location', 'sampling_facility', 'sampling_trial_sample_type', 'acquisition_date', 'tissue_sample_name', 'plot_number', 'rep_number', 'source_observation_unit_name', 'accession_name', 'synonyms', 'dna_person', 'notes', 'tissue_type', 'extraction', 'concentration', 'volume');
48 my @header;
49 foreach (@possible_cols){
50 if ($selected_cols{$_}){
51 push @header, $_;
54 push @output, \@header;
56 my $trial_name = $trial->get_name ? $trial->get_name : '';
57 my $location_name = $trial->get_location ? $trial->get_location->[1] : '';
58 my $trial_year = $trial->get_year ? $trial->get_year : '';
59 my $sampling_facility_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'sampling_facility', 'project_property')->cvterm_id();
60 my $sampling_trial_sample_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'sampling_trial_sample_type', 'project_property')->cvterm_id();
61 my $sampling_facility = $schema->resultset("Project::Projectprop")->search({ project_id => $trial->get_trial_id(), type_id => $sampling_facility_cvterm_id } )->first->value();
62 my $sampling_type = $schema->resultset("Project::Projectprop")->search({ project_id => $trial->get_trial_id(), type_id => $sampling_trial_sample_type_cvterm_id } )->first->value();
64 my $pedigree_strings = $self->_get_all_pedigrees(\%design);
66 foreach my $key (sort { $a cmp $b} keys %design) {
67 my $design_info = $design{$key};
68 my $line;
69 foreach (@possible_cols){
70 if ($selected_cols{$_}){
71 if ($_ eq 'trial_name'){
72 push @$line, $trial_name;
73 } elsif ($_ eq 'year'){
74 push @$line, $trial_year;
75 } elsif ($_ eq 'location'){
76 push @$line, $location_name;
77 } elsif ($_ eq 'sampling_facility'){
78 push @$line, $sampling_facility;
79 } elsif ($_ eq 'sampling_trial_sample_type'){
80 push @$line, $sampling_type;
81 } elsif ($_ eq 'tissue_sample_name'){
82 push @$line, $design_info->{'plot_name'};
83 } elsif ($_ eq 'synonyms'){
84 my $accession = CXGN::Stock::Accession->new({schema=>$schema, stock_id=>$design_info->{"accession_id"}});
85 push @$line, join ',', @{$accession->synonyms}
86 } elsif ($_ eq 'pedigree'){
87 push @$line, $pedigree_strings->{$design_info->{"accession_name"}};
88 } else {
89 push @$line, $design_info->{$_};
93 push @output, $line;
96 return \@output;