aboutsummaryrefslogtreecommitdiff
path: root/gn3/api
diff options
context:
space:
mode:
authorArun Isaac2023-01-24 18:17:57 +0000
committerArun Isaac2023-01-24 18:30:05 +0000
commite5bcdbc83031c10092f1405b847695ecefbada44 (patch)
tree6660629c849b38c3bc2344dfd7127b3acca8af26 /gn3/api
parentd33454f3e7fb7810fd4b7a987e6a68b3c48ec98e (diff)
downloadgenenetwork3-e5bcdbc83031c10092f1405b847695ecefbada44.tar.gz
search: Abstract out parsing of position spec into separate function.
* gn3/api/search.py (parse_position): New function. (parse_location_field): Use parse_position. * tests/unit/test_search.py: Import parse_position from gn3.api.search. (test_parse_position_close_to_zero_location): New test.
Diffstat (limited to 'gn3/api')
-rw-r--r--gn3/api/search.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/gn3/api/search.py b/gn3/api/search.py
index 0d3f9ba..e497743 100644
--- a/gn3/api/search.py
+++ b/gn3/api/search.py
@@ -135,6 +135,19 @@ def apply_si_suffix(location: str) -> int:
return int(location)
+def parse_position(spec: str) -> tuple[Maybe[int], Maybe[int]]:
+ """Parse position specifiation converting point locations to ranges."""
+ # Range
+ if ".." in spec:
+ return tuple(limit.map(apply_si_suffix) # type: ignore
+ for limit in parse_range(spec))
+ # If point location, assume +/- 50 kbases on either side.
+ else:
+ width = 50*10**3
+ point = apply_si_suffix(spec)
+ return Just(max(0, point - width)), Just(point + width)
+
+
def parse_location_field(species_query: xapian.Query,
chromosome_prefix: str, location_slot: int,
liftover_function: IntervalLiftoverFunction,
@@ -146,19 +159,11 @@ def parse_location_field(species_query: xapian.Query,
"""
def split_query(query: str) -> ChromosomalInterval:
"""Split query into chromosome and location tuple."""
- chromosome, location_str = query.lower().split(":")
+ chromosome, position_spec = query.lower().split(":")
if not chromosome.startswith("chr"):
raise ValueError
- location: tuple[Maybe[int], Maybe[int]]
- if ".." in location_str:
- location = tuple(limit.map(apply_si_suffix) # type: ignore
- for limit in parse_range(location_str))
- # If point location, assume +/- 50 kbases on either side.
- else:
- width = 50*10**3
- point = apply_si_suffix(location_str)
- location = Just(max(0, point - width)), Just(point + width)
- return ChromosomalInterval(chromosome.removeprefix("chr"), *location)
+ return ChromosomalInterval(chromosome.removeprefix("chr"),
+ *parse_position(position_spec))
def make_query(interval: ChromosomalInterval) -> xapian.Query:
# TODO: Convert the xapian index to use bases instead of megabases.