Skip to content

Commit 9a75c2e

Browse files
committed
Merge pull request #1062 from josenavas/generate-files
Creating generate_files function
2 parents 39d15d2 + 93b8d99 commit 9a75c2e

File tree

5 files changed

+65
-48
lines changed

5 files changed

+65
-48
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,17 @@ def _transform_to_dict(self, values):
989989

990990
return result
991991

992+
def generate_files(self):
993+
r"""Generates all the files that contain data from this template
994+
995+
Raises
996+
------
997+
QiitaDBNotImplementedError
998+
This method should be implemented by the subclasses
999+
"""
1000+
raise QiitaDBNotImplementedError(
1001+
"generate_files should be implemented in the subclass!")
1002+
9921003
def to_file(self, fp, samples=None):
9931004
r"""Writes the MetadataTemplate to the file `fp` in tab-delimited
9941005
format

qiita_db/metadata_template/prep_template.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,8 @@ def create(cls, md_template, raw_data, study, data_type,
157157
# some other error we haven't seen before so raise it
158158
raise
159159

160-
# figuring out the filepath of the backup
161-
_id, fp = get_mountpoint('templates')[0]
162-
fp = join(fp, '%d_prep_%d_%s.txt' % (study.id, prep_id,
163-
strftime("%Y%m%d-%H%M%S")))
164-
# storing the backup
165160
pt = cls(prep_id)
166-
pt.to_file(fp)
167-
168-
# adding the fp to the object
169-
pt.add_filepath(fp)
170-
171-
# creating QIIME mapping file
172-
pt.create_qiime_mapping_file(fp)
161+
pt.generate_files()
173162

174163
return pt
175164

@@ -411,6 +400,22 @@ def study_id(self):
411400
raise QiitaDBError("No studies found associated with prep "
412401
"template ID %d" % self._id)
413402

403+
def generate_files(self):
404+
r"""Generates all the files that contain data from this template
405+
"""
406+
# figuring out the filepath of the prep template
407+
_id, fp = get_mountpoint('templates')[0]
408+
fp = join(fp, '%d_prep_%d_%s.txt' % (self.study_id, self._id,
409+
strftime("%Y%m%d-%H%M%S")))
410+
# storing the template
411+
self.to_file(fp)
412+
413+
# adding the fp to the object
414+
self.add_filepath(fp)
415+
416+
# creating QIIME mapping file
417+
self.create_qiime_mapping_file(fp)
418+
414419
def create_qiime_mapping_file(self, prep_template_fp):
415420
"""This creates the QIIME mapping file and links it in the db.
416421

qiita_db/metadata_template/sample_template.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from future.builtins import zip
1111
from os.path import join
1212
from time import strftime
13-
from os.path import basename
1413

1514
import pandas as pd
1615
import warnings
@@ -140,15 +139,8 @@ def create(cls, md_template, study):
140139

141140
conn_handler.execute_queue(queue_name)
142141

143-
# figuring out the filepath of the backup
144-
_id, fp = get_mountpoint('templates')[0]
145-
fp = join(fp, '%d_%s.txt' % (study.id, strftime("%Y%m%d-%H%M%S")))
146-
# storing the backup
147142
st = cls(study.id)
148-
st.to_file(fp)
149-
150-
# adding the fp to the object
151-
st.add_filepath(fp)
143+
st.generate_files()
152144

153145
return st
154146

@@ -220,6 +212,23 @@ def study_id(self):
220212
"""
221213
return self._id
222214

215+
def generate_files(self):
216+
r"""Generates all the files that contain data from this template
217+
"""
218+
# figuring out the filepath of the sample template
219+
_id, fp = get_mountpoint('templates')[0]
220+
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
221+
# storing the sample template
222+
self.to_file(fp)
223+
224+
# adding the fp to the object
225+
self.add_filepath(fp)
226+
227+
# generating all new QIIME mapping files
228+
for rd_id in Study(self.id).raw_data():
229+
for pt_id in RawData(rd_id).prep_templates:
230+
PrepTemplate(pt_id).generate_files()
231+
223232
def extend(self, md_template):
224233
"""Adds the given sample template to the current one
225234
@@ -305,14 +314,7 @@ def extend(self, md_template):
305314
values, many=True)
306315
conn_handler.execute_queue(queue_name)
307316

308-
# figuring out the filepath of the backup
309-
_id, fp = get_mountpoint('templates')[0]
310-
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
311-
# storing the backup
312-
self.to_file(fp)
313-
314-
# adding the fp to the object
315-
self.add_filepath(fp)
317+
self.generate_files()
316318

317319
def update(self, md_template):
318320
r"""Update values in the sample template
@@ -375,21 +377,4 @@ def update(self, md_template):
375377
for col in changed_cols:
376378
self.update_category(col, new_map[col].to_dict())
377379

378-
# figuring out the filepath of the backup
379-
_id, fp = get_mountpoint('templates')[0]
380-
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
381-
# storing the backup
382-
self.to_file(fp)
383-
384-
# adding the fp to the object
385-
self.add_filepath(fp)
386-
387-
# generating all new QIIME mapping files
388-
for rd_id in Study(self.id).raw_data():
389-
for pt_id in RawData(rd_id).prep_templates:
390-
pt = PrepTemplate(pt_id)
391-
for _, fp in pt.get_filepaths():
392-
# the difference between a prep and a qiime template is the
393-
# word qiime within the name of the file
394-
if '_qiime_' not in basename(fp):
395-
pt.create_qiime_mapping_file(fp)
380+
self.generate_files()

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,14 @@ def test_create_already_prefixed_samples(self):
10101010
self.assertEqual(filepaths[0][0], 22)
10111011
self.assertEqual(filepaths[1][0], 21)
10121012

1013+
def test_generate_files(self):
1014+
fp_count = get_count("qiita.filepath")
1015+
self.tester.generate_files()
1016+
obs = get_count("qiita.filepath")
1017+
# We just make sure that the count has been increased by 2, since
1018+
# the contents of the files have been tested elsewhere.
1019+
self.assertEqual(obs, fp_count + 2)
1020+
10131021
def test_create_qiime_mapping_file(self):
10141022
pt = PrepTemplate(1)
10151023

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from qiita_db.sql_connection import SQLConnectionHandler
2929
from qiita_db.study import Study, StudyPerson
3030
from qiita_db.user import User
31-
from qiita_db.util import exists_table, get_table_cols
31+
from qiita_db.util import exists_table, get_table_cols, get_count
3232
from qiita_db.metadata_template.sample_template import SampleTemplate, Sample
3333
from qiita_db.metadata_template.prep_template import PrepTemplate, PrepSample
3434

@@ -1338,6 +1338,14 @@ def test_update(self):
13381338
with self.assertRaises(QiitaDBError):
13391339
st.update(self.metadata_dict_updated_column_error)
13401340

1341+
def test_generate_files(self):
1342+
fp_count = get_count("qiita.filepath")
1343+
self.tester.generate_files()
1344+
obs = get_count("qiita.filepath")
1345+
# We just make sure that the count has been increased by 2, since
1346+
# the contents of the files have been tested elsewhere.
1347+
self.assertEqual(obs, fp_count + 3)
1348+
13411349
def test_to_file(self):
13421350
"""to file writes a tab delimited file with all the metadata"""
13431351
fd, fp = mkstemp()

0 commit comments

Comments
 (0)