Class: IGV

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd) ⇒ IGV

Create IGV client object

Parameters:

  • host (String) (defaults to: '127.0.0.1')

    hostname or IP address of IGV server

  • port (Integer) (defaults to: 60_151)

    port number of IGV server

  • path (String)

    directory path to save snapshots

Raises:

  • (ArgumentError)


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.expand_path(snapshot_dir)
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



12
13
14
# File 'lib/igv.rb', line 12

def history
  @history
end

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/igv.rb', line 12

def host
  @host
end

#portObject (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.

Parameters:

  • host (String) (defaults to: '127.0.0.1')

    hostname or IP address of IGV server

  • port (Integer) (defaults to: 60_151)

    port number of IGV server

  • path (String)

    directory path to save snapshots

Yields:

  • (IGV)

    IGV client object

Returns:

  • (IGV)

    IGV client object



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

Parameters:

  • port (Integer) (defaults to: 60_151)

    port number

  • command (String) (defaults to: 'igv')

    command to launch IGV

  • snapshot_dir (String) (defaults to: Dir.pwd)

    directory path to save snapshots

Returns:

  • (IGV)

    IGV client object



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

#clearObject

Note:

IGV Batch command



275
276
277
# File 'lib/igv.rb', line 275

def clear
  send :clear
end

#closeObject

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

Returns:

  • (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

Note:

IGV Batch command

Collapses a given track.

Parameters:

  • track (String) (defaults to: nil)

    The track to collapse. If not specified, collapses all tracks.



250
251
252
# File 'lib/igv.rb', line 250

def collapse(track = nil)
  send :collapse, track
end

#commandsObject

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

Note:

IGV Batch command

Writes the value of “param” back to the response

Parameters:

  • param (String) (defaults to: nil)

    The parameter to echo.

Returns:

  • (String)

    The value of “param”. If param is not specified, “echo”.



172
173
174
# File 'lib/igv.rb', line 172

def echo(param = nil)
  send :echo, param
end

#exitObject Also known as: quit

Note:

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

Note:

IGV Batch command

Expands the given track.

Parameters:

  • track (String) (defaults to: nil)

    The track to expand. If not specified, expands all tracks.



240
241
242
# File 'lib/igv.rb', line 240

def expand(track = nil)
  send :expand, track
end

#genome(name_or_path) ⇒ Object

Note:

IGV Batch command

Selects a genome by id, or loads a genome (or indexed fasta) from the supplied path.

Parameters:

  • name_or_path (String)

    The genome to load



181
182
183
184
185
186
187
188
# File 'lib/igv.rb', line 181

def genome(name_or_path)
  path = File.expand_path(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

Note:

IGV Batch command

Go to the specified location

Parameters:

  • location (String)

    The location to go to.



211
212
213
# File 'lib/igv.rb', line 211

def goto(position)
  send :goto, position
end

#killObject

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

Note:

IGV Batch command

Loads a data or session file by specifying a full path to a local file or a URL.

Parameters:

  • path_or_url (String)

    The path to a local file or a URL

  • index (String) (defaults to: nil)

    The index of the file



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.expand_path(path_or_url)
                end
  index = "index=#{index}" if index
  send :load, path_or_url, index
end

#preferences(key, value) ⇒ Object

Note:

IGV Batch command

Temporarily set the preference named key to the specified value.

Parameters:

  • key (String)

    The preference name

  • value (String)

    The preference value



342
343
344
# File 'lib/igv.rb', line 342

def preferences(key, value)
  send :preferences, key, value
end

#region(chr, start, end_) ⇒ Object

Note:

IGV Batch command

Defines a region of interest bounded by the two loci

Parameters:

  • chr (String)

    The chromosome of the region

  • start (Integer)

    The start position of the region

  • end_ (Integer)

    The end position of the region



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

Note:

IGV Batch command

Save the current session. It is recommended that a full path be used for filename. IGV release 2.11.1

Parameters:

  • filename (String)

    The path to the session file



359
360
361
362
# File 'lib/igv.rb', line 359

def save_session(file_path)
  file_path = File.expand_path(file_path)
  send :saveSession, file_path
end

#send(*cmds) ⇒ String

Send batch commands to IGV.

Parameters:

  • *cmds (String, Symbol, Numeric)

    batch commands

Returns:

  • (String)

    response from 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.

Examples:

igv.set :SleepInterval, 100
igv.send "setSleepInterval", 100 # same as above

Parameters:

  • cmd (String, Symbol)

    batch commands

  • *params (String, Symbol, Numeric)

    batch commands



153
154
155
156
# File 'lib/igv.rb', line 153

def set(cmd, *params)
  cmd = "set#{cmd}"
  send(cmd, *params)
end

#show_preferences_tableObject

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

Note:

IGV Batch command (modified)

Note:

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.

Parameters:

  • file_path (String) (defaults to: nil)

    The path to the image file.



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

Note:

IGV Batch command (modified)

Sets the directory in which to write images. Retruns the current snapshot directory if no argument is given.

Parameters:

  • path (String)

    The path to the directory.



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.expand_path(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

Note:

IGV Batch command

Squish a given track.

Parameters:

  • track (String) (defaults to: nil)

    The track to squish. If not specified, squishes all tracks.



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”.

Parameters:

  • track (String) (defaults to: nil)

    The track to set. If not specified, sets all tracks.



269
270
271
# File 'lib/igv.rb', line 269

def viewaspairs(track = nil)
  send :viewaspairs, track
end