dylanplummer commited on
Commit
45e97b2
·
1 Parent(s): 0d9ab7b

update violins

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -173,6 +173,7 @@ def perturb(gene, locus1, locus2):
173
  # get integer index of gene
174
  gene_idx = gene_idx_map[gene]
175
  rna.obs['log2FC'] = ism[:, gene_idx]
 
176
 
177
  fig = sc.pl.umap(rna,
178
  color=['celltype', 'log2FC'],
@@ -193,21 +194,32 @@ def perturb(gene, locus1, locus2):
193
  # convert from argb to rgba
194
  image_from_plot = image_from_plot[:, :, [1, 2, 3, 0]]
195
 
196
- fig, ax = plt.subplots(figsize=(6, 6))
197
- sc.pl.violin(rna, gene, groupby='celltype', show=False, ax=ax)
 
198
  fig.canvas.draw()
199
  violin_img = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
200
  violin_img = violin_img.reshape(fig.canvas.get_width_height()[::-1] + (4,))
201
  violin_img = violin_img[:, :, [1, 2, 3, 0]]
202
 
203
- fig, ax = plt.subplots(figsize=(6, 6))
 
 
 
 
 
 
 
 
204
  sc.pl.violin(rna, 'log2FC', groupby='celltype', show=False, ax=ax)
 
 
205
  fig.canvas.draw()
206
  violin_img_fc = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
207
  violin_img_fc = violin_img_fc.reshape(fig.canvas.get_width_height()[::-1] + (4,))
208
  violin_img_fc = violin_img_fc[:, :, [1, 2, 3, 0]]
209
 
210
- return image_from_plot, violin_img, violin_img_fc
211
 
212
 
213
 
@@ -223,11 +235,12 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
223
  out_img = gr.Image(elem_id='out-img', scale=1)
224
  with gr.Row():
225
  out_violin = gr.Image(elem_id='out-violin', scale=1)
 
226
  out_violin_fc = gr.Image(elem_id='out-violin-fc', scale=1)
227
  #out_plot = gr.Plot(elem_id='out-plot', scale=1)
228
 
229
  inputs = [in_locus, anchor1, anchor2]
230
- outputs = [out_img, out_violin, out_violin_fc]
231
 
232
  gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840'],
233
  ['INS', 'INS', 'IGF1'],
@@ -239,7 +252,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
239
  inputs=inputs,
240
  outputs=outputs,
241
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
242
- run_button.click(perturb, [in_locus, anchor1, anchor2], outputs=outputs)
243
 
244
 
245
  if __name__ == "__main__":
 
173
  # get integer index of gene
174
  gene_idx = gene_idx_map[gene]
175
  rna.obs['log2FC'] = ism[:, gene_idx]
176
+ rna.layers['new_counts'] = perterbed_rna_recon
177
 
178
  fig = sc.pl.umap(rna,
179
  color=['celltype', 'log2FC'],
 
194
  # convert from argb to rgba
195
  image_from_plot = image_from_plot[:, :, [1, 2, 3, 0]]
196
 
197
+ fig, ax = plt.subplots(figsize=(6, 3))
198
+ sc.pl.violin(rna, gene, groupby='celltype', layer='counts', show=False, ax=ax)
199
+ ax.set_title(f'Normal {gene} expression')
200
  fig.canvas.draw()
201
  violin_img = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
202
  violin_img = violin_img.reshape(fig.canvas.get_width_height()[::-1] + (4,))
203
  violin_img = violin_img[:, :, [1, 2, 3, 0]]
204
 
205
+ fig, ax = plt.subplots(figsize=(6, 3))
206
+ sc.pl.violin(rna, gene, groupby='celltype', layer='new_counts', show=False, ax=ax)
207
+ ax.set_title(f'{gene} expression after perturbation')
208
+ fig.canvas.draw()
209
+ violin_img_new = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
210
+ violin_img_new = violin_img_new.reshape(fig.canvas.get_width_height()[::-1] + (4,))
211
+ violin_img_new = violin_img_new[:, :, [1, 2, 3, 0]]
212
+
213
+ fig, ax = plt.subplots(figsize=(6, 3))
214
  sc.pl.violin(rna, 'log2FC', groupby='celltype', show=False, ax=ax)
215
+ ax.set_title(f'{gene} expression change')
216
+ ax.hlines(0, -1, len(celltypes), linestyles='dashed')
217
  fig.canvas.draw()
218
  violin_img_fc = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
219
  violin_img_fc = violin_img_fc.reshape(fig.canvas.get_width_height()[::-1] + (4,))
220
  violin_img_fc = violin_img_fc[:, :, [1, 2, 3, 0]]
221
 
222
+ return image_from_plot, violin_img, violin_img_new, violin_img_fc
223
 
224
 
225
 
 
235
  out_img = gr.Image(elem_id='out-img', scale=1)
236
  with gr.Row():
237
  out_violin = gr.Image(elem_id='out-violin', scale=1)
238
+ out_violin_new = gr.Image(elem_id='out-violin-new', scale=1)
239
  out_violin_fc = gr.Image(elem_id='out-violin-fc', scale=1)
240
  #out_plot = gr.Plot(elem_id='out-plot', scale=1)
241
 
242
  inputs = [in_locus, anchor1, anchor2]
243
+ outputs = [out_img, out_violin, out_violin_new, out_violin_fc]
244
 
245
  gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840'],
246
  ['INS', 'INS', 'IGF1'],
 
252
  inputs=inputs,
253
  outputs=outputs,
254
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
255
+ run_button.click(perturb, inputs, outputs=outputs)
256
 
257
 
258
  if __name__ == "__main__":