ruby-igv 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +51 -14
  3. data/lib/igv/version.rb +1 -1
  4. data/lib/igv.rb +163 -14
  5. metadata +7 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ca8294136b5eab8a344a40bf1d5ebb6d8ebf08f2794a1c6e1f582f593432049
4
- data.tar.gz: c36d4c09a163ade8bfd860519a50519a28ae3f93121a14c482bf1bceb6239cde
3
+ metadata.gz: 7e4a99096506fa47659b90377b84c90adfdaa05212d158f182c2f5350463f927
4
+ data.tar.gz: ffa21ca61aa0c54c89082e7b2094b716a9949c3380f00d2ba20a6142a2986940
5
5
  SHA512:
6
- metadata.gz: 1a085655336dcd4d06db5b51269f6b72d8d7174767f0b2990796d29cd410fb90adc884b86df3cf97087e75b35d387f655ed557628145c3af9badf17d3b29c50e
7
- data.tar.gz: d8e0741301b1504f7714077ef435a987b51c520fcc3788f85acaf42ad3785fcec7bf435e897666a5f9a16050d2f503cfab5daa68525a1fc86a18b5219018a6ed
6
+ metadata.gz: e9f3bf96a5830840591966525f26a43e2c63c1aaf76efc1c6ba48cc76622f56390ef8f2ce82f0f8c28e18b8dc2bc4074047257c72b7ad77bdc26511f1cc27970
7
+ data.tar.gz: 27542e590daa398caa2fc460b82c05da84195c16e7d220e9a58973979321b776cfcbc2c44473e572d17401d1570a18a8494bdeb66d4bbc16bd531f400f9cb64a
data/README.md CHANGED
@@ -1,15 +1,17 @@
1
1
  # ruby-igv
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/ruby-igv.svg)](https://badge.fury.io/rb/ruby-igv)
4
- [![Docs Latest](https://img.shields.io/badge/docs-latest-blue.svg)](https://rubydoc.info/gems/ruby-igv)
4
+ [![Docs Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://rubydoc.info/gems/ruby-igv)
5
+ [![Docs Latest](https://img.shields.io/badge/docs-latest-blue.svg)](https://kojix2.github.io/ruby-igv)
5
6
  [![The MIT License](https://img.shields.io/badge/license-MIT-orange.svg)](LICENSE.txt)
6
7
  [![DOI](https://zenodo.org/badge/281373245.svg)](https://zenodo.org/badge/latestdoi/281373245)
7
8
 
8
-
9
- <img src="https://user-images.githubusercontent.com/5798442/182540876-c3ca2906-7d05-4c93-9107-ce4135ae9765.png" align="right">
9
+ Ruby-IGV is a simple tool for controlling the Integrated Genomics Viewer (IGV) from the Ruby language. It provides an automated way to load files, specify genome locations, and take and save screenshots using IGV.
10
10
 
11
11
  ## Installation
12
12
 
13
+ <img src="https://user-images.githubusercontent.com/5798442/182540876-c3ca2906-7d05-4c93-9107-ce4135ae9765.png" width="300" align="right">
14
+
13
15
  Requirement :
14
16
 
15
17
  * [Ruby](https://github.com/ruby/ruby)
@@ -23,52 +25,87 @@ gem install ruby-igv
23
25
 
24
26
  ## Quickstart
25
27
 
28
+ <img src="https://user-images.githubusercontent.com/5798442/182623864-a9fa59aa-abb9-4cb1-8311-2b3479b7414e.png" width="300" align="right">
29
+
26
30
  ```ruby
27
31
  require 'igv'
28
32
 
29
33
  igv = IGV.start # This launch IGV
34
+
35
+ igv.set :SleepInterval, 200 # give a time interval
30
36
  igv.genome 'hg19'
31
- igv.load 'http://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/' \
32
- 'wgEncodeUwRepliSeq/wgEncodeUwRepliSeqK562G1AlnRep1.bam'
33
- igv.go 'chr18:78,016,233-78,016,640'
37
+ igv.load 'http://hgdownload.cse.ucsc.edu/goldenPath/' \
38
+ 'hg19/encodeDCC/wgEncodeUwRepliSeq/' \
39
+ 'wgEncodeUwRepliSeqK562G1AlnRep1.bam'
40
+ igv.go 'chr18:78016233-78016640'
34
41
  igv.snapshot 'region.png'
35
- igv.exit
36
42
  ```
37
43
 
38
44
  ## Usage
39
45
 
40
- ### docs
46
+ ### IGV batch commands
41
47
 
42
- See [the list of Batch commands](https://github.com/igvteam/igv/wiki/Batch-commands).
48
+ The commonly used commands in IGV are summarized in the official [list of batch commands](https://github.com/igvteam/igv/wiki/Batch-commands). (but even this does not seem to be all of them). You can also call the `commands` method from Ruby to open a browser and view the list.
43
49
 
44
50
  ```ruby
45
51
  igv.commands # Show the IGV command reference in your browser
46
52
  ```
47
53
 
48
- [docs](https://rubydoc.info/gems/ruby-igv)
54
+ ### docs
55
+
56
+ See [yard docs](https://rubydoc.info/gems/ruby-igv/IGV). Commonly used IGV batch commands can be called from Ruby methods of the same name. However, not all IGV batch commands are implemented in Ruby. Use the `send` method described below.
49
57
 
50
58
  ### send
51
59
 
52
- Not all commands are implemented in Ruby. Commands that are not implemented can be sent using the send method.
60
+ Commands that are not implemented can be sent using the send method.
53
61
 
54
62
  ```ruby
55
63
  igv.send("maxPanelHeight", 10)
56
64
  ```
57
65
 
66
+ To avoid unexpected behavior, ruby-igv does not use the `method_missing` mechanism.
58
67
 
59
68
  ### Launch IGV
60
69
 
61
- Launch IGV from Ruby scripot.
70
+ Launch IGV from Ruby script.
62
71
 
63
72
  ```ruby
64
73
  igv = IGV.start # launch IGV app using spawn
65
74
  ```
66
75
 
76
+ You can specify the port.
77
+
78
+ ```ruby
79
+ igv = IGV.start(port: 60152)
80
+ ```
81
+
82
+ If you start IGV in this way, you can force IGV to terminate by calling the kill method.
83
+
84
+ ```ruby
85
+ igv.kill
86
+ ```
87
+
67
88
  ### Open socket connection to IGV
68
89
 
90
+ If IGV is already running, use `new` or `open`.
91
+
92
+ new
93
+
69
94
  ```ruby
70
95
  igv = IGV.new # create an IGV object. Then you will type `igv.connect`
96
+ igv = IGV.new(host: "127.0.0.1", port: 60151, snapshot_dir: "~/igv_snapshot")
97
+ igv.connect # To start a connection, call connect explicitly.
98
+ igv.close
99
+ ```
100
+
101
+ open
102
+
103
+ ```ruby
71
104
  igv = IGV.open # create an IGV object and connect it to an already activated IGV.
105
+ igv.close
106
+ IGV.open(host: "127.0.0.1", port: 60151, snapshot_dir: "~/igv_snapshot") do |igv|
107
+ # do something
108
+ end # The socket is automatically closed.
72
109
  ```
73
110
 
74
111
  ### Close IGV
@@ -77,7 +114,7 @@ The behavior of the following methods is different.
77
114
 
78
115
  ```ruby
79
116
  igv.close # close the socket connection
80
- igv.exit # send exit command to IGV
117
+ igv.exit # send exit command to IGV then close the socket connection
81
118
  igv.quit # alias method to exit
82
119
  igv.kill # kill group pid created with IGV.start
83
120
  ```
@@ -90,7 +127,7 @@ igv.kill # kill group pid created with IGV.start
90
127
  * Suggest or add new features
91
128
 
92
129
  ```
93
- Do you need commit rights to my repository?
130
+ Do you need commit rights to this repository?
94
131
  Do you want to get admin rights and take over the project?
95
132
  If so, please feel free to contact me @kojix2.
96
133
  ```
data/lib/igv/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class IGV
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.9'
5
5
  end
data/lib/igv.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'igv/version'
4
+ require 'uri'
4
5
  require 'socket'
5
6
  require 'fileutils'
6
7
 
@@ -11,13 +12,31 @@ class IGV
11
12
 
12
13
  attr_reader :host, :port, :history
13
14
 
15
+ # Create IGV client object
16
+ #
17
+ # @param host [String] hostname or IP address of IGV server
18
+ # @param port [Integer] port number of IGV server
19
+ # @param path [String] directory path to save snapshots
20
+ # @return [IGV] IGV client object
21
+
14
22
  def initialize(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd)
23
+ raise ArgumentError, 'IGV#initialize does not accept a block.' if block_given?
24
+
15
25
  @host = host
16
26
  @port = port
17
27
  @snapshot_dir = File.expand_path(snapshot_dir)
18
28
  @history = []
19
29
  end
20
30
 
31
+ # Create IGV object and connect to IGV server
32
+ # This method accepts a block.
33
+ #
34
+ # @param host [String] hostname or IP address of IGV server
35
+ # @param port [Integer] port number of IGV server
36
+ # @param path [String] directory path to save snapshots
37
+ # @return [IGV] IGV client object
38
+ # @yield [IGV] IGV client object
39
+
21
40
  def self.open(host: '127.0.0.1', port: 60_151, snapshot_dir: Dir.pwd)
22
41
  igv = new(host: host, port: port, snapshot_dir: snapshot_dir)
23
42
  igv.connect
@@ -31,9 +50,25 @@ class IGV
31
50
  igv
32
51
  end
33
52
 
34
- # Launch IGV from ruby script
53
+ def self.port_open?(port)
54
+ system("lsof -i:#{port}", out: '/dev/null')
55
+ end
56
+ private_class_method :port_open?
35
57
 
36
- def self.start(port: 60_151, command: 'igv')
58
+ # Launch IGV from ruby script
59
+ #
60
+ # @param port [Integer] port number
61
+ # @param command [String] command to launch IGV
62
+ # @param snapshot_dir [String] directory path to save snapshots
63
+ # @return [IGV] IGV client object
64
+
65
+ def self.start(port: 60_151, command: 'igv', snapshot_dir: Dir.pwd)
66
+ case port_open?(port)
67
+ when nil then warn "Cannot tell if port #{port} is open"
68
+ when true then raise("Port #{port} is already in use")
69
+ when false then warn "Port #{port} is available"
70
+ else raise "Unexpected return value from port_open?(#{port})"
71
+ end
37
72
  r, w = IO.pipe
38
73
  pid_igv = spawn(command, '-p', port.to_s, pgroup: true, out: w, err: w)
39
74
  pgid_igv = Process.getpgid(pid_igv)
@@ -44,7 +79,7 @@ class IGV
44
79
  break if line.include? "Listening on port #{port}"
45
80
  end
46
81
  puts "\e[0m"
47
- igv = open(port: port)
82
+ igv = self.open(port: port, snapshot_dir: snapshot_dir)
48
83
  igv.instance_variable_set(:@pgid_igv, pgid_igv)
49
84
  igv
50
85
  end
@@ -88,17 +123,19 @@ class IGV
88
123
  end
89
124
 
90
125
  # Send batch commands to IGV.
91
- # @param [String] cmds
126
+ #
127
+ # @param *cmds [String, Symbol, Numeric] batch commands
128
+ # @return [String] response from IGV
92
129
 
93
130
  def send(*cmds)
94
131
  cmd = \
95
132
  cmds
96
133
  .compact
97
- .map do |cmd|
98
- case cmd
99
- when String, Symbol, Numeric then cmd.to_s
100
- when ->(c) { c.respond_to?(:to_str) } then cmd.to_str
101
- else raise ArgumentError, "#{cmd.inspect} is not a string"
134
+ .map do |cm|
135
+ case cm
136
+ when String, Symbol, Numeric then cm.to_s
137
+ when ->(c) { c.respond_to?(:to_str) } then cm.to_str
138
+ else raise ArgumentError, "#{cm.inspect} is not a string"
102
139
  end.strip.encode(Encoding::UTF_8)
103
140
  end
104
141
  .join(' ')
@@ -107,6 +144,19 @@ class IGV
107
144
  @socket.gets&.chomp("\n")
108
145
  end
109
146
 
147
+ # Syntactic sugar for IGV commands that begin with set.
148
+ #
149
+ # @example
150
+ # igv.set :SleepInterval, 100
151
+ # igv.send "setSleepInterval", 100 # same as above
152
+ # @param cmd [String, Symbol] batch commands
153
+ # @param *params [String, Symbol, Numeric] batch commands
154
+
155
+ def set(cmd, *params)
156
+ cmd = "set#{cmd}"
157
+ send(cmd, *params)
158
+ end
159
+
110
160
  # Show IGV batch commands in the browser.
111
161
  # https://github.com/igvteam/igv/wiki/Batch-commands
112
162
 
@@ -116,6 +166,7 @@ class IGV
116
166
  end
117
167
 
118
168
  # Writes the value of "param" back to the response
169
+ # @note IGV Batch command
119
170
  #
120
171
  # @param param [String] The parameter to echo.
121
172
  # @return [String] The value of "param". If param is not specified, "echo".
@@ -125,6 +176,7 @@ class IGV
125
176
  end
126
177
 
127
178
  # Selects a genome by id, or loads a genome (or indexed fasta) from the supplied path.
179
+ # @note IGV Batch command
128
180
  #
129
181
  # @param name_or_path [String] The genome to load
130
182
 
@@ -138,6 +190,7 @@ class IGV
138
190
  end
139
191
 
140
192
  # Loads a data or session file by specifying a full path to a local file or a URL.
193
+ # @note IGV Batch command
141
194
  #
142
195
  # @param path_or_url [String] The path to a local file or a URL
143
196
  # @param index [String] The index of the file
@@ -150,18 +203,22 @@ class IGV
150
203
  end
151
204
  index = "index=#{index}" if index
152
205
  send :load, path_or_url, index
206
+ rescue URI::InvalidURIError
207
+ raise ArgumentError, "Invalid URI or file path: #{path_or_url}"
153
208
  end
154
209
 
155
210
  # Go to the specified location
211
+ # @note IGV Batch command
156
212
  #
157
213
  # @param location [String] The location to go to.
158
214
 
159
- def goto(position)
160
- send :goto, position
215
+ def goto(*position)
216
+ send :goto, *position
161
217
  end
162
218
  alias go goto
163
219
 
164
220
  # Defines a region of interest bounded by the two loci
221
+ # @note IGV Batch command
165
222
  #
166
223
  # @param chr [String] The chromosome of the region
167
224
  # @param start [Integer] The start position of the region
@@ -179,6 +236,7 @@ class IGV
179
236
  end
180
237
 
181
238
  # Expands the given track.
239
+ # @note IGV Batch command
182
240
  #
183
241
  # @param track [String] The track to expand.
184
242
  # If not specified, expands all tracks.
@@ -188,6 +246,7 @@ class IGV
188
246
  end
189
247
 
190
248
  # Collapses a given track.
249
+ # @note IGV Batch command
191
250
  #
192
251
  # @param track [String] The track to collapse.
193
252
  # If not specified, collapses all tracks.
@@ -197,6 +256,7 @@ class IGV
197
256
  end
198
257
 
199
258
  # Squish a given track.
259
+ # @note IGV Batch command
200
260
  #
201
261
  # @param track [String] The track to squish.
202
262
  # If not specified, squishes all tracks.
@@ -205,7 +265,7 @@ class IGV
205
265
  send :squish, track
206
266
  end
207
267
 
208
- # Set the display mode for an alignment track to "View as pairs".
268
+ # Set the display mode for an alignment track to "View as pairs".
209
269
  #
210
270
  # @param track [String] The track to set.
211
271
  # If not specified, sets all tracks.
@@ -214,11 +274,20 @@ class IGV
214
274
  send :viewaspairs, track
215
275
  end
216
276
 
277
+ # @note IGV Batch command
278
+
279
+ def new
280
+ send :new
281
+ end
282
+
283
+ # @note IGV Batch command
284
+
217
285
  def clear
218
286
  send :clear
219
287
  end
220
288
 
221
- # Exit (close) the IGV application.
289
+ # Exit (close) the IGV application and close the socket.
290
+ # @note IGV Batch command (modified)
222
291
 
223
292
  def exit
224
293
  send :exit
@@ -227,7 +296,8 @@ class IGV
227
296
  alias quit exit
228
297
 
229
298
  # Sets the directory in which to write images.
230
- # Retruns the current snapshot directory if no argument is given.
299
+ # Returns the current snapshot directory if no argument is given.
300
+ # @note IGV Batch command (modified)
231
301
  #
232
302
  # @param path [String] The path to the directory.
233
303
 
@@ -252,6 +322,7 @@ class IGV
252
322
  # If filename is omitted, writes a PNG file with a filename generated based on the locus.
253
323
  # If filename is specified, the filename extension determines the image file format,
254
324
  # which must be either .png or .svg.
325
+ # @note IGV Batch command (modified)
255
326
  # @note In Ruby-IGV, it is possible to pass absolute or relative paths as well as file names;
256
327
  # the Snapshot directory is set to Dir.pwd by default.
257
328
  #
@@ -273,6 +344,7 @@ class IGV
273
344
  end
274
345
 
275
346
  # Temporarily set the preference named key to the specified value.
347
+ # @note IGV Batch command
276
348
  #
277
349
  # @param key [String] The preference name
278
350
  # @param value [String] The preference value
@@ -290,6 +362,7 @@ class IGV
290
362
 
291
363
  # Save the current session.
292
364
  # It is recommended that a full path be used for filename. IGV release 2.11.1
365
+ # @note IGV Batch command
293
366
  #
294
367
  # @param filename [String] The path to the session file
295
368
 
@@ -297,4 +370,80 @@ class IGV
297
370
  file_path = File.expand_path(file_path)
298
371
  send :saveSession, file_path
299
372
  end
373
+
374
+ # @note IGV Batch command
375
+
376
+ def set_alt_color(color, track)
377
+ send :setAltColor, color, track
378
+ end
379
+
380
+ # @note IGV Batch command
381
+
382
+ def set_color(color, track)
383
+ send :setColor, color, track
384
+ end
385
+
386
+ # @note IGV Batch command
387
+
388
+ def set_data_range(range, track)
389
+ send :setDataRange, range, track
390
+ end
391
+
392
+ # @note IGV Batch command
393
+
394
+ def set_log_scale(bool, track)
395
+ bool = 'true' if bool == true
396
+ bool = 'false' if bool == false
397
+ send :setLogScale, bool, track
398
+ end
399
+
400
+ # @note IGV Batch command
401
+
402
+ def set_sequence_strand(strand)
403
+ send :setSequenceStrand, strand
404
+ end
405
+
406
+ # @note IGV Batch command
407
+
408
+ def set_sequence_show_translation(bool)
409
+ bool = 'true' if bool == true
410
+ bool = 'false' if bool == false
411
+ send :setSequenceShowTranslation, bool
412
+ end
413
+
414
+ # @note IGV Batch command
415
+
416
+ def set_sleep_interval(ms)
417
+ send :setSleepInterval, ms
418
+ end
419
+
420
+ # @note IGV Batch command
421
+
422
+ def set_track_height(height, track)
423
+ send :setTrackHeight, height, track
424
+ end
425
+
426
+ # @note IGV Batch command
427
+
428
+ def max_panel_height(height)
429
+ send :maxPanelHeight, height
430
+ end
431
+
432
+ # @note IGV Batch command
433
+
434
+ def color_by(option, tag)
435
+ send :colorBy, option, tag
436
+ end
437
+
438
+ # @note IGV Batch command
439
+
440
+ def group(option, tag)
441
+ send :group, option, tag
442
+ end
443
+
444
+ # @note IGV Batch command
445
+
446
+ def overlay(overlaid_track, *tracks)
447
+ send :overlay, overlaid_track, *tracks
448
+ end
300
449
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-igv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-03 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: launchy
@@ -95,7 +95,7 @@ homepage: https://github.com/kojix2/ruby-igv
95
95
  licenses:
96
96
  - MIT
97
97
  metadata: {}
98
- post_install_message:
98
+ post_install_message:
99
99
  rdoc_options: []
100
100
  require_paths:
101
101
  - lib
@@ -103,15 +103,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
104
  - - ">="
105
105
  - !ruby/object:Gem::Version
106
- version: '2.4'
106
+ version: '2.6'
107
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.3.7
114
- signing_key:
113
+ rubygems_version: 3.5.5
114
+ signing_key:
115
115
  specification_version: 4
116
116
  summary: Control IGV (Integrative Genomics Viewer) with Ruby.
117
117
  test_files: []