6 AddMissingProgramPropsToLocations.pm
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.
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>
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.
33 package AddMissingProgramPropsToLocations
;
36 use Bio
::Chado
::Schema
;
37 use SGN
::Model
::Cvterm
;
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
.
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 } );
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
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)
74 my $h = $schema->storage->dbh->prepare($query);
77 my $breeding_program_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'breeding_program', 'project_property')->cvterm_id();
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 });
85 #skip, this program - location link is already stored in nd_geolocationprop
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 });
95 $schema->txn_do($coderef);
97 die " patch failed! " . $_ . "\n" ;
100 print "You're done!\n";