4 merge_stocks.pl - merge stocks using a file with stocks to merge
8 merge_stocks.pl -H [database host] -D [database name] [ -x ] mergefile.txt
14 -x flag; if present, delete the empty remaining accession
16 mergefile.txt: A tab-separated file with two columns. Include the following header as the first line: 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.
23 Lukas Mueller <lam87@cornell.edu>
30 use CXGN
::DB
::InsertDBH
;
31 use CXGN
::DB
::Schemas
;
35 our($opt_H, $opt_D, $opt_x);
39 print "Password for $opt_H / $opt_D: \n";
43 my $delete_merged_stock = $opt_x;
45 print STDERR
"Note: -x: Deleting stocks that have been merged into other stocks.\n";
47 print STDERR
"Connecting to database...\n";
48 my $dsn = 'dbi:Pg:database='.$opt_D.";host=".$opt_H.";port=5432";
50 my $dbh = DBI
->connect($dsn, "postgres", $pw, { AutoCommit
=> 0, RaiseError
=>1 });
52 print STDERR
"Connecting to DBI schema...\n";
53 my $bcs_schema = Bio
::Chado
::Schema
->connect($dsn, "postgres", $pw);
55 my $s = CXGN
::DB
::Schemas
->new({ dbh
=> $dbh });
56 my $schema = $s->bcs_schema();
59 open(my $F, "<", $file) || die "Can't open file $file.\n";
62 print STDERR
"Skipping header line $header\n";
65 print STDERR
"Read line: $_\n";
67 my ($merge_stock_name, $good_stock_name) = split /\t/;
68 print STDERR
"bad name: $merge_stock_name, good name: $good_stock_name\n";
69 my $stock_row = $schema->resultset("Stock::Stock")->find( { uniquename
=> $good_stock_name } );
71 print STDERR
"Stock $good_stock_name not found. Skipping...\n";
76 my $merge_row = $schema->resultset("Stock::Stock")->find( { uniquename
=> $merge_stock_name } );
78 print STDERR
"Stock $merge_stock_name not available for merging. Skipping\n";
82 my $good_stock = CXGN
::Stock
->new( { schema
=> $schema, stock_id
=> $stock_row->stock_id });
83 my $merge_stock = CXGN
::Stock
->new( { schema
=> $schema, stock_id
=> $merge_row->stock_id });
85 print STDERR
"Merging stock $merge_stock_name into $good_stock_name... ";
86 $good_stock->merge($merge_stock->stock_id(), $delete_merged_stock);
87 print STDERR
"Done.\n";
92 print STDERR
"An ERROR occurred ($@). Rolling back changes...\n";
96 print STDERR
"Script is done. Committing... ";
98 print STDERR
"Done.\n";