Background
Ontologies are knowledge representations using controlled vocabularies that are designed to help knowledge sharing and computer reasoning 1. Many ontologies can be represented by directed acyclic graphs (DAGs), whereby the nodes of the DAG, which are also called terms of the ontology, are assigned to items in the domain and the edges between the nodes represent semantic relations. Ontologies are designed such that terms closer to the root are more general than their descendant terms. For the ontologies we consider in this paper, the annotationpropagation rule applies, that is, items are annotated to the most specific term possible but are assumed to be implicitly annotated to all ancestors of that term.
Examples for ontologies are the Foundational Model of Anatomy (FMA) ontology 2, the Sequence Ontology 3, the Cell Ontology 4, and the Chemical Entities of Biological Interest (ChEBI) ontology 5, which describe objects from the domains of anatomy, biological sequences, cells, and biologically relevant chemicals. In contrast, other ontologies are used to describe the attributes of the items of a domain. For instance, GO terms are used to annotate genes or proteins by describing the biological functions or characteristics to the proteins. The Mammalian Phenotype Ontology (MPO) 6 and the Human Phenotype Ontology (HPO) 7 describe the attributes of mammalian and human diseases. In this case, the domain object is a disease such as Marfan syndrome, whose attributes are the clinical features of the disease such as arachnodactyly and aortic dilatation. In other words, terms of phenotype ontologies such as the MPO and HPO can be conceived of as describing abnormal qualities (e.g. hypoplastic) of anatomical or biochemical entities 8.
Semantic similarity between any two terms within an ontology is based on the annotations to items in the domain and on the structure of the DAG. Different semantic similarity measures have been proposed 910 and the measures have been used in many different applications in computational biology. For example, different studies show that semantic similarity between proteins annotated with GO terms correlate with sequence similarity 111213. Other studies investigated the correlation of gene coexpression with semantic similarity using GO terms 1415. In addition, semantic similarity measures for GO terms have been used to predict protein subnuclear localization 16.
In another application we have implemented a semantic similarity search algorithm in the setting of medical diagnosis. A user enters HPO terms describing the clinical abnormalities observed in a patient and a ranked list of the best matching differential diagnoses is returned 17. This kind of search can be performed using raw semantic similarity scores calculated using any of the semantic similarity measures 181920211222. However, among these different measures the nodebased pairwise similarity measure defined by Resnik turned out to have the best performance in our previous study 17 and is therefore considered in this work.
The search is based on q attributes (HPO terms) that describe the phenotypic abnormalities seen in a patient for whom a diagnosis is being sought. For each of the entries of a database containing diseases annotated with HPO terms corresponding to their characteristic signs and symptoms, the best match between each of the q terms of the query with one of the terms annotating the disease is found and the average of the semantic similarity scores is determined. The diseases are then ranked according to these scores and returned to the user as suggestions for the differential diagnosis.
The distribution of scores that a domain object can achieve varies according to the number and specificity of the ontology terms used to annotate it. In a recent study by Wang et al. 23, it was discovered that many of the commonly used semantic similarity measures, including the ones used in this work, are biased towards domain objects that have more annotations. The effect was termed annotation bias. Applications that use the scores alone therefore tend to preferentially select items with higher numbers of annotations, which may lead to wrong conclusions 23.
Previously, we developed a statistical model to assign Pvalues to the resulting similarity scores on the basis of the probability of a random query obtaining at least as high a score in order to compensate for the fact that different domain objects may have a different number of annotations. Using extensive simulations, we showed that this approach outperformed searches based on the semantic similarity scores alone 17. A disadvantage of that procedure was the fact that extensive simulations using randomized queries were necessary in order to estimate the true distribution of the semantic similarity scores, which is needed in order to calculate a Pvalue for any given similarity score.
In this paper, we describe an algorithm to collapse a DAG representing an ontology into connected components of nodes corresponding to terms that make identical contributions to the semantic similarity score. The new algorithm reduces the amount of computational time needed to calculate the score distribution (and thereby Pvalues) by many orders of magnitude compared to a naive calculation. A preliminary description of the algorithm was presented in a conference paper 24. Here, we validate the algorithm by comparing to sampling based approaches and show using simulations that the application of the exact Pvalue outperforms sampling based approaches in the context of clinical diagnostics with the HPO.
Methods
Notation
We consider an ontology O composed of a set of terms that are linked via an isa or partof relationship. The ontology O can then be represented by a DAG G
=
(
V
,
E
)
, where every term is a node in V and every link is a directed edge in E. A directed edge going from node n_{1 }to n_{2 }is denoted e_{1,2 }and we refer to n_{2 }as the parent of n_{1}. An item i is defined as an abstract entity to which terms of the ontology are annotated. Let Anc(n) be defined as the ancestors of n, i.e., the nodes that are found on all paths from node n to the root of G
, including n. We note that the annotationpropagation rule states that if an item is explicitly annotated to a term n, it is implicitly annotated to Anc(n). In order to describe the implicit annotations we define
T
I
M
P
L
. Let T
be the set of terms that has been explicitly annotated to item i, then
T
I
M
P
L
=
∪
n
∈
T
A
n
c
(
n
)
, namely all terms that are annotated to item i and all their ancestors in G. Let the set of common ancestors of two nodes n_{1 }and n_{2 }be defined as ComAnc(n_{1}, n_{2}) = Anc(n_{1}) ⋂ Anc(n_{2}). Let Desc(n) be the set of descendant nodes of n, again including n. Note that in this notation descendant nodes are considered only once, even if there are multiple paths leading to them.
Multisets
In what follows we need to compute the similarity also between a multiset and a set of terms. The concept of multisets 25 is a generalization of the concept of sets. In contrast to sets, in which elements can only have a single membership, the elements of multisets may appear more than once.
Formally, a multiset M is a set of pairs, M = {(s_{1}, m_{1}),..., (s_{d}, m_{d})}, in which s_{i }∈ U = {s_{1},..., s_{d}} are the elements of the underlying set U. Furthermore, m_{i }defines the multiplicity of s_{i }in the multiset. The sum of the multiplicities of M is called the multiset cardinality of M, denoted M. Only multiplicities in the domain of positive integers are considered, i.e., m_{i }∈ ℕ^{+}. We define a multi subset relation between multiset N and multiset M, denoted as N ⊆ M, as a generalization of the subset relation between two sets:
N
⊆
M
⇔
∀
(
s
,
n
)
∈
N
:
∃
m
≥
n
:
(
s
,
m
)
∈
M
.
The multiset coefficient M
(
n
,
q
)
=
n
+
q

