-
Notifications
You must be signed in to change notification settings - Fork 31
Add function update_structured_annotations
to update structured annotations on a Table
#580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@jkgoodrich I get this error: |
I put this function in release.py, passed checks, is it Okay? |
gnomad/utils/release.py
Outdated
return index_dict | ||
|
||
|
||
def update_sample_annotations(expr: hl.expr, sample_annotations: Dict[str, hl.expr]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should go in annotations, it makes a lot more sense there. You can handle the circular import using import as
for example import gnomad.utils.filtering as filter_utils
Also, I modified it a little:
def update_structured_annotations(
ht: hl.Table,
annotation_update_exprs: Dict[str, hl.Expression],
annotation_update_label: Optional[str] = None,
) -> hl.Table:
"""
Update highly structured annotations on a Table.
This function recursively updates annotations defined by `annotation_update_exprs`
and if `annotation_update_label` is supplied, it checks if the sample annotations
are different from the input and adds a flag to the Table, indicating which
annotations have been updated for each sample.
:param ht: Input Table with structured annotations to update.
:param annotation_update_exprs: Dictionary of annotations to update, structured as
they are structured on the input `ht`.
:param annotation_update_label: Optional string of the label to use for an
annotation indicating which annotations have been updated. Default is None, so
no annotation is added.
:return: Table with updated annotations and optionally a flag indicating which
annotations were changed.
"""
def _update_struct(
struct_expr: hl.expr.StructExpression,
update_exprs: Union[Dict[str, hl.Expression], hl.expr.Expression]
) -> Tuple[Dict[str, hl.expr.BooleanExpression], hl.expr.StructExpression]:
"""
Update a StructExpression.
:param struct_expr: StructExpression to update.
:param update_exprs: Dictionary of annotations to update.
:return: Tuple of the updated annotations and the updated flag.
"""
if isinstance(update_exprs, dict):
updated_struct_expr = {}
updated_flag_expr = {}
for ann, expr in update_exprs.items():
updated_flag, updated_ann = _update_struct(struct_expr[ann], expr)
updated_flag_expr.update(
{ann + ("." + k if k else ""): v for k, v in updated_flag.items()}
)
updated_struct_expr[ann] = updated_ann
return updated_flag_expr, struct_expr.annotate(**updated_struct_expr)
else:
return {"": update_exprs != struct_expr}, update_exprs
annotation_update_flag, updated_rows = _update_struct(
ht.row_value, annotation_update_exprs
)
if annotation_update_label is not None:
updated_rows = updated_rows.annotate(
**{
annotation_update_label: filter_utils.add_filters_expr(
filters=annotation_update_flag
)
}
)
return ht.annotate(**updated_rows)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I put it back to annotations.py, and modified the return type in _updata_struct().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a couple questions about types
gnomad/utils/annotations.py
Outdated
|
||
def _update_struct( | ||
struct_expr: hl.expr.StructExpression, | ||
update_exprs: Union[Dict[str, hl.Expression], hl.expr.Expression], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update_exprs: Union[Dict[str, hl.Expression], hl.expr.Expression], | |
update_exprs: Union[Dict[str, hl.expr.Expression], hl.expr.Expression], |
gnomad/utils/annotations.py
Outdated
def _update_struct( | ||
struct_expr: hl.expr.StructExpression, | ||
update_exprs: Union[Dict[str, hl.Expression], hl.expr.Expression], | ||
) -> Tuple[Dict[str, Any], hl.expr]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the first one not always Dict[str, hl.expr.BooleanExpression]
? and why hl.expr
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this warning for the return in else:
Expected type 'tuple[dict[str, BooleanExpression], StructExpression]', got 'tuple[dict[str, Any], Expression]' instead: 1846.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try this Tuple[Dict[str, hl.expr.BooleanExpression], Any]
I think the first should be a BooleanExpression, the suppose the second can be multiple different things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
update_structured_annotations
to update structured annotations on a Table
Update highly structured annotations such as gnomAD sample meta.