Skip to content
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

Improve how the E.M type is detected to match fair data station properties #2069

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/seek/fair_data_station/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ def detect_extended_metadata_type(seek_entity, datastation_entity)

# collect and sort those with the most properties that match, eliminating any where no properties match
candidates = ::ExtendedMetadataType.where(supported_type: seek_entity.class.name).includes(:extended_metadata_attributes).collect do |emt|
pids = collect_extended_metadata_type_attibute_pids(emt)
score = (property_ids - pids).length
emt = nil if (property_ids & pids).empty?
[score, emt]
extended_metadata_property_ids = collect_extended_metadata_type_attibute_pids(emt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will properties in the datastation_entity that are not covered by the EMT be ignored?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, they are included in the difference score. It's why I used

(property_ids | extended_metadata_property_ids) - intersection

instead of just

extended_metadata_property_ids - property_ids

intersection = (property_ids & extended_metadata_property_ids)
difference = (property_ids | extended_metadata_property_ids) - intersection
emt = nil if intersection.length.zero?
[intersection.length, difference.length, emt]
end.sort_by do |x|
x[0]
# order by the number of properties matched coming top, but downgraded by the number of differences
[-x[0], x[1]]
end

candidates.first&.last
Expand Down
1 change: 1 addition & 0 deletions test/factories/extended_metadata_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
supported_type { 'Assay' }
after(:build) do |a|
a.extended_metadata_attributes << FactoryBot.create(:study_title_extended_metadata_attribute, title: 'Facility', pid:'http://fairbydesign.nl/ontology/facility')
a.extended_metadata_attributes << FactoryBot.create(:study_title_extended_metadata_attribute, title: 'Protocol', pid:'http://fairbydesign.nl/ontology/protocol')
a.extended_metadata_attributes << FactoryBot.create(:study_title_extended_metadata_attribute, title: 'Forward Primer', pid:'http://fairbydesign.nl/ontology/forwardPrimer')
a.extended_metadata_attributes << FactoryBot.create(:study_title_extended_metadata_attribute, title: 'Instrument Model', pid:'http://fairbydesign.nl/ontology/instrument_model')
a.extended_metadata_attributes << FactoryBot.create(:study_title_extended_metadata_attribute, title: 'Library Selection', pid:'http://fairbydesign.nl/ontology/library_selection')
Expand Down
33 changes: 33 additions & 0 deletions test/unit/fair_data_station/fair_data_station_writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class FairDataStationWriterTest < ActiveSupport::TestCase
assert_equal assay_metadata_type, assay.extended_metadata.extended_metadata_type

assert_equal 'NMIMR', assay.extended_metadata.get_attribute_value('Facility')
assert_equal 'MiSeq Reagent Kit v3 (600-cycle) with a 20% PhiX (Illumina) spike-in', assay.extended_metadata.get_attribute_value('Protocol')
assert_equal 'CCTACGGGNGGCWGCAG', assay.extended_metadata.get_attribute_value('Forward Primer')
assert_equal 'Illumina MiSeq', assay.extended_metadata.get_attribute_value('Instrument Model')
assert_equal 'PCR', assay.extended_metadata.get_attribute_value('Library Selection')
Expand Down Expand Up @@ -685,6 +686,38 @@ class FairDataStationWriterTest < ActiveSupport::TestCase

end

test 'detect extended metadata type' do
virtual_demo_assay = FactoryBot.create(:fairdata_virtual_demo_assay_extended_metadata)
seek_test_case_assay = FactoryBot.create(:fairdata_test_case_assay_extended_metadata)
FactoryBot.create(:simple_assay_extended_metadata_type)
FactoryBot.create(:fairdata_test_case_obsv_unit_extended_metadata)
FactoryBot.create(:simple_observation_unit_extended_metadata_type)
writer = Seek::FairDataStation::Writer.new

path = "#{Rails.root}/test/fixtures/files/fair_data_station/seek-fair-data-station-test-case.ttl"
inv = Seek::FairDataStation::Reader.new.parse_graph(path).first
assay = inv.studies.first.assays.first
seek_assay = ::Assay.new
detected_type = writer.send(:detect_extended_metadata_type, seek_assay, assay)
assert_equal seek_test_case_assay, detected_type

path = "#{Rails.root}/test/fixtures/files/fair_data_station/demo.ttl"
inv = Seek::FairDataStation::Reader.new.parse_graph(path).first
assay = inv.studies.first.assays.first
detected_type = writer.send(:detect_extended_metadata_type, seek_assay, assay)
assert_equal virtual_demo_assay, detected_type

path = "#{Rails.root}/test/fixtures/files/fair_data_station/indpensim.ttl"
inv = Seek::FairDataStation::Reader.new.parse_graph(path).first
obs_unit = inv.studies.first.observation_units.first
detected_type = writer.send(:detect_extended_metadata_type, ::ObservationUnit.new, obs_unit)
assert_nil detected_type

inpensim_obs_unit = FactoryBot.create(:fairdata_indpensim_obsv_unit_extended_metadata)
detected_type = writer.send(:detect_extended_metadata_type, ::ObservationUnit.new, obs_unit)
assert_equal inpensim_obs_unit, detected_type
end

private

def setup_test_case_investigation
Expand Down