1
q
denotes the number of distinct multisets of cardinality q, with elements taken from a finite set of cardinality n. It describes how many ways there are to choose q elements from a set of n elements if repetitions are allowed.
Similarity measures
We will concentrate in this work on the class of similarity measures that are based on the information content (IC) of a node:
I
C
(
n
)
=

log
p
(
n
)
,
where p(n) denotes the frequency among all items in the domain of annotations to n, which implicitly contains all annotations of descendants of n due to the annotationpropagation rule. The information content is a nondecreasing function on the nodes of G as we descend in the hierarchy and is therefore monotonic. The similarity between two nodes was defined by Resnik as the maximum information content among all common ancestors 19:
s
i
m
(
n
1
,
n
2
)
=
m
a
x
{
I
C
(
a
)

a
∈
C
o
m
A
n
c
(
n
1
,
n
2
)
}
.
Equation (2) provides a definition for the similarity between two terms. Other popular pairwise measures that additionally incorporate the IC of the query terms, for example 2021, are not considered here (see Discussion).
One can extend this concept to define a similarity between two domain objects that are each annotated by multiple ontology terms by taking the average of the best pairwise similarities for all terms 11:
s
i
m
a
v
g
(
T
1
,
T
2
)
=
1

T
1

∑
n
1
∈
T
1
max
n
2
∈
T
2
s
i
m
(
n
1
,
n
2
)
.
Note that Eq. (3) is not symmetric 12, i.e., it is not necessarily true that
s
i
m
a
v
g
(
T
1
,
T
2
)
=
s
i
m
a
v
g
(
T
2
,
T
1
)
. We point out that in other works average often refers to a symmetric definition. Using the nomenclature of Pesquita et al. 9, Eq. (3) may be referred to as asymmetric bestmatch average, here average for short.
Instead of taking the average the maximum similarity between a term annotating one of the domain objects and a term annotating the other domain object can be used to define the following symmetric measure:
s
i
m
m
a
x
(
T
1
,
T
2
)
=
max
n
1
∈
T
1
,
n
2
∈
T
2
s
i
m
(
n
1
,
n
2
)
.
Equation (4) can be considered a simplified case of Eq. (3) because instead of averaging over all bestpairwise terms for each
n
1
∈
T
1
compared to
n
2
∈
T
2
only the highest similarity of all possible pairs is retained. Therefore, we will show the algorithm applied to Eq. (3) and sketch the changes for Eq. (4) later. One can use equation (3) or (4) to define a similarity between a set of query terms Q
, i.e.,
T
1
=
Q
and an object in a database. Then, Q can represent any set of terms from the ontology O whereas
T
2
refers to database objects (such as diseases annotated to HPO terms). As we are using this setup for the similarity queries we will omit the index and refer to T2 as the target set T. See Figure 1 for an example computation of sim^{avg}.
<p>Figure 1</p>Example Computation of sim^{avg}
Example Computation of sim^{avg}. Computation of sim^{avg }on a DAG with six nodes. The target set is T
=
{
B
,
C
}
(black nodes) and the query set Q
=
{
D
,
F
}
(nodes with horizontal lines). The IC value of a node is shown in a small, dashed, attached oval. The most similar terms for D and F are B and C respectively, because IC(B) > IC(A) and IC(C) > IC(B). Therefore, s
i
m
a
v
g
(
Q
,
T
)
=
s
i
m
(
D
,
B
)
+
s
i
m
(
F
,
C
)
2
=
I
C
(
B
)
+
I
C
(
C
)
2
=
2
+
2
.
5
2
. Note that only terms involving nodes in
T
I
M
P
L
=
{
A
,
B
,
C
}
were considered in the calculation.
Because we later make use of scores derived at the maximization step in Eq. (3) we define:
s
i
m
(
n
1
,
T
)
=
max
n
2
∈
T
s
i
m
(
n
1
,
n
2
)
,
to be the target set similarity score of n_{1 }against a target set T. To avoid confusion we will denote scores of the score distribution of sim^{avg }by S and target set similarity scores s
i
m
(
n
,
T
)
by s.
Definition of statistical significance for semantic similarity scores
In this paper we will present methods for analytically calculating the probability distribution of similarity scores for comparisons between a query set Q with q terms against an item that has been annotated with a target set T of nodes. For example, if a clinician chooses a set Q of HPO terms describing abnormalities seen in a patient and uses Eq. (3) to calculate an observed score S_{obs }to a disease that has been annotated with terms of the HPO, we would like to know the probability of a randomly chosen set of q nodes achieving a score of S_{obs }or greater. In this case, each disease in the database represents a target set (for instance, there are currently over 5000 diseases in the clinical database used by the Phenomizer at the HPO Web site).
In other words, our methods will be used to calculate a Pvalue for the null hypothesis that a similarity score of S_{obs }or greater for a set of q query terms Q and a target set T has been observed by chance. We take all queries to be equally likely and define the Pvalue to be the proportion of queries having a score of at least S_{obs}:
P
q
,
T
s
i
m
(
S
≥
S
o
b
s
)
=

