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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 def initialize(tmpdir, infile, options)
41 pattern = Pattern.new(options[:forward], options[:reverse], options[:max_dist])
42 @pat_files = pattern.save(tmpdir)
45 # Run scan_for_matches using different pattern files on the subject
46 # FASTA file and return a list of the result files.
50 @pat_files.each do |pat_file|
51 outfile = pat_file.sub("pat", "fna")
53 command = "scan_for_matches"
55 command << " #{pat_file}"
56 command << " < #{@infile}"
57 command << " > #{outfile}"
60 raise "Command failed: #{command}" unless $?.success?
70 attr_accessor :forward, :reverse
72 def initialize(forward, reverse, max_dist)
78 # For each primer pair we need to check 4 possible
79 # combinations that can give rise to PCR products:
80 # - forward and reverse (the intended product)
81 # - forward and revcomp forward
82 # - revcomp reverse and reverse
83 # - revcomp reverse and revcomp forward
84 # Thus we create 4 pattern files and return
89 revcomp_forward = revcomp(forward)
90 revcomp_reverse = revcomp(reverse)
94 file = File.join(tmpdir, "forward_reverse.pat")
95 self.forward = forward
96 self.reverse = reverse
100 file = File.join(tmpdir, "forward_forward.pat")
101 self.forward = forward
102 self.reverse = revcomp_forward
106 file = File.join(tmpdir, "reverse_reverse.pat")
107 self.forward = revcomp_reverse
108 self.reverse = reverse
112 file = File.join(tmpdir, "reverse_forward.pat")
113 self.forward = revcomp_reverse
114 self.reverse = revcomp_forward
123 # Method to output a pattern.
125 "#{@forward} 1 ... #{@max_dist} #{@reverse}"
128 # Save a pattern to file
129 def save_pattern(file)
130 File.open(file, mode="w") do |ios|
135 # Split a primer pattern in the form of ATCG[3,2,1] into
136 # sequence and match descriptor, reverse complement the
137 # primer and append the match descriptor: CGAT[3,2,1].
139 if pattern.match(/^(\w+)(\[.+\])?/)
143 raise "Failed splitting pattern: #{pattern}"
151 descriptor ? seq.seq + descriptor : seq.seq
156 casts << {:long=>'forward', :short=>'f', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
157 casts << {:long=>'reverse', :short=>'r', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
158 casts << {:long=>'max_dist', :short=>'m', :type=>'uint', :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
162 options = bp.parse(ARGV, casts)
164 infile = File.join(tmpdir, "in.fna")
166 Fasta.open(infile, mode="w") do |ios|
167 bp.each_record do |record|
173 outfiles = Pcr.new(tmpdir, infile, options).run
175 outfiles.each do |outfile|
176 Fasta.open(outfile, mode="r") do |ios|
179 record[:REC_TYPE] = "PCR"
180 record[:STRAND] = "+"
181 record[:TYPE] = File.basename(outfile).sub(".fna", "").upcase
182 record[:SEQ_NAME].match(/(.+):\[(\d+),(\d+)\]$/)
183 record[:SEQ_NAME] = $1
184 record[:PCR_BEG] = $2
185 record[:PCR_END] = $3
187 if record[:PCR_BEG] > record[:PCR_END]
188 record[:PCR_BEG], record[:PCR_END] = record[:PCR_END], record[:PCR_BEG]
189 record[:STRAND] = "-"
198 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<