#!/usr/bin/env ruby require 'gtk2' require 'gtruby' require 'etc' TMPFILENAME = "/tmp/tmp.png" RCFILENAME = "#{Etc.getpwuid.dir}/.quickgtkrc" (stylefile, gff3file) = ARGV def save_position(seqid, from, to) File.open(RCFILENAME, "w+") do |f| f.write("#{seqid}\t#{from}\t#{to}") end end def get_position begin File.open(RCFILENAME) do |f| vals = f.read.split("\t") return [vals[0], vals[1].to_f, vals[2].to_f] end rescue return [nil, 0, 10000] end end # load style file style = GT::Style.new() style.load_file(stylefile) # create feature index feature_index = GT::FeatureIndexMemory.new() # add GFF3 file to index feature_index.add_gff3file(gff3file) @trackselector = Proc.new { |block, data| if block.get_type == "LTRHit" "LTR retrotransposon from Ensembl" elsif block.get_type == "RR_tract" "PPT" elsif block.get_type == "primer_binding_site" "PBS" else block.get_type end } def refresh_pic(seqid, from, to, feature_index, style) if seqid.nil? then seqid = feature_index.get_first_seqid() end if from.nil? or to.nil? then range = feature_index.get_range_for_seqid(seqid) else range = GT::Range.malloc range.start = from range.end = to end diagram = GT::Diagram.from_index(feature_index, seqid, range, style) diagram.set_track_selector_func(@trackselector) layout = GT::Layout.new(diagram, 1000, style) diagram.release_track_selector_func canvas = GT::CanvasCairoFile.new(style, 1000, layout.get_height, nil) layout.sketch(canvas) canvas.to_file(TMPFILENAME) end window = Gtk::Window.new window.set_size_request(1000, -1) window.title = "quick gtk browser" window.signal_connect('delete_event') { false } window.border_width = 10 startseqid, startfrom, startto = get_position seqids = feature_index.get_seqids if startseqid.nil? then startseqid = seqids[0] end refresh_pic(startseqid, startfrom, startto, feature_index, style) image = Gtk::Image.new(TMPFILENAME) vbox = Gtk::VBox.new(false, 0) hbox = Gtk::HBox.new(false, 0) vbox.pack_start(hbox, false, false, 0) vbox.pack_start_defaults(image) seqidbox = Gtk::ComboBox.new seqids.each do |seqid| seqidbox.append_text(seqid) end seqidbox.active=seqids.index(startseqid) frombox = Gtk::SpinButton.new(0.0, 100000000.0, 1.0) tobox = Gtk::SpinButton.new(0.0, 100000000.0, 1.0) frombox.value = startfrom.to_f tobox.value = startto.to_f back_button = Gtk::Button.new(Gtk::Stock::GO_BACK) fwd_button = Gtk::Button.new(Gtk::Stock::GO_FORWARD) go_button = Gtk::Button.new("Go") [seqidbox, back_button, frombox, tobox, fwd_button, go_button].each do |i| hbox.pack_start_defaults(i) end go_button.signal_connect('clicked') do refresh_pic(seqidbox.active_text, frombox.text.to_i, tobox.text.to_i, feature_index, style) image.set(TMPFILENAME) end back_button.signal_connect('clicked') do displacement = tobox.text.to_i - frombox.text.to_i frombox.value = (frombox.text.to_i - displacement).to_f tobox.value = (tobox.text.to_i - displacement).to_f refresh_pic(seqidbox.active_text, frombox.text.to_i, tobox.text.to_i, \ feature_index, style) image.set(TMPFILENAME) end fwd_button.signal_connect('clicked') do displacement = tobox.text.to_i - frombox.text.to_i frombox.value = (frombox.text.to_i + displacement).to_f tobox.value = (tobox.text.to_i + displacement).to_f refresh_pic(seqidbox.active_text, frombox.value.to_i, tobox.value.to_i, \ feature_index, style) image.set(TMPFILENAME) end window.add(vbox) window.show_all window.signal_connect('destroy') do save_position(seqidbox.active_text, frombox.value.to_i, tobox.value.to_i) Gtk.main_quit end Gtk.main