- {
- $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 );
-}
+ raise "Failed to parse seq_name: #{entry.seq_name} in patscan result"
+ end
+ end
+ end
+
+ results
+ end
+
+ private
+
+ # Method to save a patscan pattern to a file.
+ def pattern_save
+ File.open(@pat_file, "w") do |ios|
+ ios.puts @pattern
+ end
+ end
+
+ # Subclass to define Patscan hits.
+ class Match
+ attr_reader :start, :stop, :strand, :match
+
+ def initialize(start, stop, strand, match)
+ @start = start
+ @stop = stop
+ @strand = strand
+ @match = match
+ end
+
+ def length
+ @stop - @start + 1
+ end
+ end
+end
+
+casts = []
+casts << {:long=>'pattern', :short=>'p', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'inline', :short=>'i', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'comp', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+casts << {:long=>'max_hits', :short=>'h', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
+casts << {:long=>'max_misses', :short=>'m', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
+
+options = Biopieces.options_parse(ARGV, casts)
+
+tmp_dir = Biopieces.mktmpdir
+tmp_file = File.join(tmp_dir, "tmp.stream")
+pat_file = File.join(tmp_dir, "pat.txt")
+in_file = File.join(tmp_dir, "in.fna")
+out_file = File.join(tmp_dir, "out.fna")
+out_file = File.join(tmp_dir, "out.fna")
+
+seq_name_count = 0
+seq_name_hash = {}
+seq_type = nil
+
+Biopieces.open(options[:stream_in], tmp_file) do |input, output|
+ Fasta.open(in_file, mode="w") do |fasta_io|
+ input.each_record do |record|
+ if record.has_key? :SEQ_NAME
+ seq_name_hash[seq_name_count] = record[:SEQ_NAME]
+ record[:SEQ_NAME] = seq_name_count
+ seq_name_count += 1
+
+ seq = Seq.new_bp(record)
+
+ if seq_type.nil?
+ seq_type = seq.type_guess
+ end
+
+ fasta_io.puts seq.to_fasta
+ end
+
+ output.puts record
+ end
+ end
+end
+
+patscan = Patscan.new(pat_file, in_file, out_file, options[:pattern])
+patscan.run(options[:comp], seq_type, options[:max_hits], options[:max_misses], options[:verbose])
+results = patscan.results_parse
+
+Biopieces.open(tmp_file, options[:stream_out]) do |input, output|
+ input.each_record do |record|
+ if record.has_key? :SEQ_NAME
+ key = record[:SEQ_NAME].to_i
+ record[:SEQ_NAME] = seq_name_hash[key]
+
+ if options[:inline]
+ if results.has_key? key
+ results[key].each do |result|
+ record[:PATTERN] = options[:pattern]
+ record[:MATCH] = result.match
+ record[:S_BEG] = result.start
+ record[:S_END] = result.stop
+ record[:STRAND] = result.strand
+ record[:MATCH_LEN] = result.length
+
+ output.puts record
+ end
+ else
+ output.puts record
+ end
+ else
+ output.puts record
+
+ new_record = {}
+
+ results[key].each do |result|
+ new_record[:REC_TYPE] = "PATSCAN"
+ new_record[:S_ID] = record[:SEQ_NAME]
+ new_record[:Q_ID] = options[:pattern]
+ new_record[:MATCH] = result.match
+ new_record[:S_BEG] = result.start
+ new_record[:S_END] = result.stop
+ new_record[:STRAND] = result.strand
+ new_record[:SCORE] = 100
+ new_record[:MATCH_LEN] = result.length
+
+ output.puts new_record
+ end
+ end
+ else
+ output.puts record
+ end
+ end
+end