BooleanExpression

class hail.expr.BooleanExpression[source]

Expression of type tbool.

>>> t = hl.literal(True)
>>> f = hl.literal(False)
>>> na = hl.missing(hl.tbool)
>>> hl.eval(t)
True
>>> hl.eval(f)
False
>>> hl.eval(na)
None

Attributes

dtype

The data type of the expression.

Methods

__add__(other)

Add two numbers.

Examples

>>> hl.eval(x + 2)
5
>>> hl.eval(x + y)
7.5
Parameters:

other (NumericExpression) – Number to add.

Returns:

NumericExpression – Sum of the two numbers.

__and__(other)[source]

Return True if the left and right arguments are True.

Examples

>>> hl.eval(t & f)
False
>>> hl.eval(t & na)
None
>>> hl.eval(f & na)
False

The & and | operators have higher priority than comparison operators like ==, <, or >. Parentheses are often necessary:

>>> x = hl.literal(5)
>>> hl.eval((x < 10) & (x > 2))
True
Parameters:

other (BooleanExpression) – Right-side operand.

Returns:

BooleanExpressionTrue if both left and right are True.

__eq__(other)

Returns True if the two expressions are equal.

Examples

>>> x = hl.literal(5)
>>> y = hl.literal(5)
>>> z = hl.literal(1)
>>> hl.eval(x == y)
True
>>> hl.eval(x == z)
False

Notes

This method will fail with an error if the two expressions are not of comparable types.

Parameters:

other (Expression) – Expression for equality comparison.

Returns:

BooleanExpressionTrue if the two expressions are equal.

__floordiv__(other)

Divide two numbers with floor division.

Examples

