Skip to content

Commit

Permalink
Merge branch 'seek-1.14' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Nov 9, 2023
2 parents fb4803b + 8bef10a commit 4d0808b
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 21 deletions.
2 changes: 0 additions & 2 deletions app/assets/javascripts/sample_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ var SampleTypes = {
attributeTypeChanged: function (e, resetSelection=true) {
//check if it is a controlled vocab, and change the state of the controlled vocab selector if need be
var use_cv = $j(this).find(':selected').data('use-cv');
var is_ontology = $j(this).find(':selected').data('is-ontology');
var cv_element = $j(this).siblings('.controlled-vocab-block');
if (use_cv) {
var cv_selection = cv_element.find('.controlled-vocab-selection');
cv_selection.find('option').show();
cv_selection.find(`option[data-is-ontology="${!is_ontology}"]`).hide();
if (resetSelection) cv_selection.find('option:selected').prop("selected", false);
cv_element.show();
}
Expand Down
3 changes: 3 additions & 0 deletions app/models/sample_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class SampleAttribute < ApplicationRecord
# sample type exists
attr_reader :deferred_link_to_self

# whether this attribute is tied to a controlled vocab which is based on an ontology
delegate :ontology_based?, to: :sample_controlled_vocab, allow_nil: true

def title=(title)
super
store_accessor_name
Expand Down
5 changes: 1 addition & 4 deletions app/models/sample_attribute_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SampleAttributeType < ApplicationRecord

has_many :sample_attributes, inverse_of: :sample_attribute_type
has_many :extended_metadata_attributes, inverse_of: :sample_attribute_type
has_many :isa_template_attributes, class_name: 'TemplateAttribute', inverse_of: :sample_attribute_type

before_save :set_defaults_attributes
after_initialize :set_defaults_attributes
Expand Down Expand Up @@ -108,10 +109,6 @@ def seek_data_file?
base_type == Seek::Samples::BaseType::SEEK_DATA_FILE
end

def ontology?
controlled_vocab? && title == 'Ontology'
end

def base_type_handler(additional_options = {})
Seek::Samples::AttributeTypeHandlers::AttributeTypeHandlerFactory.instance.for_base_type(base_type, additional_options)
end
Expand Down
6 changes: 4 additions & 2 deletions app/views/sample_types/_sample_attribute_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@
</td>
<td>
<%= select_tag "#{field_name_prefix}[sample_attribute_type_id]",
options_for_select(displayed_sample_attribute_types.sort_by(&:title).sort_by { |t| t.default? ? 0 : 1 }.map { |t| [t.title, t.id,{'data-use-cv'=>t.controlled_vocab? || t.seek_cv_list?,'data-is-ontology'=>t.ontology?, 'data-is-seek-sample'=>t.seek_sample? || t.seek_sample_multi? }] }, attribute_type_id),
options_for_select(displayed_sample_attribute_types.sort_by(&:title).sort_by { |t| t.default? ? 0 : 1 }.map do |t|
[t.title, t.id,{'data-use-cv': t.controlled_vocab? || t.seek_cv_list?, 'data-is-seek-sample': t.seek_sample? || t.seek_sample_multi? }]
end , attribute_type_id),
class: 'form-control sample-type-attribute-type',
disabled: !allow_type_change, data: { attr: "type" } %>
<div class='controlled-vocab-block' style="<%= 'display:none;' unless sample_attribute.try(:controlled_vocab?) || sample_attribute.try(:seek_cv_list?) %>">
<br/>
<%= select_tag "#{field_name_prefix}[sample_controlled_vocab_id]",
options_for_select(SampleControlledVocab.all.map { |scv| [scv.title, scv.id, {'data-editable'=>scv.can_edit?, 'data-is-ontology'=>scv.custom_input?}] },
options_for_select(SampleControlledVocab.all.map { |scv| [scv.title, scv.id, {'data-editable': scv.can_edit?}] },
sample_controlled_vocab_id),
include_blank: true,
class: 'form-control controlled-vocab-selection',
Expand Down
6 changes: 4 additions & 2 deletions app/views/templates/_template_attribute_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
</td>
<td>
<%= select_tag "#{field_name_prefix}[sample_attribute_type_id]",
options_for_select(SampleAttributeType.all.sort_by(&:title).sort_by { |t| t.default? ? 0 : 1 }.map { |t| [t.title, t.id,{'data-is-cv'=>t.controlled_vocab?,'data-is-ontology'=>t.ontology?, 'data-is-seek-sample'=>t.seek_sample? || t.seek_sample_multi? }] }, attribute_type_id),
options_for_select(SampleAttributeType.all.sort_by(&:title).sort_by { |t| t.default? ? 0 : 1 }.map do |t|
[t.title, t.id,{'data-is-cv': t.controlled_vocab?,'data-is-seek-sample': t.seek_sample? || t.seek_sample_multi? }]
end, attribute_type_id),
class: 'form-control sample-type-attribute-type', data: {attr: "type"} %>

<div class='controlled-vocab-block' style="<%= 'display:none;' unless template_attribute.try(:controlled_vocab?) %>">
<br/>
<%= select_tag "#{field_name_prefix}[sample_controlled_vocab_id]",
options_for_select(SampleControlledVocab.all.map { |scv| [scv.title, scv.id, {'data-editable'=>scv.can_edit?, 'data-is-ontology'=>scv.custom_input?}] },
options_for_select(SampleControlledVocab.all.map { |scv| [scv.title, scv.id, {'data-editable': scv.can_edit?}] },
sample_controlled_vocab_id),include_blank: true,
class: 'form-control controlled-vocab-selection', data: {attr: "cv_id"} %>

Expand Down
11 changes: 4 additions & 7 deletions db/seeds/007_sample_attribute_types.seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@
seek_sample_type = SampleAttributeType.find_or_initialize_by(title:'Registered Sample')
seek_sample_type.update(base_type: Seek::Samples::BaseType::SEEK_SAMPLE)

seek_sample_multi_type = SampleAttributeType.find_or_initialize_by(title:'Registered Sample (multiple)')
seek_sample_multi_type = SampleAttributeType.find_or_initialize_by(title:'Registered Sample List')
seek_sample_multi_type.update(base_type: Seek::Samples::BaseType::SEEK_SAMPLE_MULTI)

cv_type = SampleAttributeType.find_or_initialize_by(title:'Controlled Vocabulary')
cv_type.update(base_type: Seek::Samples::BaseType::CV)

cv_list_type = SampleAttributeType.find_or_initialize_by(title:'Controlled Vocabulary List')
cv_list_type.update(base_type: Seek::Samples::BaseType::CV_LIST)

uri_type = SampleAttributeType.find_or_initialize_by(title:'URI')
uri_type.update(base_type: Seek::Samples::BaseType::STRING, regexp: URI.regexp.to_s, placeholder: 'http://www.example.com/123', resolution:'\\0')

Expand All @@ -79,12 +82,6 @@
data_file_type = SampleAttributeType.find_or_initialize_by(title: 'Registered Data file')
data_file_type.update(base_type: Seek::Samples::BaseType::SEEK_DATA_FILE)

ontology_type = SampleAttributeType.find_or_initialize_by(title:'Ontology')
ontology_type.update(base_type: Seek::Samples::BaseType::CV)

cv_list_type = SampleAttributeType.find_or_initialize_by(title:'Controlled Vocabulary List')
cv_list_type.update(base_type: Seek::Samples::BaseType::CV_LIST)

linked_extended_metadata_type = SampleAttributeType.find_or_initialize_by(title:'Linked Extended Metadata')
linked_extended_metadata_type.update(base_type: Seek::Samples::BaseType::LINKED_EXTENDED_METADATA)

Expand Down
4 changes: 2 additions & 2 deletions lib/isa_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def convert_ontologies
sample_types = @investigation.studies.map(&:sample_types) + @investigation.assays.map(&:sample_type)
sample_types.flatten.each do |sa|
sa.sample_attributes.each do |atr|
source_ontologies << atr.sample_controlled_vocab.source_ontology if atr.sample_attribute_type.ontology?
source_ontologies << atr.sample_controlled_vocab.source_ontology if atr.ontology_based?
end
end
source_ontologies.uniq.map { |s| { name: s, file: '', version: '', description: '' } }
Expand Down Expand Up @@ -487,7 +487,7 @@ def extract_sample_ids(obj, type)
end

def get_ontology_details(sample_attribute, label, vocab_term)
is_ontology = sample_attribute.sample_attribute_type.ontology?
is_ontology = sample_attribute.ontology_based?
iri = ''
if is_ontology
iri =
Expand Down
40 changes: 38 additions & 2 deletions lib/tasks/seek_upgrades.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace :seek do
decouple_extracted_samples_projects
link_sample_datafile_attributes
strip_sample_attribute_pids
rename_registered_sample_multiple_attribute_type
remove_ontology_attribute_type
]

# these are the tasks that are executes for each upgrade as standard, and rarely change
Expand Down Expand Up @@ -48,8 +50,16 @@ namespace :seek do
end
end

task(rename_registered_sample_multiple_attribute_type: [:environment]) do
attr = SampleAttributeType.find_by(title:'Registered Sample (multiple)')
if attr
puts "..... Renaming sample attribute type 'Registered Sample (multiple)' to 'Registered Sample List'."
attr.update_column(:title, 'Registered Sample List')
end
end

task(strip_sample_attribute_pids: [:environment]) do
puts '... Stripping Sample Attribute PIds ...'
puts '..... Stripping Sample Attribute PIds ...'
n = 0
SampleAttribute.where('pid is NOT NULL AND pid !=?','').each do |attribute|
new_pid = attribute.pid.strip
Expand All @@ -58,7 +68,33 @@ namespace :seek do
n += 1
end
end
puts "... Finished stripping #{n} Sample Attribute PIds."
puts "..... Finished stripping #{n} Sample Attribute PIds."
end

task(remove_ontology_attribute_type: [:environment]) do
ontology_attr_type = SampleAttributeType.find_by(title:'Ontology')
cv_attr_type = SampleAttributeType.find_by(title:'Controlled Vocabulary')
if ontology_attr_type
puts '..... Removing the Ontology sample attribute type ...'
if cv_attr_type
if ontology_attr_type.sample_attributes.any?
puts "..... Moving #{ontology_attr_type.sample_attributes.count} sample attributes to Controlled Vocabulary"
ontology_attr_type.sample_attributes.each do |attr_type|
attr_type.update_column(:sample_attribute_type_id, cv_attr_type.id)
end
end
if ontology_attr_type.isa_template_attributes.any?
puts "..... Moving #{ontology_attr_type.isa_template_attributes.count} template attributes to Controlled Vocabulary"
ontology_attr_type.isa_template_attributes.each do |attr_type|
attr_type.update_column(:sample_attribute_type_id, cv_attr_type.id)
end
end

ontology_attr_type.destroy
else
puts '..... Target Controlled Vocabulary attribute type not found'
end
end
end

task(decouple_extracted_samples_policies: [:environment]) do
Expand Down
16 changes: 16 additions & 0 deletions test/unit/sample_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ class SampleAttributeTest < ActiveSupport::TestCase
assert_equal '', attribute.short_pid
end

test 'ontology_based?' do
attribute = FactoryBot.create(:sample_sample_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.ontology_based?

attribute = FactoryBot.create(:simple_string_sample_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.ontology_based?

attribute = FactoryBot.create(:apples_controlled_vocab_attribute, sample_type: FactoryBot.create(:simple_sample_type))
refute attribute.sample_controlled_vocab.ontology_based?
refute attribute.ontology_based?

attribute.sample_controlled_vocab = FactoryBot.create(:topics_controlled_vocab)
assert attribute.sample_controlled_vocab.ontology_based?
assert attribute.ontology_based?
end

private

def valid_value?(attribute, value)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/sample_attribute_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ class SampleAttributeTypeTest < ActiveSupport::TestCase
type = FactoryBot.create(:controlled_vocab_attribute_type)
refute type.seek_data_file?
end

test 'isa_template_attributes' do
type = FactoryBot.create(:age_sample_attribute_type)
type2 = FactoryBot.create(:weight_sample_attribute_type)

ta1 = FactoryBot.create(:template_attribute, sample_attribute_type: type)
ta2 = FactoryBot.create(:template_attribute, sample_attribute_type: type)
ta3 = FactoryBot.create(:template_attribute, sample_attribute_type: type)
ta4 = FactoryBot.create(:template_attribute, sample_attribute_type: type2)

type.reload
template_attributes = type.isa_template_attributes
assert_equal 3, template_attributes.count
assert_includes template_attributes, ta1
assert_includes template_attributes, ta2
assert_includes template_attributes, ta3
assert_not_includes template_attributes, ta4
end
end

0 comments on commit 4d0808b

Please sign in to comment.