]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/slice_align
f63ac7e203c2e41c40c15d294fd11798e760b5d5
[biopieces.git] / bp_bin / slice_align
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2010 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Slice aligned sequences in the stream to obtain subsequences.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
33 require 'maasha/seq'
34
35 casts = []
36 casts << {long: 'beg',            short: 'b', type: 'uint',   mandatory: false, default: nil, allowed: nil, disallowed: "0"}
37 casts << {long: 'end',            short: 'e', type: 'uint',   mandatory: false, default: nil, allowed: nil, disallowed: "0"}
38 casts << {long: 'forward',        short: 'f', type: 'string', mandatory: false, default: nil, allowed: nil, disallowed: nil}
39 casts << {long: 'reverse',        short: 'r', type: 'string', mandatory: false, default: nil, allowed: nil, disallowed: nil}
40 casts << {long: 'reverse_rc',     short: 'R', type: 'string', mandatory: false, default: nil, allowed: nil, disallowed: nil}
41 casts << {long: 'template_file',  short: 't', type: 'file!',  mandatory: false, default: nil, allowed: nil, disallowed: nil}
42 casts << {long: 'mismatches',     short: 'm', type: 'uint',   mandatory: false, default: 2,   allowed: nil, disallowed: nil}
43 casts << {long: 'insertions',     short: 'i', type: 'uint',   mandatory: false, default: 1,   allowed: nil, disallowed: nil}
44 casts << {long: 'deletions',      short: 'd', type: 'uint',   mandatory: false, default: 1,   allowed: nil, disallowed: nil}
45
46 options = Biopieces.options_parse(ARGV, casts)
47
48 if options[:beg]
49   raise "both --beg and --end must be speficied" unless options[:end]
50   options[:beg] -= 1
51   options[:end] -= 1
52   raise "--beg (#{options[:beg]}) must be less than --end (#{options[:end]})" if options[:beg] > options[:end]
53 elsif options[:forward]
54   raise "both --forward and --reverse or --reverse_rc must be specified" unless options[:reverse] or options[:reverse_rc]
55
56   if options[:reverse_rc]
57     options[:reverse] = Seq.new(seq: options[:reverse_rc], type: :dna).reverse.complement.seq
58   end
59 else
60   raise "either --beg/--end or --forward/--reverse|--reverse_rc must be specified"
61 end
62
63 if options[:template_file]
64   template = Fasta.open(options[:template_file]).get_entry
65 end
66
67 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
68   input.each_record do |record|
69     if record[:SEQ]
70       entry = Seq.new(seq: record[:SEQ])
71
72       unless options[:beg]
73         compact = template ? template : Seq.new(seq: entry.seq.dup)
74         compact.seq.delete! "-.~"
75
76         fmatch = compact.patmatch(options[:forward],
77                                   max_mismatches: options[:mismatches],
78                                   max_insertions: options[:insertions],
79                                   max_deletions: options[:deletions])
80
81         raise "forward primer: #{options[:forward]} not found" if fmatch.nil?
82
83         rmatch = compact.patmatch(options[:reverse],
84                                   max_mismatches: options[:mismatches],
85                                   max_insertions: options[:insertions],
86                                   max_deletions: options[:deletions])
87
88         raise "reverse primer: #{options[:reverse]} not found" if rmatch.nil?
89
90         mbeg = fmatch.pos
91         mend = rmatch.pos + rmatch.length - 1
92
93         indels = Regexp.new(/-|\.|~/)
94
95         i = 0
96
97         while entry.seq[i]
98           unless entry.seq[i].match indels
99             if mbeg > 0
100               mbeg -= 1
101               mend -= 1
102             else
103               options[:beg] = i
104               break
105             end
106           end
107
108           i += 1
109         end
110
111         while entry.seq[i]
112           unless entry.seq[i].match indels
113             if mend > 0
114               mend -= 1
115             else
116               options[:end] = i
117               break
118             end
119           end
120
121           i += 1
122         end
123       end
124
125       record[:SEQ]     = entry[options[:beg] .. options[:end]].seq
126       record[:SEQ_LEN] = record[:SEQ].length
127     end
128
129     output.puts record
130   end
131 end
132
133
134 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
135
136
137 __END__