ontologySimilarity Examples

Daniel Greene

2016-10-27

This vignette demonstrates how some typical simple analyses can be done with ontologySimilarilty.

Computing similarity matrices

Similarity matrices can be computed with the function get_sim_grid. Full detail on using the function can be found in the help file: ?get_sim_grid, but briefly, the typical ingredients for computing a similarity matrix are:

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

information_content <- descendants_IC(hpo)

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

sim_mat <- get_sim_grid(ontology=hpo, term_sets=term_sets)
sim_mat
##           [,1]      [,2]      [,3]       [,4]      [,5]      [,6]
## [1,] 1.0000000 0.2324045 0.2806641 0.22478731 0.2268486 0.2712622
## [2,] 0.2324045 1.0000000 0.2550073 0.18568647 0.2068138 0.2063774
## [3,] 0.2806641 0.2550073 1.0000000 0.22167974 0.2447336 0.1519214
## [4,] 0.2247873 0.1856865 0.2216797 1.00000000 0.3493560 0.1842775
## [5,] 0.2268486 0.2068138 0.2447336 0.34935602 1.0000000 0.1390903
## [6,] 0.2712622 0.2063774 0.1519214 0.18427754 0.1390903 1.0000000
## [7,] 0.1255041 0.1981318 0.2676284 0.06214505 0.1999068 0.1627042
##            [,7]
## [1,] 0.12550408
## [2,] 0.19813182
## [3,] 0.26762835
## [4,] 0.06214505
## [5,] 0.19990679
## [6,] 0.16270421
## [7,] 1.00000000

sim_mat can then of course be used by other packages’ clustering functions. Note that many of these expect a distance matrix, not a similarity matrix. Thus transformation may be required, e.g.

dist_mat <- max(sim_mat) - sim_mat
plot(hclust(as.dist(dist_mat)))

Assessing statistical significance of similarity

Given a collection of objects annotated with ontological terms, you can represent them as a list of character vectors of term IDs for compatibility with ontologySimilarity’s functions: a list of term sets.

Given a particular subgroup of interest - for example, in the context of the collection of all genes’ GO annotations, this could be a group of genes identified through an experiment - a natural question might be, ‘are these objects as a group particularly similar to one another?’.

We can attempt to answer that question by calculating a ‘group similarity’: a measure of similarity for a group of ontologically annotated objects, or ‘list of term sets’. ontologySimilarity supports two different methods for calculating the similarity of a group.

The function get_sim is used to calculate these similarities. The argument group_sim can be either "average" or "min" accordingly.

The value of the similarity itself doesn’t tell you how similar the group members are relative to other subsets. So to evaluate the signficance of the group similarity of subset S within population of term sets L, we compute a p-value by permutation test: that is, we compute the proportion of the ‘null’ distribution of similarities of all subsets of the same size as S that are greater than the similarity of S.

This is done by passing the indices of S within L to the function get_sim_p. Instead of computing the p-value exactly by comparing to the similarity of all choose(length(L), length(S)) permutations, the function get_sim_p samples a specified maximum number of random subsets.

For efficiency in the situation where only statistically significant results are of interest, get_sim_p allows the user to configure a threshold triggering the sampling to stop early, should the strength of evidence against it being significant become strong enough. Precisely, this is done using the arguments:

The function get_sim_p requires an argument called pop_sim which stores information on the similarities of the entire collection of objects. Depending on the type of parameter you pass, it is done in different ways which suit different scenarios.

Using a similarity matrix

One way is to pass a matrix having the similarity between all term set pairs. The null distribution is then estimated by measuring the similarity of random subsets of the matrix. This is the fastest method if such a matrix is available as it doesn’t require any between-object similarities to be calculated. However computing such a matrix may be infeasible if N is large. Note that this method can be used for any matrix of similarities, and no references to ontologies are required.

Using a sim_index object

One way is to pass an argument of class sim_index as created with the function create_sim_index. To create the index you need to pass the list of term sets and an ontology argument. Note that if you do not wish to store the sim_index object you can call the function get_sim_p_from_ontology. See ?create_sim_index and ?get_sim_p_from_ontology for more details. The null distribution of group similarities is then estimated by calculating group similarities of random subsets of L. This method is useful when you don’t want to compute and store a similarity matrix - for example if N is large.

See ?get_sim and ?get_sim_p for further details.

Example

In this example we’ll generate a collection 100 random sets of HPO terms and map them to minimal sets. We’ll then assess whether the subgroup comprising the first 10 sets is significantly similar - first by using the ontology_index object and information content directly, then using a similarity matrix.

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

#lets measure the group similarity of objects 1-10
group <- 1:10

get_sim_p_from_ontology(
    ontology=hpo, 
    information_content=information_content,
    term_sets=collection,
    group=group)
## [1] 0.9420579
sim_mat <- get_sim_grid(ontology=hpo, term_sets=collection)

#p-value by "matrix" method
get_sim_p(
    sim_mat,
    group=group)
## [1] 0.9320679

Against a vector

Another scenario might be: you’ve measured the similarity between a collecton of ontologically annotated objects and a foreign object, and have obtained a vector of similarities, and now, you’re interested in whether some subgroup of the collection are particularly similar on average. In this case you can pass a numeric vector to get_sim_p.

In this example, we’ll generate some hypothetical genes with GO annotation [i.e. randomly sample 8 GO terms per gene and map to minimal sets]. Then - supposing we are interested in genes which have functions bearing on the ‘golgi apparatus’ - use get_profile_sims to calculate the similarity of the genes to an ‘ontological profile’ containing golgi related terms. Finally - supposing for example genes 1-3 came up in the results of an experiment - we’ll calculate a similarity p-value of genes 1-3 to the ‘golgi GO profile’ using get_sim_p, passing it the pre-computed profile similarities.

