-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from PGScatalog/dev
Merge dev for release v0.1.1
- Loading branch information
Showing
24 changed files
with
809 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.1.0' | ||
__version__ = '0.1.1' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import requests | ||
import logging | ||
from functools import reduce | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def query_publication(pgp: str) -> list[str]: | ||
api: str = f'https://www.pgscatalog.org/rest/publication/{pgp}' | ||
logger.debug("Querying PGS Catalog with publication PGP ID") | ||
r: requests.models.Response = requests.get(api) | ||
|
||
if r.json() == {}: | ||
logger.critical(f"Bad response from PGS Catalog for EFO term: {pgp}") | ||
raise Exception | ||
|
||
pgs: dict[str, list[str]] = r.json().get('associated_pgs_ids') | ||
logger.debug(f"Valid response from PGS Catalog for PGP ID: {pgp}") | ||
return list(reduce(lambda x, y: set(x).union(set(y)), pgs.values())) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import requests | ||
import logging | ||
import jq | ||
import sys | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_url(pgs: list[str], build: str) -> dict[str, str]: | ||
pgs_result: list[str] = [] | ||
url_result: list[str] = [] | ||
|
||
for chunk in _chunker(pgs): | ||
try: | ||
response = _parse_json_query(query_score(chunk), build) | ||
pgs_result = pgs_result + list(response.keys()) | ||
url_result = url_result + list(response.values()) | ||
except TypeError: | ||
logger.error(f"Bad response from PGS Catalog API. Is {pgs} a valid ID?") | ||
sys.exit(1) | ||
|
||
missing_pgs = set(pgs).difference(set(pgs_result)) | ||
|
||
if missing_pgs: | ||
logger.warning(f"Some queries missing in PGS Catalog response: {missing_pgs}") | ||
|
||
return dict(zip(pgs_result, url_result)) | ||
|
||
|
||
def query_score(pgs_id: list[str]) -> dict: | ||
pgs: str = ','.join(pgs_id) | ||
api: str = f'https://www.pgscatalog.org/rest/score/search?pgs_ids={pgs}' | ||
r: requests.models.Response = requests.get(api) | ||
return r.json() | ||
|
||
|
||
def _chunker(pgs: list[str]): | ||
size = 50 # /rest/score/{pgs_id} limit when searching multiple IDs | ||
return(pgs[pos: pos + size] for pos in range(0, len(pgs), size)) | ||
|
||
|
||
def _parse_json_query(json: dict, build: str | None) -> dict[str, str]: | ||
result = jq.compile(".results").input(json).first() | ||
if not result: | ||
logger.warning("No results in response from PGS Catalog API. Please check the PGS IDs.") | ||
else: | ||
return _extract_ftp_url(json, build) | ||
|
||
|
||
def _extract_ftp_url(json: list[dict], build: str | None) -> dict[str, str]: | ||
id: list[str] = jq.compile('[.results][][].id').input(json).all() | ||
if build is None: | ||
result: list[str] = jq.compile(f'[.results][][].ftp_scoring_file').input( | ||
json).all() | ||
else: | ||
result: list[str] = jq.compile(f'[.results][][].ftp_harmonized_scoring_files.{build}.positions').input(json).all() | ||
return dict(zip(id, [x.replace('https', 'ftp') for x in result])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
import logging | ||
from functools import reduce | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def query_trait(trait: str) -> list[str]: | ||
api: str = f'https://www.pgscatalog.org/rest/trait/{trait}?include_children=1' | ||
logger.debug(f"Querying PGS Catalog with trait {trait}") | ||
r: requests.models.Response = requests.get(api) | ||
|
||
if r.json() == {}: | ||
logger.critical(f"Bad response from PGS Catalog for EFO term: {trait}") | ||
raise Exception | ||
|
||
keys: list[str] = ['associated_pgs_ids', 'child_associated_pgs_ids'] | ||
pgs: list[str] = [] | ||
for key in keys: | ||
pgs.append(r.json().get(key)) | ||
|
||
logger.debug(f"Valid response from PGS Catalog for EFO term: {trait}") | ||
return list(reduce(lambda x, y: set(x).union(set(y)), pgs)) |
Oops, something went wrong.