interaction on sommer
[sgn.git] / lib / CXGN / Trial.pm
blob9b18edec931514d714ac7de2fee0aaf9384de1a0
2 =head1 NAME
4 CXGN::Trial - factory object for project entries (phenotyping trials, genotyping trials, crossing trials, and analyses
6 =head1 DESCRIPTION
8 my $trial = CXGN::Trial->new( { bcs_schema => $schema, ... , trial_id => $trial_id });
10 If $trial_id is a phenotyping trial, the type of object returned will be CXGN::PhenotypingTrial.
12 =head1 AUTHOR
14 Lukas Mueller <lam87@cornell.edu>
16 =head1 METHODS
18 =cut
20 package CXGN::Trial;
22 use Moose;
23 use Data::Dumper;
24 use Try::Tiny;
25 use Data::Dumper;
26 use CXGN::Trial::Folder;
27 use CXGN::Trial::TrialLayout;
28 use CXGN::Trial::TrialLayoutDownload;
29 use SGN::Model::Cvterm;
30 use Time::Piece;
31 use Time::Seconds;
32 use CXGN::Calendar;
33 use JSON;
34 use File::Basename qw | basename dirname|;
36 =head1 NAME
39 =head1 DESCRIPTION
41 If $trial_id is a genotyping trial, the type of object returned will be CXGN::GenotypingTrial.
43 If $trial_id is a crossing trial, the type of object returned will be CXGN::CrossingTrial.
45 If $trial_id is an analysis, the type of object returned will be CXGN::Analysis.
47 (you get the idea).
49 Inheritance structure of Trial objects:
51 CXGN::Trial - Factory object (for backwards compatibility)
53 CXGN::Project
55 ---CXGN::PhenotypingTrial
56 | |
57 | ---CXGN::GenotypingTrial
58 | |
59 | ---CXGN::CrossingTrial
61 ---CXGN::Analysis
63 =head1 AUTHOR
65 Lukas Mueller <lam87@cornell.edu>
67 Based on work by the entire group :-)
69 =cut
71 package CXGN::Trial;
73 use CXGN::PhenotypingTrial;
74 use CXGN::GenotypingTrial;
75 use CXGN::CrossingTrial;
76 use CXGN::Analysis;
77 use CXGN::SamplingTrial;
78 use CXGN::ManagementFactor;
79 use CXGN::GenotypeDataProject;
80 use CXGN::AerialImagingEventBandProject;
81 use CXGN::AerialImagingEventProject;
82 use Data::Dumper;
84 sub new {
85 my $class = shift;
86 my $args = shift;
87 my $schema = $args->{bcs_schema};
88 my $trial_id = $args->{trial_id};
90 my $trial_rs = $schema->resultset("Project::Projectprop")->search( { project_id => $trial_id },{ join => 'type' });
92 if ($trial_id && $trial_rs->count() == 0) {
93 return CXGN::PhenotypingTrial->new($args);
96 my $object;
97 while (my $trial_row = $trial_rs->next()) {
98 my $name = $trial_row->type()->name();
99 my $val = $trial_row->value();
100 # print STDERR Dumper [$name, $val];
101 if ($val eq "genotyping_plate") {
102 return CXGN::GenotypingTrial->new($args);
104 elsif ($name eq "crossing_trial") {
105 return CXGN::CrossingTrial->new($args);
107 elsif ($name eq "analysis") {
108 return CXGN::Analysis->new($args);
110 elsif ($val eq "treatment") {
111 return CXGN::ManagementFactor->new($args);
113 elsif ($val eq "sampling_trial") {
114 return CXGN::SamplingTrial->new($args);
116 elsif (($val eq "genotype_data_project") || ($val eq "pcr_genotype_data_project")) {
117 return CXGN::GenotypeDataProject->new($args);
119 elsif ($val eq "drone_run") {
120 return CXGN::AerialImagingEventProject->new($args);
122 elsif ($val eq "drone_run_band") {
123 return CXGN::AerialImagingEventBandProject->new($args);
125 else {
126 $object = CXGN::PhenotypingTrial->new($args);
129 return $object;
132 =head2 class method get_all_locations()
134 Usage: my $locations = CXGN::Trial::get_all_locations($schema)
135 Desc:
136 Ret:
137 Args:
138 Side Effects:
139 Example:
141 =cut
143 sub get_all_locations {
144 my $schema = shift;
145 my $location_id = shift;
146 my @locations;
148 my %search_params;
149 if ($location_id){
150 $search_params{'nd_geolocation_id'} = $location_id;
153 my $loc = $schema->resultset('NaturalDiversity::NdGeolocation')->search( \%search_params, {order_by => { -asc => 'nd_geolocation_id' }} );
154 while (my $s = $loc->next()) {
155 my $loc_props = $schema->resultset('NaturalDiversity::NdGeolocationprop')->search( { nd_geolocation_id => $s->nd_geolocation_id() }, {join=>'type', '+select'=>['me.value', 'type.name'], '+as'=>['value', 'cvterm_name'] } );
157 my %attr;
158 $attr{'geodetic datum'} = $s->geodetic_datum();
160 my $country = '';
161 my $country_code = '';
162 my $location_type = '';
163 my $abbreviation = '';
164 my $address = '';
166 while (my $sp = $loc_props->next()) {
167 if ($sp->get_column('cvterm_name') eq 'country_name') {
168 $country = $sp->get_column('value');
169 } elsif ($sp->get_column('cvterm_name') eq 'country_code') {
170 $country_code = $sp->get_column('value');
171 } elsif ($sp->get_column('cvterm_name') eq 'location_type') {
172 $location_type = $sp->get_column('value');
173 } elsif ($sp->get_column('cvterm_name') eq 'abbreviation') {
174 $abbreviation = $sp->get_column('value');
175 } elsif ($sp->get_column('cvterm_name') eq 'geolocation address') {
176 $address = $sp->get_column('value');
177 } else {
178 $attr{$sp->get_column('cvterm_name')} = $sp->get_column('value') ;
182 push @locations, [$s->nd_geolocation_id(), $s->description(), $s->latitude(), $s->longitude(), $s->altitude(), $country, $country_code, \%attr, $location_type, $abbreviation, $address],
185 return \@locations;
188 # CLASS METHOD!
190 =head2 class method get_all_project_types()
192 Usage: my @cvterm_ids = CXGN::Trial::get_all_project_types($schema)
193 Desc:
194 Ret:
195 Args:
196 Side Effects:
197 Example:
199 =cut
201 sub get_all_project_types {
202 ##my $class = shift;
203 my $schema = shift;
204 my $project_type_cv_id = $schema->resultset('Cv::Cv')->find( { name => 'project_type' } )->cv_id();
205 my $rs = $schema->resultset('Cv::Cvterm')->search( { cv_id=> $project_type_cv_id }, {order_by=>'me.cvterm_id'} );
206 my @cvterm_ids;
207 if ($rs->count() > 0) {
208 @cvterm_ids = map { [ $_->cvterm_id(), $_->name(), $_->definition ] } ($rs->all());
210 return @cvterm_ids;
214 =head2 function get_all_phenotype_metadata($schema, $n)
216 Note: Class method!
217 Usage: CXGN::Trial->get_phenotype_metadata($schema, 100);
218 Desc: retrieves maximally $n metadata.md_file entries for the any trial . These entries are created during StorePhenotypes.
219 Ret:
220 Args:
221 Side Effects:
222 Example:
224 =cut
226 sub get_all_phenotype_metadata {
227 my $class = shift;
228 my $schema = shift;
229 my $n = shift || 200;
230 my @file_array;
231 my %file_info;
232 my $q = "SELECT file_id, m.create_date, p.sp_person_id, p.username, basename, dirname, filetype FROM nd_experiment_project JOIN nd_experiment_phenotype USING(nd_experiment_id) JOIN phenome.nd_experiment_md_files ON (nd_experiment_phenotype.nd_experiment_id=nd_experiment_md_files.nd_experiment_id) LEFT JOIN metadata.md_files using(file_id) LEFT JOIN metadata.md_metadata as m using(metadata_id) LEFT JOIN sgn_people.sp_person as p ON (p.sp_person_id=m.create_person_id) WHERE m.obsolete = 0 and NOT (metadata.md_files.filetype='generated from plot from plant phenotypes') and NOT (metadata.md_files.filetype='direct phenotyping') ORDER BY file_id ASC LIMIT $n";
233 my $h = $schema->storage()->dbh()->prepare($q);
234 $h->execute();
236 while (my ($file_id, $create_date, $person_id, $username, $basename, $dirname, $filetype) = $h->fetchrow_array()) {
237 $file_info{$file_id} = [$file_id, $create_date, $person_id, $username, $basename, $dirname, $filetype];
239 foreach (keys %file_info){
240 push @file_array, $file_info{$_};
242 return \@file_array;