{
Q

s
i
m
(
Q
,
T
)
≥
S
o
b
s
,
Q
=
{
n
1
,
.
.
.
,
n
q
}
⊆
V
}

V 
q
.
In this definition all nodes of V can be part of a query, even if one node is an ancestor of the other. Note that the number of distinct scores for the complete score distribution of
P
q
,
T
s
i
m
is dependent on q
,
T
, and the similarity measure.
Simulation of patients for clinical diagnosis
Similar to our previous work 17, we use simulations to compare different approaches. Using 1701 OMIM diseases currently annotated with 25 HPO terms in the Phenotypic abnormality subontology, we generated artificial queries by (i) taking all terms annotated to the disease with no noise or imprecision as the query (NONE), (ii) randomly exchanging one term if q = 3 or q = 4 and two terms if q = 5 (NOISE), (iii) with probability 0.5 exchange a term with one of its parent terms if possible, (IMPRECISION), or (iv) using first IMPRECISION then NOISE.
For each of the 1701 OMIM diseases we generate the query as described above and rank all diseases using one of the measures (Score, Pvalue sampled 10^{3}, 10^{4}, or 10^{5 }times, and Pvalue exact). We then calculate the rank of the disease from which the query was generated. In case of ties we take the average rank (e.g. if four diseases rank first with the same value, all four get rank 2.5). Note that for the rankings using Pvalues (sampled or exact) we ranked first by Pvalues and then by score.
Results
A naive algorithm: exhaustive computation of score distributions
We represent the score distribution as S
D
=
{
(
S
1
,
F
1
)
,
…
,
(
S
k
,
F
k
)
}
. Every pair
(
S
i
,
F
i
)
∈
S
D
contains a unique score S_{i }and a count F_{i }that defines its frequency within the distribution.
A naive approach to calculating the complete score distribution is to determine the similarity of each possible term combination Q ⊆ V of size q with the fixed target set T. The complete procedure is outlined in Algorithm 1. It requires two basic operations that are applied to the set S
D
. The first operation called getScorePair returns the pair that represents the given score or nil in case the entry does not exist. The second operation denoted putScorePair puts the given pair into the set SD, overwriting any previously added pair with the same score. For further analyses we assume that both operations have constant running time.
Input: V, q, T
Output: Score distribution SD={(S1,F1),…,(Sk,Fk)}
1 S
D
=
∅
2 foreach Q = {n_{1}, n_{2},..., n_{q}} ⊆ V do
3
S
n
e
w
←
s
i
m
a
v
g
(
Q
,
T
)
4
(
S
,
F
)
←
g
e
t
S
c
o
r
e
P
a
i
r
(
S
D
,
S
n
e
w
)
5 if (S, F) ≠ nil then
6 p
u
t
S
c
o
r
e
P
a
i
r
(
S
D
,
(
S
n
e
w
,
F
+
1
)
)
7 else
8 p
u
t
S
c
o
r
e
P
a
i
r
(
S
D
,
(
S
n
e
w
,
1
)
)
9 return SD
Algorithm 1: Naive score distribution computation for sim^{avg}
As the number of possible term combinations is
(
q

V

)
and each similarity computation (line 3) costs
O
(
q
⋅

T

)
operations for Eq. (3) Algorithm 1 runs in O
(

V

q
⋅
q
⋅

T

)
time. A typical size of V = 10000 as for the HPO demonstrates that the naive approach is impractical for values q > 2. The naive approach neglects the relationships of the nodes in G and T. We will exploit these relationships in the next section and group nodes in G according to their contribution to the score distribution computation.
A faster algorithm: exploiting redundant computations
Recall that all terms from the target set T are contained in TIMPL. We will prove now that only the IC values of nodes in TIMPL are relevant for the score distribution computation.
Lemma 1. Given a DAG G=(V,E) and a target set T
=
{
n
1
,
…
,
n
k
}
⊆
V
, all scores in the score distribution of the similarity measure of Eq. (3) are derived from IC values of the nodes in TIMPL.
Proof. Computing the complete score distribution involves repeatedly evaluating s
i
m
a
v
g
(
Q
,
T
)
in Alg. 1 using equation (3). The first step for the computation of Eq. (3) is to maximize sim(n_{1},n_{2}) for each node
n
1
∈
Q
compared to nodes
n
2
∈
T
. The maximum IC value for sim(n_{1}, n_{2}) must be taken from a node in TIMPL, because by definition
A
n
c
(
n
2
)
⊆
T
I
M
P
L
.
Lemma 1 implies that the computations in the naive algorithm, which enumerates all nodes in V, are highly redundant as the size of TIMPL is an upper bound on the number of different target set similarities encountered during score distribution computation. Figure 2 shows the contribution of all possible queries of size q = 2 for an example ontology. For instance, whenever node C or D are part of a query the target set similarity score obtained from Eq. (5) is IC(C) = 4, highlighted in red in Figure 2, and used for computing simavg(Q,T).
<p>Figure 2</p>Redundancy in Naive Score Distribution Computation with sim^{avg }for Queries of Size Two
Redundancy in Naive Score Distribution Computation with sim^{avg }for Queries of Size Two. Computation of the score distribution for sim^{avg }on a DAG G
with four nodes for all possible queries of size two. The target set T
=
{
A
,
B
,
C
}
is shown as black nodes. Note that T
=
T
I
M
P
L
here. The IC value for nodes is shown in a small dashed oval. All computations of Eq. (5) that result in the same target similarity score are colored in blue, green, and red for the target set similarity scores 0, 2, and 4, respectively.
Therefore, instead of enumerating over the nodes in V, we will first group nodes that have the same target set similarity score s in the maximization step in Eq. (3). Denote all nodes n ∈ V that have the same target set similarity score s for a given target set T as N_{s}:
N
s
=
{
n

n
∈
V
,
s
i
m
(
n
,
T
)
=
s
}
.
Example 1. It can be seen in Figure 2 that N_{0 }= {A}, N_{2 }= {B}, and N_{4 }= {C, D} for G with T={A,B,C}.
Observe that two nodes
n
i
,
n
j
∈
T
I
M
P
L
,
n
i
≠
n
j
, belong to the same set N_{s}, if IC(n_{i}) = IC(n_{j}). This observation will be essential when we devise an algorithm for computing N_{s}.
The intuition behind the fast computation is that instead of selecting combinations of all nodes of V and constructing the score distribution one by one, we focus on the combinations of different target set similarity scores s and use their frequency N_{s} to avoid redundant enumeration. For any T the set U
of distinct target set similarity scores is defined as:
U
=
{
I
C
(
n
)

n
∈
T
I
M
P
L
}
.
Instead of considering sets of nodes in V we will now consider multisets M^{q }of target set similarity scores in U, where M^{q} = q. In order to do that we define as M
the multiset induced by all target similarity scores s and their corresponding multiplicities m, that is,
M
=
{
(
s
1
,
m
1
)
,
…
,
(
s
d
,
m
d
)

s
i
∈
U
,
m
i
=

N
s
i

}
.
Then
M
a
l
l
q
represents the set of all multi subsets of M that have multiset cardinality q, i.e.,
M
a
l
l
q
=
{
M
q

M
q
⊆
M
,

M
q

=
q
}
.
The value of sim^{avg }computed for a particular M^{q }is the same for all query sets of nodes that correspond to M^{q }(see Figure 2, Example 2). Therefore, if we can calculate the number of such sets as well as the score corresponding to each multiset M^{q }of target set similarity scores in U, we can determine the distribution of similarity scores sim^{avg }for all possible queries of any given size q.
Denote the similarity for a multiset M^{q }as:
s
i
m
a
v
g
(
M
q
)
=
1
q
∑
(
s
,
m
)
∈
M
q
m
⋅
s
.
The number of ways of drawing m nodes from a component of size N_{s} can be calculated using the binomial coefficient. The total number of combinations is then the product of all binomial coefficients, denoted as the multiset frequency for a multiset M^{q}:
f
r
e
q
(
M
q
)
=
∏
(
s
,
m
)
∈
M
q

