Skip to content

Commit c16ef72

Browse files
committed
API: passing PrepTemplate.unique_ids
1 parent 678867e commit c16ef72

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

qiita_db/metadata_template/prep_template.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,50 @@ def delete(cls, id_):
324324

325325
qdb.sql_connection.TRN.execute()
326326

327+
def unique_ids(self):
328+
r"""Return a stable mapping of sample_name to integers
329+
330+
Obtain a map from a sample_name to an integer. The association is
331+
unique Qiita-wide and 1-1.
332+
333+
This method is idempotent.
334+
335+
Returns
336+
------
337+
dict
338+
{sample_name: integer_index}
339+
"""
340+
sample_idx = qdb.study.Study(self.study_id).sample_template.unique_ids()
341+
342+
paired = []
343+
for p_id in sorted(self.keys()):
344+
if p_id in sample_idx:
345+
paired.append([self._id, sample_idx[p_id]])
346+
347+
with qdb.sql_connection.TRN:
348+
# insert any IDs not present
349+
sql = """INSERT INTO map_prep_sample_idx (prep_idx, sample_idx)
350+
VALUES (%s, %s)
351+
ON CONFLICT (prep_idx, sample_idx)
352+
DO NOTHING"""
353+
qdb.sql_connection.TRN.add(sql, paired, many=True)
354+
355+
# obtain the association
356+
sql = """SELECT
357+
sample_name,
358+
prep_sample_idx
359+
FROM map_prep_sample_idx
360+
JOIN map_sample_idx USING (sample_idx)"""
361+
qdb.sql_connection.TRN.add(sql)
362+
363+
# form into a dict
364+
mapping = {r[0]: r[1] for r in qdb.sql_connection.TRN.execute_fetchindex()}
365+
366+
# commit in the event changes were made
367+
qdb.sql_connection.TRN.commit()
368+
369+
return mapping
370+
327371
def data_type(self, ret_id=False):
328372
"""Returns the data_type or the data_type id
329373

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def test_init(self):
432432

433433
def test_unique_ids(self):
434434
obs = self.tester.unique_ids()
435-
exp = {name: idx for idx, name in enumerate(sorted(self.tester.keys()))}
435+
exp = {name: idx for idx, name in enumerate(sorted(self.tester.keys()), 1)}
436436
self.assertEqual(obs, exp)
437437

438438
# verify a repeat call is unchanged

qiita_db/support_files/patches/95.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CREATE TABLE map_prep_sample_idx (
1616
prep_sample_idx BIGINT NOT NULL PRIMARY KEY DEFAULT NEXTVAL('sequence_prep_sample_idx'),
1717
prep_idx BIGINT NOT NULL,
1818
sample_idx BIGINT NOT NULL,
19-
UNIQUE (prep_idx, prep_sample_idx),
19+
CONSTRAINT uc_prep_sample UNIQUE(prep_idx, sample_idx),
2020
CONSTRAINT fk_prep_template FOREIGN KEY (prep_idx) REFERENCES qiita.prep_template (prep_template_id)
2121
);
2222

@@ -25,6 +25,6 @@ CREATE TABLE map_artifact_sample_idx (
2525
artifact_sample_idx BIGINT NOT NULL PRIMARY KEY DEFAULT NEXTVAL('sequence_artifact_sample_idx'),
2626
artifact_idx BIGINT NOT NULL,
2727
prep_sample_idx BIGINT NOT NULL,
28-
UNIQUE (artifact_idx, artifact_sample_idx),
28+
CONSTRAINT uc_artifact_sample UNIQUE(artifact_idx, prep_sample_idx),
2929
CONSTRAINT fk_artifact FOREIGN KEY (artifact_idx) REFERENCES qiita.artifact (artifact_id)
3030
);

0 commit comments

Comments
 (0)