Skip to content

Commit

Permalink
improved the algorithm for detecting the best match extended metadata
Browse files Browse the repository at this point in the history
due to unexpected results found during testing.
  • Loading branch information
stuzart committed Nov 19, 2024
1 parent 81fe740 commit 16e11a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
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)
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

0 comments on commit 16e11a4

Please sign in to comment.