Skip to content

Commit 47aac33

Browse files
committed
reducing code duplication
1 parent d3fedea commit 47aac33

File tree

3 files changed

+68
-80
lines changed

3 files changed

+68
-80
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
QiitaDBDuplicateHeaderError)
2323
from qiita_db.base import QiitaObject
2424
from qiita_db.sql_connection import SQLConnectionHandler
25-
from qiita_db.util import (exists_table, get_table_cols, convert_to_id,
26-
get_mountpoint, insert_filepaths)
25+
from qiita_db.util import (exists_table, get_table_cols, get_mountpoint,
26+
insert_filepaths)
2727
from qiita_db.logger import LogEntry
2828
from .util import get_invalid_sample_names, prefix_sample_names_with_id
2929

@@ -425,6 +425,7 @@ class MetadataTemplate(QiitaObject):
425425
_id_column = None
426426
_sample_cls = None
427427
_filepath_table = None
428+
_filepath_type = None
428429

429430
def _check_id(self, id_, conn_handler=None):
430431
r"""Checks that the MetadataTemplate id_ exists on the database"""
@@ -839,28 +840,19 @@ def add_filepath(self, filepath, conn_handler=None):
839840
# one if not.
840841
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
841842

842-
if self._table == 'required_sample_info':
843-
fp_id = convert_to_id("sample_template", "filepath_type",
844-
conn_handler)
845-
table = 'sample_template_filepath'
846-
column = 'study_id'
847-
elif self._table == 'common_prep_info':
848-
fp_id = convert_to_id("prep_template", "filepath_type",
849-
conn_handler)
850-
table = 'prep_template_filepath'
851-
column = 'prep_template_id'
852-
else:
853-
raise QiitaDBNotImplementedError(
854-
'add_filepath for %s' % self._table)
855-
856843
try:
857-
fpp_id = insert_filepaths([(filepath, fp_id)], None, "templates",
858-
"filepath", conn_handler,
844+
fpp_id = insert_filepaths([(filepath, self._filepath_type)], None,
845+
"templates", "filepath", conn_handler,
859846
move_files=False)[0]
860847
values = (self._id, fpp_id)
861-
conn_handler.execute(
862-
"INSERT INTO qiita.{0} ({1}, filepath_id) "
863-
"VALUES (%s, %s)".format(table, column), values)
848+
sql = """INSERT INTO qiita.{0} ({1}, filepath_id)
849+
VALUES (%s, %s)""".format(self._filepath_table,
850+
self._id_column)
851+
# If this call fails, filepaths will have been added to the DB,
852+
# but they're not linked to anything. However, this is not a
853+
# problem, as any subsequent call to purge_filepaths will remove
854+
# those filepaths
855+
conn_handler.execute(sql, values)
864856
except Exception as e:
865857
LogEntry.create('Runtime', str(e),
866858
info={self.__class__.__name__: self.id})
@@ -875,22 +867,16 @@ def get_filepaths(self, conn_handler=None):
875867
# one if not.
876868
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
877869

878-
if self._table == 'required_sample_info':
879-
table = 'sample_template_filepath'
880-
column = 'study_id'
881-
elif self._table == 'common_prep_info':
882-
table = 'prep_template_filepath'
883-
column = 'prep_template_id'
884-
else:
885-
raise QiitaDBNotImplementedError(
886-
'get_filepath for %s' % self._table)
887-
870+
sql = """SELECT filepath_id, filepath
871+
FROM qiita.filepath
872+
WHERE filepath_id IN (
873+
SELECT filepath_id
874+
FROM qiita.{0}
875+
WHERE {1}=%s)
876+
ORDER BY filepath_id DESC""".format(self._filepath_table,
877+
self._id_column)
888878
try:
889-
filepath_ids = conn_handler.execute_fetchall(
890-
"SELECT filepath_id, filepath FROM qiita.filepath WHERE "
891-
"filepath_id IN (SELECT filepath_id FROM qiita.{0} WHERE "
892-
"{1}=%s) ORDER BY filepath_id DESC".format(table, column),
893-
(self.id, ))
879+
filepath_ids = conn_handler.execute_fetchall(sql, (self.id, ))
894880
except Exception as e:
895881
LogEntry.create('Runtime', str(e),
896882
info={self.__class__.__name__: self.id})

qiita_db/metadata_template/prep_template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class PrepTemplate(MetadataTemplate):
7373
_id_column = "prep_template_id"
7474
_sample_cls = PrepSample
7575
_filepath_table = "prep_template_filepath"
76+
_filepath_type = convert_to_id("sample_template", "filepath_type")
7677

7778
@classmethod
7879
def create(cls, md_template, raw_data, study, data_type,

qiita_db/metadata_template/sample_template.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
QiitaDBUnknownIDError, QiitaDBError,
2222
QiitaDBWarning, QiitaDBExecutionError)
2323
from qiita_db.sql_connection import SQLConnectionHandler
24-
from qiita_db.util import get_mountpoint, scrub_data
24+
from qiita_db.util import get_mountpoint, scrub_data, convert_to_id
2525
from qiita_db.study import Study
2626
from qiita_db.data import RawData
2727
from .base_metadata_template import BaseSample, MetadataTemplate
@@ -75,49 +75,7 @@ class SampleTemplate(MetadataTemplate):
7575
_id_column = "study_id"
7676
_sample_cls = Sample
7777
_filepath_table = "sample_template_filepath"
78-
79-
@classmethod
80-
def _delete_checks(cls, id_, conn_handler=None):
81-
r"""Performs the checks to know if a SampleTemplate can be deleted
82-
83-
A sample template cannot be removed if there is a prep template
84-
referencing it.
85-
86-
Parameters
87-
----------
88-
id_ : int
89-
The sample template identifier
90-
conn_handler : SQLConnectionHandler, optional
91-
The connection handler connected to the DB
92-
93-
Raises
94-
------
95-
QiitaDBExecutionError
96-
If the sample template cannot be removed
97-
"""
98-
sql = """SELECT EXISTS(
99-
SELECT *
100-
FROM qiita.prep_template
101-
JOIN qiita.study_raw_data USING (raw_data_id)
102-
WHERE study_id=%s)"""
103-
exists = conn_handler.execute_fetchone(sql, (id_,))[0]
104-
if exists:
105-
raise QiitaDBExecutionError(
106-
"Cannot remove sample template %d because a prep template "
107-
"referencing to it has been already added." % id_)
108-
109-
@classmethod
110-
def _check_template_special_columns(cls, md_template, study_id):
111-
r"""Checks for special columns based on obj type
112-
113-
Parameters
114-
----------
115-
md_template : DataFrame
116-
The metadata template file contents indexed by sample ids
117-
study_id : int
118-
The study to which the sample template belongs to.
119-
"""
120-
return set()
78+
_filepath_type = convert_to_id("sample_template", "filepath_type")
12179

12280
@classmethod
12381
def create(cls, md_template, study):
@@ -196,6 +154,49 @@ def create(cls, md_template, study):
196154

197155
return st
198156

157+
@classmethod
158+
def _delete_checks(cls, id_, conn_handler=None):
159+
r"""Performs the checks to know if a SampleTemplate can be deleted
160+
161+
A sample template cannot be removed if there is a prep template
162+
referencing it.
163+
164+
Parameters
165+
----------
166+
id_ : int
167+
The sample template identifier
168+
conn_handler : SQLConnectionHandler, optional
169+
The connection handler connected to the DB
170+
171+
Raises
172+
------
173+
QiitaDBExecutionError
174+
If the sample template cannot be removed
175+
"""
176+
sql = """SELECT EXISTS(
177+
SELECT *
178+
FROM qiita.prep_template
179+
JOIN qiita.study_raw_data USING (raw_data_id)
180+
WHERE study_id=%s)"""
181+
exists = conn_handler.execute_fetchone(sql, (id_,))[0]
182+
if exists:
183+
raise QiitaDBExecutionError(
184+
"Cannot remove sample template %d because a prep template "
185+
"referencing to it has been already added." % id_)
186+
187+
@classmethod
188+
def _check_template_special_columns(cls, md_template, study_id):
189+
r"""Checks for special columns based on obj type
190+
191+
Parameters
192+
----------
193+
md_template : DataFrame
194+
The metadata template file contents indexed by sample ids
195+
study_id : int
196+
The study to which the sample template belongs to.
197+
"""
198+
return set()
199+
199200
@property
200201
def study_id(self):
201202
"""Gets the study id with which this sample template is associated

0 commit comments

Comments
 (0)