Spaces:
Sleeping
Sleeping
initial port of the CyrptoCEN space
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import altair as alt
|
6 |
+
|
7 |
+
st.set_page_config(layout='wide')
|
8 |
+
|
9 |
+
# parse out gene_ids from URL query args to it's possible to link to this page
|
10 |
+
query_params = st.query_params
|
11 |
+
if "gene_id_1" in query_params.keys():
|
12 |
+
gene_id_1 = query_params["gene_id_1"]
|
13 |
+
else:
|
14 |
+
gene_id_1 = "TGME49_231630"
|
15 |
+
|
16 |
+
if "gene_id_2" in query_params.keys():
|
17 |
+
gene_id_2 = query_params["gene_id_2"]
|
18 |
+
else:
|
19 |
+
gene_id_2 = "TGME49_230210"
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
st.markdown("""
|
25 |
+
# ToxoCEN Expression Scatter
|
26 |
+
**ToxoCEN** is a co-expression network for *Toxoplasma gondii* built on 719oo RNA-seq runs across 39 studies.
|
27 |
+
A pair of genes are said to be co-expressed when their expression is correlated across different conditions and
|
28 |
+
is often a marker for genes to be involved in similar processes.
|
29 |
+
|
30 |
+
To Cite:
|
31 |
+
CS Arnold, Y Wang, VB Carruthers, MJ O'Meara
|
32 |
+
ToxoCEN: A Co-Expression Network for Toxoplasma gondii
|
33 |
+
|
34 |
+
* Code available at https://github.com/maomlab/CalCEN/tree/master/vignettes/ToxoCEN
|
35 |
+
* Full network and dataset: https://huggingface.co/datasets/maomlab/ToxoCEN
|
36 |
+
|
37 |
+
## Plot scatter plot expression for a pair of genes across studies.
|
38 |
+
Put in the ``TGME49_######`` gene_id for two genes.
|
39 |
+
""")
|
40 |
+
|
41 |
+
TGME49_transcript_annotations = datasets.load_dataset(
|
42 |
+
path = "maomlab/ToxoCEN",
|
43 |
+
data_files = {"TGME49_transcript_annotations": "TGME49_transcript_annotations.tsv"})
|
44 |
+
TGME49_transcript_annotations = TGME49_transcript_annotations["TGME49_transcript_annotations"].to_pandas()
|
45 |
+
|
46 |
+
estimated_expression_meta = datasets.load_dataset(
|
47 |
+
path = "maomlab/ToxoCEN",
|
48 |
+
data_files = {"estimated_expression_meta": "data/estimated_expression_meta.tsv"})
|
49 |
+
estimated_expression_meta = estimated_expression_meta["estimated_expression_meta"].to_pandas()
|
50 |
+
|
51 |
+
estimated_expression = datasets.load_dataset(
|
52 |
+
path = "maomlab/ToxoCEN",
|
53 |
+
data_files = {"estimated_expression": "data/estimated_expression.parquet"})
|
54 |
+
estimated_expression = estimated_expression["estimated_expression"].to_pandas()
|
55 |
+
|
56 |
+
#DEBUG
|
57 |
+
print(f"estimated_expression shape: {estimated_expression.shape}")
|
58 |
+
|
59 |
+
col1, col2, col3, padding = st.columns(spec = [0.2, 0.2, 0.2, 0.4])
|
60 |
+
with col1:
|
61 |
+
gene_id_1 = st.text_input(
|
62 |
+
label = "Gene ID 1",
|
63 |
+
value = f"{gene_id_1}",
|
64 |
+
max_chars = 10,
|
65 |
+
help = "TGME49 Gene ID e.g. TGME49_231630")
|
66 |
+
|
67 |
+
with col2:
|
68 |
+
gene_id_2 = st.text_input(
|
69 |
+
label = "Gene ID 2",
|
70 |
+
value = f"{gene_id_2}",
|
71 |
+
max_chars = 10,
|
72 |
+
help = "TGME49 Gene ID e.g. TGME49_230210")
|
73 |
+
|
74 |
+
# check the user input
|
75 |
+
try:
|
76 |
+
TGME49_id_1 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_1]["TGME49_id"].values[0]
|
77 |
+
gene_name_1 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_1]["gene_name"].values[0]
|
78 |
+
description_1 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_1]["description"].values[0]
|
79 |
+
except:
|
80 |
+
st.error(f"Unable to locate TGME49_id for Gene ID 1: {gene_id_1}, it should be of the form 'TGME49_######'")
|
81 |
+
|
82 |
+
try:
|
83 |
+
TGME49_id_2 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_2]["TGME49_id"].values[0]
|
84 |
+
gene_name_2 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_2]["gene_name"].values[0]
|
85 |
+
description_2 = TGME49_transcript_annotations.loc[TGME49_transcript_annotations["gene_id"] == gene_id_2]["description"].values[0]
|
86 |
+
except:
|
87 |
+
st.error(f"Unable to locate TGME49_id for Gene ID 2: {gene_id_2}, it should be of the form 'TGME49_######'")
|
88 |
+
|
89 |
+
chart_data = pd.DataFrame({
|
90 |
+
"gene_id_1": gene_id_1,
|
91 |
+
"gene_id_2": gene_id_2,
|
92 |
+
"expression_1": estimated_expression.loc[TGME49_transcript_annotations["gene_id"] == gene_id_1].to_numpy()[0],
|
93 |
+
"expression_2": estimated_expression.loc[TGME49_transcript_annotations["gene_id"] == gene_id_2].to_numpy()[0],
|
94 |
+
"log_expression_1": np.log10(estimated_expression.loc[TGME49_transcript_annotations["gene_id"] == gene_id_1].to_numpy()[0] + 1),
|
95 |
+
"log_expression_2": np.log10(estimated_expression.loc[TGME49_transcript_annotations["gene_id"] == gene_id_2].to_numpy()[0] + 1),
|
96 |
+
"run_accession": estimated_expression.columns})
|
97 |
+
chart_data = chart_data.merge(
|
98 |
+
right = estimated_expression_meta,
|
99 |
+
on = "run_accession")
|
100 |
+
|
101 |
+
with col3:
|
102 |
+
st.text('') # help alignment with input box
|
103 |
+
st.download_button(
|
104 |
+
label = "Download data as TSV",
|
105 |
+
data = chart_data.to_csv(sep ='\t').encode('utf-8'),
|
106 |
+
file_name = f"ToxoCEN_expression_{gene_id_1}_vs_{gene_id_2}.tsv",
|
107 |
+
mime = "text/csv")
|
108 |
+
|
109 |
+
|
110 |
+
st.markdown(f"""
|
111 |
+
#### Gene 1:
|
112 |
+
* *Gene ID*: [{gene_id_1}](https://toxodb.org/toxodb/app/record/gene/{gene_id_1})
|
113 |
+
{'* *Gene Symbol*:' + gene_symbol_1 if gene_symbol_1 is not None else ''}
|
114 |
+
* *Description*: {description_1}
|
115 |
+
* *Top [Co-Expressed Partners](https://huggingface.co/spaces/maomlab/ToxoCEN-TopHits?gene_id={gene_id_1})*
|
116 |
+
|
117 |
+
#### Gene 2:
|
118 |
+
* *Gene ID*: [{gene_id_2}](https://toxodb.org/toxodb/app/record/gene/{gene_id_2})
|
119 |
+
{'* *Gene Symbol*:' + gene_symbol_2 if gene_symbol_2 is not None else ''}
|
120 |
+
* *Description*: {description_2}
|
121 |
+
* *Top [Co-Expressed Partners](https://huggingface.co/spaces/maomlab/ToxoCEN-TopHits?gene_id={gene_id_2})*
|
122 |
+
""")
|
123 |
+
|
124 |
+
chart = (
|
125 |
+
alt.Chart(
|
126 |
+
chart_data,
|
127 |
+
width = 750,
|
128 |
+
height = 750)
|
129 |
+
.mark_circle()
|
130 |
+
.encode(
|
131 |
+
x=alt.X("log_expression_1", title=f"Log10[{gene_id_1}+1] Expression"),
|
132 |
+
y=alt.Y("log_expression_2", title=f"Log10[{gene_id_2}+1] Expression"),
|
133 |
+
color=alt.Color("study_accession", title="Study Accession"),
|
134 |
+
tooltip=["run_accession", "study_accession"]))
|
135 |
+
|
136 |
+
st.altair_chart(
|
137 |
+
chart)
|
138 |
+
|