reference_id in locusgroup_member is a dbxref
[phenome.git] / db / 00002 / PhenomeAnnotationsToStock.pm
blob2a60cf70471c4a8fd0392c25a8d2425854bd8d36
1 #!/usr/bin/env perl
4 =head1 NAME
6 PhenomeAnnotationsToStock.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
17 This is a patch for loading cvterm annotation data in phenome.individual_dbxref in the stock module (stock_cvterm) and the evidence codes into stock_cvtermprop. Previous annotations loaded into stock_dbxref and stock_dbxrefprop will be deleted.
19 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
21 =head1 AUTHOR
23 Naama Menda<nm249@cornell.edu>
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2011 Boyce Thompson Institute for Plant Research
29 This program is free software; you can redistribute it and/or modify
30 it under the same terms as Perl itself.
32 =cut
35 package PhenomeAnnotationsToStock;
37 use Try::Tiny;
38 use Moose;
39 extends 'CXGN::Metadata::Dbpatch';
41 use Bio::Chado::Schema;
43 use CXGN::Phenome::Population;
44 use CXGN::Phenome::Individual;
45 use CXGN::Chado::Dbxref;
47 use CXGN::People::Person;
50 sub init_patch {
51 my $self=shift;
52 my $name = __PACKAGE__;
53 print "dbpatch name is : '" . $name . "'\n\n";
54 my $description = 'Loading the phenome individual annotations into stock_cvterm ';
55 my @previous_requested_patches = ('LoadPhenomeInStock', 'AddStockCvtermProp','PopulateStockPub'); #ADD HERE
57 $self->name($name);
58 $self->description($description);
59 $self->prereq(\@previous_requested_patches);
63 sub patch {
64 my $self=shift;
66 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
68 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
70 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } , { on_connect_do => ['SET search_path TO public;'], autocommit => 1 });
72 ###################################################################
73 my $pub_curator = $schema->resultset('Pub::Pub')->find( { title => 'curator' });
75 ##################
76 my $result = $schema->txn_do( sub {
77 print "loading ...\n";
78 # do this for GO, PO, SP - copy from stock_dbxref to stock cvterm, and the evidence codes copied to stock_cvtermprop.
79 my $stockdbxrefs = $schema->resultset("General::Db")->search( {
80 -or => [
81 name => 'GO',
82 name => 'PO',
83 name => 'SP',
84 ], } )
85 ->search_related('dbxrefs')->search_related('stock_dbxrefs');
86 while ( my $sd = $stockdbxrefs->next ) {
87 # for each stock_dbxref create a stock_cvterm
88 my $stock_cvterm = $sd->stock->find_or_create_related('stock_cvterms' , {
89 cvterm_id => $sd->dbxref->search_related('cvterm')->first->cvterm_id ,
90 pub_id => $pub_curator->pub_id } );
91 #print "Added cvterm for stock " . $sd->stock->name . "\n";
93 # get all the properties of the stock_dbxref , and load them into stock_cvtermprop
94 my $dprops = $sd->search_related('stock_dbxrefprops') ;
95 PROP: while ( my $dprop = $dprops->next ) {
96 my $cv_name = $dprop->type->cv->name;
97 my $type_name = $dprop->type->name;
98 my $db_name = $dprop->type->dbxref->db->name;
99 my $value = $dprop->value
100 or die "Invalid stock_dbxrefprop: ".Data::Dumper::Dumper({ $dprop->get_columns });
101 my $rank = $dprop->rank;
102 if ($cv_name eq 'local' ) {
103 # load the reference into stock_cvterm.pub_id
104 if ($type_name eq 'reference' ) {
105 my @pubs = $schema->resultset("General::Dbxref")
106 ->find({ dbxref_id => $value })
107 ->pubs_mm;
108 if( @pubs > 1 ) {
109 die "multiple pubs for stock_dbxrefprop ID ".$dprop->stock_dbxrefprop_id;
111 $stock_cvterm->update( { pub_id => $_->pub_id } ) for @pubs;
112 next PROP;
114 $stock_cvterm->create_stock_cvtermprops(
115 { $type_name => $value } , { cv_name =>'local' , autocreate=>1} ) if ( $value && $rank == 0) ;
116 #print "Added prop to stock_cvterm. type= $type_name , value = $value \n";
118 if ($cv_name eq 'relationship' || $cv_name eq 'evidence_code' ) {
119 $stock_cvterm->create_stock_cvtermprops(
120 { $cv_name => $dprop->type_id } , { cv_name =>$cv_name , db_name => $db_name} ) if $value && $rank == 0 ;
121 #print "Added prop to stock_cvterm. type= $cv_name , value = $type_name \n";
123 #delete the stock_dbxrefprop
124 $dprop->delete
126 # delete the stock_dbxref
127 $sd->delete;
129 if ( $self->trial ) {
130 print "Trial mode! Rolling back transaction.\n\n";
131 $schema->txn_rollback;
132 return 0;
133 } else {
134 print "Committing.\n";
135 return 1;
139 print $result ? "Patch applied successfully.\n" : "Patch not applied.\n";
142 return 1;