mizlab 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mizlab/version.rb +1 -1
  3. data/lib/mizlab.rb +111 -54
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0997e1a56ba6b17aa4eedc2fd89e84462e3ac2d3f51c1c251221a0fc7613ff66'
4
- data.tar.gz: aaac55320663e3b8fef03c5518d73156ea3a1880736936c8debb1a6eb9d60935
3
+ metadata.gz: ee3b63815d8af26656457f4d496fc4d5b8527cd4402591d454739052f98b6950
4
+ data.tar.gz: 7aad66b65cf70b3e820ad31626ab35c3ad924f2c8944973afa03f1768a06b56c
5
5
  SHA512:
6
- metadata.gz: a5e073bceea54fd25ad99e80c35052732c265a6e827b8e23bcf4f2bc21f3b03095243d5e9b718bf6b521ae8097f5059b72a15cc32ec67e7bfe534e3b8c7d35bc
7
- data.tar.gz: 61f1545fd3218ce1a74b030a6a64718964a8129f30587deaebeb038cadfe82991b9d7356751129ba92df751278193d5a1cf92c147790e8a88008bb285fa665c8
6
+ metadata.gz: 9278897277093689b3b7f855d01574e6d580ec1ff3b5a5efd3e74ec9ba1981b66ad62d2fc1cf257191e4f66697d213bc368423f829c174212fe2df81deae37af
7
+ data.tar.gz: bb2a961976d7b414e06bc99d6969752e3f54e4c472cf2faa22873116ebf1b64e8ae8769160e92c1051bbba0b5e276919a20fc84b3484d919c63aadea3aaca24d
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mizlab
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
data/lib/mizlab.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "mizlab/version"
4
4
  require "set"
5
5
  require "bio"
6
6
  require "stringio"
7
+ require "open3"
7
8
  require "rexml/document"
8
9
 
9
10
  module Mizlab
@@ -72,7 +73,7 @@ module Mizlab
72
73
  # @param [Bio::Sequence] sequence sequence
73
74
  # @param [Hash] mappings Hash formated {String => [Float...]}. All of [Float...] must be have same dimention.
74
75
  # @param [Hash] weights Weights for some base combination.
75
- # @param [Integer] Size of window when scanning sequence. If not give this, will use `mappings.keys[0].length -1`.
76
+ # @param [Integer] window_size Size of window when scanning sequence. If not give this, will use `mappings.keys[0].length -1`.
76
77
  # @return [Array] coordinates like [[dim1...], [dim2...]...].
77
78
  def calculate_coordinates(sequence, mappings,
78
79
  weights = nil, window_size = nil)
@@ -117,8 +118,8 @@ module Mizlab
117
118
  end
118
119
 
119
120
  # Compute local patterns from coordinates.
120
- # @param [Array] x_coordinates coordinates on x.
121
- # @param [Array] y_coordinates coordinates on y.
121
+ # @param [Array] x_coordinates Coordinates on x dimention.
122
+ # @param [Array] y_coordinates Coordinates on y dimention.
122
123
  # @return [Array] Local pattern histgram (unnormalized).
123
124
  def local_patterns(x_coordinates, y_coordinates)
124
125
  length = x_coordinates.length
@@ -127,14 +128,21 @@ module Mizlab
127
128
  end
128
129
 
129
130
  filled_pixs = Set.new
130
- 0.upto(length - 2) do |idx|
131
- filled_pixs += bresenham(x_coordinates[idx].truncate, y_coordinates[idx].truncate,
132
- x_coordinates[idx + 1].truncate, y_coordinates[idx + 1].truncate)
131
+ x_coordinates[...-1].zip(y_coordinates[...-1],
132
+ x_coordinates[1...],
133
+ y_coordinates[1...]) do |x_start, y_start, x_end, y_end|
134
+ bresenham(x_start.truncate, y_start.truncate,
135
+ x_end.truncate, y_end.truncate).each do |pix|
136
+ filled_pixs.add("#{pix[0]}##{pix[1]}")
137
+ # NOTE:
138
+ # In set or hash, if including array make it so slow.
139
+ # Prevend it by converting array into symbol or freezed string.
140
+ end
133
141
  end
