Skip to content

Commit

Permalink
change query functionality to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmamidi committed Jan 8, 2024
1 parent ba4eb5f commit 6724363
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
28 changes: 1 addition & 27 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
from fastapi import FastAPI
from utils.query import get_ditto_score
from utils.query import get_ditto_score, query_variant
import json

# run me https://fastapi.tiangolo.com/#installation
Expand Down Expand Up @@ -37,28 +36,3 @@ def get_variant_score(chromosome, position, ref, alt):
# # TODO call the lookup for a variant
# # scores = get_variant_score( stuff from vep for variant info on genome build)
# return {"variant": hgvs_cdna}


# Function to query variant reference allele based on posiiton from UCSC API
def query_variant(chrom: str, pos: int, allele_len: int) -> json:

if not chrom.startswith("chr"):
chrom = "chr" + chrom

url = f"https://api.genome.ucsc.edu/getData/sequence?genome=hg38;chrom={chrom};start={pos-1};end={pos+allele_len-1}"

get_fields = requests.get(url, timeout=20)

if "statusCode" in get_fields.json().keys():
print(
f"Error {str(get_fields.json()['statusCode'])}: {get_fields.json()['statusMessage']}. Possibly invalid or out of range position."
)

# Check if the request was successful
try:
get_fields.raise_for_status()
except requests.exceptions.RequestException as expt:
print(f"Could not get UCSC Annotations for chrom={chrom} and pos={str(pos)}.")
raise expt

return get_fields.json()
27 changes: 26 additions & 1 deletion src/utils/query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pandas as pd
import yaml
import requests
import json
from utils.parse import OCApiParser
from utils.predict import parse_and_predict
Expand Down Expand Up @@ -53,6 +54,30 @@ def get_ditto_score(chrom: str, pos: int, ref: str, alt: str):
return "Could not get variant annotations from OpenCravat's API. Please check the variant info and try again."
else:
var_df_scores = parse_and_predict(overall, config_dict, clf)
# var_df_scores = var_df_scores.set_index('transcript')
var_df_scores = var_df_scores.set_index('transcript')
var_df_scores = var_df_scores.astype({"DITTO": str, "pos": str})
return json.loads(var_df_scores.to_json(orient="index"))

# Function to query variant reference allele based on posiiton from UCSC API
def query_variant(chrom: str, pos: int, allele_len: int) -> json:

if not chrom.startswith("chr"):
chrom = "chr" + chrom

url = f"https://api.genome.ucsc.edu/getData/sequence?genome=hg38;chrom={chrom};start={pos-1};end={pos+allele_len-1}"

get_fields = requests.get(url, timeout=20)

if "statusCode" in get_fields.json().keys():
print(
f"Error {str(get_fields.json()['statusCode'])}: {get_fields.json()['statusMessage']}. Possibly invalid or out of range position."
)

# Check if the request was successful
try:
get_fields.raise_for_status()
except requests.exceptions.RequestException as expt:
print(f"Could not get UCSC Annotations for chrom={chrom} and pos={str(pos)}.")
raise expt

return get_fields.json()

0 comments on commit 6724363

Please sign in to comment.