Skip to content

Commit 2c5027e

Browse files
committed
Merge pull request #1074 from josenavas/fix-sample-obj
Fix sample obj
2 parents 6177e4e + 0439de9 commit 2c5027e

File tree

5 files changed

+46
-134
lines changed

5 files changed

+46
-134
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 19 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -194,30 +194,13 @@ def _get_categories(self, conn_handler):
194194
set of str
195195
The set of all available metadata categories
196196
"""
197-
# Get all the required columns
198-
required_cols = get_table_cols(self._table, conn_handler)
199-
# Get all the the columns in the dynamic table
200-
dynamic_cols = get_table_cols(self._dynamic_table, conn_handler)
201-
# Get the union of the two previous lists
202-
cols = set(required_cols).union(dynamic_cols)
203-
# Remove the sample_id column and the study_id/raw_data_id columns,
204-
# as this columns are used internally for data storage and they don't
205-
# actually belong to the metadata
197+
# Get all the columns
198+
cols = get_table_cols(self._dynamic_table, conn_handler)
199+
# Remove the sample_id column as this column is used internally for
200+
# data storage and it doesn't actually belong to the metadata
206201
cols.remove('sample_id')
207-
cols.remove(self._id_column)
208-
try:
209-
# study_id could be potentially removed by _id_column, so wrap
210-
# in a try except
211-
cols.remove('study_id')
212-
except KeyError:
213-
pass
214-
# Change the *_id columns, as this is for convenience internally,
215-
# and add the original categories
216-
for key, value in viewitems(self._md_template.translate_cols_dict):
217-
cols.remove(key)
218-
cols.add(value)
219202

220-
return cols
203+
return set(cols)
221204

222205
def _to_dict(self):
223206
r"""Returns the categories and their values in a dictionary
@@ -229,22 +212,13 @@ def _to_dict(self):
229212
"""
230213
conn_handler = SQLConnectionHandler()
231214
d = dict(conn_handler.execute_fetchone(
232-
"SELECT * FROM qiita.{0} WHERE {1}=%s AND "
233-
"sample_id=%s".format(self._table, self._id_column),
234-
(self._md_template.id, self._id)))
235-
dynamic_d = dict(conn_handler.execute_fetchone(
236215
"SELECT * from qiita.{0} WHERE "
237216
"sample_id=%s".format(self._dynamic_table),
238217
(self._id, )))
239-
d.update(dynamic_d)
218+
219+
# Remove the sample_id, is not part of the metadata
240220
del d['sample_id']
241-
del d[self._id_column]
242-
d.pop('study_id', None)
243221

244-
# Modify all the *_id columns to include the string instead of the id
245-
for k, v in viewitems(self._md_template.translate_cols_dict):
246-
d[v] = self._md_template.str_cols_handlers[k][d[k]]
247-
del d[k]
248222
return d
249223

250224
def __len__(self):
@@ -283,39 +257,17 @@ def __getitem__(self, key):
283257
"""
284258
conn_handler = SQLConnectionHandler()
285259
key = key.lower()
286-
if key in self._get_categories(conn_handler):
287-
# It's possible that the key is asking for one of the *_id columns
288-
# that we have to do the translation
289-
def handler(x):
290-
return x
291-
292-
# prevent flake8 from complaining about the function not being
293-
# used and a redefinition happening in the next few lines
294-
handler(None)
295-
296-
if key in self._md_template.translate_cols_dict.values():
297-
handler = (
298-
lambda x: self._md_template.str_cols_handlers[key][x])
299-
key = "%s_id" % key
300-
# Check if we have either to query the table with required columns
301-
# or the dynamic table
302-
if key in get_table_cols(self._table, conn_handler):
303-
result = conn_handler.execute_fetchone(
304-
"SELECT {0} FROM qiita.{1} WHERE {2}=%s AND "
305-
"sample_id=%s".format(key, self._table, self._id_column),
306-
(self._md_template.id, self._id))[0]
307-
return handler(result)
308-
else:
309-
return conn_handler.execute_fetchone(
310-
"SELECT {0} FROM qiita.{1} WHERE "
311-
"sample_id=%s".format(key, self._dynamic_table),
312-
(self._id, ))[0]
313-
else:
260+
if key not in self._get_categories(conn_handler):
314261
# The key is not available for the sample, so raise a KeyError
315262
raise KeyError("Metadata category %s does not exists for sample %s"
316263
" in template %d" %
317264
(key, self._id, self._md_template.id))
318265

