3 # Copyright (C) 2007-2010 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 # Runs virtual PCR on sequences in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
33 require 'maasha/fasta'
37 def initialize(tmpdir, infile, options)
40 pattern = Pattern.new(options[:forward], options[:reverse], options[:max_dist])
41 @pat_files = pattern.save(tmpdir)
44 # Run scan_for_matches using different pattern files on the subject
45 # FASTA file and return a list of the result files.
49 @pat_files.each do |pat_file|
50 outfile = pat_file.sub("pat", "fna")
52 command = "scan_for_matches"
54 command << " #{pat_file}"
55 command << " < #{@infile}"
56 command << " > #{outfile}"
59 raise "Command failed: #{command}" unless $?.success?
69 attr_accessor :forward, :reverse
71 def initialize(forward, reverse, max_dist)
77 # For each primer pair we need to check 4 possible
78 # combinations that can give rise to PCR products:
79 # - forward and reverse (the intended product)
80 # - forward and revcomp forward
81 # - revcomp reverse and reverse
82 # - revcomp reverse and revcomp forward
83 # Thus we create 4 pattern files and return
88 revcomp_forward = revcomp(forward)
89 revcomp_reverse = revcomp(reverse)
93 file = File.join(tmpdir, "forward_reverse.pat")
94 self.forward = forward
95 self.reverse = reverse
99 file = File.join(tmpdir, "forward_forward.pat")
100 self.forward = forward
101 self.reverse = revcomp_forward
105 file = File.join(tmpdir, "reverse_reverse.pat")
106 self.forward = revcomp_reverse
107 self.reverse = reverse
111 file = File.join(tmpdir, "reverse_forward.pat")
112 self.forward = revcomp_reverse
113 self.reverse = revcomp_forward
122 # Method to output a pattern.
124 "#{@forward} 1 ... #{@max_dist} #{@reverse}"
127 # Save a pattern to file
128 def save_pattern(file)
129 File.open(file, mode="w") do |ios|
134 # Split a primer pattern in the form of ATCG[3,2,1] into
135 # sequence and match descriptor, reverse complement the
136 # primer and append the match descriptor: CGAT[3,2,1].
138 if pattern.match(/^(\w+)(\[.+\])?/)
142 raise "Failed splitting pattern: #{pattern}"
150 descriptor ? seq.seq + descriptor : seq.seq
155 casts << {:long=>'forward', :short=>'f', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
156 casts << {:long=>'reverse', :short=>'r', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
157 casts << {:long=>'max_dist', :short=>'m', :type=>'uint', :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
159 options = Biopieces.options_parse(ARGV, casts)
160 tmpdir = Biopieces.mktmpdir
161 infile = File.join(tmpdir, "in.fna")
163 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
164 Fasta.open(infile, mode="w") do |ios|
165 input.each_record do |record|
171 outfiles = Pcr.new(tmpdir, infile, options).run
173 outfiles.each do |outfile|
174 Fasta.open(outfile, mode="r") do |ios|
177 record[:REC_TYPE] = "PCR"
178 record[:STRAND] = "+"
179 record[:TYPE] = File.basename(outfile).sub(".fna", "").upcase
180 record[:SEQ_NAME].match(/(.+):\[(\d+),(\d+)\]$/)
181 record[:SEQ_NAME] = $1
182 record[:PCR_BEG] = $2
183 record[:PCR_END] = $3
185 if record[:PCR_BEG] > record[:PCR_END]
186 record[:PCR_BEG], record[:PCR_END] = record[:PCR_END], record[:PCR_BEG]
187 record[:STRAND] = "-"
197 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<