4 delete_stocks.pl - delete stocks from a cxgn database
8 perl delete_stocks.pl -H [host] -D [dbname] -t (for testing) file
10 where the file contains a list of uniquenames specifying the stocks to be deleted, one per line.
12 If the -t flag is provided, the changes will be rolled back in the database.
14 Note that it may be possible that some stocks have additional connections, such as images, that this script does not delete yet, and so won't be able to delete those stocks.
18 Lukas Mueller <lam87@cornell.edu>
25 use Bio
::Chado
::Schema
;
26 use CXGN
::Phenome
::Schema
;
28 our ($opt_H, $opt_D, $opt_t);
33 print "Password for $opt_H / $opt_D: \n";
37 print STDERR
"Connecting to database...\n";
38 my $dsn = 'dbi:Pg:database='.$opt_D.";host=".$opt_H.";port=5432";
40 my $dbh = DBI
->connect($dsn, "postgres", $pw);
42 print STDERR
"Connecting to DBI schema...\n";
43 my $bcs_schema = Bio
::Chado
::Schema
->connect($dsn, "postgres", $pw);
44 my $phenome_schema = CXGN
::Phenome
::Schema
->connect($dsn, "postgres", $pw, { on_connect_do
=> ['set search_path to public,phenome;'] });
47 my $deleted_stock_count = 0;
48 my $stock_owner_count = 0;
49 my $missing_stocks = 0;
51 open(my $F, "<", $file) || die " Can't open file $file\n";
64 print STDERR
"Processing $stock\n";
66 my $stock_row = $bcs_schema->resultset("Stock::Stock")->find( { uniquename
=> $stock });
69 print STDERR
"Could not find stock $stock. Skipping...\n";
74 my $owner_rs = $phenome_schema->resultset("StockOwner")->search( { stock_id
=> $stock_row->stock_id() });
75 if ($owner_rs->count() > 1) {
76 print STDERR
"Weird. $stock has more than one owner.\n";
79 my $subject_relationship_rs = $bcs_schema->resultset("Stock::StockRelationship")->search( { object_id
=> $stock_row->stock_id() });
81 while (my $r = $subject_relationship_rs->next()) {
82 print STDERR
"Found object relationship with stock ".$r->subject_id()." of type ".$r->type_id()."\n";
85 my $object_relationship_rs = $bcs_schema->resultset("Stock::StockRelationship")->search( { subject_id
=> $stock_row->stock_id() });
86 while (my $r = $object_relationship_rs->next()) {
87 print STDERR
"Found subject relationship with stock ".$r->object_id()." of type ".$r->type_id()."\n";
90 while (my $owner_row = $owner_rs->next()) {
94 print STDERR
"Removing stockowner (".$owner_row->stock_id().")...\n";
98 print STDERR
"Could not delete owner of stock $stock because of: $@\stock";
102 $stock_owner_count++;
107 $stock_row->delete();
110 print STDERR
"Could not delete entry for stock $stock because of: $@\n";
113 $deleted_stock_count++;
118 print STDERR
"Done. Total stocks deleted: $deleted_stock_count of $stock_count stocks, and removed $stock_owner_count owner relationships. Stocks not found: $missing_stocks\n";