Class: Accessionable::Sample

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

Overview

rubocop:todo Style/Documentation

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

#add_updated_event, #center_name, #errors, #extract_accession_number, #extract_array_express_accession_number, #file_name, #label_scope, #schema_type, #update_array_express_accession_number!

Constructor Details

#initialize(sample) ⇒ Sample

Returns a new instance of Sample.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/accessionable/sample.rb', line 10

def initialize(sample)
  @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 do |datum|
    Tag.new(label_scope, datum.name, sample.[datum.tag], datum.downcase)
  end

  # TODO: maybe unify this with the previous loop
  # Don't send managed AE data to SRA
  unless sample.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


8
9
10
# File 'app/models/accessionable/sample.rb', line 8

def common_name
  @common_name
end

Returns the value of attribute links


8
9
10
# File 'app/models/accessionable/sample.rb', line 8

def links
  @links
end

#tagsObject (readonly)

Returns the value of attribute tags


8
9
10
# File 'app/models/accessionable/sample.rb', line 8

def tags
  @tags
end

#taxon_idObject (readonly)

Returns the value of attribute taxon_id


8
9
10
# File 'app/models/accessionable/sample.rb', line 8

def taxon_id
  @taxon_id
end

Instance Method Details

#accessionable_idObject


43
44
45
# File 'app/models/accessionable/sample.rb', line 43

def accessionable_id
  @sample.id
end

#aliasObject


47
48
49
# File 'app/models/accessionable/sample.rb', line 47

def alias
  @sample.uuid
end

#protect?(service) ⇒ Boolean

Returns:

  • (Boolean)

96
97
98
# File 'app/models/accessionable/sample.rb', line 96

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

#released?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'app/models/accessionable/sample.rb', line 100

def released?
  @sample.released?
end

#sample_element_attributesObject


55
56
57
58
59
60
61
62
63
# File 'app/models/accessionable/sample.rb', line 55

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

#titleObject


51
52
53
# File 'app/models/accessionable/sample.rb', line 51

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

#update_accession_number!(user, accession_number) ⇒ Object


89
90
91
92
93
94
# File 'app/models/accessionable/sample.rb', line 89

def update_accession_number!(user, accession_number)
  @accession_number = accession_number
  add_updated_event(user, "Sample #{@sample.id}", @sample) if @accession_number
  @sample..sample_ebi_accession_number = accession_number
  @sample.save!
end

#xmlObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/accessionable/sample.rb', line 65

def xml
  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 do
        tags.each do |tag|
          xml.SAMPLE_ATTRIBUTE do
            tag.build(xml)
          end
        end
      end if tags.present?

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