Skip to content

Commit

Permalink
Merge pull request cogent3#1815 from GavinHuttley/develop
Browse files Browse the repository at this point in the history
MAINT: SQL where clause function now handles numpy ints for start, stop
  • Loading branch information
GavinHuttley committed Apr 17, 2024
2 parents f83fd0b + c484a64 commit 4303bb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/cogent3/core/annotation_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def _matching_conditions(
sql.append(" AND ".join(conds))
vals = tuple(vals)

if isinstance(start, int) and isinstance(stop, int):
if start is not None and stop is not None:
if allow_partial:
# allow matches that overlap the segment
cond = [
Expand All @@ -347,11 +347,11 @@ def _matching_conditions(
# only matches within bounds
cond = f"start >= {start} AND stop <= {stop}"
sql.append(f"({cond})")
elif isinstance(start, int):
elif start is not None:
# if query has no stop, then any feature containing start
cond = f"(start <= {start} AND {start} < stop)"
sql.append(f"({cond})")
elif isinstance(stop, int):
elif stop is not None:
# if query has no start, then any feature containing stop
cond = f"(start <= {stop} AND {stop} < stop)"
sql.append(f"({cond})")
Expand Down
22 changes: 21 additions & 1 deletion tests/test_core/test_features.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase

import numpy
import pytest

from cogent3 import ASCII, DNA, get_moltype, make_aligned_seqs
Expand Down Expand Up @@ -135,7 +136,9 @@ def test_feature_residue():
db.add_feature(seqid="x", biotype="exon", name="ex1", spans=[(0, 4)])
aln_exons = list(aln.get_features(seqid="x", biotype="exon"))
assert "biotype='exon', name='ex1', map=[0:1, 2:5]/10" in str(aln_exons)
assert aln_exons[0].get_slice().to_dict() == dict(x="CCCC", y="----")
exon = aln_exons[0]
exon_seq = exon.get_slice()
assert exon_seq.to_dict() == dict(x="CCCC", y="----")
# Feature.as_one_span(), is applied to the exon that
# straddles the gap in x. The result is we preserve that feature.
exon_full_aln = aln_exons[0].as_one_span()
Expand Down Expand Up @@ -621,6 +624,21 @@ def test_feature_out_range():
assert not f


@pytest.mark.parametrize("cast", (list, numpy.array))
def test_search_with_ints(cast):
"""searching for features with numpy ints should work"""
start, stop = cast([2, 5])
seq = DNA.make_seq("AAAGGGGGAACCCT", name="x")
db = GffAnnotationDb()
db.add_feature(seqid="x", biotype="exon", name="E1", spans=[(3, 8)])
db.add_feature(seqid="x", biotype="exon", name="E2", spans=[(10, 13)])
seq.annotation_db = db
feats = list(
seq.get_features(biotype="exon", allow_partial=True, start=start, stop=stop)
)
assert len(feats) == 1


def test_roundtripped_alignment_with_slices():
"""Sliced Alignment with annotations roundtrips correctly"""
# annotations just on member sequences
Expand All @@ -633,6 +651,8 @@ def test_roundtripped_alignment_with_slices():
aln.annotation_db = db
# at the alignment level
sub_aln = aln[:-3]
feats = list(sub_aln.get_features(biotype="exon", allow_partial=True))
assert len(feats) == 2
new = deserialise_object(sub_aln.to_json())
gf1, gf2 = list(new.get_features(biotype="exon", allow_partial=True))
assert gf1.get_slice().to_dict() == {"x": "GGGGG", "y": "--TTT"}
Expand Down

0 comments on commit 4303bb1

Please sign in to comment.