-
Notifications
You must be signed in to change notification settings - Fork 0
/
findClusters_treeSearch.m
349 lines (258 loc) · 8.65 KB
/
findClusters_treeSearch.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
% findClusters Generate list of cluster of a given order
%
% Clusters = findClusters(Nuclei,order)
%
% Inputs:
% Nuclei ... structure with fields
% .ValidPair ... adjacency matrix
% order ... ckuster order: 1, 2, 3, etc
%
% Outputs:
% Clusters ... Mxorder array of clusters of size order, one
% cluster per row
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function Clusters = findClusters_treeSearch(Nuclei,order,adjacencyOrder,...
inClusters, Method)
% Get number of nuclei
N = Nuclei.number;
inOrder = numel(inClusters);
% Get adjacency matrix and convert to logical
if (inOrder < order) && (inOrder>=2)
Adjacency ...
= clusters2Adjacency(inClusters{2},N) | logical(Nuclei.Adjacency(:,:,1));
else
Adjacency = logical(Nuclei.Adjacency(:,:,adjacencyOrder));
AntiAdjacency = Nuclei.AntiAdjacency(:,:,adjacencyOrder);
end
% Initialize output array
Clusters = cell(order,1);
intDataType = 'uint16';
% Get list of 1-clusters
all1Clusters = cast(1:N,intDataType).';
valid1Clusters = cast(all1Clusters(Nuclei.valid),intDataType);
remove = setdiff(all1Clusters,valid1Clusters);
% Set 1-clusters
if inOrder >= 1
Clusters{1} = inClusters{1};
else
Clusters{1} = valid1Clusters;
end
% Get average number of neighbors
q = mean(sum(Adjacency));
% Loop over cluster sizes
for clusterSize = 2:order
if inOrder >= clusterSize
Clusters{clusterSize} = inClusters{clusterSize};
continue;
end
% Estimate the number of clusters
estNum_ = ceil(...
1000 + 1/factorial(clusterSize)*N*q^(clusterSize-1) ...
);
% Initialize cluster list
Clusters{clusterSize} = zeros(estNum_,clusterSize,intDataType);
% Get number of clusters for clusterSize-1
numC_ = size(Clusters{clusterSize-1},1);
% Initialize template for adding new vertices
newVertex = zeros(1,clusterSize,intDataType);
newVertex(clusterSize) = 1;
% Initialize cluster counters
cluster_counter0 = 1;
cluster_counter1 = 1;
% Loop over all clusters of size clusterSize-1
for icluster = 1:numC_
cluster = Clusters{clusterSize-1}(icluster,:);
% Find all vertices connected to nuclei in cluster
neighbors = any(Adjacency(:,cluster),2);
neighbors(remove) = false;
neighbors(cluster) = false;
neighbors(1:cluster(1)) = false;
neighbornuclei = all1Clusters(neighbors);
if isempty(neighbornuclei)
continue
end
% Remove forbidden clusters
for ispin = cluster
neighbornuclei(AntiAdjacency(ispin, neighbornuclei))=[];
end
% Form expanded clusters that contain parent cluster
newclusters = [repmat(cluster,numel(neighbornuclei),1), neighbornuclei];
% Update cluster counters
cluster_counter0 = cluster_counter1;
cluster_counter1 = cluster_counter0 + size(newclusters,1);
% Reallot memory if necessary
if size(Clusters{clusterSize},1) < cluster_counter1
Clusters{clusterSize}(end+estNum_,clusterSize) = 0;
end
% Add clusters to cluster list
Clusters{clusterSize}(cluster_counter0:cluster_counter1-1,:) = newclusters;
end
% Trim array
C = Clusters{clusterSize}(1:cluster_counter1-1,:);
% Sort nuclei in each cluster by nuclei index
C = sort(C,2);
% Sort clusters
C = sortrows(C);
% Remove duplicates
keep = [true; any(C(1:end-1,:)~=C(2:end,:),2)];
%
useCompleteGraph = strcmp(Method.graphCriterion,'complete');
if useCompleteGraph && clusterSize > 2
for iCluster = 1:size(C,1)
if ~keep(iCluster), continue; end
Cluster = C(iCluster,:);
keep(iCluster) = validateCluster(Cluster,Adjacency,useCompleteGraph);
end
end
if ~isempty(C)
Clusters{clusterSize} = C(keep,:);
elseif Method.emptyClusterSetsOkay
Clusters{clusterSize} = C;
else
error(['Error in findClusters_treeSearch(): ', ...
'no cluster found of size ', num2str(clusterSize), ...
'. Try relaxing a cutoff or setting',...
'\n Method.emptyClusterSetsOkay = true;'])
end
end
%doMethylCoupledOnly = any(strcmp(Method.Criteria,'methyl coupled only'));
doMethylCoupledOnly = any(Method.neighborCutoff.methylCoupledOnly);
if doMethylCoupledOnly
% Methyl Groups
isMethylCarbon = strcmp(Nuclei.Type,'CH3');
isMethylHydron = strcmp(Nuclei.Type,'CH3_1H');
isMethyl = isMethylCarbon | isMethylHydron;
for clusterSize = 3:order
if ~Method.neighborCutoff.methylCoupledOnly(clusterSize)
continue;
end
C = Clusters{clusterSize};
keep = sum(isMethyl(C),2) >= Method.neighborCutoff.methylCoupledOnlyNumber;
for ii=1:length(keep)
if ~keep(ii), continue; end
cluster = C(ii,:);
IDs = Nuclei.MethylID(cluster);
IDs = IDs(IDs>0);
uniqueIDs = unique(IDs);
keep(ii) = all(sum(uniqueIDs == IDs',1)==3);
end
Clusters{clusterSize} = C(keep,:);
end
end
if Method.includeAllSubclusters
Clusters{order};
for clusterSize = order-1:-1:1
C = Clusters{clusterSize};
for ii=1:clusterSize+1
C = [C; [Clusters{clusterSize+1}(:,1:ii-1), ...
Clusters{clusterSize+1}(:,ii+1:clusterSize+1) ] ];
end
% Sort nuclei in each cluster by nuclei index
C = sort(C,2);
% Sort clusters
C = sortrows(C);
% Remove duplicates
keep = [true; any(C(1:end-1,:)~=C(2:end,:),2)];
Clusters{clusterSize} = C(keep,:);
end
end
if Method.useMethylPseudoParticles
for cluster_size = 1:order
% Initialize keep seletor.
keep = true( size(Clusters{cluster_size},1),1 );
% Select all clusters with at least one methyl hydron.
if cluster_size == 1
sele = Nuclei.MethylID(Clusters{cluster_size})' > 0;
keep(sele) = false;
else
sele = any(Nuclei.MethylID(Clusters{cluster_size}) > 0,2);
% Remove clusters that do not contain all 3 hydron of a methyl
keep(sele) = remove_incomplete_methyls(...
Nuclei.MethylID(Clusters{cluster_size}(sele,:)));
end
% Finalize removal.
Clusters{cluster_size} = Clusters{cluster_size}(keep,:);
end
end
if Method.randomize_cluster_ordering
for cluster_size = 1:order
n_cluster = size(Clusters{cluster_size},1);
new_ordering = randperm(n_cluster);
Clusters{cluster_size} = Clusters{cluster_size}(new_ordering,:);
end
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function Adjacency = clusters2Adjacency(C2,N1)
Adjacency = zeros(N1);
N2 = size(C2,1);
for ii =1:N2
m_ = C2(ii,1);
n_ = C2(ii,2);
Adjacency(m_,n_) = 1;
Adjacency(n_,m_) = 1;
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
% cluster_methylIDs is an array of methyl ids, where each row is mapped
% from a cluster to the methyl ids of the corresponding atom.
function keep = remove_incomplete_methyls(methylIDs)
% Get the number of cluster.
n_clusters = size(methylIDs,1);
% 1 and 2-clusters cannot hold a full methyl group.
if size(methylIDs,2)<3
keep = false(n_clusters,1);
return;
end
% Initialize selector.
keep = true(n_clusters,1);
% Loop through clusters.
for icluster = 1:n_clusters
% Find unique ids.
unique_ids = unique( methylIDs(icluster,:) );
% Remove clusters that do not contain all or none of the protons from every
% methyl group.
keep(icluster) = all( ...
sum(methylIDs(icluster,:) == unique_ids',2)==3 ...
| unique_ids' ==0);
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function isvalid = validateCluster(Cluster,Nuclei_ValidPair,completeGraph)
clusterSize = numel(Cluster);
if clusterSize == 1
isvalid = true;
return;
end
if size(Nuclei_ValidPair,3) >= clusterSize
Adjacency = Nuclei_ValidPair(Cluster,Cluster,clusterSize);
else
Adjacency = Nuclei_ValidPair(Cluster,Cluster,end);
end
if all(Adjacency(:)==0)
isvalid = false;
return
end
Degree = diag(sum(Adjacency,2));
Laplacian = Degree - Adjacency;
if completeGraph
% Check if the cluster forms a complete graph.
clusterSize = length(Cluster);
isvalid = min(diag(Laplacian)) == clusterSize-1;
else
% Check if the cluster forms a connected graph.
eigenvalues = eig(Laplacian);
nZeroEigenvalues = sum(abs(eigenvalues) < 1e-12);
if abs(nZeroEigenvalues-1) < 1e-12
isvalid = true;
elseif nZeroEigenvalues > 1
isvalid = false;
else
error('Laplacian matrix has no zero eigenvalues.');
end
end
end
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>