Skip to content

Commit 678867e

Browse files
committed
API: passing SampleTemplate.unique_ids
1 parent d6092cb commit 678867e

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,6 @@ class MetadataTemplate(qdb.base.QiitaObject):
470470
# forbidden_words not defined for base class. Please redefine for
471471
# sub-classes.
472472
_forbidden_words = {}
473-
# qiita-unique integer identifier mapping table
474-
_id_map_table = None
475473

476474
@classmethod
477475
def _check_id(cls, id_):

qiita_db/metadata_template/sample_template.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,43 @@ def columns_restrictions(self):
172172
"""
173173
return qdb.metadata_template.constants.SAMPLE_TEMPLATE_COLUMNS
174174

175+
def unique_ids(self):
176+
r"""Return a stable mapping of sample_name to integers
177+
178+
Obtain a map from a sample_name to an integer. The association is
179+
unique Qiita-wide and 1-1.
180+
181+
This method is idempotent.
182+
183+
Returns
184+
------
185+
dict
186+
{sample_name: integer_index}
187+
"""
188+
samples = [[self._id, s_id] for s_id in sorted(self.keys())]
189+
with qdb.sql_connection.TRN:
190+
# insert any IDs not present
191+
sql = """INSERT INTO map_sample_idx (study_idx, sample_name)
192+
VALUES (%s, %s)
193+
ON CONFLICT (sample_name)
194+
DO NOTHING"""
195+
qdb.sql_connection.TRN.add(sql, samples, many=True)
196+
197+
# obtain the association
198+
sql = """SELECT
199+
sample_name,
200+
sample_idx
201+
FROM map_sample_idx"""
202+
qdb.sql_connection.TRN.add(sql)
203+
204+
# form into a dict
205+
mapping = {r[0]: r[1] for r in qdb.sql_connection.TRN.execute_fetchindex()}
206+
207+
# commit in the event changes were made
208+
qdb.sql_connection.TRN.commit()
209+
210+
return mapping
211+
175212
def delete_samples(self, sample_names):
176213
"""Delete `sample_names` from sample information file
177214

qiita_db/metadata_template/test/test_sample_template.py

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

501501
def test_unique_ids(self):
502502
obs = self.tester.unique_ids()
503-
exp = {name: idx for idx, name in enumerate(sorted(self.tester.keys()))}
503+
exp = {name: idx for idx, name in enumerate(sorted(self.tester.keys()), 1)}
504504
self.assertEqual(obs, exp)
505505

506506
# verify a repeat call is unchanged

qiita_db/support_files/patches/95.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ CREATE TABLE map_sample_idx (
77
sample_name VARCHAR NOT NULL PRIMARY KEY,
88
study_idx BIGINT NOT NULL,
99
sample_idx BIGINT DEFAULT NEXTVAL('sequence_sample_idx') NOT NULL,
10-
UNIQUE (study_idx, sample_idx),
1110
UNIQUE (sample_idx),
1211
CONSTRAINT fk_study FOREIGN KEY (study_idx) REFERENCES qiita.study (study_id)
1312
);

0 commit comments

Comments
 (0)