add video tutorial link to about menu.
[sgn.git] / bin / change_name_of_obsolete_stocks_matching_pattern.pl
blob43e3dfa4b5a532565ab66d77bbdb3705c4923cc1
1 #!/usr/bin/env perl
3 =head1
5 change_name_of_obsolete_stocks_matching_pattern.pl
7 =head1 SYNOPSIS
9 change_name_of_obsolete_stocks_matching_pattern.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 rename obsolete stocks\nUsage: change_name_of_obsolete_stocks_matching_pattern.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' => {like => $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."obsolete";
82 print STDERR "new name: $newname\n";
83 if ($stock->is_obsolete()) {
84 print STDERR "$stockid changing name $uniquename to $newname\n";
85 $stock->uniquename($newname);
86 $stock->update();