Skip to content

Creating generate_files function #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,17 @@ def _transform_to_dict(self, values):

return result

def generate_files(self):
r"""Generates all the files that contain data from this template

Raises
------
QiitaDBNotImplementedError
This method should be implemented by the subclasses
"""
raise QiitaDBNotImplementedError(
"generate_files should be implemented in the subclass!")

def to_file(self, fp, samples=None):
r"""Writes the MetadataTemplate to the file `fp` in tab-delimited
format
Expand Down
29 changes: 17 additions & 12 deletions qiita_db/metadata_template/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,8 @@ def create(cls, md_template, raw_data, study, data_type,
# some other error we haven't seen before so raise it
raise

# figuring out the filepath of the backup
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_prep_%d_%s.txt' % (study.id, prep_id,
strftime("%Y%m%d-%H%M%S")))
# storing the backup
pt = cls(prep_id)
pt.to_file(fp)

# adding the fp to the object
pt.add_filepath(fp)

# creating QIIME mapping file
pt.create_qiime_mapping_file(fp)
pt.generate_files()

return pt

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

def generate_files(self):
r"""Generates all the files that contain data from this template
"""
# figuring out the filepath of the prep template
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_prep_%d_%s.txt' % (self.study_id, self._id,
strftime("%Y%m%d-%H%M%S")))
# storing the template
self.to_file(fp)

# adding the fp to the object
self.add_filepath(fp)

# creating QIIME mapping file
self.create_qiime_mapping_file(fp)

def create_qiime_mapping_file(self, prep_template_fp):
"""This creates the QIIME mapping file and links it in the db.
Expand Down
55 changes: 20 additions & 35 deletions qiita_db/metadata_template/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from future.builtins import zip
from os.path import join
from time import strftime
from os.path import basename

import pandas as pd
import warnings
Expand Down Expand Up @@ -140,15 +139,8 @@ def create(cls, md_template, study):

conn_handler.execute_queue(queue_name)

# figuring out the filepath of the backup
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_%s.txt' % (study.id, strftime("%Y%m%d-%H%M%S")))
# storing the backup
st = cls(study.id)
st.to_file(fp)

# adding the fp to the object
st.add_filepath(fp)
st.generate_files()

return st

Expand All @@ -163,6 +155,23 @@ def study_id(self):
"""
return self._id

def generate_files(self):
r"""Generates all the files that contain data from this template
"""
# figuring out the filepath of the sample template
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
# storing the sample template
self.to_file(fp)

# adding the fp to the object
self.add_filepath(fp)

# generating all new QIIME mapping files
for rd_id in Study(self.id).raw_data():
for pt_id in RawData(rd_id).prep_templates:
PrepTemplate(pt_id).generate_files()

def extend(self, md_template):
"""Adds the given sample template to the current one

Expand Down Expand Up @@ -248,14 +257,7 @@ def extend(self, md_template):
values, many=True)
conn_handler.execute_queue(queue_name)

# figuring out the filepath of the backup
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
# storing the backup
self.to_file(fp)

# adding the fp to the object
self.add_filepath(fp)
self.generate_files()

def update(self, md_template):
r"""Update values in the sample template
Expand Down Expand Up @@ -318,21 +320,4 @@ def update(self, md_template):
for col in changed_cols:
self.update_category(col, new_map[col].to_dict())

# figuring out the filepath of the backup
_id, fp = get_mountpoint('templates')[0]
fp = join(fp, '%d_%s.txt' % (self.id, strftime("%Y%m%d-%H%M%S")))
# storing the backup
self.to_file(fp)

# adding the fp to the object
self.add_filepath(fp)

# generating all new QIIME mapping files
for rd_id in Study(self.id).raw_data():
for pt_id in RawData(rd_id).prep_templates:
pt = PrepTemplate(pt_id)
for _, fp in pt.get_filepaths():
# the difference between a prep and a qiime template is the
# word qiime within the name of the file
if '_qiime_' not in basename(fp):
pt.create_qiime_mapping_file(fp)
self.generate_files()
8 changes: 8 additions & 0 deletions qiita_db/metadata_template/test/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,14 @@ def test_create_already_prefixed_samples(self):
self.assertEqual(filepaths[0][0], 22)
self.assertEqual(filepaths[1][0], 21)

def test_generate_files(self):
fp_count = get_count("qiita.filepath")
self.tester.generate_files()
obs = get_count("qiita.filepath")
# We just make sure that the count has been increased by 2, since
# the contents of the files have been tested elsewhere.
self.assertEqual(obs, fp_count + 2)

def test_create_qiime_mapping_file(self):
pt = PrepTemplate(1)

Expand Down
10 changes: 9 additions & 1 deletion qiita_db/metadata_template/test/test_sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from qiita_db.sql_connection import SQLConnectionHandler
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.util import exists_table, get_table_cols
from qiita_db.util import exists_table, get_table_cols, get_count
from qiita_db.metadata_template.sample_template import SampleTemplate, Sample
from qiita_db.metadata_template.prep_template import PrepTemplate, PrepSample

Expand Down Expand Up @@ -1332,6 +1332,14 @@ def test_update(self):
with self.assertRaises(QiitaDBError):
st.update(self.metadata_dict_updated_column_error)

def test_generate_files(self):
fp_count = get_count("qiita.filepath")
self.tester.generate_files()
obs = get_count("qiita.filepath")
# We just make sure that the count has been increased by 2, since
# the contents of the files have been tested elsewhere.
self.assertEqual(obs, fp_count + 3)

def test_to_file(self):
"""to file writes a tab delimited file with all the metadata"""
fd, fp = mkstemp()
Expand Down