# SwitchBuilder¶

class hail.expr.builders.SwitchBuilder[source]

Class for generating conditional trees based on value of an expression.

Examples

>>> csq = hl.literal('loss of function')
>>> expr = (hl.switch(csq)
...           .when('synonymous', 1)
...           .when('SYN', 1)
...           .when('missense', 2)
...           .when('MIS', 2)
...           .when('loss of function', 3)
...           .when('LOF', 3)
...           .or_missing())
>>> hl.eval(expr)
3


Notes

All expressions appearing as the then parameters to when() or default() method calls must be the same type.

Parameters

expr (Expression) – Value to match against.

Attributes

Methods

 default Finish the switch statement by adding a default case. or_error Finish the switch statement by throwing an error with the given message. or_missing Finish the switch statement by returning missing. when Add a value test. when_missing Add a test for missingness.
default(then)[source]

Finish the switch statement by adding a default case.

Notes

If no value from a when() call is matched, then then is returned.

Parameters

then (Expression)

Returns

Expression

or_error(message)[source]

Finish the switch statement by throwing an error with the given message.

Notes

If no value from a SwitchBuilder.when() call is matched, then an error is thrown.

Parameters

message (Expression of type tstr)

Returns

Expression

or_missing()[source]

Finish the switch statement by returning missing.

Notes

If no value from a when() call is matched, then the result is missing.

Parameters

then (Expression)

Returns

Expression

when(value, then)[source]

Add a value test. If the base expression is equal to value, then returns then.

Warning

Missingness always compares to missing. Both NA == NA and NA != NA return NA. Use when_missing() to test missingness.

Parameters
Returns

SwitchBuilder – Mutates and returns self.

when_missing(then)[source]

Add a test for missingness. If the base expression is missing, returns then.

Parameters

then (Expression)

Returns

SwitchBuilder – Mutates and returns self.