]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_stacked_histogram
added ugly fix to ruby gnuplot dumb print problem
[biopieces.git] / bp_bin / plot_stacked_histogram
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 # Plot the nucleotide distribution in percent of sequences in the stream.
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 require 'maasha/biopieces'
28 require 'gnuplot'
29 require 'pp'
30
31 terminals = "dumb,x11,aqua,post,pdf,png,svg"
32 title     = "Stacked Histogram"
33
34 casts = []
35 casts << {:long=>'no_stream', :short=>'x', :type=>'flag',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
36 casts << {:long=>'cols',      :short=>'c', :type=>'string', :mandatory=>true,  :default=>nil,    :allowed=>nil,       :disallowed=>nil}
37 casts << {:long=>'rows',      :short=>'r', :type=>'string', :mandatory=>true,  :default=>nil,    :allowed=>nil,       :disallowed=>nil}
38 casts << {:long=>'data_out',  :short=>'o', :type=>'file',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
39 casts << {:long=>'terminal',  :short=>'t', :type=>'string', :mandatory=>false, :default=>'dumb', :allowed=>terminals, :disallowed=>nil}
40 casts << {:long=>'title',     :short=>'T', :type=>'string', :mandatory=>false, :default=>title,  :allowed=>nil,       :disallowed=>nil}
41 casts << {:long=>'xlabel',    :short=>'X', :type=>'string', :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
42 casts << {:long=>'ylabel',    :short=>'Y', :type=>'string', :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
43
44 options = Biopieces.options_parse(ARGV, casts)
45 options[:xlabel] ||= options[:cols].capitalize
46 options[:ylabel] ||= options[:rows].capitalize
47
48 class PlotData
49   include Enumerable
50
51   def initialize()
52     @data       = Hash.new { |h, k| h[k] = Hash.new(0) }
53     @seen_cols  = {}
54     @seen_rows  = {}
55     @table      = []
56   end
57
58   def add(row, col)
59     @data[col][row] += 1
60     @seen_cols[col]  = true
61     @seen_rows[row]  = true
62   end
63
64   def each
65     tabulize() if @table.empty?
66
67     @table.each do |row|
68       yield row
69     end
70   end
71
72   def row_titles(i)
73     @seen_rows.keys[i]
74   end
75
76   def col_titles
77     titles = []
78
79     @seen_cols.keys.each_with_index do |col, i|
80       titles << %{"#{col}" #{i}}
81     end
82
83     "(" + titles.join(", ") + ")"
84   end
85
86   private
87
88   def tabulize
89     @seen_rows.size.times { @table << Array.new(@seen_cols.size, 0) }  # table init
90
91     i = 0
92
93     @seen_cols.each_key do |col|
94       j = 0
95
96       @seen_rows.each_key do |row|
97         @table[j][i] = @data[col][row]
98
99         j += 1
100       end
101
102       i += 1
103     end
104
105     @table
106   end
107 end
108
109 data = PlotData.new()
110
111 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
112   input.each_record do |record|
113     cols = record[options[:cols].to_sym]
114     rows = record[options[:rows].to_sym]
115
116     if cols and rows
117       data.add(rows, cols)
118     end
119
120     output.puts record unless options[:no_stream]
121   end
122 end
123
124 Gnuplot.open do |gp|
125   Gnuplot::Plot.new(gp) do |plot|
126     plot.terminal options[:terminal]
127     plot.title    options[:title]
128     plot.xlabel   options[:xlabel]
129     plot.ylabel   options[:ylabel]
130     plot.output   options[:data_out] || "/dev/stderr"
131     plot.ytics    "out"
132     plot.auto     "fix"
133     plot.key      "outside right top vertical Left reverse enhanced autotitles columnhead nobox"
134     plot.key      "invert samplen 4 spacing 1 width 0 height 0"
135     plot.style    "fill solid 0.5 border"
136     plot.style    "histogram rowstacked"
137     plot.style    "data histograms"
138     plot.boxwidth "0.75 absolute"
139     plot.xtics    "out"
140     plot.xtics    "norangelimit"
141     plot.xtics    data.col_titles
142
143     data.each_with_index do |row, i|
144       plot.data << Gnuplot::DataSet.new(row.unshift(0)) do |ds|
145         ds.using = "1:xtic(2)"
146         ds.title = data.row_titles(i)
147       end
148     end
149   end
150 end
151
152 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
153
154
155 __END__