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 and xlsx files 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 Spreadsheet
::ParseXLSX
;
67 use Bio
::Chado
::Schema
;
68 use CXGN
::DB
::InsertDBH
;
70 use SGN
::Model
::Cvterm
;
72 our ($opt_H, $opt_D, $opt_i, $opt_s, $opt_t, $opt_n);
74 getopts
('H:D:i:s:tn');
76 if (!$opt_H || !$opt_D || !$opt_i) {
77 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file)\n");
82 my $stock_type = $opt_s || "accession";
84 # Match a dot, extension .xls / .xlsx
85 my ($extension) = $opt_i =~ /(\.[^.]+)$/;
88 if ($extension eq '.xlsx') {
89 $parser = Spreadsheet
::ParseXLSX
->new();
92 $parser = Spreadsheet
::ParseExcel
->new();
95 my $excel_obj = $parser->parse($opt_i);
97 my $dbh = CXGN
::DB
::InsertDBH
->new({
100 dbargs
=> {AutoCommit
=> 0, RaiseError
=> 1}
103 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
104 $dbh->do('SET search_path TO public,sgn');
106 my $synonym_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_synonym', 'stock_property')->cvterm_id();
109 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
110 my ( $row_min, $row_max ) = $worksheet->row_range();
111 my ( $col_min, $col_max ) = $worksheet->col_range();
113 my $stock_type_row = $schema->resultset("Cv::Cvterm")->find( { name
=> $stock_type });
115 if (! $stock_type_row) { die "The stock type $stock_type is not in the database."; }
117 my $stock_type_id = $stock_type_row->cvterm_id();
120 for my $row ( 0 .. $row_max ) {
122 my $db_uniquename = $worksheet->get_cell($row,0)->value();
123 my $new_uniquename = $worksheet->get_cell($row,1)->value();
125 print STDERR
"processing row $row: $db_uniquename -> $new_uniquename\n";
127 my $old_stock = $schema->resultset('Stock::Stock')->find({ name
=> $db_uniquename, uniquename
=> $db_uniquename, type_id
=> $stock_type_id });
130 print STDERR
"Warning! Stock with uniquename $db_uniquename was not found in the database.\n";
134 my $new_stock = $old_stock->update({ name
=> $new_uniquename, uniquename
=> $new_uniquename});
136 print STDERR
"Storing old name ($db_uniquename) as synonym or stock with id ".$new_stock->stock_id()." and type_id $synonym_id...\n";
137 my $synonym = { value
=> $db_uniquename,
138 type_id
=> $synonym_id,
139 stock_id
=> $new_stock->stock_id(),
142 print STDERR
"find_or_create...\n";
143 $schema->resultset('Stock::Stockprop')->find_or_create($synonym);
144 print STDERR
"Done.\n";
149 my $transaction_error;
153 $transaction_error = $_;
157 print STDERR
"Not storing with test flag (-t). Rolling back.\n";
158 $schema->txn_rollback();
160 elsif ($transaction_error) {
161 print STDERR
"Transaction error storing terms: $transaction_error. Rolling back.\n";
162 $schema->txn_rollback();
164 print STDERR
"Everything looks good. Committing.\n";
165 $schema->txn_commit();
166 print STDERR
"Script Complete.\n";