From: martinahansen Date: Tue, 2 Jun 2009 12:17:53 +0000 (+0000) Subject: migrating read_ tools X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=93c8e44f71df52dfbef3766e20ff726c051a4406;p=biopieces.git migrating read_ tools git-svn-id: http://biopieces.googlecode.com/svn/trunk@460 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/bp_bin/read_2bit b/bp_bin/read_2bit index fdf5bd2..25f0193 100755 --- a/bp_bin/read_2bit +++ b/bp_bin/read_2bit @@ -1,6 +1,110 @@ -#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Read nucleotide sequences in 2bit format from one or more files. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Filesys; +use Maasha::TwoBit; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $file, $data_in, $mask, $toc, $line, $num ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'data_in', short => 'i', type => 'files!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'num', short => 'n', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => '0' }, + { long => 'no_mask', short => 'N', 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" } ); + +while ( $record = Maasha::Biopieces::get_record( $in ) ) { + Maasha::Biopieces::put_record( $record, $out ); +} + +$mask = 1 if not $options->{ "no_mask" }; + +$num = 1; + +foreach $file ( @{ $options->{ "data_in" } } ) +{ + $data_in = Maasha::Filesys::file_read_open( $file ); + + $toc = Maasha::TwoBit::twobit_get_TOC( $data_in ); + + foreach $line ( @{ $toc } ) + { + $record->{ "SEQ_NAME" } = $line->[ 0 ]; + $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask ); + $record->{ "SEQ_LEN" } = length $record->{ "SEQ" }; + + Maasha::Biopieces::put_record( $record, $out ); + + goto NUM if $options->{ "num" } and $num == $options->{ "num" }; + + $num++; + } + + close $data_in; +} + +NUM: + +close $data_in if $data_in; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +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/bp_bin/read_bed b/bp_bin/read_bed index fdf5bd2..18c5c22 100755 --- a/bp_bin/read_bed +++ b/bp_bin/read_bed @@ -1,6 +1,101 @@ -#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Read BED data (Browser Extensible Data) from one or more files. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Filesys; +use Maasha::UCSC::BED; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $file, $record, $bed_entry, $data_in, $num ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'data_in', short => 'i', type => 'files!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'cols', short => 'c', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => '0,1,2' }, + { long => 'num', short => 'n', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => '0' }, + { 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" } ); + +while ( $record = Maasha::Biopieces::get_record( $in ) ) { + Maasha::Biopieces::put_record( $record, $out ); +} + +if ( $options->{ 'data_in' } ) +{ + $num = 1; + + $data_in = Maasha::Filesys::files_read_open( $options->{ 'data_in' } ); + + while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $options->{ 'cols' }, $options->{ 'check' } ) ) + { + $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ); + + Maasha::Biopieces::put_record( $record, $out ); + + last if $options->{ "num" } and $num == $options->{ "num" }; + + $num++; + } + + close $data_in; +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +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/bp_bin/read_fasta b/bp_bin/read_fasta index 092e0c5..9449a0d 100755 --- a/bp_bin/read_fasta +++ b/bp_bin/read_fasta @@ -27,9 +27,9 @@ use strict; -use Maasha::Common; -use Maasha::Fasta; use Maasha::Biopieces; +use Maasha::Filesys; +use Maasha::Fasta; # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @@ -53,7 +53,7 @@ while ( $record = Maasha::Biopieces::get_record( $in ) ) { if ( $options->{ 'data_in' } ) { - $data_in = Maasha::Common::read_open_multi( $options->{ 'data_in' } ); + $data_in = Maasha::Filesys::files_read_open( $options->{ 'data_in' } ); $num = 1; diff --git a/code_perl/Maasha/BioRun.pm b/code_perl/Maasha/BioRun.pm index aebeb94..7b5a9da 100644 --- a/code_perl/Maasha/BioRun.pm +++ b/code_perl/Maasha/BioRun.pm @@ -39,14 +39,9 @@ use Maasha::Config; use Maasha::Common; use Maasha::Filesys; use Maasha::Fasta; -use Maasha::Align; -use Maasha::Matrix; -use Maasha::Match; use Maasha::EMBL; use Maasha::Stockholm; use Maasha::Seq; -use Maasha::Patscan; -use Maasha::Plot; use Maasha::Calc; use Maasha::UCSC; use Maasha::UCSC::BED; @@ -117,7 +112,6 @@ sub run_script if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) } elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) } - elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) } elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) } elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) } elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) } @@ -125,7 +119,6 @@ sub run_script elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) } elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) } elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) } - elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) } elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) } elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) } elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) } @@ -174,15 +167,6 @@ sub get_options num|n=s ); } - elsif ( $script eq "read_bed" ) - { - @options = qw( - data_in|i=s - cols|c=s - num|n=s - check|C - ); - } elsif ( $script eq "read_fixedstep" ) { @options = qw( @@ -240,14 +224,6 @@ sub get_options num|n=s ); } - elsif ( $script eq "read_2bit" ) - { - @options = qw( - data_in|i=s - num|n=s - no_mask|N - ); - } elsif ( $script eq "read_solexa" ) { @options = qw( @@ -548,53 +524,6 @@ sub script_read_psl } -sub script_read_bed -{ - # Martin A. Hansen, August 2007. - - # Read bed table from stream or file. - - my ( $in, # handle to in stream - $out, # handle to out stream - $options, # options hash - ) = @_; - - # Returns nothing. - - my ( $cols, $file, $record, $bed_entry, $data_in, $num ); - - $cols = $options->{ 'cols' }->[ 0 ]; - - while ( $record = Maasha::Biopieces::get_record( $in ) ) { - Maasha::Biopieces::put_record( $record, $out ); - } - - $num = 1; - - foreach $file ( @{ $options->{ "files" } } ) - { - $data_in = Maasha::Common::read_open( $file ); - - while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) ) - { - $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ); - - Maasha::Biopieces::put_record( $record, $out ); - - goto NUM if $options->{ "num" } and $num == $options->{ "num" }; - - $num++; - } - - close $data_in; - } - - NUM: - - close $data_in if $data_in; -} - - sub script_read_fixedstep { # Martin A. Hansen, Juli 2008. @@ -1027,57 +956,6 @@ sub script_read_gff } -sub script_read_2bit -{ - # Martin A. Hansen, March 2008. - - # Read sequences from 2bit file. - - my ( $in, # handle to in stream - $out, # handle to out stream - $options, # options hash - ) = @_; - - # Returns nothing. - - my ( $record, $file, $data_in, $mask, $toc, $line, $num ); - - $mask = 1 if not $options->{ "no_mask" }; - - while ( $record = Maasha::Biopieces::get_record( $in ) ) { - Maasha::Biopieces::put_record( $record, $out ); - } - - $num = 1; - - foreach $file ( @{ $options->{ "files" } } ) - { - $data_in = Maasha::Common::read_open( $file ); - - $toc = Maasha::TwoBit::twobit_get_TOC( $data_in ); - - foreach $line ( @{ $toc } ) - { - $record->{ "SEQ_NAME" } = $line->[ 0 ]; - $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask ); - $record->{ "SEQ_LEN" } = length $record->{ "SEQ" }; - - Maasha::Biopieces::put_record( $record, $out ); - - goto NUM if $options->{ "num" } and $num == $options->{ "num" }; - - $num++; - } - - close $data_in; - } - - NUM: - - close $data_in if $data_in; -} - - sub script_read_solexa { # Martin A. Hansen, March 2008. diff --git a/code_perl/Maasha/Common.pm b/code_perl/Maasha/Common.pm index 75f89cd..d998d80 100644 --- a/code_perl/Maasha/Common.pm +++ b/code_perl/Maasha/Common.pm @@ -237,46 +237,6 @@ sub read_open } -sub read_open_multi -{ - # Martin A. Hansen, May 2009. - - # Cats a number of files and returns a filehandle. - - my ( $files, # full path to file - ) = @_; - - # returns filehandle - - my ( $file, $fh, $type, %type_hash, $file_string ); - - foreach $file ( @{ $files } ) - { - Maasha::Common::error( qq(No such file: $file) ) if not -f $file; - - $type = `file $file`; - - if ( $type =~ /gzip compressed/ ) { - $type_hash{ 'gzip' } = 1; - } else { - $type_hash{ 'ascii' } = 1; - } - } - - Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1; - - $file_string = join " ", @{ $files }; - - if ( $type =~ /gzip compressed/ ) { - $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) ); - } else { - $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) ); - } - - return $fh; -} - - sub write_open { # Martin A. Hansen, January 2004. diff --git a/code_perl/Maasha/Filesys.pm b/code_perl/Maasha/Filesys.pm index cb27c95..b189a41 100644 --- a/code_perl/Maasha/Filesys.pm +++ b/code_perl/Maasha/Filesys.pm @@ -66,6 +66,46 @@ sub file_read_open } +sub files_read_open +{ + # Martin A. Hansen, May 2009. + + # Cats a number of files and returns a filehandle. + + my ( $files, # full path to file + ) = @_; + + # returns filehandle + + my ( $file, $fh, $type, %type_hash, $file_string ); + + foreach $file ( @{ $files } ) + { + Maasha::Common::error( qq(No such file: $file) ) if not -f $file; + + $type = `file $file`; + + if ( $type =~ /gzip compressed/ ) { + $type_hash{ 'gzip' } = 1; + } else { + $type_hash{ 'ascii' } = 1; + } + } + + Maasha::Common::error( qq(Mixture of zipped and unzipped files) ) if scalar keys %type_hash > 1; + + $file_string = join " ", @{ $files }; + + if ( $type =~ /gzip compressed/ ) { + $fh = new IO::File "zcat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) ); + } else { + $fh = new IO::File "cat $file_string|" or Maasha::Common::error( qq(Could not open pipe: $!) ); + } + + return $fh; +} + + sub file_write_open { # Martin A. Hansen, January 2004.