Rename app_.py to app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import umap
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import os
|
6 |
+
import tempfile
|
7 |
+
import scanpy as sc
|
8 |
+
import argparse
|
9 |
+
import subprocess
|
10 |
+
from io import BytesIO
|
11 |
+
from evaluate import AnndataProcessor
|
12 |
+
from accelerate import Accelerator
|
13 |
+
from huggingface_hub import hf_hub_download
|
14 |
+
|
15 |
+
|
16 |
+
def main(input_file_path, species):
|
17 |
+
|
18 |
+
# clone and cd into UCE repo
|
19 |
+
os.system('git clone https://github.com/minwoosun/UCE.git')
|
20 |
+
os.chdir('UCE')
|
21 |
+
|
22 |
+
##############
|
23 |
+
# UCE #
|
24 |
+
##############
|
25 |
+
|
26 |
+
# python eval_single_anndata.py --adata_path "./data/10k_pbmcs_proc.h5ad" --dir "./" --model_loc "minwoosun/uce-100m"
|
27 |
+
script_name = "eval_single_anndata.py"
|
28 |
+
args = ["--adata_path", input_file_path, "--dir", "/home/user/app/UCE/", "--model_loc", "minwoosun/uce-100m"]
|
29 |
+
command = ["python", script_name] + args
|
30 |
+
|
31 |
+
try:
|
32 |
+
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
33 |
+
print(result.stdout)
|
34 |
+
print(result.stderr)
|
35 |
+
except subprocess.CalledProcessError as e:
|
36 |
+
print(f"Error executing command: {e}")
|
37 |
+
|
38 |
+
|
39 |
+
##############
|
40 |
+
# UMAP #
|
41 |
+
##############
|
42 |
+
UMAP = True
|
43 |
+
|
44 |
+
if (UMAP):
|
45 |
+
adata = sc.read_h5ad('/home/user/app/UCE/10k_pbmcs_proc_uce_adata.h5ad')
|
46 |
+
|
47 |
+
labels = pd.Categorical(adata.obs["cell_type"])
|
48 |
+
|
49 |
+
reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, random_state=42)
|
50 |
+
embedding = reducer.fit_transform(adata.obsm["X_uce"])
|
51 |
+
|
52 |
+
plt.figure(figsize=(10, 8))
|
53 |
+
|
54 |
+
# Create the scatter plot
|
55 |
+
scatter = plt.scatter(embedding[:, 0], embedding[:, 1], c=labels.codes, cmap='Set1', s=50, alpha=0.6)
|
56 |
+
|
57 |
+
# Create a legend
|
58 |
+
handles = []
|
59 |
+
for i, cell_type in enumerate(labels.categories):
|
60 |
+
handles.append(plt.Line2D([0], [0], marker='o', color='w', label=cell_type,
|
61 |
+
markerfacecolor=plt.cm.Set1(i / len(labels.categories)), markersize=10))
|
62 |
+
|
63 |
+
plt.legend(handles=handles, title='Cell Type')
|
64 |
+
plt.title('UMAP projection of the data')
|
65 |
+
plt.xlabel('UMAP1')
|
66 |
+
plt.ylabel('UMAP2')
|
67 |
+
|
68 |
+
# Save plot to a BytesIO object
|
69 |
+
buf = BytesIO()
|
70 |
+
plt.savefig(buf, format='png')
|
71 |
+
buf.seek(0)
|
72 |
+
|
73 |
+
# Read the image from BytesIO object
|
74 |
+
img = plt.imread(buf, format='png')
|
75 |
+
else:
|
76 |
+
img = None
|
77 |
+
print("no image")
|
78 |
+
|
79 |
+
# this need to be changed based on data file name
|
80 |
+
output_file = '/home/user/app/UCE/10k_pbmcs_proc_uce_adata.h5ad'
|
81 |
+
|
82 |
+
return img, output_file
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
|
87 |
+
# Define Gradio inputs and outputs
|
88 |
+
file_input = gr.File(label="Upload a .h5ad single cell gene expression file")
|
89 |
+
species_input = gr.Dropdown(choices=["human", "mouse"], label="Select species")
|
90 |
+
image_output = gr.Image(type="numpy", label="UMAP of UCE Embeddings")
|
91 |
+
file_output = gr.File(label="Download embeddings")
|
92 |
+
|
93 |
+
# Create the Gradio interface
|
94 |
+
demo = gr.Interface(
|
95 |
+
fn=main,
|
96 |
+
inputs=[file_input, species_input],
|
97 |
+
outputs=[image_output, file_output],
|
98 |
+
title="UCE 100M Demo",
|
99 |
+
description="Upload a .h5ad single cell gene expression file, and get a UMAP scatter plot along with the UMAP coordinates in a CSV file."
|
100 |
+
)
|
101 |
+
|
102 |
+
demo.launch()
|
103 |
+
|
104 |
+
|
app_.py
DELETED
@@ -1,41 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import umap
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import os
|
6 |
-
import tempfile
|
7 |
-
import scanpy as sc
|
8 |
-
import argparse
|
9 |
-
import subprocess
|
10 |
-
from io import BytesIO
|
11 |
-
from evaluate import AnndataProcessor
|
12 |
-
from accelerate import Accelerator
|
13 |
-
from huggingface_hub import hf_hub_download
|
14 |
-
|
15 |
-
|
16 |
-
def main(input_file_path, species):
|
17 |
-
|
18 |
-
# clone and cd into UCE repo
|
19 |
-
os.system('git clone https://github.com/minwoosun/UCE.git')
|
20 |
-
os.chdir('UCE')
|
21 |
-
|
22 |
-
# python eval_single_anndata.py --adata_path "./data/10k_pbmcs_proc.h5ad" --dir "./" --model_loc "minwoosun/uce-100m"
|
23 |
-
script_name = "eval_single_anndata.py"
|
24 |
-
args = ["--adata_path", input_file_path, "--dir", "./", "--model_loc", "minwoosun/uce-100m"]
|
25 |
-
command = ["python", script_name] + args
|
26 |
-
|
27 |
-
try:
|
28 |
-
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
29 |
-
print(result.stdout)
|
30 |
-
print(result.stderr)
|
31 |
-
except subprocess.CalledProcessError as e:
|
32 |
-
print(f"Error executing command: {e}")
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
if __name__ == "__main__":
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|