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

Remove labcontrol stub and push AG metadata #256

Merged
merged 18 commits into from
Aug 27, 2019
2 changes: 2 additions & 0 deletions knimin/config.txt.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ help_email =
base_data_dir = ./knimin/tests/data
# Path to the logging directory
BASE_LOG_DIR = /tmp
ATTEMPT_GEOCODE = False

[postgres]
USER = postgres
Expand Down Expand Up @@ -42,3 +43,4 @@ QIITA_CLIENT_ID = test
QIITA_CLIENT_SECRET = test
QIITA_CERT = test
QIITA_STUDY_ID = 1

354 changes: 347 additions & 7 deletions knimin/handlers/barcode_util.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions knimin/lib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def _get_main(self, config):
self.help_email = config.get('main', 'help_email')
self.base_data_dir = config.get('main', 'base_data_dir')
self.base_log_dir = config.get('main', 'BASE_LOG_DIR')
self.attempt_geocode = config.getboolean('main', 'ATTEMPT_GEOCODE')

def _get_postgres(self, config):
"""Get the configuration of the postgres section"""
Expand Down
1 change: 0 additions & 1 deletion knimin/lib/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,6 @@ def pulldown(self, barcodes, blanks=None, external=None, # noqa
if len(all_survey_info) > 0:
all_results, errors = self.format_survey_data(all_survey_info,
external, full)

# Do the pulldown for the environmental samples
sql = """SELECT barcode, environment_sampled
FROM ag.ag_kit_barcodes
Expand Down
4 changes: 4 additions & 0 deletions knimin/lib/geocoder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple
import requests
from geopy.geocoders import Nominatim
from knimin.lib.configuration import config


geolocator = Nominatim(user_agent='biocore/labadmin')
Expand Down Expand Up @@ -32,6 +33,9 @@ def elevation(lat, lng):


def geocode(address):
if not config.attempt_geocode:
return Location(address, None, None, None, None, None, None, None)

location = geolocator.geocode(address, addressdetails=True,
timeout=30, language='en')

Expand Down
3 changes: 2 additions & 1 deletion knimin/lib/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_init(self):

# test that expection is raised if not all sections are specified
config = tempfile.NamedTemporaryFile()
config.write(test_config[:100])
config.write(test_config[:122])
config.seek(0)
config_fp = config.name
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -53,6 +53,7 @@ def test_get_tornado(self):
help_email = help@email.com
base_data_dir = /some/dir/path
BASE_LOG_DIR = /tmp
ATTEMPT_GEOCODE = False

[postgres]
user = test
Expand Down
7 changes: 6 additions & 1 deletion knimin/lib/tests/test_geocoder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase, main
from knimin.lib.geocoder import Location, geocode
import random
from knimin.lib.configuration import config
import string


Expand All @@ -9,8 +10,12 @@ def test_geocode_nonmock(self):
obs = geocode('9500 Gilman Dr, La Jolla, CA')
exp = Location('9500 Gilman Dr, La Jolla, CA', 32.8794239,
-117.2369135, 105, 'San Diego', 'California',
'92161', 'USA')
'92093', 'USA')
self.assertEqual(obs.input, exp.input)
if not config.attempt_geocode:
# if we're not attempting to geocode then skip this
return

self.assertAlmostEqual(obs.lat, exp.lat, delta=0.1)
self.assertAlmostEqual(obs.long, exp.long, delta=0.1)
# self.assertIsInstance(obs.elev, int)
Expand Down
57 changes: 56 additions & 1 deletion knimin/tests/test_barcode_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from string import ascii_letters
from datetime import date, time
import os
import StringIO

import pandas as pd
from tornado.escape import url_escape, xhtml_escape, json_decode

from knimin import db
from knimin.tests.tornado_test_base import TestHandlerBase
from knimin.handlers.barcode_util import BarcodeUtilHelper, get_qiita_client
from knimin.handlers.barcode_util import BarcodeUtilHelper, get_qiita_client, \
align_with_qiita_categories, AG_DEBUG_OBSERVED_CATEGORIES


class TestQiitaPush(TestHandlerBase):
Expand Down Expand Up @@ -59,13 +62,65 @@ def test_post_has_barcodes(self):
self.assertIn('000004215', exp)
self.post(r"/notify-qiita/", data={'foo': 'bar'})

# cannot find another way to force the async call to
# actually behave as synchronous here.
try:
self.wait()
except AssertionError:
pass

obs = db._con.execute_fetchall("""SELECT barcode
FROM barcodes.project_qiita_buffer
WHERE pushed_to_qiita='Y'""")
obs = [i[0] for i in obs]
self.assertIn('000004216', obs)
self.assertIn('000004215', obs)

def test_align_with_qiita_categories(self):
samples = ['000004216', '000017291', '000004215']

# apparently the call to pulldown is not idempotent
# the first call is != to the second, but the second
# is equal to the third.
db.pulldown(samples)
data = db.pulldown(samples)

data_as_pd = pd.read_csv(StringIO.StringIO(data[0][1]), sep='\t',
dtype=str)
data_as_pd.set_index('sample_name', inplace=True)
data_as_pd.columns = [c.lower() for c in data_as_pd.columns]

# as of 15august2019, 000017291 does not successfully pulldown. this
# sample has an inconsistency in the metadata that triggers a failure
# condition. This test SHOULD fail when metadata pulldown is
# successfully revisited.
self.assertFalse('000017291' in data_as_pd.index)
nc = len(data_as_pd.columns)
data_as_pd = data_as_pd.append(pd.Series(['pulldown-issue'] * nc,
index=data_as_pd.columns,
name='000017291'))

# per a request from Gail
data_as_pd.loc['000017291', 'env_package'] = 'Air'

for c in set(AG_DEBUG_OBSERVED_CATEGORIES) - set(data_as_pd.columns):
data_as_pd[c] = 'Missing: Not provided'

exp = {'000004216': data_as_pd.loc['000004216'].to_dict(),
'000017291': data_as_pd.loc['000017291'].to_dict(),
'000004215': data_as_pd.loc['000004215'].to_dict()}

obs = align_with_qiita_categories(samples,
AG_DEBUG_OBSERVED_CATEGORIES)

# for an undetermined reason, simply testing equality on the obs
# and exp dicts is very time consuming.
self.assertEqual(sorted(obs.keys()), sorted(exp.keys()))
for k in obs.keys():
o_items = sorted(obs[k].items())
e_items = sorted(exp[k].items())
self.assertEqual(o_items, e_items)

def test_post_no_barcodes(self):
self.mock_login()
db.alter_access_levels('test', [3])
Expand Down