Merge pull request #5199 from solgenomics/topic/tracking_transformation
[sgn.git] / bin / delete_trials.pl
blob169b8b9055e76e72f61253a6d785824f89f3bf8f
2 =head1 NAME
4 delete_trials.pl - script to delete trials
6 =head1 DESCRIPTION
8 perl delete_trials.pl -i trial_ids [ -t trial_names ] [ -F file_with_trial_names ] [ -f file_with_trial_ids ] -H host -D dbname -U dbuser -P dbpass -b basepath -r temp_file_nd_experiment_id [ -n ]
10 Options:
12 =over 5
14 =item -H
16 hostname for database
18 =item -D
20 database name
22 =item -i
24 comma separated list of trial ids
26 =item -t
28 comma separated list of trial names
30 =item -b
32 basebath is the install path of the software, most commonly /home/production/cxgn/sgn which is the default.
34 =item -r
36 Specifies the temp file used to track nd_experiment_ids to delete. Defaults to /tmp/temp_nd_experiment_id_[date_and_time].
38 =item -F
40 a file with trial names, one per line
42 =item -f
44 file with trial ids, one per line
46 =item -n
48 non-interactive mode. Will not prompt for confirmation of each trial to delete
50 =back
52 First, it deletes metadata, then trial layouts, then phenotypes, and finally the trial entry in the project table. All deletes are hard deletes. There is no way of bringing the trial back, except from a backup. So be careful!
54 =head1 AUTHOR
56 Lukas Mueller <lam87@cornell.edu>
58 =cut
60 use strict;
62 use Getopt::Std;
63 use DateTime;
64 use Bio::Chado::Schema;
65 use CXGN::Metadata::Schema;
66 use CXGN::Phenome::Schema;
67 use CXGN::DB::InsertDBH;
68 use CXGN::Trial;
70 our ($opt_H, $opt_D, $opt_U, $opt_P, $opt_b, $opt_i, $opt_t, $opt_n, $opt_r, $opt_F, $opt_f);
72 getopts('H:D:U:P:b:i:t:r:nf:F:');
74 my $dt = DateTime->now();
75 my $date_string = $dt->ymd()."T".$dt->hms();
76 my $dbhost = $opt_H;
77 my $dbname = $opt_D;
78 my $dbuser = $opt_U;
79 my $dbpass = $opt_P;
80 my $trial_ids = $opt_i;
81 my $trial_names = $opt_t;
82 my $trial_names_file = $opt_F;
83 my $trial_ids_file = $opt_f;
84 my $non_interactive = $opt_n;
85 my $basepath = $opt_b || '/home/production/cxgn/sgn';
86 my $tempfile = $opt_r || "/tmp/temp_nd_experiment_id_$date_string";
88 my $dbh = CXGN::DB::InsertDBH->new( { dbhost=>$dbhost,
89 dbname=>$dbname,
90 dbargs => {AutoCommit => 0,
91 RaiseError => 1}
95 print STDERR "Connecting to database...\n";
96 my $schema= Bio::Chado::Schema->connect( sub { $dbh->get_actual_dbh() } );
97 my $metadata_schema = CXGN::Metadata::Schema->connect( sub { $dbh->get_actual_dbh() });
98 my $phenome_schema = CXGN::Phenome::Schema->connect( sub { $dbh->get_actual_dbh() });
100 my @trial_ids = split ",", $trial_ids;
101 my @trial_names = split ",", $trial_names;
103 if ($trial_names_file) {
104 open(my $F, "<", $trial_names_file) || die "Can't open the file $trial_names_file";
105 while (<$F>) {
106 chomp;
107 push @trial_names, $_;
109 close($F);
112 if ($trial_ids_file) {
113 open(my $F, "<", $trial_ids_file) || die "Can't open the file $trial_ids_file";
114 while(<$F>) {
115 chomp;
116 push @trial_ids, $_;
118 close($F);
121 foreach my $name (@trial_names) {
122 my $trial = $schema->resultset("Project::Project")->find( { name => $name });
123 if (!$trial) { print STDERR "Trial $name not found. Skipping...\n"; next; }
124 push @trial_ids, $trial->project_id();
127 foreach my $trial_id (@trial_ids) {
128 print STDERR "Retrieving trial information for trial $trial_id...\n";
130 my $t = CXGN::Trial->new({
131 bcs_schema => $schema,
132 metadata_schema => $metadata_schema,
133 phenome_schema => $phenome_schema,
134 trial_id => $trial_id
137 my $answer = "";
138 if (!$non_interactive) {
139 print $t->get_name().", ".$t->get_description().". Delete? ";
140 $answer = <>;
142 if ($non_interactive || $answer =~ m/^y/i) {
143 eval {
144 delete_trial($metadata_schema, $phenome_schema, $t);
146 if ($@) {
147 print STDERR "An error occurred trying to delete trial ".$t->get_name()." ($@)\n";
148 $dbh->rollback();
150 else {
151 $dbh->commit();
152 print STDERR "Trial ".$t->get_name()." successfully deleted\n";
159 $dbh->disconnect();
160 print STDERR "Done with everything (though nd_experiment entry deletion may still be occuring asynchronously).\n";
162 sub delete_trial {
163 my $metadata_schema = shift;
164 my $phenome_schema = shift;
165 my $t = shift;
167 print STDERR "Deleting trial ".$t->get_name()."\n";
168 print STDERR "Delete metadata...\n";
169 $t->delete_metadata();
170 print STDERR "Deleting phenotypes...\n";
171 $t->delete_phenotype_data($opt_b, $dbhost, $dbname, $dbuser, $dbpass, $opt_r);
172 print STDERR "Deleting layout...\n";
173 $t->delete_field_layout();
174 print STDERR "Delete project entry...\n";
175 $t->delete_project_entry();