5 remove_whitespace_from_names.pl - Find any stock, synonym, project, or location that is saved with leading or trailing whitespace. If opt s, update the names with the whitespace removed.
9 remove_whitespace_from_names.pl -H localhost -D cxgn -s
11 =head1 COMMAND-LINE OPTIONS
22 Bryan Ellerbrock bje24@cornell.edu
29 use Bio
::Chado
::Schema
;
30 use CXGN
::DB
::InsertDBH
;
34 our ($opt_H, $opt_D, $opt_s);
38 if (!$opt_H || !$opt_D) {
39 pod2usage
(-verbose
=> 2, -message
=> "Must provide options -H and -D\n");
45 my $dbh = CXGN
::DB
::InsertDBH
->new({
48 dbargs
=> {AutoCommit
=> 1, RaiseError
=> 1}
51 my $schema= Bio
::Chado
::Schema
->connect( sub { $dbh->get_actual_dbh() } );
52 $dbh->do('SET search_path TO public,sgn');
54 print STDERR
"Found the following entries with leading or trailing whitespace:\n";
55 print STDERR
"Name:\tType:\n";
58 my $stock_query = 'SELECT format( \'"%s"\', uniquename ) AS name, cvterm.name AS type FROM stock JOIN cvterm ON(type_id = cvterm_id) WHERE trim(uniquename) != uniquename';
59 my $stocks = $dbh->prepare($stock_query);
62 while (my ($name, $type) = $stocks->fetchrow_array()) {
63 print STDERR
$name . "\t" . $type ."\n";
67 my $synonym_query = 'SELECT format( \'"%s"\', value ) AS name, cvterm.name AS type FROM stockprop JOIN cvterm ON(type_id = cvterm_id) WHERE cvterm.name = \'stock_synonym\' AND trim(value) != value';
68 my $synonyms = $dbh->prepare($synonym_query);
71 while (my ($name, $type) = $synonyms->fetchrow_array()) {
72 print STDERR
$name . "\t" . $type ."\n";
76 my $project_query = 'SELECT format( \'"%s"\', name ) AS name FROM project WHERE trim(name) != name';
77 my $projects = $dbh->prepare($project_query);
80 while (my $name = $projects->fetchrow_array()) {
81 print STDERR
$name . "\tproject\n";
85 my $location_query = 'SELECT format( \'"%s"\', description) AS name FROM nd_geolocation WHERE trim(description) != description';
86 my $locations = $dbh->prepare($location_query);
87 $locations->execute();
89 while (my $name = $locations->fetchrow_array()) {
90 print STDERR
$name . "\tlocation\n";
96 my $merge_query = 'SELECT stock_id, trim(uniquename) FROM stock JOIN cvterm ON(type_id = cvterm_id) WHERE trim(uniquename) != uniquename AND trim(uniquename) IN (SELECT uniquename FROM stock)';
97 my $merges = $dbh->prepare($merge_query);
99 while (my ($merge_id, $trimmed_name) = $merges->fetchrow_array()) {
100 my $correct_stock = $schema->resultset("Stock::Stock")->find({ uniquename
=> $trimmed_name });
101 my $s = CXGN
::Stock
->new( schema
=> $schema, stock_id
=> $correct_stock->stock_id());
102 $s->merge($merge_id, 1);
105 my $stock_query = 'UPDATE stock SET uniquename = trim(uniquename) WHERE trim(uniquename) != uniquename';
106 my $stocks = $dbh->prepare($stock_query);
109 my $synonym_query = 'UPDATE stockprop SET value = trim(value) WHERE type_id = (SELECT cvterm_id from cvterm where cvterm.name = \'stock_synonym\') AND trim(value) != value';
110 my $synonyms = $dbh->prepare($synonym_query);
111 $synonyms->execute();
113 my $project_query = 'UPDATE project SET name = trim(name) WHERE trim(name) != name';
114 my $projects = $dbh->prepare($project_query);
115 $projects->execute();
117 my $location_query = 'UPDATE nd_geolocation SET description = trim(description) WHERE trim(description) != description';
118 my $locations = $dbh->prepare($location_query);
119 $locations->execute();