data(go)

genes <- replicate(simplify=FALSE, n=100, expr=minimal_set(go, sample(go$id, size=8)))
names(genes) <- paste("gene", 1:length(genes))
genes[1:3]
## $`gene 1`
## [1] "GO:0071569" "GO:0060387" "GO:0050144" "GO:0036116" "GO:0090482"
## [6] "GO:0018787" "GO:0019203" "GO:0002331"
## 
## $`gene 2`
## [1] "GO:1901616" "GO:0070313" "GO:0047195" "GO:0007361" "GO:1902634"
## [6] "GO:0097361" "GO:1904606" "GO:0000419"
## 
## $`gene 3`
## [1] "GO:0038152" "GO:0019337" "GO:1903794" "GO:0046257" "GO:0043054"
## [6] "GO:0047442" "GO:0015949" "GO:1905347"
go_profile <- as.character(go$id[grep(x=go$name, pattern="golgi apparatus", ignore.case=TRUE)])
go$name[go_profile]
##                                GO:0005794 
##                         "Golgi apparatus" 
##                                GO:0034067 
## "protein localization to Golgi apparatus" 
##                                GO:0044177 
##               "host cell Golgi apparatus" 
##                                GO:0044431 
##                    "Golgi apparatus part" 
##                                GO:0045053 
##    "protein retention in Golgi apparatus" 
##                                GO:0048280 
##     "vesicle fusion with Golgi apparatus" 
##                                GO:1904381 
##        "Golgi apparatus mannose trimming"
profile_sims <- get_profile_sims(ontology=go, term_sets=genes, profile=go_profile)
profile_sims
##     gene 1     gene 2     gene 3     gene 4     gene 5     gene 6 
## 0.12535639 0.25561367 0.13172914 0.24288156 0.17762680 0.13918669 
##     gene 7     gene 8     gene 9    gene 10    gene 11    gene 12 
## 0.22830071 0.18060944 0.09339188 0.21690501 0.17691685 0.24283967 
##    gene 13    gene 14    gene 15    gene 16    gene 17    gene 18 
## 0.13712868 0.13982364 0.10912224 0.10326966 0.06235164 0.14704090 
##    gene 19    gene 20    gene 21    gene 22    gene 23    gene 24 
## 0.19370571 0.17669249 0.22279466 0.16827874 0.21705232 0.18592535 
##    gene 25    gene 26    gene 27    gene 28    gene 29    gene 30 
## 0.15511051 0.11506600 0.14603679 0.18512554 0.15106479 0.23740671 
##    gene 31    gene 32    gene 33    gene 34    gene 35    gene 36 
## 0.19735416 0.15340610 0.13062262 0.19181452 0.15425828 0.20594077 
##    gene 37    gene 38    gene 39    gene 40    gene 41    gene 42 
## 0.18813951 0.06095896 0.12344699 0.15836879 0.23511225 0.13049328 
##    gene 43    gene 44    gene 45    gene 46    gene 47    gene 48 
## 0.11160574 0.14184513 0.18382310 0.13550545 0.23349108 0.26729240 
##    gene 49    gene 50    gene 51    gene 52    gene 53    gene 54 
## 0.19075559 0.15001962 0.19645278 0.09045459 0.15136831 0.17828567 
##    gene 55    gene 56    gene 57    gene 58    gene 59    gene 60 
## 0.13667923 0.11907502 0.15261857 0.18259343 0.33245462 0.14121220 
##    gene 61    gene 62    gene 63    gene 64    gene 65    gene 66 
## 0.14748221 0.14926361 0.11777674 0.17373804 0.23312454 0.16741643 
##    gene 67    gene 68    gene 69    gene 70    gene 71    gene 72 
## 0.15604781 0.23370550 0.14032199 0.12077355 0.16950362 0.08830179 
##    gene 73    gene 74    gene 75    gene 76    gene 77    gene 78 
## 0.15298999 0.13696887 0.15199468 0.26112899 0.22092678 0.15921844 
##    gene 79    gene 80    gene 81    gene 82    gene 83    gene 84 
## 0.23477853 0.17411424 0.10785546 0.09959734 0.17220584 0.12676252 
##    gene 85    gene 86    gene 87    gene 88    gene 89    gene 90 
## 0.11924316 0.24074072 0.12250996 0.13931193 0.22776981 0.10555832 
##    gene 91    gene 92    gene 93    gene 94    gene 95    gene 96 
## 0.13380144 0.16277767 0.10262271 0.09351604 0.14536490 0.26852243 
##    gene 97    gene 98    gene 99   gene 100 
## 0.13833015 0.17295118 0.14272117 0.28573472
#Note that you can pass character vectors to get_sim_p
get_sim_p(profile_sims, c("gene 1", "gene 2", "gene 3"))
## [1] 0.4315684

Sampling the null distribution

Because of the ‘early stopping’, get_sim_p should be sufficiently performant to loop or lapply over a long list of subgroups. However, p-values for groups of the same size are technically being computed by comparison with the same null distribution. Thus, if the number of applications required is very long, the user could consider storing the null distributions for particular group sizes, and computing p-values later by comparing with the actual group similarities (calculated with get_sim).

The function sample_group_sim can be used to sample from the null distribution of similarities.

group_sim <- get_sim(sim_mat, group=group)

samples <- sample_group_sim(sim_mat, group_size=length(group))
hist(samples)
abline(v=group_sim, col="red")