Class: GnuPlotGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/seqtrimnext/classes/gnu_plot_graph.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name, x, y, title = nil) ⇒ GnuPlotGraph

Returns a new instance of GnuPlotGraph.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/seqtrimnext/classes/gnu_plot_graph.rb', line 5

def initialize(file_name,x,y,title=nil)
    $VERBOSE=true
    Gnuplot.open do |gp|
      # histogram
      Gnuplot::Plot.new( gp ) do |plot|
     
        # plot.space= 5 # it's the free space between the first/last value and the begin/end of axis X
        
       #plot.set("xrange [#{xr_min}: #{xr_max}]") 
				if !title
				 title=file_name
				end
				
        plot.title "#{title}"
        plot.xlabel "length"
        plot.ylabel "Number of sequences"
        plot.set "key off" #leyend
        
        
#        plot.set "style fill   solid 1.00 border -1"
#        #plot.set "style histogram clustered gap 0 title offset character 0, 0, 0"
#        plot.set "style data histograms"
#        plot.set "boxwidth 0.2 absolute"
        
# For this next line, lw is linewidth (2-4)?
#plot [XMIN:XMAX] 'myHistogramData' with boxes lw VALUE

        contains_strings=false

        x.each do |v|
  	 	  	begin
  		 	 	  r=Integer(v)
  	 	  	rescue
  		 	 	  contains_strings=true
  		 	 	  break
  	 	    end
  	 	   end
        
        
        if !contains_strings
            # plot.set "xrange [*:*]"
            # puts "INTEGER GRAPH"
				    plot.style "fill  pattern 22  border -1"
				    plot.set "boxwidth 0.2" # Probably 3-5.
				     
				    plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds| 
				      #ds.with=  " boxes lw 1"
              # ds.using=""
				      				      ds.with=  " imp lw 4"
				    end         
        
        else #graph with strings in X axis
            # puts "STRING GRAPH"        
          plot.xlabel ""
            
          plot.set "style fill solid 1.00 border -1"
          plot.set "style histogram clustered gap 1 title offset character 0, 0, 0"
          plot.set "style data histogram"
          plot.set "boxwidth 0.2 absolute"
          if x.count>4 then
            plot.set "xtics offset 0,graph 0 rotate 90"
          end
          # $VERBOSE=true
          # plot.set "style data linespoints"
          # plot.set "xtics border in scale 1,0.5 nomirror rotate by -45  offset character 0, 0, 0"
          
          # s = []
          # # i=0
          # x.each_with_index do |v,i|
          #   #s.push "\"#{v}\""
          #   s.push "#{v} #{i}"
          #   
          #   # i+=1
          # end
          # 
          # 
          # plot.set "xtics (#{s.join(',')})"
          # puts "XTICKS: (#{s.join(',')})"
          # puts "X:"
          #           puts x.join(';')
          #           puts "Y:"
          #           puts y.join(';')
          
          # if more than 20 strings, then keep greater ones
          
          if x.count>20
            # puts "original X:#{x.count}"
            $VERBOSE=true            
            h = {}
            
            x.each_with_index do |x1,i|
              h[x1]=y[i]
            end
            
            # puts h.to_json
            x=[]
            y=[]
            
            10.times do
              ma=h.max_by{|k,v| v}
              if ma
                puts "MAX:",ma.join(' * '),"of",h.values.sort.join(',')
                x.push ma[0]
                y.push ma[1]
                h.delete(ma[0])
              end
            end
            
            # puts "MAX 20 #{x.length}:#{x.join(';')}"
            
            # set key below
            # plot.set "label 3 below" 
            
          end

		      plot.data << Gnuplot::DataSet.new( [x,y] ) do |ds| 
            ds.using = "2:xticlabels(1)"   #show the graph and use labels at x
            # ds.using="2"
		        #ds.with=  " boxes lw 1"
		        # ds.using = "2 t 'Sequences' " #show the legend in the graph          
		      end
          
	      end
         
        if !file_name.nil?
          plot.terminal "png size 800,600"
          plot.output "#{file_name}"
        end
      end
      
   end

end