N
s

m
.
Example 2. In total there are 2 query sets with s
i
m
a
v
g
(
Q
,
T
)
=
2
for the DAG in Figure 2, namely {A, C}, {A, D}. After preprocessing, we obtain N_{0 }= {A}, N_{2 }= {B}, and N_{4 }= {C, D} (Example 1). Alg. 2 enumerates all valid multisets of cardinality 2 for the sets N_{s }considering their size N_{s }. The only way of attaining an average score of 2 is to select one node out of N_{0 }and N_{4}, represented by the multiset M^{2 }= {(0,1), (4,1)} for which sim^{avg}(M^{2}) = 2. The multiset frequency of M^{2 }gives the same result as shown in Figure 2, f
r
e
q
(
M
2
)
=

N
0

1
⋅

N
4

1
=
1
⋅
2
=
2
. Instead of iterating over two sets we consider one multiset.
Theorem 1. Let SD={(S1,F1),…,(Sk,Fk)} be the score distribution computed with sim^{avg }for an ontology DAG G=(V,E), target set T
⊆
V
and query size q. The frequency F with which any given score S occurs amongst all possible queries of size q is then:
F
=
∑
M
q
∈
M
a
l
l
q
,
s
i
m
a
v
g
(
M
q
)
=
S
f
r
e
q
(
M
q
)
.
A proof of Theorem 1 is provided in Appendix A and a faster algorithm based on Theorem 1 is shown in Alg. 2. We enumerate all distinct multisets of Mallq and add their frequency to the score distribution SD, instead of iterating over all sets of size q in Alg. 1, thereby reducing the number of operations. In order to apply the algorithm to score distribution computation for sim^{max}, line 3 of Alg. 2 needs to be replaced. Instead of computing the average of all scores in the multiset, the maximum among them is assigned to S_{new}.
Preprocessing of the DAG for faster computation
So far we have neglected how we can compute the values 
N
s

