ranges

Range Objects

class Range()

A class representing a numerical range with start and end values. inclusive

__init__

def __init__(start, end)

Initializes a Range object.

Arguments:

  • start: The start value of the range (inclusive).
  • end: The end value of the range (inclusive).

shift

def shift(amount=0)

move the range by amount units, can be negative

Arguments:

  • amount: which way to move the range, if positive to the right if negative to the left

Returns:

self but moved

extend

def extend(start=0, end=0)

extend the range in either direction

Arguments:

  • start: how much to extend the start of the range (can be negative)
  • end: how much to extend the end of the range (can be negative)

Returns:

self but extended

overlaps

def overlaps(other, type="exact")

determine whether two ranges overlap

Arguments:

  • other: other Range to compare to
  • type: what kind of overlap to check for, options are: “exact”: ranges are exactly the same “within”: other is completely within self “start”: other starts within self “end”: other ends within self “any”: any overlap between the two ranges

Returns:

bool True or False depending on whether they overlap in the specified way

distance

def distance(other)

calculate the distance between two ranges if they overlap by any amount the distance is 0

Arguments:

  • other: other Range to compare to

split

def split(n)

Splits the range into n equal parts

Arguments:

  • n: number of parts to split the range into

Returns:

a RangesList of the split ranges

RangesList Objects

class RangesList()

a list of ranges, this is a single list, the items cannot be rangeslists themselves

__init__

def __init__(ranges)

constructor

Arguments:

  • ranges: a list of Range objects

pop

def pop(index)

remove and return item at index

Arguments:

  • index: index to remove if larger than length-1 will raise IndexError

Returns:

the item

insert

def insert(index, value)

insert value at index

Arguments:

  • index: index to insert at
  • value: range to insert

Returns:

self

append

def append(item)

add to the end

Arguments:

  • item: what to add

Returns:

self

extend

def extend(other)

extend by another RangesList

Arguments:

  • other: RangesList to extend by

Returns:

self

find_overlaps

def find_overlaps(other=None, type="exact", return_ranges=True)

find overlapping pair indices between two RangesLists, if other is none that means other is self

Arguments:

  • other: other rangeslist
  • type: what kind of overlap to check for, see Range.overlaps for options
  • return_ranges: whether to return a tuple of indices or a tuple of ranges

Returns:

a tuple of overlapping pairs ranges or indices

coverage

def coverage()

calculate coverage across all ranges in the RangesList, this means the number of ranges covering each position

Returns:

a list of coverage values, where the index corresponds to the position relative to the minimum start position 0 index corresponds to min start position, 1 index to min start + 1

reduce

def reduce()

reduce the RangesList to a single Range that covers all ranges in the list

Returns:

a range that covers all ranges in the list

RangesDict Objects

class RangesDict(dict)

__init__

def __init__(keys, values)

constructor

Arguments:

  • keys: list of strings
  • values: list of RangesList or Range objects

to_df

def to_df()

convert the RangesDict to a pandas DataFrame

0 columns: name, start, end

Returns:

a dataframe representation of the RangesDict

genomicranges

GenomicRange Objects

class GenomicRange()

Class representing a genomic range with chromosome, start, end, strand, and optional annotations.

__init__

def __init__(chrom, start, end, strand, annotation=None)

Initialize a GenomicRange object.

Arguments:

  • chrom: Chromosome name (string)
  • start: Genomic start (int)
  • end: Genomic end (int)
  • strand: Strand information (‘+’, ‘-‘, or ‘*’)
  • annotation: Optional annotation (string or dict) if string will be dict like {“annot”: annotation}

shift

def shift(amount)

Shift the genomic range by a specified amount.

extend

def extend(start, end)

Extend the genomic range by specified amounts at start and end.

overlaps

def overlaps(other, ignore_strand=False, type="any")

Check if this genomic range overlaps with another.

distance

def distance(other, ignore_strand=False)

Calculate the distance between this genomic range and another. if they overlap, distance is 0.

add_annotation

def add_annotation(key, value)

Add or update an annotation.

GenomicRangesList Objects

class GenomicRangesList()

__init__

def __init__(granges)

Initialize a GenomicRangesList object. this cannot be a nested list.

pop

def pop(index)

Remove and return item at index.

insert

def insert(index, value)

Insert a GenomicRange at a specific index.

append

def append(item)

Append a GenomicRange to the list.

extend

def extend(other)

Extend the list with another GenomicRangesList.

find_overlaps

def find_overlaps(other=None,
                  type="exact",
                  ignore_strand=False,
                  return_ranges=True)

Find overlaps between this GenomicRangesList and another.

Arguments:

  • other: other GenomicRangesList to compare with, if none, compares with self
  • type: what kind of overlap to look for, one of “exact”, “within”, “start”, “end”, “any”
  • ignore_strand: whether to ignore strand information when finding overlaps
  • return_ranges: whether to return the overlapping GenomicRange objects or their indices

Returns:

a list of tuples of overlapping ranges or their indices

coverage

def coverage(ignore_strand=False)

Calculate coverage depth at each position per chromosome and strand.

Arguments:

  • ignore_strand: If True, combines coverage from both strands

Returns:

Dictionary of chromosomes, each containing coverage arrays (either single array or separate arrays for + and - strands)

reduce

def reduce(ignore_strand=False)

Reduce overlapping or adjacent genomic ranges into minimal set of non-overlapping ranges. This will be done per chromosome

Arguments:

  • ignore_strand: whether to ignore strand information when reducing

GenomicRangesDict Objects

class GenomicRangesDict(dict)

Class representing a dictionary of genomic ranges or lists of genomic ranges.

__init__

def __init__(keys, values)

Initialize a GenomicRangesDict object.

to_df

def to_df()

Convert the GenomicRangesDict to a pandas DataFrame.