Merge pull request #4933 from solgenomics/fix-startup-warnings-p1
[sgn.git] / bin / load_composed_cvprops.pl
bloba919b9c53e185e7204e8ead1f3419e8a56bb6c57
1 #!/usr/bin/env perl
3 =head1
5 load_composed_cvprops.pl
7 =head1 SYNOPSIS
9 load_composed_cvprops.pl -H [dbhost] -D [dbname] -T [trait_ontology cv name] -c [composed_trait_ontology cv name] -o [object_ontology cv name] -a [attribute_ontology cv name] -m [method_ontology cv name] -u [unit_ontology cv name] -t [time_ontology cv name]
11 =head1 COMMAND-LINE OPTIONS
13 -H host name
14 -D database name
16 optional:
17 -T [trait_ontology cv name]
18 -c [composed_trait_ontology cv name]
19 -o [object_ontology cv name]
20 -a [attribute_ontology cv name]
21 -m [method_ontology cv name]
22 -u [unit_ontology cv name]
23 -t [time_ontology cv name]
25 =head2 DESCRIPTION
28 =head2 AUTHOR
30 Bryan Ellerbrock (bje24@cornell.edu)
32 Feb 2017
34 =cut
36 use strict;
37 use warnings;
38 use Data::Dumper;
39 use Getopt::Std;
40 use Bio::Chado::Schema;
41 use CXGN::DB::InsertDBH;
42 use CXGN::DB::Connection;
43 use Try::Tiny;
45 our ( $opt_H, $opt_D, $opt_T, $opt_c, $opt_o, $opt_a, $opt_m, $opt_u, $opt_t );
46 getopts('H:D:T:c:o:a:m:u:t:');
48 sub print_help {
49 print STDERR
50 "A script to load composed cvprops\nUsage: load_composed_cvprops.pl -H [dbhost] -D [dbname] -T [trait_ontology cv name] -c [composed_trait_ontology cv name] -o [object_ontology cv name] -a [attribute_ontology cv name] -m [method_ontology cv name] -u [unit_ontology cv name] -t [time_ontology cv name] \n";
53 if ( !$opt_D || !$opt_H ) {
54 print_help();
55 die("Exiting: -H [dbhost] and -D [dbname] options missing\n");
58 my $dbh = CXGN::DB::InsertDBH->new(
60 dbname => $opt_D,
61 dbhost => $opt_H,
62 dbargs => {
63 AutoCommit => 1,
64 RaiseError => 1
69 my $schema = Bio::Chado::Schema->connect( sub { $dbh->get_actual_dbh() } );
71 my %cvprop_hash = (
72 trait_ontology => $opt_T,
73 composed_trait_ontology => $opt_c,
74 object_ontology => $opt_o,
75 attribute_ontology => $opt_a,
76 method_ontology => $opt_m,
77 unit_ontology => $opt_u,
78 time_ontology => $opt_t
81 my $coderef = sub {
83 my $composable_cvtypes =
84 $schema->resultset("Cv::Cv")->find( { name => 'composable_cvtypes' } );
85 if ( !$composable_cvtypes ) {
86 print STDERR
87 "No cv found for composable_cvtypes in database '$opt_D'.\n Run patch AddComposedCvtypeCv.pm on this database to load missing composable_cvtype cv.";
90 my ( $ontology, $ontology_cvtype, $new_ontology_cvprop );
91 while ( my ( $key, $value ) = each %cvprop_hash ) {
93 if ($value) {
94 $ontology =
95 $schema->resultset("Cv::Cv")->find( { name => $value } );
97 if ( !$ontology ) {
98 print STDERR
99 "No cv was found with the name '$value' in database '$opt_D'.\n";
102 $ontology_cvtype = $schema->resultset("Cv::Cvterm")->find(
104 name => $key,
105 cv_id => $composable_cvtypes->cv_id()
109 if ( !$ontology_cvtype ) {
110 print STDERR
111 "No term found for composable_cvtype '$key' in database '$opt_D'.\n Skipping cv '$value'.\n Run patch AddComposedCvtypeCv.pm on this database to load missing composable_cvtypes.\n";
112 next;
115 $new_ontology_cvprop =
116 $schema->resultset("Cv::Cvprop")->find_or_new(
118 cv_id => $ontology->cv_id(),
119 type_id => $ontology_cvtype->cvterm_id()
123 if ( !$new_ontology_cvprop->in_storage ) {
124 print STDERR
125 "Giving cv with name '$value' the cvprop '$key'... \n";
126 $new_ontology_cvprop->insert;
128 else {
129 print STDERR
130 "The Cv with name '$value' already has the cvprop '$key'... \n";
137 try {
138 $schema->txn_do($coderef);
141 catch {
142 die "Load failed! " . $_ . "\n";
145 print "You're done!\n";