## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(glyrepr)

## -----------------------------------------------------------------------------
comps <- glycan_composition(
  c(Man = 5, GlcNAc = 2),
  c(Man = 3, Gal = 2, GlcNAc = 4),
  c(Man = 3, Gal = 2, GlcNAc = 4, Neu5Ac = 1, Fuc = 1)
)
comps

## -----------------------------------------------------------------------------
as_glycan_composition(list(
  c(Man = 5, GlcNAc = 2),
  c(Man = 3, Gal = 2, GlcNAc = 4),
  c(Man = 3, Gal = 2, GlcNAc = 4, Neu5Ac = 1, Fuc = 1)
))

## -----------------------------------------------------------------------------
comp_list <- list(
  c(Man = 5, GlcNAc = 2),
  c(Man = 3, Gal = 2, GlcNAc = 4),
  c(Man = 3, Gal = 2, GlcNAc = 4, Neu5Ac = 1, Fuc = 1)
)
as_glycan_composition(comp_list)

## -----------------------------------------------------------------------------
as_glycan_composition(c("H5N2", "Hex(3)HexNAc(2)"))

## -----------------------------------------------------------------------------
strucs <- c(o_glycan_core_1(), o_glycan_core_2())
as_glycan_composition(strucs)

## -----------------------------------------------------------------------------
# This raises an error because the monosaccharide names are mixed.
try(as_glycan_composition(c("Hex(5)HexNAc(2)", "Man(5)GlcNAc(2)")), silent = TRUE)

## -----------------------------------------------------------------------------
comps

## -----------------------------------------------------------------------------
count_mono(comps, "Man")

## -----------------------------------------------------------------------------
count_mono(comps, "Neu5Ac")

## -----------------------------------------------------------------------------
count_mono(comps, "Hex")

## -----------------------------------------------------------------------------
count_mono(comps)

## -----------------------------------------------------------------------------
c(comps, comps)

## -----------------------------------------------------------------------------
comps[1:2]

## -----------------------------------------------------------------------------
comps[integer()]

## -----------------------------------------------------------------------------
length(comps)

## -----------------------------------------------------------------------------
dup_comps <- c(comps, comps)
dup_comps

## -----------------------------------------------------------------------------
unique(dup_comps)

## -----------------------------------------------------------------------------
rep(comps, times = 2)

## -----------------------------------------------------------------------------
sort(comps)

## -----------------------------------------------------------------------------
sort(comps, decreasing = TRUE)

## -----------------------------------------------------------------------------
library(tibble)

tb <- tibble(
  id = c("glycan1", "glycan2", "glycan3"),
  composition = comps
)
tb

## -----------------------------------------------------------------------------
library(dplyr)

tb |>
  mutate(n_sia = count_mono(composition, "Neu5Ac")) |>
  filter(n_sia > 0)

## -----------------------------------------------------------------------------
comps_with_na <- glycan_composition(c(Man = 5, GlcNAc = 2), NA)
comps_with_na

## -----------------------------------------------------------------------------
count_mono(comps_with_na, "Man")

## -----------------------------------------------------------------------------
strucs <- as_glycan_structure(c(
  "Gal(b1-3)GalNAc(a1-",
  "Gal(b1-3)[GlcNAc(b1-6)]GalNAc(a1-"
))
strucs

## -----------------------------------------------------------------------------
get_structure_graphs(strucs)

## -----------------------------------------------------------------------------
count_mono(strucs, "Gal")

## -----------------------------------------------------------------------------
# This function works element-wise
has_linkages(strucs)

## -----------------------------------------------------------------------------
get_mono_type(strucs)

## -----------------------------------------------------------------------------
get_structure_level(strucs)

## -----------------------------------------------------------------------------
as_glycan_structure(c(
  "Gal(b1-3)GalNAc(a1-",
  "Gal(b1-3)[GlcNAc(b1-6)]GalNAc(a1-"
)) |> get_structure_level()

## -----------------------------------------------------------------------------
as_glycan_structure(c(
  "Gal(b1-?)GalNAc(a1-",
  "Gal(b1-?)[GlcNAc(b1-6)]GalNAc(a1-"
)) |> get_structure_level()

## -----------------------------------------------------------------------------
as_glycan_structure(c(
  "Gal(??-?)GalNAc(??-",
  "Gal(??-?)[GlcNAc(??-?)]GalNAc(??-"
)) |> get_structure_level()

## -----------------------------------------------------------------------------
as_glycan_structure(c(
  "Hex(??-?)HexNAc(??-",
  "Hex(??-?)[HexNAc(??-?)]HexNAc(??-"
)) |> get_structure_level()

## -----------------------------------------------------------------------------
as_glycan_structure(c(
  "Hex(a1-3)HexNAc(a1-",
  "Hex(a1-3)[HexNAc(b1-6)]HexNAc(a1-"
)) |> get_structure_level()

## -----------------------------------------------------------------------------
strucs

## -----------------------------------------------------------------------------
reduce_structure_level(strucs, to_level = "basic")

## -----------------------------------------------------------------------------
remove_linkages(strucs)  # same as reduce_structure_level(strucs, to_level = "topological")

## -----------------------------------------------------------------------------
strucs_with_subs <- as_glycan_structure(c(
  "Gal6S(b1-3)GalNAc(a1-",
  "Gal6S(b1-3)[GlcNAc(b1-6)]GalNAc(a1-"
))
remove_substituents(strucs_with_subs)

## -----------------------------------------------------------------------------
strings <- c(
  glycan1 = "Gal(b1-3)GalNAc(a1-",
  glycan2 = "Gal(b1-3)[GlcNAc(b1-6)]GalNAc(a1-"
)
as_glycan_structure(strings)

