mizlab 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 894a3675ba2feea2a673e352e900a2000805b0fc6d44b413e912fc17a65d4b09
4
+ data.tar.gz: 26b8765e6ef006ff1bcf12dc9d93a8293a4c52b9b8cf6c84093a22ca94009dd1
5
+ SHA512:
6
+ metadata.gz: 4d9284a55408dd8bfd122313c47c5a762a5c41ab419c12e619e5b68938d578846a4c265e3c9b0e619d5b4dec388453077aa982db92d00ce998f663b793544f10
7
+ data.tar.gz: 81024dcb6542d7b269ee0ef0339fade2d3f4d8fa5467b0edf31f4c17c9ddd2dd9b3587a1b6b457700910624728197a5c14ca429dcc4fd7dec6f03d9d9fc555a1
@@ -0,0 +1,21 @@
1
+ name: Publish-gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v[0-9]+.[0-9]+.[0-9]+"
7
+
8
+ jobs:
9
+ deploy:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - name: Checkout
13
+ uses: actions/checkout@v2
14
+
15
+ - name: Publish gem
16
+ uses: dawidd6/action-publish-gem@v1
17
+ with:
18
+ # Optional, will publish to RubyGems if specified
19
+ api_key: ${{secrets.RUBYGEMS_API_KEY}}
20
+ # Optional, will publish to GitHub Packages if specified
21
+ github_token: ${{secrets.GITHUB_TOKEN}}
@@ -0,0 +1,18 @@
1
+ name: Test
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run test
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake test
@@ -0,0 +1,27 @@
1
+ name: Build page
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - name: Set up Ruby
14
+ uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 3.0.1
17
+ - name: Run yardoc
18
+ run: |
19
+ gem install bundler -v 2.2.15
20
+ bundle install
21
+ bundle exec rake yard
22
+ - name: Deploy
23
+ uses: peaceiris/actions-gh-pages@v3
24
+ if: ${{ github.ref == 'refs/heads/main' }}
25
+ with:
26
+ github_token: ${{ secrets.GITHUB_TOKEN }}
27
+ publish_dir: ./doc
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in mizlab.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "yard", "~> 0.9"
13
+
14
+ gem "bio"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 mochi-MizLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ [![Test](https://github.com/MizLab/Mizlab-ruby/actions/workflows/minitest.yml/badge.svg)](https://github.com/MizLab/Mizlab-ruby/actions/workflows/minitest.yml)
2
+ [![Build page](https://github.com/MizLab/Mizlab-ruby/actions/workflows/yardoc.yml/badge.svg)](https://github.com/MizLab/Mizlab-ruby/actions/workflows/yardoc.yml)
3
+
4
+ # Mizlab-ruby
5
+
6
+ This gem is for easy handling of the processes used in our labolatory.
7
+
8
+
9
+ ## Installation
10
+
11
+ Install it yourself as:
12
+
13
+ ```sh
14
+ gem install mizlab
15
+ ```
16
+
17
+ or add below to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'mizlab'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```sh
26
+ bundle install
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ See [document](https://mizlab.github.io/Mizlab-ruby).
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mizlab.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "yard"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ["lib/**/*.rb"]
15
+ t.options = []
16
+ t.stats_options = ["--list-undoc"]
17
+ end
18
+
19
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mizlab"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mizlab
4
+ VERSION = "0.1.0"
5
+ end
data/lib/mizlab.rb ADDED
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mizlab/version"
4
+ require "set"
5
+ require "bio"
6
+
7
+ module Mizlab
8
+ class << self
9
+ # Fetch data via genbank
10
+ # @param [String] accession Accession number Like "NC_012920".
11
+ # @param [Bool] is_protein wheather the accession is protein. Default to true.
12
+ # @return [Bio::GenBank] GenBank object.
13
+ def getobj(accession, is_protein = false)
14
+ ret = is_protein ? fetch_protein(accession) : fetch_nucleotide(accession)
15
+ parse(ret) do |o|
16
+ return o
17
+ end
18
+ end
19
+
20
+ # Fetch multiple data via genbank
21
+ # @param [Array] accessions Array of accession string.
22
+ # @param [Bool] is_protein wheather the accession is protein. Default to true.
23
+ # @yield [Bio::GenBank] GenBank object.
24
+ def getobjs(accessions, is_protein = false)
25
+ ret = is_protein ? fetch_protein(accessions) : fetch_nucleotide(accessions)
26
+ parse(ret) do |o|
27
+ yield o
28
+ end
29
+ end
30
+
31
+ # Calculate coordinates from sequence
32
+ # @param [Bio::Sequence] sequence sequence
33
+ # @param [Hash] mappings Hash formated {String => [Float...]}. All of [Float...] must be have same dimention.
34
+ # @param [Hash] weights Weights for some base combination.
35
+ # @param [Integer] Size of window when scanning sequence. If not give this, will use `mappings.keys[0].length -1`.
36
+ # @return [Array] coordinates like [[dim1...], [dim2...]...].
37
+ def calculate_coordinates(sequence, mappings,
38
+ weights = nil, window_size = nil)
39
+ # error detections
40
+ if weights.is_a?(Hash) && window_size.nil?
41
+ keys = weights.keys
42
+ expect_window_size = keys[0].length
43
+ if keys.any? { |k| k.length != expect_window_size }
44
+ raise TypeError, "When not give `window_size`, `weights` must have same length keys"
45
+ end
46
+ end
47
+ n_dimention = mappings.values[0].length
48
+ if mappings.values.any? { |v| v.length != n_dimention }
49
+ raise TypeError, "All of `mappings`.values must have same size"
50
+ end
51
+
52
+ mappings.each do |k, v|
53
+ mappings[k] = v.map(&:to_f)
54
+ end
55
+
56
+ window_size = (if window_size.nil?
57
+ unless weights.nil?
58
+ weights.keys[0].length
59
+ else
60
+ 3 # default
61
+ end
62
+ else
63
+ window_size
64
+ end)
65
+ window_size -= 1
66
+ weights = weights.nil? ? {} : weights
67
+ weights.default = 1.0
68
+ coordinates = Array.new(n_dimention) { [0.0] }
69
+ sequence.length.times do |idx|
70
+ start = idx < window_size ? 0 : idx - window_size
71
+ vector = mappings[sequence[idx]].map { |v| v * weights[sequence[start..idx]] }
72
+ vector.each_with_index do |v, j|
73
+ coordinates[j].append(coordinates[j][-1] + v)
74
+ end
75
+ end
76
+ return coordinates
77
+ end
78
+
79
+ # Compute local patterns from coordinates.
80
+ # @param [Array] x_coordinates coordinates on x.
81
+ # @param [Array] y_coordinates coordinates on y.
82
+ # @return [Array] Local pattern histgram (unnormalized).
83
+ def local_patterns(x_coordinates, y_coordinates)
84
+ length = x_coordinates.length
85
+ if length != y_coordinates.length
86
+ raise TypeError, "The arguments must have same length."
87
+ end
88
+
89
+ filled_pixs = Set.new
90
+ 0.upto(length - 2) do |idx|
91
+ filled_pixs += bresenham(x_coordinates[idx].truncate, y_coordinates[idx].truncate,
92
+ x_coordinates[idx + 1].truncate, y_coordinates[idx + 1].truncate)
93
+ end
94
+
95
+ local_pattern_list = [0] * 512
96
+ get_patterns(filled_pixs) do |pix|
97
+ local_pattern_list[convert(pix)] += 1
98
+ end
99
+ return local_pattern_list
100
+ end
101
+
102
+ private
103
+
104
+ def fetch_protein(accession)
105
+ return Bio::NCBI::REST::EFetch.protein(accession)
106
+ end
107
+
108
+ def fetch_nucleotide(accession)
109
+ return Bio::NCBI::REST::EFetch.protein(accession)
110
+ end
111
+
112
+ # get patterns from filled pixs.
113
+ # @param [Set] filleds filled pix's coordinates.
114
+ # @yield [binaries] Array like [t, f, t...].
115
+ def get_patterns(filleds)
116
+ unless filleds.is_a?(Set)
117
+ raise TypeError, "The argument must be Set"
118
+ end
119
+
120
+ centers = Set.new()
121
+ filleds.each do |focused|
122
+ get_centers(focused) do |center|
123
+ if centers.include?(center)
124
+ next
125
+ end
126
+ centers.add(center)
127
+ binaries = []
128
+ -1.upto(1) do |dy|
129
+ 1.downto(-1) do |dx|
130
+ binaries.append(filleds.include?([center[0] + dx, center[1] + dy]))
131
+ end
132
+ end
133
+ yield binaries
134
+ end
135
+ end
136
+ end
137
+
138
+ # get center coordinates of all window that include focused pixel
139
+ # @param [Array] focused coordinate of focused pixel
140
+ # @yield [Array] center coordinates of all window
141
+ def get_centers(focused)
142
+ -1.upto(1) do |dy|
143
+ 1.downto(-1) do |dx|
144
+ yield [focused[0] + dx, focused[1] + dy]
145
+ end
146
+ end
147
+ end
148
+
149
+ # Convert binary array to interger
150
+ # @param [Array] binaries Array of binaries
151
+ # @return [Integer] converted integer
152
+ def convert(binaries)
153
+ unless binaries.all? { |v| v.is_a?(TrueClass) || v.is_a?(FalseClass) }
154
+ raise TypeError, "The argument must be Boolean"
155
+ end
156
+ rst = 0
157
+ binaries.reverse.each_with_index do |b, i|
158
+ if b
159
+ rst += 2 ** i
160
+ end
161
+ end
162
+ return rst
163
+ end
164
+
165
+ # Compute fill pixels by bresenham algorithm
166
+ # @param [Interger] x0 the start point on x.
167
+ # @param [Interger] y0 the start point on y.
168
+ # @param [Interger] x1 the end point on x.
169
+ # @param [Interger] x1 the end point on y.
170
+ # @return [Array] filled pixels
171
+ def bresenham(x0, y0, x1, y1)
172
+ if ![x0, y0, x1, y1].all? { |v| v.is_a?(Integer) }
173
+ raise TypeError, "All of arguments must be Integer"
174
+ end
175
+ dx = (x1 - x0).abs
176
+ dy = (y1 - y0).abs
177
+ sx = x0 < x1 ? 1 : -1
178
+ sy = y0 < y1 ? 1 : -1
179
+ err = dx - dy
180
+ lines = []
181
+ while true
182
+ lines.append([x0, y0])
183
+ if (x0 == x1 && y0 == y1)
184
+ break
185
+ end
186
+ e2 = 2 * err
187
+ if e2 > -dy
188
+ err = err - dy
189
+ x0 = x0 + sx
190
+ end
191
+ if e2 < dx
192
+ err = err + dx
193
+ y0 = y0 + sy
194
+ end
195
+ end
196
+ return lines
197
+ end
198
+
199
+ # Parse fetched data.
200
+ # @param [String] entries Entries as string
201
+ # @yield [Object] Object that match entry format.
202
+ def parse(entries)
203
+ tmp_file_name = ".mizlab_fetch_tmpfile"
204
+ File.open(tmp_file_name, "w") do |f|
205
+ f.puts entries
206
+ end
207
+ Bio::FlatFile.auto(tmp_file_name).each_entry do |e|
208
+ yield e
209
+ end
210
+ File.delete(tmp_file_name)
211
+ end
212
+ end
213
+ end
data/mizlab.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mizlab/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mizlab"
7
+ spec.version = Mizlab::VERSION
8
+ spec.authors = ["Omochice"]
9
+ spec.email = ["mochice.mls.ntl@gmail.com"]
10
+
11
+ spec.summary = "The tools for our laboratory"
12
+ spec.description = "The tools for our laboratory"
13
+ spec.homepage = "https://mizlab.github.io/Mizlab-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/MizLab/Mizlab-ruby"
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mizlab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Omochice
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The tools for our laboratory
14
+ email:
15
+ - mochice.mls.ntl@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/deploy.yml"
21
+ - ".github/workflows/minitest.yml"
22
+ - ".github/workflows/yardoc.yml"
23
+ - ".gitignore"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/mizlab.rb
31
+ - lib/mizlab/version.rb
32
+ - mizlab.gemspec
33
+ homepage: https://mizlab.github.io/Mizlab-ruby
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://mizlab.github.io/Mizlab-ruby
38
+ source_code_uri: https://github.com/MizLab/Mizlab-ruby
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.1.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: The tools for our laboratory
58
+ test_files: []