Merge pull request #1890 from solgenomics/topic/UpdateBrapiSearchDBlist
[sgn.git] / db / 00046 / UpdateStockSynonymCvterms.pm
blob0b1eac3b6e1c6248b217ed8ee266675ece019dfb
1 #!/usr/bin/env perl
4 =head1 NAME
6 UpdateStockSynonymCvterms
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
17 This patch moves all stockprops of cvterm type "synonym" to a "stock_synonym" cvterm, "stock_property" cv , and "autocreated:stock_synonym" dbxref.
18 This is done to eliminate redundant usage of synonym-like cvterms loaded in the different databases using different cv terms (null, local, stock_property), making the cvterm name for stock synonyms more explicit: "stock_synonym" instead of "synonym" which may be used for other CVs such as organism_property.
19 This also solves a potential conflict with the unique constraint in the dbxref table, since using the cvterm name "synonym" causes creating a dbxref.accession of "autocreated:synonym" when creating properties using BCS create_stockprops function. The same accession will be attempted to be created when autocreating another property with the name "synonym", e.g. create_organismprop (organism_synonym will be taken care of in another db_patch)
21 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
23 =head1 AUTHOR
25 Naama Menda<nm249@cornell.edu>
27 =head1 COPYRIGHT & LICENSE
29 Copyright 2010 Boyce Thompson Institute for Plant Research
31 This program is free software; you can redistribute it and/or modify
32 it under the same terms as Perl itself.
34 =cut
37 package UpdateStockSynonymCvterms;
39 use Moose;
40 use Bio::Chado::Schema;
41 use Try::Tiny;
43 extends 'CXGN::Metadata::Dbpatch';
46 has '+description' => ( default => <<'' );
47 This patch will find_or_create a cvterm name stock_synonym
48 with cv of stock_property and dbxref of autocreated:stock_synonym
49 Then all stockprop of type_id matching the word synonym will be associated with
50 the stock_synonym cvterm
51 this is important for making stock synonyms unified across the different databases and eliminating redundancy of cvterms with name = synonym
53 has '+prereq' => (
54 default => sub {
55 [],
59 sub patch {
60 my $self=shift;
62 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
64 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
66 print STDOUT "\nExecuting the SQL commands.\n";
67 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
70 #find or create cvterm with name stock_synonym
71 #make sure it has a dbxref of autocreated:stock_synonym and db = null
72 ##there might be an existing dbxref with accession = autocreated:synonym
73 #we will not be using that one anymore for distincting between synonyms of different entities.
74 my $coderef = sub {
76 my $stock_synonym_cvterm = $schema->resultset("Cv::Cvterm")->create_with( {
77 name => 'stock_synonym',
78 cv => 'stock_property', }
81 my $stock_synonym_cvterm_id = $stock_synonym_cvterm->cvterm_id;
82 print "***stock_synonym_cvterm_id is $stock_synonym_cvterm_id \n";
83 #find all stockprops that have a type_id ilike %synonym%' and change it to the
84 #stock_synonym cvterm
85 # delete the cvterm of name = synonym and cv name = "local" or "null"
87 my $stockprops = $schema->resultset("Stock::Stockprop")->search(
89 'type.name' => { ilike => 'synonym%' },
92 join => 'type'
96 print "** found " . $stockprops->count . " stockprops \n\n";
97 print "**Changing cvterm name of stock synonyms to stock_synonym , cv= stock_property ";
98 $stockprops->update( { type_id => $stock_synonym_cvterm_id } ) ;
100 if ($self->trial) {
101 print "Trial mode! Rolling back transaction\n\n";
102 $schema->txn_rollback;
103 return 0;
105 return 1;
108 try {
109 $schema->txn_do($coderef);
111 } catch {
112 die "Load failed! " . $_ . "\n" ;
116 print "You're done!\n";
120 ####
121 1; #
122 ####