Merge pull request #1927 from solgenomics/topic/slurm_backend2
[sgn.git] / bin / merge_stocks.pl
blobc9b7225bd5937cf2c4cd82cbd1d226d8b954a9c9
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: 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 ($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";
69 next();
72 my $merge_row = $schema->resultset("Stock::Stock")->find( { uniquename => $merge_stock_name } );
73 if (!$merge_row) {
74 print STDERR "Stock $merge_stock_name not available for merging. Skipping\n";
75 next();
78 my $good_stock = CXGN::Chado::Stock->new($schema, $stock_row->stock_id);
79 my $merge_stock = CXGN::Chado::Stock->new($schema, $merge_row->stock_id);
81 print STDERR "Merging stock $merge_stock_name into $good_stock_name... ";
82 $good_stock->merge($merge_stock->get_stock_id(), $delete_merged_stock);
83 print STDERR "Done.\n";
87 if ($@) {
88 print STDERR "An ERROR occurred ($@). Rolling back changes...\n";
89 $dbh->rollback();
91 else {
92 print STDERR "Script is done. Committing... ";
93 $dbh->commit();
94 print STDERR "Done.\n";