]> git.donarmstrong.com Git - biopieces.git/commitdiff
added remove_ucsc_tracks
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 11 Nov 2008 06:26:04 +0000 (06:26 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 11 Nov 2008 06:26:04 +0000 (06:26 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@304 74ccb610-7750-0410-82ae-013aeee3265d

bp_bin/remove_ucsc_tracks [new file with mode: 0644]
code_perl/Maasha/Biopieces.pm

diff --git a/bp_bin/remove_ucsc_tracks b/bp_bin/remove_ucsc_tracks
new file mode 100644 (file)
index 0000000..4cd1d44
--- /dev/null
@@ -0,0 +1,6 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use Maasha::Biopieces;
index fda79fb0cebfc67e2db91ffeadd938d244fce31f..f4bb8344d27ae30c3792d1f5f113bdd11e30fd35 100644 (file)
@@ -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.