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

feat!: use stronger type claims #18

Merged
merged 2 commits into from
Jan 19, 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
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ chainfile = "0.2.1"
directories = "5.0"

[dependencies.pyo3]
version = "0.20.0"
version = "0.20.2"
features = ["abi3-py38"]
23 changes: 19 additions & 4 deletions src/chainlifter/lifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,30 @@ def convert_coordinate(
:return: list of coordinate matches (possibly empty)
"""
try:
result = self._chainlifter.lift(chrom, pos, strand)
results = self._chainlifter.lift(chrom, pos, strand)
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to update the return type?

Copy link
Member Author

Choose a reason for hiding this comment

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

...yes...

except _core.NoLiftoverError:
result = []
results = []
except _core.ChainfileError:
_logger.error(
"Encountered internal error while converting coordinates - is the chainfile invalid? (%s, %s, %s)",
chrom,
pos,
strand,
)
result = []
return result
results = []
if results:
formatted_results = []
for result in results:
try:
pos = int(result[1])
except ValueError:
_logger.error("Got invalid position value in %s", result)
continue
try:
strand = Strand(result[2])
except ValueError:
_logger.error("Got invalid Strand value in %s", result)
continue
formatted_results.append((result[0], pos, strand))
results = formatted_results
return results
18 changes: 9 additions & 9 deletions tests/test_liftover.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Run liftover tests."""
from chainlifter.lifter import ChainLifter, Genome
from chainlifter.lifter import ChainLifter, Genome, Strand


def test_hg19_to_hg38():
Expand All @@ -8,37 +8,37 @@ def test_hg19_to_hg38():

result = ch.convert_coordinate("chr7", 140439611)
assert len(result) == 1
assert result[0] == ["chr7", "140739811", "+"]
assert result[0] == ("chr7", 140739811, Strand.POSITIVE)

result = ch.convert_coordinate("chr7", 140439746)
assert len(result) == 1
assert result[0] == ["chr7", "140739946", "+"]
assert result[0] == ("chr7", 140739946, Strand.POSITIVE)

result = ch.convert_coordinate("chr7", 140439703)
assert len(result) == 1
assert result[0] == ["chr7", "140739903", "+"]
assert result[0] == ("chr7", 140739903, Strand.POSITIVE)

result = ch.convert_coordinate("chr7", 140453136)
assert len(result) == 1
assert result[0] == ["chr7", "140753336", "+"]
assert result[0] == ("chr7", 140753336, Strand.POSITIVE)

# coordinate exceeds bounds
result = ch.convert_coordinate("chr7", 14040053136)
assert result == []


def test_hg38_to_hg19():
"""Test hg38 to hg19 lifter."""
"Test hg38 to hg19 lifter." ""
ch = ChainLifter(Genome.HG38, Genome.HG19)

result = ch.convert_coordinate("chr7", 140739811)
assert len(result) == 1
assert result[0] == ["chr7", "140439611", "+"]
assert result[0] == ("chr7", 140439611, Strand.POSITIVE)

result = ch.convert_coordinate("chr7", 140759820)
assert len(result) == 1
assert result[0] == ["chr7", "140459620", "+"]
assert result[0] == ("chr7", 140459620, Strand.POSITIVE)

result = ch.convert_coordinate("chr7", 60878240)
assert len(result) == 1
assert result[0] == ["chr7", "61646115", "+"]
assert result[0] == ("chr7", 61646115, Strand.POSITIVE)