Skip to content

Instantly share code, notes, and snippets.

@lgatto
Created September 2, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgatto/c72b1ff5a4116118dbb34d9d2bc3470a to your computer and use it in GitHub Desktop.
Save lgatto/c72b1ff5a4116118dbb34d9d2bc3470a to your computer and use it in GitHub Desktop.
ggPlotMzDelta
ggPlotMzDelta <- function(delta, aaLabels = TRUE) {
stopifnot(require("ggplot2"))
## from PSM::getAminoAcids()
amino_acids <-
structure(list(AA = c("peg", "A", "R", "N", "D", "C", "E", "Q",
"G", "H", "I", "L", "K", "M", "F", "P", "S",
"T", "W", "Y", "V"),
ResidueMass = c(44, 71.03711, 156.10111, 114.04293,
115.02694, 103.00919, 129.04259,
128.05858, 57.02146, 137.05891,
113.08406, 113.08406, 128.09496,
131.04049, 147.06841, 97.05276,
87.03203, 101.04768, 186.07931,
163.06333, 99.06841),
Abbrev3 = c(NA, "Ala", "Arg", "Asn", "Asp", "Cys",
"Glu", "Gln", "Gly", "His", "Ile",
"Leu", "Lys", "Met", "Phe", "Pro",
"Ser", "Thr", "Trp", "Tyr", "Val"),
ImmoniumIonMass = c(NA, 44.05003, 129.114,
87.05584, 88.03986, 76.0221,
102.0555, 101.0715, 30.03438,
110.0718, 86.09698, 86.09698,
101.1079, 104.0534, 120.0813,
70.06568, 60.04494, 74.06059,
159.0922, 136.0762, 72.08133),
Name = c("Polyethylene glycol", "Alanine",
"Arginine", "Asparagine", "Aspartic acid",
"Cysteine", "Glutamic acid", "Glutamine",
"Glycine", "Histidine", "Isoleucine",
"Leucine", "Lysine", "Methionine",
"Phenylalanine", "Proline", "Serine",
"Threonine", "Tryptophan", "Tyrosine",
"Valine"),
Hydrophobicity = c(NA, 0.62, -2.53, -0.78, -0.9,
0.29, -0.74, -0.85, 0.48, -0.4,
1.38, 1.06, -1.5, 0.64, 1.19,
0.12, -0.18, -0.05, 0.81, 0.26,
1.08),
Hydrophilicity = c(NA, -0.5, 3, 0.2, 3, -1, 3, 0.2,
0, -0.5, -1.8, -1.8, 3, -1.3,
-2.5, 0, 0.3, -0.4, -3.4, -2.3,
-1.5),
SideChainMass = c(NA, 15, 101, 58, 59, 47, 73, 72,
1, 82, 57, 57, 73, 75, 91, 42,
31, 45, 130, 107, 43),
pK1 = c(NA, 2.35, 2.18, 2.18, 1.88, 1.71, 2.19,
2.17, 2.34, 1.78, 2.32, 2.36, 2.2, 2.28,
2.58, 1.99, 2.21, 2.15, 2.38, 2.2, 2.29),
pK2 = c(NA, 9.87, 9.09, 9.09, 9.6, 10.78, 9.67,
9.13, 9.6, 8.97, 9.76, 9.6, 8.9, 9.21,
9.24, 10.6, 9.15, 9.12, 9.39, 9.11, 9.74),
pI = c(NA, 6.11, 10.76, 10.76, 2.98, 5.02, 3.08,
5.65, 6.06, 7.64, 6.04, 6.04, 9.47, 5.74,
5.91, 6.3, 5.68, 5.6, 5.88, 5.63, 6.02)),
class = "data.frame",
row.names = c(NA, -21L))
ResidueMass <- ..density.. <- NULL ## to accomodate codetools
value <- AA <- NULL
delta <- data.frame(value = unlist(delta))
p <- ggplot(delta, aes(x = value)) +
geom_histogram(aes(y = ..density..), stat = "bin", binwidth = 1) +
xlab("M/Z delta") + ylab("Density") +
ggtitle("Histogram of Mass Delta Distribution")
## Adding annotations
if (aaLabels) {
y_offset <- x_offset <- rep(0.5, 21)
names(y_offset) <- names(x_offset) <- amino_acids$AA
x_offset[c("I", "L", "K", "Q")] <- 1
y_offset[c("V", "C")] <- 1
y_offset[c("P", "T")] <- 0
y_offset[c("N", "E")] <- 1
y_offset[c("K", "Q", "I", "L")] <- 0
y_offset[c("D", "M")] <- 0
aa <- cbind(amino_acids, x_offset, y_offset)
## removing Isoleucine, as it has the same residue mass
## as leucine, and updating leucine's label to I/L
aa$AA <- as.character(aa$AA)
aa[aa$AA == "I", "ResidueMass"] <- NA
aa[aa$AA == "L", "AA"] <- "I/L"
## Removing Q as it is too close to K to show
## up correctly and updating K label to K/Q
aa[aa$AA == "Q", "ResidueMass"] <- NA
aa[aa$AA == "K", "AA"] <- "K/Q"
p <- p +
geom_vline(data = aa,
aes(xintercept = ResidueMass,
colour = AA),
alpha = I(1/2)) +
geom_text(data = aa,
aes(x = ResidueMass,
y = -0.001, label = AA,
vjust = y_offset,
hjust = x_offset),
size = 2.5) +
theme(legend.position = "none")
}
p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment