# knitr::knit_hooks$set(optipng = knitr::hook_optipng)
# knitr::opts_chunk$set(optipng = '-o7')

knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(fig.align = "center")
knitr::opts_chunk$set(fig.width = 12)
knitr::opts_chunk$set(fig.height = 6)

library(immunarch)
# source("../R/testing.R")
# immdata = load_test_data()
data(immdata)

Immunarch data format

immunarch comes with its own data format, including tab-delimited columns that can be specified as follows:

Input / output

The package provides several IO functions:

repLoad has the .format argument that sets the format for input repertoire files. Do not provide it if you want immunarch to detect the formats and parse files automatically! In case you want to force the package to parse the data in a specific format, you can choose one of the several options for the argument:

These parsers will be available soon.

Please contact us if there are more file formats you want to be supported.

For parsing IgBLAST results process the data with MigMap first.

You can load the data either from a single file, a list of repertoire file paths or from a folder with repertoire files. A single file can be loaded as follows:

# To load the data from a single file without forcing the data format:
immdata <- repLoad("path/to/your/folder/immunoseq_1.txt")

# To load the data from a single ImmunoSEQ file go with:
immdata <- repLoad("path/to/your/folder/immunoseq_1.txt", .format = "immunoseq")

In other cases you may want to provide a metadata file and locate it in the folder. It is necessary to name it exactly "metadata.txt".

# For instance you have a following structure in your folder:
# >_ ls
# immunoseq1.txt
# immunoseq2.txt
# immunoseq3.txt
# metadata.txt

With the metadata repLoad will create a list in the environment with 2 elements, namely data and meta. All the data will be accessible simply from immdata$data.

Otherwise repLoad will create a dummy metadata file with only sample names.

# To load the whole folder with every file in it type:

immdata <- repLoad("path/to/your/folder/")

# In order to do that your folder must contain metadata file named
# exactly "metadata.txt".

# In R, when you load your data:
# > immdata <- repLoad("path/to/your/folder/")
# > names(immdata)
# [1] "data" "meta"

# Suppose you do not have "metadata.txt":
# > immdata <- repLoad("path/to/your/folder/")
# > names(immdata)
# [1] "data" "meta"

Dummy metadata data frame look like this:

as_tibble(data.frame(Sample = c("immunoseq1", "immunoseq2", "immunoseq3"), stringsAsFactors = F))

The metadata file "metadata.txt" has to be tab delimited file with first column named "Sample" and any number of additional columns with arbitrary names. The first column should contain base names of files without extensions in your folder.

| Sample |Sex|Age|Status| |:----------:|:-----:|:-----:|:--------:| |immunoseq_1|M |1 |C | |immunoseq_2|M |2 |C | |immunoseq_3|F |3 |A |

In order to import data from the external databases you have to create a connection to this database and then load the data. Make sure that the table format in your database matches the immunarch's format.

To illustrate the use of external database, here is an example demonstrating data loading to the local MonetDB database:

# Your list of repertoires in immunarch's format
DATA
# Metadata data frame
META

# Create a temporary directory
dbdir = tempdir()

# Create a DBI connection to MonetDB in the temporary directory.
con = DBI::dbConnect(MonetDBLite::MonetDBLite(), embedded = dbdir)

# Write each repertoire to MonetDB. Each table has corresponding name from the DATA
for (i in 1:length(DATA)) {
  DBI::dbWriteTable(con, names(DATA)[i], DATA[[i]], overwrite=TRUE)
}

# Create a source in the temporary directory with MonetDB
ms = MonetDBLite::src_monetdblite(dbdir = dbdir)
res_db = list()

# Load the data from MonetDB to dplyr tables
for (i in 1:length(DATA)) {
  res_db[[names(DATA)[i]]] = dplyr::tbl(ms, names(DATA)[i])
}

# Your data is ready to use
list(data = res_db, meta = META)

You might want to make a list with data, which is a list of tables of repertoires, and the meta, containing metadata:

# Load the data to the immdata variables. Metadata file "metadata.txt" will be found automatically.
immdata = repLoad("your_folder", "optionally_your_format")
# Repertoires
immdata$data
# Metadata
immdata$meta

immunarch is compatible with following sources:

Basic data manipulations with dplyr and immunarch

You can find the introduction to dplyr here: https://CRAN.R-project.org/package=dplyr/vignettes/dplyr.html

Get the most abundant clonotypes

The function returns the most abundant clonotypes for the given repertoire:

top(immdata$data[[1]])

Filter functional / non-functional / in-frame / out-of-frame clonotypes

Conveniently, functions are vectorised over the list of data frames; and coding(immdata$data) in the example below returns a list of data frames with coding sequences:

coding(immdata$data[[1]])

The next one operates in a similar fashion:

noncoding(immdata$data[[1]])

Now, the computation of the number of filtered sequences is straightforward:

nrow(inframes(immdata$data[[1]]))

And for the out-of-frame clonotypes:

nrow(outofframes(immdata$data[[1]]))

Get subset of clonotypes with a specific V gene

It is simple to subset data frame according to labels in the specified index. In the example the resulting data frame contains only records with 'TRBV10-1' V gene:

filter(immdata$data[[1]], V.name == 'TRBV10-1')

Downsampling

ds = repSample(immdata$data, "downsample", 100)
sapply(ds, nrow)
ds = repSample(immdata$data, "sample", .n = 10)
sapply(ds, nrow)


charlotterich/immunarch_code documentation built on Jan. 24, 2020, 12:08 a.m.