-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrichAnalyze_cellType.m
152 lines (130 loc) · 4.57 KB
/
enrichAnalyze_cellType.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function enrichAnalyze_cellType(clusts, ctList, outF)
load('results\hClustResults_NEW\exp2\allSamples_corrDist.mat');
gCorr = squareform(corrDist);
load('files/gNames.mat');
load('files/genesStatus_5RPKM.mat');
load('files/entrezIDs.mat');
load('files/enIDs.mat');
gIND = find(genesStatus_5RPKM == 1);
gNames_5RPKM = gNames(gIND);
entrezIDs_5RPKM = entrezIDs(gIND);
enIDs_5RPKM = enIDs(gIND);
N = length(gIND);
clear gNames;
clear genesStatus_5RPKM;
%%% extract each cluster genes' entrezIDs
for j = 1 : max(clusts)
clust = find(clusts == j);
clustIDs{j} = entrezIDs_5RPKM(clust);
clear clust;
end
[num txt] = xlsread('files\DataFiles\cellTypeEnrichedGenes.xlsx');
neo = num(:,1); n1 = find(isnan(neo) == 1); neo(n1) = [];
oligo = num(:,2); n2 = find(isnan(oligo) == 1); oligo(n2) = [];
astro = num(:,3); n3 = find(isnan(astro) == 1); astro(n3) = [];
[neo_2 ia ib] = intersect(neo, entrezIDs_5RPKM);
[oligo_2 ja jb] = intersect(oligo, entrezIDs_5RPKM);
[astro_2 ka kb] = intersect(astro, entrezIDs_5RPKM);
ctEIDs{1} = neo_2;
ctEIDs{2} = oligo_2;
ctEIDs{3} = astro_2;
clear num; clear txt;
Nnames = gNames_5RPKM(ib);
Nen = enIDs_5RPKM(ib);
Onames = gNames_5RPKM(jb);
Oen = enIDs_5RPKM(jb);
Anames = gNames_5RPKM(kb);
Aen = enIDs_5RPKM(kb);
fname = 'files\DataFiles\cellTypeGenes.xls';
xlswrite(fname, Nnames, 1, 'A2');
xlswrite(fname, Nen, 1, 'B2');
xlswrite(fname, Onames, 1, 'C2');
xlswrite(fname, Oen, 1, 'D2');
xlswrite(fname, Anames, 1, 'E2');
xlswrite(fname, Aen, 1, 'F2');
%%% Cell Type Enrichment
for dL = 1 : length(ctList)
clear ctName; clear current_ctEIDs;
ctName = ctList{dL};
current_ctEIDs = ctEIDs{dL};
%%% calculate RR score for each cluster
for k = 1 : length(clustIDs)
% find the cell-type enriched genes in each cluster
ctGenesInMod = intersect(current_ctEIDs, clustIDs{k});
nModDG = length(ctGenesInMod);
nMod = length(clustIDs{k});
nDG = length(current_ctEIDs);
RR(dL, k) = hygepdf(nModDG, N, nDG, nMod);
clustSize(k) = length(clustIDs{k});
end
tempP = RR(dL,:);
% pVals = tempP;
pVals = mafdr(tempP);%, 'Method', 'polynomial');
RR(dL,:) = pVals;
clear tempP; clear pVals;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [num txt] = xlsread(['Data\Donors\ASDs.xls']);
% asd = txt(:,1);
% clear num; clear txt;
% [num txt] = xlsread(['Data\Donors\SZ.xls']);
% sz = txt(:,1);
% clear num; clear txt;
% com = intersect(asd, sz);
% asdO = setdiff(asd, sz);
% szO = setdiff(sz, asd);
% dgNames{1} = asdO;
% dgNames{2} = szO;
% dgNames{3} = com;
%
% for dL = 1 : 3
%
% %%% calculate RR score for each cluster
% for k = 1 : length(clustNames)
% clear dGenesInMod; clear nModDG; clear nMod; clear nDG;
% % find the disease genes in each cluster
% dGenesInMod = intersect(dgNames{dL}, clustNames{k});
% nModDG = length(dGenesInMod);
% nMod = length(clustNames{k});
% nDG = length(dgNames{dL});
% RR(dL, k) = hygepdf(nModDG, N, nDG, nMod);
% clustSize(k) = length(clustNames{k});
% L(dL,k) = length(dGenesInMod);
% end
% % tempP = RR(dL,:);
% % % pVals = mafdr(tempP);
% % pVals = mafdr(tempP, 'Method', 'polynomial');
% % RR(dL,:) = pVals;
% % clear tempP; clear pVals;
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save([outF '_enrichmentScores.mat'], 'RR');
csvwrite([outF 'cellType_enrichmentScores.csv'], RR);
f = figure;
bar((-1*log(RR')), 'EdgeColor', 'black');
colormap(autumn)
grid on
% line([0 length(RR)+1], [(-1*log(0.0001)) (-1*log(0.0001))])
% line([0 length(RR)+1], [(-1*log(0.05/(length(clustNames)))) (-1*log(0.05/(length(clustNames))))])
title('Clusters enrichment of cell-type enriched genes', 'fontweight', 'bold')
xlabel('Clusters', 'fontweight', 'bold')
ylabel('P-values', 'fontweight', 'bold')
set(gca, 'XTick', [1:length(clustIDs)])
axis([0 size(RR,2)+1 0 220])
legend(ctList)
legend({'Neurons', 'Oligodendrytes', 'Astrocytes'})
saveas(f, 'results\hClustResults_NEW\exp2\clustersEnrich\cellType_p-values.fig');
saveas(f, 'results\hClustResults_NEW\exp2\clustersEnrich\cellType_p-values.jpg');
% close(f);
% f2 = figure;
% bar(L', 'EdgeColor', 'black');
% colormap(autumn)
% grid on
% % line([0 length(RR)+1], [(-1*log(0.05)) (-1*log(0.05))])
% title('Distribution of disease-related genes over clusters', 'fontweight', 'bold')
% xlabel('Clusters', 'fontweight', 'bold')
% ylabel('Number of Genes', 'fontweight', 'bold')
% set(gca, 'XTick', [1:length(clustNames)])
% % axis([0 size(RR,2)+1 0 max(max(RR))+0.001])
% % legend(dList)
% legend({'ASDs Only', 'SZ Only', 'Common'})