Skip to content

Commit

Permalink
Merge branch 'seek-1.15' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Sep 23, 2024
2 parents dc9c3b4 + fe711f2 commit 41337ac
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 47 deletions.
10 changes: 10 additions & 0 deletions app/assets/javascripts/sample_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ var SampleTypes = {
else {
seek_sample_element.hide();
}
},

expandIRI: function () {
var termIRI = $j(this).next('.term-iri');
termIRI.toggleClass('visible');
}

};


$j(document).ready(function() {
$j('.term-label').on('click', SampleTypes.expandIRI);
});
18 changes: 18 additions & 0 deletions app/assets/stylesheets/linked_extended_metadata.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
.term-label {
font-weight: normal;
text-decoration: underline;
}

.term-iri {
display: none;
background-color: rgba(177, 183, 187, 0.47);
color: black;
font-weight: normal;
}

.term-iri.visible {
display: inline;
opacity: 1;
margin-left: 10px;
}

.linked_extended_metdata .panel-default .panel-heading {
font-weight: bold;
color: #333;
Expand Down
70 changes: 52 additions & 18 deletions app/controllers/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,45 @@ def typeahead

def query
project_ids = params[:project_ids]&.map(&:to_i)

attribute_filter_value = params[:template_attribute_value]&.downcase
@result = params[:template_id].present? ?
Template.find(params[:template_id]).sample_types.map(&:samples).flatten : []

if params[:template_attribute_id].present? && params[:template_attribute_value].present?
attribute_title = TemplateAttribute.find(params[:template_attribute_id]).title
@result = @result.select { |s| s.get_attribute_value(attribute_title)&.include?(params[:template_attribute_value]) }
if params[:template_attribute_id].present? && attribute_filter_value.present?
template_attribute = TemplateAttribute.find(params[:template_attribute_id])
@result = @result.select do |s|
sample_attribute = s.sample_type.sample_attributes.detect { |sa| template_attribute.sample_attributes.include? sa }
sample_attribute_title = sample_attribute&.title
if sample_attribute&.sample_attribute_type&.seek_sample_multi?
attr_value = s.get_attribute_value(sample_attribute_title)
attr_value&.any? { |v| v[:title].downcase.include?(attribute_filter_value) }
elsif sample_attribute&.sample_attribute_type&.seek_sample?
s.get_attribute_value(sample_attribute_title)[:title]&.downcase&.include?(attribute_filter_value)
elsif sample_attribute&.sample_attribute_type&.seek_cv_list?
attr_value = s.get_attribute_value(sample_attribute_title)
attr_value&.any? { |v| v.downcase.include?(attribute_filter_value) }
else
s.get_attribute_value(sample_attribute_title)&.downcase&.include?(attribute_filter_value)
end
end
end

if params[:input_template_id].present? # linked
title =
TemplateAttribute.find(params[:input_attribute_id]).title if params[:input_attribute_id].present?
@result = find_samples(@result, :linked_samples,
{ attribute_id: params[:input_attribute_id],
input_template_attribute =
TemplateAttribute.find(params[:input_attribute_id])
@result = filter_linked_samples(@result, :linked_samples,
{ attribute_id: params[:input_attribute_id],
attribute_value: params[:input_attribute_value],
template_id: params[:input_template_id] }, title)
template_id: params[:input_template_id] }, input_template_attribute)
end

if params[:output_template_id].present? # linking
title =
TemplateAttribute.find(params[:output_attribute_id]).title if params[:output_attribute_id].present?
@result = find_samples(@result, :linking_samples,
{ attribute_id: params[:output_attribute_id],
output_template_attribute =
TemplateAttribute.find(params[:output_attribute_id])
@result = filter_linked_samples(@result, :linking_samples,
{ attribute_id: params[:output_attribute_id],
attribute_value: params[:output_attribute_value],
template_id: params[:output_template_id] }, title)
template_id: params[:output_template_id] }, output_template_attribute)
end

@result = @result.select { |s| (project_ids & s.project_ids).any? } if project_ids.present?
Expand Down Expand Up @@ -321,16 +335,36 @@ def find_index_assets
end
end

def find_samples(samples, link, options, title)
# Filters linked samples based on the provided options and template attribute title.
#
# @param samples [Array<Sample>] the list of samples to filter
# @param link [Symbol] the method to call on each sample to get the linked samples (:linked_samples or :linking_samples)
# @param options [Hash] the options for filtering
# @option options [Integer] :template_id the ID of the template to filter by
# @option options [String] :attribute_value the value of the attribute to filter by
# @option options [Integer] :attribute_id the ID of the attribute to filter by
# @param template_attribute_title [String] the title of the template attribute to filter by
# @return [Array<Sample>] the filtered list of samples
def filter_linked_samples(samples, link, options, template_attribute)
raise ArgumentError, "Invalid linking method provided. '#{link.to_s}' is not allowed!" unless %i[linked_samples linking_samples].include? link

template_attribute_title = template_attribute&.title
samples.select do |s|
s.send(link).any? do |x|
selected = x.sample_type.template_id == options[:template_id].to_i
selected = x.get_attribute_value(title)&.include?(options[:attribute_value]) if title.present? && selected
selected || find_samples([x], link, options, title).present?
if template_attribute.sample_attribute_type.seek_sample_multi?
selected = x.get_attribute_value(template_attribute_title)&.any? { |v| v[:title].downcase.include?(options[:attribute_value]) } if template_attribute.present? && selected
elsif template_attribute.sample_attribute_type.seek_sample?
selected = x.get_attribute_value(template_attribute_title)&[:title].downcase&.include?(options[:attribute_value]) if template_attribute.present? && selected
elsif template_attribute.sample_attribute_type.seek_cv_list?
selected = x.get_attribute_value(template_attribute_title)&.any? { |v| v.downcase.include?(options[:attribute_value]) } if template_attribute.present? && selected
else
selected = x.get_attribute_value(template_attribute_title)&.downcase&.include?(options[:attribute_value]&.downcase) if template_attribute.present? && selected
end
selected || filter_linked_samples([x], link, options, template_attribute).present?
end
end
end

def templates_enabled?
unless Seek::Config.isa_json_compliance_enabled
flash[:error] = 'Not available'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def populate_template
# post
def template_attributes
template = Template.find(params[:id])
items = template.template_attributes.map { |a| { id: a.id, title: a.title } }
items = template.template_attributes.map { |a| { id: a.id, title: a.title.sanitize } }
respond_to do |format|
format.json { render json: items.to_json }
end
Expand Down
15 changes: 10 additions & 5 deletions app/helpers/samples_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def display_attribute_value(value, attribute, options = {})
when Seek::Samples::BaseType::CV
seek_cv_attribute_display(value, attribute)
when Seek::Samples::BaseType::CV_LIST
value.each{|v| seek_cv_attribute_display(v, attribute) }.join(', ')
value.map do |v|
seek_cv_attribute_display(v, attribute)
end.join(', ').html_safe
when Seek::Samples::BaseType::LINKED_EXTENDED_METADATA
linked_extended_metadata_attribute_display(value, attribute)
when Seek::Samples::BaseType::LINKED_EXTENDED_METADATA_MULTI
Expand Down Expand Up @@ -173,12 +175,15 @@ def select_cv_source_ontology(sample_controlled_vocab)
end

def seek_cv_attribute_display(value, attribute)
term = attribute.sample_controlled_vocab.sample_controlled_vocab_terms.where(label:value).last
content = value
term = attribute.sample_controlled_vocab.sample_controlled_vocab_terms.where(label: value).last
if term && term.iri.present?
content << " (#{term.iri}) "
iri_content = term.iri.match?(/^https?:\/\//) ? link_to(term.iri, term.iri, target: '_blank') : term.iri
label_tag = content_tag(:label, term.label, class: 'term-label')
iri_tag = content_tag(:label, iri_content, class: 'term-iri badge')
"#{label_tag}#{iri_tag}".html_safe
else
term.label
end
content
end

def linked_extended_metadata_attribute_display(value, attribute)
Expand Down
22 changes: 11 additions & 11 deletions app/helpers/templates_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def template_attribute_details(template_attribute)
def load_templates
privilege = Seek::Permissions::Translator.translate('view')
Template.order(:group, :group_order).select { |t| t.can_perform?(privilege) }.map do |item|
{ title: item.title,
group: item.group,
level: item.level,
organism: item.organism,
{ title: item.title&.sanitize,
group: item.group&.sanitize,
level: item.level&.sanitize,
organism: item.organism&.sanitize,
template_id: item.id,
description: item.description,
description: item.description&.sanitize,
group_order: item.group_order,
attributes: item.template_attributes.order(:pos).map { |a| map_template_attributes(a) } }
end
Expand Down Expand Up @@ -77,19 +77,19 @@ def template_attribute_type_link(template_attribute)
def map_template_attributes(attribute)
{
attribute_type_id: attribute.sample_attribute_type_id,
data_type: SampleAttributeType.find(attribute.sample_attribute_type_id)&.title,
data_type: SampleAttributeType.find(attribute.sample_attribute_type_id)&.title&.sanitize,
cv_id: attribute.sample_controlled_vocab_id,
allow_cv_free_text: attribute.allow_cv_free_text,
title: attribute.title,
title: attribute.title&.sanitize,
is_title: attribute.is_title,
short_name: attribute.short_name,
description: attribute.description,
pid: attribute.pid,
short_name: attribute.short_name&.sanitize,
description: attribute.description&.sanitize,
pid: attribute.pid&.sanitize,
required: attribute.required,
unit_id: attribute.unit_id,
pos: attribute.pos,
isa_tag_id: attribute.isa_tag_id,
isa_tag_title: attribute.isa_tag&.title,
isa_tag_title: attribute.isa_tag&.title&.sanitize,
linked_sample_type_id: attribute.linked_sample_type_id,
template_attribute_id: attribute.id
}
Expand Down
24 changes: 24 additions & 0 deletions app/models/sample_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ class SampleType < ApplicationRecord

has_annotation_type :sample_type_tag, method_name: :tags

# Creates sample attributes from an ISA template.
# @param template [Template] The ISA template to create sample attributes from.
# @param linked_sample_type [SampleType, nil] The linked sample type, if any.
def create_sample_attributes_from_isa_template(template, linked_sample_type = nil)
self.sample_attributes = template.template_attributes.map do |temp_attr|
has_seek_samples = temp_attr.sample_attribute_type.seek_sample? || temp_attr.sample_attribute_type.seek_sample_multi?
has_linked_st = linked_sample_type && has_seek_samples

SampleAttribute.new(
title: temp_attr.title,
description: temp_attr.description,
sample_attribute_type_id: temp_attr.sample_attribute_type_id,
required: temp_attr.required,
unit_id: temp_attr.unit_id,
is_title: temp_attr.is_title,
sample_controlled_vocab_id: temp_attr.sample_controlled_vocab_id,
linked_sample_type_id: has_linked_st ? linked_sample_type&.id : nil,
isa_tag_id: temp_attr.isa_tag_id,
allow_cv_free_text: temp_attr.allow_cv_free_text,
template_attribute_id: temp_attr.id
)
end
end

def level
isa_template&.level
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/samples/_attribute_values.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<label><%= sample_attribute_display_title(attribute) %> : </label>
<%= display_attribute(sample, attribute, link: true) %>
</p>
<% end %>
<% end %>
8 changes: 4 additions & 4 deletions app/views/samples/query_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<label>Select Project(s)</label>
<select id="projects" class="form-control select2" multiple="multiple">
<% projects.each do |p| %>
<option value='<%= p[:id] %>'><%= p[:title] %></option>
<option value='<%= p[:id] %>'><%= p[:title].sanitize %></option>
<% end %>
</select>
</div>
Expand All @@ -31,7 +31,7 @@
<% templates.pluck(:group).uniq.each do |group_name| %>
<optgroup label='<%= group_name %>'>
<% templates.select{|t| t[:group]==group_name}.each do |t| %>
<option value='<%= t[:template_id] %>'><%=t[:title]%></option>
<option value='<%= t[:template_id] %>'><%=t[:title].sanitize%></option>
<% end %>
</optgroup>
<% end %>
Expand Down Expand Up @@ -66,7 +66,7 @@
<% templates.pluck(:group).uniq.each do |group_name| %>
<optgroup label='<%= group_name %>'>
<% templates.select{|t| t[:group]==group_name}.each do |t| %>
<option value='<%= t[:template_id] %>'><%= t[:title] %></option>
<option value='<%= t[:template_id] %>'><%= t[:title].sanitize %></option>
<% end %>
</optgroup>
<% end %>
Expand Down Expand Up @@ -99,7 +99,7 @@
<% templates.pluck(:group).uniq.each do |group_name| %>
<optgroup label='<%= group_name %>'>
<% templates.select{|t| t[:group]==group_name}.each do |t| %>
<option value='<%= t[:template_id] %>'><%= t[:title] %></option>
<option value='<%= t[:template_id] %>'><%= t[:title].sanitize %></option>
<% end %>
</optgroup>
<% end %>
Expand Down
Loading

0 comments on commit 41337ac

Please sign in to comment.