]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/slice_align
586c2701570dfbd46d546a3cdf0e3bc012f731e5
[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 # Join sequences in the stream.
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 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
64   input.each_record do |record|
65     if record[:SEQ]
66       entry = Seq.new(seq: record[:SEQ])
67
68       unless options[:beg]
69         compact = Seq.new(seq: entry.seq.dup)
70         compact.seq.delete! "-.~"
71
72         fmatch = compact.patmatch(options[:forward],
73                                   max_mismatches: options[:mismatches],
74                                   max_insertions: options[:insertions],
75                                   max_deletions: options[:deletions])
76
77         raise "forward primer: #{options[:forward]} not found" if fmatch.nil?
78
79         rmatch = compact.patmatch(options[:reverse],
80                                   max_mismatches: options[:mismatches],
81                                   max_insertions: options[:insertions],
82                                   max_deletions: options[:deletions])
83
84         raise "reverse primer: #{options[:reverse]} not found" if rmatch.nil?
85
86         mbeg = fmatch.pos
87         mend = rmatch.pos + rmatch.length - 1
88
89         indels = Regexp.new(/-|\.|~/)
90
91         i = 0
92
93         while entry.seq[i]
94           unless entry.seq[i].match indels
95             if mbeg > 0
96               mbeg -= 1
97               mend -= 1
98             else
99               options[:beg] = i
100               break
101             end
102           end
103
104           i += 1
105         end
106
107         while entry.seq[i]
108           unless entry.seq[i].match indels
109             if mend > 0
110               mend -= 1
111             else
112               options[:end] = i
113               break
114             end
115           end
116
117           i += 1
118         end
119       end
120
121       record[:SEQ]     = entry[options[:beg] .. options[:end]].seq
122       record[:SEQ_LEN] = record[:SEQ].length
123     end
124
125     output.puts record
126   end
127 end
128
129
130 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
131
132
133 __END__