From 26dd404dbd0919eea500ee2d2aa2ceef46fc4e32 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Thu, 28 May 2009 09:43:16 +0000 Subject: [PATCH] migrated patscan_seq git-svn-id: http://biopieces.googlecode.com/svn/trunk@434 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/patscan_seq | 243 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 3 deletions(-) diff --git a/bp_bin/patscan_seq b/bp_bin/patscan_seq index fdf5bd2..800eac7 100755 --- a/bp_bin/patscan_seq +++ b/bp_bin/patscan_seq @@ -1,6 +1,243 @@ -#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# Scan sequences in the stream or a specified genome for patterns using patscan. + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + -use warnings; use strict; +use Maasha::Biopieces; +use Maasha::Fasta; +use Maasha::Filesys; +use Maasha::Patscan; + +use constant { + SEQ_NAME => 0, + SEQ => 1, +}; + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, @args, $arg, $type, $tmp_dir, + $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $patterns, $pattern, $entry, $result, %head_hash, $i ); + +$options = Maasha::Biopieces::parse_options( + [ + { long => 'patterns', short => 'p', type => 'string', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'patterns_in', short => 'P', type => 'file!', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'comp', short => 'c', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef }, + { long => 'max_hits', short => 'h', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + { long => 'max_misses', short => 'm', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + { long => 'genome', short => 'g', type => 'genome', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 }, + ] +); + +$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } ); +$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } ); + +$tmp_dir = Maasha::Biopieces::get_tmpdir(); + +$pat_file = "$tmp_dir/patscan.pat"; +$out_file = "$tmp_dir/patscan.out"; + +$patterns = parse_patterns( $options ); +$arg = parse_args( $options ); + +if ( defined $options->{ 'genome' } ) +{ + $seq_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna"; +} +else +{ + $seq_file = "$tmp_dir/patscan.seq"; + + $fh_out = Maasha::Filesys::file_write_open( $seq_file ); + + $i = 0; + + while ( $record = Maasha::Biopieces::get_record( $in ) ) + { + if ( $entry = Maasha::Biopieces::biopiece2fasta( $record ) ) + { + $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type; + + $entry->[ SEQ_NAME ] = $i; + + Maasha::Fasta::put_entry( $entry, $fh_out ); + + $head_hash{ $i } = $record->{ "SEQ_NAME" }; + + $i++; + } + } + + close $fh_out; + + $arg .= " -p" if $type eq "protein"; +} + +foreach $pattern ( @{ $patterns } ) +{ + pat_write( $pat_file, $pattern ); + + `scan_for_matches $arg $pat_file < $seq_file > $out_file`; + # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" ); + + $fh_in = Maasha::Filesys::file_read_open( $out_file ); + + while ( $entry = Maasha::Fasta::get_entry( $fh_in ) ) + { + $result = Maasha::Patscan::parse_scan_result( $entry, $pattern ); + + if ( $options->{ 'genome' } ) + { + $result->{ "CHR" } = $result->{ "S_ID" }; + $result->{ "CHR_BEG" } = $result->{ "S_BEG" }; + $result->{ "CHR_END" } = $result->{ "S_END" }; + + delete $result->{ "S_ID" }; + delete $result->{ "S_BEG" }; + delete $result->{ "S_END" }; + } + else + { + $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } }; + } + + Maasha::Biopieces::put_record( $result, $out ); + } + + close $fh_in; +} + +unlink $pat_file; +unlink $out_file; +unlink $seq_file if not $options->{ 'genome' }; + +Maasha::Filesys::dir_remove( $tmp_dir ); + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +sub parse_patterns +{ + # Martin A. Hansen, May 2009. + + # Parse pattern arguments based on information in the + # options hash and returns a list of patterns. + # Raises an error if no info. + + my ( $options, # options hash + ) = @_; + + # Returns a list. + + my ( $patterns ); + + if ( $options->{ "patterns" } ) { + $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } ); + } elsif ( -f $options->{ "patterns_in" } ) { + $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } ); + } else { + Maasha::Common::error( qq(no patterns specified.) ); + } + + return wantarray ? @{ $patterns } : $patterns; +} + + +sub parse_args +{ + # Martin A. Hansen, May 2009. + + # Generate an argument string for executing scan_for_matches based + # on information in the option hash. + + my ( $options, # options hash + ) = @_; + + # Returns a string. + + my ( @args ); + + push @args, "-c" if $options->{ "comp" }; + push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' }; + push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' }; + + return join " ", @args; +} + + +sub pat_write +{ + # Martin A. Hansen, May 2009. + + # Write a scan_for_matches pattern to file. + + my ( $file, # target file to write pattern to. + $pattern, # pattern to write. + ) = @_; + + # Returns nothing. + + my ( $fh_out ); + + $fh_out = Maasha::Common::write_open( $pat_file ); + + print $fh_out "$pattern\n"; + + close $fh_out; +} + + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + + +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; -- 2.39.5