Skip to content

Commit

Permalink
feat: Improved error messages for protocol parameters & components (#106
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sellth authored Jan 18, 2024
1 parent e10df5e commit 571bfcc
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions altamisa/isatab/parse_investigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ def _parse_comment_header(val):

# Helper function to extract protocol parameters
def _split_study_protocols_parameters(
names, name_term_accs, name_term_srcs
protocol_name: str, names_str: str, name_term_accs_str: str, name_term_srcs_str: str
) -> Iterator[models.FreeTextOrTermRef]:
names = names.split(";")
name_term_accs = name_term_accs.split(";")
name_term_srcs = name_term_srcs.split(";")
names = names_str.split(";")
name_term_accs = name_term_accs_str.split(";")
name_term_srcs = name_term_srcs_str.split(";")
if not (len(names) == len(name_term_accs) == len(name_term_srcs)): # pragma: no cover
tpl = 'Unequal protocol parameter splits; found: "{}", "{}", "{}"'
msg = tpl.format(names, name_term_accs, name_term_srcs)
msg = (
f"Unequal parameter splits in protocol '{protocol_name}':\n"
f"Parameter Names: {len(names)}\n"
f"Term Accession Numers: {len(name_term_accs)}\n"
f"Term Source REFs: {len(name_term_srcs)}"
)
raise ParseIsatabException(msg)
if len(names) > len(set(names)): # pragma: no cover
tpl = "Repeated protocol parameter; found: {}"
Expand All @@ -62,17 +66,26 @@ def _split_study_protocols_parameters(

# Helper function to extract protocol components
def _split_study_protocols_components(
names, types, type_term_accs, type_term_srcs
protocol_name: str,
names_str: str,
types_str: str,
type_term_accs_str: str,
type_term_srcs_str: str,
) -> Iterator[models.ProtocolComponentInfo]:
names = names.split(";")
types = types.split(";")
type_term_accs = type_term_accs.split(";")
type_term_srcs = type_term_srcs.split(";")
names = names_str.split(";")
types = types_str.split(";")
type_term_accs = type_term_accs_str.split(";")
type_term_srcs = type_term_srcs_str.split(";")
if not (
len(names) == len(types) == len(type_term_accs) == len(type_term_srcs)
): # pragma: no cover
tpl = "Unequal protocol component splits; " 'found: "{}", "{}", "{}", "{}"'
msg = tpl.format(names, types, type_term_accs, type_term_srcs)
msg = (
f"Unequal component splits in protocol '{protocol_name}':\n"
f"Components Names: {len(names)}\n"
f"Components Types: {len(types)}\n"
f"Type Term Accession Numers: {len(type_term_accs)}\n"
f"Type Term Source REFs: {len(type_term_srcs)}"
)
raise ParseIsatabException(msg)
if len(names) > len(set(names)): # pragma: no cover
tpl = "Repeated protocol components; found: {}"
Expand Down Expand Up @@ -193,8 +206,10 @@ def _read_multi_column_section(self, prefix: str, ref_keys: Sequence[str], secti
msg = tpl.format(section_name, list(sorted(section)))
raise ParseIsatabException(msg) # TODO: should be warning?
if not len(set([len(v) for v in section.values()])) == 1: # pragma: no cover
tpl = "Inconsistent entry lengths in section {}"
msg = tpl.format(section_name)
lengths = "\n".join(
map(str, [f"{key}: {len(value)}" for key, value in section.items()])
)
msg = f"Inconsistent entry lengths in section {section_name}:\n{lengths}"
raise ParseIsatabException(msg)
return section, comment_keys

Expand Down Expand Up @@ -565,15 +580,15 @@ def _read_study_protocols(self) -> Iterator[models.ProtocolInfo]:
type_ont = models.OntologyTermRef(type_term, type_term_acc, type_term_src)
paras: Dict[str, models.FreeTextOrTermRef] = {}
for p in _split_study_protocols_parameters(
para_names, para_name_term_accs, para_name_term_srcs
name, para_names, para_name_term_accs, para_name_term_srcs
):
key = models.free_text_or_term_ref_to_str(p)
if key:
paras[key] = p
comps = {
c.name: c
for c in _split_study_protocols_components(
comp_names, comp_types, comp_type_term_accs, comp_type_term_srcs
name, comp_names, comp_types, comp_type_term_accs, comp_type_term_srcs
)
}
comments = _parse_comments(section, comment_keys, i)
Expand Down

0 comments on commit 571bfcc

Please sign in to comment.