]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/calc_N50
argh!
[biopieces.git] / bp_bin / calc_N50
1 #!/usr/bin/env ruby
2
3 #arg!
4
5 # Copyright (C) 2007-2011 Martin A. Hansen.
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 # http://www.gnu.org/copyleft/gpl.html
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 # This program is part of the Biopieces framework (www.biopieces.org).
26
27 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
28
29 # Calculate n50 for sequences in the stream.
30
31 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32
33
34 require 'biopieces'
35 require 'pp'
36
37 casts = []
38 casts << {:long=>'no_stream', :short=>'x', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
39 casts << {:long=>'data_out',  :short=>'o', :type=>'file', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
40
41 bp = Biopieces.new
42
43 options = bp.parse(ARGV, casts)
44
45 total   = 0
46 lengths = []
47
48 bp.each_record do |record|
49   bp.puts record unless options[:no_stream]
50
51   if record.has_key? :SEQ
52     total   += record[:SEQ].length
53     lengths << record[:SEQ].length
54   end
55 end
56
57 bp.out = Stream.write(options[:data_out])
58
59 count = 0
60
61 lengths.sort.reverse.each do |length|
62   count += length
63
64   if count >= total * 0.50
65     bp.puts "N50" => length
66     break
67   end
68 end
69
70 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
71
72
73 __END__