Skip to content

Unify update category #1050

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 3 commits into from
Apr 9, 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
27 changes: 27 additions & 0 deletions qiita_db/metadata_template/base_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,30 @@ def categories(self):
cols[idx] = self.translate_cols_dict[c]

return cols

def update_category(self, category, samples_and_values):
"""Update an existing column

Parameters
----------
category : str
The category to update
samples_and_values : dict
A mapping of {sample_id: value}

Raises
------
QiitaDBUnknownIDError
If a sample_id is included in values that is not in the template
QiitaDBColumnError
If the column does not exist in the table. This is implicit, and
can be thrown by the contained Samples.
"""
if not set(self.keys()).issuperset(samples_and_values):
missing = set(self.keys()) - set(samples_and_values)
table_name = self._table_name(self.study_id)
raise QiitaDBUnknownIDError(missing, table_name)

for k, v in viewitems(samples_and_values):
sample = self[k]
sample[category] = v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do self[k][category] = v and not need to instantiate sample.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not saving the instantiation, it is happening at the moment that you do self[k]. Anyways, this is left like this because I modify this in my other PR #1054....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

29 changes: 0 additions & 29 deletions qiita_db/metadata_template/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from __future__ import division
from future.builtins import zip
from future.utils import viewitems
from copy import deepcopy
from os.path import join
from time import strftime
Expand All @@ -20,7 +19,6 @@

from qiita_core.exceptions import IncompetentQiitaDeveloperError
from qiita_db.exceptions import (QiitaDBDuplicateError, QiitaDBColumnError,
QiitaDBUnknownIDError,
QiitaDBDuplicateHeaderError, QiitaDBError,
QiitaDBWarning)
from qiita_db.sql_connection import SQLConnectionHandler
Expand Down Expand Up @@ -407,33 +405,6 @@ def update(self, md_template):
if '_qiime_' not in basename(fp):
pt.create_qiime_mapping_file(fp)

def update_category(self, category, samples_and_values):
"""Update an existing column

Parameters
----------
category : str
The category to update
samples_and_values : dict
A mapping of {sample_id: value}

Raises
------
QiitaDBUnknownIDError
If a sample_id is included in values that is not in the template
QiitaDBColumnError
If the column does not exist in the table. This is implicit, and
can be thrown by the contained Samples.
"""
if not set(self.keys()).issuperset(samples_and_values):
missing = set(self.keys()) - set(samples_and_values)
table_name = self._table_name(self.study_id)
raise QiitaDBUnknownIDError(missing, table_name)

for k, v in viewitems(samples_and_values):
sample = self[k]
sample[category] = v

def add_category(self, category, samples_and_values, dtype, default):
"""Add a metadata category

Expand Down
31 changes: 31 additions & 0 deletions qiita_db/metadata_template/test/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,37 @@ def test_status(self):
self.test_study, self.data_type_id)
self.assertEqual(pt.status, 'sandbox')

def test_update_category(self):
with self.assertRaises(QiitaDBUnknownIDError):
self.tester.update_category('barcodesequence', {"foo": "bar"})

with self.assertRaises(QiitaDBColumnError):
self.tester.update_category('missing column',
{'1.SKB7.640196': 'bar'})

neg_test = self.tester['1.SKB7.640196']['barcodesequence']
mapping = {'1.SKB8.640193': 'AAAAAAAAAAAA',
'1.SKD8.640184': 'CCCCCCCCCCCC'}

self.tester.update_category('barcodesequence', mapping)

self.assertEqual(self.tester['1.SKB7.640196']['barcodesequence'],
neg_test)
self.assertEqual(self.tester['1.SKB8.640193']['barcodesequence'],
'AAAAAAAAAAAA')
self.assertEqual(self.tester['1.SKD8.640184']['barcodesequence'],
'CCCCCCCCCCCC')

neg_test = self.tester['1.SKB7.640196']['center_name']
mapping = {'1.SKB8.640193': 'FOO',
'1.SKD8.640184': 'BAR'}

self.tester.update_category('center_name', mapping)

self.assertEqual(self.tester['1.SKB7.640196']['center_name'], neg_test)
self.assertEqual(self.tester['1.SKB8.640193']['center_name'], 'FOO')
self.assertEqual(self.tester['1.SKD8.640184']['center_name'], 'BAR')


EXP_PREP_TEMPLATE = (
'sample_name\tbarcodesequence\tcenter_name\tcenter_project_name\t'
Expand Down