Merge pull request #4106 from solgenomics/topic/wishlist
[sgn.git] / bin / remove_parents.pl
blobe8c162a9885dc314a09079dc599112db1aadb9d5
2 use strict;
3 use Getopt::Std;
4 use DBI;
5 use Bio::Chado::Schema;
7 our ($opt_H, $opt_D);
8 getopts('H:D:');
10 my $file = shift;
12 print "Password for $opt_H / $opt_D: \n";
13 my $pw = <>;
14 chomp($pw);
16 print STDERR "Connecting to database...\n";
17 my $dsn = 'dbi:Pg:database='.$opt_D.";host=".$opt_H.";port=5432";
19 my $dbh = DBI->connect($dsn, "postgres", $pw);
21 print STDERR "Connecting to DBI schema...\n";
22 my $bcs_schema = Bio::Chado::Schema->connect($dsn, "postgres", $pw);
24 my $female_parent_type_id = $bcs_schema->resultset("Cv::Cvterm")->find( { name => "female_parent" })->cvterm_id();
26 my $male_parent_type_id = $bcs_schema->resultset("Cv::Cvterm")->find( { name=> "male_parent" })->cvterm_id();
28 my $total_count = 0;
30 open(my $F, "<", $file) || die " Can't open file $file\n";
31 while (<$F>) {
32 chomp;
34 my $stock = $_;
35 $stock =~ s/\r//g;
36 if (!$stock) {
37 next();
40 print STDERR "Processing $stock\n";
42 my $stock_row = $bcs_schema->resultset("Stock::Stock")->find( { uniquename => $stock });
44 if (!$stock_row) {
45 print STDERR "Could not find stock $stock. Skipping...\n";
46 next;
50 my $parent_rs = $bcs_schema->resultset("Stock::StockRelationship")->search( { object_id => $stock_row->stock_id(), type_id => { -in => [ $female_parent_type_id, $male_parent_type_id] } });
52 print STDERR "Found ".$parent_rs->count()." parents for stock $stock\n";
54 while (my $p = $parent_rs->next()) {
55 print STDERR "Removing parent with id ".$p->subject_id()."...\n";
56 $p->delete();
57 $total_count++;
61 print STDERR "Done. Total relationships deleted: $total_count.\n";