From 29cad734b647e381560e36044a160998dc5fdc0c Mon Sep 17 00:00:00 2001 From: jamesjcai Date: Thu, 26 Dec 2024 13:15:03 -0600 Subject: [PATCH] . --- +gui/callback_scTenifoldCko.m | 212 +++++++++++++++++----------------- +gui/i_measuretext.m | 17 +++ +gui/i_singlegraph.m | 4 +- 3 files changed, 126 insertions(+), 107 deletions(-) create mode 100644 +gui/i_measuretext.m diff --git a/+gui/callback_scTenifoldCko.m b/+gui/callback_scTenifoldCko.m index 2b970b49..7d3c4d9c 100644 --- a/+gui/callback_scTenifoldCko.m +++ b/+gui/callback_scTenifoldCko.m @@ -128,35 +128,39 @@ function callback_scTenifoldCko(src, ~) switch Cko_approach case 'Block Ligand-Receptor' - [indx2, tf] = listdlg('PromptString', {'Select a ligand-receptor pair (two genes)'}, ... - 'SelectionMode', 'multiple', 'ListString', gsorted, 'ListSize', [220, 300]); - if tf == 1 - if numel(indx2)==2 - [~, idx] = ismember(gsorted(indx2), sce.g); - else - return; - end + + [idx2] = gui.i_selmultidlg(gsorted, [], FigureHandle); + if isempty(idx2), return; end + if numel(idx2)==2 + [~, idx] = ismember(gsorted(idx2), sce.g); else return; end targetg = sce.g(idx); + targetpath = ... - [sprintf('%s (%s) -> %s (%s)', celltype1, targetg(1), celltype2, targetg(2));... + string([sprintf('%s (%s) -> %s (%s)', celltype1, targetg(1), celltype2, targetg(2));... sprintf('%s (%s) -> %s (%s)', celltype1, targetg(2), celltype2, targetg(1));... sprintf('%s (%s) <- %s (%s)', celltype1, targetg(1), celltype2, targetg(2));... - sprintf('%s (%s) <- %s (%s)', celltype1, targetg(2), celltype2, targetg(1))]; - + sprintf('%s (%s) <- %s (%s)', celltype1, targetg(2), celltype2, targetg(1))]); + [width] = min([max(strlength(targetpath))*6, 500]); [indx2, tf] = listdlg('PromptString', {'Select path(s) to block:'}, ... - 'SelectionMode', 'multiple', 'ListString', targetpath, 'ListSize', [220, 300]); - + 'SelectionMode', 'multiple', 'ListString', targetpath, 'ListSize', [width, 300]); if tf == 1 targetpath = targetpath(indx2); else return; end - [Tcell] = run.py_scTenifoldCko(sce, celltype1, celltype2, targetg, ... - targetpath, wkdir, true, prepare_input_only); + celltype1 + celltype2 + targetg + targetpath + + + return; + %[Tcell] = run.py_scTenifoldCko_path(sce, celltype1, celltype2, targetg, ... + % targetpath, wkdir, true, prepare_input_only); case 'Complete Gene Knockout' @@ -185,106 +189,104 @@ function callback_scTenifoldCko(src, ~) [Tcell] = run.py_scTenifoldCko_type(sce, celltype1, celltype2, targetg, ... targettype, wkdir, true, prepare_input_only); otherwise - + errordlg('Invalid option.',''); + return; end - - - -if ~isempty(Tcell) - [T1] = Tcell{1}; - [T2] = Tcell{2}; - if ~isempty(T1) - a = sprintf('%s -> %s', celltype1, celltype2); - T1 = addvars(T1, repelem(a, height(T1), 1), 'Before', 1); - T1.Properties.VariableNames{'Var1'} = 'direction'; - end - if ~isempty(T2) - a = sprintf('%s -> %s', celltype2, celltype1); - T2 = addvars(T2, repelem(a, height(T2), 1), 'Before', 1); - T2.Properties.VariableNames{'Var1'} = 'direction'; + if ~isempty(Tcell) + [T1] = Tcell{1}; + [T2] = Tcell{2}; + if ~isempty(T1) + a = sprintf('%s -> %s', celltype1, celltype2); + T1 = addvars(T1, repelem(a, height(T1), 1), 'Before', 1); + T1.Properties.VariableNames{'Var1'} = 'direction'; + end + if ~isempty(T2) + a = sprintf('%s -> %s', celltype2, celltype1); + T2 = addvars(T2, repelem(a, height(T2), 1), 'Before', 1); + T2.Properties.VariableNames{'Var1'} = 'direction'; + end + T = [T1; T2]; end - T = [T1; T2]; -end - -if ~isempty(T) - mfolder = fileparts(mfilename('fullpath')); - load(fullfile(mfolder, '..', 'resources', 'Ligand_Receptor', ... - 'Ligand_Receptor_more.mat'), 'ligand','receptor'); - A = [string(T.ligand) string(T.receptor)]; - B = [ligand receptor]; - [knownpair]= ismember(A, B, 'rows'); - assert(length(knownpair)==height(T)); - - T=[T, table(knownpair)]; - % T(:,[4 5 6 7 11])=[]; - outfile = fullfile(wkdir,"outfile.csv"); - if isfile(outfile) - answerx = questdlg('Overwrite outfile.csv? Select No to save in a temporary file.'); - else - answerx = 'Yes'; - end - if isempty(wkdir) || ~isfolder(wkdir) || ~strcmp(answerx, 'Yes') - [a, b] = pkg.i_tempdirfile("sctendifoldcko"); - writetable(T, b); + if ~isempty(T) + mfolder = fileparts(mfilename('fullpath')); + load(fullfile(mfolder, '..', 'resources', 'Ligand_Receptor', ... + 'Ligand_Receptor_more.mat'), 'ligand','receptor'); + A = [string(T.ligand) string(T.receptor)]; + B = [ligand receptor]; + [knownpair]= ismember(A, B, 'rows'); + assert(length(knownpair)==height(T)); - answer = questdlg(sprintf('Result has been saved in %s', b), ... - '', 'Export result...', 'Locate result file...', 'Export result...'); - switch answer - case 'Locate result file...' - winopen(a); - pause(2) - if strcmp(questdlg('Export result to other format?'), 'Yes') + T=[T, table(knownpair)]; + % T(:,[4 5 6 7 11])=[]; + + outfile = fullfile(wkdir,"outfile.csv"); + if isfile(outfile) + answerx = questdlg('Overwrite outfile.csv? Select No to save in a temporary file.'); + else + answerx = 'Yes'; + end + if isempty(wkdir) || ~isfolder(wkdir) || ~strcmp(answerx, 'Yes') + [a, b] = pkg.i_tempdirfile("sctendifoldcko"); + writetable(T, b); + + answer = questdlg(sprintf('Result has been saved in %s', b), ... + '', 'Export result...', 'Locate result file...', 'Export result...'); + switch answer + case 'Locate result file...' + winopen(a); + pause(2) + if strcmp(questdlg('Export result to other format?'), 'Yes') + gui.i_exporttable(T, false, 'Ttenifldcko', 'TenifldCkoTable'); + end + case 'Export result...' gui.i_exporttable(T, false, 'Ttenifldcko', 'TenifldCkoTable'); - end - case 'Export result...' - gui.i_exporttable(T, false, 'Ttenifldcko', 'TenifldCkoTable'); - otherwise - winopen(a); + otherwise + winopen(a); + end + else + writetable(T, outfile); + waitfor(helpdlg(sprintf('Result has been saved in %s', outfile), '')); end else - writetable(T, outfile); - waitfor(helpdlg(sprintf('Result has been saved in %s', outfile), '')); - end -else - if ~prepare_input_only - helpdlg('No ligand-receptor pairs are identified.', ''); - else - if strcmp(questdlg('Input files are prepared successfully. Open working folder?',''), 'Yes') - winopen(wkdir); + if ~prepare_input_only + helpdlg('No ligand-receptor pairs are identified.', ''); + else + if strcmp(questdlg('Input files are prepared successfully. Open working folder?',''), 'Yes') + winopen(wkdir); + end end end -end - -fn=fullfile(wkdir, 'merged_embeds.h5'); - -eb = h5read(fn,'/data')'; - -n = height(eb); -sl = n / 4; - -% Split the eb into four equal-length sub-ebs -a = eb(1:sl,:); -b = eb(sl+1:2*sl,:); -c = eb(2*sl+1:3*sl,:); -d = eb(3*sl+1:4*sl,:); - -%dx = abs(pdist2(a,b)-pdist2(c,d)); -%[x,y]=pkg.i_maxij(dx, 1050); -%[sce.g(x) sce.g(y)] -%dx1 = pdist2(a,c); -%dx1 = pdist2(b,d); -%[x,y]=maxij(dx1, 50); -%[sce.g(x) sce.g(y)] - -[T] = ten.i_dr(a, c, sce.g, true); -outfile = sprintf('outfile_%s_expression_changes.csv',celltype1); -writetable(T, outfile); - -[T] = ten.i_dr(b, d, sce.g, true); -outfile = sprintf('outfile_%s_expression_changes.csv',celltype1); -writetable(T, outfile); + + fn=fullfile(wkdir, 'merged_embeds.h5'); + + eb = h5read(fn,'/data')'; + + n = height(eb); + sl = n / 4; + + % Split the eb into four equal-length sub-ebs + a = eb(1:sl,:); + b = eb(sl+1:2*sl,:); + c = eb(2*sl+1:3*sl,:); + d = eb(3*sl+1:4*sl,:); + + %dx = abs(pdist2(a,b)-pdist2(c,d)); + %[x,y]=pkg.i_maxij(dx, 1050); + %[sce.g(x) sce.g(y)] + %dx1 = pdist2(a,c); + %dx1 = pdist2(b,d); + %[x,y]=maxij(dx1, 50); + %[sce.g(x) sce.g(y)] + + [T] = ten.i_dr(a, c, sce.g, true); + outfile = sprintf('outfile_%s_expression_changes.csv',celltype1); + writetable(T, outfile); + + [T] = ten.i_dr(b, d, sce.g, true); + outfile = sprintf('outfile_%s_expression_changes.csv',celltype1); + writetable(T, outfile); end diff --git a/+gui/i_measuretext.m b/+gui/i_measuretext.m new file mode 100644 index 00000000..20bc3302 --- /dev/null +++ b/+gui/i_measuretext.m @@ -0,0 +1,17 @@ +function [width, height] = i_measuretext(txt, textOpts, axis) + if nargin < 3 + axis = gca(); + end + if nargin < 2 + textOpts = struct(); + textOpts.HorizontalAlignment = 'center'; + textOpts.VerticalAlignment = 'middle'; + textOpts.FontSize = 20; + textOpts.FontWeight = 'normal'; + end + hTest = text(axis, 0, 0, txt, textOpts); + textExt = get(hTest, 'Extent'); + delete(hTest); + height = textExt(4)/3; %Height + width = textExt(3)/3; %Width +end \ No newline at end of file diff --git a/+gui/i_singlegraph.m b/+gui/i_singlegraph.m index ea4d1f09..11e3aa90 100644 --- a/+gui/i_singlegraph.m +++ b/+gui/i_singlegraph.m @@ -455,8 +455,8 @@ function stopDragFcn(~, ~) h = graph(s, t); end - function [width, height] = measureText(txt, textOpts, axis) - if(nargin < 3) +function [width, height] = measureText(txt, textOpts, axis) + if nargin < 3 axis = gca(); end if nargin < 2