Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

added a function to retrieve the environment type for a barcode #165

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions knimin/lib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ def _get_email(self, config):
self.smtp_user = config.get('email', 'USERNAME')
self.smtp_password = config.get('email', 'PASSWORD')


config = KniminConfig()
1 change: 1 addition & 0 deletions knimin/lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@
def default_blank():
return 'Not applicable'


blanks_values = defaultdict(default_blank,
ASSIGNED_FROM_GEO="No",
COMMON_NAME="unclassified metagenome",
Expand Down
63 changes: 62 additions & 1 deletion knimin/lib/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,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
Expand Down Expand Up @@ -2422,3 +2425,61 @@ def _revert_ready(self, barcodes):
SET results_ready = NULL
WHERE barcode IN %s"""
self._con.execute(sql, [tuple(barcodes)])

def get_barcode_environment_types(self, barcode):
# TODO: write test code once test data have been dumped into DB!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you prefer holding this pr until this is resolved?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on the timing of the DB dump. I don't want to wait for weeks with further development.

""" 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
1 change: 1 addition & 0 deletions knimin/lib/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GoogleAPIRequestDenied(Exception):
class GoogleAPIInvalidRequest(Exception):
pass


Location = namedtuple('Location', ['input', 'lat', 'long', 'elev', 'city',
'state', 'postcode', 'country'])

Expand Down
1 change: 1 addition & 0 deletions knimin/lib/mem_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def write_to_buffer(self):
self.in_memory_zip.close()
return self.in_memory_data.getvalue()


if __name__ == "__main__":
# Run a test
imz = InMemoryZip()
Expand Down
1 change: 1 addition & 0 deletions knimin/lib/string_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ def _convertToCamel(self, snake_cased_str, separator):
camelCasedStr = components[0]
return preffix + camelCasedStr + suffix


converter = Converter()
1 change: 1 addition & 0 deletions knimin/lib/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_get_tornado(self):
config = KniminConfig(self.config_fp)
self.assertEqual(config.http_port, 8888)


test_config = """[main]
debug = True
help_email = help@email.com
Expand Down
3 changes: 2 additions & 1 deletion knimin/lib/tests/test_data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def test_getAGBarcodeDetails(self):
'sample_date': datetime.date(2014, 8, 13),
'date_of_last_email': datetime.date(2014, 8, 15),
'other_text': 'REMOVED',
'site_sampled': 'Stool'}
'site_sampled': 'Stool',
'sample_environments': 'Human'}
self.assertEqual(obs, exp)

def test_get_barcode_info_by_kit_id(self):
Expand Down
2 changes: 2 additions & 0 deletions knimin/lib/tests/test_geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_geocode_bad_address(self):
None)
self.assertEqual(obs, exp)


# Results copied from Google API responses on 2015-10-25
ok = '''{
"results" : [
Expand Down Expand Up @@ -278,5 +279,6 @@ def test_geocode_bad_address(self):
"status" : "INVALID_REQUEST"
}'''


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions knimin/lib/tests/test_mem_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ def test_write_to_buffer(self):
res_contents = zhandle.read(self.test_fname)
self.assertEqual(res_contents, exp_contents)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions knimin/lib/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,6 @@ def test_fetch_url(self):
self.assertTrue(isinstance(fetch_url('http://www.google.com'),
StringIO))


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions knimin/templates/barcode_util.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ <h2>{{parent_project}} Details</h2>
<li>
Sample details: <br/>
<table>
<tr><td>Sample Environment</td><td>{{proj_barcode_info['sample_environments']}}</td></tr>
<tr><td>Sample Date</td><td>{{proj_barcode_info['sample_date']}}</td></tr>
<tr><td>Sample Time</td><td>{{proj_barcode_info['sample_time']}}</td></tr>
<tr><td>Sample Site</td><td>{{proj_barcode_info['site_sampled']}}</td></tr>
Expand Down
1 change: 1 addition & 0 deletions knimin/tests/test_ag_new_barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,6 @@ def test_post(self):
(num_barcodes, ", ".join(projects + [newProject])),
response.body)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions knimin/tests/test_ag_pulldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ def test_post(self):
self.assertIn('failures.txt', response.body)
self.assertIn('survey_1_md.txt', response.body)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions knimin/tests/test_ag_results_ready.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ def test_post(self):
self.assertEqual(response.code, 200)
self.assertIn('ERROR: No barcode results available', response.body)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions knimin/tests/test_ag_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ def test_post_barcode(self):
else:
self.assertIn(sample[field], response.body)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions knimin/tests/test_ag_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ def test_stats_page(self):
stat = '' if stat is None else stat
self.assertIn('%s</td><td>%s' % (item, stat), response.body)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions knimin/tests/test_auth_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ def test_get(self):
self.assertTrue(response.effective_url.endswith(
'/login/?next=%2Fprojects%2Fsummary%2F'))


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion knimin/tests/test_barcode_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ def test_get_ag_details_SJ(self):
'email_type': '1', 'name': 'REMOVED',
'sample_time': time(6, 50),
'notes': 'REMOVED',
'email': 'REMOVED'})
'email': 'REMOVED',
'sample_environments': 'Human'})

# check that None values are set to ''
barcode = '000016744'
Expand Down
1 change: 1 addition & 0 deletions knimin/tests/test_consent_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ def test_post_no_consent(self):
self.assertEqual(response.code, 200)
self.assertIn('000004126</td><td style="color:red">', response.body)


if __name__ == '__main__':
main()