Skip to content

Commit fcab341

Browse files
committed
removing conn_handler from def and calls - 2
1 parent 520bcfa commit fcab341

File tree

12 files changed

+29
-81
lines changed

12 files changed

+29
-81
lines changed

qiita_db/analysis.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,9 @@ def build_files(self, rarefaction_depth=None):
655655
if rarefaction_depth <= 0:
656656
raise ValueError("rarefaction_depth must be greater than 0")
657657

658-
conn_handler = SQLConnectionHandler()
659658
samples = self._get_samples()
660659
self._build_mapping_file(samples)
661-
self._build_biom_tables(samples, rarefaction_depth,
662-
conn_handler=conn_handler)
660+
self._build_biom_tables(samples, rarefaction_depth)
663661

664662
def _get_samples(self):
665663
"""Retrieves dict of samples to proc_data_id for the analysis"""
@@ -669,8 +667,7 @@ def _get_samples(self):
669667
"GROUP BY processed_data_id")
670668
return dict(conn_handler.execute_fetchall(sql, [self._id]))
671669

672-
def _build_biom_tables(self, samples, rarefaction_depth,
673-
conn_handler=None):
670+
def _build_biom_tables(self, samples, rarefaction_depth):
674671
"""Build tables and add them to the analysis"""
675672
# filter and combine all study BIOM tables needed for each data type
676673
new_tables = {dt: None for dt in self.data_types}
@@ -700,7 +697,6 @@ def _build_biom_tables(self, samples, rarefaction_depth,
700697
new_tables[data_type] = new_tables[data_type].merge(table)
701698

702699
# add the new tables to the analysis
703-
conn_handler = SQLConnectionHandler()
704700
_, base_fp = get_mountpoint(self._table)[0]
705701
for dt, biom_table in viewitems(new_tables):
706702
# rarefy, if specified
@@ -712,7 +708,7 @@ def _build_biom_tables(self, samples, rarefaction_depth,
712708
biom_table.to_hdf5(f, "Analysis %s Datatype %s" %
713709
(self._id, dt))
714710
self._add_file("%d_analysis_%s.biom" % (self._id, dt),
715-
"biom", data_type=dt, conn_handler=conn_handler)
711+
"biom", data_type=dt)
716712

717713
def _build_mapping_file(self, samples):
718714
"""Builds the combined mapping file for all samples
@@ -773,10 +769,9 @@ def _build_mapping_file(self, samples):
773769
merged_map.to_csv(mapping_fp, index_label='#SampleID',
774770
na_rep='unknown', sep='\t')
775771

776-
self._add_file("%d_analysis_mapping.txt" % self._id,
777-
"plain_text", conn_handler=conn_handler)
772+
self._add_file("%d_analysis_mapping.txt" % self._id, "plain_text")
778773

779-
def _add_file(self, filename, filetype, data_type=None, conn_handler=None):
774+
def _add_file(self, filename, filetype, data_type=None):
780775
"""adds analysis item to database
781776
782777
Parameters
@@ -785,7 +780,6 @@ def _add_file(self, filename, filetype, data_type=None, conn_handler=None):
785780
filename to add to analysis
786781
filetype : {plain_text, biom}
787782
data_type : str, optional
788-
conn_handler : SQLConnectionHandler object, optional
789783
"""
790784
conn_handler = SQLConnectionHandler()
791785

qiita_db/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def status(self, status):
226226
"(SELECT {0}_status_id FROM qiita.{0}_status WHERE status = %s) "
227227
"WHERE {0}_id = %s".format(self._table), (status, self._id))
228228

229-
def check_status(self, status, exclude=False, conn_handler=None):
229+
def check_status(self, status, exclude=False):
230230
r"""Checks status of object.
231231
232232
Parameters
@@ -236,8 +236,6 @@ def check_status(self, status, exclude=False, conn_handler=None):
236236
exclude: bool, optional
237237
If True, will check that database status is NOT one of the statuses
238238
passed. Default False.
239-
conn_handler: SQLConnectionHandler, optional
240-
The connection handler object connected to the DB
241239
242240
Returns
243241
-------

qiita_db/data.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _link_data_filepaths(self, fp_ids, conn_handler):
139139
"VALUES (%s, %s)".format(self._data_filepath_table,
140140
self._data_filepath_column), values)
141141

