create umap function
Browse files
app.py
CHANGED
@@ -5,13 +5,9 @@ import umap
|
|
5 |
import json
|
6 |
import matplotlib.pyplot as plt
|
7 |
import os
|
8 |
-
# import tempfile
|
9 |
import scanpy as sc
|
10 |
-
# import argparse
|
11 |
import subprocess
|
12 |
import sys
|
13 |
-
# from evaluate import AnndataProcessor
|
14 |
-
# from accelerate import Accelerator
|
15 |
from io import BytesIO
|
16 |
from sklearn.linear_model import LogisticRegression
|
17 |
from huggingface_hub import hf_hub_download
|
@@ -40,6 +36,40 @@ def load_and_predict_with_classifier(x, model_path, output_path, save):
|
|
40 |
return y_pred
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
def main(input_file_path, species, default_dataset):
|
44 |
|
45 |
# Get the current working directory
|
@@ -80,21 +110,12 @@ def main(input_file_path, species, default_dataset):
|
|
80 |
from evaluate import AnndataProcessor
|
81 |
from accelerate import Accelerator
|
82 |
|
83 |
-
# # python eval_single_anndata.py --adata_path "./data/10k_pbmcs_proc.h5ad" --dir "./" --model_loc "minwoosun/uce-100m"
|
84 |
-
# script_name = "/home/user/app/UCE/eval_single_anndata.py"
|
85 |
-
# args = ["--adata_path", input_file_path, "--dir", "/home/user/app/UCE/", "--model_loc", "minwoosun/uce-100m"]
|
86 |
-
# command = ["python", script_name] + args
|
87 |
-
|
88 |
dir_path = '/home/user/app/UCE/'
|
89 |
model_loc = 'minwoosun/uce-100m'
|
90 |
|
91 |
print(input_file_path)
|
92 |
print(dir_path)
|
93 |
print(model_loc)
|
94 |
-
|
95 |
-
# # Verify adata_path is not None
|
96 |
-
# if input_file_path is None or not os.path.exists(input_file_path):
|
97 |
-
# raise ValueError(f"Invalid adata_path: {input_file_path}. Please check if the file exists.")
|
98 |
|
99 |
# Construct the command
|
100 |
command = [
|
@@ -118,7 +139,6 @@ def main(input_file_path, species, default_dataset):
|
|
118 |
# Cell-type classification #
|
119 |
################################
|
120 |
|
121 |
-
|
122 |
# Set output file path
|
123 |
file_name_with_ext = os.path.basename(input_file_path)
|
124 |
file_name = os.path.splitext(file_name_with_ext)[0]
|
@@ -136,49 +156,8 @@ def main(input_file_path, species, default_dataset):
|
|
136 |
##############
|
137 |
# UMAP #
|
138 |
##############
|
139 |
-
|
140 |
-
|
141 |
-
if (UMAP):
|
142 |
-
|
143 |
-
# # Set output file path
|
144 |
-
# file_name_with_ext = os.path.basename(input_file_path)
|
145 |
-
# file_name = os.path.splitext(file_name_with_ext)[0]
|
146 |
-
# output_file = "/home/user/app/UCE/" + f"{file_name}_uce_adata.h5ad"
|
147 |
-
|
148 |
-
# adata = sc.read_h5ad(output_file)
|
149 |
-
|
150 |
-
labels = pd.Categorical(adata.obs["cell_type"])
|
151 |
-
|
152 |
-
reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, random_state=42)
|
153 |
-
embedding = reducer.fit_transform(adata.obsm["X_uce"])
|
154 |
-
|
155 |
-
plt.figure(figsize=(10, 8))
|
156 |
-
|
157 |
-
# Create the scatter plot
|
158 |
-
scatter = plt.scatter(embedding[:, 0], embedding[:, 1], c=labels.codes, cmap='Set1', s=50, alpha=0.6)
|
159 |
-
|
160 |
-
# Create a legend
|
161 |
-
handles = []
|
162 |
-
for i, cell_type in enumerate(labels.categories):
|
163 |
-
handles.append(plt.Line2D([0], [0], marker='o', color='w', label=cell_type,
|
164 |
-
markerfacecolor=plt.cm.Set1(i / len(labels.categories)), markersize=10))
|
165 |
-
|
166 |
-
plt.legend(handles=handles, title='Cell Type')
|
167 |
-
plt.title('UMAP projection of the data')
|
168 |
-
plt.xlabel('UMAP1')
|
169 |
-
plt.ylabel('UMAP2')
|
170 |
-
|
171 |
-
# Save plot to a BytesIO object
|
172 |
-
buf = BytesIO()
|
173 |
-
plt.savefig(buf, format='png')
|
174 |
-
buf.seek(0)
|
175 |
-
|
176 |
-
# Read the image from BytesIO object
|
177 |
-
img = plt.imread(buf, format='png')
|
178 |
-
else:
|
179 |
-
img = None
|
180 |
-
print("no image")
|
181 |
-
|
182 |
return img, output_file, pred_file
|
183 |
|
184 |
|
|
|
5 |
import json
|
6 |
import matplotlib.pyplot as plt
|
7 |
import os
|
|
|
8 |
import scanpy as sc
|
|
|
9 |
import subprocess
|
10 |
import sys
|
|
|
|
|
11 |
from io import BytesIO
|
12 |
from sklearn.linear_model import LogisticRegression
|
13 |
from huggingface_hub import hf_hub_download
|
|
|
36 |
return y_pred
|
37 |
|
38 |
|
39 |
+
def plot_umap(adata):
|
40 |
+
|
41 |
+
labels = pd.Categorical(adata.obs["cell_type"])
|
42 |
+
|
43 |
+
reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2, random_state=42)
|
44 |
+
embedding = reducer.fit_transform(adata.obsm["X_uce"])
|
45 |
+
|
46 |
+
plt.figure(figsize=(10, 8))
|
47 |
+
|
48 |
+
# Create the scatter plot
|
49 |
+
scatter = plt.scatter(embedding[:, 0], embedding[:, 1], c=labels.codes, cmap='Set1', s=50, alpha=0.6)
|
50 |
+
|
51 |
+
# Create a legend
|
52 |
+
handles = []
|
53 |
+
for i, cell_type in enumerate(labels.categories):
|
54 |
+
handles.append(plt.Line2D([0], [0], marker='o', color='w', label=cell_type,
|
55 |
+
markerfacecolor=plt.cm.Set1(i / len(labels.categories)), markersize=10))
|
56 |
+
|
57 |
+
plt.legend(handles=handles, title='Cell Type')
|
58 |
+
plt.title('UMAP projection of the data')
|
59 |
+
plt.xlabel('UMAP1')
|
60 |
+
plt.ylabel('UMAP2')
|
61 |
+
|
62 |
+
# Save plot to a BytesIO object
|
63 |
+
buf = BytesIO()
|
64 |
+
plt.savefig(buf, format='png')
|
65 |
+
buf.seek(0)
|
66 |
+
|
67 |
+
# Read the image from BytesIO object
|
68 |
+
img = plt.imread(buf, format='png')
|
69 |
+
|
70 |
+
return img
|
71 |
+
|
72 |
+
|
73 |
def main(input_file_path, species, default_dataset):
|
74 |
|
75 |
# Get the current working directory
|
|
|
110 |
from evaluate import AnndataProcessor
|
111 |
from accelerate import Accelerator
|
112 |
|
|
|
|
|
|
|
|
|
|
|
113 |
dir_path = '/home/user/app/UCE/'
|
114 |
model_loc = 'minwoosun/uce-100m'
|
115 |
|
116 |
print(input_file_path)
|
117 |
print(dir_path)
|
118 |
print(model_loc)
|
|
|
|
|
|
|
|
|
119 |
|
120 |
# Construct the command
|
121 |
command = [
|
|
|
139 |
# Cell-type classification #
|
140 |
################################
|
141 |
|
|
|
142 |
# Set output file path
|
143 |
file_name_with_ext = os.path.basename(input_file_path)
|
144 |
file_name = os.path.splitext(file_name_with_ext)[0]
|
|
|
156 |
##############
|
157 |
# UMAP #
|
158 |
##############
|
159 |
+
img = plot_umap(adata)
|
160 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
return img, output_file, pred_file
|
162 |
|
163 |
|