266+
sql = """SELECT {0} FROM qiita.{1}
267+
WHERE sample_id=%s""".format(key, self._dynamic_table)
268+
269+
return conn_handler.execute_fetchone(sql, (self._id, ))[0]
270+
319271
def add_setitem_queries(self, column, value, conn_handler, queue):
320272
"""Adds the SQL queries needed to set a value to the provided queue
321273
@@ -336,40 +288,15 @@ def add_setitem_queries(self, column, value, conn_handler, queue):
336288
QiitaDBColumnError
337289
If the column does not exist in the table
338290
"""
339-
# try dynamic tables
340-
sql = """SELECT EXISTS (
341-
SELECT column_name
342-
FROM information_schema.columns
343-
WHERE table_name=%s
344-
AND table_schema='qiita'
345-
AND column_name=%s)"""
346-
exists_dynamic = conn_handler.execute_fetchone(
347-
sql, (self._dynamic_table, column))[0]
348-
# try required_sample_info
349-
sql = """SELECT EXISTS (
350-
SELECT column_name
351-
FROM information_schema.columns
352-
WHERE table_name=%s
353-
AND table_schema='qiita'
354-
AND column_name=%s)"""
355-
exists_required = conn_handler.execute_fetchone(
356-
sql, (self._table, column))[0]
357-
358-
if exists_dynamic:
359-
sql = """UPDATE qiita.{0}
360-
SET {1}=%s
361-
WHERE sample_id=%s""".format(self._dynamic_table,
362-
column)
363-
elif exists_required:
364-
# here is not required the type check as the required fields have
365-
# an explicit type check
366-
sql = """UPDATE qiita.{0}
367-
SET {1}=%s
368-
WHERE sample_id=%s""".format(self._table, column)
369-
else:
291+
# Check if the column exist in the table
292+
if column not in self._get_categories(conn_handler):
370293
raise QiitaDBColumnError("Column %s does not exist in %s" %
371294
(column, self._dynamic_table))
372295

296+
sql = """UPDATE qiita.{0}
297+
SET {1}=%s
298+
WHERE sample_id=%s""".format(self._dynamic_table, column)
299+
373300
conn_handler.add_to_queue(queue, sql, (value, self._id))
374301

375302
def __setitem__(self, column, value):

