Merge branch 'master' into topic/upload_fieldbook_images
[sgn.git] / bin / merge_stocks.pl
blobbaa7bf0e746ef353376ae610ff4c8293c9d54474
2 =head1 NAME
4 merge_stocks.pl - merge stocks using a file with stocks to merge
6 =head1 DESCRIPTION
8 merge_stocks.pl -H [database host] -D [database name] [ -x ] mergefile.txt
10 Options:
12 -H the database host
13 -D the database name
14 -x flag; if present, delete the empty remaining accession
16 mergefile.txt: A file with three columns: first column is ignored, bad name, good name.
18 All the metadata of bad name will be transferred to good name.
19 If -x is used, stock with name bad name will be deleted.
21 =head1 AUTHOR
23 Lukas Mueller <lam87@cornell.edu>
25 =cut
27 use strict;
29 use Getopt::Std;
30 use CXGN::DB::InsertDBH;
31 use CXGN::DB::Schemas;
32 use CXGN::Chado::Stock;
34 our($opt_H, $opt_D, $opt_x);
35 getopts('H:D:x');
37 my $dbh = CXGN::DB::InsertDBH->new(
39 dbhost => $opt_H,
40 dbname => $opt_D ,
41 dbargs => {
42 AutoCommit => 0,
43 RaiseError => 1,
44 limit_dialect => 'LimitOffset',
46 });
48 my $delete_merged_stock = $opt_x;
50 print STDERR "Note: -x: Deleting stocks that have been merged into other stocks.\n";
52 my $s = CXGN::DB::Schemas->new({ dbh => $dbh });
53 my $schema = $s->bcs_schema();
54 my $file = shift;
56 open(my $F, "<", $file) || die "Can't open file $file.\n";
58 my $header = <$F>;
60 eval {
61 while (<$F>) {
62 chomp;
63 my ($line_count, $merge_stock_name, $good_stock_name) = split /\t/;
65 my $stock_row = $schema->resultset("Stock::Stock")->find( { uniquename => $good_stock_name });
66 if (!$stock_row) {
67 print STDERR "Stock $good_stock_name not found. Skipping..\n";
68 next();
71 my $merge_row = $schema->resultset("Stock::Stock")->find( { uniquename => $merge_stock_name });
72 if (!$merge_row) {
73 print STDERR "Stock $merge_stock_name not available for merging. Skipping\n";
74 next();
77 my $good_stock = CXGN::Chado::Stock->new($schema, $stock_row->stock_id);
78 my $merge_stock = CXGN::Chado::Stock->new($schema, $merge_row->stock_id);
80 print STDERR "Merging stock $merge_stock_name into $good_stock_name... ";
81 $good_stock->merge($merge_stock->get_stock_id(), $delete_merged_stock);
82 print STDERR "Done.\n";
86 if ($@) {
87 print STDERR "An ERROR occurred ($@). Rolling back changes...\n";
88 $dbh->rollback();
90 else {
91 print STDERR "Script is done. Committing... ";
92 $dbh->commit();
93 print STDERR "Done.\n";