142-
def add_filepaths(self, filepaths, conn_handler=None):
142+
def add_filepaths(self, filepaths):
143143
r"""Populates the DB tables for storing the filepaths and connects the
144144
`self` objects with these filepaths"""
145145
# Check that this function has been called from a subclass
@@ -326,7 +326,7 @@ def create(cls, filetype, studies, filepaths=None):
326326

327327
# If file paths have been provided, add them to the raw data object
328328
if filepaths:
329-
rd.add_filepaths(filepaths, conn_handler)
329+
rd.add_filepaths(filepaths)
330330

331331
return rd
332332

@@ -465,14 +465,9 @@ def prep_templates(self):
465465
"WHERE raw_data_id = %s ORDER BY prep_template_id")
466466
return [x[0] for x in conn_handler.execute_fetchall(sql, (self._id,))]
467467

468-
def _is_preprocessed(self, conn_handler=None):
468+
def _is_preprocessed(self):
469469
"""Returns whether the RawData has been preprocessed or not
470470
471-
Parameters
472-
----------
473-
conn_handler : SQLConnectionHandler
474-
The connection handler object connected to the DB
475-
476471
Returns
477472
-------
478473
bool
@@ -507,7 +502,7 @@ def _remove_filepath(self, fp, conn_handler, queue):
507502
"""
508503
# If the RawData has been already preprocessed, we cannot remove any
509504
# file - raise an error
510-
if self._is_preprocessed(conn_handler):
505+
if self._is_preprocessed():
511506
msg = ("Cannot clear all the filepaths from raw data %s, it has "
512507
"been already preprocessed" % self._id)
513508
self._set_link_filepaths_status("failed: %s" % msg)
@@ -525,7 +520,7 @@ def _remove_filepath(self, fp, conn_handler, queue):
525520
raise QiitaDBError(msg)
526521

527522
# Get the filpeath id
528-
fp_id = get_filepath_id(self._table, fp, conn_handler)
523+
fp_id = get_filepath_id(self._table, fp)
529524
fp_is_mine = conn_handler.execute_fetchone(
530525
"SELECT EXISTS(SELECT * FROM qiita.{0} WHERE filepath_id=%s AND "
531526
"{1}=%s)".format(self._data_filepath_table,
@@ -803,7 +798,7 @@ def create(cls, study, preprocessed_params_table, preprocessed_params_id,
803798
conn_handler.execute_queue(q)
804799

805800
# Add the filepaths to the database and connect them
806-
ppd.add_filepaths(filepaths, conn_handler)
801+
ppd.add_filepaths(filepaths)
807802
return ppd
808803

809804
@classmethod
@@ -1320,7 +1315,7 @@ def create(cls, processed_params_table, processed_params_id, filepaths,
13201315
"(%s, %s)".format(cls._study_processed_table),
13211316
(study_id, pd_id))
13221317

1323-
pd.add_filepaths(filepaths, conn_handler)
1318+
pd.add_filepaths(filepaths)
13241319
return cls(pd_id)
13251320

13261321
@classmethod

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ def to_dataframe(self):
10251025

10261026
return df
10271027

1028-
def add_filepath(self, filepath, conn_handler=None, fp_id=None):
1028+
def add_filepath(self, filepath, fp_id=None):
10291029
r"""Populates the DB tables for storing the filepath and connects the
10301030
`self` objects with this filepath"""
10311031
# Check that this function has been called from a subclass
@@ -1050,7 +1050,7 @@ def add_filepath(self, filepath, conn_handler=None, fp_id=None):
10501050
info={self.__class__.__name__: self.id})
10511051
raise e
10521052

1053-
def get_filepaths(self, conn_handler=None):
1053+
def get_filepaths(self):
10541054
r"""Retrieves the list of (filepath_id, filepath)"""
10551055
# Check that this function has been called from a subclass
10561056
self._check_subclass()

qiita_db/metadata_template/prep_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def create_qiime_mapping_file(self):
495495

496496
# adding the fp to the object
497497
self.add_filepath(
498-
filepath, conn_handler=conn_handler,
498+
filepath,
499499
fp_id=convert_to_id("qiime_map", "filepath_type"))
500500

501501
return filepath

qiita_db/parameters.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ def _check_id(self, id_):
138138
self._table, self._column_id),
139139
(id_, ))[0]
140140

