Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / db / 00133 / AddProtocoltoStoredNIRSRetroactively.pm
blob2add7bcc5efd2d8a090d25cf847d0b55cb606e4e
1 #!/usr/bin/env perl
4 =head1 NAME
6 AddProtocoltoStoredNIRSRetroactively
8 =head1 SYNOPSIS
10 mx-run AddProtocoltoStoredNIRSRetroactively [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 retroactively adds a protocol for the uploaded NIRS data
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
19 =head1 AUTHOR
21 =head1 COPYRIGHT & LICENSE
23 Copyright 2010 Boyce Thompson Institute for Plant Research
25 This program is free software; you can redistribute it and/or modify
26 it under the same terms as Perl itself.
28 =cut
31 package AddProtocoltoStoredNIRSRetroactively;
33 use Moose;
34 use Bio::Chado::Schema;
35 use Try::Tiny;
36 use SGN::Model::Cvterm;
37 use JSON;
38 extends 'CXGN::Metadata::Dbpatch';
41 has '+description' => ( default => <<'' );
42 This patch retroactively adds a protocol for the uploaded NIRS data
44 has '+prereq' => (
45 default => sub {
46 [],
51 sub patch {
52 my $self=shift;
54 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
56 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
58 print STDOUT "\nExecuting the SQL commands.\n";
59 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
61 my $high_dim_nirs_protocol_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'high_dimensional_phenotype_nirs_protocol', 'protocol_type')->cvterm_id();
62 my $high_dim_nirs_protocol_prop_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'high_dimensional_phenotype_protocol_properties', 'protocol_property')->cvterm_id();
64 my %nirs_protocol_prop = (device_type => 'SCIO');
66 my $protocol = $schema->resultset('NaturalDiversity::NdProtocol')->create({
67 name => 'NIRS Protocol',
68 type_id => $high_dim_nirs_protocol_cvterm_id,
69 nd_protocolprops => [{type_id => $high_dim_nirs_protocol_prop_cvterm_id, value => encode_json \%nirs_protocol_prop}]
70 });
71 my $protocol_id = $protocol->nd_protocol_id();
73 my $desc_q = "UPDATE nd_protocol SET description=? WHERE nd_protocol_id=?;";
74 my $desc_dbh = $schema->storage->dbh()->prepare($desc_q);
75 $desc_dbh->execute('Default NIRS protocol', $protocol_id);
77 my $protocol_query = "INSERT INTO nd_experiment_protocol ( nd_experiment_id, nd_protocol_id) VALUES (?,?);";
78 my $protocol_dbh = $schema->storage->dbh()->prepare($protocol_query);
80 my $q = "SELECT nd_experiment_id
81 FROM phenome.nd_experiment_md_json
82 JOIN metadata.md_json USING(json_id)
83 WHERE json_type='nirs_spectra';";
84 my $dbh = $schema->storage->dbh()->prepare($q);
85 $dbh->execute();
86 while (my ($nd_experiment_id) = $dbh->fetchrow_array()) {
87 $protocol_dbh->execute($nd_experiment_id, $protocol_id);
90 print "You're done!\n";
94 ####
95 1; #
96 ####