6 CopyAccessionToStock.pm
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.
17 This is a patch for copying sgn accessions into the stock module.
18 sgn.accession should be deprecated! This will require some code refactoring like the genetic maps.
20 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
24 Naama Menda<nm249@cornell.edu>
26 =head1 COPYRIGHT & LICENSE
28 Copyright 2010 Boyce Thompson Institute for Plant Research
30 This program is free software; you can redistribute it and/or modify
31 it under the same terms as Perl itself.
36 package CopyAccessionToStock
;
39 extends
'CXGN::Metadata::Dbpatch';
40 use Bio
::Chado
::Schema
;
45 my $name = __PACKAGE__
;
46 print "dbpatch name is ':" . $name . "\n\n";
47 my $description = 'populate the chado stock table with sgn accessions';
48 my @previous_requested_patches = ('AddStockLinks');
50 $self->description($description);
51 $self->prereq(\
@previous_requested_patches);
58 print STDOUT
"Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
60 print STDOUT
"\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
62 my $schema = Bio
::Chado
::Schema
->connect( sub { $self->dbh->clone } );
65 my $q = "SELECT accession.accession_id, common_name, chado_organism_id , accession_name FROM sgn.accession JOIN sgn.accession_names USING (accession_name_id)";
66 my $sth = $self->dbh->prepare($q);
69 my $accession_cvterm = $schema->resultset("Cv::Cvterm")->create_with( {
73 dbxref
=> 'accession', });
75 my $accession_cvterm_id = $accession_cvterm->cvterm_id;
77 while ( my ($accession_id, $cname, $organism_id , $acc_name) = $sth->fetchrow_array) {
78 print "Storing new stock $acc_name ($cname)\n";
79 $cname = " ($cname)" if $cname;
80 my $stock = $schema->resultset("Stock::Stock")->find_or_create(
82 uniquename
=> $acc_name.$cname,
83 organism_id
=> $organism_id,
84 type_id
=> $accession_cvterm_id,
86 my $stockprop = $stock->create_stockprops( { stock_synonym
=> $cname } , {autocreate
=> 1 } );
87 #add the stock_id to the accession table
88 my $stock_id = $stock->stock_id;
89 my $q = "UPDATE sgn.accession SET stock_id = ? WHERE accession_id = ?";
90 my $sth = $self->dbh->prepare($q);
91 $sth->execute($stock_id, $accession_id);
94 print "You're done!\n";
96 print "Trial mode! Rolling back transaction\n\n";
97 $schema->txn_rollback;
103 $schema->txn_do($coderef);
104 print "Data committed! \n";
106 die "Load failed! " . $_ . "\n" ;