|
a |
|
b/code_final/visutils.R |
|
|
1 |
loadC2L = function(d,sd2zero=0){ |
|
|
2 |
r = read.csv(paste0(d,'/q05_cell_abundance_w_sf.csv'),row.names = 1,check.names = FALSE) |
|
|
3 |
sd = read.csv(paste0(d,'/stds_cell_abundance_w_sf.csv'),row.names = 1,check.names = FALSE) |
|
|
4 |
colnames(r) = sub('q05cell_abundance_w_sf_','',colnames(r)) |
|
|
5 |
colnames(sd) = sub('stdscell_abundance_w_sf_','',colnames(sd)) |
|
|
6 |
sd = sd [rownames(r),colnames(r)] |
|
|
7 |
r[r<sd*sd2zero] = 0 |
|
|
8 |
mm = do.call(rbind,strsplit(rownames(r),'|',T)) |
|
|
9 |
r$barcode = mm[,2] |
|
|
10 |
r = split(r,mm[,1]) |
|
|
11 |
|
|
|
12 |
for(i in 1:length(r)){ |
|
|
13 |
rownames(r[[i]]) = r[[i]]$barcode |
|
|
14 |
r[[i]]$barcode = NULL |
|
|
15 |
r[[i]] = as.matrix(r[[i]]) |
|
|
16 |
} |
|
|
17 |
r |
|
|
18 |
} |
|
|
19 |
plotNMFConsSummary <- function( |
|
|
20 |
coefs, cons, |
|
|
21 |
clcols = NULL, |
|
|
22 |
max.cex = 4.5/14*8, |
|
|
23 |
colfun = function(x) { |
|
|
24 |
visutils::num2col( |
|
|
25 |
x, c('blue','gray','orange','violet','black'), minx = 0, maxx = 1 |
|
|
26 |
) |
|
|
27 |
}, ylab.cex = 1, xlab.cex = 1 |
|
|
28 |
){ |
|
|
29 |
cons = cons[colnames(coefs),colnames(coefs)] |
|
|
30 |
cls = apply(coefs,2,which.max) |
|
|
31 |
if(is.null(clcols)) |
|
|
32 |
clcols = RColorBrewer::brewer.pal(nrow(coefs),'Set3') |
|
|
33 |
o = names(cls)[order(cls)] |
|
|
34 |
parl = par(no.readonly=TRUE) |
|
|
35 |
par(mar=c(3,9,2.5,6),bty='n',oma=c(0,0,1,0),cex=1,tcl=-0.2,mgp=c(1.2,0.3,0),las=1) |
|
|
36 |
dotPlot( |
|
|
37 |
t(coefs[, o]), |
|
|
38 |
rowColours = cbind(clcols[cls[o]]), |
|
|
39 |
colColours = clcols, |
|
|
40 |
max.cex = max.cex, ylab.cex = ylab.cex, |
|
|
41 |
xlab.cex = xlab.cex, |
|
|
42 |
scaleWM = F, |
|
|
43 |
colfun = colfun, |
|
|
44 |
plot.legend = TRUE |
|
|
45 |
) |
|
|
46 |
par(parl) |
|
|
47 |
} |
|
|
48 |
plotFeatureProfiles_fun <- function ( |
|
|
49 |
m, features, features_facet = NULL, cols = NULL, sd.mult = 2, legend. = TRUE, |
|
|
50 |
ylim = NULL, scaleY = TRUE, area.opacity = 0.2, lwd = 2, |
|
|
51 |
xlab = "Distance (spots)", ylab = "Relative abundance", main = "", |
|
|
52 |
xlim = NULL, ... |
|
|
53 |
) { |
|
|
54 |
if (is.null(features_facet)) |
|
|
55 |
features_facet = sapply(features, function(x) dimnames(m)[[3]], simplify = FALSE) |
|
|
56 |
if (is.null(cols)) |
|
|
57 |
cols = char2col(features) |
|
|
58 |
if (is.null(names(cols))) |
|
|
59 |
names(cols) = features |
|
|
60 |
x = as.numeric(dimnames(m)[[1]]) |
|
|
61 |
areas = lapply(features, function(ct) { |
|
|
62 |
do.call(rbind, apply( |
|
|
63 |
m[, gsub("Healthy ", "", ct), unlist(features_facet[ct])], 1, |
|
|
64 |
function(x) { |
|
|
65 |
x = x[!is.na(x)] |
|
|
66 |
data.frame(mean = mean(x), sd = sd(x)/sqrt(length(x)), |
|
|
67 |
n = length(x)) |
|
|
68 |
})) |
|
|
69 |
}) |
|
|
70 |
names(areas) = features |
|
|
71 |
if (scaleY) |
|
|
72 |
areas = lapply(areas, function(a) { |
|
|
73 |
a[, 1:2] = a[, 1:2]/max(a$mean, na.rm = TRUE) |
|
|
74 |
a |
|
|
75 |
}) |
|
|
76 |
if (is.null(ylim)) |
|
|
77 |
ylim = range(sapply(areas, function(a) c(min(a$mean - |
|
|
78 |
a$sd * sd.mult, na.rm = TRUE), max(a$mean + a$sd * |
|
|
79 |
sd.mult, na.rm = TRUE)))) |
|
|
80 |
if (is.null(xlim)) |
|
|
81 |
xlim = range(x) |
|
|
82 |
plot(1, t = "n", xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab, |
|
|
83 |
main = main, ...) |
|
|
84 |
for (n in names(areas)) plotArea(x, areas[[n]][, 1:2], sd.mult = sd.mult, |
|
|
85 |
col = cols[n], new = FALSE, lwd = lwd, area.transp = area.opacity) |
|
|
86 |
if (!is.null(legend.) && (!is.logical(legend.) || legend.)) { |
|
|
87 |
if (!is.list(legend.)) |
|
|
88 |
legend. = list() |
|
|
89 |
if (is.null(legend.$x)) { |
|
|
90 |
legend.$x = grconvertX(1, "npc", "user") |
|
|
91 |
legend.$y = grconvertY(1, "npc", "user") |
|
|
92 |
} |
|
|
93 |
legend.$fill = cols |
|
|
94 |
legend.$legend = names(cols) |
|
|
95 |
legend.$bty = par("bty") |
|
|
96 |
legend.$border = NA |
|
|
97 |
legend.$xpd = NA |
|
|
98 |
do.call(legend, legend.) |
|
|
99 |
} |
|
|
100 |
} |