>>> hl.eval(x // 2)
1
>>> hl.eval(y // 2)
2.0
Parameters:

other (NumericExpression) – Dividend.

Returns:

NumericExpression – The floor of the left number divided by the right.

__ge__(other)

Greater-than-or-equals comparison.

Examples

>>> hl.eval(y >= 4)
True
Parameters:

other (NumericExpression) – Right side for comparison.

Returns:

BooleanExpressionTrue if the left side is greater than or equal to the right side.

__gt__(other)

Greater-than comparison.

Examples

>>> hl.eval(y > 4)
True
Parameters:

other (NumericExpression) – Right side for comparison.

Returns:

BooleanExpressionTrue if the left side is greater than the right side.

__invert__()[source]

Return the boolean negation.

Examples

>>> hl.eval(~t)
False
>>> hl.eval(~f)
True
>>> hl.eval(~na)
None
Returns:

BooleanExpression – Boolean negation.

__le__(other)

Less-than-or-equals comparison.

Examples

>>> hl.eval(x <= 3)
True
Parameters:

other (NumericExpression) – Right side for comparison.

Returns:

BooleanExpressionTrue if the left side is smaller than or equal to the right side.

__lt__(other)

Less-than comparison.

Examples

>>> hl.eval(x < 5)
True
Parameters:

other (NumericExpression) – Right side for comparison.

Returns:

BooleanExpressionTrue if the left side is smaller than the right side.

__mod__(other)

Compute the left modulo the right number.

Examples

>>> hl.eval(32 % x)
2
>>> hl.eval(7 % y)
2.5
Parameters:

other (NumericExpression) – Dividend.

Returns:

NumericExpression – Remainder after dividing the left by the right.

__mul__(other)

Multiply two numbers.

Examples

>>> hl.eval(x * 2)
6
>>> hl.eval(x * y)
13.5
Parameters:

other (NumericExpression) – Number to multiply.

Returns:

NumericExpression – Product of the two numbers.

__ne__(other)

Returns True if the two expressions are not equal.

Examples

>>> x = hl.literal(5)
>>> y = hl.literal(5)
>>> z = hl.literal(1)
>>> hl.eval(x != y)
False
>>> hl.eval(x != z)
True

Notes

This method will fail with an error if the two expressions are not of comparable types.

Parameters:

other (Expression) – Expression for inequality comparison.

Returns:

BooleanExpressionTrue if the two expressions are not equal.

__neg__()

Negate the number (multiply by -1).

Examples

>>> hl.eval(-x)
-3
Returns:

NumericExpression – Negated number.

__or__(other)[source]

Return True if at least one of the left and right arguments is True.

Examples

>>> hl.eval(t | f)
True
>>> hl.eval(t | na)
True
>>> hl.eval(f | na)
None

The & and | operators have higher priority than comparison operators like ==, <, or >. Parentheses are often necessary:

>>> x = hl.literal(5)
>>> hl.eval((x < 10) | (x > 20))
True
Parameters:

other (BooleanExpression) – Right-side operand.

Returns:

BooleanExpressionTrue if either left or right is True.

__pow__(power, modulo=None)

Raise the left to the right power.

Examples

>>> hl.eval(x ** 2)
9.0
>>> hl.eval(x ** -2)
0.1111111111111111
>>> hl.eval(y ** 1.5)
9.545941546018392
Parameters:
Returns:

Expression of type tfloat64 – Result of raising left to the right power.

__sub__(other)

Subtract the right number from the left.

Examples

>>> hl.eval(x - 2)
1
>>> hl.eval(x - y)
-1.5
Parameters:

other (NumericExpression) – Number to subtract.

Returns:

NumericExpression – Difference of the two numbers.

__truediv__(other)

Divide two numbers.

Examples

>>> hl.eval(x / 2)
1.5
>>> hl.eval(y / 0.1)
45.0
Parameters:

other (NumericExpression) – Dividend.

Returns:

NumericExpression – The left number divided by the left.

collect(_localize=True)

Collect all records of an expression into a local list.

Examples

Collect all the values from C1:

>>> table1.C1.collect()
[2, 2, 10, 11]

Warning

Extremely experimental.

Warning

The list of records may be very large.

Returns:

list

describe(handler=<built-in function print>)

Print information about type, index, and dependencies.

property dtype

The data type of the expression.

Returns:

HailType

export(path, delimiter='\t', missing='NA', header=True)

Export a field to a text file.

Examples

>>> small_mt.GT.export('output/gt.tsv')
>>> with open('output/gt.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
locus   alleles 0       1       2       3
1:1     ["A","C"]       0/1     0/0     0/1     0/0
1:2     ["A","C"]       1/1     0/1     0/1     0/1
1:3     ["A","C"]       0/0     0/1     0/0     0/0
1:4     ["A","C"]       0/1     1/1     0/1     0/1
>>> small_mt.GT.export('output/gt-no-header.tsv', header=False)
>>> with open('output/gt-no-header.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
1:1     ["A","C"]       0/1     0/0     0/1     0/0
1:2     ["A","C"]       1/1     0/1     0/1     0/1
1:3     ["A","C"]       0/0     0/1     0/0     0/0
1:4     ["A","C"]       0/1     1/1     0/1     0/1
>>> small_mt.pop.export('output/pops.tsv')
>>> with open('output/pops.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
sample_idx      pop
0       1
1       2
2       2
3       2
>>> small_mt.ancestral_af.export('output/ancestral_af.tsv')
>>> with open('output/ancestral_af.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
locus   alleles ancestral_af
1:1     ["A","C"]       3.8152e-01
1:2     ["A","C"]       7.0588e-01
1:3     ["A","C"]       4.9991e-01
1:4     ["A","C"]       3.9616e-01
>>> small_mt.bn.export('output/bn.tsv')
>>> with open('output/bn.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
bn
{"n_populations":3,"n_samples":4,"n_variants":4,"n_partitions":4,"pop_dist":[1,1,1],"fst":[0.1,0.1,0.1],"mixture":false}

Notes

For entry-indexed expressions, if there is one column key field, the result of calling str() on that field is used as the column header. Otherwise, each compound column key is converted to JSON and used as a column header. For example:

>>> small_mt = small_mt.key_cols_by(s=small_mt.sample_idx, family='fam1')
>>> small_mt.GT.export('output/gt-no-header.tsv')
>>> with open('output/gt-no-header.tsv', 'r') as f:
...     for line in f:
...         print(line, end='')
locus   alleles {"s":0,"family":"fam1"} {"s":1,"family":"fam1"} {"s":2,"family":"fam1"} {"s":3,"family":"fam1"}
1:1     ["A","C"]       0/1     0/0     0/1     0/0
1:2     ["A","C"]       1/1     0/1     0/1     0/1
1:3     ["A","C"]       0/0     0/1     0/0     0/0
1:4     ["A","C"]       0/1     1/1     0/1     0/1
Parameters:
  • path (str) – The path to which to export.

  • delimiter (str) – The string for delimiting columns.

  • missing (str) – The string to output for missing values.

  • header (bool) – When True include a header line.

show(n=None, width=None, truncate=None, types=True, handler=None, n_rows=None, n_cols=None)

Print the first few records of the expression to the console.

If the expression refers to a value on a keyed axis of a table or matrix table, then the accompanying keys will be shown along with the records.

Examples

>>> table1.SEX.show()
+-------+-----+
|    ID | SEX |
+-------+-----+
| int32 | str |
+-------+-----+
|     1 | "M" |
|     2 | "M" |
|     3 | "F" |
|     4 | "F" |
+-------+-----+
>>> hl.literal(123).show()
+--------+
| <expr> |
+--------+
|  int32 |
+--------+
|    123 |
+--------+

Notes

The output can be passed piped to another output source using the handler argument:

>>> ht.foo.show(handler=lambda x: logging.info(x))  
Parameters:
  • n (int) – Maximum number of rows to show.

  • width (int) – Horizontal width at which to break columns.

  • truncate (int, optional) – Truncate each field to the given number of characters. If None, truncate fields to the given width.

  • types (bool) – Print an extra header line with the type of each field.

summarize(handler=None)

Compute and print summary information about the expression.

Danger

This functionality is experimental. It may not be tested as well as other parts of Hail and the interface is subject to change.

take(n, _localize=True)

Collect the first n records of an expression.

Examples

Take the first three rows:

>>> table1.X.take(3)
[5, 6, 7]

Warning

Extremely experimental.

Parameters:

n (int) – Number of records to take.

Returns:

list