Skip to content

Commit 8a791a3

Browse files
committed
Merge pull request #840 from ElDeveloper/issue-713
BUG: Improve message when sample_name isn't found
2 parents 4887a81 + f24e5dd commit 8a791a3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

qiita_db/metadata_template.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,8 @@ def load_template_to_dataframe(fn):
18951895
18961896
Raises
18971897
------
1898+
QiitaDBColumnError
1899+
If the sample_name column is not present in the template.
18981900
UserWarning
18991901
When columns are dropped because they have no content for any sample.
19001902
@@ -1928,6 +1930,10 @@ def load_template_to_dataframe(fn):
19281930

19291931
initial_columns = set(template.columns)
19301932

1933+
if 'sample_name' not in template.columns:
1934+
raise QiitaDBColumnError("The 'sample_name' column is missing from "
1935+
"your template, this file cannot be parsed.")
1936+
19311937
# remove rows that have no sample identifier but that may have other data
19321938
# in the rest of the columns
19331939
template.dropna(subset=['sample_name'], how='all', inplace=True)

qiita_db/test/test_metadata_template.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,14 @@ def test_load_template_to_dataframe_column_with_nas(self):
15881588
exp.index.name = 'sample_name'
15891589
assert_frame_equal(obs, exp)
15901590

1591+
def test_load_template_to_dataframe_exception(self):
1592+
with self.assertRaises(QiitaDBColumnError):
1593+
x = load_template_to_dataframe(
1594+
StringIO(SAMPLE_TEMPLATE_NO_SAMPLE_NAME))
1595+
1596+
# prevent flake8 from complaining
1597+
x.strip()
1598+
15911599
def test_get_invalid_sample_names(self):
15921600
all_valid = ['2.sample.1', 'foo.bar.baz', 'roses', 'are', 'red',
15931601
'v10l3t5', '4r3', '81u3']
@@ -1790,6 +1798,21 @@ def test_get_get_invalid_sample_names_mixed(self):
17901798
"True\tNotIdentified\t4.8\t4.41\tlocation1\treceived\ttype1\t"
17911799
"NA\n")
17921800

1801+
SAMPLE_TEMPLATE_NO_SAMPLE_NAME = (
1802+
":L}={\tcollection_timestamp\tdescription\thas_extracted_data\t"
1803+
"has_physical_specimen\thost_subject_id\tlatitude\tlongitude\t"
1804+
"physical_location\trequired_sample_info_status\tsample_type\t"
1805+
"str_column\n"
1806+
"2.Sample1\t2014-05-29 12:24:51\tTest Sample 1\tTrue\tTrue\t"
1807+
"NotIdentified\t42.42\t41.41\tlocation1\treceived\ttype1\t"
1808+
"NA\n"
1809+
"2.Sample2\t2014-05-29 12:24:51\t"
1810+
"Test Sample 2\tTrue\tTrue\tNotIdentified\t4.2\t1.1\tlocation1\treceived\t"
1811+
"type1\tNA\n"
1812+
"2.Sample3\t2014-05-29 12:24:51\tTest Sample 3\tTrue\t"
1813+
"True\tNotIdentified\t4.8\t4.41\tlocation1\treceived\ttype1\t"
1814+
"NA\n")
1815+
17931816
SAMPLE_TEMPLATE_DICT_FORM = {
17941817
'collection_timestamp': {'2.Sample1': '2014-05-29 12:24:51',
17951818
'2.Sample2': '2014-05-29 12:24:51',

0 commit comments

Comments
 (0)