From: martinahansen Date: Tue, 11 Nov 2008 06:26:04 +0000 (+0000) Subject: added remove_ucsc_tracks X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=5f7a9f91b41e9012ff33cf09d640cf04f5e65819;p=biopieces.git added remove_ucsc_tracks git-svn-id: http://biopieces.googlecode.com/svn/trunk@304 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/remove_ucsc_tracks b/bp_bin/remove_ucsc_tracks new file mode 100644 index 0000000..4cd1d44 --- /dev/null +++ b/bp_bin/remove_ucsc_tracks @@ -0,0 +1,6 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +use Maasha::Biopieces; diff --git a/code_perl/Maasha/Biopieces.pm b/code_perl/Maasha/Biopieces.pm index fda79fb..f4bb834 100644 --- a/code_perl/Maasha/Biopieces.pm +++ b/code_perl/Maasha/Biopieces.pm @@ -236,6 +236,7 @@ sub run_script elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) } elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) } elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) } + elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) } elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) } elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) } elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) } @@ -775,7 +776,18 @@ sub get_options database|d=s tables|t=s keys|k=s - query|q=s + user|u=s + password|p=s + no_stream|x + ); + } + elsif ( $script eq "remove_ucsc_tracks" ) + { + @options = qw( + database|d=s + tracks|t=s + keys|k=s + config_file|c=s user|u=s password|p=s no_stream|x @@ -1012,6 +1024,7 @@ sub get_options $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" }; $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" }; $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" }; + $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" }; # ---- check arguments ---- @@ -5056,6 +5069,78 @@ sub script_remove_mysql_tables } +sub script_remove_ucsc_tracks +{ + # Martin A. Hansen, November 2008. + + # Remove track from MySQL tables and config file. + + my ( $in, # handle to in stream + $out, # handle to out stream + $options, # options hash + ) = @_; + + # Returns nothing. + + my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh ); + + $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user(); + $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password(); + $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra"; + + map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } }; + + while ( $record = get_record( $in ) ) + { + map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } }; + + put_record( $record, $out ) if not $options->{ 'no_stream' }; + } + + # ---- locate track in config file ---- + + $fh_in = Maasha::Common::read_open( $options->{ 'config_file' } ); + + while ( $track = Maasha::UCSC::ucsc_config_get_entry( $fh_in ) ) { + push @tracks, $track; + } + + close $fh_in; + + map { push @new_tracks, $_ if not exists $track_hash{ $_->{ 'track' } } } @tracks; + + Maasha::Common::error( qq(track not found in config file: "$options->{ 'config_file' }") ) if scalar @tracks == scalar @new_tracks; + + # ---- locate track in database ---- + + $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } ); + + foreach $track ( sort keys %track_hash ) + { + if ( Maasha::SQL::table_exists( $dbh, $track ) ) + { + print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' }; + Maasha::SQL::delete_table( $dbh, $track ); + print STDERR "done.\n" if $options->{ 'verbose' }; + } + else + { + Maasha::Common::error qq(table "$track" not found in database "$options->{ 'database' }"); + } + } + + rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~"; + + $fh_out = Maasha::Common::write_open( $options->{ 'config_file' } ); + + map { Maasha::UCSC::ucsc_config_put_entry( $_, $fh_out ) } @new_tracks; + + close $fh_out; + + Maasha::SQL::disconnect( $dbh ); +} + + sub script_rename_keys { # Martin A. Hansen, August 2007.