Merge pull request #2510 from solgenomics/topic/solgs/job-lineup-0519
[sgn.git] / bin / delete_stocks.pl
blob5d6aaf15bd97b95c406cab5c3455e9046d69af24
2 =head1 NAME
4 delete_stocks.pl - delete stocks from a cxgn database
6 =head1 DESCRIPTION
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.
16 =head1 AUTHOR
18 Lukas Mueller <lam87@cornell.edu>
20 =cut
22 use strict;
23 use Getopt::Std;
24 use DBI;
25 use Bio::Chado::Schema;
26 use CXGN::Phenome::Schema;
28 our ($opt_H, $opt_D, $opt_t);
29 getopts('H:D:t');
31 my $file = shift;
33 print "Password for $opt_H / $opt_D: \n";
34 my $pw = <>;
35 chomp($pw);
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;'] });
46 my $stock_count = 0;
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";
53 while (<$F>) {
54 chomp;
56 my $stock = $_;
57 $stock =~ s/\r//g;
58 if (!$stock) {
59 next();
62 $stock_count++;
64 print STDERR "Processing $stock\n";
66 my $stock_row = $bcs_schema->resultset("Stock::Stock")->find( { uniquename => $stock });
68 if (!$stock_row) {
69 print STDERR "Could not find stock $stock. Skipping...\n";
70 $missing_stocks++;
71 next;
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()) {
92 if (! $opt_t) {
93 eval {
94 print STDERR "Removing stockowner (".$owner_row->stock_id().")...\n";
95 $owner_row->delete();
97 if ($@) {
98 print STDERR "Could not delete owner of stock $stock because of: $@\stock";
102 $stock_owner_count++;
105 if (! $opt_t) {
106 eval {
107 $stock_row->delete();
109 if ($@) {
110 print STDERR "Could not delete entry for stock $stock because of: $@\n";
112 else {
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";