]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/trim_seq
upgraded read_fastq to handle illumina1.8 better
[biopieces.git] / bp_bin / trim_seq
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 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 # Trim sequence ends for residues with a low quality score.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'pp'
33
34 casts = []
35 casts << {:long=>'min',  :short=>'m', :type=>'uint',   :mandatory=>true, :default=>15,     :allowed=>nil, :disallowed=>'0'}
36 casts << {:long=>'trim', :short=>'t', :type=>'string', :mandatory=>true, :default=>'both', :allowed=>'left,right,both', :disallowed=>nil}
37
38 options = Biopieces.options_parse(ARGV, casts)
39
40 ILLUMINA_BASE = 64
41
42 regex_left  = Regexp.new("^[#{(ILLUMINA_BASE).chr}-#{(ILLUMINA_BASE + options[:min]).chr}]+")
43 regex_right = Regexp.new("[#{(ILLUMINA_BASE).chr}-#{(ILLUMINA_BASE + options[:min]).chr}]+$")
44
45 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
46   input.each_record do |record|
47     if record.has_key? :SEQ and record.has_key? :SCORES
48       case options[:trim]
49       when /both/
50         record[:SCORES].match(regex_right) do |m|
51           record[:SEQ]       = record[:SEQ][0 ... record[:SEQ].length - m.to_s.length]
52           record[:SCORES]    = $`
53           record[:SEQ_LEN]   = record[:SEQ].length
54           record[:TRIM_LEFT] = m.to_s.length
55         end
56         record[:SCORES].match(regex_left) do |m|
57           record[:SEQ]        = record[:SEQ][m.to_s.length ... record[:SEQ].length]
58           record[:SCORES]     = $'
59           record[:SEQ_LEN]    = record[:SEQ].length
60           record[:TRIM_RIGHT] = $'.length - 1
61         end
62       when /left/
63         record[:SCORES].match(regex_left) do |m|
64           record[:SEQ]       = record[:SEQ][m.to_s.length ... record[:SEQ].length]
65           record[:SCORES]    = $'
66           record[:SEQ_LEN]   = record[:SEQ].length
67           record[:TRIM_LEFT] = m.to_s.length
68         end
69       when /right/
70         record[:SCORES].match(regex_right) do |m|
71           record[:SEQ]        = record[:SEQ][0 ... record[:SEQ].length - m.to_s.length]
72           record[:SCORES]     = $`
73           record[:SEQ_LEN]    = record[:SEQ].length
74           record[:TRIM_RIGHT] = $`.length - 1
75         end
76       end
77     end
78
79     output.puts record
80   end
81 end
82
83
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
85
86
87 __END__