Class: Accession::Sample

Inherits:
Object
  • Object
show all
Includes:
Accessionable, ActiveModel::Model
Defined in:
lib/accession/sample.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Accessionable

#date, #ebi_alias_datestamped, #filename, #schema_type, #to_file, #to_xml

Constructor Details

#initialize(standard_tags, sample) ⇒ Sample

Returns a new instance of Sample.



26
27
28
29
30
31
# File 'lib/accession/sample.rb', line 26

def initialize(standard_tags, sample)
  @standard_tags = standard_tags
  @sample = sample
  @tags = standard_tags.extract(sample.)
  @service = Service.new(sample.study_for_accessioning&.data_release_strategy)
end

Instance Attribute Details

#sampleObject (readonly)

Returns the value of attribute sample.



22
23
24
# File 'lib/accession/sample.rb', line 22

def sample
  @sample
end

#serviceObject (readonly)

Returns the value of attribute service.



22
23
24
# File 'lib/accession/sample.rb', line 22

def service
  @service
end

#standard_tagsObject (readonly)

Returns the value of attribute standard_tags.



22
23
24
# File 'lib/accession/sample.rb', line 22

def standard_tags
  @standard_tags
end

#tagsObject (readonly)

Returns the value of attribute tags.



22
23
24
# File 'lib/accession/sample.rb', line 22

def tags
  @tags
end

Instance Method Details

#accessioned?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/accession/sample.rb', line 105

def accessioned?
  ebi_accession_number.present?
end

#build_xml(xml) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/accession/sample.rb', line 60

def build_xml(xml) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  tag_groups = tags.by_group

  xml.SAMPLE_SET(XML_NAMESPACE) do # rubocop:disable Metrics/BlockLength
    xml.SAMPLE(alias: ebi_alias) do
      xml.TITLE title if title.present?
      xml.SAMPLE_NAME do
        tag_groups[:sample_name].each { |_k, tag| xml.tag!(tag.label.tr(' ', '_').upcase, tag.value) }
      end
      xml.SAMPLE_ATTRIBUTES do
        tag_groups[:sample_attributes].each do |_k, tag|
          xml.SAMPLE_ATTRIBUTE do
            xml.TAG tag.label
            if tag.label == 'gender'
              xml.VALUE tag.value.downcase
            else
              xml.VALUE tag.value
            end
          end
        end
        if service.ena?
          tag_groups[:array_express].each do |_k, tag|
            xml.SAMPLE_ATTRIBUTE do
              xml.TAG tag.array_express_label
              xml.VALUE tag.value
            end
          end
        end
      end
    end
  end
end

#ebi_aliasObject



93
94
95
# File 'lib/accession/sample.rb', line 93

def ebi_alias
  sample.uuid
end

#nameObject



33
34
35
# File 'lib/accession/sample.rb', line 33

def name
  @name ||= (sample..sample_public_name || sample.name).sanitize
end

#titleObject



37
38
39
# File 'lib/accession/sample.rb', line 37

def title
  @title ||= sample..sample_public_name || sample.sanger_sample_id
end

#update_accession_number(accession_number, event_user) ⇒ Object

Updates the accession number, saving the sample and adding an event to the events table for viewing under sample history.



99
100
101
102
103
# File 'lib/accession/sample.rb', line 99

def update_accession_number(accession_number, event_user)
  sample..sample_ebi_accession_number = accession_number
  sample..save # prevent an infinite loop due to after_save callbacks on sample.save
  sample.events.assigned_accession_number!('sample', accession_number, event_user)
end

#validate!Object

Validates the sample for accessioning.

If the sample is valid, the method returns silently. If the sample is invalid, logs an error message and raisesAccession::InternalValidationError with details of the validation errors.

Raises:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/accession/sample.rb', line 48

def validate!
  return if valid?

  # Add errors from the accession sample to the underlying sample for user feedback
  @sample.errors.add(:base, errors.full_messages.join(', '))

  # Add sample context to the error message for logging
  error_message = "Sample '#{sample.name}' cannot be accessioned: #{errors.full_messages.join(', ')}"
  Rails.logger.error(error_message)
  raise Accession::InternalValidationError, error_message
end