7
7
# -----------------------------------------------------------------------------
8
8
9
9
from __future__ import division
10
+ from future .utils import viewitems
10
11
from os .path import join
11
12
from functools import partial
12
13
from copy import deepcopy
14
+ import warnings
13
15
14
16
import pandas as pd
15
17
from skbio .util import find_duplicates
16
18
17
19
from qiita_core .exceptions import IncompetentQiitaDeveloperError
18
20
from qiita_db .exceptions import (QiitaDBUnknownIDError , QiitaDBColumnError ,
19
- QiitaDBNotImplementedError ,
21
+ QiitaDBNotImplementedError , QiitaDBWarning ,
20
22
QiitaDBDuplicateHeaderError )
21
23
from qiita_db .base import QiitaObject
22
24
from qiita_db .sql_connection import SQLConnectionHandler
@@ -911,8 +913,7 @@ def categories(self):
911
913
return get_table_cols (self ._table_name (self ._id ))
912
914
913
915
@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 ):
916
917
"""Takes care of all validation and cleaning of templates
917
918
918
919
Parameters
@@ -921,6 +922,8 @@ def _clean_validate_template(cls, md_template, study_id,
921
922
The metadata template file contents indexed by sample ids
922
923
study_id : int
923
924
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
924
927
925
928
Returns
926
929
-------
@@ -929,11 +932,11 @@ def _clean_validate_template(cls, md_template, study_id,
929
932
"""
930
933
invalid_ids = get_invalid_sample_names (md_template .index )
931
934
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
+
937
940
# We are going to modify the md_template. We create a copy so
938
941
# we don't modify the user one
939
942
md_template = deepcopy (md_template )
@@ -949,6 +952,19 @@ def _clean_validate_template(cls, md_template, study_id,
949
952
raise QiitaDBDuplicateHeaderError (
950
953
find_duplicates (md_template .columns ))
951
954
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 )
953
969
954
970
return md_template
0 commit comments