Class: IGV
- Inherits:
-
Object
- Object
- IGV
- Defined in:
- lib/igv.rb,
lib/igv/version.rb
Overview
The Integrative Genomics Viewer (IGV) software.broadinstitute.org/software/igv/
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.0.8'
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
-
.open(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) {|IGV| ... } ⇒ IGV
Create IGV object and connect to IGV server This method accepts a block.
-
.start(port: 60_151, command: 'igv', snapshot_dir: Dir.pwd) ⇒ IGV
Launch IGV from ruby script.
Instance Method Summary collapse
- #clear ⇒ Object
-
#close ⇒ Object
Close the socket.
- #closed? ⇒ Boolean
-
#collapse(track = nil) ⇒ Object
Collapses a given track.
-
#commands ⇒ Object
Show IGV batch commands in the browser.
-
#connect(host2 = @host, port2 = @port, connect_timeout: nil) ⇒ Object
Connect to IGV server.
-
#echo(param = nil) ⇒ String
Writes the value of “param” back to the response.
-
#exit ⇒ Object
(also: #quit)
Exit (close) the IGV application and close the socket.
-
#expand(track = nil) ⇒ Object
Expands the given track.
-
#genome(name_or_path) ⇒ Object
Selects a genome by id, or loads a genome (or indexed fasta) from the supplied path.
-
#goto(position) ⇒ Object
(also: #go)
Go to the specified location.
-
#initialize(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) ⇒ IGV
constructor
Create IGV client object.
-
#kill ⇒ Object
Kill IGV process by process group id.
-
#load(path_or_url, index: nil) ⇒ Object
Loads a data or session file by specifying a full path to a local file or a URL.
-
#preferences(key, value) ⇒ Object
Temporarily set the preference named key to the specified value.
-
#region(chr, start, end_) ⇒ Object
Defines a region of interest bounded by the two loci.
-
#save_session(file_path) ⇒ Object
Save the current session.
-
#send(*cmds) ⇒ String
Send batch commands to IGV.
-
#set(cmd, *params) ⇒ Object
Syntactic sugar for IGV commands that begin with set.
-
#show_preferences_table ⇒ Object
Show “preference.tab” in your browser.
-
#snapshot(file_path = nil) ⇒ Object
Saves a snapshot of the IGV window to an image file.
-
#snapshot_dir(dir_path = nil) ⇒ Object
Sets the directory in which to write images.
- #sort(option = 'base') ⇒ Object
-
#squish(track = nil) ⇒ Object
Squish a given track.
-
#viewaspairs(track = nil) ⇒ Object
Set the display mode for an alignment track to “View as pairs”.
Constructor Details
#initialize(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) ⇒ IGV
Create IGV client object
21 22 23 24 25 26 27 28 |
# File 'lib/igv.rb', line 21 def initialize(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) raise ArgumentError, 'new dose not take a block' if block_given? @host = host @port = port @snapshot_dir = File.(snapshot_dir) @history = [] end |
Instance Attribute Details
#history ⇒ Object (readonly)
Returns the value of attribute history.
12 13 14 |
# File 'lib/igv.rb', line 12 def history @history end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/igv.rb', line 12 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/igv.rb', line 12 def port @port end |
Class Method Details
.open(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) {|IGV| ... } ⇒ IGV
Create IGV object and connect to IGV server This method accepts a block.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/igv.rb', line 39 def self.open(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) igv = new(host: host, port: port, snapshot_dir: snapshot_dir) igv.connect return igv unless block_given? begin yield igv ensure @socket&.close end igv end |
.start(port: 60_151, command: 'igv', snapshot_dir: Dir.pwd) ⇒ IGV
Launch IGV from ruby script
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/igv.rb', line 64 def self.start(port: 60_151, command: 'igv', snapshot_dir: Dir.pwd) case port_open?(port) when nil then warn "Cannot tell if port #{port} is open" when false then raise("Port #{port} is already in use") when true then warn "Port #{port} is available" end r, w = IO.pipe pid_igv = spawn(command, '-p', port.to_s, pgroup: true, out: w, err: w) pgid_igv = Process.getpgid(pid_igv) Process.detach(pid_igv) puts "\e[33m" while (line = r.gets.chomp("\n")) puts line break if line.include? "Listening on port #{port}" end puts "\e[0m" igv = open(port: port, snapshot_dir: snapshot_dir) igv.instance_variable_set(:@pgid_igv, pgid_igv) igv end |
Instance Method Details
#clear ⇒ Object
IGV Batch command
275 276 277 |
# File 'lib/igv.rb', line 275 def clear send :clear end |
#close ⇒ Object
Close the socket. This method dose not exit IGV.
113 114 115 |
# File 'lib/igv.rb', line 113 def close @socket&.close end |
#closed? ⇒ Boolean
117 118 119 120 121 |
# File 'lib/igv.rb', line 117 def closed? return true if @socket.nil? @socket.closed? end |
#collapse(track = nil) ⇒ Object
IGV Batch command
Collapses a given track.
250 251 252 |
# File 'lib/igv.rb', line 250 def collapse(track = nil) send :collapse, track end |
#commands ⇒ Object
Show IGV batch commands in the browser. github.com/igvteam/igv/wiki/Batch-commands
161 162 163 164 |
# File 'lib/igv.rb', line 161 def commands require 'launchy' Launchy.open('https://github.com/igvteam/igv/wiki/Batch-commands') end |
#connect(host2 = @host, port2 = @port, connect_timeout: nil) ⇒ Object
Connect to IGV server
105 106 107 108 |
# File 'lib/igv.rb', line 105 def connect(host2 = @host, port2 = @port, connect_timeout: nil) @socket&.close @socket = Socket.tcp(host2, port2, connect_timeout: connect_timeout) end |
#echo(param = nil) ⇒ String
IGV Batch command
Writes the value of “param” back to the response
172 173 174 |
# File 'lib/igv.rb', line 172 def echo(param = nil) send :echo, param end |
#exit ⇒ Object Also known as: quit
IGV Batch command (modifyed)
Exit (close) the IGV application and close the socket.
282 283 284 285 |
# File 'lib/igv.rb', line 282 def exit send :exit @socket.close end |
#expand(track = nil) ⇒ Object
IGV Batch command
Expands the given track.
240 241 242 |
# File 'lib/igv.rb', line 240 def (track = nil) send :expand, track end |
#genome(name_or_path) ⇒ Object
IGV Batch command
Selects a genome by id, or loads a genome (or indexed fasta) from the supplied path.
181 182 183 184 185 186 187 188 |
# File 'lib/igv.rb', line 181 def genome(name_or_path) path = File.(name_or_path) if File.exist?(path) send :genome, path else send :genome, name_or_path end end |
#goto(position) ⇒ Object Also known as: go
IGV Batch command
Go to the specified location
211 212 213 |
# File 'lib/igv.rb', line 211 def goto(position) send :goto, position end |
#kill ⇒ Object
Kill IGV process by process group id
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/igv.rb', line 87 def kill if instance_variable_defined?(:@pgid_igv) warn \ 'This method kills the process with the group ID specified at startup. ' \ 'Please use exit or quit if possible.' else warn \ 'The kill method terminates only IGV commands invoked by the start method.' \ 'Otherwise, use exit or quit.' return end pgid = @pgid_igv Process.kill(:TERM, -pgid) close end |
#load(path_or_url, index: nil) ⇒ Object
IGV Batch command
Loads a data or session file by specifying a full path to a local file or a URL.
196 197 198 199 200 201 202 203 204 |
# File 'lib/igv.rb', line 196 def load(path_or_url, index: nil) path_or_url = if URI.parse(path_or_url).scheme path_or_url else File.(path_or_url) end index = "index=#{index}" if index send :load, path_or_url, index end |
#preferences(key, value) ⇒ Object
IGV Batch command
Temporarily set the preference named key to the specified value.
342 343 344 |
# File 'lib/igv.rb', line 342 def preferences(key, value) send :preferences, key, value end |
#region(chr, start, end_) ⇒ Object
IGV Batch command
Defines a region of interest bounded by the two loci
223 224 225 |
# File 'lib/igv.rb', line 223 def region(chr, start, end_) send :region, chr, start, end_ end |
#save_session(file_path) ⇒ Object
IGV Batch command
Save the current session. It is recommended that a full path be used for filename. IGV release 2.11.1
359 360 361 362 |
# File 'lib/igv.rb', line 359 def save_session(file_path) file_path = File.(file_path) send :saveSession, file_path end |
#send(*cmds) ⇒ String
Send batch commands to IGV.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/igv.rb', line 128 def send(*cmds) cmd = \ cmds .compact .map do |cmd| case cmd when String, Symbol, Numeric then cmd.to_s when ->(c) { c.respond_to?(:to_str) } then cmd.to_str else raise ArgumentError, "#{cmd.inspect} is not a string" end.strip.encode(Encoding::UTF_8) end .join(' ') @history << cmd @socket.puts(cmd) @socket.gets&.chomp("\n") end |
#set(cmd, *params) ⇒ Object
Syntactic sugar for IGV commands that begin with set.
153 154 155 156 |
# File 'lib/igv.rb', line 153 def set(cmd, *params) cmd = "set#{cmd}" send(cmd, *params) end |
#show_preferences_table ⇒ Object
Show “preference.tab” in your browser.
348 349 350 351 |
# File 'lib/igv.rb', line 348 def show_preferences_table require 'launchy' Launchy.open('https://raw.githubusercontent.com/igvteam/igv/master/src/main/resources/org/broad/igv/prefs/preferences.tab') end |
#snapshot(file_path = nil) ⇒ Object
IGV Batch command (modified)
In Ruby-IGV, it is possible to pass absolute or relative paths as well as file names; the Snapshot directory is set to Dir.pwd by default.
Saves a snapshot of the IGV window to an image file. If filename is omitted, writes a PNG file with a filename generated based on the locus. If filename is specified, the filename extension determines the image file format, which must be either .png or .svg.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/igv.rb', line 321 def snapshot(file_path = nil) return send(:snapshot) if file_path.nil? dir_path = File.dirname(file_path) filename = File.basename(file_path) if dir_path != @snapshot_dir snapshot_dir_internal(dir_path) r = send :snapshot, filename snapshot_dir_internal(@snapshot_dir) r else send :snapshot, filename end end |
#snapshot_dir(dir_path = nil) ⇒ Object
IGV Batch command (modified)
Sets the directory in which to write images. Retruns the current snapshot directory if no argument is given.
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/igv.rb', line 294 def snapshot_dir(dir_path = nil) return @snapshot_dir if dir_path.nil? dir_path = File.(dir_path) return if dir_path == @snapshot_dir r = snapshot_dir_internal(dir_path) @snapshot_dir = dir_path r end |
#sort(option = 'base') ⇒ Object
227 228 229 230 231 232 |
# File 'lib/igv.rb', line 227 def sort(option = 'base') vop = %w[base position strand quality sample readGroup] raise "options is one of: #{vop.join(', ')}" unless vop.include? option send :sort, option end |
#squish(track = nil) ⇒ Object
IGV Batch command
Squish a given track.
260 261 262 |
# File 'lib/igv.rb', line 260 def squish(track = nil) send :squish, track end |
#viewaspairs(track = nil) ⇒ Object
Set the display mode for an alignment track to “View as pairs”.
269 270 271 |
# File 'lib/igv.rb', line 269 def viewaspairs(track = nil) send :viewaspairs, track end |