knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

R-CMD-check Codecov test coverage Lifecycle: maturing Project Status: Active – The project has reached a stable, usable state and is being actively developed. rOpenSci CRAN status

BaseSet

The goal of BaseSet is to facilitate working with sets in an efficient way. The package implements methods to work on sets, doing intersection, union, complementary, power sets, cartesian product and other set operations in a tidy way.

The package supports classical and fuzzy sets. Fuzzy sets are similar to classical sets but there is some vagueness on the relationship between the element and the set.

It also allows to import from several formats used in the life science world. Like the GMT and the GAF or the OBO format file for ontologies.

You can save information about the elements, sets and their relationship on the object itself. For instance origin of the set, categorical or numeric data associated with sets...

Watch BaseSet working on the examples below and in the vignettes. You can also find related packages and the differences with BaseSet. If you have some questions or bugs open an issue (remember the Code of Conduct)

Installation

The package depends on some packages from Bioconductor. In order to install some of its dependencies you'll need first to install {BiocManager}:

if (!require("BiocManager")) {
  install.packages("BiocManager")
}

You can install the latest version of BaseSet from Github with:

BiocManager::install("ropensci/BaseSet", 
                     dependencies = TRUE, build_vignettes = TRUE, force = TRUE)

Examples {#Examples}

library("BaseSet")

Sets

We can create a set like this:

sets <- list(A = letters[1:5], B = c("a", "f"))
sets_analysis <- tidySet(sets)
sets_analysis

Perform typical operations like union, intersection. You can name the resulting set or let the default name:

union(sets_analysis, sets = c("A", "B")) 
# Or we can give a name to the new set
union(sets_analysis, sets = c("A", "B"), name = "D")
# Or the intersection
intersection(sets_analysis, sets = c("A", "B"))
# Keeping the other sets:
intersection(sets_analysis, sets = c("A", "B"), name = "D", keep = TRUE) 

And compute size of sets among other things:

set_size(sets_analysis)

The elements in one set not present in other:

subtract(sets_analysis, set_in = "A", not_in = "B", keep = FALSE)

Or any other verb from dplyr. We can add columns, filter, remove them and add information about the sets:

library("magrittr")
set.seed(4673) # To make it reproducible in your machine
sets_enriched <- sets_analysis %>% 
  mutate(Keep = sample(c(TRUE, FALSE), 7, replace = TRUE)) %>% 
  filter(Keep == TRUE) %>% 
  select(-Keep) %>% 
  activate("sets") %>% 
  mutate(sets_origin = c("Reactome", "KEGG"))
sets_enriched

# Activating sets makes the verb affect only them:
elements(sets_enriched)
relations(sets_enriched)
sets(sets_enriched)

Fuzzy sets

In fuzzy sets the elements are vaguely related to the set by a numeric value usually between 0 and 1. This implies that the association is not guaranteed.

relations <- data.frame(sets = c(rep("A", 5), "B", "B"), 
                        elements = c("a", "b", "c", "d", "e", "a", "f"),
                        fuzzy = runif(7))
fuzzy_set <- tidySet(relations)
fuzzy_set

The equivalent operations performed on classical sets are possible with fuzzy sets:

union(fuzzy_set, sets = c("A", "B")) 
# Or we can give a name to the new set
union(fuzzy_set, sets = c("A", "B"), name = "D")
# Or the intersection
intersection(fuzzy_set, sets = c("A", "B"))
# Keeping the other sets:
intersection(fuzzy_set, sets = c("A", "B"), name = "D", keep = TRUE) 

Assuming that the fuzzy value is a probability, we can calculate which is the probability of having several elements:

# A set could be empty
set_size(fuzzy_set)
# The more probable size of the sets:
set_size(fuzzy_set) %>% 
  group_by(sets) %>% 
  filter(probability == max(probability))
# Probability of belonging to several sets:
element_size(fuzzy_set)

With fuzzy sets we can filter at certain levels (called alpha cut):

fuzzy_set %>% 
  filter(fuzzy > 0.5) %>% 
  activate("sets") %>% 
  mutate(sets_origin = c("Reactome", "KEGG"))

Related packages {#related}

There are several other packages related to sets, which partially overlap with BaseSet functionality:

Why this package? {#why}

On bioinformatics when looking for the impact of an experiment enrichment methods are applied. This involves obtaining several sets of genes from several resources and methods. Usually these curated sets of genes are taken at face value. However, there are several resources of sets and they do not agree between them, regardless they are used without considering any uncertainty on sets composition.

Fuzzy theory has long studied sets whose elements have degrees of membership and/or uncertainty. Therefore one way to improve the methods involve using fuzzy methods and logic on this field. As I couldn't find any package that provided methods for this I set on creating it (after trying to expand the existing one I knew).

This package is intended to be easy to use for someone who is working with collections of sets but flexible about the methods and logic it can use. To be consistent, the standard fuzzy logic is the default but it might not be the right one for your data. Consider changing the defaults to match with the framework the data was obtained with.

Code of Conduct {#CoC}

Please note that this package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.



llrs/BaseSet documentation built on Jan. 28, 2024, 9:05 a.m.