/
ladder_pL.R
144 lines (130 loc) · 5.55 KB
/
ladder_pL.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# function to plot ladder diagram for metal-ligand complexes
# pk_list: list of pK values, in order of fewest ligands to most
# ligands; default values are for cadmium-ammine complexes
# ligands: list giving number of ligands added for each pK value;
# defaults to vector of (1, 1, 1, 1) for the four stepwise
# cadmium-amine complexes
# pL_axis: logical; defaults to FALSE but TRUE draws pLigand axis
# pL_limit: limits for pLigand axis; defaults to 0 to 14
# type: the type of ladder diagram; options are "arrow," which is the
# default, or "strip"
# buffer: logical; defaults to FALSE, but TRUE will add buffer regions
# species: option to enter name of weak acid to add as title for plot;
# defaults to NULL, which supresses title
# labels: option to enter vector of labels for legend; defaults to
# NULL, which uses a default legend
# locate: x-axis location of arrow or center of strip; defaults to 2,
# which is practical lower limit; increase in steps of three
# will separate diagrams; practical upper limit is 12
# overlay: logical; defaults to FALSE, but setting to TRUE allows for
# adding a new ladder diagram
library(shape)
ladder_pL = function(pk_list = c(2.55, 2.01, 1.34, 0.84),
ligands = c(1, 1, 1, 1),
pL_axis = FALSE,
pL_limit = c(0, 14),
type = "arrow",
shade = "color",
buffer = FALSE,
species = NULL,
labels = NULL,
locate = 2,
overlay = FALSE){
# initial set-up; creates vector of limits for adding labels;
# creates counter, n, for the number of alpha values;
# sets colors for strip version of ladder diagram
pks = pk_list/ligands
n = length(pks)
limits = c(pL_limit[2], pks, pL_limit[1])
if (shade == "color") {
col.func = colorRampPalette(c("lightyellow2", "steelblue2"))
colors = col.func(n + 1)
} else {
col.func = colorRampPalette(c("gray70", "gray30"))
colors = col.func(n + 1)
}
# creates default set of alpha labels if labels are not provided
if (is.null(labels) == TRUE) {
labels = rep(0, n + 1)
labels[1] = expression(alpha[0])
num.ligands = 0
for (i in 1:(n)) {
num.ligands = num.ligands + ligands[i]
labels[i + 1] = eval(substitute(expression(alpha[I]),
list(I = num.ligands)))
}
}
# routines for plotting the ladder diagrams for each possible set
# of options: new or overlay; arrow or strip; with or without
# pH axis, and with or without buffer regions
if (overlay == FALSE) {if (pL_axis == FALSE) {
pLax = "n"
pLlabel = "pLigand"
pLaxis = ""
} else {
pLax = "s"
pLlabel = ""
pLaxis = "pLigand"
}
plot(NULL, xlim = c(0,14), ylim = c(pL_limit[1],pL_limit[2]),
type = "n", xaxt = "n", yaxt = pLax,
bty = "n", xlab = "", ylab = pLaxis,
xaxs = "i", yaxs = "i")
text(locate + 0.25, pL_limit[2] - (pL_limit[2] - pL_limit[1])/25,
pLlabel, pos = 4)
}
if (type == "arrow") {
Arrows(locate, pL_limit[1], locate, pL_limit[2], lwd = 2,
arr.type = "simple")
segments(x0 = rep(locate - 0.3, n), y0 = pks,
x1 = rep(locate + 0.3, n), y1 = pks, lwd = 2)
} else if (type == "strip") {
for (i in 1:(n + 1)) {
filledrectangle(mid = c(locate, (limits[i] + limits[i + 1])/2),
wx = 0.5, wy = limits[i + 1] - limits[i],
col = colors[i], lcol = "black")
}
} else {
return(paste(type, " is not an option.", sep = ""))
}
for (i in 1:n) {
text(x = locate + 0.25, y = pks[i],
labels = pks[i], pos = 4)
}
for (i in 1:(n + 1)){
text(x = locate - 0.25, y = (limits[i + 1] + limits[i])/2,
labels[i], pos = 2)
}
if (buffer == TRUE) {
if (n == 1) {
segments(x0 = locate, y0 = pks - 1/ligands, x1 = locate,
y1 = pks + 1/ligands, lwd = 5, lend = "butt")
} else { for (i in 1:n) {
if (i %% 2 == 0){
segments(x0 = locate + 0.05, y0 = pks[i] - 1/ligands[i],
x1 = locate + 0.05, y1 = pks[i] + 1/ligands[i],
lwd = 5, lend = "butt")
} else {
segments(x0 = locate - 0.05, y0 = pks[i] - 1/ligands[i],
x1 = locate - 0.05, y1 = pks[i] + 1/ligands[i],
lwd = 5, lend = "butt")
}
}
}
}
if (is.null(species) == FALSE) {
text(x = locate - 1, y = pL_limit[2], species, pos = 2,
srt = 90, col = "darkred")
}
}
# code to test
# ladder_pL(pL_axis = TRUE, type = "arrow", species = "cadmium-ammonia",
# locate = 2, pL_limit = c(0, 5))
# ladder_pL(type = "arrow", locate = 5, overlay = TRUE,
# pk_list = c(6.87, 2.03), ligands = c(3, 1),
# species = "zinc-ammonia", pL_limit = c(0, 5))
# ladder_pL(type = "strip", species = "cadmiium-ammonia", locate = 8,
# pL_limit = c(0, 5), shade = "color", overlay = TRUE)
# ladder_pL(type = "strip", locate = 11, overlay = TRUE,
# pL_limit = c(0, 5), pk_list = c(6.87, 2.03), ligands = c(3, 1),
# species = "zinc-ammonia", shade = "gray")