Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjcai committed Dec 26, 2024
1 parent 7ed5c7c commit 29cad73
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 107 deletions.
212 changes: 107 additions & 105 deletions +gui/callback_scTenifoldCko.m
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions +gui/i_measuretext.m
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions +gui/i_singlegraph.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29cad73

Please sign in to comment.