Skip to content

Commit

Permalink
Move store_gene_annotations and test
Browse files Browse the repository at this point in the history
  • Loading branch information
MatBarba committed Feb 29, 2024
1 parent 020add2 commit 71b7ff5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/python/ensembl/io/genomio/gff3/extract_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,18 @@ def to_json(self, out_path: PathLike) -> None:
self.transfer_descriptions()
feats_list = self._to_list()
print_json(Path(out_path), feats_list)

def store_gene(self, gene: SeqFeature) -> None:
"""Record the functional_annotations of a gene and its children features."""
self.add_feature(gene, "gene")

cds_found = False
for transcript in gene.sub_features:
self.add_feature(transcript, "transcript", gene.id)
for feat in transcript.sub_features:
if feat.type != "CDS":
continue
# Store CDS functional annotation only once
if not cds_found:
cds_found = True
self.add_feature(feat, "translation", transcript.id)
17 changes: 1 addition & 16 deletions src/python/ensembl/io/genomio/gff3/simplifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,9 @@ def simpler_gff3_feature(self, feat: SeqFeature) -> Optional[SeqFeature]:

# Normalize, store annotation, and return the cleaned up gene
gene = self.normalize_gene(gene)
self.store_gene_annotations(gene)
self.annotations.store_gene(gene)
return self.clean_gene(gene)

def store_gene_annotations(self, gene: SeqFeature) -> None:
"""Record the functional_annotations of the gene and its children features."""
self.annotations.add_feature(gene, "gene")

cds_found = False
for transcript in gene.sub_features:
self.annotations.add_feature(transcript, "transcript", gene.id)
for feat in transcript.sub_features:
if feat.type != "CDS":
continue
# Store CDS functional annotation only once
if not cds_found:
cds_found = True
self.annotations.add_feature(feat, "translation", transcript.id)

def clean_gene(self, gene: SeqFeature) -> SeqFeature:
"""Return the same gene without qualifiers unrelated to the gene structure."""

Expand Down
29 changes: 29 additions & 0 deletions src/python/tests/gff3/test_extract_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,32 @@ def test_transfer_descriptions(
transcs = annot.get_features("transcript")
assert genes[gene_name].get("description") == out_gene_desc
assert transcs[transcript_name].get("description") == out_transc_desc


@pytest.mark.dependency(depends=["add_feature"])
@pytest.mark.parametrize(
"with_cds, num_genes, num_tr, num_cds",
[
pytest.param(False, 1, 1, 0, id="Store gene without CDS"),
pytest.param(True, 1, 1, 1, id="Store gene with CDS"),
],
)
def test_store_gene(with_cds: bool, num_genes: int, num_tr: int, num_cds: int) -> None:
"""Test store_gene given a gene Feature with transcripts and optional translations.
."""
annot = FunctionalAnnotations()
gene_name = "gene_A"
transcript_name = "tran_A"
one_gene = SeqFeature(type="gene", id=gene_name)
one_gene.sub_features = []
one_transcript = SeqFeature(type="mRNA", id=transcript_name)
one_transcript.sub_features = []
if with_cds:
one_translation = SeqFeature(type="CDS", id="cds_A")
one_transcript.sub_features.append(one_translation)
one_gene.sub_features.append(one_transcript)

annot.store_gene(one_gene)
assert len(annot.features["gene"]) == num_genes
assert len(annot.features["transcript"]) == num_tr
assert len(annot.features["translation"]) == num_cds

0 comments on commit 71b7ff5

Please sign in to comment.