]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/find_homopolymers
revamped find_homopolymers
[biopieces.git] / bp_bin / find_homopolymers
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2013 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 # Find homopolymers in sequences in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31
32 require 'maasha/biopieces'
33 require 'maasha/seq'
34 require 'pp'
35
36 class Seq; include Homopolymer; end
37
38 casts = []
39 casts << {:long=>'min',     :short=>'m', :type=>'uint', :mandatory=>false, :default=>1,   :allowed=>nil, :disallowed=>"0"}
40 casts << {:long=>'limit',   :short=>'l', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0"}
41 casts << {:long=>'longest', :short=>'L', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
42
43 options = Biopieces.options_parse(ARGV, casts)
44
45 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
46   input.each_record do |record|
47     if record[:SEQ]
48       seq = Seq.new(nil, record[:SEQ])
49
50       longest = Seq::Homopolymer.new("", 0, 0)
51       got_one = false
52       count   = 0
53
54       seq.each_homopolymer(options[:min]) do |h|
55         got_one = true
56
57         record[:HOMOPOL_PAT] = h.pattern
58         record[:HOMOPOL_LEN] = h.length
59         record[:HOMOPOL_POS] = h.pos
60
61         if options[:longest]
62           longest = h.length > longest.length ? h : longest
63         else
64           output.puts record
65
66           count += 1
67
68           break if options[:limit] and options[:limit] == count
69         end
70       end
71
72       if options[:longest]
73         if longest.length > options[:min]
74           record[:HOMOPOL_PAT] = longest.pattern
75           record[:HOMOPOL_LEN] = longest.length
76           record[:HOMOPOL_POS] = longest.pos
77         end
78
79         output.puts record
80       elsif not got_one
81         output.puts record
82       end
83     else
84       output.puts record
85     end
86   end
87 end
88
89
90 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
91
92
93 __END__