Merge branch 'master' into topic/tracking_transformation
[sgn.git] / db / 00106 / AddMissingProgramPropsToLocations.pm
blobbc2be74a6f50ea1d0a060babc624eac98cc0ed08
1 #!/usr/bin/env perl
4 =head1 NAME
6 AddMissingProgramPropsToLocations.pm
8 =head1 SYNOPSIS
10 mx-run ThisPackageName [options] -H hostname -D dbname -u username [-F]
12 this is a subclass of L<CXGN::Metadata::Dbpatch>
13 see the perldoc of parent class for more details.
15 =head1 DESCRIPTION
16 This patch finds locations that are missing a program prop, and adds the correct program if it can be inferred based on the trials that have been held at the location.
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
19 =head1 AUTHOR
21 Bryan Ellerbrock
23 =head1 COPYRIGHT & LICENSE
25 Copyright 2010 Boyce Thompson Institute for Plant Research
27 This program is free software; you can redistribute it and/or modify
28 it under the same terms as Perl itself.
30 =cut
33 package AddMissingProgramPropsToLocations;
35 use Moose;
36 use Bio::Chado::Schema;
37 use SGN::Model::Cvterm;
38 use Try::Tiny;
39 extends 'CXGN::Metadata::Dbpatch';
42 has '+description' => ( default => <<'' );
43 This patch finds locations that are missing a program prop, and adds the correct program if it can be inferred based on the trials that have been held at the location.
45 has '+prereq' => (
46 default => sub {
47 [],
52 sub patch {
53 my $self=shift;
55 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
57 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
59 print STDOUT "\nExecuting the SQL commands.\n";
61 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
63 my $coderef = sub {
64 print STDERR "Updating location breeding programs . . .\n";
66 my $query = "SELECT nd_geolocation.nd_geolocation_id, project.project_id, nd_geolocation.description, project.name
67 FROM nd_geolocation
68 JOIN nd_experiment using(nd_geolocation_id)
69 JOIN nd_experiment_project using(nd_experiment_id)
70 JOIN project_relationship on (nd_experiment_project.project_id = subject_project_id and project_relationship.type_id = (select cvterm_id from cvterm where name = 'breeding_program_trial_relationship'))
71 JOIN project on (project_relationship.object_project_id = project.project_id)
72 GROUP by 1,2,3,4";
74 my $h = $schema->storage->dbh->prepare($query);
75 $h->execute();
77 my $breeding_program_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'breeding_program', 'project_property')->cvterm_id();
78 my @locations;
79 while (my ($location_id, $program_id, $location_name, $program_name) = $h->fetchrow_array()) {
80 print STDERR "Trials from $program_name have been held at location $location_name. Checking to see if these are already linked through nd_geolocationprop\n";
81 my $location = $schema->resultset("NaturalDiversity::NdGeolocation")->find( { nd_geolocation_id => $location_id });
82 my $rs = $schema->resultset("NaturalDiversity::NdGeolocationprop")->search({ nd_geolocation_id=> $location_id, value => $program_id, type_id => $breeding_program_cvterm_id });
84 if($rs->next()) {
85 #skip, this program - location link is already stored in nd_geolocationprop
86 } else {
87 print STDERR "No prop found, linking $program_name to $location_name in nd_geolocationprop\n";
88 my $count = $schema->resultset("NaturalDiversity::NdGeolocationprop")->search({ nd_geolocation_id=> $location_id, type_id => $breeding_program_cvterm_id })->count();
89 my $stored_location = $location->create_geolocationprops({ 'breeding_program' => $program_id }, {cv_name => 'project_property', rank => $count });
94 try {
95 $schema->txn_do($coderef);
96 } catch {
97 die " patch failed! " . $_ . "\n" ;
100 print "You're done!\n";
105 ####
106 1; #
107 ####