Skip to main content

Project generated with PyScaffold PyPI-Server Unit tests

GenomicRanges

GenomicRanges provides container classes designed to represent genomic locations and support genomic analysis. It is similar to Bioconductor's GenomicRanges. Intervals are inclusive on both ends and starts at 1.

Note: V0.4.0 is a complete overhaul of the package, as such the constructor to GenomicRanges has changed!

To get started, install the package from PyPI

pip install genomicranges

GenomicRanges

GenomicRanges is the base class to represent and operate over genomic regions and annotations.

From UCSC or GTF file

You can easily download and parse genome annotations from UCSC or load a genome annotation from a GTF file,

import genomicranges

gr = genomicranges.read_gtf(<PATH TO GTF>)
# OR
gr = genomicranges.read_ucsc(genome="hg19")

print(gr)
## output
## GenomicRanges with 1760959 intervals & 10 metadata columns.
## ... truncating the console print ...

from IRanges (Preferred way)

If you have all relevant information to create a GenomicRanges object

from genomicranges import GenomicRanges
from iranges import IRanges
from biocframe import BiocFrame
from random import random

gr = GenomicRanges(
    seqnames=[
        "chr1",
        "chr2",
        "chr3",
        "chr2",
        "chr3",
    ],
    ranges=IRanges(start=[x for x in range(101, 106)], width=[11, 21, 25, 30, 5]),
    strand=["*", "-", "*", "+", "-"],
    mcols=BiocFrame(
        {
            "score": range(0, 5),
            "GC": [random() for _ in range(5)],
        }
    ),
)

print(gr)
## output
GenomicRanges with 5 ranges and 5 metadata columns
    seqnames    ranges           strand     score                  GC
       <str> <IRanges> <ndarray[int64]>   <range>              <list>
[0]     chr1 101 - 112                * |       0  0.2593301003406461
[1]     chr2 102 - 123                - |       1  0.7207993213776644
[2]     chr3 103 - 128                * |       2 0.23391468067222065
[3]     chr2 104 - 134                + |       3  0.7671026589720187
[4]     chr3 105 - 110                - |       4 0.03355777784472458
------
seqinfo(3 sequences): chr1 chr2 chr3

Pandas DataFrame

A common representation in Python is a pandas DataFrame for all tabular datasets. DataFrame must contain columns "seqnames", "starts", and "ends" to represent genomic intervals. Here's an example:

import genomicranges import GenomicRanges
import pandas as pd

df = pd.DataFrame(
    {
        "seqnames": ["chr1", "chr2", "chr1", "chr3", "chr2"],
        "starts": [101, 102, 103, 104, 109],
        "ends": [112, 103, 128, 134, 111],
        "strand": ["*", "-", "*", "+", "-"],
        "score": range(0, 5),
        "GC": [random() for _ in range(5)],
    }
)

gr = GenomicRanges.from_pandas(df)
print(gr)
## output
GenomicRanges with 5 ranges and 5 metadata columns
  seqnames    ranges           strand    score                  GC
     <str> <IRanges> <ndarray[int64]>   <list>              <list>
0     chr1 101 - 112                * |      0  0.4862658925128007
1     chr2 102 - 103                - |      1 0.27948386889389953
2     chr1 103 - 128                * |      2  0.5162697718607901
3     chr3 104 - 134                + |      3  0.5979843806415466
4     chr2 109 - 111                - |      4 0.04740781186083798
------
seqinfo(3 sequences): chr1 chr2 chr3

Interval Operations

GenomicRanges supports most interval based operations.

subject = genomicranges.from_ucsc(genome="hg38")

query = genomicranges.from_pandas(
    pd.DataFrame(
        {
            "seqnames": ["chr1", "chr2", "chr3"],
            "starts": [100, 115, 119],
            "ends": [103, 116, 120],
        }
    )
)

hits = subject.nearest(query, ignore_strand=True)
print(hits)
## output
[[0, 1], [1677082, 1677083, 1677084], [1003411, 1003412]]

