-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jaccard_coefficient_cluster_resolver.pm
executable file
·269 lines (162 loc) · 6.35 KB
/
Jaccard_coefficient_cluster_resolver.pm
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
package main;
our $SEE;
package Jaccard_coefficient_cluster_resolver;
use strict;
use SingleLinkageClusterer;
=head1 NAME
package Jaccard_coefficient_cluster_resolver
=cut
=head1 DESCRIPTION
Module is used to resolve the clusters within a graph by using the Jaccard's similarity coefficient to break edges within loosely coupled clusters.
The Jaccard's similarity coefficient is defined as:
given nodes A and B,
X = set of nodes connected to A including A
Y = set of nodes connected to B including B
#(nodes intersecting X and Y) (X && Y)
Jlink (A,B) = ----------------------------- = ----------
#(nodes in set X union set Y) (X || Y)
Jlink = 1 when nodes A and B are identically connected.
Jlink = 0 when nodes A and B have no connected neighbors in common, and are themselves unconnected.
For connected nodes A and B which have different neighbors, the Jlink score will be between 0 and 1, providing a similiarity coefficient for the level of similarity between sets of connections.
Another description of the Jaccard similarity coefficient: (http://www.ergometrika.org/Volume3/mulqueen-rev-2003.htm)
The Jaccard index was originally developed to assess similarity among distributions of flora in different geographic areas (Jaccard, 1912). The procedure results in a matching coefficient for binary variables in which joint absences are excluded from both the denominator and the numerator and equal weight is given to matches and non-matches:
SJ = a/(a+b+c) x 100, where
SJ = Jaccard similarity coefficient,
a = number of elements shared by all groups,
b = number of elements unique to the first group, and
c = number of elements unique to the second group.
Jaccard, P. (1912). The distribution of flora in the alpine zone. The New Phytologist, 11(2), 37-50.
=cut
=over 4
=item new()
B<Description:> Constructor: Instantiate an object of Jaccard_coefficient_cluster_resolver
B<Parameters:> $linkScore
$linkScore is a real number between 0 and 1 defined as the Jaccard similarity coefficient.
B<Returns:> $Jaccard_coefficient_cluster_resolver_object
=back
=cut
sub new {
my $packagename = shift;
my $linkScore = shift;
unless ($linkScore >= 0 && $linkScore <= 1) {
die "Invalid link score ($linkScore). \n\n 0 <= link_score <= 1\n\n\n";
}
my $self = {
linkScore => $linkScore
};
bless ($self, $packagename);
return ($self);
}
=over 4
=item resolve_clusters()
B<Description:> Given a set of paired elements, the edges between pairs are removed if the elements have a link score less than that set in the constructor.
B<Parameters:> @pairs
@pairs is a list of paired elements in the form of array references. For example:
@pairs = (
[ a, b],
[b, c],
[e, f]
)
B<Returns:> @clusters
@clusters is a list of array references where each array references provides a list of elements belonging to a single cluster.
For example, given the inputted pairs above as input,
@clusters = (
[a, b, c],
[e, f],
)
See SingleLinkageClusterer.pm for more details.
=back
=cut
sub resolve_clusters {
my $self = shift;
my @pairs = @_;
my $linkScore = $self->{linkScore};
## Transform each pair to a data structure which provides sort of a graph, with each element (node) pointing to a list of other elements(nodes), implemented with a hash{node_id} = (node id list)
my %inputGraph;
foreach my $pair (@pairs) {
my ($a, $b) = @$pair;
my $a_aref = $inputGraph{$a};
unless ($a_aref) {
$a_aref = $inputGraph{$a} = [];
}
my $b_aref = $inputGraph{$b};
unless ($b_aref) {
$b_aref = $inputGraph{$b} = [];
}
&add_element($a_aref, $b);
&add_element($b_aref, $a);
}
## Now examine the Jaccard similarity coefficient between each member
my @resolved_pairs; #all pairs meeting link score restrictions.
my %seen; #avoid analyzing the same pairs twice.
my @all_elements = keys %inputGraph;
foreach my $element (@all_elements) {
my $connected_elements_aref = $inputGraph{$element};
my @listA = ($element, @$connected_elements_aref);
foreach my $connected_element (@$connected_elements_aref) {
if ($seen{$element}->{$connected_element}) { next;} #avoid duplicate comparisons.
my $neighbors_list_aref = $inputGraph{$connected_element};
my @listB = ($connected_element, @$neighbors_list_aref);
my $current_link_score = &calculate_Jaccard_coeff(\@listA, \@listB);
if ($current_link_score >= $linkScore) { #passed test
push (@resolved_pairs, [$element, $connected_element]);
}
$seen{$element}->{$connected_element} = 1;
$seen{$connected_element}->{$element} = 1;
}
}
my @clusters;
if (@resolved_pairs) {
@clusters = &SingleLinkageClusterer::build_clusters(@resolved_pairs);
}
return (@clusters);
}
#private
sub add_element {
my ($list_aref, $element) = @_;
my $has_element = 0;
foreach my $existing_element (@$list_aref) {
if ($element eq $existing_element) {
$has_element = 1;
last;
}
}
if (! $has_element) {
push (@$list_aref, $element);
}
}
#private
####
sub calculate_Jaccard_coeff {
my ($a_list_aref, $b_list_aref) = @_;
if ($SEE) {
print "\nList a: (@$a_list_aref)\n"
. "List b: (@$b_list_aref)\n";
}
## Track each element
my (%all_entries, %a_entries, %b_entries);
foreach my $element (@$a_list_aref) {
$a_entries{$element} = 1;
$all_entries{$element} = 1;
}
foreach my $element (@$b_list_aref) {
$b_entries{$element} = 1;
$all_entries{$element} = 1;
}
## Determine number of unique entries.
my @unique_entries = keys %all_entries;
my $num_unique_entries = $#unique_entries + 1;
print "$num_unique_entries unique entries between a and b: (@unique_entries)\n" if $SEE;
## See how many a and b have in common.
my $num_in_common = 0;
foreach my $element (@unique_entries) {
if ($a_entries{$element} && $b_entries{$element}) {
$num_in_common++;
print "($element) is common.\n" if $SEE;
}
}
my $link_score = ($num_in_common / $num_unique_entries);
print "link score (a,b) = ($num_in_common / $num_unique_entries) = $link_score\n" if $SEE;
return ($link_score);
}
1; #EOM