-
Notifications
You must be signed in to change notification settings - Fork 19
Multiple ontology support #1296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cwant
wants to merge
6
commits into
ElixirTeSS:master
Choose a base branch
from
cwant:cwant/multi_ontology
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e072d44
Model concern doesn't need to have an Edam-specific name.
cwant c022c1d
Make HasOntologyTerm testable, with small demo test.
cwant eefc840
Add support for multiple ontologies for a field.
cwant 9a6dea6
Suggestion from Co-pilot to make the `ontology_term_fields` class var…
cwant 4a26db5
From Co-pilot: ensure `scoped_lookup_by_name_or_synonym` is defined i…
cwant 586ab52
Make code more robust when term URIs aren't found in any ontology.
cwant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
app/models/concerns/has_edam_terms.rb → ...models/concerns/has_terms_and_synonyms.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| require 'test_helper' | ||
|
|
||
| class HasOntologyTermsTest < ActiveSupport::TestCase | ||
| # Summary: we create attributes 'test_topics' and 'multi_test_topics' for | ||
| # the fake model DummyMaterial. 'test_topics' uses DummyOntology, | ||
| # 'multi_test_topics' uses both DummyOntology and Edam::Ontology | ||
|
|
||
| teardown do | ||
| DummyMaterial.clear_index! | ||
| end | ||
|
|
||
| class DummyTerm | ||
| attr_reader :label, :uri | ||
| def initialize(term) | ||
| @label = term | ||
| @uri = "http://dummy/#{term}" | ||
| end | ||
| alias_method :preferred_label, :label | ||
| end | ||
|
|
||
| class DummyOntology < ::Ontology | ||
| # A very permissive ontology: it allows any term as long as it doesn't have Chemistry in it | ||
| include Singleton | ||
|
|
||
| def initialize | ||
| end | ||
|
|
||
| def uri | ||
| 'http://dummy/' | ||
| end | ||
|
|
||
| def scoped_lookup_by_name(term, subset = :_) | ||
| return DummyTerm.new(term) unless term =~ /chemistry/i | ||
| end | ||
| alias_method :scoped_lookup_by_name_or_synonym, :scoped_lookup_by_name | ||
|
|
||
| def lookup(uri) | ||
| term = uri[/http:\/\/dummy\/(.*)/,1] | ||
| return DummyTerm.new(term) unless term.blank? | ||
| end | ||
| end | ||
|
|
||
| class DummyMaterial < ::Material | ||
| has_ontology_terms(:test_topics, ontology: DummyOntology.instance) | ||
| has_ontology_terms(:multi_test_topics, | ||
| ontologies: [{ ontology: Edam::Ontology.instance, | ||
| branch: EDAM.topics}, | ||
| { ontology: DummyOntology.instance}]) | ||
|
|
||
| # TODO: see similar tests with model subclasses, maybe can be in a module? | ||
| def self.index | ||
| (@index ||= Hash.new).values.flatten.uniq | ||
| end | ||
|
|
||
| def self.add_to_index(m) | ||
| index | ||
| @index[m.id] = m.reload.collections.to_a | ||
| end | ||
|
|
||
| def self.clear_index! | ||
| @index = Hash.new | ||
| end | ||
|
|
||
| def solr_index | ||
| self.class.add_to_index(self) | ||
| end | ||
| end | ||
|
|
||
| test 'can create an attribute with terms from a single ontology' do | ||
| # See the Event/Material model tests for many examples of this ... | ||
| dummy = materials(:good_material).becomes(DummyMaterial) | ||
|
|
||
| # This is found in the ontology ... | ||
| dummy.test_topic_names = ['Bioinformatics'] | ||
| dummy.save! | ||
| assert_equal dummy.test_topics.count, 1 | ||
| assert_equal dummy.test_topic_names, ['Bioinformatics'] | ||
| assert_equal dummy.test_topic_uris, ['http://dummy/Bioinformatics'] | ||
|
|
||
| # This is not | ||
| dummy.test_topic_names = ['Biochemistry'] | ||
| dummy.save! | ||
| assert_equal dummy.test_topics.count, 0 | ||
| assert_equal dummy.test_topic_names, [] | ||
| assert_equal dummy.test_topic_uris, [] | ||
| end | ||
|
|
||
| test 'can create an attribute with terms from multiple ontologies' do | ||
| dummy = materials(:good_material).becomes(DummyMaterial) | ||
|
|
||
| # This is found in both ontologies ... | ||
| dummy.multi_test_topic_names = ['Bioinformatics'] | ||
| dummy.save! | ||
| assert_equal dummy.multi_test_topics.count, 2 | ||
| assert_equal Set.new(dummy.multi_test_topic_uris), | ||
| Set.new(['http://edamontology.org/topic_0091', 'http://dummy/Bioinformatics']) | ||
| # The two exact names collapse into one ... | ||
| assert_equal dummy.multi_test_topic_names, ['Bioinformatics'] | ||
|
|
||
| # This is found in only in Edam ... | ||
| dummy.multi_test_topic_names = ['Biochemistry'] | ||
| dummy.save! | ||
| assert_equal dummy.multi_test_topics.count, 1 | ||
| assert_equal dummy.multi_test_topic_names, ['Biochemistry'] | ||
| assert_equal dummy.multi_test_topic_uris, ['http://edamontology.org/topic_3292'] | ||
|
|
||
| # This is found only in DummyOntology ... | ||
| dummy.multi_test_topic_names = ['Poodles'] | ||
| dummy.save! | ||
| assert_equal dummy.multi_test_topics.count, 1 | ||
| assert_equal dummy.multi_test_topic_names, ['Poodles'] | ||
| assert_equal dummy.multi_test_topic_uris, ['http://dummy/Poodles'] | ||
|
|
||
| # This is found in neither ... | ||
| dummy.multi_test_topic_names = ['Poodle Chemistry'] | ||
| dummy.save! | ||
| assert_equal dummy.multi_test_topics.count, 0 | ||
| assert_equal dummy.multi_test_topic_names, [] | ||
| assert_equal dummy.multi_test_topic_uris, [] | ||
|
|
||
| # Set via URIs | ||
| dummy.multi_test_topic_uris = ['http://dummy/Poodles', | ||
| 'http://edamontology.org/topic_3292'] | ||
| dummy.save! | ||
| assert_equal dummy.multi_test_topics.count, 2 | ||
| assert_equal Set.new(dummy.multi_test_topic_names), | ||
| Set.new(['Biochemistry', 'Poodles']) | ||
| assert_equal Set.new(dummy.multi_test_topic_uris), | ||
| Set.new(['http://dummy/Poodles', | ||
| 'http://edamontology.org/topic_3292']) | ||
| assert_equal dummy.ontology_term_links.map(&:field), ["multi_test_topics", | ||
| "multi_test_topics"] | ||
| assert_equal Set.new(dummy.ontology_term_links.map(&:term_uri)), | ||
| Set.new(['http://dummy/Poodles', | ||
| 'http://edamontology.org/topic_3292']) | ||
| end | ||
|
|
||
| test "Ignores attributes that don't come from any ontology" do | ||
| dummy = materials(:good_material).becomes(DummyMaterial) | ||
| dummy.ontology_term_links.create(field: :test_topics, term_uri: 'http://not-a-term.com') | ||
| dummy.ontology_term_links.create(field: :multi_test_topics, term_uri: 'http://also-not-a-term.com') | ||
|
|
||
| assert_equal dummy.ontology_term_links.count, 2 | ||
|
|
||
| assert_equal dummy.test_topics, [] | ||
| assert_equal dummy.test_topic_names, [] | ||
| assert_equal dummy.test_topic_uris, [] | ||
|
|
||
| assert_equal dummy.multi_test_topics, [] | ||
| assert_equal dummy.multi_test_topic_names, [] | ||
| assert_equal dummy.multi_test_topic_uris, [] | ||
|
|
||
| # Setting URI manually wipes out the ontology_term_links | ||
| dummy.test_topic_uris = ['http://not-a-term.com'] | ||
| dummy.multi_test_topic_uris = ['http://also-not-a-term.com'] | ||
|
|
||
| assert_equal dummy.ontology_term_links.count, 0 | ||
|
|
||
| # What if there is a term in here already, plus a bogus term link? | ||
| # (perhaps bogus because a previous ontology was take out). | ||
| dummy.test_topic_names = ['Bioinformatics'] | ||
| dummy.multi_test_topic_names = ['Biochemistry', 'Bioinformatics', 'Poodles'] | ||
| assert_equal dummy.ontology_term_links.count, 5 | ||
| assert_equal dummy.test_topic_links.count, 1 | ||
| assert_equal dummy.multi_test_topic_links.count, 4 | ||
|
|
||
| dummy.ontology_term_links.create(field: :test_topics, term_uri: 'http://not-a-term.com') | ||
| dummy.ontology_term_links.create(field: :multi_test_topics, term_uri: 'http://also-not-a-term.com') | ||
| assert_equal dummy.ontology_term_links.count, 7 | ||
| assert_equal dummy.test_topic_links.count, 2 | ||
| assert_equal dummy.multi_test_topic_links.count, 5 | ||
|
|
||
| # Terms with bogus URIs don't appear here | ||
| assert_equal dummy.test_topics.count, 1 | ||
| assert_equal dummy.test_topic_names, ['Bioinformatics'] | ||
| assert_equal dummy.test_topic_uris, ['http://dummy/Bioinformatics'] | ||
|
|
||
| assert_equal dummy.multi_test_topics.count, 4 | ||
| # Bioinformatics is in both ontologies | ||
| assert_equal Set.new(dummy.multi_test_topic_names), Set.new(['Biochemistry', 'Bioinformatics', 'Poodles']) | ||
| assert_equal Set.new(dummy.multi_test_topic_uris), | ||
| Set.new(['http://edamontology.org/topic_3292', | ||
| 'http://edamontology.org/topic_0091', | ||
| 'http://dummy/Bioinformatics', | ||
| 'http://dummy/Poodles']) | ||
| end | ||
|
|
||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.