alphagenome.data.genome.Interval#
- class alphagenome.data.genome.Interval(chromosome, start, end, strand='.', name='', info=<factory>)[source]#
Represents a genomic interval.
A genomic interval is a region on a chromosome defined by a start and end position. This class provides methods for manipulating and comparing intervals, and for calculating coverage and overlap.
- chromosome#
The chromosome name (e.g., ‘chr1’, ‘1’).
- start#
The 0-based start position.
- end#
The 0-based end position (must be greater than or equal to start).
- strand#
The strand of the interval (‘+’, ‘-’, or ‘.’). Defaults to ‘.’ (unstranded).
- name#
An optional name for the interval.
- info#
An optional dictionary to store additional information.
- negative_strand#
True if the interval is on the negative strand, False otherwise.
- width#
The width of the interval (end - start).
Attributes#
Table
Returns True if interval is on the negative strand, False otherwise. |
|
Returns the width of the interval. |
|
- Interval.negative_strand#
Returns True if interval is on the negative strand, False otherwise.
- Interval.width#
Returns the width of the interval.
Methods#
Table
Returns an unstranded copy of the interval. |
|
|
Boolean mask True if any interval overlaps the bin: coverage > 0. |
|
Boolean mask True if any interval overlaps the bin: coverage > 0. |
|
Extends or shrinks the interval by adjusting the positions with padding. |
|
Computes the center of the interval. |
|
Checks if this interval completely contains another interval. |
|
Returns a deep copy of the interval. |
|
Computes coverage track from sequence of intervals overlapping interval. |
|
Computes a coverage track from intervals overlapping this interval. |
|
Creates an Interval from a dictionary. |
|
Creates an Interval from a protobuf message. |
|
Creates an Interval from a pyranges-like dictionary. |
|
Creates an Interval from a string (e.g., 'chr1:100-200:+'). |
|
Returns the intersection of this interval with another interval. |
|
Returns overlapping ranges from intervals overlapping this interval. |
|
Checks if this interval overlaps with another interval. |
|
Pads the interval by adding the specified padding to the start and end. |
|
Pads the interval in place by adding padding to the start and end. |
|
Resizes the interval to a new width, centered around the original center. |
|
Resizes the interval in place, centered around the original center. |
|
Shifts the interval by the given offset. |
Swaps the strand of the interval. |
|
Converts the interval to a dictionary. |
|
|
Converts the interval to a protobuf message. |
Converts the interval to a pyranges-like dictionary. |
|
|
Truncates the interval to fit within the valid reference range. |
|
Checks if the interval is within the valid reference range. |
- Interval.binary_mask(intervals, bin_size=1)[source]#
Boolean mask True if any interval overlaps the bin: coverage > 0.
- Return type:
- Interval.binary_mask_stranded(intervals, bin_size=1)[source]#
Boolean mask True if any interval overlaps the bin: coverage > 0.
- Return type:
- Interval.boundary_shift(start_offset=0, end_offset=0, use_strand=True)[source]#
Extends or shrinks the interval by adjusting the positions with padding.
- Parameters:
- Return type:
Self
- Returns:
A new interval with adjusted boundaries.
- Interval.center(use_strand=True)[source]#
Computes the center of the interval.
For intervals with an odd width, the center is rounded down to the nearest integer.
If
use_strand
is True and the interval is on the negative strand, the center is calculated differently to maintain consistency when stacking sequences from different intervals oriented in the forward strand direction. This ensures that the relative distance between the interval’s upstream boundary and its center is preserved.- Parameters:
use_strand (
bool
(default:True
)) – If True, the strand of the interval is considered when calculating the center.- Return type:
- Returns:
The integer representing the center position of the interval.
Examples
>>> Interval('1', 1, 3, '+').center() 2 >>> Interval('1', 1, 3, '-').center() # Strand doesn't matter. 2 >>> Interval('1', 1, 4, '+').center() 3 >>> Interval('1', 1, 4, '-').center() 2 >>> Interval('1', 1, 4, '-').center() 2 >>> Interval('1', 1, 4, '+').center(use_strand=False) 3 >>> Interval('1', 1, 2, '-').center() 1
- Interval.contains(interval)[source]#
Checks if this interval completely contains another interval.
- Return type:
- Interval.coverage(intervals, *, bin_size=1)[source]#
Computes coverage track from sequence of intervals overlapping interval.
This method calculates the coverage of this interval by a set of other intervals. The coverage is defined as the number of intervals that overlap each position within this interval.
The
bin_size
parameter allows you to bin the coverage into equal-sized windows. This can be useful for summarizing coverage over larger regions. Ifbin_size
is 1, the coverage is calculated at single-base resolution.- Parameters:
- Return type:
- Returns:
A 1D numpy array representing the coverage track. The length of the array is
self.width // bin_size
. Each element in the array represents the summed coverage within the corresponding bin.- Raises:
ValueError – If
bin_size
is not a positive integer or if the interval width is not divisible bybin_size
.
- Interval.coverage_stranded(intervals, *, bin_size=1)[source]#
Computes a coverage track from intervals overlapping this interval.
This method considers the strand information of both self and intervals.
- Parameters:
- Returns:
, 0] represents coverage for intervals on the same strand as self and output[:, 1] represents coverage of intervals on the opposite strand.
- Return type:
Numpy array of shape (self.width // bin_size, 2) where output[
- classmethod Interval.from_interval_dict(interval)[source]#
Creates an Interval from a dictionary.
- Return type:
Self
- classmethod Interval.from_proto(proto)[source]#
Creates an Interval from a protobuf message.
- Return type:
Self
- classmethod Interval.from_pyranges_dict(row, ignore_info=False)[source]#
Creates an Interval from a pyranges-like dictionary.
This method constructs an
Interval
object from a dictionary that follows the pyranges format, such as a row from apandas.DataFrame
converted to a dict.The dictionary should have the following keys:
‘Chromosome’: The chromosome name.
‘Start’: The start position.
‘End’: The end position.
‘Strand’: The strand (optional, defaults to unstranded).
‘Name’: The interval name (optional).
Any other keys in the dictionary will be added to the
info
attribute of theInterval
object, unlessignore_info
is set to True.- Parameters:
- Return type:
- Returns:
An
Interval
object created from the input dictionary.
- classmethod Interval.from_str(string)[source]#
Creates an Interval from a string (e.g., ‘chr1:100-200:+’).
- Return type:
Self
- Interval.intersect(interval)[source]#
Returns the intersection of this interval with another interval.
- Return type:
Optional
[Self
]
- Interval.overlap_ranges(intervals)[source]#
Returns overlapping ranges from intervals overlapping this interval.
- Interval.overlaps(interval)[source]#
Checks if this interval overlaps with another interval.
- Return type:
- Interval.pad(start_pad, end_pad, *, use_strand=True)[source]#
Pads the interval by adding the specified padding to the start and end.
- Interval.pad_inplace(start_pad, end_pad, *, use_strand=True)[source]#
Pads the interval in place by adding padding to the start and end.
- Interval.resize(width, use_strand=True)[source]#
Resizes the interval to a new width, centered around the original center.
- Interval.resize_inplace(width, use_strand=True)[source]#
Resizes the interval in place, centered around the original center.