5 rename_trials.pl - a script for renaming trials
9 rename_trials.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 trials in bulk using an xls and xlsx files as input with two columns: the first column is the old projectname as it is in the database, and in the second column is the new projectname. There is no header line. Both stock.name and stock.projectname 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)
57 Srikanth Kumar K (sk2783@cornell.edu)
67 use Spreadsheet
::ParseExcel
;
68 use Spreadsheet
::ParseXLSX
;
69 use Bio
::Chado
::Schema
;
70 use CXGN
::DB
::InsertDBH
;
72 use SGN
::Model
::Cvterm
;
74 our ($opt_H, $opt_D, $opt_i, $opt_s, $opt_t, $opt_n);
76 getopts
('H:D:i:s:tn');
78 if (!$opt_H || !$opt_D || !$opt_i) {
79 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file)\n");
84 my $stock_type = $opt_s || "accession";
86 # Match a dot, extension .xls / .xlsx
87 my ($extension) = $opt_i =~ /(\.[^.]+)$/;
90 if ($extension eq '.xlsx') {
91 $parser = Spreadsheet
::ParseXLSX
->new();
94 $parser = Spreadsheet
::ParseExcel
->new();
97 my $excel_obj = $parser->parse($opt_i);
99 my $dbh = CXGN
::DB
::InsertDBH
->new({
102 dbargs
=> {AutoCommit
=> 0, RaiseError
=> 1}
105 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
106 $dbh->do('SET search_path TO public,sgn');
108 my $synonym_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_synonym', 'stock_property')->cvterm_id();
111 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
112 my ( $row_min, $row_max ) = $worksheet->row_range();
113 my ( $col_min, $col_max ) = $worksheet->col_range();
116 for my $row ( 0 .. $row_max ) {
118 my $db_projectname = $worksheet->get_cell($row,0)->value();
119 my $new_projectname = $worksheet->get_cell($row,1)->value();
121 print STDERR
"processing row $row: $db_projectname -> $new_projectname\n";
123 my $old_project = $schema->resultset('Project::Project')->find({ name
=> $db_projectname, projectname
=> $db_projectname });
126 print STDERR
"Warning! Stock with projectname $db_projectname was not found in the database.\n";
130 my $new_project = $old_project->update({ name
=> $new_projectname });
134 my $transaction_error;
138 $transaction_error = $_;
142 print STDERR
"Not storing with test flag (-t). Rolling back.\n";
143 $schema->txn_rollback();
145 elsif ($transaction_error) {
146 print STDERR
"Transaction error storing terms: $transaction_error. Rolling back.\n";
147 $schema->txn_rollback();
149 print STDERR
"Everything looks good. Committing.\n";
150 $schema->txn_commit();
151 print STDERR
"Script Complete.\n";