134
142
 
135
143
  local_pattern_list = [0] * 512
136
- get_patterns(filled_pixs) do |pix|
137
- local_pattern_list[convert(pix)] += 1
144
+ get_patterns(filled_pixs) do |pattern|
145
+ local_pattern_list[pattern] += 1
138
146
  end
139
147
  return local_pattern_list
140
148
  end
@@ -167,87 +175,86 @@ module Mizlab
167
175
  end
168
176
 
169
177
  # get patterns from filled pixs.
170
- # @param [Set] filleds filled pix's coordinates.
171
- # @yield [binaries] Array like [t, f, t...].
178
+ # @param [Set] filleds Filled pix's coordinates.
179
+ # @yield [Integer] Pattern that shown as binary
172
180
  def get_patterns(filleds)
173
181
  unless filleds.is_a?(Set)
174
182
  raise TypeError, "The argument must be Set"
175
183
  end
176
184
 
177
- centers = Set.new()
185
+ centers = Set.new
178
186
  filleds.each do |focused|
179
- get_centers(focused) do |center|
187
+ x, y = focused.split("#").map(&:to_i)
188
+ get_centers(x, y) do |center|
180
189
  if centers.include?(center)
181
190
  next
182
191
  end
183
192
  centers.add(center)
184
- binaries = []
193
+ binary = ""
194
+ x, y = center.split("#").map(&:to_i)
185
195
  -1.upto(1) do |dy|
186
196
  1.downto(-1) do |dx|
187
- binaries.append(filleds.include?([center[0] + dx, center[1] + dy]))
197
+ binary += filleds.include?("#{x + dx}##{y + dy}") ? "1" : "0"
188
198
  end
189
199
  end
190
- yield binaries
200
+ yield binary.to_i(2)
191
201
  end
192
202
  end
193
203
  end
194
204
 
195
205
  # get center coordinates of all window that include focused pixel
196
- # @param [Array] focused coordinate of focused pixel
197
- # @yield [Array] center coordinates of all window
198
- def get_centers(focused)
206
+ # @param [Integer] focused_x Coordinate of focused pixel on x dimention
207
+ # @param [Integer] focused_y Coordinate of focused pixel on y dimention
208
+ # @yield [String] Center coordinates of all window as string
209
+ def get_centers(focused_x, focused_y)
199
210
  -1.upto(1) do |dy|
200
211
  1.downto(-1) do |dx|
201
- yield [focused[0] + dx, focused[1] + dy]
212
+ yield "#{focused_x + dx}##{focused_y + dy}"
202
213
  end
203
214
  end
204
215
  end
205
216
 
206
- # Convert binary array to interger
207
- # @param [Array] binaries Array of binaries
208
- # @return [Integer] converted integer
209
- def convert(binaries)
210
- unless binaries.all? { |v| v.is_a?(TrueClass) || v.is_a?(FalseClass) }
211
- raise TypeError, "The argument must be Boolean"
212
- end
213
- rst = 0
214
- binaries.reverse.each_with_index do |b, i|
215
- if b
216
- rst += 2 ** i
217
- end
218
- end
219
- return rst
220
- end
221
-
222
217
  # Compute fill pixels by bresenham algorithm
223
218
  # @param [Interger] x0 the start point on x.
224
219
  # @param [Interger] y0 the start point on y.
225
220
  # @param [Interger] x1 the end point on x.
226
221
  # @param [Interger] x1 the end point on y.
227
- # @return [Array] filled pixels
222
+ # @return [Array] Filled pixels
223
+ # ref https://aidiary.hatenablog.com/entry/20050402/1251514618 (japanese)
228
224
  def bresenham(x0, y0, x1, y1)
229
225
  if ![x0, y0, x1, y1].all? { |v| v.is_a?(Integer) }
230
226
  raise TypeError, "All of arguments must be Integer"
