5 replace_plots.pl - a bulk script to replace accessions in field layouts
9 rename_stocks.pl -H [dbhost] -D [dbname] -i [infile] <-t>
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 -t (optional) test - do not modify the database.
20 This script replaces accessions in field layouts in bulk.
22 The infile provided has three columns:
24 1) the stock_id of the plot that needs to be changed.
25 2) the stock uniquename as it is in the database that is currently associated
26 3) the new stock uniquename (needs to be in the database).
27 4) optional: a new plot name
29 There is no header on the infile and the infile is .xls and .xlsx.
31 The script will dissociate the old accession in column 1 and associate the new accession. The plot name will be unchanged.
35 Lukas Mueller (lam87@cornell.edu)
37 Adapted from a cvterm renaming script by:
38 Guillaume Bauchet (gjb99@cornell.edu)
39 Nicolas Morales (nm529@cornell.edu)
49 use Spreadsheet
::ParseExcel
;
50 use Spreadsheet
::ParseXLSX
;
51 use Bio
::Chado
::Schema
;
52 use CXGN
::DB
::InsertDBH
;
55 our ($opt_H, $opt_D, $opt_i, $opt_t);
59 if (!$opt_H || !$opt_D || !$opt_i) {
60 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H (hostname), -D (database name), -i (input file)\n");
66 # Match a dot, extension .xls / .xlsx
67 my ($extension) = $opt_i =~ /(\.[^.]+)$/;
70 if ($extension eq '.xlsx') {
71 $parser = Spreadsheet
::ParseXLSX
->new();
74 $parser = Spreadsheet
::ParseExcel
->new();
77 my $excel_obj = $parser->parse($opt_i);
79 my $dbh = CXGN
::DB
::InsertDBH
->new({
82 dbargs
=> {AutoCommit
=> 1, RaiseError
=> 1}
85 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
86 $dbh->do('SET search_path TO public,sgn');
89 my $worksheet = ( $excel_obj->worksheets() )[0]; #support only one worksheet
90 my ( $row_min, $row_max ) = $worksheet->row_range();
91 my ( $col_min, $col_max ) = $worksheet->col_range();
94 for my $row ( 0 .. $row_max ) {
96 my $plot_name = $worksheet->get_cell($row, 0)->value();
97 my $plot_id = $worksheet->get_cell($row, 1)->value();
98 my $db_uniquename = $worksheet->get_cell($row,2)->value();
99 my $new_uniquename = $worksheet->get_cell($row,3)->value();
101 my $new_plotname = "";
103 $new_plotname = $worksheet->get_cell($row, 4)->value();
107 print STDER
"no alternate plot name provided. Keeping old name.\n";
110 print STDERR
"$db_uniquename -> $new_uniquename\n";
112 my $old_stock = $schema->resultset('Stock::Stock')->find({ uniquename
=> $db_uniquename });
113 my $new_stock = $schema->resultset('Stock::Stock')->find({ uniquename
=> $new_uniquename });
116 print STDERR
"Warning! Stock with uniquename $db_uniquename was not found in the database.\n";
120 print STDERR
"Warning! Stock with uniquename $new_uniquename was not found in the database.\n";
124 my $q = "select plot.uniquename, accession.uniquename, sr.stock_relationship_id, cvterm.name from stock as plot join stock_relationship as sr on (plot.stock_id=sr.subject_id) join stock as accession on (accession.stock_id=sr.object_id) join cvterm on (sr.type_id=cvterm.cvterm_id) where plot.stock_id=? and accession.uniquename=? and cvterm.name='plot_of'";
126 my $h = $dbh->prepare($q);
127 $h->execute($plot_id, $db_uniquename);
129 my ($plot, $acc, $stock_relationship_id) = $h->fetchrow_array();
131 #update stock_relationship row with new object_id...
132 my $uq = "UPDATE stock_relationship set object_id=? where stock_relationship_id=?";
133 my $uh = $dbh->prepare($uq);
134 print STDERR
"Changing object_id to ".$new_stock->stock_id()." for relationship $stock_relationship_id\n";
136 print STDERR
"Not updating because of -t option...\n";
139 $uh->execute($new_stock->stock_id, $stock_relationship_id);
143 print STDERR
"Renaming plot to $new_plotname...\n";
144 my $pq = "UPDATE stock SET uniquename=?, name=? WHERE stock_id=?";
145 my $ph = $dbh->prepare($pq);
146 $ph->execute($new_plotname, $new_plotname, $plot_id);
152 my $transaction_error;
154 $schema->txn_do($coderef);
156 $transaction_error = $_;
159 if ($transaction_error) {
160 print STDERR
"Transaction error storing terms: $transaction_error\n";
162 print STDERR
"Script Complete.\n";