141-
def _get_values_as_dict(self, conn_handler):
141+
def _get_values_as_dict(self):
142142
r""""""
143+
conn_handler = SQLConnectionHandler()
143144
return dict(conn_handler.execute_fetchone(
144145
"SELECT * FROM qiita.{0} WHERE {1}=%s".format(
145146
self._table, self._column_id), (self.id,)))
@@ -152,11 +153,10 @@ def to_str(self):
152153
str
153154
The string with all the parameters
154155
"""
155-
conn_handler = SQLConnectionHandler()
156156
table_cols = get_table_cols_w_type(self._table)
157157
table_cols.remove([self._column_id, 'bigint'])
158158

159-
values = self._get_values_as_dict(conn_handler=conn_handler)
159+
values = self._get_values_as_dict()
160160

161161
result = []
162162
for p_name, p_type in sorted(table_cols):
@@ -212,8 +212,7 @@ def to_file(self, f):
212212
File-like object to write the parameters. Should support the write
213213
operation
214214
"""
215-
conn_handler = SQLConnectionHandler()
216-
values = self._get_values_as_dict(conn_handler)
215+
values = self._get_values_as_dict()
217216

218217
# Remove the id column
219218
del values[self._column_id]

qiita_db/test/test_analysis.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ def test_retrieve_dropped_samples(self):
237237

238238
samples = {1: ['1.SKB8.640193', '1.SKD8.640184', '1.SKB7.640196'],
239239
2: ['2.SKB8.640193', '2.SKD8.640184']}
240-
self.analysis._build_biom_tables(samples, 10000,
241-
conn_handler=self.conn_handler)
240+
self.analysis._build_biom_tables(samples, 10000)
242241
exp = {1: {'1.SKM4.640180', '1.SKM9.640192'},
243242
2: {'2.SKB7.640196'}}
244243
self.assertEqual(self.analysis.dropped_samples, exp)
@@ -423,8 +422,7 @@ def test_build_mapping_file_duplicate_samples(self):
423422
def test_build_biom_tables(self):
424423
new_id = get_count('qiita.filepath') + 1
425424
samples = {1: ['1.SKB8.640193', '1.SKD8.640184', '1.SKB7.640196']}
426-
self.analysis._build_biom_tables(samples, 100,
427-
conn_handler=self.conn_handler)
425+
self.analysis._build_biom_tables(samples, 100)
428426
obs = self.analysis.biom_tables
429427

430428
self.assertEqual(obs, {'18S': self.biom_fp})

qiita_db/test/test_base.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -87,46 +87,27 @@ def test_status(self):
8787
def test_check_status_single(self):
8888
"""check_status works passing a single status"""
8989
self.assertTrue(self.tester.check_status(["in_construction"]))
90-
self.assertTrue(self.tester.check_status(["in_construction"],
91-
conn_handler=self.conn_handler))
9290
self.assertFalse(self.tester.check_status(["queued"]))
93-
self.assertFalse(self.tester.check_status(["queued"],
94-
conn_handler=self.conn_handler))
9591

9692
def test_check_status_exclude_single(self):
9793
"""check_status works passing a single status and the exclude flag"""
9894
self.assertTrue(self.tester.check_status(["public"], exclude=True))
99-
self.assertTrue(self.tester.check_status(["public"], exclude=True,
100-
conn_handler=self.conn_handler))
10195
self.assertFalse(self.tester.check_status(["in_construction"],
10296
exclude=True))
103-
self.assertFalse(self.tester.check_status(["in_construction"],
104-
exclude=True, conn_handler=self.conn_handler))
10597

