diff --git a/emperor/core.py b/emperor/core.py index b5684e0d..c981d6d0 100644 --- a/emperor/core.py +++ b/emperor/core.py @@ -325,11 +325,22 @@ def _validate_metadata(self, metadata, matrix, ignore_missing_samples, ' to the same dataset.' % kind) if difference and not ignore_missing_samples: - elements = ', '.join(sorted([str(i) for i in difference])) + # sort the elements so we have a deterministic output + difference = sorted([str(i) for i in difference]) + + # if there's more than 5 missing elements, truncate the list + if len(difference) > 5: + elements = ', '.join(difference[:5]) + suffix = ("Showing only the first 5 %ss out of %d: %s ..." % + (kind, len(difference), elements)) + else: + elements = ', '.join(difference) + suffix = ("Offending %ss: %s" % (kind, elements)) + raise KeyError("There are %ss not included in the %s mapping " "file. Override this error by using the " - "`ignore_missing_samples` argument. Offending " - "%ss: %s" % (kind, kind, kind, elements)) + "`ignore_missing_samples` argument. %s" % + (kind, kind, suffix)) elif difference and ignore_missing_samples: warnings.warn("%d out of %d %ss have no metadata and are being" " included with a placeholder value." % diff --git a/tests/test_core.py b/tests/test_core.py index 6956ce3c..2155445d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -241,6 +241,19 @@ def test_one_dimensional(self): "two dimensions are not supported"): Emperor(self.ord_res, self.mf, remote=False) + def test_initial_unbalanced_more_than_five(self): + mf = self.mf.copy() + mf.drop(['PC.354', 'PC.355', 'PC.356', 'PC.481', 'PC.607', 'PC.636'], + inplace=True) + with self.assertRaisesRegexp(KeyError, "There are samples not " + "included in the sample mapping file. " + "Override this error by using the " + "`ignore_missing_samples` argument. " + "Showing only the first 5 samples out of " + "6: PC.354, PC.355, PC.356, PC.481, " + "PC.607 ..."): + Emperor(self.ord_res, mf, remote=self.url) + def test_initial_unbalanced(self): mf = self.mf.copy() mf.drop(['PC.354'], inplace=True)