,
s
∈
U
but we will introduce an efficient algorithm in this section. We denote the algorithm as preprocessing because computation of N_{s} is independent of q. The preprocessing will divide the original graph into a set of connected components from which the N_{s} values can be deduced.
Input: Mallq
Output: Score distribution SD={(S1,F1),…,(Sk,Fk)}
1 SD=∅
2 foreach multiset
M
q
∈
M
a
l
l
q
do
3 S_{new }← sim^{avg}(M^{q})
4 (S,F)←getScorePair(SD,Snew)
5 if (S,F) ≠ nil then
6 p
u
t
S
c
o
r
e
P
a
i
r
(
S
D
,
(
S
n
e
w
,
F
+
f
r
e
q
(
M
q
)
)
)
7 else
8 p
u
t
S
c
o
r
e
P
a
i
r
(
S
D
,
(
S
n
e
w
,
f
r
e
q
(
M
q
)
)
)
9 return SD
Algorithm 2: Faster score distribution computation for sim^{avg}
First, we invert the direction of all edges in E such that the edges are directed from the root towards the leaves of the DAG, and introduce edge weights w_{i,j }to the edges of G. Let
w
i
,
j
=
I
C
(
n
i
)
,
if
n
i
∈
T
I
M
P
L
max
{
w
h
,
i

e
h
,
i
∈
E
}
otherwise
.
The edge weights are defined in a recursive manner. First, all weights of edges emerging from nodes in TIMPL are set. Then the maximum edge weight of all incoming edges for each node not in TIMPL are propagated to all outgoing edges of the node, and as such propagated throughout the graph. Computing the edge weights is efficiently done after the nodes of G have been sorted in topological order, see Alg. 3. We now iterate across all nodes n_{i }∈ V. For each node
n
i
∈
V
,
n
i
∉
T
I
M
P
L
, there is at least one path that leads to the node
n
j
=
argmax
n
k
∈
T
s
i
m
(
n
i
,
n
k
)
. If a node has multiple parents, then by construction of the edge weights, an edge with a maximum weight will be a member of a path to n_{j}. We therefore remove all other incoming edges. If there are multiple incoming edges with an identical, maximum edge weight, one of them can be chosen arbitrarily and the others are removed (Alg. 3, lines 79). We now iterate over all remaining edges e_{i,j }and remove all edges for which
n
i
,
n
j
∈
T
I
M
P
L
holds (Alg. 3, lines 1012). Note that exactly 
T
I
M
P
L

