Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaitodor committed Apr 4, 2024
1 parent 7992242 commit 84546e7
Showing 1 changed file with 60 additions and 51 deletions.
111 changes: 60 additions & 51 deletions genomics-apps/polygenicScore.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import streamlit as st
import requests
import csv
import statistics

import requests
import streamlit as st

st.set_page_config(
page_title="Get Polygenic Score",
page_icon="🧬",
Expand All @@ -29,31 +30,35 @@
:arrow_forward: *high risk:* polygenic score (raw) > selected StDev from the population mean. \n\
:arrow_forward: *population mean* and *standard deviation* are based on Operations reference implementation population.")


@st.cache
def findSubjectVariants(subject, range):
def findSubjectVariants(subject, range):
url = 'https://fhir-gen-ops.herokuapp.com/subject-operations/genotype-operations/$find-subject-variants?subject='+subject+'&ranges='+range+'&includeVariants=true'
headers = {'Accept': 'application/json'}
return requests.get(url, headers=headers)


@st.cache
def findSubjectSpecificVariants(subject, variant):
url = 'https://fhir-gen-ops.herokuapp.com/subject-operations/genotype-operations/$find-subject-specific-variants?subject='+subject+'&variants='+variant+'&includeVariants=true'
# url = 'https://fhir-gen-ops-dev-ca42373833b6.herokuapp.com/subject-operations/genotype-operations/$find-subject-specific-variants?subject='+subject+'&variants='+variant+'&includeVariants=true'
headers = {'Accept': 'application/json'}
return requests.get(url, headers=headers)


@st.cache
def getPolygenicScoreMetadata(pgs_id):
url = 'https://www.pgscatalog.org/rest/score/'+pgs_id
headers = {'Accept': 'application/json'}
return requests.get(url, headers=headers)


def populatePolygenicScoreModels():
with open('genomics-apps/data/polygenicScore.csv') as polygenicScoreFile:
polygenicScores = csv.reader(polygenicScoreFile, delimiter=',', quotechar='"')
pgs_id_list = []
phenotype_list = []
pgs_name_list = []
# pgs_name_list = []
GRCh37_SPDI_list = []
effect_weight_list = []
for item in polygenicScores:
Expand All @@ -65,6 +70,7 @@ def populatePolygenicScoreModels():
polygenicScoreModels = [pgs_id_list, phenotype_list, GRCh37_SPDI_list, effect_weight_list]
return polygenicScoreModels


def populatePolygenicPopulationStatistics():
with open('genomics-apps/data/polygenicPopulationStatistics.csv') as polygenicPopulationStatisticsFile:
populationStatistics = csv.reader(polygenicPopulationStatisticsFile, delimiter=',', quotechar='"')
Expand All @@ -78,7 +84,8 @@ def populatePolygenicPopulationStatistics():
polygenicPopulationStatistics = [pgs_id_list, mean_list, stdev_list]
return polygenicPopulationStatistics

def getPolygenicRawScore (subject, polygenicModelID):

def getPolygenicRawScore(subject, polygenicModelID):
itemNumber = 0
phenotype = ""
results_table = []
Expand All @@ -88,86 +95,88 @@ def getPolygenicRawScore (subject, polygenicModelID):
phenotype = polygenicScoreModels[1][itemNumber]
GRCh37_SPDI = polygenicScoreModels[2][itemNumber]
effect_weight = float(polygenicScoreModels[3][itemNumber])
if GRCh37_SPDI.split(":")[2]!=GRCh37_SPDI.split(":")[3]: # effect allele is not wildtype
variant = findSubjectSpecificVariants(subject,GRCh37_SPDI)
print(f"GRCh37_SPDI: {GRCh37_SPDI}; variant: {variant}") # returns an error if NCBI SPDI services are down
if variant.json()["parameter"][0]["part"][1]["valueBoolean"]==False:
dose=0
score=0
if GRCh37_SPDI.split(":")[2] != GRCh37_SPDI.split(":")[3]: # effect allele is not wildtype
variant = findSubjectSpecificVariants(subject, GRCh37_SPDI)
print(f"GRCh37_SPDI: {GRCh37_SPDI}; variant: {variant}") # returns an error if NCBI SPDI services are down
if not variant.json()["parameter"][0]["part"][1]["valueBoolean"]:
dose = 0
score = 0
total_score = total_score + score
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
else:
for j in variant.json()["parameter"][0]["part"][2]["resource"]["component"]:
if j["code"]["coding"][0]["code"] == "53034-5":
allelicStateCode = j["valueCodeableConcept"]["coding"][0]["code"]
allelicStateDisplay = j["valueCodeableConcept"]["coding"][0]["display"]
allelicStateCode = j["valueCodeableConcept"]["coding"][0]["code"]
# allelicStateDisplay = j["valueCodeableConcept"]["coding"][0]["display"]
if allelicStateCode == "LA6706-1":
dose=1
score=dose * effect_weight
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
dose = 1
score = dose * effect_weight
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
total_score = total_score + score
elif allelicStateCode == "LA6705-3":
dose=2
score=dose * effect_weight
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
dose = 2
score = dose * effect_weight
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
total_score = total_score + score
else:
dose="undefined"
score="undefined"
results_table.append({"allele":GRCh37_SPDI,"dose":"undefined","effect_weight":effect_weight,"score":"undefined"})
else: # effect allele is wildtype
dose = "undefined"
score = "undefined"
results_table.append({"allele": GRCh37_SPDI, "dose": "undefined", "effect_weight": effect_weight, "score": "undefined"})
else: # effect allele is wildtype
refSeq = GRCh37_SPDI.split(":")[0]
position = GRCh37_SPDI.split(":")[1]
variant = findSubjectVariants(subject,refSeq+":"+position+"-"+position)
if variant.json()["parameter"][0]["part"][1]["valueBoolean"]==False: # no variant found, thus homozygous wildtype
dose=2
score=dose * effect_weight
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
variant = findSubjectVariants(subject, refSeq+":"+position+"-"+position)
if not variant.json()["parameter"][0]["part"][1]["valueBoolean"]: # no variant found, thus homozygous wildtype
dose = 2
score = dose * effect_weight
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
total_score = total_score + score
else:
for j in variant.json()["parameter"][0]["part"][2]["resource"]["component"]:
if j["code"]["coding"][0]["code"] == "53034-5":
allelicStateCode = j["valueCodeableConcept"]["coding"][0]["code"]
allelicStateDisplay = j["valueCodeableConcept"]["coding"][0]["display"]
allelicStateCode = j["valueCodeableConcept"]["coding"][0]["code"]
# allelicStateDisplay = j["valueCodeableConcept"]["coding"][0]["display"]
if allelicStateCode == "LA6706-1":
dose=1
score=dose * effect_weight
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
dose = 1
score = dose * effect_weight
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
total_score = total_score + score
elif allelicStateCode == "LA6705-3":
dose=0
score=dose * effect_weight
results_table.append({"allele":GRCh37_SPDI,"dose":dose,"effect_weight":effect_weight,"score":score})
dose = 0
score = dose * effect_weight
results_table.append({"allele": GRCh37_SPDI, "dose": dose, "effect_weight": effect_weight, "score": score})
total_score = total_score + score
else:
dose="undefined"
score="undefined"
results_table.append({"allele":GRCh37_SPDI,"dose":"undefined","effect_weight":effect_weight,"score":"undefined"})
dose = "undefined"
score = "undefined"
results_table.append({"allele": GRCh37_SPDI, "dose": "undefined", "effect_weight": effect_weight, "score": "undefined"})
itemNumber = itemNumber + 1
polygenicRawScore = {"subject": subject, "polygenicModelID":polygenicModelID, "phenotype": phenotype, "scores": results_table, "rawScore": total_score}
polygenicRawScore = {"subject": subject, "polygenicModelID": polygenicModelID, "phenotype": phenotype, "scores": results_table, "rawScore": total_score}
return polygenicRawScore

def getSummaryStatistics (polygenicModelID):

def getSummaryStatistics(polygenicModelID):
score = []
for subject in ["HG00403","HG00406","HG02657","NA18498","NA18499","NA18870","NA18871","NA19190","NA19210","NA19238","NA19239","NA19240"]:
for subject in ["HG00403", "HG00406", "HG02657", "NA18498", "NA18499", "NA18870", "NA18871", "NA19190", "NA19210", "NA19238", "NA19239", "NA19240"]:
rawScore = getPolygenicRawScore(subject, polygenicModelID)["rawScore"]
score.append(rawScore)
mean = statistics.mean(score)
stdev = statistics.stdev(score)
polygenicSummaryStatistics = {"polygenicModelID": polygenicModelID, "mean": mean, "stdev": stdev}
return polygenicSummaryStatistics


polygenicScoreModels = populatePolygenicScoreModels()
polygenicPopulationStatistics = populatePolygenicPopulationStatistics()

# ******* This code computes mean and stdev for ALL models - it takes a while to run ******
# ******* Alternatively, call getSummaryStatistics for just the model of interest *********
# for polygenicModelID in set(polygenicScoreModels[0]):
# print(getSummaryStatistics(polygenicModelID))
# print(getSummaryStatistics(polygenicModelID))
# print(getSummaryStatistics("PHECODE15S"))

with st.sidebar:
subject = st.selectbox("Select patient", ["HG00403","HG00406","HG02657","NA18498","NA18499","NA18870","NA18871","NA19190","NA19210","NA19238","NA19239","NA19240"])
subject = st.selectbox("Select patient", ["HG00403", "HG00406", "HG02657", "NA18498", "NA18499", "NA18870", "NA18871", "NA19190", "NA19210", "NA19238", "NA19239", "NA19240"])
unique_pgs_list = []
itemCount = 0
for item in polygenicScoreModels[0]:
Expand All @@ -176,8 +185,8 @@ def getSummaryStatistics (polygenicModelID):
unique_pgs_list.append(itemDisplay)
itemCount = itemCount + 1
polygenicModelID = st.selectbox("Select polygenic model", unique_pgs_list).split(" ")[0]
riskThreshold = st.slider("Select StDev threshold for high risk",max_value=3.0,value=1.0,step=0.1)
st.image("genomics-apps/data/normalDistribution.png",width=300)
riskThreshold = st.slider("Select StDev threshold for high risk", max_value=3.0, value=1.0, step=0.1)
st.image("genomics-apps/data/normalDistribution.png", width=300)

if st.sidebar.button("Run"):
polygenicRawScore = getPolygenicRawScore(subject, polygenicModelID)
Expand Down Expand Up @@ -208,11 +217,11 @@ def getSummaryStatistics (polygenicModelID):

with st.expander("Polygenic Score Metadata"):
st.write(getPolygenicScoreMetadata(polygenicModelID).json())

with st.expander("Polygenic Score Model"):
polygenicModelFile = "genomics-apps/data/" + polygenicModelID + ".txt"
polygenicModel = open("genomics-apps/data/"+ polygenicModelID + ".txt","r")
fileContents=""
polygenicModel = open("genomics-apps/data/" + polygenicModelID + ".txt", "r")
fileContents = ""
for item in polygenicModel:
fileContents=fileContents+item
fileContents = fileContents+item
st.text(fileContents)

0 comments on commit 84546e7

Please sign in to comment.