Skip to content

Commit 4260c39

Browse files
committed
Adding column checker
1 parent fd7608a commit 4260c39

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
# -----------------------------------------------------------------------------
88

99
from __future__ import division
10+
from future.utils import viewitems
1011
from os.path import join
1112
from functools import partial
1213
from copy import deepcopy
14+
import warnings
1315

1416
import pandas as pd
1517
from skbio.util import find_duplicates
1618

1719
from qiita_core.exceptions import IncompetentQiitaDeveloperError
1820
from qiita_db.exceptions import (QiitaDBUnknownIDError, QiitaDBColumnError,
19-
QiitaDBNotImplementedError,
21+
QiitaDBNotImplementedError, QiitaDBWarning,
2022
QiitaDBDuplicateHeaderError)
2123
from qiita_db.base import QiitaObject
2224
from qiita_db.sql_connection import SQLConnectionHandler
@@ -911,8 +913,7 @@ def categories(self):
911913
return get_table_cols(self._table_name(self._id))
912914

913915
@classmethod
914-
def _clean_validate_template(cls, md_template, study_id,
915-
conn_handler=None):
916+
def _clean_validate_template(cls, md_template, study_id, restriction_dict):
916917
"""Takes care of all validation and cleaning of templates
917918
918919
Parameters
@@ -921,6 +922,8 @@ def _clean_validate_template(cls, md_template, study_id,
921922
The metadata template file contents indexed by sample ids
922923
study_id : int
923924
The study to which the template belongs to.
925+
restriction_dict : dict of {str: Restriction}
926+
A dictionary with the restrictions that apply to the metadata
924927
925928
Returns
926929
-------
@@ -929,11 +932,11 @@ def _clean_validate_template(cls, md_template, study_id,
929932
"""
930933
invalid_ids = get_invalid_sample_names(md_template.index)
931934
if invalid_ids:
932-
raise QiitaDBColumnError("The following sample names in the sample"
933-
" template contain invalid characters "
934-
"(only alphanumeric characters or periods"
935-
" are allowed): %s." %
936-
", ".join(invalid_ids))
935+
raise QiitaDBColumnError(
936+
"The following sample names in the %s contain invalid "
937+
"characters (only alphanumeric characters or periods are "
938+
"allowed): %s." % (cls.__name__, ", ".join(invalid_ids)))
939+
937940
# We are going to modify the md_template. We create a copy so
938941
# we don't modify the user one
939942
md_template = deepcopy(md_template)
@@ -949,6 +952,19 @@ def _clean_validate_template(cls, md_template, study_id,
949952
raise QiitaDBDuplicateHeaderError(
950953
find_duplicates(md_template.columns))
951954

952-
conn_handler = conn_handler if conn_handler else SQLConnectionHandler()
955+
# Check if we have the columns needed for some functionality
956+
warning_msg = []
957+
for key, restriction in viewitems(restriction_dict):
958+
missing = [col for col in restriction.columns
959+
if col not in md_template]
960+
if missing:
961+
warning_msg.append(
962+
"%s: %s" % (restriction.error_msg, ', '.join(missing)))
963+
964+
if warning_msg:
965+
warnings.warn(
966+
"Some functionality will be disabled due to missing "
967+
"columns:\n\t%s" % "\n\t".join(warning_msg),
968+
QiitaDBWarning)
953969

954970
return md_template

0 commit comments

Comments
 (0)