From 4348f0240a08fd1eec3be316987b704a27624cfa Mon Sep 17 00:00:00 2001 From: martinahansen Date: Tue, 26 May 2009 09:14:05 +0000 Subject: [PATCH] migrated and fixed calc_fixedstep git-svn-id: http://biopieces.googlecode.com/svn/trunk@410 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/calc_fixedstep | 123 +++++++++++++++++++++++++++++++- code_perl/Maasha/BioRun.pm | 65 ----------------- code_perl/Maasha/UCSC/Wiggle.pm | 6 +- 3 files changed, 124 insertions(+), 70 deletions(-) diff --git a/bp_bin/calc_fixedstep b/bp_bin/calc_fixedstep index fdf5bd2..9c14f2c 100755 --- a/bp_bin/calc_fixedstep +++ b/bp_bin/calc_fixedstep @@ -1,6 +1,123 @@ -#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Calculate fixedstep entries from records in the stream. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Filesys; +use Maasha::UCSC::BED; +use Maasha::UCSC::Wiggle; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $tmp_dir, $bed_file, $fh_in, $fh_out, + $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'check', short => 'C', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + ] +); + +$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); +$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); + +$tmp_dir = Maasha::Biopieces::get_tmpdir(); + +$bed_file = "$tmp_dir/calc_fixedstep.bed"; +$fh_out = Maasha::Filesys::file_write_open( $bed_file ); + +while ( $record = Maasha::Biopieces::get_record( $in ) ) +{ + if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) { + Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } ); + } + + Maasha::Biopieces::put_record( $record, $out ); +} + +close $fh_out; + +$file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $tmp_dir ); + +unlink $bed_file; + +foreach $chr ( sort keys %{ $file_hash } ) +{ + $bed_file = $file_hash->{ $chr }; + + $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } ); + + #$fixedstep_file = "$bed_file.fixedstep"; + + # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" ); + + $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file ); + + while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) ) + { + if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) { + Maasha::Biopieces::put_record( $record, $out ); + } + } + + close $fh_in; + + unlink $bed_file; + unlink $fixedstep_file; +} + +Maasha::Filesys::dir_remove( $tmp_dir ); + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +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 ); +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use Maasha::BioRun; +__END__ diff --git a/code_perl/Maasha/BioRun.pm b/code_perl/Maasha/BioRun.pm index edb52b0..bb5437f 100644 --- a/code_perl/Maasha/BioRun.pm +++ b/code_perl/Maasha/BioRun.pm @@ -135,7 +135,6 @@ sub run_script elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) } elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) } elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) } - elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) } elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) } elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) } elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) } @@ -326,12 +325,6 @@ sub get_options num|n=s ); } - elsif ( $script eq "calc_fixedstep" ) - { - @options = qw( - check|C - ); - } elsif ( $script eq "length_seq" ) { @options = qw( @@ -1833,64 +1826,6 @@ sub script_create_weight_matrix } -sub script_calc_fixedstep -{ - # Martin A. Hansen, September 2008. - - # Calculates fixedstep entries from data in the stream. - - my ( $in, # handle to in stream - $out, # handle to out stream - $options, # options hash - ) = @_; - - # Returns nothing. - - my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry ); - - $bed_file = "$BP_TMP/calc_fixedstep.bed"; - $fh_out = Maasha::Filesys::file_write_open( $bed_file ); - - while ( $record = Maasha::Biopieces::get_record( $in ) ) - { - if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) { - Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } ); - } - } - - close $fh_out; - - $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP ); - - unlink $bed_file; - - foreach $chr ( sort keys %{ $file_hash } ) - { - $bed_file = $file_hash->{ $chr }; - - $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } ); - - #$fixedstep_file = "$bed_file.fixedstep"; - - # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" ); - - $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file ); - - while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) ) - { - if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) { - Maasha::Biopieces::put_record( $record, $out ); - } - } - - close $fh_in; - - unlink $bed_file; - unlink $fixedstep_file; - } -} - - sub script_remove_indels { # Martin A. Hansen, August 2007. diff --git a/code_perl/Maasha/UCSC/Wiggle.pm b/code_perl/Maasha/UCSC/Wiggle.pm index 6103a49..0e512e1 100644 --- a/code_perl/Maasha/UCSC/Wiggle.pm +++ b/code_perl/Maasha/UCSC/Wiggle.pm @@ -87,6 +87,8 @@ sub fixedstep_entry_get $entry = <$fh>; + return if not $entry; + chomp $entry; @lines = split "\n", $entry; @@ -219,7 +221,7 @@ sub fixedstep_calc { @block = "fixedStep chrom=$chr start=" . ( $beg + 1 ) . " step=1"; - for ( $i = $beg; $i <= $end; $i++ ) + for ( $i = $beg; $i < $end; $i++ ) { if ( $use_log10 ) { push @block, sprintf "%.4f", Maasha::Calc::log10( $array->[ $i ] ); @@ -263,7 +265,7 @@ sub fixedstep_calc_array { if ( $use_score ) { $clones = $bed->[ score ]; - } elsif ( $bed->[ name ] =~ /_(\d+)$/ ) { + } elsif ( defined $bed->[ name ] and $bed->[ name ] =~ /_(\d+)$/ ) { $clones = $1; } else { $clones = 1; -- 2.39.2