GenomicRangesList

Just as it sounds, a GenomicRangesList is a named-list like object. If you are wondering why you need this class, a GenomicRanges object lets us specify multiple genomic elements, usually where the genes start and end. Genes are themselves made of many sub-regions, e.g. exons. GenomicRangesList allows us to represent this nested structure.

Currently, this class is limited in functionality.

To construct a GenomicRangesList

gr1 = GenomicRanges(
    seqnames=["chr1", "chr2", "chr1", "chr3"],
    ranges=IRanges([1, 3, 2, 4], [10, 30, 50, 60]),
    strand=["-", "+", "*", "+"],
    mcols=BiocFrame({"score": [1, 2, 3, 4]}),
)

gr2 = GenomicRanges(
    seqnames=["chr2", "chr4", "chr5"],
    ranges=IRanges([3, 6, 4], [30, 50, 60]),
    strand=["-", "+", "*"],
    mcols=BiocFrame({"score": [2, 3, 4]}),
)
grl = GenomicRangesList(ranges=[gr1, gr2], names=["gene1", "gene2"])
print(grl)
## output
GenomicRangesList with 2 ranges and 2 metadata columns

Name: gene1
GenomicRanges with 4 ranges and 4 metadata columns
    seqnames    ranges           strand    score
       <str> <IRanges> <ndarray[int64]>   <list>
[0]     chr1    1 - 11                - |      1
[1]     chr2    3 - 33                + |      2
[2]     chr1    2 - 52                * |      3
[3]     chr3    4 - 64                + |      4
------
seqinfo(3 sequences): chr1 chr2 chr3

Name: gene2
GenomicRanges with 3 ranges and 3 metadata columns
    seqnames    ranges           strand    score
       <str> <IRanges> <ndarray[int64]>   <list>
[0]     chr2    3 - 33                - |      2
[1]     chr4    6 - 56                + |      3
[2]     chr5    4 - 64                * |      4
------
seqinfo(3 sequences): chr2 chr4 chr5

Further information

Note

This project has been set up using PyScaffold 4.1.1. For details and usage information on PyScaffold see https://pyscaffold.org/.

Release files for genomicranges 0.4.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for genomicranges 0.4.12
File Size Uploaded
GenomicRanges-0.4.12.tar.gz 60.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for genomicranges 0.4.12
File Interpreter ABI Platform
GenomicRanges-0.4.12-py3-none-any.whl Python 3 none any Details

Total release size: 95.2 kB

Release files / GenomicRanges-0.4.12.tar.gz

Download URL GenomicRanges-0.4.12.tar.gz
Size 60.7 kB
Tags Source
SHA-256 checksum
How to use checksums
18a26c3f443528d7801aa0f41e201788eaf848800676c8b3a0f064c56f8d2141
BLAKE2b-256 checksum
How to use checksums
9134eb04d9b2f365eb0ad971a6d0f6deecf803f2dda7576ace544222a416a939
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / GenomicRanges-0.4.12-py3-none-any.whl

Download URL GenomicRanges-0.4.12-py3-none-any.whl
Size 34.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4398d5f3de16eff08301d979957d195e9a479f82f9b5e00285b031783e6f1fff
BLAKE2b-256 checksum
How to use checksums
6f1f04e172ebd36d6aefa03253cd8a38eba8897e9788d091d1d3332a3581a6b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

0.9.0

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.34

2 release files

0.4.33

2 release files

0.4.32

2 release files

0.4.30

2 release files

0.4.29

2 release files

0.4.28

2 release files

0.4.27

2 release files

0.4.26

2 release files

0.4.25

2 release files

0.4.23

2 release files

0.4.22

2 release files

0.4.21

2 release files

0.4.20

2 release files

0.4.19

2 release files

0.4.18

2 release files

0.4.17

2 release files

0.4.16

2 release files

0.4.15

2 release files

0.4.14

2 release files

0.4.13

2 release files

This release

0.4.12 This release

2 release files

0.4.11

2 release files

0.4.10

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.11

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page