snomed_model 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aeda96002976a3c142c42ce8e89cac8c1225b15acc9376446a7ce8c5dfb5da2d
4
+ data.tar.gz: 5b087224d77c835eee640f12bcf79bd341d8927adee9809b458022a2bddd9d25
5
+ SHA512:
6
+ metadata.gz: f59359f60f9bf1797055af2a8162ac7760e9651adfa1ff759fe6efa7a0f6e7de99d65a7727a64efec954be960f250eaa6652e1abdaf96d365dffbf6559ac26c4
7
+ data.tar.gz: 8d95605fbecd68b3617f48af20bb9c0bdfa07eafa33761f9921b8e36285e64428430f451570728fcce8eb07a178d67720abc6b9c963fffd8e9e72de245312f5b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-01-03
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at siva.g@westagilelabs.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
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 snomed_model.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem 'pry'
data/Gemfile.lock ADDED
@@ -0,0 +1,173 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snomed_model (0.1.0)
5
+ activerecord (~> 7.1, >= 7.1.2)
6
+ dag (~> 0.0.9)
7
+ pg (~> 1.5, >= 1.5.4)
8
+ pg_ltree
9
+ standalone_migrations (~> 7.1, >= 7.1.3)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ actionpack (7.1.2)
15
+ actionview (= 7.1.2)
16
+ activesupport (= 7.1.2)
17
+ nokogiri (>= 1.8.5)
18
+ racc
19
+ rack (>= 2.2.4)
20
+ rack-session (>= 1.0.1)
21
+ rack-test (>= 0.6.3)
22
+ rails-dom-testing (~> 2.2)
23
+ rails-html-sanitizer (~> 1.6)
24
+ actionview (7.1.2)
25
+ activesupport (= 7.1.2)
26
+ builder (~> 3.1)
27
+ erubi (~> 1.11)
28
+ rails-dom-testing (~> 2.2)
29
+ rails-html-sanitizer (~> 1.6)
30
+ activemodel (7.1.2)
31
+ activesupport (= 7.1.2)
32
+ activerecord (7.1.2)
33
+ activemodel (= 7.1.2)
34
+ activesupport (= 7.1.2)
35
+ timeout (>= 0.4.0)
36
+ activesupport (7.1.2)
37
+ base64
38
+ bigdecimal
39
+ concurrent-ruby (~> 1.0, >= 1.0.2)
40
+ connection_pool (>= 2.2.5)
41
+ drb
42
+ i18n (>= 1.6, < 2)
43
+ minitest (>= 5.1)
44
+ mutex_m
45
+ tzinfo (~> 2.0)
46
+ ast (2.4.2)
47
+ base64 (0.2.0)
48
+ bigdecimal (3.1.5)
49
+ builder (3.2.4)
50
+ coderay (1.1.3)
51
+ concurrent-ruby (1.2.2)
52
+ connection_pool (2.4.1)
53
+ crass (1.0.6)
54
+ dag (0.0.9)
55
+ diff-lcs (1.5.0)
56
+ drb (2.2.0)
57
+ ruby2_keywords
58
+ erubi (1.12.0)
59
+ i18n (1.14.1)
60
+ concurrent-ruby (~> 1.0)
61
+ io-console (0.7.1)
62
+ irb (1.11.0)
63
+ rdoc
64
+ reline (>= 0.3.8)
65
+ json (2.7.1)
66
+ language_server-protocol (3.17.0.3)
67
+ loofah (2.22.0)
68
+ crass (~> 1.0.2)
69
+ nokogiri (>= 1.12.0)
70
+ method_source (1.0.0)
71
+ minitest (5.20.0)
72
+ mutex_m (0.2.0)
73
+ nokogiri (1.16.0-x86_64-darwin)
74
+ racc (~> 1.4)
75
+ parallel (1.24.0)
76
+ parser (3.2.2.4)
77
+ ast (~> 2.4.1)
78
+ racc
79
+ pg (1.5.4)
80
+ pg_ltree (1.2.1)
81
+ activerecord (>= 5.2, < 8.0)
82
+ pg (>= 0.19, < 2)
83
+ pry (0.14.2)
84
+ coderay (~> 1.1)
85
+ method_source (~> 1.0)
86
+ psych (5.1.2)
87
+ stringio
88
+ racc (1.7.3)
89
+ rack (3.0.8)
90
+ rack-session (2.0.0)
91
+ rack (>= 3.0.0)
92
+ rack-test (2.1.0)
93
+ rack (>= 1.3)
94
+ rackup (2.1.0)
95
+ rack (>= 3)
96
+ webrick (~> 1.8)
97
+ rails-dom-testing (2.2.0)
98
+ activesupport (>= 5.0.0)
99
+ minitest
100
+ nokogiri (>= 1.6)
101
+ rails-html-sanitizer (1.6.0)
102
+ loofah (~> 2.21)
103
+ nokogiri (~> 1.14)
104
+ railties (7.1.2)
105
+ actionpack (= 7.1.2)
106
+ activesupport (= 7.1.2)
107
+ irb
108
+ rackup (>= 1.0.0)
109
+ rake (>= 12.2)
110
+ thor (~> 1.0, >= 1.2.2)
111
+ zeitwerk (~> 2.6)
112
+ rainbow (3.1.1)
113
+ rake (13.1.0)
114
+ rdoc (6.6.2)
115
+ psych (>= 4.0.0)
116
+ regexp_parser (2.8.3)
117
+ reline (0.4.1)
118
+ io-console (~> 0.5)
119
+ rexml (3.2.6)
120
+ rspec (3.12.0)
121
+ rspec-core (~> 3.12.0)
122
+ rspec-expectations (~> 3.12.0)
123
+ rspec-mocks (~> 3.12.0)
124
+ rspec-core (3.12.2)
125
+ rspec-support (~> 3.12.0)
126
+ rspec-expectations (3.12.3)
127
+ diff-lcs (>= 1.2.0, < 2.0)
128
+ rspec-support (~> 3.12.0)
129
+ rspec-mocks (3.12.6)
130
+ diff-lcs (>= 1.2.0, < 2.0)
131
+ rspec-support (~> 3.12.0)
132
+ rspec-support (3.12.1)
133
+ rubocop (1.59.0)
134
+ json (~> 2.3)
135
+ language_server-protocol (>= 3.17.0)
136
+ parallel (~> 1.10)
137
+ parser (>= 3.2.2.4)
138
+ rainbow (>= 2.2.2, < 4.0)
139
+ regexp_parser (>= 1.8, < 3.0)
140
+ rexml (>= 3.2.5, < 4.0)
141
+ rubocop-ast (>= 1.30.0, < 2.0)
142
+ ruby-progressbar (~> 1.7)
143
+ unicode-display_width (>= 2.4.0, < 3.0)
144
+ rubocop-ast (1.30.0)
145
+ parser (>= 3.2.1.0)
146
+ ruby-progressbar (1.13.0)
147
+ ruby2_keywords (0.0.5)
148
+ standalone_migrations (7.1.3)
149
+ activerecord (>= 6.0.0, < 7.2)
150
+ nokogiri (~> 1.14)
151
+ railties (>= 6.0.0, < 7.2)
152
+ rake (>= 10.0)
153
+ stringio (3.1.0)
154
+ thor (1.3.0)
155
+ timeout (0.4.1)
156
+ tzinfo (2.0.6)
157
+ concurrent-ruby (~> 1.0)
158
+ unicode-display_width (2.5.0)
159
+ webrick (1.8.1)
160
+ zeitwerk (2.6.12)
161
+
162
+ PLATFORMS
163
+ x86_64-darwin-19
164
+
165
+ DEPENDENCIES
166
+ pry
167
+ rake (~> 13.0)
168
+ rspec (~> 3.0)
169
+ rubocop (~> 1.21)
170
+ snomed_model!
171
+
172
+ BUNDLED WITH
173
+ 2.3.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 siva-wal
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,37 @@
1
+ # SnomedModel
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/snomed_model`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add snomed_model
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install snomed_model
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ 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).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/snomed_model. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/snomed_model/blob/master/CODE_OF_CONDUCT.md).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the SnomedModel project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/snomed_model/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "standalone_migrations"
6
+ require File.expand_path("./lib/snomed_model.rb")
7
+
8
+ load File.expand_path("./lib/tasks/snomed.rake")
9
+
10
+ StandaloneMigrations::Tasks.load_tasks
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ require "rubocop/rake_task"
15
+
16
+ RuboCop::RakeTask.new
17
+
18
+ task default: %i[spec rubocop]
@@ -0,0 +1,103 @@
1
+ require 'dag'
2
+ require 'csv'
3
+ require 'pry'
4
+
5
+ snomed_rel_file_path = "/Users/sivagollapalli/work/snomed-database-loader/PostgreSQL/tmp_extracted/sct2_StatedRelationship_Full_INT_20210131.txt"
6
+
7
+ hash = Hash.new
8
+
9
+ File.open(snomed_rel_file_path, 'r') do |f|
10
+ f.each_line do |line|
11
+ row = line.split("\t")
12
+
13
+ source = row[4].to_s
14
+ destination = row[5].to_s
15
+ relation = row[7].to_s
16
+
17
+ if hash[source].nil?
18
+ hash[source] = []
19
+ end
20
+
21
+ hash[source] << destination if relation == '116680003'
22
+ end
23
+ end
24
+
25
+ def build_or_find_vertex(dag, code)
26
+ vertex = dag.vertices.select { |c| c[:payload][:code] == code }.first
27
+
28
+ return vertex if vertex
29
+
30
+ dag.add_vertex(payload: {code: code})
31
+ end
32
+
33
+ def get_path(code, hash)
34
+ dag = DAG.new
35
+
36
+ queue = Queue.new
37
+ queue.push(code)
38
+
39
+ loop do
40
+ source = queue.pop
41
+ break if hash[source].nil?
42
+ destination = hash[source].uniq
43
+
44
+ source_vertex = build_or_find_vertex(dag, source)
45
+
46
+ if destination.size > 1
47
+ destination.each do |des|
48
+ desti_vertex = build_or_find_vertex(dag, des)
49
+
50
+ dag.add_edge from: source_vertex, to: desti_vertex
51
+
52
+ queue.push(des)
53
+
54
+ break if des == '138875005'
55
+ end
56
+ else
57
+ destination = destination.first
58
+
59
+ destination_vertex = build_or_find_vertex(dag, destination) # dag.add_vertex(payload: {code: destination})
60
+
61
+ dag.add_edge from: source_vertex, to: destination_vertex
62
+ queue.push(destination)
63
+
64
+ break if destination == '138875005'
65
+ end
66
+ end
67
+
68
+ dag
69
+ end
70
+
71
+ dag = get_path('155597006', hash)
72
+
73
+ vertex = build_or_find_vertex(dag, '155597006')
74
+
75
+ def find_all_paths(dag, start, path=[])
76
+ path += [start]
77
+
78
+ paths = [path]
79
+
80
+ start.successors.each do |ver|
81
+ newpaths = find_all_paths(dag, ver, path)
82
+
83
+ newpaths.each do |newpath|
84
+ paths += [newpath]
85
+ end
86
+ end
87
+
88
+ paths
89
+ end
90
+
91
+ paths = []
92
+
93
+ find_all_paths(dag, vertex).each do |path|
94
+ next if path.size == 1
95
+
96
+ vertex_str = path.map do |v|
97
+ v[:payload][:code]
98
+ end.join('.')
99
+
100
+ paths << "INSERT INTO test VALUES ('#{vertex_str}');"
101
+ end
102
+
103
+ pp paths
data/db/config.yml ADDED
@@ -0,0 +1,17 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ pool: 5
4
+ timeout: 5000
5
+ host: <%= ENV['SNOMED_DB_HOST'] %>
6
+ username: <%= ENV['SNOMED_DB_USERNAME'] %>
7
+ database: 'snomedct'
8
+ password: <%= ENV['SNOMED_DB_PWD'] %>
9
+
10
+ development:
11
+ <<: *default
12
+
13
+ production:
14
+ <<: *default
15
+
16
+ test: &test
17
+ <<: *default
@@ -0,0 +1,10 @@
1
+ class AddConceptTable < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :concepts do |t|
4
+ t.string :effectivetime, null: false
5
+ t.string :active, null: false
6
+ t.string :moduleid, null: false
7
+ t.string :definitionstatusid, null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ class AddIndexToConcepts < ActiveRecord::Migration[7.1]
2
+ def change
3
+ add_index :concepts, [:id, :effectivetime], unique: true
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ class AddDescriptionTable < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :descriptions do |t|
4
+ t.string :effectivetime, null: false
5
+ t.string :active, null: false
6
+ t.string :moduleid, null: false
7
+ t.string :conceptid, null: false
8
+ t.string :languagecode, null: false
9
+ t.string :typeid, null: false
10
+ t.text :term, null: false
11
+ t.string :casesignificanceid, null: false
12
+ end
13
+
14
+ add_index :descriptions, [:id, :effectivetime], unique: true
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ class AddRelationshipTable < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :relationships do |t|
4
+ t.string :effectivetime, null: false
5
+ t.string :active, null: false
6
+ t.string :moduleid, null: false
7
+ t.string :sourceid, null: false
8
+ t.string :destinationid, null: false
9
+ t.string :relationshipgroup, null: false
10
+ t.string :typeid, null: false
11
+ t.text :characteristictypeid, null: false
12
+ t.string :modifierid, null: false
13
+ end
14
+
15
+ add_index :relationships, [:id, :effectivetime], unique: true
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ class EnableLtreeExtension < ActiveRecord::Migration[7.1]
2
+ def change
3
+ enable_extension "ltree"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class AddHirerachyTable < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :hirerachies do |t|
4
+ t.ltree :path
5
+ end
6
+ end
7
+ end
data/db/schema.rb ADDED
@@ -0,0 +1,55 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema[7.1].define(version: 2024_01_09_131738) do
14
+ # These are extensions that must be enabled in order to support this database
15
+ enable_extension "ltree"
16
+ enable_extension "plpgsql"
17
+
18
+ create_table "concepts", force: :cascade do |t|
19
+ t.string "effectivetime", null: false
20
+ t.string "active", null: false
21
+ t.string "moduleid", null: false
22
+ t.string "definitionstatusid", null: false
23
+ t.index ["id", "effectivetime"], name: "index_concepts_on_id_and_effectivetime", unique: true
24
+ end
25
+
26
+ create_table "descriptions", force: :cascade do |t|
27
+ t.string "effectivetime", null: false
28
+ t.string "active", null: false
29
+ t.string "moduleid", null: false
30
+ t.string "conceptid", null: false
31
+ t.string "languagecode", null: false
32
+ t.string "typeid", null: false
33
+ t.text "term", null: false
34
+ t.string "casesignificanceid", null: false
35
+ t.index ["id", "effectivetime"], name: "index_descriptions_on_id_and_effectivetime", unique: true
36
+ end
37
+
38
+ create_table "hirerachies", force: :cascade do |t|
39
+ t.ltree "path"
40
+ end
41
+
42
+ create_table "relationships", force: :cascade do |t|
43
+ t.string "effectivetime", null: false
44
+ t.string "active", null: false
45
+ t.string "moduleid", null: false
46
+ t.string "sourceid", null: false
47
+ t.string "destinationid", null: false
48
+ t.string "relationshipgroup", null: false
49
+ t.string "typeid", null: false
50
+ t.text "characteristictypeid", null: false
51
+ t.string "modifierid", null: false
52
+ t.index ["id", "effectivetime"], name: "index_relationships_on_id_and_effectivetime", unique: true
53
+ end
54
+
55
+ end
@@ -0,0 +1,159 @@
1
+ module SnomedModel
2
+ class Concept < ActiveRecord::Base
3
+ attr_accessor :paths
4
+
5
+ ROOT_CONCEPT = "138875005".freeze
6
+ DISEASE_CONCEPT = "64572001".freeze
7
+ CLINICAL_FINDING = "404684003".freeze
8
+
9
+ has_many :descriptions, -> { active }, foreign_key: :conceptid
10
+ has_many :synonyms, -> { active.synonyms }, foreign_key: :conceptid, class_name: "Description"
11
+
12
+ scope :active, -> { where(active: "1") }
13
+ scope :leaf_concepts, lambda {
14
+ concepts = SnomedModel::Relationship
15
+ .select("destinationid::bigint")
16
+ .active
17
+ .where(typeid: SnomedModel::Relationship::IS_A_RELATION)
18
+ .group(:destinationid)
19
+
20
+ SnomedModel::Concept.active.where.not(id: concepts)
21
+ }
22
+
23
+ def direct_descendant
24
+ Description
25
+ .active
26
+ .where(conceptid: Relationship.active.direct_descendant(id).pluck(:sourceid))
27
+ end
28
+
29
+ def direct_ancestors
30
+ Description
31
+ .active
32
+ .where(conceptid: Relationship.active.direct_ancestors(id).pluck(:destinationid))
33
+ end
34
+
35
+ def detail
36
+ Description.active.detail(id)
37
+ end
38
+
39
+ def build_or_find_vertex(dag, code)
40
+ vertex = dag.vertices.detect { |c| c[:code] == code }
41
+ return vertex if vertex
42
+
43
+ dag.add_vertex(code: code)
44
+ end
45
+
46
+ def ancestor_relationships
47
+ Relationship.active.direct_ancestors(id)
48
+ end
49
+
50
+ def decedant_relationships
51
+ Relationship.active.direct_descendant(id)
52
+ end
53
+
54
+ def build_ancestor_dag
55
+ code = id
56
+ dag = DAG.new
57
+ queue = Queue.new
58
+ queue.push(code)
59
+
60
+ loop do
61
+ source = queue.pop
62
+ destinations = Concept.find(source).ancestor_relationships.distinct(:destinationid).pluck(:destinationid)
63
+ break unless destinations.any?
64
+
65
+ source_vertex = build_or_find_vertex(dag, source)
66
+ destinations.each do |dest|
67
+ add_edge(dag, source_vertex, dest)
68
+ queue.push(dest)
69
+ break if dest == ROOT_CONCEPT
70
+ end
71
+ end
72
+ dag
73
+ end
74
+
75
+ def build_descendant_dag(dag)
76
+ code = id
77
+ queue = Queue.new
78
+ queue.push(code)
79
+ leaf_nodes = []
80
+
81
+ loop do
82
+ puts "queue length #{queue.length}"
83
+ break if queue.empty?
84
+
85
+ destination = queue.pop
86
+ puts "current code is #{destination}"
87
+ sources = Concept.find(destination).decedant_relationships.distinct(:sourceid).pluck(:sourceid)
88
+ if sources.empty?
89
+ leaf_nodes << destination
90
+ end
91
+
92
+ sources.each do |source|
93
+ source_vertex = build_or_find_vertex(dag, source)
94
+ add_edge(dag, source_vertex, destination)
95
+ queue.push(source)
96
+ end
97
+ end
98
+ [dag, leaf_nodes]
99
+ end
100
+
101
+ def add_edge(dag, source_vertex, destination)
102
+ dest_vertex = build_or_find_vertex(dag, destination)
103
+ dag.add_edge from: source_vertex, to: dest_vertex
104
+ end
105
+
106
+ def find_all_paths(dag, start, path = [])
107
+ path += [start]
108
+ if start[:code] == ROOT_CONCEPT
109
+ @paths = (paths.presence || []) + [path]
110
+ path = []
111
+ end
112
+ start.successors.each do |ver|
113
+ find_all_paths(dag, ver, path)
114
+ end
115
+
116
+ paths
117
+ end
118
+
119
+ def build_paths
120
+ dag = build_ancestor_dag
121
+
122
+ start = build_or_find_vertex(dag, id)
123
+
124
+ paths = find_all_paths(dag, start).map do |path|
125
+ path_str = path.map do |v|
126
+ v[:code]
127
+ end.reverse.join(".")
128
+
129
+ { path: path_str }
130
+ end
131
+
132
+ Hirerachy.insert_all(paths)
133
+ end
134
+
135
+ def fetch_concepts(paths)
136
+ concepts = paths.each_with_object([]) do |path, array|
137
+ array << path.subpath.split(".")
138
+ end.flatten.uniq
139
+
140
+ Concept.active.includes(:descriptions).where(id: (concepts - [id.to_s]))
141
+ end
142
+
143
+ def descendant
144
+ fetch_concepts(Hirerachy.descendant(id))
145
+ end
146
+
147
+ def ancestors
148
+ fetch_concepts(Hirerachy.ancestors(id))
149
+ end
150
+
151
+ def disease?
152
+ ancestors.exists?(id: DISEASE_CONCEPT)
153
+ end
154
+
155
+ def clinical_finding?
156
+ ancestors.exists?(id: CLINICAL_FINDING)
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,15 @@
1
+ module SnomedModel
2
+ class Description < ActiveRecord::Base
3
+ FSN = "900000000000003001".freeze
4
+ SYNONYM = "900000000000013009".freeze
5
+ PREFERRED = "900000000000548007".freeze
6
+
7
+ belongs_to :concept, foreign_key: "conceptid"
8
+
9
+ scope :active, -> { where(active: "1") }
10
+ scope :detail, ->(id) { where(conceptid: id) }
11
+ scope :fsns, -> { where(typeid: FSN) }
12
+ scope :synonyms, -> { where(typeid: SYNONYM) }
13
+ scope :preferred, -> { where(typeid: PREFERRED) }
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module SnomedModel
2
+ class Hirerachy < ActiveRecord::Base
3
+ ltree :path
4
+
5
+ scope :concept_paths, ->(conceptid) { where("path ~ ?", "*.#{conceptid}.*") }
6
+ scope :descendant, lambda { |conceptid|
7
+ select("subpath(path, index(path, '#{conceptid}'), nlevel(path)) as subpath").concept_paths(conceptid)
8
+ }
9
+ scope :ancestors, lambda { |conceptid|
10
+ select("subpath(path, 0, index(path, '#{conceptid}')) as subpath").concept_paths(conceptid)
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module SnomedModel
2
+ class Relationship < ActiveRecord::Base
3
+ IS_A_RELATION = "116680003".freeze
4
+
5
+ scope :active, -> { where(active: "1") }
6
+ scope :direct_descendant, lambda { |conceptid|
7
+ where(typeid: IS_A_RELATION, destinationid: conceptid)
8
+ }
9
+ scope :direct_ancestors, lambda { |conceptid|
10
+ where(typeid: IS_A_RELATION, sourceid: conceptid)
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnomedModel
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "yaml"
5
+ require "erb"
6
+ require "dag"
7
+ require "pg_ltree"
8
+
9
+ require_relative "snomed_model/version"
10
+ require_relative "snomed_model/concept"
11
+ require_relative "snomed_model/description"
12
+ require_relative "snomed_model/relationship"
13
+ require_relative "snomed_model/hirerachy"
14
+
15
+ module SnomedModel
16
+ class Error < StandardError; end
17
+ # Your code goes here...
18
+ end
19
+
20
+ file = File.expand_path("./db/config.yml")
21
+ config = YAML.load_file(file, aliases: true)
22
+ env = config[ENV["SNOMED_ENV"] || "development"]
23
+
24
+ ActiveRecord::Base.establish_connection(
25
+ adapter: env["adapter"],
26
+ database: ERB.new(env["database"]).result,
27
+ username: ERB.new(env["username"]).result,
28
+ password: ERB.new(env["password"]).result,
29
+ host: ERB.new(env["host"]).result
30
+ )
@@ -0,0 +1,98 @@
1
+ namespace :snomed do
2
+ task :load_concepts, [:arg] do |_, args|
3
+ file = args[:arg]
4
+ records = []
5
+
6
+ File.open(file, "r") do |f|
7
+ f.each_line do |line|
8
+ row = line.split("\t")
9
+
10
+ next if row[0] == "id"
11
+
12
+ records << {
13
+ id: row[0],
14
+ effectivetime: row[1],
15
+ active: row[2],
16
+ moduleid: row[3],
17
+ definitionstatusid: row[4].to_i
18
+ }
19
+ end
20
+ end
21
+ SnomedModel::Concept.insert_all(records)
22
+ end
23
+
24
+ task :load_descriptions, [:arg] do |_, args|
25
+ file = args[:arg]
26
+ records = []
27
+
28
+ File.open(file, "r") do |f|
29
+ f.each_line do |line|
30
+ row = line.split("\t")
31
+
32
+ next if row[0] == "id"
33
+
34
+ records << {
35
+ id: row[0],
36
+ effectivetime: row[1],
37
+ active: row[2],
38
+ moduleid: row[3],
39
+ conceptid: row[4],
40
+ languagecode: row[5],
41
+ typeid: row[6],
42
+ term: row[7],
43
+ casesignificanceid: row[8].to_i
44
+ }
45
+ end
46
+ end
47
+ records.each_slice(10000) do |batch|
48
+ p "Inserting batch....."
49
+ SnomedModel::Description.insert_all(batch)
50
+ end
51
+ end
52
+
53
+ task :load_relationships, [:arg] do |_, args|
54
+ file = args[:arg]
55
+ records = []
56
+
57
+ File.open(file, "r") do |f|
58
+ f.each_line do |line|
59
+ row = line.split("\t")
60
+
61
+ next if row[0] == "id"
62
+
63
+ records << {
64
+ id: row[0],
65
+ effectivetime: row[1],
66
+ active: row[2],
67
+ moduleid: row[3],
68
+ sourceid: row[4],
69
+ destinationid: row[5],
70
+ relationshipgroup: row[6],
71
+ typeid: row[7],
72
+ characteristictypeid: row[8],
73
+ modifierid: row[9].to_i,
74
+ }
75
+ end
76
+ end
77
+ records.each_slice(10000) do |batch|
78
+ p "Inserting batch....."
79
+ SnomedModel::Relationship.insert_all(batch)
80
+ end
81
+ end
82
+
83
+ task :build_paths_for_sample_data do
84
+ Concept.leaf_concepts.order('random()').limit(100).map do |concept|
85
+ puts "Building path for #{concept.id}....."
86
+ concept.build_paths
87
+ end
88
+ end
89
+
90
+ task :build_paths do
91
+ Concept.leaf_concepts.find_in_batches do |group|
92
+ group.map do |concept|
93
+ puts "Building path for #{concept.id}....."
94
+ concept.build_paths
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,4 @@
1
+ module SnomedModel
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snomed_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Siva Gollapalli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 7.1.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '7.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 7.1.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: standalone_migrations
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '7.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 7.1.3
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '7.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 7.1.3
53
+ - !ruby/object:Gem::Dependency
54
+ name: pg
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.5'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.5.4
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.5'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.5.4
73
+ - !ruby/object:Gem::Dependency
74
+ name: dag
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.0.9
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: 0.0.9
87
+ - !ruby/object:Gem::Dependency
88
+ name: pg_ltree
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ type: :runtime
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ description:
102
+ email:
103
+ - sivagollapalli88@gmail.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".rspec"
109
+ - ".rubocop.yml"
110
+ - CHANGELOG.md
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - build_concept_full_relationship.rb
118
+ - db/config.yml
119
+ - db/migrate/20240106111800_add_concept_table.rb
120
+ - db/migrate/20240106124817_add_index_to_concepts.rb
121
+ - db/migrate/20240106153438_add_description_table.rb
122
+ - db/migrate/20240106161110_add_relationship_table.rb
123
+ - db/migrate/20240109131657_enable_ltree_extension.rb
124
+ - db/migrate/20240109131738_add_hirerachy_table.rb
125
+ - db/schema.rb
126
+ - lib/snomed_model.rb
127
+ - lib/snomed_model/concept.rb
128
+ - lib/snomed_model/description.rb
129
+ - lib/snomed_model/hirerachy.rb
130
+ - lib/snomed_model/relationship.rb
131
+ - lib/snomed_model/version.rb
132
+ - lib/tasks/snomed.rake
133
+ - sig/snomed_model.rbs
134
+ homepage:
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 2.6.0
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.2.3
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Provides ORM layer to query SNOMED
157
+ test_files: []