# Introduction to ontologySimilarity

#### Daniel Greene

#### 2016-10-27

`ontologySimilarity` is part of the ‘ontologyX’ family of packages (see the ‘Introduction to ontologyX’ vignette supplied with the `ontologyIndex` package). It contains various functions for calculating semantic similarity between ontological objects. The functions operate on various kinds of object. It’s useful to look out for particular parameter names, as each kind of object tends to be called the same thing by the functions. To make full use of the features in `ontologySimilarity`, the user is encouraged to gain familiarity of the functions in `ontologyIndex`.

- `ontology` - Objects of class `ontologyIndex`, described in the package `ontologyIndex`.
- `terms` - A `character` vector of term IDs - either representing terms individually, or terms which together annotate a particular thing, e.g. term IDs from the Gene Ontology (GO) representing the functional annotations of a gene.
- `term_sets` - A `list` of `character` vectors of term IDs.
- `information_content` - A `numeric` vector of information content values for individual terms, named by term IDs. Typically this would be used in an evaluation of either Resnik or Lin’s between-term similarity expression.
- `pop_sim` - An object which stores information about similarites of a population of (ontological) objects, either to one another or to some foreign object. Used to increase performance when many look-ups of similarity are required.

Various kinds of similarity can be calculated, including:

- similarity between individual terms,
- similarity between sets of terms,
- group similiarity of a list of term sets (e.g. taking the average similarity between all pairs of sets),
- p-values for significance of group similiarity.

Some key functions are:

- `get_term_sim_mat` for pairwise term similarities which returns a matrix,
- `get_sim_grid` for pairwise similarities between sets of terms which returns a matrix,
- `get_sim` for group similarity,
- `get_sim_p` for computing a p-value for group similarity.

## Example

To use the package, first load `ontologyIndex` and an `ontology_index` object. Here we demonstrate using the Human Phenotype Ontology, `hpo`.

    library(ontologyIndex)
    library(ontologySimilarity)
    data(hpo)
    set.seed(1)

Next, we’ll set the information content for the terms. This is typically based on some kind of ‘population frequency’, for example: the frequency with which the term is used, explicitly or implicity, to annotate objects in a database. Such frequency information is not always available, but it could still be useful to define the information content with respect to the frequency with which the term is an ancestor of other terms in the ontology (as this still captures the structure of the ontology).

    information_content <- descendants_IC(hpo)

Now we’ll generate some random sets of terms. We’ll sample 5 random term sets (which could for example represent the phenotypes of patients) of 8 terms. Note that here, we call the `minimal_set` function from the `ontologyIndex` package on each sample set to remove redundant terms. Typically, ontological annotations would be stored as such minimal sets, however if you are unsure, it is best to call `minimal_set` on each term set to guarantee the similarity expressions are faithfully evaluated (the package chooses not to map to minimal sets by default for speed).

    term_sets <- replicate(simplify=FALSE, n=5, expr=minimal_set(hpo, sample(hpo$id, size=8)))
    term_sets

    ## [[1]]
    ## [1] "HP:0004374" "HP:0006558" "HP:0010132" "HP:0100059" "HP:0003244"
    ## [6] "HP:0040272" "HP:0100503" "HP:0011223"
    ##
    ## [[2]]
    ## [1] "HP:0010839" "HP:0000929" "HP:0003312" "HP:0002835" "HP:0011540"
    ## [6] "HP:0006775" "HP:0012533" "HP:0009161"
    ##
    ## [[3]]
    ## [1] "HP:0011911" "HP:0430010" "HP:0006707" "HP:0012626" "HP:0100385"
    ## [6] "HP:0003414" "HP:0011114" "HP:0001993"
    ##
    ## [[4]]
    ## [1] "HP:0004397" "HP:0006830" "HP:0000185" "HP:0006748" "HP:0030821"
    ## [6] "HP:0005892" "HP:0008788" "HP:0010457"
    ##
    ## [[5]]
    ## [1] "HP:0009106" "HP:0002990" "HP:0030292" "HP:0011318" "HP:0012826"
    ## [6] "HP:0001712" "HP:0011981" "HP:0007411"

Then one can calculate a similarity matrix, containing pairwise term-set similarities:

    sim_mat <- get_sim_grid(ontology=hpo, term_sets=term_sets)
    sim_mat

    ##           [,1]      [,2]      [,3]      [,4]      [,5]
    ## [1,] 1.0000000 0.2324045 0.2806641 0.2247873 0.2268486
    ## [2,] 0.2324045 1.0000000 0.2550073 0.1856865 0.2068138
    ## [3,] 0.2806641 0.2550073 1.0000000 0.2216797 0.2447336
    ## [4,] 0.2247873 0.1856865 0.2216797 1.0000000 0.3493560
    ## [5,] 0.2268486 0.2068138 0.2447336 0.3493560 1.0000000

Group similarity of phenotypes 1-3, based on `sim_mat`:

    get_sim(sim_mat, group=1:3)

    ## [1] 0.2560253

p-value for significance of similarity of phenotypes 1-3:

    get_sim_p(sim_mat, group=1:3)

    ## [1] 0.2937063