many connected components C_{i }one for each
n
i
∈
T
I
M
P
L
remain.
For all pairs of connected components such that IC(n_{i}) = IC(n_{j}) for ni,nj∈TIMPL,ni≠nj, the connected components C_{i }and C_{j }are merged to arrive at the desired sets
N
s
,
s
∈
U
(Alg. 3, lines 1316).
All these steps are summarized in Alg. 3 and Figure 3.
<p>Figure 3</p>Overview of the Algorithm for Preprocessing
Overview of the Algorithm for Preprocessing. The general steps of Alg. 3 are shown on the DAG and T
of Figure 1. Nodes in
T
I
M
P
L
are colored in black. The IC value of a node is depicted in a dashed oval.
Theorem 2. Given a DAG G=(V,E) and a target set T
=
{
n
1
,
…
,
n
k
}
⊆
V
the score distribution of Eq. (3) is computed by Alg. 2 and Alg. 3 in O
(

E

+

V

+
M
(

T
I
M
P
L

,
q
)
)
time and space.
Proof. The preprocessing of the DAG in Alg. 3 involves inverting edges, topological ordering of V,
Input: V, TIMPL
Output: node sets with identical target similarity score, i.e., N_{s}
1 for n_{i }∈ V in topological order do
2 for j in e_{i,j }∈ E do /* Set weights */
3 if ni∈TIMPL then
4 w_{i,j }← IC(n_{i})
5 else
6 w_{i,j }← max{w_{h,I }e_{h,i }∈ E}
7 for n_{i }∈ V \ root do
8 choose e_{h,i }∈ E s.t. w_{h,i }≥ w_{h',i }for all edges e_{h',i}∈ E
9 remove all incoming edges of n_{i }except e_{h,i}
10 for e_{i,j }∈ E do /* Connected components C_{i }*/
11 if ni,nj∈TIMPL then
12 remove e_{i,j }from E
13 for s
∈
{
I
C
(
n
i
)

n
i
∈
T
I
M
P
L
}
do /* Merging */
14
N
s
=
∅
15 foreach ni∈TIMPL do
16 N_{s }← N_{s }∪ C_{i}
17 return N_{s}
Algorithm 3: Graph preprocessing for faster computation
introducing edge weights to E, removing edges in E, and computing the connected components of G. This can be done with depthfirst search (DFS) traversals of G with to a worstcase performance of O
(

E

+

V

)
time and space.
Algorithm 2 runs in O
(
M
(

T
I
M
P
L

,
q
)
)
time and space. The outer foreach loop runs over all distinct multisets with cardinality q. The multiset coefficient M
(

T
I
M
P
L

,
q
)
provides an upper bound for the number of these multisets. In each iteration the computation of the similarity score (line 3) and the multiset frequency, freq(M^{q}), have constant cost assuming a preprocessed lookup table for binomial coefficients and if common partial sim^{avg }values are stored between the iterations, avoiding recomputation for similar multisets. In total, Alg. 2 and Alg. 3 run in O(E+V+M(TIMPL,q)) time and space.
The theorem concludes the improvement to the naive algorithm, for example on average
T
I
M
P
L
~
38
for all diseases currently annotated with terms of the HPO, which currently has approximately 10000 terms and 13000 relations. For instance, for a query with 5 terms, the naive algorithm would thus run in time proportional to 10000^{5 }· 5 · 38 = 1.9 × 10^{22}, and the new algorithm in time proportional to 9000 + 11000 + 5 · M(38, 5) = 4.3 × 10^{6}.
Experiments
We now show the results of the new algorithm applied to the HPO 7. In our previous work we implemented the Phenomizer as a system for experts in the differential diagnosis in medical genetics; the Phenomizer can be queried with a set of HPO terms to get a ranked list of candidate diseases most similar to the query based on Pvalues derived from Resnik similarity scores, Eq. (3) 17. However, for the Phenomizer we used Monte Carlo sampling to approximate the score distribution and we will investigate now the difference in using the exact Pvalue compared to sampling.
As we are interested in ranking diseases for differential diagnosis we will take a similar simulation approach as in 17 and generate sets of artificial patients for which we know the OMIM disease, see Methods. In Figure 4 the results are shown for the investigated scenarios NONE, NOISE, IMPRECISION, and NOISE + IMPRECISION. We compared the ranking of patients with the similarity score alone, sampling based Pvalues (10^{3 } 10^{5 }repetitions, the latter used in the Phenomizer), and exact computation using the algorithm in this work. In all cases, using the exact Pvalue computation significantly outperforms the four alternative ranking methods (MannWhitney Pvalue < 0.001) and ranks the true disease on rank one most of the time. The improvement for the exact score distribution computation is due to the finegrained resolution especially for small Pvalues, where sampling is often underrepresented, but which are important for selecting the best rank (see Additional File 1).
<p>Figure 4</p>Impact of Exact Pvalue Computation for Clinical Diagnostics with the HPO
Impact of Exact Pvalue Computation for Clinical Diagnostics with the HPO. Simulations for Clinical Diagnostics using the HPO. Patient (phenotype) data was simulated and queried against the complete database of all 4992 annotated diseases. The best result is obtained if the original disease is assigned the rank one (yaxis) by the search algorithm. Different approaches are compared (xaxis). Data were generated without error NONE and with NOISE (top row, left and right) and with IMPRECISION and both IMPRECISION and NOISE (bottom row, left and right) as explained in the Methods section. The mean rank is shown below each boxplot.
<p>Additional file 1</p>
Additional File 1 contains some additional plots showing the differences in ranking by exact and sampled Pvalues for Clinical Diagnostics with the HPO.
Click here for file
We then investigated the runtime for different q values as compared to using the naive algorithm and Monte Carlo sampling (Table 1). For that purpose we selected four diseases with a different number of annotated HPO terms, and therefore different size of TIMPL, and show the runtime of the three approaches in milliseconds. The naive algorithm cannot be utilized for q > 2. The exact Pvalue computation is faster than random sampling with 10^{5 }repetitions for q = 2,3 and for the disease with only 17 terms in TIMPL independent of the analyzed q. Starting from q = 4 the sampling based approach is faster for large TIMPL because of the huge size of the score distribution to be computed, but even for q = 5 the complete score distribution can be computed in under 4 seconds for diseases with many annotations. Note again that the average size of TIMPL is 38 in the HPO.
<p>Table 1</p>Runtime in milliseconds averaged over 20 runs comparing the naive, exact, and sampled distribution computation for q = 2,3,4, and 5
Runtime Analysis with the HPO
runtime in milliseconds
OMIM ID

