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

No more non-unique column names, or non-queryable columns in list_columns #70

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 11 additions & 6 deletions src/genomic_features/ensembl/ensembldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

PKG_CACHE_DIR = "genomic-features"

BIOC_ANNOTATION_HUB_URL = (
"https://bioconductorhubs.blob.core.windows.net/annotationhub"
)
BIOC_ANNOTATION_HUB_URL = "https://bioconductorhubs.blob.core.windows.net/annotationhub"
ANNOTATION_HUB_URL = (
"https://annotationhub.bioconductor.org/metadata/annotationhub.sqlite3"
)
Expand Down Expand Up @@ -53,7 +51,7 @@ def annotation(
"""
try:
sqlite_file_path = retrieve_annotation(
f'{BIOC_ANNOTATION_HUB_URL}/AHEnsDbs/v{version}/EnsDb.{species}.v{version}.sqlite'
f"{BIOC_ANNOTATION_HUB_URL}/AHEnsDbs/v{version}/EnsDb.{species}.v{version}.sqlite"
)

if backend == "sqlite":
Expand Down Expand Up @@ -440,12 +438,19 @@ def _get_required_tables(self, tab) -> list:
return self._tables_by_degree(tab)

def list_columns(self, tables: str | list[str] | None = None) -> list[str]:
"""List all columns available in the genomic features table."""
"""List queryable columns available in these tables."""
if tables is None:
tables = self.db.list_tables() # list of table names
if "metadata" in tables:
tables.remove("metadata")
elif isinstance(tables, str):
tables = [tables] # list of tables names (only one)
columns = [c for t in tables for c in self.db.table(t).columns]

Copy link
Contributor

@gamazeps gamazeps Apr 8, 2024

Choose a reason for hiding this comment

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

columns = [c for t in tables for c in self.db.table(t).columns]
return list(sorted(set(columns)))

is shorter and more readable no ?

It even works with the current tests

Copy link
Member Author

@ivirshup ivirshup Apr 9, 2024

Choose a reason for hiding this comment

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

I think things are grouped a little more logically when it's ordered by table:

Example
In [2]: ensdb = gf.ensembl.annotation("Hsapiens", 108)

In [3]: ensdb.list_columns()
Out[3]: 
['gene_id',
 'gene_name',
 'gene_biotype',
 'gene_seq_start',
 'gene_seq_end',
 'seq_name',
 'seq_strand',
 'seq_coord_system',
 'description',
 'gene_id_version',
 'canonical_transcript',
 'tx_id',
 'tx_biotype',
 'tx_seq_start',
 'tx_seq_end',
 'tx_cds_seq_start',
 'tx_cds_seq_end',
 'tx_support_level',
 'tx_id_version',
 'gc_content',
 'tx_external_name',
 'tx_is_canonical',
 'exon_id',
 'exon_idx',
 'exon_seq_start',
 'exon_seq_end',
 'seq_length',
 'is_circular',
 'protein_id',
 'protein_sequence',
 'uniprot_id',
 'uniprot_db',
 'uniprot_mapping_type',
 'protein_domain_id',
 'protein_domain_source',
 'interpro_accession',
 'prot_dom_start',
 'prot_dom_end',
 'entrezid']

In [4]: sorted(ensdb.list_columns())
Out[4]: 
['canonical_transcript',
 'description',
 'entrezid',
 'exon_id',
 'exon_idx',
 'exon_seq_end',
 'exon_seq_start',
 'gc_content',
 'gene_biotype',
 'gene_id',
 'gene_id_version',
 'gene_name',
 'gene_seq_end',
 'gene_seq_start',
 'interpro_accession',
 'is_circular',
 'prot_dom_end',
 'prot_dom_start',
 'protein_domain_id',
 'protein_domain_source',
 'protein_id',
 'protein_sequence',
 'seq_coord_system',
 'seq_length',
 'seq_name',
 'seq_strand',
 'tx_biotype',
 'tx_cds_seq_end',
 'tx_cds_seq_start',
 'tx_external_name',
 'tx_id',
 'tx_id_version',
 'tx_is_canonical',
 'tx_seq_end',
 'tx_seq_start',
 'tx_support_level',
 'uniprot_db',
 'uniprot_id',
 'uniprot_mapping_type']

columns = []
for t in tables:
for c in self.db.table(t).columns:
if c not in columns:
columns.append(c)
return columns

def _clean_columns(self, columns: list[str]) -> list[str]:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,20 @@ def test_chromosome_columns(hsapiens108):
.reset_index(drop=True)
)
pd.testing.assert_series_equal(result["seq_length"], expected_lengths)


def test_list_columns_uniqueness(hsapiens108):
# https://github.com/scverse/genomic-features/issues/42
cols = hsapiens108.list_columns()
assert len(cols) == len(set(cols))

cols = hsapiens108.list_columns(["gene", "tx"])
assert len(cols) == len(set(cols))


def test_list_columns_include_unqueryable_cols(hsapiens108):
# https://github.com/scverse/genomic-features/issues/42
cols = hsapiens108.list_columns()
# From metadata
assert "value" not in cols
assert "name" not in cols
Loading