3 # Copyright (C) 2007-2011 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Run Patscan on sequences in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
33 require 'maasha/fasta'
36 # Class with methods to execute Patscan (a.k.a. scan_for_matches).
38 # Method to initialize a Patscan object.
39 def initialize(pat_file, in_file, out_file, pattern)
48 # Method to run Patscan
49 def run(comp = nil, type = nil, max_mis = nil, max_hit = nil, verbose = nil)
51 args << "scan_for_matches"
53 args << "-p" if type == 'protein'
54 args << "-n #{max_mis}" if max_mis
55 args << "-m #{max_hit}" if max_hit
57 args << "< #{@in_file}"
58 args << "> #{@out_file}"
59 command = args.join(" ")
60 $stderr.puts command if verbose
62 raise "Command failed: #{command}" unless $?.success?
65 # Method to parse Patscan results and return
66 # these in a hash with ID as key and a list
69 results = Hash.new { |h, k| h[k] = [] }
71 Fasta.open(@out_file, mode='r') do |ios|
73 if entry.seq_name =~ /([^:]+):\[(\d+),(\d+)\]/
81 start, stop = stop, start
85 results[id.to_i] << Match.new(start, stop, strand, entry.seq)
87 raise "Failed to parse seq_name: #{entry.seq_name} in patscan result"
97 # Method to save a patscan pattern to a file.
99 File.open(@pat_file, "w") do |ios|
104 # Subclass to define Patscan hits.
106 attr_reader :start, :stop, :strand, :match
108 def initialize(start, stop, strand, match)
122 casts << {:long=>'pattern', :short=>'p', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
123 casts << {:long=>'inline', :short=>'i', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
124 casts << {:long=>'comp', :short=>'c', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
125 casts << {:long=>'max_hits', :short=>'h', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
126 casts << {:long=>'max_misses', :short=>'m', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>'0'}
128 options = Biopieces.options_parse(ARGV, casts)
130 tmp_dir = Biopieces.mktmpdir
131 tmp_file = File.join(tmp_dir, "tmp.stream")
132 pat_file = File.join(tmp_dir, "pat.txt")
133 in_file = File.join(tmp_dir, "in.fna")
134 out_file = File.join(tmp_dir, "out.fna")
135 out_file = File.join(tmp_dir, "out.fna")
141 Biopieces.open(options[:stream_in], tmp_file) do |input, output|
142 Fasta.open(in_file, mode="w") do |fasta_io|
143 input.each_record do |record|
144 if record.has_key? :SEQ_NAME
145 seq_name_hash[seq_name_count] = record[:SEQ_NAME]
146 record[:SEQ_NAME] = seq_name_count
150 seq = Seq.new("", record[:SEQ])
151 seq_type = seq.type_guess
161 patscan = Patscan.new(pat_file, in_file, out_file, options[:pattern])
162 patscan.run(options[:comp], seq_type, options[:max_hits], options[:max_misses], options[:verbose])
163 results = patscan.results_parse
165 Biopieces.open(tmp_file, options[:stream_out]) do |input, output|
166 input.each_record do |record|
167 if record.has_key? :SEQ_NAME
168 key = record[:SEQ_NAME].to_i
169 record[:SEQ_NAME] = seq_name_hash[key]
172 if results.has_key? key
173 results[key].each do |result|
174 record[:PATTERN] = options[:pattern]
175 record[:MATCH] = result.match
176 record[:S_BEG] = result.start
177 record[:S_END] = result.stop
178 record[:STRAND] = result.strand
179 record[:MATCH_LEN] = result.length
191 results[key].each do |result|
192 new_record[:REC_TYPE] = "PATSCAN"
193 new_record[:S_ID] = record[:SEQ_NAME]
194 new_record[:Q_ID] = options[:pattern]
195 new_record[:MATCH] = result.match
196 new_record[:S_BEG] = result.start
197 new_record[:S_END] = result.stop
198 new_record[:STRAND] = result.strand
199 new_record[:SCORE] = 100
200 new_record[:MATCH_LEN] = result.length
202 output.puts new_record
212 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<