]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Plot.pm
changed to new find_adaptor
[biopieces.git] / code_perl / Maasha / Plot.pm
index 70fa3159bd2fc2c5eb29ea5aae4094ac0ba9e784..34bd123024e89960ca90c0b422a85c4b79dcd10c 100644 (file)
@@ -28,13 +28,14 @@ package Maasha::Plot;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use Data::Dumper;
 use SVG;
 use IPC::Open2;
-use Time::HiRes qw( gettimeofday );
 use Maasha::Common;
-use Maasha::Calc;
+use Maasha::Filesys;
+use Maasha::Seq;
 use vars qw ( @ISA @EXPORT );
 
 use constant {
@@ -65,13 +66,13 @@ sub lineplot_simple
 
     # Returns list.
 
-    my ( $tmp_file, $pid, $fh_in, $fh_out, $cmd, $i, $line, @lines, $xtic_space, @plot_cmd );
+    my ( $tmp_file, $pid, $fh_in, $fh_out, $cmd, $i, $line, @lines, $xtic_space, @plot_cmd, $title );
 
     $tmp_dir ||= $ENV{ 'BP_TMP' };
 
     $tmp_file = "$tmp_dir/lineplot_simple.tab";
 
-    $fh_out = Maasha::Common::write_open( $tmp_file );
+    $fh_out = Maasha::Filesys::file_write_open( $tmp_file );
 
     map { print $fh_out join( "\t", @{ $_ } ), "\n" } @{ $data };
 
@@ -79,7 +80,7 @@ sub lineplot_simple
 
     $options->{ "terminal" } ||= "dumb";
 
-    $cmd  = "gnuplot";
+    $cmd  = "gnuplot -persist";
 
     $pid = open2( $fh_out, $fh_in, $cmd );
 
@@ -89,13 +90,15 @@ sub lineplot_simple
     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
+    print $fh_in "set logscale y\n"                        if $options->{ "logscale_y" };
     print $fh_in "set grid\n"                              if not $options->{ "terminal" } eq "dumb";
     print $fh_in "set autoscale\n";
-    print $fh_in "unset key\n";
-    print $fh_in "set xtics border in scale 0 nomirror rotate by 90 offset character 0, 0, 0\n";
+    print $fh_in "set xtics rotate by -90\n";
 
-    for ( $i = 1; $i < scalar @{ $data->[ 0 ] } + 1; $i++ ) {
-        push @plot_cmd, qq("$tmp_file" using $i with lines ls 1); 
+    for ( $i = 1; $i < scalar @{ $data->[ 0 ] } + 1; $i++ )
+    {
+        $title = $options->{ 'keys' }->[ $i - 1 ] || $options->{ 'list' } || "";
+        push @plot_cmd, qq("$tmp_file" using $i with lines title "$title"); 
     }
 
     print $fh_in "plot " . join( ", ", @plot_cmd ) . "\n";
@@ -138,7 +141,7 @@ sub histogram_simple
 
     $options->{ "terminal" } ||= "dumb";
 
-    $cmd  = "gnuplot";
+    $cmd  = "gnuplot -persist";
 
     $pid = open2( $fh_out, $fh_in, $cmd );
 
@@ -149,13 +152,13 @@ sub histogram_simple
     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
+    print $fh_in "set logscale y\n"                        if $options->{ "logscale_y" };
     print $fh_in "set autoscale\n";
     print $fh_in "unset key\n";
     print $fh_in "set style fill solid\n";
     print $fh_in "set style histogram title offset character 0, 0, 0\n";
     print $fh_in "set style data histograms\n";
-    print $fh_in "set xtics border in scale 0 nomirror rotate by 90 offset character 0, 0, 0\n";
-
+    print $fh_in "set xtics border in scale 0 nomirror rotate by -90 offset character 0, 0, 0\n";
     print $fh_in "plot '-' using 2:xticlabels(1)\n";
 
     for ( $i = 0; $i < @{ $data }; $i++ )
@@ -196,7 +199,7 @@ sub histogram_lendist
 
     $options->{ "terminal" } ||= "dumb";
 
-    if ( $data->[ -1 ]->[ 0 ] <= 10 ) {
+    if ( $data->[ -1 ]->[ 0 ] <= 10 ) { # FIXME: some day figure the formula for this!
         $xtic_space = 1;
     } elsif ( $data->[ -1 ]->[ 0 ] <= 100 ) {
         $xtic_space = 5;
@@ -216,9 +219,11 @@ sub histogram_lendist
         $xtic_space = 1000;
     } elsif ( $data->[ -1 ]->[ 0 ] <= 100000 ) {
         $xtic_space = 5000;
+    } elsif ( $data->[ -1 ]->[ 0 ] <= 1000000 ) {
+        $xtic_space = 50000;
     }
 
-    $cmd  = "gnuplot";
+    $cmd  = "gnuplot -persist";
 
     $pid = open2( $fh_out, $fh_in, $cmd );
 
@@ -226,6 +231,7 @@ sub histogram_lendist
     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
+    print $fh_in "set logscale y\n"                        if $options->{ "logscale_y" };
     print $fh_in "set autoscale\n";
     print $fh_in "unset key\n";
     print $fh_in "set style fill solid\n";
@@ -275,7 +281,7 @@ sub histogram_chrdist
 
     $options->{ "terminal" } ||= "dumb";
 
-    $cmd  = "gnuplot";
+    $cmd  = "gnuplot -persist";
 
     $pid = open2( $fh_out, $fh_in, $cmd );
 
@@ -283,12 +289,13 @@ sub histogram_chrdist
     print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
     print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
     print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
+    print $fh_in "set logscale y\n"                        if $options->{ "logscale_y" };
     print $fh_in "set autoscale\n";
     print $fh_in "unset key\n";
     print $fh_in "set style fill solid\n";
     print $fh_in "set style histogram title offset character 0, 0, 0\n";
     print $fh_in "set style data histograms\n";
-    print $fh_in "set xtics border in scale 0 nomirror rotate by 90 offset character 0, 0, 0\n";
+    print $fh_in "set xtics border in scale 0 nomirror rotate by -90 offset character 0, 0, 0\n";
 
     print $fh_in "plot '-' using 2:xticlabels(1)\n";
 
@@ -313,113 +320,6 @@ sub histogram_chrdist
 }
 
 
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DOTPLOT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-
-sub dotplot_matches
-{
-    # Martin A. Hansen, August 2007.
-
-    # Generates a dotplot from a list of matches using Gnuplot.
-
-    my ( $matches,   # list of hashrefs.
-         $options,   # options hash
-         $tmp_dir,   # temporary directory
-       ) = @_;
-
-    # Returns list.
-
-    my ( $forward_file, $backward_file, $pid, $fh_forward, $fh_backward,
-         $fh_in, $fh_out, $cmd, $match, $line, @lines, $q_max, $s_max );
-
-    $tmp_dir ||= $ENV{ 'BP_TMP' };
-
-    $forward_file  = "$tmp_dir/match_f.tab";
-    $backward_file = "$tmp_dir/match_r.tab";
-
-    $fh_forward  = Maasha::Common::write_open( $forward_file );
-    $fh_backward = Maasha::Common::write_open( $backward_file );
-
-    $q_max = 0;
-    $s_max = 0;
-
-    foreach $match ( @{ $matches } )
-    {
-        if ( $match->{ "DIR" } =~ /^f/ )
-        {
-            print $fh_forward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
-            print $fh_forward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_END" } + 1 ), "\n";
-            print $fh_forward "\n\n";
-        }
-        else
-        {
-            print $fh_backward join( "\t", $match->{ "Q_BEG" } + 1, $match->{ "S_END" } + 1 ), "\n";
-            print $fh_backward join( "\t", $match->{ "Q_END" } + 1, $match->{ "S_BEG" } + 1 ), "\n";
-            print $fh_backward "\n\n";
-        }
-
-        $q_max = $match->{ "Q_END" } if $match->{ "Q_END" } > $q_max;
-        $s_max = $match->{ "S_END" } if $match->{ "S_END" } > $s_max;
-    }
-
-    $q_max++;
-    $s_max++;
-
-    close $fh_forward;
-    close $fh_backward;
-
-    $options->{ "terminal" } ||= "dumb";
-
-    $cmd  = "gnuplot";
-
-    $pid = open2( $fh_out, $fh_in, $cmd );
-    
-    print $fh_in "set terminal $options->{ 'terminal' }\n";
-    print $fh_in "set xrange [1:$q_max]\n";
-    print $fh_in "set yrange [1:$s_max]\n";
-    print $fh_in "set title \"$options->{ 'title' }\"\n"   if $options->{ "title" };
-    print $fh_in "set xlabel \"$options->{ 'xlabel' }\"\n" if $options->{ "xlabel" };
-    print $fh_in "set ylabel \"$options->{ 'ylabel' }\"\n" if $options->{ "ylabel" };
-    print $fh_in "unset key\n";
-
-    if ( $options->{ "terminal" } ne "dumb" )
-    {
-        print $fh_in "set style line 1 linetype 1 linecolor rgb \"green\" linewidth 2 pointtype 6 pointsize default\n";
-        print $fh_in "set style line 2 linetype 1 linecolor rgb \"red\" linewidth 2 pointtype 6 pointsize default\n";
-    }
-
-    print $fh_in "set xtics border out\n";
-    print $fh_in "set ytics border out\n";
-    print $fh_in "set grid\n";
-
-    if ( $options->{ "direction" } =~ /^b/ ) {
-        print $fh_in qq(plot "$forward_file" with lines ls 1, "$backward_file" with lines ls 2\n);
-    } elsif ( $options->{ "direction" } =~ /^f/ ) {
-        print $fh_in qq(plot "$forward_file" with lines ls 1\n);
-    } elsif ( $options->{ "direction" } =~ /^r/ ) {
-        print $fh_in qq(plot "$backward_file" with lines ls 2\n);
-    }
-
-    close $fh_in;
-
-    while ( $line = <$fh_out> )
-    {
-        chomp $line;
-
-        push @lines, $line;
-    }
-
-    close $fh_out;
-
-    waitpid $pid, 0;
-
-    unlink $forward_file;
-    unlink $backward_file;
-
-    return wantarray ? @lines : \@lines;
-}
-
-
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> KARYOGRAM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  
 
@@ -437,15 +337,13 @@ sub karyogram
 
     my ( $karyo_file, $svg, $features, $karyo );
 
-    if ( $options->{ "genome" } eq "human" )
+    if ( $options->{ "genome" } eq "hg18" )
     {
-        $karyo_file = "/Users/m.hansen/maasha/perl_scripts/biopieces/karyo_data/human_cytobands.txt";
-#        $karyo_file = "/home/m.hansen/maasha/perl_scripts/biopieces/karyo_data/human_cytobands.txt";
+        $karyo_file = $ENV{ 'BP_DIR' } . "/bp_data/human_cytobands.txt";
     }
-    else
+    elsif( $options->{ "genome" } eq "mm9" )
     {
-        $karyo_file = "/Users/m.hansen/maasha/perl_scripts/biopieces/karyo_data/mouse_cytobands.txt";
- #       $karyo_file = "/home/m.hansen/maasha/perl_scripts/biopieces/karyo_data/mouse_cytobands.txt";
+        $karyo_file = $ENV{ 'BP_DIR' } . "/bp_data/mouse_cytobands.txt";
     }
 
     $karyo = parse_karyo_data( $karyo_file );
@@ -491,7 +389,7 @@ sub parse_karyo_data
 #        stalk    => "gray66",
     );
 
-    $fh = Maasha::Common::read_open( $file );
+    $fh = Maasha::Filesys::file_read_open( $file );
 
     while ( $line = <$fh> )
     {
@@ -644,6 +542,8 @@ sub find_cent
     push @nums, $acen->[ 1 ]->[ 1 ];
     push @nums, $acen->[ 1 ]->[ 2 ];
 
+
+    @nums = grep { defined $_ } @nums;   # FIXME
     @nums = sort { $a <=> $b } @nums;
 
     $cent = ( $nums[ 1 ] + $nums[ 2 ] ) / 2;
@@ -871,7 +771,7 @@ sub seq_logo
 
     $type = Maasha::Seq::seq_guess_type( $entries->[ 0 ]->[ 1 ] );
 
-    if ( $type =~ /^p/i ) {
+    if ( $type eq "PROTEIN" ) {
         $bit_max = 4;
     } else {
         $bit_max = 2;