Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjcai committed Dec 27, 2024
1 parent 29cad73 commit 2a10bc4
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 168 deletions.
27 changes: 27 additions & 0 deletions +gui/callback_SelectCellsByQC.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
sce = guidata(FigureHandle);
% 'SC_QCFILTER (QC Preserves Lowly-expressed Cells/Genes)',...

oldcn = sce.NumCells;
oldgn = sce.NumGenes;

listitems = {'SC_QCFILTER (Basic QC for Cells/Genes)', ...
'SC_QCFILTER (Enabling Whitelist Genes)', ...
'------------------------------------------------', ...
Expand Down Expand Up @@ -445,6 +448,30 @@
return;
end
end

newcn = sce.NumCells;
newgn = sce.NumGenes;
if newgn==0
helpdlg("All genes are removed. Opertaion is cancelled.",'');
requirerefresh = false;
return;
end
if newcn==0
helpdlg("All cells are removed. Opertaion is cancelled.",'');
requirerefresh = false;
return;
end
if oldcn-newcn==0 && oldgn-newgn==0
helpdlg("No cells and genes are removed.",'');
requirerefresh = false;
return;
end
answer = questdlg(sprintf('%d cells will be removed; %d genes will be removed.', ...
oldcn-newcn, oldgn-newgn),'','Accept Changes', 'Cancel Changes', 'Accept Changes');
if ~strcmp(answer, 'Accept Changes')
requirerefresh = false;
return;
end
guidata(FigureHandle, sce);
end

Expand Down
66 changes: 29 additions & 37 deletions +gui/callback_scTenifoldCko.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ function callback_scTenifoldCko(src, ~)
gsorted = natsort(sce.g);
if isempty(gsorted), return; end







Cko_approach = questdlg('Select CKO approach:','','Block Ligand-Receptor','Complete Gene Knockout','Block Ligand-Receptor');
if ~ismember(Cko_approach, {'Block Ligand-Receptor','Complete Gene Knockout'}), return; end

Expand All @@ -142,27 +136,24 @@ function callback_scTenifoldCko(src, ~)
targetpath = ...
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));...
sprintf('%s (%s) <- %s (%s)', celltype1, targetg(1), celltype2, targetg(2))]);
[width] = min([max(strlength(targetpath))*6, 500]);
[indx2, tf] = listdlg('PromptString', {'Select path(s) to block:'}, ...
[targetpathid, tf] = listdlg('PromptString', {'Select path(s) to block:'}, ...
'SelectionMode', 'multiple', 'ListString', targetpath, 'ListSize', [width, 300]);
if tf == 1
targetpath = targetpath(indx2);
else
return;
end
celltype1
celltype2
targetg
targetpath


return;
%[Tcell] = run.py_scTenifoldCko_path(sce, celltype1, celltype2, targetg, ...
% targetpath, wkdir, true, prepare_input_only);
if tf ~= 1, return; end

% assignin("base","sce",sce)
% assignin("base","celltype1",celltype1)
% assignin("base","celltype2",celltype2)
% assignin("base","targetg",targetg)
% assignin("base","targetpathid",targetpathid)
% prepare_input_only = true;


[Tcell] = run.py_scTenifoldCko_path(sce, celltype1, celltype2, targetg, ...
targetpathid, wkdir, true, prepare_input_only);

% return;
case 'Complete Gene Knockout'
[indx2, tf] = listdlg('PromptString', {'Select a KO gene'}, ...
'SelectionMode', 'single', 'ListString', gsorted, 'ListSize', [220, 300]);
Expand All @@ -186,13 +177,12 @@ function callback_scTenifoldCko(src, ~)
end

T = [];
[Tcell] = run.py_scTenifoldCko_type(sce, celltype1, celltype2, targetg, ...
[Tcell] = run.py_scTenifoldCko_gene(sce, celltype1, celltype2, targetg, ...
targettype, wkdir, true, prepare_input_only);
otherwise
errordlg('Invalid option.','');
return;
end


if ~isempty(Tcell)
[T1] = Tcell{1};
Expand Down Expand Up @@ -222,9 +212,9 @@ function callback_scTenifoldCko(src, ~)
T=[T, table(knownpair)];
% T(:,[4 5 6 7 11])=[];

outfile = fullfile(wkdir,"outfile.csv");
outfile = fullfile(wkdir,"outfile_interaction_changes.csv");
if isfile(outfile)
answerx = questdlg('Overwrite outfile.csv? Select No to save in a temporary file.');
answerx = questdlg('Overwrite file? Select No to save in a temporary file.');
else
answerx = 'Yes';
end
Expand Down Expand Up @@ -260,13 +250,10 @@ function callback_scTenifoldCko(src, ~)
end
end

fn=fullfile(wkdir, 'merged_embeds.h5');

eb = h5read(fn,'/data')';

fn=fullfile(wkdir, 'merged_embeds.h5');
eb = h5read(fn,'/data')';
n = height(eb);
sl = n / 4;

sl = n / 4;
% Split the eb into four equal-length sub-ebs
a = eb(1:sl,:);
b = eb(sl+1:2*sl,:);
Expand All @@ -282,11 +269,16 @@ function callback_scTenifoldCko(src, ~)
%[sce.g(x) sce.g(y)]

[T] = ten.i_dr(a, c, sce.g, true);
outfile = sprintf('outfile_%s_expression_changes.csv',celltype1);
T = addvars(T, repelem(celltype1, height(T), 1), 'Before', 1);
T.Properties.VariableNames{'Var1'} = 'celltype';
outfile = sprintf('outfile_expression_changes_in_%s.csv', ...
matlab.lang.makeValidName(celltype1));
writetable(T, outfile);

[T] = ten.i_dr(b, d, sce.g, true);
outfile = sprintf('outfile_%s_expression_changes.csv',celltype1);
T = addvars(T, repelem(celltype2, height(T), 1), 'Before', 1);
T.Properties.VariableNames{'Var1'} = 'celltype';
outfile = sprintf('outfile_expression_changes_in_%s.csv', ...
matlab.lang.makeValidName(celltype2));
writetable(T, outfile);

end
7 changes: 4 additions & 3 deletions +pkg/i_addwd2script.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ function i_addwd2script(scriptfile, targetdir, ftype)
if ~exist(targetdir, 'dir'), error('pkg.i_addwd2script error.'); end

S = fileread(scriptfile);

[~,b,c]=fileparts(scriptfile);

switch ftype
case 'R'
x = sprintf('setwd("%s")', strrep(targetdir,'\','\\'));
S = [x, newline, S];
outscriptfile = fullfile(targetdir,'script.R');
outscriptfile = fullfile(targetdir, [b,c]);
case 'python'
x = sprintf('os.chdir("%s")', strrep(targetdir,'\','\\'));
S = ['import os', newline, x, newline, S];
outscriptfile = fullfile(targetdir,'script.py');
outscriptfile = fullfile(targetdir, [b,c]);
end


Expand Down
File renamed without changes.
78 changes: 78 additions & 0 deletions +run/external/py_scTenifoldCko/script_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#script_path
#import os
import sys
#abspath = os.path.abspath(__file__)
#dname = os.path.dirname(abspath)
# os.chdir(dname)

import scanpy as sc
import scTenifoldXct as st
from scTenifoldXct.dataLoader import build_adata
import pandas as pd
import numpy as np
import h5py
import scipy
from scipy import sparse

twosided = 1
with h5py.File('X1.mat', 'r') as f:
if 'targetgid' in f:
targetgid = f['targetgid'][0]

adata1 = build_adata("X1.mat", "g1.txt", "c1.txt", delimiter=',', meta_cell_cols=['cell_type'], transpose=False)
print('Input read.............1')
xct1 = st.scTenifoldXct(data = adata1,
source_celltype = 'Source',
target_celltype = 'Target',
obs_label = "cell_type",
rebuild_GRN = False,
GRN_file_dir = './1',
verbose = True)

adata2 = build_adata("X2.mat", "g2.txt", "c2.txt", delimiter=',', meta_cell_cols=['cell_type'], transpose=False)
print('Input read.............2')
xct2 = st.scTenifoldXct(data = adata2,
source_celltype = 'Source',
target_celltype = 'Target',
obs_label = "cell_type",
rebuild_GRN = False,
GRN_file_dir = './2',
verbose = True)

xct2=xct2.Knk(targetgid)

XCTs = st.merge_scTenifoldXct(xct1, xct2)
emb = XCTs.get_embeds(train = True)

with h5py.File('merged_embeds.h5', 'w') as h5file:
h5file.create_dataset('data', data=emb)

XCTs.nn_aligned_diff(emb)
xcts_pairs_diff = XCTs.chi2_diff_test(pval = 1.0)
pd.DataFrame(xcts_pairs_diff).to_csv('output1.txt',index=False,header=True)

# if sys.argv[1]=="2":
if twosided==1:
xct1 = st.scTenifoldXct(data = adata1,
source_celltype = 'Target',
target_celltype = 'Source',
obs_label = "cell_type",
rebuild_GRN = False,
GRN_file_dir = './1',
verbose = True)

xct2 = st.scTenifoldXct(data = adata2,
source_celltype = 'Target',
target_celltype = 'Source',
obs_label = "cell_type",
rebuild_GRN = False,
GRN_file_dir = './2',
verbose = True)

xct2=xct2.Knk(targetgid)
XCTs = st.merge_scTenifoldXct(xct1, xct2)
emb = XCTs.get_embeds(train = True)
XCTs.nn_aligned_diff(emb)
xcts_pairs_diff = XCTs.chi2_diff_test(pval = 1.0)
pd.DataFrame(xcts_pairs_diff).to_csv('output2.txt',index=False,header=True)

4 changes: 2 additions & 2 deletions +run/py_scTenifoldCko_type.m → +run/py_scTenifoldCko_gene.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [T] = py_scTenifoldCko_type(sce, celltype1, celltype2, targetg, ...
function [T] = py_scTenifoldCko_gene(sce, celltype1, celltype2, targetg, ...
targettype, wkdir, ...
isdebug, prepare_input_only)

Expand Down Expand Up @@ -127,7 +127,7 @@

fw = gui.gui_waitbar([], [], 'Step 4 of 4: Running scTenifoldXct.py...');

codefullpath = fullfile(codepth,'script.py');
codefullpath = fullfile(codepth,'script_gene.py');
pkg.i_addwd2script(codefullpath, wkdir, 'python');


Expand Down
Loading

0 comments on commit 2a10bc4

Please sign in to comment.