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

#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


9
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
# File 'app/models/accessionable/sample.rb', line 9

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, '_') unless @name.blank?

  @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 unless value.present?

      @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


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

def common_name
  @common_name
end

Returns the value of attribute links


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

def links
  @links
end

#tagsObject (readonly)

Returns the value of attribute tags


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

def tags
  @tags
end

#taxon_idObject (readonly)

Returns the value of attribute taxon_id


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

def taxon_id
  @taxon_id
end

Instance Method Details

#accessionable_idObject


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

def accessionable_id
  @sample.id
end

#aliasObject


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

def alias
  @sample.uuid
end

#protect?(service) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#released?Boolean

Returns:

  • (Boolean)

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

def released?
  @sample.released?
end

#sample_element_attributesObject


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

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) unless accession_number.blank?
  end
end

#titleObject


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

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

#update_accession_number!(user, accession_number) ⇒ Object


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

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


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

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

      xml.SAMPLE_LINKS {} unless links.blank?
    }
  }
  xml.target!
end