#!/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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Scan sequences in the stream or a specified genome for patterns using patscan.
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
use warnings;
use strict;
+use Maasha::Biopieces;
+use Maasha::Common;
+use Maasha::Fasta;
+use Maasha::Filesys;
+use Maasha::Patscan;
+
+use constant {
+ SEQ_NAME => 0,
+ SEQ => 1,
+};
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+my ( $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::Fasta::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::Biopieces::close_stream( $in );
+Maasha::Biopieces::close_stream( $out );
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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
+{
+ Maasha::Biopieces::status_set();
+}
+
+
+END
+{
+ Maasha::Biopieces::status_log();
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
-use Maasha::BioRun;
+__END__