T


T
I
M
P
L


U

naive
exact
sampled*
q = 2
264300
5
17
16
3779
4
50
613124
7
36
36
3794
6
53
113450
12
80
72
3789
6
65
129500
20
66
61
3702
15
89
q = 3
264300
5
17
16
~ 1.2 · 10^{7}
4
49
613124
7
36
36
~ 1.2 · 10^{7}
6
53
113450
12
80
72
~ 1.2 · 10^{7}
19
66
129500
20
66
61
~ 1.2 · 10^{7}
15
79
q = 4
264300
5
17
16

5
46
613124
7
36
36

20
55
113450
12
80
72

250
65
129500
20
66
61

135
77
q = 5
264300
5
17
16

7
48
613124
7
36
36

141
54
113450
12
80
72

3896
63
129500
20
66
61

1776
79
Four OMIM diseases with a varying number of annotated HPO terms (T) were used; 264300: 17β Hydroxysteroid Dehydrogenase III deficiency, 613124: Hydrops fetalis, nonimmune, with gracile bones and dysmorphic features, 113450: Brachydactylydistal symphalangism syndrome, 129500: Ectodermal dysplasia 2, hidrotic. Entries denoted "" were terminated after four hours. *Sampling with 10^{5 }repetitions.
Discussion
In this work we have tackled the unstudied problem of computing the score distribution for similarity searches with ontologies. We have devised an efficient preprocessing of the underlying DAG of the ontology that reduces the complexity for similarity measures based on Resnik's popular definition of similarity 19. We have introduced a new algorithm based on multiset enumeration, which can be applied to score distribution computation for Eq. (3) as well as variants based on maximum similarity Eq. (4). In experiments with the HPO, as well as in theory, we show that the new algorithm is much faster than exhaustive enumeration of the score distribution or resampling approaches and that it is applicable to current ontologies.
The algorithm we describe here can be used as a component of a procedure to find the best hit in a database, i.e., we need to calculate the score for each entry in the database and rank the results according to Pvalue. This allows users to enter a list of characteristics or features in order to identify objects whose characteristics best match the query using semantic similarity. We have implemented our algorithm in the setting of medical diagnostics, where the features are the signs and symptoms of diseases and the domain objects are diseases. We have previously shown that this kind of search is useful for medical differential diagnosis 17.
Summarizing all nodes that have the same target set similarity score makes use of the fact that the pairwise similarity defined by Resnik only considers the common ancestors of the relevant terms (Lemma 1). Extending the proposed algorithm for other popular semantic similarity measures based on the information content of a node, like Jiang and Conrath or Lin 2021, or the symmetric definition of Eq. (3) 12, has not been considered here as definition of pairwise similarity additionally incorporates the information content of the nodes in the query. Therefore, additional steps are necessary which render the computations more complicated. Although this can be considered a limitation of the current approach, we believe the methodology introduced here will prove useful for other measures as well. For example the term overlap similarity measure 22, comparably, only considers common ancestors of query and target set terms, thus an algorithm with similar complexity appears possible from the results presented in this paper. One of the reasons why the Pvalue based rankings outperform the rankings based on scores is that the former account for the annotation bias as observed by Wang et al. 23. The bestmatch average semantic similarity measures based on Resnik, like Eq. (3), were shown to have a strong bias. The annotation bias is a further argument to use Pvalues instead of the similarity scores alone.
In the mentioned study by Wang et al. 23, the authors consider the comparison of two proteins via their annotated GO terms, instead of considering any possible subset of the ontology terms as query as in our search setup. Their approach is to compensate for the annotation bias by simulating the distribution of pairwise similarity scores for all annotated ontology term sets and normalizing using a power transformation. Similarly to our experiments, their method might improve when the exact score distribution is computed using our algorithm.
In a practical implementation of our algorithm, the Pvalues could be precomputed for each entry in the database (such as all the diseases in OMIM or each protein in the human proteome). For small q, the Pvalues could be calculated dynamically. This might be useful if users are allowed to filter out portions of the database from the search based on some predefined groups (for instance, in genetics, the differential diagnosis might be restricted to diseases showing a certain mode of inheritance).
Due to its simple structure the new algorithm could be parallelized to run with several threads with close to linear speedup, by keeping the scores in different hash structures for each thread and merging all hashes at the end to get the complete distribution. Also, as often only the Pvalue is of interest, a branch and bound formulation of the new algorithm might lead to a significant speedup in practice.
Appendix A
In this Appendix, we will prove Theorem 1 for arbitrary q. In the following text, we will outline the approach of the proof and introduce a few new definitions. We can calculate the Pvalues, Eq. (6), by computing the frequency F_{i }of each score S_{i }in the score distribution, i.e., by calculating the number of queries that result in score S_{i }for each possible score. We will consider all query sets Q that result in score S, denoted as
Q
S
later in Eq. (15). These initial query sets consist of the nodes from the Ontology DAG G=(V,E). Subsequently, we will substitute sets of nodes Q by multisets
M
q
(
Q
)
over their target set similarity scores in Eq. (16). This is the important switch that establishes the independence of the number of nodes in the graph by only considering their target set similarity scores. At this step, changing from sets to multisets is necessary, because the same target set similarity score may occur more than once given nodes in a single Q. However, the induced multisets from all sets in QS are themselves not unique and therefore we will use the multiset frequency, Eq. (12), over the set of unique multisets
M
S
q
given QS to compute the desired quantity F in the proof.
We are interested in the set QS of all sets {n_{1},..., n_{q}} of nodes {n_{1},..., n_{q}} ⊆ V, which result in the same average score S. That is, QS is the set of all queries of size q that result in the same average score S:
Q
S
=
{
{
n
1
,
…
,
n
q
}

{
n
1
,
…
,
n
q
}
⊆
V
,
s
i
m
a
v
g
(
{
n
1
,
…
,
n
q
}
,
T
)
=
S
}
.
The core message of Theorem 1 is that we can define a multiset M^{q }over the target set similarity scores s whose frequency can be used to compute the frequency F of scores S in the score distribution. A necessary first step therefore is to express a query set Q
=
{
n
1
,
…
,
n
q
}
⊆
V
as a multiset Mq(Q):
M
q
(
Q
)
=
{
(
s
1
,
m
1
)
,
…
,
(
s
o
,
m
o
)

s
i
∈
U
Q
,
m
i
=
m
s
i
Q
}
,
where
U
Q
=
{
s
i

n
i
∈
Q
,
s
i
m
(
n
i
,
T
)
=
s
i
}
and
m
s
i
Q
=

