]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_distribution
added ugly fix to ruby gnuplot dumb print problem
[biopieces.git] / bp_bin / plot_distribution
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 # Plot the distribution of numerical values for a specified key.
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27
28 require 'maasha/biopieces'
29 require 'gnuplot'
30 require 'pp'
31
32 terminals = "dumb,x11,aqua,post,pdf,png,svg"
33 title     = "Distribution"
34 ylabel    = "n"
35
36 casts = []
37 casts << {:long=>'key',        :short=>'k', :type=>'string', :mandatory=>true,  :default=>nil,    :allowed=>nil,       :disallowed=>nil}
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 casts << {:long=>'terminal',   :short=>'t', :type=>'string', :mandatory=>false, :default=>'dumb', :allowed=>terminals, :disallowed=>nil}
41 casts << {:long=>'title',      :short=>'T', :type=>'string', :mandatory=>false, :default=>title,  :allowed=>nil,       :disallowed=>nil}
42 casts << {:long=>'xlabel',     :short=>'X', :type=>'string', :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
43 casts << {:long=>'ylabel',     :short=>'Y', :type=>'string', :mandatory=>false, :default=>ylabel, :allowed=>nil,       :disallowed=>nil}
44 casts << {:long=>'logscale_y', :short=>'L', :type=>'flag',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
45
46 options = Biopieces.options_parse(ARGV, casts)
47
48 key = options[:key].to_sym
49
50 options[:xlabel] = key unless options[:xlabel]
51 options[:ylabel] = "log10(#{options[:ylabel]})" if options[:logscale_y]
52
53 count_hash = Hash.new(0)
54
55 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
56   input.each_record do |record|
57     if record[key]
58       count_hash[record[key].to_i] += 1
59     end
60
61     output.puts record unless options[:no_stream]
62   end
63 end
64
65 x_max = count_hash.keys.max
66
67 x = []
68 y = []
69
70 (0 .. x_max).each do |i|
71   x << i
72   y << count_hash[i]
73 end
74
75 Gnuplot.open do |gp|
76   Gnuplot::Plot.new(gp) do |plot|
77     plot.terminal options[:terminal]
78     plot.title    options[:title]
79     plot.xlabel   options[:xlabel]
80     plot.ylabel   options[:ylabel]
81     plot.output   options[:data_out] || "/dev/stderr"
82     plot.logscale "y"                if options[:logscale_y]
83     plot.xrange   "[#{x.min - 1}:#{x.max + 1}]"
84     plot.style    "fill solid 0.5 border"
85     plot.xtics    "out"
86     plot.ytics    "out"
87     
88     plot.data << Gnuplot::DataSet.new([x, y]) do |ds|
89       ds.with = "boxes"
90       ds.notitle
91     end
92   end
93 end
94
95
96 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
97
98
99 __END__