qiita_db/metadata_template/prep_template.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
QiitaDBError, QiitaDBExecutionError)
1616
from qiita_db.sql_connection import SQLConnectionHandler
1717
from qiita_db.ontology import Ontology
18-
from qiita_db.util import (get_emp_status, convert_to_id,
18+
from qiita_db.util import (convert_to_id,
1919
convert_from_id, get_mountpoint, infer_status)
2020
from .base_metadata_template import BaseSample, MetadataTemplate
2121
from .util import load_template_to_dataframe
@@ -31,7 +31,7 @@ class PrepSample(BaseSample):
3131
BaseSample
3232
Sample
3333
"""
34-
_table = "common_prep_info"
34+
_table = "prep_template_sample"
3535
_table_prefix = "prep_"
3636
_column_table = "prep_columns"
3737
_id_column = "prep_template_id"
@@ -62,13 +62,11 @@ class PrepTemplate(MetadataTemplate):
6262
MetadataTemplate
6363
SampleTemplate
6464
"""
65-
_table = "common_prep_info"
65+
_table = "prep_template_sample"
6666
_table_prefix = "prep_"
6767
_column_table = "prep_columns"
6868
_id_column = "prep_template_id"
6969
translate_cols_dict = {'emp_status_id': 'emp_status'}
70-
id_cols_handlers = {'emp_status_id': get_emp_status()}
71-
str_cols_handlers = {'emp_status_id': get_emp_status(key='emp_status_id')}
7270
_sample_cls = PrepSample
7371

7472
@classmethod

qiita_db/metadata_template/sample_template.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Sample(BaseSample):
3131
BaseSample
3232
PrepSample
3333
"""
34-
_table = "required_sample_info"
34+
_table = "study_sample"
3535
_table_prefix = "sample_"
3636
_column_table = "study_sample_columns"
3737
_id_column = "study_id"
@@ -62,17 +62,12 @@ class SampleTemplate(MetadataTemplate):
6262
MetadataTemplate
6363
PrepTemplate
6464
"""
65-
_table = "required_sample_info"
65+
_table = "study_sample"
6666
_table_prefix = "sample_"
6767
_column_table = "study_sample_columns"
6868
_id_column = "study_id"
6969
translate_cols_dict = {
7070
'required_sample_info_status_id': 'required_sample_info_status'}
71-
id_cols_handlers = {
72-
'required_sample_info_status_id': get_required_sample_info_status()}
73-
str_cols_handlers = {
74-
'required_sample_info_status_id': get_required_sample_info_status(
75-
key='required_sample_info_status_id')}
7671
_sample_cls = Sample
7772

7873
@staticmethod

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def test_add_setitem_queries_required(self):
7171
'center_name', 'FOO', conn_handler, queue)
7272

7373
obs = conn_handler.queues[queue]
74-
sql = """UPDATE qiita.common_prep_info
75-
SET center_name=%s
76-
WHERE sample_id=%s"""
74+
sql = """UPDATE qiita.prep_1
75+
SET center_name=%s
76+
WHERE sample_id=%s"""
7777
exp = [(sql, ('FOO', '1.SKB8.640193'))]
7878
self.assertEqual(obs, exp)
7979

@@ -87,8 +87,8 @@ def test_add_setitem_queries_dynamic(self):
8787

8888
obs = conn_handler.queues[queue]
8989
sql = """UPDATE qiita.prep_1
90-
SET barcodesequence=%s
91-
WHERE sample_id=%s"""
90+
SET barcodesequence=%s
91+
WHERE sample_id=%s"""
9292
exp = [(sql, ('AAAAAAAAAAAA', '1.SKB8.640193'))]
9393
self.assertEqual(obs, exp)
9494

@@ -262,9 +262,6 @@ def test_setitem(self):
262262
with self.assertRaises(QiitaDBColumnError):
263263
self.tester['column that does not exist'] = 0.3
264264

265-
with self.assertRaises(ValueError):
266-
self.tester['emp_status_id'] = "Error!"
267-
268265
self.assertEqual(self.tester['center_name'], 'ANL')
269266
self.tester['center_name'] = "FOO"
270267
self.assertEqual(self.tester['center_name'], "FOO")

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def setUp(self):
3838
self.sample_template = SampleTemplate(1)
3939
self.sample_id = '1.SKB8.640193'
4040
self.tester = Sample(self.sample_id, self.sample_template)
41-
self.exp_categories = {'physical_location', 'has_physical_specimen',
42-
'has_extracted_data', 'sample_type',
43-
'required_sample_info_status',
41+
self.exp_categories = {'physical_specimen_location',
42+
'physical_specimen_remaining',
43+
'dna_extracted', 'sample_type',
4444
'collection_timestamp', 'host_subject_id',
4545
'description', 'season_environment',
4646
'assigned_from_geo', 'texture', 'taxon_id',
@@ -68,12 +68,12 @@ def test_add_setitem_queries_required(self):
6868
conn_handler.create_queue(queue)
6969

7070
self.tester.add_setitem_queries(
71-
'has_physical_specimen', True, conn_handler, queue)
71+
'physical_specimen_remaining', True, conn_handler, queue)
7272

7373
obs = conn_handler.queues[queue]
74-
sql = """UPDATE qiita.required_sample_info
75-
SET has_physical_specimen=%s
76-
WHERE sample_id=%s"""
74+
sql = """UPDATE qiita.sample_1
75+
SET physical_specimen_remaining=%s
76+
WHERE sample_id=%s"""
7777
exp = [(sql, (True, '1.SKB8.640193'))]
7878
self.assertEqual(obs, exp)
7979

@@ -87,8 +87,8 @@ def test_add_setitem_queries_dynamic(self):
8787

8888
obs = conn_handler.queues[queue]
8989
sql = """UPDATE qiita.sample_1
90-
SET tot_nitro=%s
91-
WHERE sample_id=%s"""
90+
SET tot_nitro=%s
91+
WHERE sample_id=%s"""
9292
exp = [(sql, ('1234.5', '1.SKB8.640193'))]
9393
self.assertEqual(obs, exp)
9494

