5 rename_stocks.pl - a script for renaming stocks
9 rename_stocks.pl -H [dbhost] -D [dbname] -i [infile]
11 =head2 Command-line options
17 host name (required) e.g. "localhost"
21 database name (required) e.g. "cxgn_cassava"
25 path to infile (required)
29 stock type (default: accession)
33 don't store old name as a synonym
37 test mode, do not commit changes.
43 This script renames stocks in bulk using an xls file as input with two columns: 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 line. Both stock.name and stock.uniquename fields will be changed to the new name.
45 The oldname will be stored as a synonym unless option -n is given.
49 Guillaume Bauchet (gjb99@cornell.edu)
51 Lukas Mueller <lam87@cornell.edu> (added -n option)
53 Adapted from a cvterm renaming script by:
55 Nicolas Morales (nm529@cornell.edu)
65 use Spreadsheet
::ParseExcel
;
66 use Bio
::Chado
::Schema
;
67 use CXGN
::DB
::InsertDBH
;
69 use SGN
::Model
::Cvterm
;
71 our ($opt_H, $opt_D, $opt_i, $opt_s, $opt_t, $opt_n);
73 getopts
('H:D:i:s:tn');
75 if (!$opt_H || !$opt_D || !$opt_i) {
76 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file)\n");
81 my $stock_type = $opt_s || "accession";
82 my $parser = Spreadsheet
::ParseExcel
->new();
83 my $excel_obj = $parser->parse($opt_i);
85 my $dbh = CXGN
::DB
::InsertDBH
->new({
88 dbargs
=> {AutoCommit
=> 0, RaiseError
=> 1}
91 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
92 $dbh->do('SET search_path TO public,sgn');
94 my $synonym_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_synonym', 'stock_property')->cvterm_id();
97 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
98 my ( $row_min, $row_max ) = $worksheet->row_range();
99 my ( $col_min, $col_max ) = $worksheet->col_range();
101 my $stock_type_row = $schema->resultset("Cv::Cvterm")->find( { name
=> $stock_type });
103 if (! $stock_type_row) { die "The stock type $stock_type is not in the database."; }
105 my $stock_type_id = $stock_type_row->cvterm_id();
108 for my $row ( 0 .. $row_max ) {
110 my $db_uniquename = $worksheet->get_cell($row,0)->value();
111 my $new_uniquename = $worksheet->get_cell($row,1)->value();
113 print STDERR
"processing row $row: $db_uniquename -> $new_uniquename\n";
115 my $old_stock = $schema->resultset('Stock::Stock')->find({ name
=> $db_uniquename, uniquename
=> $db_uniquename, type_id
=> $stock_type_id });
118 print STDERR
"Warning! Stock with uniquename $db_uniquename was not found in the database.\n";
122 my $new_stock = $old_stock->update({ name
=> $new_uniquename, uniquename
=> $new_uniquename});
124 print STDERR
"Storing old name ($db_uniquename) as synonym or stock with id ".$new_stock->stock_id()." and type_id $synonym_id...\n";
125 my $synonym = { value
=> $db_uniquename,
126 type_id
=> $synonym_id,
127 stock_id
=> $new_stock->stock_id(),
130 print STDERR
"find_or_create...\n";
131 $schema->resultset('Stock::Stockprop')->find_or_create($synonym);
132 print STDERR
"Done.\n";
137 my $transaction_error;
141 $transaction_error = $_;
145 print STDERR
"Not storing with test flag (-t). Rolling back.\n";
146 $schema->txn_rollback();
148 elsif ($transaction_error) {
149 print STDERR
"Transaction error storing terms: $transaction_error. Rolling back.\n";
150 $schema->txn_rollback();
152 print STDERR
"Everything looks good. Committing.\n";
153 $schema->txn_commit();
154 print STDERR
"Script Complete.\n";