Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get table of promoter region #34

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/genomic_features/ensembl/ensembldb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import ibis
import numpy as np
import requests
from ibis import _
from pandas import DataFrame, Timestamp
Expand Down Expand Up @@ -127,3 +128,53 @@ def genes(
def chromosomes(self):
"""Get chromosome information."""
return self.db.table("chromosome").execute()

def promoters(
self,
filter: _filters.AbstractFilterExpr = filters.EmptyFilter(),
upstream: int = 2000,
downstream: int = 200,
canonical_transcripts: bool = False,
) -> DataFrame:
"""Get promoter annotations.

Parameters
----------
filter
Filter expression to apply to the genes table.
upstream
Number of base pairs upstream of the TSS (default: 2000).
downstream
Number of base pairs downstream of the TSS (default: 200).
canonical_transcripts
If True, return only canonical transcript for each gene (default: False).

Returns
-------
DataFrame
A table of promoter annotations.
"""
# TODO: change to get transcript table with gene level columns
# something like:
# tx_table = self.transcripts(cols = set(cols + ['seq_strand', 'seq_name', 'tx_is_canonical']), filter = filter)
tx_table = self.genes(filter)

# Get promoter region based on strand
# strand = 1 |>>>>>>>>>>>>>>|
# strand = -1 |<<<<<<<<<<<<<<|
# Tx SS: * *
# Promoter ------ ------
tx_ss = np.where(
tx_table["seq_strand"] == 1,
tx_table["gene_seq_start"],
tx_table["gene_seq_end"],
)
tx_table["promoter_seq_start"] = np.where(
tx_table["seq_strand"] == 1, tx_ss - upstream, tx_ss - downstream
)
tx_table["promoter_seq_end"] = np.where(
tx_table["seq_strand"] == 1, tx_ss + downstream, tx_ss + upstream
)
Comment on lines +308 to +318
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Would you be up for PR-ing this into bioframe as well? open2c/bioframe#144

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted in open2c/bioframe#152

# if canonical_transcripts:
# tx_table = tx_table[tx_table["tx_is_canonical"] == 1]
return tx_table
25 changes: 25 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,28 @@ def test_genes():
def test_missing_version():
with pytest.raises(ValueError):
gf.ensembl.annotation("Hsapiens", 86)


def test_promoters():
promoters = gf.ensembl.annotation("Hsapiens", 108).promoters()
assert isinstance(promoters, pd.DataFrame)
promoters = gf.ensembl.annotation("Hsapiens", 108).promoters(
upstream=100, downstream=100
)
assert ((promoters.promoter_seq_end - promoters.promoter_seq_start) == 200).all()
promoters = gf.ensembl.annotation("Hsapiens", 108).promoters(
upstream=1000, downstream=100
)
assert ((promoters.promoter_seq_end - promoters.promoter_seq_start) == 1100).all()
# Test strandedness
promoters = gf.ensembl.annotation("Hsapiens", 108).promoters(
upstream=1000, downstream=100
)
assert (
promoters[promoters.seq_strand == -1].promoter_seq_start
== promoters[promoters.seq_strand == -1].gene_seq_end - 100
).all()
assert (
promoters[promoters.seq_strand == 1].promoter_seq_start
== promoters[promoters.seq_strand == 1].gene_seq_start - 1000
).all()