6 AdaptToNewGenotypeStorage
10 mx-run AdaptToNewGenotypeStorage [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.
16 This patch adapts to the new genotype storage by changing nd_protocolprop.value to JSONB and by changing the 'snp genotyping' values in genotypeprop to {'markername1' : {'DS' : '1'}, 'markername2' : {'DS' : '0'}, ... } and by changing the type of these genotypeprops to 'vcf_snp_genotyping'
17 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
22 =head1 COPYRIGHT & LICENSE
24 Copyright 2010 Boyce Thompson Institute for Plant Research
26 This program is free software; you can redistribute it and/or modify
27 it under the same terms as Perl itself.
32 package AdaptToNewGenotypeStorage
;
35 use Bio
::Chado
::Schema
;
37 use SGN
::Model
::Cvterm
;
39 extends
'CXGN::Metadata::Dbpatch';
42 has
'+description' => ( default => <<'' );
43 This patch adapts to the new genotype storage by changing nd_protocolprop
.value to JSONB
and by changing the
'snp genotyping' values in genotypeprop to
{'markername1' : {'DS' : '1'}, 'markername2' : {'DS' : '0'}, ... } and by changing the type of these genotypeprops to
'vcf_snp_genotyping'
55 print STDOUT
"Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
57 print STDOUT
"\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
59 print STDOUT
"\nExecuting the SQL commands.\n";
60 my $schema = Bio
::Chado
::Schema
->connect( sub { $self->dbh->clone } );
62 my $snp_genotyping_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'snp genotyping', 'genotype_property')->cvterm_id();
63 my $vcf_snp_genotyping_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'vcf_snp_genotyping', 'genotype_property')->cvterm_id();
67 ALTER TABLE nd_protocolprop ALTER COLUMN value TYPE JSONB USING value::JSON;
69 $schema->storage->dbh->do($sql);
71 my $pre_q = "SELECT genotype_id FROM genotype;";
72 my $q = "SELECT genotypeprop_id, value FROM genotypeprop WHERE type_id = $snp_genotyping_cvterm_id AND genotype_id = ?;";
73 my $update1_q = "UPDATE genotypeprop SET value = ? WHERE genotypeprop_id = ?;";
74 my $update2_q = "UPDATE genotypeprop SET type_id = $vcf_snp_genotyping_cvterm_id WHERE genotypeprop_id = ?;";
76 my $pre_h = $schema->storage->dbh()->prepare($pre_q);
77 my $h = $schema->storage->dbh()->prepare($q);
78 my $h_update1 = $schema->storage->dbh()->prepare($update1_q);
79 my $h_update2 = $schema->storage->dbh()->prepare($update2_q);
82 while (my ($genotype_id) = $pre_h->fetchrow_array()) {
83 $h->execute($genotype_id);
84 while (my ($genotypeprop_id, $json_val) = $h->fetchrow_array()) {
85 print STDERR
"Converting $genotypeprop_id \n";
86 my $val = decode_json
$json_val;
88 while (my ($marker_name, $dosage_value) = each %$val) {
89 $new_val{$marker_name} = {'DS' => $dosage_value};
91 my $genotypeprop_json = encode_json \
%new_val;
92 $h_update1->execute($genotypeprop_json, $genotypeprop_id);
93 $h_update2->execute($genotypeprop_id);
98 my $transaction_error;
100 $schema->txn_do($coderef);
102 $transaction_error = $_;
104 if ($transaction_error){
105 print STDERR
"ERROR: $transaction_error\n";
107 print "You're done!\n";