# IntervalExpression¶

class hail.expr.IntervalExpression[source]

Bases: hail.expr.expressions.base_expression.Expression

Expression of type tinterval.

>>> interval = hl.interval(3, 11)
>>> locus_interval = hl.parse_locus_interval("1:53242-90543")


Attributes

 dtype The data type of the expression. end Returns the end point. includes_end True if the interval includes the end point. includes_start True if the interval includes the start point. start Returns the start point.

Methods

 __init__ Initialize self. collect Collect all records of an expression into a local list. contains Tests whether a value is contained in the interval. describe Print information about type, index, and dependencies. export Export a field to a text file. overlaps True if the the supplied interval contains any value in common with this one. show Print the first few rows of the table to the console. summarize Compute and print summary information about the expression. take Collect the first n records of an expression.
__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. BooleanExpression – True if the two expressions are equal.
__ge__(other)

Return self>=value.

__gt__(other)

Return self>value.

__le__(other)

Return self<=value.

__lt__(other)

Return self<value.

__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. BooleanExpression – True if the two expressions are not equal.
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
contains(value)[source]

Tests whether a value is contained in the interval.

Examples

>>> hl.eval(interval.contains(3))
True

>>> hl.eval(interval.contains(11))
False

Parameters: value – Object with type matching the interval point type. BooleanExpression – True if value is contained in the interval, False otherwise.
describe(handler=<built-in function print>)

Print information about type, index, and dependencies.

dtype

The data type of the expression.

Returns: HailType
end

Returns the end point.

Examples

>>> hl.eval(interval.end)
11

Returns: Expression
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/1     0/0     0/0
1:2     ["A","C"]       1/1     0/1     1/1     1/1
1:3     ["A","C"]       1/1     0/1     0/1     0/0
1:4     ["A","C"]       1/1     0/1     1/1     1/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/1     0/0     0/0
1:2     ["A","C"]       1/1     0/1     1/1     1/1
1:3     ["A","C"]       1/1     0/1     0/1     0/0
1:4     ["A","C"]       1/1     0/1     1/1     1/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       2
1       2
2       0
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"]       5.3905e-01
1:2     ["A","C"]       8.6768e-01
1:3     ["A","C"]       4.3765e-01
1:4     ["A","C"]       7.6300e-01

>>> mt = small_mt
>>> 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":8,"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 hl.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')
>>> 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/1     0/0     0/0
1:2     ["A","C"]       1/1     0/1     1/1     1/1
1:3     ["A","C"]       1/1     0/1     0/1     0/0
1:4     ["A","C"]       1/1     0/1     1/1     1/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.
includes_end

True if the interval includes the end point.

Examples

>>> hl.eval(interval.includes_end)
False

Returns: BooleanExpression
includes_start

True if the interval includes the start point.

Examples

>>> hl.eval(interval.includes_start)
True

Returns: BooleanExpression
overlaps(interval)[source]

True if the the supplied interval contains any value in common with this one.

Examples

>>> hl.eval(interval.overlaps(hl.interval(5, 9)))
True

>>> hl.eval(interval.overlaps(hl.interval(11, 20)))
False

Parameters: interval (Expression with type tinterval) – Interval object with the same point type. BooleanExpression
show(n=None, width=None, truncate=None, types=True, handler=None, n_rows=None, n_cols=None)

Print the first few rows of the table to the console.

Examples

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

>>> hl.literal(123).show()
+--------+
| <expr> |
+--------+
|  int32 |
+--------+
|    123 |
+--------+


Warning

Extremely experimental.

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.
start

Returns the start point.

Examples

>>> hl.eval(interval.start)
3

Returns: Expression
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. list