Skip to content

Commit

Permalink
Patch 4.90.1: fix parsing mme dates (#4961)
Browse files Browse the repository at this point in the history
* Fix parsing of Matchmaker Exchange's matches dates

* Make it a patch release

* Rename test

* tests depends on other factors, simplify

* typo

* Fix according review

---------

Co-authored-by: Chiara Rasi <rasi.chiara@gmacil.com>
  • Loading branch information
northwestwitch and Chiara Rasi authored Oct 18, 2024
1 parent dcd473a commit d46a7e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

About changelog [here](https://keepachangelog.com/en/1.0.0/)

## [4.90.1]
### Fixed
- Parsing Matchmaker Exchange's matches dates

## [4.90]
### Added
- Link to chanjo2 MANE coverage overview on case page and panel page
Expand Down
2 changes: 1 addition & 1 deletion scout/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.90"
__version__ = "4.90.1"
24 changes: 18 additions & 6 deletions scout/parse/matchmaker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import datetime
import logging
from datetime import datetime
from typing import Union

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -158,6 +159,19 @@ def genomic_features(store, case_obj, sample_name, candidate_vars, genes_only):
return g_features


def parse_datetime(match_date: Union[str, int]) -> datetime:
"""Converts a date to a datetime. match_date could be either a millisecond timestamp (Patientmatcher < 4.5) or a string (Patientmatcher >= 4.5), including milliseconds or not."""
try:
date_datetime = datetime.fromtimestamp(match_date / 1000.0)
except TypeError:
if "." in match_date:
date_datetime = datetime.strptime(match_date, "%Y-%m-%dT%H:%M:%S.%fZ")
else:
date_datetime = datetime.strptime(match_date, "%Y-%m-%dT%H:%M:%SZ")

return date_datetime


def parse_matches(patient_id, match_objs):
"""Parse a list of matchmaker matches objects and returns
a readable list of matches to display in matchmaker matches view.
Expand Down Expand Up @@ -186,11 +200,9 @@ def parse_matches(patient_id, match_objs):
parsed_matches = []

for match_obj in match_objs:
match_date = match_obj["created"]["$date"]
try: # Patientmatcher < 4.5 returns dates in milliseconds
mdate = datetime.datetime.fromtimestamp(match_date / 1000.0)
except TypeError: # Patientmatcher >= 4.5 returns dates in isoformat
mdate = datetime.datetime.strptime(match_date, "%Y-%m-%dT%H:%M:%S.%fZ")

mdate = parse_datetime(match_obj["created"]["$date"])

match_type = "external"
matching_patients = []

Expand Down
28 changes: 27 additions & 1 deletion tests/parse/test_parse_matchmaker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
from datetime import datetime

import pytest

from scout.parse.matchmaker import genomic_features, hpo_terms, omim_terms, parse_matches
from scout.parse.matchmaker import (
genomic_features,
hpo_terms,
omim_terms,
parse_datetime,
parse_matches,
)


def test_parse_datetime_timestamp_milliseconds():
"""Test parsing matching results date in timestamp milliseconds format."""
datetime_int = 1729229925000
assert isinstance(parse_datetime(datetime_int), datetime)


def test_parse_datetime_timestamp():
"""Test parsing matching results date in timestamp with no fractions of seconds."""
datetime_str = "2023-08-16T01:03:53Z"
assert parse_datetime(datetime_str) == datetime(2023, 8, 16, 1, 3, 53)


def test_parse_datetime_timestamp_fractional_seconds():
"""Test parsing matching results date in timestamp with fractions of seconds."""
datetime_str = "2023-08-16T01:03:53.123456Z"
assert parse_datetime(datetime_str) == datetime(2023, 8, 16, 1, 3, 53, 123456)


def test_parse_hpo_terms(case_obj, test_hpo_terms):
Expand Down

0 comments on commit d46a7e8

Please sign in to comment.