Skip to content

gff.get_annotation(): entries with the same ID are only merged if adjacent #1060

Backlog

Description

@padix-key

Description

get_annotation() of biotite.sequence.io.gff documents that "multiple entries with the same ID attribute are interpreted as the same feature". But it only merges entries that are adjacent. The GFF3 specification does not require this ("All lines that share an ID must collectively represent a single feature"), and common files interleave them, e.g. Ensembl GFF3 alternates exon and CDS lines of a transcript. Such a CDS is split into one feature per segment.

Minimal example

import io
import biotite.sequence.io.gff as gff

# Ensembl-style ordering: exon and CDS lines are interleaved
file = gff.GFFFile.read(io.StringIO(
    "##gff-version 3\n"
    "chr1\t.\texon\t1\t20\t.\t+\t.\tParent=mRNA1\n"
    "chr1\t.\tCDS\t11\t20\t.\t+\t0\tID=cds1;Parent=mRNA1\n"
    "chr1\t.\texon\t31\t50\t.\t+\t.\tParent=mRNA1\n"
    "chr1\t.\tCDS\t31\t40\t.\t+\t2\tID=cds1;Parent=mRNA1\n"
))
for feature in sorted(gff.get_annotation(file)):
    print(feature.key, sorted((loc.first, loc.last) for loc in feature.locs))

Actual:

exon [(1, 20)]
CDS [(11, 20)]
exon [(31, 50)]
CDS [(31, 40)]

Expected:

exon [(1, 20)]
CDS [(11, 20), (31, 40)]
exon [(31, 50)]

Proposed fix

Collect the locations per ID in a dictionary instead of only comparing with the previous entry:

--- a/src/biotite/sequence/io/gff/convert.py
+++ b/src/biotite/sequence/io/gff/convert.py
@@ def get_annotation(gff_file: GFFFile) -> Annotation:
-    annot = Annotation()
-    current_key: str | None = None
-    current_locs: list[Location] = []
-    current_qual: dict[str, str | None] = {}
-    current_id = None
-    for record in gff_file:
+    # Entries with the same ID belong to the same feature,
+    # even if they are not adjacent
+    features: dict[str | int, tuple[str, list[Location], dict[str, str | None]]] = {}
+    for i, record in enumerate(gff_file):
         strand = record.strand
         if strand is None:
             strand = Location.Strand.FORWARD
+        loc = Location(record.start, record.end, strand)
         id = record.attributes.get("ID")
-        if id != current_id or id is None:
-            # current_key is None, when there is no previous feature
-            # (beginning of the file)
-            if current_key is not None:
-                # Beginning of new feature -> Save previous feature
-                annot.add_feature(Feature(current_key, current_locs, current_qual))
-            # Track new feature
-            current_key = record.type
-            current_locs = [Location(record.start, record.end, strand)]
-            current_qual = dict(record.attributes)
+        # Entries without ID are always separate features
+        feature_id = i if id is None else id
+        if feature_id in features:
+            features[feature_id][1].append(loc)
         else:
-            current_locs.append(Location(record.start, record.end, strand))
-        current_id = id
-    # Save last feature
-    if current_key is not None:
-        annot.add_feature(Feature(current_key, current_locs, current_qual))
+            features[feature_id] = (record.type, [loc], dict(record.attributes))
+    annot = Annotation(
+        [Feature(key, locs, qual) for key, locs, qual in features.values()]
+    )
     return annot

Authored by Claude

Activity

  1. padix-key commented on Nov 26, 2026

    @padix-key
    MemberAuthor

    Triage: high priority.

    A single multi-part CDS may be interleaved with exon or other feature records in normal GFF3 files. Treating each nonadjacent part as a separate feature breaks the annotated gene structure and subsequent sequence extraction without rejecting the input. This affects standard annotation layouts, so it should be fixed before less common formatting edge cases.

    Authored by Codex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions