Class: Accessionable::Sample

Inherits:
Base
  • Object
show all
Defined in:
app/models/accessionable/sample.rb

Constant Summary collapse

ARRAY_EXPRESS_FIELDS =
%w[
  genotype
  phenotype
  strain_or_line
  developmental_stage
  sex
  cell_type
  disease_state
  compound
  dose
  immunoprecipitate
  growth_condition
  rnai
  organism_part
  species
  time_point
  age
  treatment
].freeze

Constants inherited from Base

Base::InvalidData

Instance Attribute Summary collapse

Attributes inherited from Base

#accession_number, #date, #date_short, #name

Instance Method Summary collapse

Methods inherited from Base

#center_name, #errors, #extract_accession_number, #extract_array_express_accession_number, #file_name, #label_scope, #released?, #schema_type, #update_array_express_accession_number!

Constructor Details

#initialize(sample) ⇒ Sample

rubocop:todo Metrics/MethodLength, Metrics/AbcSize



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/accessionable/sample.rb', line 29

def initialize(sample) # rubocop:todo Metrics/CyclomaticComplexity
  @sample = sample
  super(sample.ebi_accession_number)

  sampname = sample..sample_public_name
  @name = sampname.presence || sample.name
  @name = @name.gsub(/[^a-z\d]/i, '_') if @name.present?

  @common_name = sample..sample_common_name
  @taxon_id = sample..sample_taxon_id

  # Tags from the 'ENA attributes' property group
  # NOTE[xxx]: This used to also look for 'ENA links' and push them to the 'data[:links]' value, but group was empty
  @links = []
  @tags =
    sample.tags.map { |datum| Tag.new(label_scope, datum.name, sample.[datum.tag], datum.downcase) }

  # TODO: maybe unify this with the previous loop
  # Don't send managed AE data to SRA
  accession_service = AccessionService.select_for_sample(sample)
  unless accession_service.private?
    ARRAY_EXPRESS_FIELDS.each do |datum|
      value = sample..send(datum)
      next if value.blank?

      @tags << ArrayExpressTag.new(label_scope, datum, value)
    end
  end

  sample_hold = sample..sample_sra_hold
  @hold = sample_hold.presence || 'hold'
end

Instance Attribute Details

#common_nameObject (readonly)

Returns the value of attribute common_name.



26
27
28
# File 'app/models/accessionable/sample.rb', line 26

def common_name
  @common_name
end

Returns the value of attribute links.



26
27
28
# File 'app/models/accessionable/sample.rb', line 26

def links
  @links
end

#tagsObject (readonly)

Returns the value of attribute tags.



26
27
28
# File 'app/models/accessionable/sample.rb', line 26

def tags
  @tags
end

#taxon_idObject (readonly)

Returns the value of attribute taxon_id.



26
27
28
# File 'app/models/accessionable/sample.rb', line 26

def taxon_id
  @taxon_id
end

Instance Method Details

#accessionable_idObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



64
65
66
# File 'app/models/accessionable/sample.rb', line 64

def accessionable_id
  @sample.id
end

#aliasObject



68
69
70
# File 'app/models/accessionable/sample.rb', line 68

def alias
  @sample.uuid
end

#protect?(service) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/accessionable/sample.rb', line 109

def protect?(service)
  service.sample_visibility(@sample) == AccessionService::PROTECT
end

#sample_element_attributesObject



76
77
78
79
# File 'app/models/accessionable/sample.rb', line 76

def sample_element_attributes
  # In case the accession number is defined, we won't send the alias
  { alias: self.alias, accession: accession_number }.tap { |obj| obj.delete(:alias) if accession_number.present? }
end

#titleObject



72
73
74
# File 'app/models/accessionable/sample.rb', line 72

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

#update_accession_number!(user, accession_number) ⇒ Object

rubocop:enable Metrics/MethodLength



102
103
104
105
106
107
# File 'app/models/accessionable/sample.rb', line 102

def update_accession_number!(user, accession_number)
  @accession_number = accession_number
  @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, user)
end

#xmlObject

rubocop:todo Metrics/MethodLength



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/accessionable/sample.rb', line 82

def xml # rubocop:todo Metrics/AbcSize
  xml = Builder::XmlMarkup.new
  xml.instruct!
  xml.SAMPLE_SET('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance') do
    xml.SAMPLE(sample_element_attributes) do
      xml.TITLE title unless title.nil?
      xml.SAMPLE_NAME do
        xml.COMMON_NAME common_name
        xml.TAXON_ID taxon_id
      end
      xml.SAMPLE_ATTRIBUTES { tags.each { |tag| xml.SAMPLE_ATTRIBUTE { tag.build(xml) } } } if tags.present?

      xml.SAMPLE_LINKS {} if links.present?
    end
  end
  xml.target!
end