Spaces:
Running
Running
wzkariampuzha
commited on
Commit
•
94675c0
1
Parent(s):
8d4dc06
Create old_app.py
Browse files- old_app.py +160 -0
old_app.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nltk
|
2 |
+
nltk.data.path.append("/home/user/app/nltk_data")
|
3 |
+
#nltk.download('stopwords')
|
4 |
+
#nltk.download('punkt')
|
5 |
+
import classify_abs
|
6 |
+
import extract_abs
|
7 |
+
import pandas as pd
|
8 |
+
#pd.set_option('display.max_colwidth', None)
|
9 |
+
import streamlit as st
|
10 |
+
st.set_page_config(layout="wide")
|
11 |
+
import spacy
|
12 |
+
import tensorflow as tf
|
13 |
+
import pickle
|
14 |
+
import re
|
15 |
+
import plotly.graph_objects as go
|
16 |
+
|
17 |
+
#### LOGO ####
|
18 |
+
st.markdown('''<img src="https://huggingface.co/spaces/ncats/EpiPipeline4RD/raw/main/ncats.svg" alt="National Center for Advancing Translational Sciences Logo">''',unsafe_allow_html=True)
|
19 |
+
st.markdown("")
|
20 |
+
st.markdown('''<img src="https://huggingface.co/spaces/ncats/EpiPipeline4RD/resolve/main/Logo_GARD_fullres.png" alt="NIH Genetic and Rare Diseases Information Center Logo" width=400>''',unsafe_allow_html=True)
|
21 |
+
|
22 |
+
|
23 |
+
#st.markdown('''<img src="https://huggingface.co/spaces/ncats/EpiPipeline4GARD/raw/main/ncats.svg" alt="National Center for Advancing Translational Sciences Logo" width=800>''',unsafe_allow_html=True)
|
24 |
+
#st.markdown("")
|
25 |
+
#st.markdown('''<img src="https://huggingface.co/spaces/ncats/EpiPipeline4GARD/resolve/main/Logo_GARD_fullres.png" alt="NIH Genetic and Rare Diseases Information Center Logo" width=800>''',unsafe_allow_html=True)
|
26 |
+
#st.markdown("![National Center for Advancing Translational Sciences (NCATS) Logo](https://huggingface.co/spaces/ncats/EpiPipeline4GARD/resolve/main/NCATS_logo.png)")
|
27 |
+
|
28 |
+
#### TITLE ####
|
29 |
+
st.title("Epidemiological Information Extraction Pipeline for Rare Diseases")
|
30 |
+
#st.subheader("National Center for Advancing Translational Sciences (NIH/NCATS)")
|
31 |
+
|
32 |
+
#### CHANGE SIDEBAR WIDTH ###
|
33 |
+
st.markdown(
|
34 |
+
"""
|
35 |
+
<style>
|
36 |
+
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
|
37 |
+
width: 250px;
|
38 |
+
}
|
39 |
+
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
|
40 |
+
width: 250px;
|
41 |
+
margin-left: -350px;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
+
""",
|
45 |
+
unsafe_allow_html=True,
|
46 |
+
)
|
47 |
+
|
48 |
+
#### DESCRIPTION ####
|
49 |
+
st.markdown("This application was built by the [National Center for Advancing Translational Sciences (NCATS)](https://ncats.nih.gov/) to automatically search and extract rare disease epidemiology information from PubMed abstracts.")
|
50 |
+
|
51 |
+
#### SIDEBAR WIDGETS ####
|
52 |
+
|
53 |
+
#max_results is Maximum number of PubMed ID's to retrieve BEFORE filtering
|
54 |
+
max_results = st.sidebar.number_input("Maximum number of articles to find in PubMed", min_value=1, max_value=None, value=50)
|
55 |
+
|
56 |
+
filtering = st.sidebar.radio("What type of filtering would you like?",('Strict', 'Lenient', 'None')).lower()
|
57 |
+
|
58 |
+
extract_diseases = st.sidebar.checkbox("Extract Rare Diseases", value=False)
|
59 |
+
|
60 |
+
#### MODEL LOADING ####
|
61 |
+
|
62 |
+
@st.experimental_singleton(show_spinner=False)
|
63 |
+
def load_models_experimental():
|
64 |
+
classify_model_vars = classify_abs.init_classify_model()
|
65 |
+
NER_pipeline, entity_classes = extract_abs.init_NER_pipeline()
|
66 |
+
GARD_dict, max_length = extract_abs.load_GARD_diseases()
|
67 |
+
return classify_model_vars, NER_pipeline, entity_classes, GARD_dict, max_length
|
68 |
+
|
69 |
+
#### DOWNLOAD FUNCTION ####
|
70 |
+
|
71 |
+
@st.cache
|
72 |
+
def convert_df(df):
|
73 |
+
# IMPORTANT: Cache the conversion to prevent computation on every rerun
|
74 |
+
return df.to_csv().encode('utf-8')
|
75 |
+
|
76 |
+
#### SANKEY FUNCTION ####
|
77 |
+
|
78 |
+
#@st.cache(allow_output_mutation=True)
|
79 |
+
@st.experimental_singleton()
|
80 |
+
def epi_sankey(sankey_data, disease_or_gard_id):
|
81 |
+
found, relevant, epidemiologic = sankey_data
|
82 |
+
|
83 |
+
fig = go.Figure(data=[go.Sankey(
|
84 |
+
node = dict(
|
85 |
+
pad = 15,
|
86 |
+
thickness = 20,
|
87 |
+
line = dict(color = "white", width = 0.5),
|
88 |
+
label = ["PubMed IDs Gathered", "Irrelevant Abstracts","Relevant Abstracts Gathered","Epidemiologic Abstracts","Not Epidemiologic"],
|
89 |
+
color = "purple"
|
90 |
+
),
|
91 |
+
#label = ["A1", "A2", "B1", "B2", "C1", "C2"]
|
92 |
+
link = dict(
|
93 |
+
source = [0, 0, 2, 2],
|
94 |
+
target = [2, 1, 3, 4],
|
95 |
+
value = [relevant, found-relevant, epidemiologic, relevant-epidemiologic]
|
96 |
+
))])
|
97 |
+
fig.update_layout(
|
98 |
+
hovermode = 'x',
|
99 |
+
title="Search for the Epidemiology of "+disease_or_gard_id,
|
100 |
+
font=dict(size = 10, color = 'black'),
|
101 |
+
)
|
102 |
+
|
103 |
+
return fig
|
104 |
+
|
105 |
+
#### BEGIN APP ####
|
106 |
+
|
107 |
+
with st.spinner('Loading Epidemiology Models and Dependencies...'):
|
108 |
+
classify_model_vars, NER_pipeline, entity_classes, GARD_dict, max_length = load_models_experimental()
|
109 |
+
loaded = st.success('All Models and Dependencies Loaded!')
|
110 |
+
|
111 |
+
disease_or_gard_id = st.text_input("Input a rare disease term or NIH GARD ID.")
|
112 |
+
|
113 |
+
loaded.empty()
|
114 |
+
|
115 |
+
st.markdown("Examples of rare diseases include [**Fellman syndrome**](https://rarediseases.info.nih.gov/diseases/1/gracile-syndrome), [**Classic Homocystinuria**](https://rarediseases.info.nih.gov/diseases/6667/classic-homocystinuria), [**7383**](https://rarediseases.info.nih.gov/diseases/7383/phenylketonuria), and [**GARD:0009941**](https://rarediseases.info.nih.gov/diseases/9941/fshmd1a). A full list of rare diseases tracked by the NIH Genetic and Rare Diseases Information Center (GARD) can be found [here](https://rarediseases.info.nih.gov/diseases/browse-by-first-letter).")
|
116 |
+
|
117 |
+
if disease_or_gard_id:
|
118 |
+
df, sankey_data, name_gardID = extract_abs.streamlit_extraction(disease_or_gard_id, max_results, filtering,
|
119 |
+
NER_pipeline, entity_classes,
|
120 |
+
extract_diseases, GARD_dict, max_length,
|
121 |
+
classify_model_vars)
|
122 |
+
#IF it returns something, then continue.
|
123 |
+
if sankey_data:
|
124 |
+
df.replace(to_replace='None', value="None")
|
125 |
+
st.dataframe(df, height=200)
|
126 |
+
csv = convert_df(df)
|
127 |
+
disease, gardID = name_gardID
|
128 |
+
#if the user input does not have a number in it (i.e. weak proxy for if it is a GARD ID), then preserve the user input as the disease term.
|
129 |
+
if not bool(re.search(r'\d', disease_or_gard_id)):
|
130 |
+
disease = disease_or_gard_id
|
131 |
+
|
132 |
+
st.download_button(
|
133 |
+
label="Download epidemiology results for "+disease+" as CSV",
|
134 |
+
data = csv,
|
135 |
+
file_name=disease+'.csv',
|
136 |
+
mime='text/csv',
|
137 |
+
)
|
138 |
+
|
139 |
+
st.markdown('See the NIH GARD page for ['+disease+'](https://rarediseases.info.nih.gov/diseases/'+str(re.sub('GARD:|0','',gardID))+'/'+str('-'.join(disease.split()))+')')
|
140 |
+
|
141 |
+
fig = epi_sankey(sankey_data,disease)
|
142 |
+
st.plotly_chart(fig, use_container_width=True)
|
143 |
+
|
144 |
+
if 'IDS' in list(df.columns):
|
145 |
+
st.markdown('''COLUMNS: \\
|
146 |
+
- PROB_OF_EPI: Probability that the paper is an epidemiologic study based on its abstract. \\
|
147 |
+
- IsEpi: If it is an epidemiologic study (If PROB_OF_EPI >0.5) \\
|
148 |
+
- DIS: Rare disease terms or synonyms identified in the abstract from the GARD Dictionary
|
149 |
+
- IDS: GARD IDs identified in the abstract from the GARD Dictionary \\
|
150 |
+
- EPI: Epidemiology Types are the metrics used to estimate disease burden such as "incidence", "prevalence rate", or "occurrence"
|
151 |
+
- STAT: Epidemiology Rates describe how many people are afflicted by a disease.
|
152 |
+
- DATE: The dates when the epidemiologic studies were conducted
|
153 |
+
- LOC: Where the epidemiologic studies were conducted.
|
154 |
+
- SEX: The biological sexes mentioned in the abstract. Useful for diseases that disproportionately affect one sex over the other or may provide context to composition of the study population
|
155 |
+
- ETHN: Ethnicities, races, and nationalities of those represented in the epidemiologic study.
|
156 |
+
''')
|
157 |
+
else:
|
158 |
+
st.subheader("Categories of Results")
|
159 |
+
st.markdown(" - **PROB_OF_EPI**: Probability that the paper is an epidemiologic study based on its abstract. \n - **IsEpi**: If it is an epidemiologic study (If PROB_OF_EPI >0.5) \n - **EPI**: Epidemiology Types are the metrics used to estimate disease burden such as 'incidence', 'prevalence rate', or 'occurrence' \n - **STAT**: Epidemiology Rates describe how many people are afflicted by a disease. \n - **DATE**: The dates when the epidemiologic studies were conducted \n - **LOC**: Where the epidemiologic studies were conducted. \n - **SEX**: The biological sexes mentioned in the abstract. Useful for diseases that disproportionately affect one sex over the other or may provide context to composition of the study population \n - **ETHN**: Ethnicities, races, and nationalities of those represented in the epidemiologic study.")
|
160 |
+
#st.dataframe(data=None, width=None, height=None)
|