fix conflict
[sgn.git] / bin / replace_string_in_stock_name.pl
blob8b07283916c407fca424481b25d48fb8d64dc4df
1 #!/usr/bin/env perl
3 =head1
5 replace_string_stock_name.pl
7 =head1 SYNOPSIS
9 replace_string_stock_name.pl -H [dbhost] -D [dbname] -f "Ug" -r "UG"
11 =head1 COMMAND-LINE OPTIONS
13 -H host name
14 -D database name
15 -f string to find
16 -r replace string with this
18 =head2 DESCRIPTION
21 =head2 AUTHOR
23 Jeremy D. Edwards (jde22@cornell.edu)
25 April 2014
27 =head2 TODO
29 Add support for other spreadsheet formats
31 =cut
33 use strict;
34 use warnings;
36 use lib 'lib';
37 use Getopt::Std;
38 use Bio::Chado::Schema;
39 use CXGN::DB::InsertDBH;
40 use CXGN::DB::Connection;
44 our ($opt_H, $opt_D, $opt_f, $opt_r);
45 getopts('H:D:f:r:');
48 sub print_help {
49 print STDERR "A script to find and replace strings in stock names\nUsage: replace_string_stock_name.pl -D [database name] -H [database host, e.g., localhost] -f [find string] -r [replace string with this]\n";
53 if (!$opt_D || !$opt_H || !$opt_f ) {
54 print_help();
55 die("Exiting: options missing\n");
58 my $dbh = CXGN::DB::InsertDBH
59 ->new({
60 dbname => $opt_D,
61 dbhost => $opt_H,
62 dbargs => {AutoCommit => 1,
63 RaiseError => 1},
64 });
66 my $chado_schema = Bio::Chado::Schema->connect( sub { $dbh->get_actual_dbh() } );
68 my $rs = $chado_schema->resultset('Stock::Stock')->search({'uniquename' => {ilike => $opt_f.'%'}});
70 my $count = 0;
71 foreach my $stock ($rs->all()) {
72 print STDERR "Original name: ".$stock->uniquename()."\n";
73 $count++;
75 print STDERR "\nFound $count accessions\n";
77 if ($opt_r) {
78 foreach my $stock ($rs->all()) {
79 my $stockid = $stock->stock_id();
80 my $uniquename = $stock->uniquename();
81 my $newname = $uniquename;
82 $newname =~ s/^$opt_f/$opt_r/i;
83 print STDERR "new name: $newname\n";
84 if ($newname ne $uniquename) {
85 print STDERR "$stockid changing name $uniquename to $newname\n";
86 $stock->uniquename($newname);
87 $stock->update();