Spaces:
Sleeping
Sleeping
File size: 13,905 Bytes
93e1b64 a6bd112 93e1b64 ec6a815 a6bd112 4ae75ec 1f35211 93e1b64 1b1c01c 93e1b64 1b1c01c 93e1b64 1b1c01c 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 1f35211 646d392 1f35211 646d392 e7d7b51 a6bd112 93e1b64 4ae75ec 27d40b9 47c6369 a6bd112 7833461 47c6369 7833461 47c6369 7833461 47c6369 7833461 47c6369 7833461 a6bd112 1f35211 4ae75ec 1f35211 4ae75ec 1f35211 4ae75ec 47c6369 1f35211 4ae75ec 1f35211 a6bd112 4ae75ec a6bd112 f26b169 a6bd112 f26b169 27d40b9 f26b169 a6bd112 e7d7b51 a6bd112 e7d7b51 a6bd112 e7d7b51 a6bd112 e7d7b51 a6bd112 e7d7b51 a6bd112 ec6a815 a6bd112 ec6a815 a6bd112 ec6a815 a6bd112 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 1f35211 4ae75ec 1f35211 4ae75ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# %%
import os
from typing import Any, Dict, List
import pandas as pd
import requests
import streamlit as st
from sentence_transformers import SentenceTransformer
from sqlalchemy import create_engine, text
username = "demo"
password = "demo"
hostname = os.getenv("IRIS_HOSTNAME", "localhost")
port = "1972"
namespace = "USER"
CONNECTION_STRING = f"iris://{username}:{password}@{hostname}:{port}/{namespace}"
engine = create_engine(CONNECTION_STRING)
def get_all_diseases_name(engine) -> List[List[str]]:
print("Fetching all disease names...")
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT label FROM Test.EntityEmbeddings
"""
result = conn.execute(text(sql))
data = result.fetchall()
all_diseases = [row[0] for row in data if row[0] != "nan"]
return all_diseases
def get_uri_from_name(engine, name: str) -> str:
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT uri FROM Test.EntityEmbeddings
WHERE label = '{name}'
"""
result = conn.execute(text(sql))
data = result.fetchall()
return data[0][0].split("/")[-1]
def get_most_similar_diseases_from_uri(
engine, original_disease_uri: str, threshold: float = 0.8
) -> List[str]:
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT * FROM Test.EntityEmbeddings
"""
result = conn.execute(text(sql))
data = result.fetchall()
all_diseases = [row[1] for row in data if row[1] != "nan"]
return all_diseases
def get_uri_from_name(engine, name: str) -> str:
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT uri FROM Test.EntityEmbeddings
WHERE label = '{name}'
"""
result = conn.execute(text(sql))
data = result.fetchall()
return data[0][0].split("/")[-1]
def get_most_similar_diseases_from_uri(
engine, original_disease_uri: str, threshold: float = 0.8
) -> List[str]:
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT TOP 10 e1.uri AS uri1, e2.uri AS uri2, e1.label AS label1, e2.label AS label2,
VECTOR_COSINE(e1.embedding, e2.embedding) AS distance
FROM Test.EntityEmbeddings e1, Test.EntityEmbeddings e2
WHERE e1.uri = 'http://identifiers.org/medgen/{original_disease_uri}'
AND VECTOR_COSINE(e1.embedding, e2.embedding) > {threshold}
AND e1.uri != e2.uri
ORDER BY distance DESC
"""
result = conn.execute(text(sql))
data = result.fetchall()
similar_diseases = [
(row[1].split("/")[-1], row[3], row[4]) for row in data if row[3] != "nan"
]
return similar_diseases
def get_clinical_record_info(clinical_record_id: str) -> Dict[str, Any]:
# Request:
# curl -X GET "https://clinicaltrials.gov/api/v2/studies/NCT00841061" \
# -H "accept: text/csv"
request_url = f"https://clinicaltrials.gov/api/v2/studies/{clinical_record_id}"
response = requests.get(request_url, headers={"accept": "application/json"})
return response.json()
def get_clinical_records_by_ids(clinical_record_ids: List[str]) -> List[Dict[str, Any]]:
clinical_records = []
for clinical_record_id in clinical_record_ids:
clinical_record_info = get_clinical_record_info(clinical_record_id)
clinical_records.append(clinical_record_info)
return clinical_records
def get_similarities_among_diseases_uris(
uri_list: List[str],
) -> List[tuple[str, str, float]]:
uri_list = ", ".join([f"'{uri}'" for uri in uri_list])
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT e1.uri AS uri1, e2.uri AS uri2, VECTOR_COSINE(e1.embedding, e2.embedding) AS distance
FROM Test.EntityEmbeddings e1, Test.EntityEmbeddings e2
WHERE e1.uri IN ({uri_list}) AND e2.uri IN ({uri_list}) AND e1.uri != e2.uri
"""
result = conn.execute(text(sql))
data = result.fetchall()
return [
{
"uri1": row[0].split("/")[-1],
"uri2": row[1].split("/")[-1],
"distance": float(row[2]),
}
for row in data
]
def augment_the_set_of_diseaces(diseases: List[str]) -> str:
augmented_diseases = diseases.copy()
for i in range(10 - len(augmented_diseases)):
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT TOP 1 e2.uri AS new_disease, (SUM(VECTOR_COSINE(e1.embedding, e2.embedding))/ {len(augmented_diseases)}) AS score
FROM Test.EntityEmbeddings e1, Test.EntityEmbeddings e2
WHERE e1.uri IN ({','.join([f"'{disease}'" for disease in augmented_diseases])})
AND e2.uri NOT IN ({','.join([f"'{disease}'" for disease in augmented_diseases])})
AND e2.label != 'nan'
GROUP BY e2.label
ORDER BY score DESC
"""
result = conn.execute(text(sql))
data = result.fetchall()
augmented_diseases.append(data[0][0])
return augmented_diseases
def get_embedding(string: str, encoder) -> List[float]:
# Embed the string using sentence-transformers
vector = encoder.encode(string, show_progress_bar=False)
return vector
def get_diseases_related_to_a_textual_description(
description: str, encoder
) -> List[str]:
# Embed the description using sentence-transformers
description_embedding = get_embedding(description, encoder)
string_representation = str(description_embedding.tolist())[1:-1]
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT TOP 10 d.uri, VECTOR_COSINE(d.embedding, TO_VECTOR('{string_representation}', DOUBLE)) AS distance
FROM Test.DiseaseDescriptions d
ORDER BY distance DESC
"""
result = conn.execute(text(sql))
data = result.fetchall()
return [
{"uri": row[0], "distance": float(row[1])}
for row in data
if float(row[1]) > 0.8
]
def get_clinical_trials_related_to_diseases(diseases: List[str], encoder) -> List[str]:
# Embed the diseases using sentence-transformers
diseases_string = ", ".join(diseases)
disease_embedding = get_embedding(diseases_string, encoder)
string_representation = str(disease_embedding.tolist())[1:-1]
with engine.connect() as conn:
with conn.begin():
sql = f"""
SELECT TOP 20 d.nct_id, VECTOR_COSINE(d.embedding, TO_VECTOR('{string_representation}', DOUBLE)) AS distance
FROM Test.ClinicalTrials d
ORDER BY distance DESC
"""
result = conn.execute(text(sql))
data = result.fetchall()
return [{"nct_id": row[0], "distance": row[1]} for row in data]
def get_similarities_df(diseases: List[Dict[str, Any]]) -> pd.DataFrame:
# Find out the score of each disease by averaging the cosine similarity of the embeddings of the diseases that include it as uri1 or uri2
df_diseases_similarities = pd.DataFrame(diseases)
# Use uri1 as the index, and uri2 as the columns. The values are the distances.
df_diseases_similarities = df_diseases_similarities.pivot(
index="uri1", columns="uri2", values="distance"
)
# Fill the diagonal with 1.0
df_diseases_similarities = df_diseases_similarities.fillna(1.0)
return df_diseases_similarities
def filter_out_less_promising_diseases(info_dicts: List[Dict[str, Any]]) -> List[str]:
df_diseases_similarities = get_similarities_df(info_dicts)
# Filter out the diseases that are 0.2 standard deviations below the mean
mean = df_diseases_similarities.mean().mean()
std = df_diseases_similarities.mean().std()
filtered_diseases = df_diseases_similarities.mean()[
df_diseases_similarities.mean() > mean - 0.2 * std
].index.tolist()
return filtered_diseases, df_diseases_similarities
def get_labels_of_diseases_from_uris(uris: List[str]) -> List[str]:
with engine.connect() as conn:
with conn.begin():
joined_uris = ", ".join([f"'{uri}'" for uri in uris])
sql = f"""
SELECT label FROM Test.EntityEmbeddings
WHERE uri IN ({joined_uris})
"""
result = conn.execute(text(sql))
data = result.fetchall()
return [row[0] for row in data]
def to_capitalized_case(string: str) -> str:
string = string.replace("_", " ")
if string.isupper():
return string[0] + string[1:].lower()
def list_to_capitalized_case(strings: List[str]) -> str:
strings = [to_capitalized_case(s) for s in strings]
return ", ".join(strings)
def render_trial_details(trial: dict) -> None:
# TODO: handle key errors for all cases (→ do not render)
official_title = trial["protocolSection"]["identificationModule"]["officialTitle"]
st.write(f"##### {official_title}")
try:
st.write(trial["protocolSection"]["descriptionModule"]["briefSummary"])
except KeyError:
try:
st.write(
trial["protocolSection"]["descriptionModule"]["detailedDescription"]
)
except KeyError:
st.error("No description available.")
st.write("###### Status")
try:
status_module = {
"Status": to_capitalized_case(
trial["protocolSection"]["statusModule"]["overallStatus"]
),
"Status Date": trial["protocolSection"]["statusModule"][
"statusVerifiedDate"
],
"Has Results": trial["hasResults"],
}
st.table(status_module)
except KeyError:
st.info("No status information available.")
st.write("###### Design")
try:
design_module = {
"Study Type": to_capitalized_case(
trial["protocolSection"]["designModule"]["studyType"]
),
"Phases": list_to_capitalized_case(
trial["protocolSection"]["designModule"]["phases"]
),
"Allocation": to_capitalized_case(
trial["protocolSection"]["designModule"]["designInfo"]["allocation"]
),
"Primary Purpose": to_capitalized_case(
trial["protocolSection"]["designModule"]["designInfo"]["primaryPurpose"]
),
"Participants": trial["protocolSection"]["designModule"]["enrollmentInfo"][
"count"
],
"Masking": to_capitalized_case(
trial["protocolSection"]["designModule"]["designInfo"]["maskingInfo"][
"masking"
]
),
"Who Masked": list_to_capitalized_case(
trial["protocolSection"]["designModule"]["designInfo"]["maskingInfo"][
"whoMasked"
]
),
}
st.table(design_module)
except KeyError:
st.info("No design information available.")
st.write("###### Interventions")
try:
interventions_module = {}
for intervention in trial["protocolSection"]["armsInterventionsModule"][
"interventions"
]:
name = intervention["name"]
desc = intervention["description"]
interventions_module[name] = desc
st.table(interventions_module)
except KeyError:
st.info("No interventions information available.")
# Button to go to ClinicalTrials.gov and see the trial. It takes the user to the official page of the trial.
st.markdown(
f"See more in [ClinicalTrials.gov](https://clinicaltrials.gov/study/{trial['protocolSection']['identificationModule']['nctId']})"
)
if __name__ == "__main__":
username = "demo"
password = "demo"
hostname = os.getenv("IRIS_HOSTNAME", "localhost")
port = "1972"
namespace = "USER"
CONNECTION_STRING = f"iris://{username}:{password}@{hostname}:{port}/{namespace}"
try:
engine = create_engine(CONNECTION_STRING)
diseases = get_most_similar_diseases_from_uri("C1843013")
for disease in diseases:
print(disease)
except Exception as e:
print(e)
try:
print(get_uri_from_name(engine, "Alzheimer disease 3"))
except Exception as e:
print(e)
clinical_record_info = get_clinical_records_by_ids(["NCT00841061"])
print(clinical_record_info)
textual_description = (
"A disease that causes memory loss and other cognitive impairments."
)
encoder = SentenceTransformer("allenai-specter")
diseases = get_diseases_related_to_a_textual_description(
textual_description, encoder
)
for disease in diseases:
print(disease)
try:
similarities = get_similarities_among_diseases_uris(
[
"http://identifiers.org/medgen/C4553765",
"http://identifiers.org/medgen/C4553176",
"http://identifiers.org/medgen/C4024935",
]
)
for similarity in similarities:
print(
f'{similarity[0].split("/")[-1]} and {similarity[1].split("/")[-1]} have a similarity of {similarity[2]}'
)
except Exception as e:
print(e)
# %%
|