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



140
141
142
143
144
145
# File 'app/services/lab/concepts_service.rb', line 140

def self.reasons_for_test
  ConceptSet.find_members_by_name(Lab::::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::::SPECIMEN_TYPE_CONCEPT_NAME)
  specimen_types = specimen_types.filter_members(name: name) if name

  unless test_type
    return ActiveRecord::Base.connection.select_all "      SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid\n        FROM concept_attribute ca\n      INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id\n        AND ca2.attribute_type_id = \#{ConceptAttributeType.nlims_code.concept_attribute_type_id}\n      INNER JOIN concept c ON c.concept_id = ca.concept_id\n      WHERE ca.attribute_type_id = \#{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}\n      AND ca.concept_id IN (\#{specimen_types.select(:concept_id).to_sql})\n      GROUP BY ca.concept_id\n    SQL\n  end\n\n  # Retrieve only those specimen types that belong to concept\n  # set of the selected test_type\n  test_types = ConceptSet.find_members_by_name(Lab::Metadata::TEST_TYPE_CONCEPT_NAME)\n                         .filter_members(name: test_type&.strip)\n                         .select(:concept_id)\n\n  concept_set = ConceptSet.where(\n    concept_id: specimen_types.select(:concept_id),\n    concept_set: test_types\n  )\n\n  return ActiveRecord::Base.connection.select_all <<~SQL\n    SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid\n      FROM concept_attribute ca\n    INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id\n      AND ca2.attribute_type_id = \#{ConceptAttributeType.nlims_code.concept_attribute_type_id}\n    INNER JOIN concept c ON c.concept_id = ca.concept_id\n    WHERE ca.attribute_type_id = \#{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}\n    AND ca.concept_id IN (\#{concept_set.pluck(:concept_id).push(0).join(',')})\n    GROUP BY ca.concept_id\n  SQL\nend\n"

.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 "    SELECT cn.name, cn.concept_id\n    FROM concept_set cs\n            INNER JOIN concept_name cn ON cn.concept_id = cs.concept_id\n            INNER JOIN concept_attribute ca ON ca.value_reference = \#{ActiveRecord::Base.connection.quote(nlims_code)}\n        AND ca.attribute_type_id = \#{ConceptAttributeType.nlims_code.concept_attribute_type_id}\n    WHERE cs.concept_id IN (SELECT concept_set.concept_id\n                            FROM concept_set\n                            WHERE concept_set.concept_set IN (SELECT concept_name.concept_id\n                                                              FROM concept_name\n                                                              WHERE concept_name.voided = 0\n                                                                AND concept_name.name = 'Recommended test method'))\n      AND cs.concept_set IN (SELECT concept_set.concept_id\n                                      FROM concept_set\n                                      WHERE concept_set.concept_set IN (SELECT concept_name.concept_id\n                                                                        FROM concept_name\n                                                                        WHERE concept_name.voided = 0\n                                                                          AND concept_name.name = 'Test type')\n                                        )\n    AND cs.concept_set = ca.concept_id\n    GROUP BY cn.concept_id\n  SQL\nend\n"

.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
134
135
136
137
138
# 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::::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::::TEST_RESULT_INDICATOR_CONCEPT_NAME)
                       .select(:concept_id)
        # Get specimen types to exclude from results
  specimen_types = ConceptSet.find_members_by_name(Lab::::SPECIMEN_TYPE_CONCEPT_NAME)
                             .select(:concept_id)

  # Find concepts that are linked to the test type AND are measures (but NOT specimens)
  sets = ConceptSet.where(concept_set: test, concept_id: measures)
                   .where.not(concept_id: specimen_types)

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

.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::::TEST_TYPE_CONCEPT_NAME)
  test_types = test_types.filter_members(name:) if name

  unless specimen_type
    return ActiveRecord::Base.connection.select_all "      SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid\n        FROM concept_attribute ca\n      INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id\n        AND ca2.attribute_type_id = \#{ConceptAttributeType.nlims_code.concept_attribute_type_id}\n      INNER JOIN concept c ON c.concept_id = ca.concept_id\n      WHERE ca.attribute_type_id = \#{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}\n      AND ca.concept_id IN (\#{test_types.select(:concept_id).to_sql})\n      GROUP BY ca.concept_id\n    SQL\n  end\n\n  # Filter out only those test types that have the specified specimen\n  # type.\n  specimen_types = ConceptSet.find_members_by_name(Lab::Metadata::SPECIMEN_TYPE_CONCEPT_NAME)\n                             .filter_members(name: specimen_type)\n                             .select(:concept_id)\n\n  concept_set = ConceptSet.where(\n    concept_id: specimen_types,\n    concept_set: test_types.select(:concept_id)\n  )\n\n  return ActiveRecord::Base.connection.select_all <<~SQL\n    SELECT ca.concept_id, ca.value_reference as name, ca2.value_reference as nlims_code, c.uuid\n      FROM concept_attribute ca\n    INNER JOIN concept_attribute ca2 ON ca.concept_id = ca2.concept_id\n      AND ca2.attribute_type_id = \#{ConceptAttributeType.nlims_code.concept_attribute_type_id}\n    INNER JOIN concept c ON c.concept_id = ca.concept_id\n    WHERE ca.attribute_type_id = \#{ConceptAttributeType.test_catalogue_name.concept_attribute_type_id}\n    AND ca.concept_id IN (\#{concept_set.select(:concept_set).to_sql})\n    GROUP BY ca.concept_id\n  SQL\nend\n"