From: martinahansen Date: Fri, 29 May 2009 09:23:06 +0000 (+0000) Subject: migrated plot_chrdist X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=baade7b2e288dee28f907ede7a2b74884d6a552a;p=biopieces.git migrated plot_chrdist git-svn-id: http://biopieces.googlecode.com/svn/trunk@447 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/plot_chrdist b/bp_bin/plot_chrdist index fdf5bd2..dd12136 100755 --- a/bp_bin/plot_chrdist +++ b/bp_bin/plot_chrdist @@ -1,6 +1,127 @@ -#!/usr/bin/env perl +#!/usr/bin/env perl -w + +# Copyright (C) 2007-2009 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Plot chromosome distribution histogram. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Plot; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $default, $terminals, $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh ); + +$default = "Chromosome Distribution"; +$terminals = "dumb,x11,aqua,post,svg"; + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'data_out', short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'terminal', short => 't', type => 'string', mandatory => 'no', default => 'dumb', allowed => $terminals, disallowed => undef }, + { long => 'title', short => 'T', type => 'string', mandatory => 'no', default => $default, allowed => undef, disallowed => undef }, + { long => 'xlabel', short => 'X', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'ylabel', short => 'Y', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + ] +); + +$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); +$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); + +while ( $record = Maasha::Biopieces::get_record( $in ) ) +{ + if ( $record->{ "CHR" } ) { # generic + $data_hash{ $record->{ "CHR" } }++; + } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan + $data_hash{ $record->{ "S_ID" } }++; + } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL + $data_hash{ $record->{ "S_ID" } }++; + } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST + $data_hash{ $record->{ "S_ID" } }++; + } + + Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" }; +} + +foreach $elem ( keys %data_hash ) +{ + $sort_key = $elem; + + $sort_key =~ s/chr//i; + + $sort_key =~ s/^X(.*)/99$1/; + $sort_key =~ s/^Y(.*)/99$1/; + $sort_key =~ s/^Z(.*)/999$1/; + $sort_key =~ s/^M(.*)/9999$1/; + $sort_key =~ s/^U(.*)/99999$1/; + + $count = $sort_key =~ tr/_//; + + $sort_key =~ s/_.*/"999999" x $count/ex; + + push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ]; +} + +@data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list; + +$result = Maasha::Plot::histogram_chrdist( \@data_list, $options ); + +$fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); + +print $fh "$_\n" foreach @{ $result }; + +close $fh; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +BEGIN +{ + $run_time_beg = Maasha::Biopieces::run_time(); + + Maasha::Biopieces::log_biopiece(); +} + + +END +{ + Maasha::Biopieces::close_stream( $in ); + Maasha::Biopieces::close_stream( $out ); + + $run_time_end = Maasha::Biopieces::run_time(); + + Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options ); +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +__END__ -use Maasha::BioRun; diff --git a/code_perl/Maasha/BioRun.pm b/code_perl/Maasha/BioRun.pm index 7d29d82..f926070 100644 --- a/code_perl/Maasha/BioRun.pm +++ b/code_perl/Maasha/BioRun.pm @@ -145,7 +145,6 @@ sub run_script elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) } elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) } elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) } - elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) } elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) } elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) } elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) } @@ -447,17 +446,6 @@ sub get_options key|k=s ); } - elsif ( $script eq "plot_chrdist" ) - { - @options = qw( - no_stream|x - data_out|o=s - terminal|t=s - title|T=s - xlabel|X=s - ylabel|Y=s - ); - } elsif ( $script eq "plot_karyogram" ) { @options = qw( @@ -1746,7 +1734,7 @@ sub script_write_psl $first = 1; - $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } ); while ( $record = Maasha::Biopieces::get_record( $in ) ) { @@ -1779,7 +1767,7 @@ sub script_write_fixedstep my ( $fh, $record, $entry ); - $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } ); while ( $record = Maasha::Biopieces::get_record( $in ) ) { @@ -1814,7 +1802,7 @@ sub script_write_2bit $tmp_file = "$BP_TMP/write_2bit.fna"; $fh_tmp = Maasha::Common::write_open( $tmp_file ); - $fh_out = write_stream( $options->{ "data_out" } ); + $fh_out = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); while ( $record = Maasha::Biopieces::get_record( $in ) ) { @@ -1853,7 +1841,7 @@ sub script_write_solid my ( $record, $fh, $entry ); - $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } ); while ( $record = Maasha::Biopieces::get_record( $in ) ) { @@ -1887,7 +1875,7 @@ sub script_write_ucsc_config my ( $record, $fh ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); while ( $record = Maasha::Biopieces::get_record( $in ) ) { @@ -1926,7 +1914,7 @@ sub script_plot_seqlogo $logo = Maasha::Plot::seq_logo( \@entries ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh $logo; @@ -1981,7 +1969,7 @@ sub script_plot_phastcons_profiles $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh "$_\n" foreach @{ $plot }; @@ -2146,7 +2134,7 @@ sub script_plot_histogram $result = Maasha::Plot::histogram_simple( \@data_list, $options ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh "$_\n" foreach @{ $result }; @@ -2186,70 +2174,7 @@ sub script_plot_lendist $result = Maasha::Plot::histogram_lendist( \@data_list, $options ); - $fh = write_stream( $options->{ "data_out" } ); - - print $fh "$_\n" foreach @{ $result }; - - close $fh; -} - - -sub script_plot_chrdist -{ - # Martin A. Hansen, August 2007. - - # Plot chromosome distribution using GNU plot. - - my ( $in, # handle to in stream - $out, # handle to out stream - $options, # options hash - ) = @_; - - # Returns nothing. - - my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh ); - - $options->{ "title" } ||= "Chromosome Distribution"; - - while ( $record = Maasha::Biopieces::get_record( $in ) ) - { - if ( $record->{ "CHR" } ) { # generic - $data_hash{ $record->{ "CHR" } }++; - } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan - $data_hash{ $record->{ "S_ID" } }++; - } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL - $data_hash{ $record->{ "S_ID" } }++; - } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST - $data_hash{ $record->{ "S_ID" } }++; - } - - Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" }; - } - - foreach $elem ( keys %data_hash ) - { - $sort_key = $elem; - - $sort_key =~ s/chr//i; - - $sort_key =~ s/^X(.*)/99$1/; - $sort_key =~ s/^Y(.*)/99$1/; - $sort_key =~ s/^Z(.*)/999$1/; - $sort_key =~ s/^M(.*)/9999$1/; - $sort_key =~ s/^U(.*)/99999$1/; - - $count = $sort_key =~ tr/_//; - - $sort_key =~ s/_.*/"999999" x $count/ex; - - push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ]; - } - - @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list; - - $result = Maasha::Plot::histogram_chrdist( \@data_list, $options ); - - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh "$_\n" foreach @{ $result }; @@ -2287,7 +2212,7 @@ sub script_plot_karyogram $result = Maasha::Plot::karyogram( \%data_hash, \%options ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh $result; @@ -2327,7 +2252,7 @@ sub script_plot_matches $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP ); - $fh = write_stream( $options->{ "data_out" } ); + $fh = Maasha::Biopieces::write_stream( $options->{ "data_out" } ); print $fh "$_\n" foreach @{ $result }; diff --git a/code_perl/Maasha/Plot.pm b/code_perl/Maasha/Plot.pm index 70fa315..07673c1 100644 --- a/code_perl/Maasha/Plot.pm +++ b/code_perl/Maasha/Plot.pm @@ -32,9 +32,9 @@ 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 { @@ -71,7 +71,7 @@ sub lineplot_simple $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 }; @@ -337,8 +337,8 @@ sub dotplot_matches $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 ); + $fh_forward = Maasha::Filesys::file_write_open( $forward_file ); + $fh_backward = Maasha::Filesys::file_write_open( $backward_file ); $q_max = 0; $s_max = 0; @@ -491,7 +491,7 @@ sub parse_karyo_data # stalk => "gray66", ); - $fh = Maasha::Common::read_open( $file ); + $fh = Maasha::Filesys::file_read_open( $file ); while ( $line = <$fh> ) {