diff --git a/knimin/lib/data_access.py b/knimin/lib/data_access.py index 55f6f48..3df1fb3 100644 --- a/knimin/lib/data_access.py +++ b/knimin/lib/data_access.py @@ -2214,7 +2214,10 @@ def getAGBarcodeDetails(self, barcode): if not results: return {} else: - return dict(results) + res = dict(results) + res['sample_environments'] = \ + ",".join(self.get_barcode_environment_types(barcode)) + return res def get_barcode_info_by_kit_id(self, ag_kit_id): sql = """SELECT DISTINCT cast(ag_kit_barcode_id as varchar(100)) as @@ -2437,6 +2440,63 @@ def _revert_ready(self, barcodes): WHERE barcode IN %s""" self._con.execute(sql, [tuple(barcodes)]) + def get_barcode_environment_types(self, barcode): + """ Returns the environments of the given barcode. + + Parameters + ---------- + barcode : str + A single AG barcode ID + + Returns + ------- + list of environments, i.e. survey types that have been associated + with this barcode. + + Raises + ------ + ValueError + given barcode not in DB.""" + + environments = [] + + # check if the barcode exists in the DB at all + sql = """SELECT barcode + FROM ag.ag_kit_barcodes + WHERE barcode = %s""" + res = self._con.execute_fetchone(sql, [barcode]) + if res is None: + raise ValueError("Barcode '%s' not in DB." % barcode) + + sql = """SELECT DISTINCT ags.survey_id + FROM ag.ag_kit_barcodes + JOIN ag.survey_answers USING (survey_id) + JOIN ag.group_questions USING (survey_question_id) + JOIN ag.surveys ags USING (survey_group) + WHERE barcode = %s""" + res = self._con.execute_fetchall(sql, [barcode]) + if len(res) > 0: + if res[0] == [1]: + environments.append('Human') + elif res[0] == [2]: + # find out what species it is + sql = """SELECT response + FROM ag.ag_kit_barcodes + JOIN ag.survey_answers USING (survey_id) + WHERE barcode = %s AND survey_question_id = 128""" + res = self._con.execute_fetchone(sql, [barcode]) + environments.append('Animal(%s)' % res[0]) + + sql = """SELECT environment_sampled + FROM ag.ag_kit_barcodes + WHERE barcode = %s""" + res = self._con.execute_fetchall(sql, [barcode]) + + if (len(res) > 0) and (res[0][0] is not None): + environments.append('Environment(%s)' % res[0][0]) + + return environments + def ut_remove_external_survey(self, name, description, url): """ Remove an external survey from DB. For unit testing only! diff --git a/knimin/lib/tests/test_data_access.py b/knimin/lib/tests/test_data_access.py index 5f4a73e..0061941 100644 --- a/knimin/lib/tests/test_data_access.py +++ b/knimin/lib/tests/test_data_access.py @@ -329,6 +329,43 @@ def test_get_ag_barcode_details(self): self.assertEqual({k: obs[key][k] for k in exp[key]}, exp[key]) self.assertIn(obs[key]['participant_name'], participant_names) + def test_get_barcode_environment_types(self): + # check that error is raised, if barcode is not in database + barcode = '9notInDB1' + with self.assertRaises(ValueError) as e: + db.get_barcode_environment_types(barcode) + self.assertEqual(e.exception.message, + u"Barcode '%s' not in DB." % barcode) + + # barcode that is not assigned to a survey, result should be the empty + # list + barcode = '000027834' + exp = [] + obs = db.get_barcode_environment_types(barcode) + self.assertEqual(obs, exp) + + # environmental barcode + barcode = '000015774' + exp = [u'Environment(Sole of shoe)'] + obs = db.get_barcode_environment_types(barcode) + self.assertEqual(obs, exp) + + # human barcode + barcode = '000004216' + exp = [u'Human'] + obs = db.get_barcode_environment_types(barcode) + self.assertEqual(obs, exp) + + # animal barcodes + barcode = '000002012' + exp = [u'Human'] + obs = db.get_barcode_environment_types(barcode) + self.assertEqual(obs, exp) + barcode = '000013439' + exp = [u'Animal(Large Mammal)'] + obs = db.get_barcode_environment_types(barcode) + self.assertEqual(obs, exp) + if __name__ == "__main__": main() diff --git a/knimin/lib/tests/test_geocoder.py b/knimin/lib/tests/test_geocoder.py index 331633e..5a4e075 100644 --- a/knimin/lib/tests/test_geocoder.py +++ b/knimin/lib/tests/test_geocoder.py @@ -281,5 +281,6 @@ def test_geocode_bad_address(self): "status" : "INVALID_REQUEST" }''' + if __name__ == '__main__': main() diff --git a/knimin/templates/barcode_util.html b/knimin/templates/barcode_util.html index b2fe069..f3ff1d5 100644 --- a/knimin/templates/barcode_util.html +++ b/knimin/templates/barcode_util.html @@ -144,6 +144,7 @@
Sample Environment | {{proj_barcode_info['sample_environments']}} |
Sample Date | {{proj_barcode_info['sample_date']}} |
Sample Time | {{proj_barcode_info['sample_time']}} |
Sample Site | {{proj_barcode_info['site_sampled']}} |