-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsmootheningExpr.m
59 lines (58 loc) · 2.17 KB
/
smootheningExpr.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function smoothExprIfo = smootheningExpr(data,pseudotime,cellOrder,nbins,cellw)
% calculate the smooth gene expression based on the pseudotime
% Inputs:
% data : the single cell data matrix
% pseudotime : the ordered pseudotime
% cellOrder : the cell order
% nbins : the number of bins for dividing the pseudotime, default = 10
% cellw : 1 x m vector, weights for cubic spline fitting, default = [1,...,1].
% Outputs:
% smoothExprIfo: a struct variable
% smoothExprIfo.smoothExpr: smooth gene expression in each cell
% smoothExprIfo.aveExpr: average gene expression in each bin
% smoothExprIfo.avecellw: average weights in each bin
% smoothExprIfo.nbins: the number of used bins
if ~exist('nbins','var') || isempty(nbins)
nbins = 10;
end
if ~exist('cellw','var') || isempty(cellw)
cellw = ones(1,size(data,2));
end
t0 = pseudotime(1);
t1 = pseudotime(end);
binHalfSize = (t1-t0)/2/nbins;
boundariesAndCenters = t0:binHalfSize:t1;
binCenters = boundariesAndCenters([1 2:2:end-1 end]);
binWidths = 0.08*(t1-t0);
expr = data(:,cellOrder); cellw = cellw(cellOrder);
aveExpr = zeros(size(expr,1),length(binCenters));
avecellw = zeros(1,length(binCenters));
index0 = [];
for index = 1:length(binCenters)
cellLocations = pseudotime > binCenters(index) - binWidths & pseudotime < binCenters(index) + binWidths;
if nnz(cellLocations) < 5
index0 = [index0,index];
continue;
end
Q = quantile(expr(:,cellLocations),[0.25, 0.5, 0.75],2);
aveExpr(:,index) = 0.25*Q(:,1)+0.5*Q(:,2)+0.25*Q(:,3);
avecellw(index) = mean(cellw(cellLocations));
end
if ~isempty(index0)
for i = 1:length(index0)
try
aveExpr(:,index0(i)) = mean([aveExpr(:,index0(i)-1),aveExpr(:,index0(i)+1),aveExpr(:,index0(i)+2)],2);
catch
continue;
end
end
end
% fitting cubic curve
pp = csaps(binCenters,aveExpr,[],[],avecellw);
xii = linspace(min(binCenters),max(binCenters),length(pseudotime));
smoothExpr = fnval(pp,xii);
smoothExpr(smoothExpr < 0) = 0;
smoothExprIfo.smoothExpr = smoothExpr;
smoothExprIfo.aveExpr = aveExpr;
smoothExprIfo.avecellw = avecellw;
smoothExprIfo.nbins = nbins;