{
n
i

n
i
∈
Q
,
s
i
m
(
n
i
,
T
)
=
s
i
}

.
The underlying set U
Q
for a multiset Mq(Q) consists of all existing distinct target set similarity scores s_{i }of the nodes in Q, Eq. (17), and their multiplicity is the number of nodes in Q that share the same score s_{i}, Eq. (18).
Now that we know how to create a multiset of target set similarity scores from any given set of nodes in V, we need another variable MSq to represent all distinct multisets that can be generated using Eq. (16) from the set QS. The set of distinct multisets MSq generated for a given QS is defined as:
M
S
q
=
{
M
q
(
Q
)

Q
∈
Q
S
}
.
We can now state the proof of Theorem 1 as follows.
Proof.
F
=

Q
S

=
∑
M
q
∈
M
S
q
∏
(
s
,
m
)
∈
M
q

N
s

m
=
∑
M
q
∈
M
S
q
f
r
e
q
(
M
q
)
=
∑
M
q
∈
M
a
l
l
q
,
s
i
m
a
v
g
(
M
q
)
=
S
f
r
e
q
(
M
q
)
Eq. (20) merely restates the definition of the Frequency F given by Eq. (15), namely the number of all queries Q
⊆
V
that result in sim^{avg }= S. Note that Eq. (15) is representing the number of such queries in terms of sets of nodes of the ontology. Eq. (21) switches the representation from nodes in V to multisets MSq over the similarity scores of nodes in V using Eq. (19) and the definition of multiset frequency given in Eq. (12). Eq. (22) follows directly from the definition of the multiset frequency in Eq. (12). The equality between Eq. (22) and (23) is a direct consequence of Eq. (15) and (19).