]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/remove_ucsc_tracks
migrated removers
[biopieces.git] / bp_bin / remove_ucsc_tracks
1 #!/usr/bin/env perl -w
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Remove MySQL database tables.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use strict;
30 use Maasha::Biopieces;
31 use Maasha::Common;
32 use Maasha::SQL;
33 use Maasha::UCSC;
34
35
36 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37
38
39 my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $conf_file, %track_hash, $fh_in, $fh_out, $track, @tracks, $dbh, @new_tracks );
40
41 $conf_file = $ENV{ 'HOME' } . "/ucsc/my_tracks.ra";
42
43 $options = Maasha::Biopieces::parse_options(
44     [
45         { long => 'database',    short => 'd', type => 'string', mandatory => 'yes', default => undef,      allowed => undef, disallowed => undef },
46         { long => 'tables',      short => 't', type => 'list',   mandatory => 'no',  default => undef,      allowed => undef, disallowed => undef },
47         { long => 'keys',        short => 'k', type => 'list',   mandatory => 'no',  default => undef,      allowed => undef, disallowed => undef },
48         { long => 'config_file', short => 'c', type => 'file!',  mandatory => 'no',  default => $conf_file, allowed => undef, disallowed => undef },
49         { long => 'user',        short => 'u', type => 'string', mandatory => 'no',  default => undef,      allowed => undef, disallowed => undef },
50         { long => 'password',    short => 'p', type => 'string', mandatory => 'no',  default => undef,      allowed => undef, disallowed => undef },
51     ]   
52 );
53
54 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
55 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
56
57 Maasha::Common::error( qq(both --tables and --keys specified) ) if     $options->{ "tables" } and     $options->{ "keys" };
58 Maasha::Common::error( qq(no --tables or --keys specified) )    if not $options->{ "tables" } and not $options->{ "keys" };
59
60 $options->{ "user" }     ||= Maasha::UCSC::ucsc_get_user();
61 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
62
63 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
64
65 while ( $record = Maasha::Biopieces::get_record( $in ) )
66 {
67     map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
68
69     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
70 }
71
72 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
73
74 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
75     push @tracks, $track;
76 }
77
78 close $fh_in;
79
80 foreach $track ( @tracks )
81 {
82     if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
83         print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
84     } else {
85         push @new_tracks, $track;
86     }
87 }
88
89 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
90
91 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
92
93 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
94
95 close $fh_out;
96
97 # ---- locate track in database ----
98
99 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
100
101 foreach $track ( sort keys %track_hash )
102 {
103     if ( Maasha::SQL::table_exists( $dbh, $track ) )
104     {
105         print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
106         Maasha::SQL::delete_table( $dbh, $track );
107         print STDERR "done.\n" if $options->{ 'verbose' };
108     }
109     else
110     {
111         print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
112     }
113 }
114
115 Maasha::SQL::disconnect( $dbh );
116
117 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
118
119
120 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121
122
123 BEGIN
124 {
125     $run_time_beg = Maasha::Biopieces::run_time();
126
127     Maasha::Biopieces::log_biopiece();
128 }
129
130
131 END
132 {
133     Maasha::Biopieces::close_stream( $in );
134     Maasha::Biopieces::close_stream( $out );
135
136     $run_time_end = Maasha::Biopieces::run_time();
137
138     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
139 }
140
141
142 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
143
144
145 __END__
146