-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneCentricEssentiality.m
196 lines (164 loc) · 8.08 KB
/
GeneCentricEssentiality.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function GeneCentricEssentiality(GCTFile, CellDescriptionFile, HUGOFile, B, ESNullFile, OutputFile)
%Generate gene-centric essentiality values from shRNA or CRISPR screens with HUGO nomenclature.
%Inputs:
%GCTFile (string) - filename and path to a GCT file containing essentiality values from
% shRNA or CRISPR screens. The first column contains the probe identifier.
% The second column contains the corresponding symbol of the target gene.
% The third column and beyond contains essentiality values representing the
% screen readout of log(after/before). Here we assume 0 --> neutral, negative
% --> lethal, and positive --> proliferation.
% (see Data folder for example file: Achilles.QCv2.4.3.rnai.gct).
% The first row (columns 3 and beyond) contain cell line identifiers.
%CellDescriptionFile (string) - filename and path to a tab-delimited text file containing cell-line
% information in columns. The cancer type column is indicated by the
% header 'Type' in the first row, the subtype column is indicated by
% 'Subtype', and the cell line name is indicated by 'Name'.
% (see Data folder for example file: Achilles.CellLineData.txt)
%HUGOFile (string) - filename and path to a HUGO gene set file containing official gene symbols
% and past aliases. Example is available here: ftp://ftp.ebi.ac.uk/pub/databases/genenames/new/tsv/hgnc_complete_set.txt
%B (scalar) - number of resamplings used in estimating shRNA significance
%ESNullFile (string) - filename and path to a file containing null-distribution values for
% Kolmogorov-Smirnov enrichment scores. If the file exists from a previous
% analysis, providing the path and filename will reduce computation time.
% If it does not already exist, the null enrichment scores will be saved at the
% provided location.
%OutputFile (string) - filename and path to store function outputs in .mat format. Variables
% describing the probes, gene symbols, unique gen symbols, correspondence
% between probes and symbols will be stored, along with gene-centric
% essentiality scores aggregated by three methods: most lethal probe per gene,
% second most lethal probe per gene, and KS enrichment of all probes per gene.
%Licensed to the Apache Software Foundation (ASF) under one
%or more contributor license agreements. See the NOTICE file
%distributed with this work for additional information
%regarding copyright ownership. The ASF licenses this file
%to you under the Apache License, Version 2.0 (the
%"License"); you may not use this file except in compliance
%with the License. You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
%Unless required by applicable law or agreed to in writing,
%software distributed under the License is distributed on an
%"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%KIND, either express or implied. See the License for the
%specific language governing permissions and limitations
%under the License.
%build HUGO structure
HUGO = ParseHUGO(HUGOFile);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%read in shRNA values from .gct - Achilles or the like
Contents = text2cell(GCTFile, '\t');
Probes = Contents(2:end,1);
Symbols = Contents(2:end,2);
Lines = Contents(1,3:end);
Contents = Contents(2:end,3:end);
Symbols = HUGOLookup(Symbols, HUGO, 'Symbol');
Missing = cellfun(@isempty, Symbols);
Symbols(Missing) = [];
Contents(Missing,:) = [];
[UniqueSymbols, ~, Correspondence] = unique(Symbols);
Scores = cellfun(@(x)str2double(x), Contents);
Scores = knnimpute(Scores, 2);
clear Contents;
%read in site and histology information, map to cell lines
Contents = text2cell(CellDescriptionFile, '\t');
TypeCol = find(strcmp(Contents(1,:), 'Type'));
SubtypeCol = find(strcmp(Contents(1,:), 'Subtype'));
NameCol = find(strcmp(Contents(1,:), 'Name'));
Strings = cellfun(@(x,y) [x '.' y], Contents(2:end, TypeCol),...
Contents(2:end, SubtypeCol), 'UniformOutput', false);
Mapping = StringMatch(Lines, Contents(2:end, NameCol));
Histology = cell(size(Lines));
Histology(~cellfun(@isempty, Mapping)) = Strings([Mapping{:}]);
%calculate scores using KS statistic of all shRNAs %%%%%%%%%%%%%%%%%%%%%%%%
%generate null distributions of KS statistics for gene sets with varying sizes
N = numel(Scores); %total number of shRNA measurements across all lines
[~, Sorted] = sort(Scores(:)); %rank all shRNA scores
Ranks(Sorted) = 1:N;
Ranks = reshape(Ranks, size(Scores));
if(~exist(ESNullFile, 'file')) %generate null ES scores for gene sets of size 1:MaxProbes
NullES = zeros(B, MaxProbes);
for i = 1:MaxProbes
NG = i; %number of shRNAs in set
Indices = ceil(size(Scores,1)*size(Scores,2) * rand(B*i,1)); %random sampling of B sets of NG shRNAs
NullData = reshape(Ranks(Indices), [B i]);
for j = 1:B %calculate ES for each sampled shRNA set
[i j]
CDF = zeros(1,N); %rank CDF for 'in' shRNAs
CDF(NullData(j,:)) = 1;
Pg = cumsum(CDF) / NG;
CDF = ones(1,N); %rank CDF for control 'out' shRNAs
CDF(NullData(j,:)) = 0;
Pn = cumsum(CDF) / (N - NG);
Difference = Pg - Pn; %calculate enrichment score
[~, Index] = max(abs(Difference));
NullES(j,i) = Difference(Index);
end
end
save(ESNullFile, 'NullES', 'MaxProbes');
else
load(ESNullFile);
%capture dimensions 'B'
B = size(NullES,1);
end
%calculate actual enrichment scores using KS of shRNAs corresponding to each gene
N = size(Scores,1);
[~, Sorted] = sort(Scores,1); %rank all shRNA scores
Ranks = zeros(N, length(Lines));
for i = 1:length(Lines)
Ranks(Sorted(:,i),i) = 1:N;
end
ScoresES = zeros(length(UniqueSymbols), length(Lines));
for i = 1:length(UniqueSymbols)
i
List = Correspondence == i; %get indices of shRNAs for gene 'i'
NG = sum(List); %get number of shRNAs for gene 'i'
for j = 1:length(Lines)
CDF = zeros(1,N); %rank CDF for 'in' shRNAs
CDF(Ranks(List,j)) = 1;
Pg = cumsum(CDF) / NG;
CDF = ones(1,N); %rank CDF for control 'out' shRNAs
CDF(Ranks(List,j)) = 0;
Pn = cumsum(CDF) / (N - NG);
Difference = Pg - Pn; %calculate enrichment score
[~, Index] = max(abs(Difference));
ES = Difference(Index);
ScoresES(i,j) = sum(NullES(:,NG) <= ES) / B; %calculate p-value
end
end
%calculate scores using top ranking shRNA (most lethal) %%%%%%%%%%%%%%%%%%%
%sort for each gene and each cell line
ScoresMax = zeros(length(UniqueSymbols), size(Scores,2));
for i = 1:length(UniqueSymbols)
List = Correspondence == i;
ScoresMax(i,:) = min(Scores(List,:), [], 1);
end
%calculate scores using second best rank (second most lethal) %%%%%%%%%%%%%
%generate null distribution of second-rank values
MaxProbes = max(hist(Correspondence, [1:length(UniqueSymbols)])); %find max number of probes
Null = zeros(B, MaxProbes);
for i = 1:MaxProbes
Indices = ceil(size(Scores,1)*size(Scores,2) * rand(B*i,1));
Values = reshape(Scores(Indices), [B i]);
if i > 1
Values = sort(Values, 2);
Null(:,i) = Values(:,2);
else
Null(:,i) = Values;
end
end
%scan through each gene and each line, calculate p-value for second most
%lethal shRNA
ScoresSecond = zeros(length(UniqueSymbols), length(Lines));
for i = 1:length(UniqueSymbols)
List = Correspondence == i; %get indices of shRNAs for gene 'i'
N = sum(List); %get number of shRNAs for gene 'i'
Sorted = sort(Scores(List,:), 1);
Second = Sorted(min(2, N), :);
for j = 1:length(Lines)
ScoresSecond(i,j) = sum(Null(:,N) <= Second(j)) / B;
end
end
%write outputs
save(OutputFile, 'Probes', 'Symbols', 'UniqueSymbols', 'Correspondence',...
'ScoresMax', 'ScoresSecond', 'ScoresES', 'Lines');