From 9ed6c68487b9d8688a57bd3ee7314d22d46a7a4f Mon Sep 17 00:00:00 2001 From: martinahansen Date: Thu, 17 Sep 2009 16:17:28 +0000 Subject: [PATCH] added get_seq git-svn-id: http://biopieces.googlecode.com/svn/trunk@669 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/format_genome | 6 +- bp_bin/get_seq | 192 ++++++++++++++++++++++++++++++++++++ code_perl/Maasha/Filesys.pm | 3 +- 3 files changed, 197 insertions(+), 4 deletions(-) create mode 100755 bp_bin/get_seq diff --git a/bp_bin/format_genome b/bp_bin/format_genome index 35aa8b3..2907aa4 100755 --- a/bp_bin/format_genome +++ b/bp_bin/format_genome @@ -109,11 +109,13 @@ while ( $record = Maasha::Biopieces::get_record( $in ) ) Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" }; } +close $fh_out if $fh_out; + foreach $format ( @{ $options->{ 'formats' } } ) { print STDERR qq(Creating format: $format for $genome ... ) if $options->{ 'verbose' }; - if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) } + if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", $fasta_dir, "$genome.index" ) } elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) } elsif ( $format =~ /^blat$/i ) { warn "BLAT FORMAT NOT IMPLEMENTED" } elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $tmp_dir ) } @@ -124,8 +126,6 @@ foreach $format ( @{ $options->{ 'formats' } } ) print STDERR qq(done.\n) if $options->{ 'verbose' }; } -close $fh_out if $fh_out; - Maasha::Biopieces::close_stream( $in ); Maasha::Biopieces::close_stream( $out ); diff --git a/bp_bin/get_seq b/bp_bin/get_seq new file mode 100755 index 0000000..72c797e --- /dev/null +++ b/bp_bin/get_seq @@ -0,0 +1,192 @@ +#!/usr/bin/env perl + +# 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Extract subsequences from an indexed sequence file. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +use warnings; +use strict; +use Data::Dumper; +use Maasha::Biopieces; +use Maasha::Filesys; +use Maasha::Fasta; +use Maasha::Seq; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $options, $in, $out, $index, $fh, $record, $index_beg, $index_len, $beg, $end, $len, $seq ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'index', short => 'i', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef }, + { long => 'seq_name', short => 's', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'beg', short => 'b', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + { long => 'end', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + { long => 'len', short => 'l', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + ] +); + +$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); +$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); + +if ( $options->{ 'beg' } ) { + $options->{ 'beg' }--; +} else { + $options->{ 'beg' } = 0; +} + +$index = Maasha::Fasta::index_retrieve( $options->{ 'index' } . ".index" ); + +if ( $index->{ 'FILE_SIZE' } != Maasha::Filesys::file_size( $options->{ 'index' } . ".seq" ) ) { + Maasha::Common::error( qq(Filesize mismatch: $options->{ 'index' }.seq != $index->{ 'FILE_SIZE' }) ); +} + +$fh = Maasha::Filesys::file_read_open( $options->{ 'index' } . ".seq" ); + + +if ( $options->{ 'seq_name' } and exists $index->{ $options->{ 'seq_name' } } ) +{ + ( $index_beg, $index_len ) = @{ $index->{ $options->{ 'seq_name' } } }; + + $beg = $options->{ 'beg' }; + $end = $options->{ 'end' }; + $len = $options->{ 'len' }; + + $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len ); + + if ( $seq ) + { + $record->{ 'SEQ_NAME' } = $options->{ 'seq_name' }; + $record->{ 'SEQ' } = $seq; + $record->{ 'SEQ_LEN' } = length $record->{ 'SEQ' }; + + Maasha::Biopieces::put_record( $record, $out ); + } +} + + +while ( $record = Maasha::Biopieces::get_record( $in ) ) +{ + if ( exists $index->{ $record->{ 'SEQ_NAME' } } ) + { + ( $index_beg, $index_len ) = @{ $index->{ $record->{ 'SEQ_NAME' } } }; + + $beg = $record->{ 'BEG' } || $options->{ 'beg' }; + $end = $record->{ 'END' } || $options->{ 'end' }; + $len = $record->{ 'LEN' } || $options->{ 'len' }; + + $seq = seq_get( $fh, $index_beg, $index_len, $beg, $end, $len ); + + if ( $seq ) + { + $record->{ 'SEQ' } = $seq; + $record->{ 'SEQ_LEN' } = length $record->{ 'SEQ' }; + } + } + + Maasha::Biopieces::put_record( $record, $out ); +} + +close $fh; + +Maasha::Biopieces::close_stream( $in ); +Maasha::Biopieces::close_stream( $out ); + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +sub seq_get +{ + # Martin A. Hansen, September 2009. + + # Adjust the index coordianates according to specified options, + # and retrieves the sequence thus specified. The sequence is + # returned. + + my ( $fh, # filehandle to sequence file + $index_beg, # begin position of sequence entry + $index_len, # length of sequence entry + $opt_beg, # optional begin position - OPTIONAL + $opt_end, # optional end position - OPTIONAL + $opt_len, # optional length - OPTIONAL + ) = @_; + + # Returns a string. + + my ( $beg, $len, $seq ); + + $beg = $index_beg; + + if ( $opt_beg ) { + $beg += $opt_beg; + } + + if ( $opt_len ) + { + $len = $opt_len; + } + elsif ( $opt_end ) + { + $len = $opt_end - $opt_beg; + } + else + { + $len = $index_len - $opt_beg; + } + + if ( $len > $index_len - $opt_beg ) { + $len = $index_len - $opt_beg; + } + + return if $beg > $index_beg + $index_len - 1 or $len <= 0; + + $seq = Maasha::Filesys::file_read( $fh, $beg, $len ); + + return $seq; +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +BEGIN +{ + Maasha::Biopieces::status_set(); +} + + +END +{ + Maasha::Biopieces::status_log(); +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +__END__ diff --git a/code_perl/Maasha/Filesys.pm b/code_perl/Maasha/Filesys.pm index e8e40a7..4033064 100644 --- a/code_perl/Maasha/Filesys.pm +++ b/code_perl/Maasha/Filesys.pm @@ -33,6 +33,7 @@ use warnings; use strict; use IO::File; use Storable; +use Data::Dumper; use Maasha::Common; use Exporter; @@ -302,7 +303,7 @@ sub file_size # returns integer - my $file_size = ( stat ( $path ) )[ 7 ]; + my $file_size = -s $path; return $file_size; } -- 2.39.5