10698
def test_check_status_list(self):
10799
"""check_status work passing a list of status"""
108100
self.assertTrue(self.tester.check_status(
109101
["in_construction", "queued"]))
110-
self.assertTrue(self.tester.check_status(
111-
["in_construction", "queued"],
112-
conn_handler=self.conn_handler))
113102
self.assertFalse(self.tester.check_status(
114103
["public", "queued"]))
115-
self.assertFalse(self.tester.check_status(
116-
["public", "queued"], conn_handler=self.conn_handler))
117104

118105
def test_check_status_exclude_list(self):
119106
"""check_status work passing a list of status and the exclude flag"""
120107
self.assertTrue(self.tester.check_status(
121108
["public", "queued"], exclude=True))
122-
self.assertTrue(self.tester.check_status(
123-
["public", "queued"], exclude=True,
124-
conn_handler=self.conn_handler))
125109
self.assertFalse(self.tester.check_status(
126110
["in_construction", "queued"], exclude=True))
127-
self.assertFalse(self.tester.check_status(
128-
["in_construction", "queued"], exclude=True,
129-
conn_handler=self.conn_handler))
130111

131112
def test_check_status_unknown_status(self):
132113
"""check_status raises an error if an invalid status is provided"""
@@ -136,13 +117,6 @@ def test_check_status_unknown_status(self):
136117
with self.assertRaises(ValueError):
137118
self.tester.check_status(["foo"], exclude=True)
138119

139-
with self.assertRaises(ValueError):
140-
self.tester.check_status(["foo"], conn_handler=self.conn_handler)
141-
142-
with self.assertRaises(ValueError):
143-
self.tester.check_status(["foo"], exclude=True,
144-
conn_handler=self.conn_handler)
145-
146120
def test_check_status_unknown_status_list(self):
147121
"""check_status raises an error if an invalid status list is provided
148122
"""
@@ -152,13 +126,5 @@ def test_check_status_unknown_status_list(self):
152126
with self.assertRaises(ValueError):
153127
self.tester.check_status(["foo", "bar"], exclude=True)
154128

155-
with self.assertRaises(ValueError):
156-
self.tester.check_status(["foo", "bar"],
157-
conn_handler=self.conn_handler)
158-
159-
with self.assertRaises(ValueError):
160-
self.tester.check_status(["foo", "bar"], exclude=True,
161-
conn_handler=self.conn_handler)
162-
163129
if __name__ == '__main__':
164130
main()

qiita_db/test/test_study.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ def setUp(self):
175175
def _change_processed_data_status(self, new_status):
176176
# Change the status of the studies by changing the status of their
177177
# processed data
178-
id_status = convert_to_id(new_status, 'processed_data_status',
179-
self.conn_handler)
178+
id_status = convert_to_id(new_status, 'processed_data_status')
180179
self.conn_handler.execute(
181180
"UPDATE qiita.processed_data SET processed_data_status_id = %s",
182181
(id_status,))

qiita_db/test/test_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,12 @@ def test_move_filepaths_to_upload_folder(self):
446446
def test_get_filepath_id(self):
447447
_, base = get_mountpoint("raw_data")[0]
448448
fp = join(base, '1_s_G1_L001_sequences.fastq.gz')
449-
obs = get_filepath_id("raw_data", fp, self.conn_handler)
449+
obs = get_filepath_id("raw_data", fp)
450450
self.assertEqual(obs, 1)
451451

452452
def test_get_filepath_id_error(self):
453453
with self.assertRaises(QiitaDBError):
454-
get_filepath_id("raw_data", "Not_a_path", self.conn_handler)
454+
get_filepath_id("raw_data", "Not_a_path")
455455

456456
def test_get_mountpoint(self):
457457
exp = [(5, join(get_db_files_base_dir(), 'raw_data', ''))]

0 commit comments

Comments
 (0)