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 all 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
Expand Up @@ -7,6 +7,7 @@
from typing import Final, Literal

import ibis
import numpy as np
import requests
from ibis import deferred
from ibis.expr.types import Table as IbisTable
Expand Down Expand Up @@ -269,6 +270,56 @@ def chromosomes(self) -> DataFrame:
"""
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=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

def _build_query(
self,
table: Literal["gene", "tx", "exon"],
Expand Down
19 changes: 19 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,22 @@ def test_seqs_as_int(hsapiens108):

pd.testing.assert_frame_equal(result_w_ints, result_w_strs)
pd.testing.assert_frame_equal(result_w_ints, result_w_mixed)


def test_promoters(hsapiens108):
promoters = hsapiens108.promoters()
assert isinstance(promoters, pd.DataFrame)
promoters = hsapiens108.promoters(upstream=100, downstream=100)
assert ((promoters.promoter_seq_end - promoters.promoter_seq_start) == 200).all()
promoters = hsapiens108.promoters(upstream=1000, downstream=100)
assert ((promoters.promoter_seq_end - promoters.promoter_seq_start) == 1100).all()
# Test strandedness
promoters = hsapiens108.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()
Loading