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"
55 command << " #{pat_file}"
56 command << " < #{@infile}"
57 command << " > #{outfile}"
60 raise "Command failed: #{command}" unless $?.success?
70 attr_accessor :forward, :reverse
72 # Split a primer pattern in the form of ATCG[3,2,1] into
73 # sequence and match descriptor, reverse complement the
74 # primer and append the match descriptor: CGAT[3,2,1].
75 def self.revcomp(pattern)
76 if pattern.match(/^(\w+)(\[.+\])?/)
80 raise "Failed splitting pattern: #{pattern}"
86 seq.reverse!.complement!
88 descriptor ? seq.seq + descriptor : seq.seq
91 def initialize(forward, reverse, max_dist)
97 # For each primer pair we need to check 4 possible
98 # combinations that can give rise to PCR products:
99 # - forward and reverse (the intended product)
100 # - forward and revcomp forward
101 # - revcomp reverse and reverse
102 # - revcomp reverse and revcomp forward
103 # Thus we create 4 pattern files and return
108 revcomp_forward = Pattern.revcomp(forward)
109 revcomp_reverse = Pattern.revcomp(reverse)
113 file = File.join(tmpdir, "forward_reverse.pat")
114 self.forward = forward
115 self.reverse = reverse
119 file = File.join(tmpdir, "forward_forward.pat")
120 self.forward = forward
121 self.reverse = revcomp_forward
125 file = File.join(tmpdir, "reverse_reverse.pat")
126 self.forward = revcomp_reverse
127 self.reverse = reverse
131 file = File.join(tmpdir, "reverse_forward.pat")
132 self.forward = revcomp_reverse
133 self.reverse = revcomp_forward
142 # Method to output a pattern.
144 "#{@forward} 1 ... #{@max_dist} #{@reverse}"
147 # Save a pattern to file
148 def save_pattern(file)
149 File.open(file, "w") do |ios|
156 casts << {:long=>'forward', :short=>'f', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
157 casts << {:long=>'forward_rc', :short=>'F', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
158 casts << {:long=>'reverse', :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
159 casts << {:long=>'reverse_rc', :short=>'R', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
160 casts << {:long=>'max_dist', :short=>'m', :type=>'uint', :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
162 options = Biopieces.options_parse(ARGV, casts)
163 tmpdir = Biopieces.mktmpdir
164 infile = File.join(tmpdir, "in.fna")
166 if options[:forward_rc]
167 options[:forward] = Pattern.revcomp(options[:forward_rc])
170 if options[:reverse_rc]
171 options[:reverse] = Pattern.revcomp(options[:reverse_rc])
174 raise ArgumentError, "no adaptor specified" unless options[:forward] or options[:reverse]
175 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
176 Fasta.open(infile, "w") do |ios|
177 input.each_record do |record|
181 entry = Seq.new_bp(record)
182 ios.puts entry.to_fasta
187 outfiles = Pcr.new(tmpdir, infile, options).run
189 outfiles.each do |outfile|
190 Fasta.open(outfile, "r") do |ios|
193 record[:REC_TYPE] = "PCR"
194 record[:STRAND] = "+"
195 record[:TYPE] = File.basename(outfile).sub(".fna", "").upcase
196 record[:SEQ_NAME].match(/(.+):\[(\d+),(\d+)\]$/)
197 record[:SEQ_NAME] = $1
198 record[:PCR_BEG] = $2.to_i
199 record[:PCR_END] = $3.to_i
201 if record[:PCR_BEG] > record[:PCR_END]
202 record[:PCR_BEG], record[:PCR_END] = record[:PCR_END], record[:PCR_BEG]
203 record[:STRAND] = "-"
213 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<