231
227
  end
232
- dx = (x1 - x0).abs
233
- dy = (y1 - y0).abs
234
- sx = x0 < x1 ? 1 : -1
235
- sy = y0 < y1 ? 1 : -1
236
- err = dx - dy
237
- lines = []
238
- while true
239
- lines.append([x0, y0])
240
- if (x0 == x1 && y0 == y1)
241
- break
242
- end
243
- e2 = 2 * err
244
- if e2 > -dy
245
- err = err - dy
246
- x0 = x0 + sx
228
+
229
+ dx = x1 - x0
230
+ dy = y1 - y0
231
+ step_x = dx.positive? ? 1 : -1
232
+ step_y = dy.positive? ? 1 : -1
233
+ dx, dy = [dx, dy].map { |x| (x * 2).abs }
234
+
235
+ lines = [[x0, y0]]
236
+
237
+ if dx > dy
238
+ fraction = dy - dx / 2
239
+ while x0 != x1
240
+ if fraction >= 0
241
+ y0 += step_y
242
+ fraction -= dx
243
+ end
244
+ x0 += step_x
245
+ fraction += dy
246
+ lines << [x0, y0]
247
247
  end
248
- if e2 < dx
249
- err = err + dx
250
- y0 = y0 + sy
248
+ else
249
+ fraction = dx - dy / 2
250
+ while y0 != y1
251
+ if fraction >= 0
252
+ x0 += step_x
253
+ fraction -= dx
254
+ end
255
+ y0 += step_y
256
+ fraction += dx
257
+ lines << [x0, y0]
251
258
  end
252
259
  end
253
260
  return lines
@@ -278,4 +285,54 @@ module Mizlab
278
285
  return { element.name.to_sym => value }
279
286
  end
280
287
  end
288
+
289
+ class Blast < Bio::Blast
290
+ # Execute blast+
291
+ # @param [Bio::Sequence, Bio::Sequence::NA, Bio::Sequence::AA] q Query sequence
292
+ # @param [Hash] opts commandline arguments optionaly
293
+ # @return [Bio::Blast::Report] Result for blast+
294
+ def query(q, opts = {})
295
+ # NOTE: I dont use **kwargs for compatibility
296
+ case q
297
+ when Bio::Sequence
298
+ q = q.output(:fasta)
299
+ when Bio::Sequence::NA, Bio::Sequence::AA, Bio::Sequence::Generic
300
+ q = q.to_fasta("query", 70)
301
+ else
302
+ q = q.to_s
303
+ end
304
+ stdout, _ = exec_local(q, opts)
305
+ return parse_result(stdout)
306
+ end
307
+
308
+ private
309
+
310
+ # Execute blast on local
311
+ # @param [string] query_string Query string, fasta etc
312
+ # @param [Hash] opts commandline arguments optionaly
313
+ # @return [Array] Array [stdout, stderr] as string
314
+ # TODO: compatibility with original
315
+ def exec_local(query_string, opts = {})
316
+ # NOTE: I dont use **kwargs for compatibility
317
+ cmd = []
318
+ cmd << @program if @program
319
+ cmd += ["-db", @db] if @program
320
+ cmd += ["-outfmt", "5"]
321
+ opts.each do |kv|
322
+ cmd += kv.map(&:to_s)
323
+ end
324
+ return execute_command(cmd, stdin: query_string)
325
+ end
326
+
327
+ # Execute command on shell
328
+ # @param [Array] cmd Array of command strings that splited by white space
329
+ # @param [String] stdin String of stdin
330
+ # @return [Array] String of stdout and stderr
331
+ # @raise [IOError] Command finished without status 0
332
+ def execute_command(cmd, stdin)
333
+ stdout, stderr, status = Open3.capture3(cmd.join(" "), stdin_data: stdin)
334
+ raise IOError, stderr unless status == 0
335
+ return [stdout, stderr]
336
+ end
337
+ end
281
338
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mizlab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Omochice
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-28 00:00:00.000000000 Z
11
+ date: 2021-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bio