scripts/unload.pl untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 151 CC 14 SCAR 46.9
CC
14
Cov
0%
remove_set
182.0
notfound
6.0
BEGIN
2.0
CRAP 108.7
1 # removes the specified connectorset
2  
3 use Carp;
4 use lib qw( blib/lib/ ../lib ../../..);
5 use Getopt::Long;
6 use File::Path;
7 use Bio::ConnectDots::DB;
8 use Bio::ConnectDots::Config;
9 use strict;
10  
11 my($HELP,$VERBOSE,$ECHO_CMD,$DATABASE,$HOST,$USER,$PASSWORD,$LOADDIR,$LOADSAVE,$CONNECTORSET,$REMOVEDOTS,$VERSION);
12  
13 GetOptions ('help' => \$HELP,
14 'verbose' => \$VERBOSE,
15 'X|echo' => \$ECHO_CMD,
16 'database=s'=>\$DATABASE,
17 'db=s'=>\$DATABASE,
18 'host=s'=>\$HOST,
19 'user=s'=>\$USER,
20 'password=s'=>\$PASSWORD,
21 'loaddir=s'=>\$LOADDIR,
22 'loadsave=s'=>\$LOADSAVE,
23 'connectorset=s'=>\$CONNECTORSET,
24 'removedots=s'=>\$REMOVEDOTS,
25 'version=s'=>\$VERSION,
26 ) and !$HELP or die <<USAGE;
27 Usage: $0 [options] cnf_file data_file
28  
29 Load LocusLink into Connect-the-Dots database
30  
31 Options
32 -------
33 --help Print this message
34 --verbose (for testing)
35 -X or --echo Echo command line (for testing and use in scripts)
36 --database Postgres database (default: --user)
37 --db Synonym for --database
38 --host Postgres database (default: socks)
39 --user Postgres user (default: ngoodman)
40 --password Postgres password (default: undef)
41 --loaddir Directory for load files (default: /usr/tmp/user_name)
42 --loadsave Specifies whether to save load files
43 Options: 'none', 'last', 'all'. Default: 'none'
44 --connectorset Specifies the name of the ConnectorSet to remove
45 --removedots Removes this connectorsets dots from the database
46 --version Version of connectorset to remove. Default is the newest version.
47  
48 Options may be abbreviated. Values are case insenstive.
49  
50 USAGE
51 ;
52  
53 my $dbinfo = Bio::ConnectDots::Config::db('production');
54 $HOST or $HOST=$dbinfo->{host};
55 $USER or $USER=$dbinfo->{user};
56 $PASSWORD or $PASSWORD=$dbinfo->{password};
57 $DATABASE or $DATABASE=$dbinfo->{dbname};
58 die "Must provide a ConnectorSet name to remove: --connectorset name" unless $CONNECTORSET;
59  
60 print "### Removing ConntorSet: $CONNECTORSET";
61 print ", version: $VERSION" if $VERSION;
62 print "\n";
63  
64 my $db=new Bio::ConnectDots::DB
65 (-database=>$DATABASE,-host=>$HOST,-user=>$USER,-password=>$PASSWORD,-ext_directory=>$LOADDIR);
66 remove_set($db,$CONNECTORSET,$REMOVEDOTS,$VERSION);
67  
68  
69 ### allows you to remove a connectorset. Pass in the name of the connectorset and whether you want
70 ### the dots from that set deleted (an expensive operation that may be unnecessary)
71 ### usage remove_set( <ConnectDots database> <connectorset name>, <remove dots> )
72 sub remove_set {
73 my ($db, $cs_name, $remove_dots, $version) = @_;
74 throw("Must have database connection to remove a connectorset.") unless $db;
75 my $dbh = $db->{dbh};
76 my $iterator;
77 my ($cs_id, $max_ver);
78
79 # get cs_id of specified version. Otherwise select most current version
80 if($version) {
81 $iterator = $dbh->prepare("SELECT connectorset_id,version FROM connectorset WHERE name='$cs_name' AND version='$version'");
82 $iterator->execute();
83 my @cols = $iterator->fetchrow_array();
84 notfound($db, $cs_name, $version) unless $cols[0];
85 $cs_id = $cols[0];
86 }
87 else {
88 $iterator = $dbh->prepare("SELECT connectorset_id,version FROM connectorset WHERE name='$cs_name'");
89 $iterator->execute();
90 while(my ($id, $ver) = $iterator->fetchrow_array()) {
91 if($ver gt $max_ver) {
92 $max_ver = $ver;
93 $cs_id = $id;
94 }
95 }
96 }
97
98 notfound($db, $cs_name, $version) unless $cs_id;
99
100 # remove set from connectorset and connectdot (easy deletes)
101 $db->do_sql("DELETE FROM connectorset WHERE connectorset_id=$cs_id");
102 $db->do_sql("DELETE FROM connectdot WHERE connectorset_id=$cs_id");
103
104 # Delete and save labels and dotset ids for this connectorset
105 my %dotsets; # hash on dotset_id
106 my %labels; # hash on label_id
107 $iterator = $dbh->prepare("SELECT dotset_id,label_id FROM connectdotset WHERE connectorset_id=$cs_id");
108 $iterator->execute();
109 while (my @cols = $iterator->fetchrow_array()) {
110 $dotsets{$cols[0]} = 'delete' if $cols[0];
111 $labels{$cols[1]} = 'delete' if $cols[1];
112 }
113 $db->do_sql("DELETE FROM connectdotset WHERE connectorset_id=$cs_id");
114
115 # scan connectdotset again and delete labels and dotsets that are not found
116 $iterator = $dbh->prepare("SELECT dotset_id,label_id FROM connectdotset");
117 $iterator->execute();
118 while (my @cols = $iterator->fetchrow_array()) {
119 $dotsets{$cols[0]} = 'save' if $cols[0];
120 $labels{$cols[1]} = 'save' if $cols[1];
121 }
122 foreach my $ds_id (keys %dotsets) { # delete dotsets
123 $db->do_sql("DELETE FROM dotset WHERE dotset_id=$ds_id") if $dotsets{$ds_id} eq 'delete';
124 }
125 foreach my $label_id (keys %labels) { # delete labels
126 $db->do_sql("DELETE FROM label WHERE label_id=$label_id") if $labels{$label_id} eq 'delete';
127 }
128
129 # remove dependant connectortable entries in connectortablesets
130 my $sql = qq(DELETE FROM connectortableset WHERE connectortable_id IN
131 (SELECT DISTINCT connectortable_id
132 FROM connectortableset
133 WHERE connectorset_id=$cs_id)
134 );
135  
136 # remove dots from dot that are not in connectdot
137 $db->do_sql("DELETE FROM dot WHERE dot_id NOT IN (SELECT dot_id FROM connectdot)") if $remove_dots;
138
139 }
140  
141 sub notfound {
142 my ($db, $cs_name, $version) = @_;
143 print "Unknown connectorset: $cs_name";
144 print ", version $version" if $version;
145 print " in database ", $db->{database}, ".\n"
146 and die;
147 }
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162