-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbifurcation_direction.m
107 lines (89 loc) · 3.23 KB
/
bifurcation_direction.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
%Identify the bifurcation directions.
%Project data along the bifurcation directions.
function bifurcation_direction(dataset, cluster_mode);
[dataFile processDataMat processDataTxt PCAdataFile dataFolder resultsDir intermediate_filesDir figuresDir] = initialization(dataset);
load(PCAdataFile);
tree_in = fullfile(intermediate_filesDir,'final_tree.mat');
bif_out = fullfile(intermediate_filesDir,'bifurcation_direction.mat');
DirectionOutfile = fullfile(intermediate_filesDir,'bifurcation_direction.txt');
ProjectionOutfile = fullfile(intermediate_filesDir,'projection_all_data.txt');
load(tree_in);
bifurcationEvents = find(T.pa(1:end-1) == T.pa(2:end));
if isempty(bifurcationEvents),
warning('No bifurcation found.');
else,
clear bif;
bif.t = T.t(bifurcationEvents);
bif.stage = T.stage(bifurcationEvents);
ngene = length(pro.gname);
ncell = length(pro.cell);
nbif = length(bifurcationEvents);
%projection of each cell onto the bifurcation direction
bif.direction = zeros(ngene, nbif); %Bifurcation direction
bif.proj = zeros(ncell, nbif); %Projection of each cell along the bifurcation direction
bif.clu_id = cell(1, nbif); %corresponding cluster idx association with each bifurcation: [before, middle, after(1), after(2)]
for k = 1:nbif,
I1 = bifurcationEvents(k);
I2 = I1+1;
pa = T.pa(I1);
ppa = T.pa(pa);
bif.clu_id{k} = [ppa, pa, I1, I2];
t = T.t(I1);
v = T.mu(I2, :) - T.mu(I1, :);
switch cluster_mode,
case 'pca',
v = v/norm(v);
bif.direction(:, k) = pro.weight*v';
bif.proj(:, k) = pro.pca*v';
case 'pca2'
v = v/norm(v);
bif.direction(:, k) = pro.weight2*v';
bif.proj(:, k) = pro.pca2*v';
otherwise,
v = v/norm(v);
bif.direction(:, k) = v;
bif.proj(:, k) = pro.expr*v';
end
end
save(bif_out, 'bif');
%output the bifurcation direction.
fs = fopen(DirectionOutfile, 'w+');
fprintf(fs, '%s\t', 'Time');
for k = 1:nbif-1,
fprintf(fs, '%d\t', bif.t(k));
end
fprintf(fs, '%d\n', bif.t(end));
fprintf(fs, '%s\t', 'Stage');
for k = 1:nbif-1,
fprintf(fs, '%d\t', bif.stage(k));
end
fprintf(fs, '%d\n', bif.stage(end));
for j = 1:ngene,
fprintf(fs, '%s\t', pro.gname{j});
for k = 1:nbif-1,
fprintf(fs, '%e\t', bif.direction(j, k));
end
fprintf(fs, '%e\n', bif.direction(j, end));
end
fclose(fs);
%output the projection of data to bifurcation directions.
fs = fopen(ProjectionOutfile, 'w+');
fprintf(fs, '%s\t', 'Time');
for k = 1:nbif-1,
fprintf(fs, '%d\t', bif.t(k));
end
fprintf(fs, '%d\n', bif.t(end));
fprintf(fs, '%s\t', 'Stage');
for k = 1:nbif-1,
fprintf(fs, '%d\t', bif.stage(k));
end
fprintf(fs, '%d\n', bif.stage(end));
for j = 1:ncell,
fprintf(fs, '%s\t', pro.cell{j});
for k = 1:nbif-1,
fprintf(fs, '%e\t', bif.proj(j, k));
end
fprintf(fs, '%e\n', bif.proj(j, end));
end
fclose all
end