fix merge conflict
[sgn.git] / db / 00047 / UpdateSnpGenotypeCvterm.pm
blob0e341e05466305949b642533b7b7c7c89c11cae3
2 #!/usr/bin/env perl
5 =head1 NAME
7 UpdateSnpGenotypeCvterm
9 =head1 SYNOPSIS
11 mx-run ThisPackageName [options] -H hostname -D dbname -u username [-F]
13 this is a subclass of L<CXGN::Metadata::Dbpatch>
14 see the perldoc of parent class for more details.
16 =head1 DESCRIPTION
18 This patch moves all genotypes and genotypeprops of cvterm type "snp genotype" and cv name 'local' , to a "snp genotype" cvterm and genotype_property cv .
19 This is done to eliminate duplicates of snp genotype cvterms loaded previously in the different databases using different cv terms (null, local, genotype_property), making the cvterm name for snp genotype more explicit.
20 This also solves a potential conflict with the unique constraint in the dbxref table, since using the cvterm name "snp genotype" causes creating a dbxref.snp genotype of "autocreated:snp genotype" when creating properties using bio chado schema create_stock function.
21 The same snp genotyping will be attempted to be created when autocreating another property with the name "snp genotype".
23 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
25 =head1 AUTHOR
27 Guillaume Bauchet<gjb99@cornell.edu>
29 =head1 COPYRIGHT & LICENSE
31 Copyright 2010 Boyce Thompson Institute for Plant Research
33 This program is free software; you can redistribute it and/or modify
34 it under the same terms as Perl itself.
36 =cut
39 package UpdateSnpGenotypeCvterm;
41 use Moose;
42 use Bio::Chado::Schema;
43 use Try::Tiny;
45 extends 'CXGN::Metadata::Dbpatch';
48 has '+description' => ( default => <<'' );
49 This patch will find_or_create a snp genotyping cvterm
50 with cv of genotype_property
51 Then all genotypes and genotypeprops of type_id matching the word snp genotyping will be associated with the snp genotyping cvterm
52 this is important for making the snp genotyping cvterm unified across the different databases and eliminating redundancy
54 has '+prereq' => (
55 default => sub {
56 [],
60 sub patch {
61 my $self=shift;
63 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
65 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
67 print STDOUT "\nExecuting the SQL commands.\n";
68 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
71 #find or create cvterm with snp genotyping name and genotype_property cv
72 ##there might be an existing dbxref with snp genotyping = autocreated:snp genotyping
73 #~
74 my $coderef = sub {
76 my $snp_genotyping_cvterm = $schema->resultset("Cv::Cvterm")->create_with(
77 { name => 'snp genotyping',
78 cv => 'genotype_property',
79 });
81 my $snp_genotyping_cvterm_id = $snp_genotyping_cvterm->cvterm_id;
82 print "***snp_genotyping_cvterm_id is $snp_genotyping_cvterm_id \n";
85 #find all genotypes and genotypeprops that have a type_id ilike %snp_genotyping%' and change it to the
86 #genotype_property "snp genotyping" cvterm
87 # delete the old cvterm
89 my $genotypeprop_rs = $schema->resultset("Genetic::Genotypeprop")->search(
91 'type.name' => { ilike => 'snp%genotyping' },
92 'cv.name' => 'local'
95 join => {'type' => 'cv' },
99 print "** found " . $genotypeprop_rs->count . " genotypeprops with type_id of 'snp genotyping' and cv.name = 'local' \n\n";
100 print "**Changing genotypeprop.type_id to cvterm.name = 'snp genotyping' , cv= genotype_property ";
101 $genotypeprop_rs->update( { type_id => $snp_genotyping_cvterm_id});
104 my $genotype_rs = $schema->resultset("Genetic::Genotype")->search(
106 'type.name' => { ilike => 'snp%genotyping' },
107 'cv.name' => 'local'
110 join => { 'type' => 'cv' },
114 print "** found " . $genotype_rs->count . " genotypes with type_id of 'snp genotyping' and cv.name = 'local' \n\n";
115 print "**Changing genotype.type_id to cvterm.name = 'snp genotyping' , cv= genotype_property ";
116 $genotype_rs->update( { type_id => $snp_genotyping_cvterm_id });
120 my $old_cvterm = $schema->resultset("Cv::Cvterm")->search(
122 'me.name' => 'snp genotyping',
123 'cv.name' => 'local' ,
126 join => 'cv'
130 $old_cvterm->delete();
133 if ($self->trial) {
134 print "Trial mode! Rolling back transaction\n\n";
135 $schema->txn_rollback;
136 return 0;
138 return 1;
141 try {
142 $schema->txn_do($coderef);
144 } catch {
145 die "Load failed! " . $_ . "\n" ;
149 print "You're done!\n";
156 ####
157 1; #
158 ####