@@ -144,28 +144,22 @@ def test_get_categories(self):
144144

145145
def test_len(self):
146146
"""Len returns the correct number of categories"""
147-
self.assertEqual(len(self.tester), 30)
147+
self.assertEqual(len(self.tester), 29)
148148

149149
def test_getitem_required(self):
150150
"""Get item returns the correct metadata value from the required table
151151
"""
152-
self.assertEqual(self.tester['physical_location'], 'ANL')
152+
self.assertEqual(self.tester['physical_specimen_location'], 'ANL')
153153
self.assertEqual(self.tester['collection_timestamp'],
154154
datetime(2011, 11, 11, 13, 00, 00))
155-
self.assertTrue(self.tester['has_physical_specimen'])
155+
self.assertTrue(self.tester['dna_extracted'])
156156

157157
def test_getitem_dynamic(self):
158158
"""Get item returns the correct metadata value from the dynamic table
159159
"""
160160
self.assertEqual(self.tester['SEASON_ENVIRONMENT'], 'winter')
161161
self.assertEqual(self.tester['depth'], 0.15)
162162

163-
def test_getitem_id_column(self):
164-
"""Get item returns the correct metadata value from the changed column
165-
"""
166-
self.assertEqual(self.tester['required_sample_info_status'],
167-
'completed')
168-
169163
def test_getitem_error(self):
170164
"""Get item raises an error if category does not exists"""
171165
with self.assertRaises(KeyError):
@@ -196,7 +190,7 @@ def test_values(self):
196190
"""values returns an iterator over the values"""
197191
obs = self.tester.values()
198192
self.assertTrue(isinstance(obs, Iterable))
199-
exp = {'ANL', True, True, 'ENVO:soil', 'completed',
193+
exp = {'ANL', True, True, 'ENVO:soil',
200194
datetime(2011, 11, 11, 13, 00, 00), '1001:M7',
201195
'Cannabis Soil Microbiome', 'winter', 'n',
202196
'64.6 sand, 17.6 silt, 17.8 clay', '1118232', 0.15, '3483',
@@ -211,9 +205,10 @@ def test_items(self):
211205
"""items returns an iterator over the (key, value) tuples"""
212206
obs = self.tester.items()
213207
self.assertTrue(isinstance(obs, Iterable))
214-
exp = {('physical_location', 'ANL'), ('has_physical_specimen', True),
215-
('has_extracted_data', True), ('sample_type', 'ENVO:soil'),
216-
('required_sample_info_status', 'completed'),
208+
exp = {('physical_specimen_location', 'ANL'),
209+
('physical_specimen_remaining', True),
210+
('dna_extracted', True),
211+
('sample_type', 'ENVO:soil'),
217212
('collection_timestamp', datetime(2011, 11, 11, 13, 00, 00)),
218213
('host_subject_id', '1001:M7'),
219214
('description', 'Cannabis Soil Microbiome'),

0 commit comments

Comments
 (0)