Module: Lab::ConceptsService

Defined in:
app/services/lab/concepts_service.rb

Overview

A read-only repository of sort for all lab-centric concepts.

Class Method Summary collapse

Class Method Details

.reasons_for_testObject



135
136
137
138
139
140
# File 'app/services/lab/concepts_service.rb', line 135

def self.reasons_for_test
  ConceptSet.find_members_by_name(Lab::Metadata::REASON_FOR_TEST_CONCEPT_NAME)
            .joins('INNER JOIN concept_name ON concept_name.concept_id = concept_set.concept_id')
            .select('concept_name.concept_id, concept_name.name, concept_name.uuid')
            .map { |concept| { name: concept.name, concept_id: concept.concept_id, uuid: concept.uuid } }
end

.specimen_types(name: nil, test_type: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/lab/concepts_service.rb', line 71

def self.specimen_types(name: nil, test_type: nil)
  specimen_types = ConceptSet.find_members_by_name(Lab::Metadata::SPECIMEN_TYPE_CONCEPT_NAME)
  specimen_types = specimen_types.filter_members(name: name) if name

  unless test_type
    return ActiveRecord::Base.connection.select_all <<~SQL
      SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid
        FROM concept_attribute ca
      INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id
        AND ca2.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
      INNER JOIN concept c ON c.concept_id = ca.concept_id
      WHERE ca.attribute_type_id = #{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}
      AND ca.concept_id IN (#{specimen_types.select(:concept_id).to_sql})
      GROUP BY ca.concept_id
    SQL
  end

  # Retrieve only those specimen types that belong to concept
  # set of the selected test_type
  test_types = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
                         .filter_members(name: test_type&.strip)
                         .select(:concept_id)

  concept_set = ConceptSet.where(
    concept_id: specimen_types.select(:concept_id),
    concept_set: test_types
  )

  return ActiveRecord::Base.connection.select_all <<~SQL
    SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid
      FROM concept_attribute ca
    INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id
      AND ca2.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
    INNER JOIN concept c ON c.concept_id = ca.concept_id
    WHERE ca.attribute_type_id = #{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}
    AND ca.concept_id IN (#{concept_set.pluck(:concept_id).push(0).join(',')})
    GROUP BY ca.concept_id
  SQL
end

.test_methods(nlims_code) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/lab/concepts_service.rb', line 6

def self.test_methods(nlims_code)
  return ActiveRecord::Base.connection.select_all <<~SQL
    SELECT cn.name, cn.concept_id
    FROM concept_set cs
            INNER JOIN concept_name cn ON cn.concept_id = cs.concept_id
            INNER JOIN concept_attribute ca ON ca.value_reference = #{ActiveRecord::Base.connection.quote(nlims_code)}
        AND ca.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
    WHERE cs.concept_id IN (SELECT concept_set.concept_id
                            FROM concept_set
                            WHERE concept_set.concept_set IN (SELECT concept_name.concept_id
                                                              FROM concept_name
                                                              WHERE concept_name.voided = 0
                                                                AND concept_name.name = 'Recommended test method'))
      AND cs.concept_set IN (SELECT concept_set.concept_id
                                      FROM concept_set
                                      WHERE concept_set.concept_set IN (SELECT concept_name.concept_id
                                                                        FROM concept_name
                                                                        WHERE concept_name.voided = 0
                                                                          AND concept_name.name = 'Test type')
                                        )
    AND cs.concept_set = ca.concept_id
    GROUP BY cn.concept_id
  SQL
end

.test_result_indicators(test_type_id) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/services/lab/concepts_service.rb', line 111

def self.test_result_indicators(test_type_id)
  # Verify that the specified test_type is indeed a test_type
  test = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
                   .where(concept_id: Concept.find(test_type_id)&.id)
                   .select(:concept_id)

  # From the members above, filter out only those concepts that are result indicators
  measures = ConceptSet.find_members_by_name(Lab::Metadata::TEST_RESULT_INDICATOR_CONCEPT_NAME)
                       .select(:concept_id)

  sets = ConceptSet.where(concept_set: test, concept_id: measures)

  return ActiveRecord::Base.connection.select_all <<~SQL
    SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid
      FROM concept_attribute ca
      INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id
        AND ca2.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
      INNER JOIN concept c ON c.concept_id = ca.concept_id
      WHERE ca.attribute_type_id = #{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}
      AND ca.concept_id IN (#{sets.pluck(:concept_id).push(0).join(',')})
      GROUP BY ca.concept_id
  SQL
end

.test_types(name: nil, specimen_type: nil) ⇒ Object



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
61
62
63
64
65
66
67
68
69
# File 'app/services/lab/concepts_service.rb', line 31

def self.test_types(name: nil, specimen_type: nil)
  test_types = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)
  test_types = test_types.filter_members(name:) if name

  unless specimen_type
    return ActiveRecord::Base.connection.select_all <<~SQL
      SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid
        FROM concept_attribute ca
      INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id
        AND ca2.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
      INNER JOIN concept c ON c.concept_id = ca.concept_id
      WHERE ca.attribute_type_id = #{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}
      AND ca.concept_id IN (#{test_types.select(:concept_id).to_sql})
      GROUP BY ca.concept_id
    SQL
  end

  # Filter out only those test types that have the specified specimen
  # type.
  specimen_types = ConceptSet.find_members_by_name(Lab::Metadata::SPECIMEN_TYPE_CONCEPT_NAME)
                             .filter_members(name: specimen_type)
                             .select(:concept_id)

  concept_set = ConceptSet.where(
    concept_id: specimen_types,
    concept_set: test_types.select(:concept_id)
  )

  return ActiveRecord::Base.connection.select_all <<~SQL
    SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid
      FROM concept_attribute ca
    INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id
      AND ca2.attribute_type_id = #{ConceptAttributeType.nlims_code.concept_attribute_type_id}
    INNER JOIN concept c ON c.concept_id = ca.concept_id
    WHERE ca.attribute_type_id = #{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}
    AND ca.concept_id IN (#{concept_set.select(:concept_set).to_sql})
    GROUP BY ca.concept_id
  SQL
end