]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_stacked_histogram
cadfcea360adeda1834183d0b52425d82dfa209a
[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
46 class PlotData
47   include Enumerable
48
49   def initialize()
50     @data       = Hash.new { |h, k| h[k] = Hash.new(0) }
51     @seen_cols  = {}
52     @seen_rows  = {}
53     @table      = []
54   end
55
56   def add(row, col)
57     @data[col][row] += 1
58     @seen_cols[col]  = true
59     @seen_rows[row]  = true
60   end
61
62   def each
63     tabulize() if @table.empty?
64
65     @table.each do |row|
66       yield row
67     end
68   end
69
70   def row_titles(i)
71     @seen_rows.keys[i]
72   end
73
74   def col_titles
75     titles = []
76
77     @seen_cols.keys.each_with_index do |col, i|
78       titles << %{"#{col}" #{i}}
79     end
80
81     "(" + titles.join(", ") + ")"
82   end
83
84   private
85
86   def tabulize
87     @seen_rows.size.times { @table << Array.new(@seen_cols.size, 0) }  # table init
88
89     i = 0
90
91     @seen_cols.each_key do |col|
92       j = 0
93
94       @seen_rows.each_key do |row|
95         @table[j][i] = @data[col][row]
96
97         j += 1
98       end
99
100       i += 1
101     end
102
103     @table
104   end
105 end
106
107 data = PlotData.new()
108
109 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
110   input.each_record do |record|
111     cols = record[options[:cols].to_sym]
112     rows = record[options[:rows].to_sym]
113
114     if cols and rows
115       data.add(rows, cols)
116     end
117
118     output.puts record unless options[:no_stream]
119   end
120 end
121
122 Gnuplot.open do |gp|
123   Gnuplot::Plot.new(gp) do |plot|
124     plot.terminal options[:terminal]
125     plot.title    options[:title]
126     plot.xlabel   options[:xlabel]
127     plot.ylabel   options[:ylabel]
128     plot.output   options[:data_out] if options[:data_out]
129     plot.ytics    "out"
130 #    plot.yrange   "[0:100]"
131 #    plot.xrange   "[0:#{max_len}]"
132     plot.auto     "fix"
133     #plot.offsets  "1"
134     plot.key      "outside right top vertical Left reverse enhanced autotitles columnhead nobox"
135     plot.key      "invert samplen 4 spacing 1 width 0 height 0"
136     plot.style    "fill solid 0.5 border"
137     plot.style    "histogram rowstacked"
138     plot.style    "data histograms"
139     plot.boxwidth "0.75 absolute"
140     plot.xtics    "out"
141     plot.xtics    "norangelimit"
142     plot.xtics    data.col_titles
143
144     data.each_with_index do |row, i|
145       plot.data << Gnuplot::DataSet.new(row.unshift(0)) do |ds|
146         ds.using = "1:xtic(2)"
147         ds.title = data.row_titles(i)
148       end
149     end
150   end
151 end
152
153 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
154
155
156 __END__