Skip to content

Commit 9fe81b0

Browse files
antgonzaElDeveloper
authored andcommitted
fix-1591 (#2370)
* fix-1591 * removing warning ATTN @josenavas, fix tests
1 parent 4105e9a commit 9fe81b0

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,7 @@ def test_create(self):
887887
def test_create_already_prefixed_samples(self):
888888
"""Creates a new PrepTemplate"""
889889
fp_count = qdb.util.get_count('qiita.filepath')
890-
pt = npt.assert_warns(
891-
qdb.exceptions.QiitaDBWarning,
892-
qdb.metadata_template.prep_template.PrepTemplate.create,
890+
pt = qdb.metadata_template.prep_template.PrepTemplate.create(
893891
self.metadata_prefixed, self.test_study, self.data_type)
894892
self._common_creation_checks(pt, fp_count)
895893

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,7 @@ def test_create_str_prefixes(self):
10841084

10851085
def test_create_already_prefixed_samples(self):
10861086
"""Creates a new SampleTemplate with the samples already prefixed"""
1087-
st = npt.assert_warns(
1088-
qdb.exceptions.QiitaDBWarning,
1089-
qdb.metadata_template.sample_template.SampleTemplate.create,
1087+
st = qdb.metadata_template.sample_template.SampleTemplate.create(
10901088
self.metadata_prefixed, self.new_study)
10911089
new_id = self.new_study.id
10921090
# The returned object has the correct id

qiita_db/metadata_template/test/test_util.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from six import StringIO
1010
from unittest import TestCase, main
11+
import warnings
1112

1213
import numpy.testing as npt
1314
import pandas as pd
@@ -36,12 +37,31 @@ def test_prefix_sample_names_with_id(self):
3637
}
3738
exp_df = pd.DataFrame.from_dict(exp_metadata_dict, orient='index',
3839
dtype=str)
39-
qdb.metadata_template.util.prefix_sample_names_with_id(
40-
self.metadata_map, 1)
40+
with warnings.catch_warnings(record=True) as warn:
41+
qdb.metadata_template.util.prefix_sample_names_with_id(
42+
self.metadata_map, 1)
43+
self.assertEqual(len(warn), 0)
4144
self.metadata_map.sort_index(inplace=True)
4245
exp_df.sort_index(inplace=True)
4346
assert_frame_equal(self.metadata_map, exp_df)
4447

48+
# test that it only prefixes the samples that are needed
49+
metadata_dict = {
50+
'Sample1': {'int_col': 1, 'float_col': 2.1, 'str_col': 'str1'},
51+
'1.Sample2': {'int_col': 2, 'float_col': 3.1, 'str_col': '200'},
52+
'Sample3': {'int_col': 3, 'float_col': 3, 'str_col': 'string30'},
53+
}
54+
metadata_map = pd.DataFrame.from_dict(
55+
metadata_dict, orient='index', dtype=str)
56+
with warnings.catch_warnings(record=True) as warn:
57+
qdb.metadata_template.util.prefix_sample_names_with_id(
58+
metadata_map, 1)
59+
self.assertEqual(len(warn), 1)
60+
self.assertEqual(str(warn[0].message), 'Some of the samples were '
61+
'already prefixed with the study id.')
62+
metadata_map.sort_index(inplace=True)
63+
assert_frame_equal(metadata_map, exp_df)
64+
4565
def test_load_template_to_dataframe(self):
4666
obs = qdb.metadata_template.util.load_template_to_dataframe(
4767
StringIO(EXP_SAMPLE_TEMPLATE))

qiita_db/metadata_template/util.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,25 @@ def prefix_sample_names_with_id(md_template, study_id):
3434
study_id : int
3535
The study to which the metadata belongs to
3636
"""
37-
# Get all the prefixes of the index, defined as any string before a '.'
38-
prefixes = {idx.split('.', 1)[0] for idx in md_template.index}
39-
# If the samples have been already prefixed with the study id, the prefixes
40-
# set will contain only one element and it will be the str representation
41-
# of the study id
42-
if len(prefixes) == 1 and prefixes.pop() == str(study_id):
43-
# The samples were already prefixed with the study id
44-
warnings.warn("Sample names were already prefixed with the study id.",
45-
qdb.exceptions.QiitaDBWarning)
46-
else:
47-
# Create a new pandas series in which all the values are the study_id
48-
# and it is indexed as the metadata template
49-
study_ids = pd.Series([str(study_id)] * len(md_template.index),
50-
index=md_template.index)
51-
# Create a new column on the metadata template that includes the
52-
# metadata template indexes prefixed with the study id
53-
md_template['sample_name_with_id'] = (study_ids + '.' +
54-
md_template.index.values)
55-
md_template.index = md_template.sample_name_with_id
56-
del md_template['sample_name_with_id']
57-
# The original metadata template had the index column unnamed - remove
58-
# the name of the index for consistency
59-
md_template.index.name = None
37+
# loop over the samples and prefix those that aren't prefixed
38+
md_template['qiita_sample_name_with_id'] = pd.Series(
39+
[idx if idx.split('.', 1)[0] == str(study_id)
40+
else '%d.%s' % (study_id, idx)
41+
for idx in md_template.index], index=md_template.index)
42+
43+
# get the rows that are gonna change
44+
changes = len(md_template.index[
45+
md_template['qiita_sample_name_with_id'] != md_template.index])
46+
if changes != 0 and changes != len(md_template.index):
47+
warnings.warn(
48+
"Some of the samples were already prefixed with the study id.",
49+
qdb.exceptions.QiitaDBWarning)
50+
51+
md_template.index = md_template.qiita_sample_name_with_id
52+
del md_template['qiita_sample_name_with_id']
53+
# The original metadata template had the index column unnamed -> remove
54+
# the name of the index for consistency
55+
md_template.index.name = None
6056

6157

6258
def load_template_to_dataframe(fn, index='sample_name'):

qiita_pet/handlers/api_proxy/tests/test_prep_template.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ def test_prep_template_post_req(self):
380380
'16S')
381381
exp = {'status': 'warning',
382382
'message': [
383-
'Sample names were already prefixed with the study id.',
384383
('Some columns required to generate a QIIME-compliant '
385384
'mapping file are not present in the template. A '
386385
'placeholder value (XXQIITAXX) has been used to populate '

0 commit comments

Comments
 (0)