5 rename_stocks.pl - a script for renaming stocks
9 rename_stocks.pl -H [dbhost] -D [dbname] -i [infile]
11 =head1 COMMAND-LINE OPTIONS
13 -H host name (required) e.g. "localhost"
14 -D database name (required) e.g. "cxgn_cassava"
15 -i path to infile (required)
16 -s stock type (default: accession)
17 -t test mode, do not commit changes.
21 This script rename stocks in bulk. The infile provided has two columns, in the first column is the stock uniquename as it is in the database, and in the second column is the new stock uniquename. There is no header on the infile and the infile is .xls. The stock.name field is untouched.
25 Guillaume Bauchet (gjb99@cornell.edu)
27 Adapted from a cvterm renaming script by:
28 Nicolas Morales (nm529@cornell.edu)
38 use Spreadsheet
::ParseExcel
;
39 use Bio
::Chado
::Schema
;
40 use CXGN
::DB
::InsertDBH
;
43 our ($opt_H, $opt_D, $opt_i, $opt_s, $opt_t);
47 if (!$opt_H || !$opt_D || !$opt_i) {
48 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file)\n");
53 my $stock_type = $opt_s || "accession";
54 my $parser = Spreadsheet
::ParseExcel
->new();
55 my $excel_obj = $parser->parse($opt_i);
57 my $dbh = CXGN
::DB
::InsertDBH
->new({
60 dbargs
=> {AutoCommit
=> 0, RaiseError
=> 1}
63 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
64 $dbh->do('SET search_path TO public,sgn');
67 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
68 my ( $row_min, $row_max ) = $worksheet->row_range();
69 my ( $col_min, $col_max ) = $worksheet->col_range();
71 my $stock_type_row = $schema->resultset("Cv::Cvterm")->find( { name
=> $stock_type });
73 if (! $stock_type_row) { die "The stock type $stock_type is not in the database."; }
75 my $stock_type_id = $stock_type_row->cvterm_id();
78 for my $row ( 0 .. $row_max ) {
80 my $db_uniquename = $worksheet->get_cell($row,0)->value();
81 my $new_uniquename = $worksheet->get_cell($row,1)->value();
83 print STDERR
"$db_uniquename -> $new_uniquename\n";
85 my $old_stock = $schema->resultset('Stock::Stock')->find({ uniquename
=> $db_uniquename, type_id
=> $stock_type_id });
88 print STDERR
"Warning! Stock with uniquename $db_uniquename was not found in the database.\n";
91 my $new_stock = $old_stock->update({ name
=> $new_uniquename, uniquename
=> $new_uniquename});
96 my $transaction_error;
100 $transaction_error = $_;
104 print STDERR
"Not storing with test flag (-t). Rolling back.\n";
105 $schema->txn_rollback();
107 elsif ($transaction_error) {
108 print STDERR
"Transaction error storing terms: $transaction_error. Rolling back.\n";
109 $schema->txn_rollback();
111 print STDERR
"Everything looks good. Committing.\n";
112 $schema->txn_commit();
113 print STDERR
"Script Complete.\n";