-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathppi_stats.R
626 lines (496 loc) · 18.2 KB
/
ppi_stats.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
library(ggplot2)
library(xtable) # for converting a data frame into a latex table
library(gridExtra) # for `grid.arrange`
library(igraph) # for power.law.fit
# load PPI name mapping
source("ppi_utils.R")
get_node_properties <- function(ppi_name = "string")
{
# load the ts/hk summary data from the database
source("sql_config.R")
con <- get_sql_conn()
ppi_node_prop_tbl <- paste(ppi_name, "node_properties", sep="_")
query <- paste("SELECT * FROM ", ppi_node_prop_tbl)
node_props <- dbGetQuery(con, query)
return(node_props)
}
get_graph_properties <- function(ppi_name="string")
{
# load the ts/hk summary data from the database
source("sql_config.R")
con <- get_sql_conn()
ppi_prop_tbl <- paste(ppi_name, "properties", sep="_")
query <- paste("SELECT * FROM ", ppi_prop_tbl)
props <- dbGetQuery(con, query)
return (props)
}
# TODO: print out graph information:
# - n
# - m
# - connected components
# - maximum degree
# - average degree
# - global clustering coeff
# - avg local clustering coeff
# - (?) degree assotativity
get_ppi_graph_stats <- function(ppi_name="string")
{
# will get and/or calculate all graph properties and save them into
# named list elements
props = list()
# get the graph information from the database
graph_props <- get_graph_properties(ppi_name)
node_props <- get_node_properties(ppi_name)
# extract easy info
for (i in 1:dim(graph_props)[1])
{
props[[graph_props[i,1]]] = graph_props[i,2]
}
# TODO: summarize node wise data
degrees <- node_props$degree
props[["avg_degree"]] <- mean(degrees)
props[["max_degree"]] <- max(degrees)
local_cc <- node_props$ClusteringCoeff
props[["avg_local_cc"]] <- mean(local_cc)
# return result
return(props)
}
get_ppi_global_stats_table <- function()
{
stats_tbl <- data.frame()
for (p in get_ppis())
{
stats <- get_ppi_graph_stats(p)
stats$ppi <- p
stats_tbl <- rbind(stats_tbl, t(stats))
}
return(stats_tbl)
}
get_latex_summary_stats <- function()
{
# get data and map alias to actual PPI name
data <- get_ppi_global_stats_table()
data$ppi_name <- sapply(data$ppi, to_short_ppi_name)
# select columns to output
out_data <- data[,c("ppi_name", "n", "m", "avg_deg", "max_deg", "conn_comp")]
# actually output the latex table to be copy-pasted into the report
xtable(out_data)
}
dpowerlaw <- function(x, alpha, xmin=1)
{
pdf <- ((alpha - 1)/xmin) * (x / xmin)**(-alpha)
return (pdf)
}
ppowerlaw <- function(x, alpha, xmin=1)
{
# integral over pdf starting from xmin
# http://www.wolframalpha.com/input/?i=integral+from+b+to+t+of+%28%28a-1%29%2Fb%29%28x%2Fb%29%5E%28-a%29
cdf <- 1 - (x / xmin)**(-alpha+1)
return(cdf)
}
#######################################################################
# Fit degree distributions (parameter estimation) #
#######################################################################
fit_binom <- function(degrees)
{
n <- length(degrees)
m <- sum(degrees) / 2
p <- 2 * m / (n*(n-1))
params <- list(size=n-1, prob=p)
return(params)
}
fit_poisson <- function(degrees)
{
avg_deg <- mean(degrees)
# the lambda of the poisson distribution is the average degree
lambda <- avg_deg
params <- list(lambda=lambda)
return(params)
}
fit_exp <- function(degrees)
{
rate <- 1 / mean(degrees)
params <- list(rate=rate)
return(params)
}
fit_powerlaw_tail <- function(degrees)
{
# use PLFit fitter, to only fit the tail (self optimizing)
fit <- power.law.fit(degrees, implementation="plfit")
alpha <- fit$alpha
xmin <- fit$xmin
params <- list(alpha=alpha, xmin=xmin)
return(params)
}
fit_powerlaw <- function(degrees)
{
# use max likelihood estimator (fits whole degree distribution)
fit <- power.law.fit(degrees, implementation="R.mle")
alpha <- coef(fit)
xmin <- min(degrees)
params <- list(alpha=alpha, xmin=xmin)
return(params)
}
#######################################################################
# Fit degree distribution to 1:max(degrees) #
#######################################################################
# fits the given degree distribution with the given fitter
# and applies the pdf with the fitted parameters to the range 1:max(degrees)
# and returns these values
fit_degree_distr <- function(degrees, fitter, pdf)
{
# get pdf parameters
params <- fitter(degrees)
# get maximum degree and with it the input range
max_degr <- max(degrees)
min_degr <- max(1, params$xmin)
x <- min_degr:max_degr
# get the relative frequency (i.e. the pdf)
freq <- do.call(pdf, c(list(x), params))
# maybe needs adjusting
if (min_degr > 1)
{
# normalization
freq_normalization <- length(degrees[which(degrees >= min_degr)]) / length(degrees)
freq <- freq * freq_normalization
}
# get the model
model <- data.frame(degree=x, Freq=freq)
return(model)
}
#######################################################################
# wrapper functions for convenience #
#######################################################################
fit_binom_degree_distr <- function(degrees)
{
return(fit_degree_distr(degrees, fit_binom, dbinom))
}
fit_poisson_degree_distr <- function(degrees)
{
return(fit_degree_distr(degrees, fit_poisson, dpois))
}
fit_powerlaw_degree_distr <- function(degrees)
{
return(fit_degree_distr(degrees, fit_powerlaw, dpowerlaw))
}
fit_powerlaw_tail_degree_distr <- function(degrees)
{
return(fit_degree_distr(degrees, fit_powerlaw_tail, dpowerlaw))
}
fit_exp_degree_distr <- function(degrees)
{
return(fit_degree_distr(degrees, fit_exp, dexp))
}
#######################################################################
# Plotting: generate legend #
#######################################################################
#extract legend
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
degr_distr_legend <- function()
{
# TODO: create some plot using all the previous data
x <- 1:10
df <- data.frame(x=x, y=x, z=as.character(x), y1=x, y2=x, y3=x, y4=x)
dists <- c("Actual Distribution", "Binomial Distribution",
"Exponential Distribution", "Power-law Distribution",
"Power-law Distribution (tail only)")
x11()
fig <- ggplot(data=df, aes(x=x, y=y, lty="a", color="a", shape="a")) + geom_point()+
geom_line(data=df, aes(x=x,y=-y1/2,lty="b", color="b", shape="b")) +
geom_line(data=df, aes(x=x,y=-y2,lty="c", color="c", shape="c")) +
geom_line(data=df, aes(x=x,y=y3,lty="d", color="d", shape="d")) +
geom_line(data=df, aes(x=x,y=y4,lty="e", color="e", shape="e")) +
scale_colour_manual(name = "Distributions",
labels = dists,
values = c("grey60", "black", "blue", "red", "orange")) +
scale_linetype_manual(name = "Distributions",
labels = dists,
values = c("blank", "dotdash", "dotted", "dashed", "solid")) +
scale_shape_manual(name = "Distributions",
labels = dists,
values = c(16, 26, 26, 26, 26))
leg <- g_legend(fig)
dev.off()
return(leg)
}
#######################################################################
# Plotting degree distributions #
#######################################################################
# plot degree distribution of the networks
plot_degree_distr <- function(ppi_name="string")
{
# get the data
data <- get_node_properties(ppi_name)
# get the degrees
degree <- as.integer(data$degree)
degree_distr <- as.data.frame(prop.table(table(degree)))
degree_distr$degree <- as.integer(levels(degree_distr$degree))
# random graph (Erdos Renyi, or Gilbert)
# get binomial random model
binom_model <- fit_binom_degree_distr(degree)
# cut of zero freq
binom_model <- binom_model[which(binom_model$Freq > 1e-20),]
# poisson model
pois_model <- fit_poisson_degree_distr(degree)
# cut of zero freq
pois_model <- pois_model[which(pois_model$Freq > 1e-20),]
# power law fit
powerlaw_model <- fit_powerlaw_degree_distr(degree)
powerlaw_tail_model <- fit_powerlaw_tail_degree_distr(degree)
# exponential distribution
exp_model <- fit_exp_degree_distr(degree)
fig <- ggplot(degree_distr, aes(x=degree, y=Freq)) + geom_point(colour="grey60")
#fig <- fig + scale_x_log10() + scale_y_log10()
fig <- fig + geom_line(data=binom_model, aes(x=degree, y=Freq), lty="dotdash")
#fig <- fig + geom_line(data=pois_model, aes(x=degree, y=Freq), lty="dotted")
fig <- fig + geom_line(data=powerlaw_tail_model, aes(x=degree, y=Freq), colour="orange", lty="solid", size=1)
fig <- fig + geom_line(data=powerlaw_model, aes(x=degree, y=Freq), lty="dashed", colour="red")
fig <- fig + geom_line(data=exp_model, aes(x=degree, y=Freq), colour="blue", lty="dotted")
fig <- fig + xlab("Degree") + ylab("Rel. Frequency")
#fig <- fig + coord_cartesian(xlim=c(0,1000), ylim =c(min(degree_distr$Freq)/5, max(degree_distr$Freq) * 5))
fig$degrees <- degree
fig$degree_distr <- degree_distr
return(fig)
}
# plot degree distribution of the networks
plot_degree_distr_no_fit <- function(ppi_name="string")
{
# get the data
data <- get_node_properties(ppi_name)
# get the degrees
degree <- as.integer(data$degree)
degree_distr <- as.data.frame(prop.table(table(degree)))
degree_distr$degree <- as.integer(levels(degree_distr$degree))
fig <- ggplot(degree_distr, aes(x=degree, y=Freq)) + geom_point(colour="grey60")
#fig <- fig + scale_x_log10() + scale_y_log10()
fig <- fig + xlab("Degree") + ylab("Rel. Frequency")
#fig <- fig + coord_cartesian(xlim=c(0,1000), ylim =c(min(degree_distr$Freq)/5, max(degree_distr$Freq) * 5))
fig$degrees <- degree
fig$degree_distr <- degree_distr
return(fig)
}
plot_degree_distr_linear <- function(ppi_name="string")
{
# get basic plot
fig <- plot_degree_distr(ppi_name)
degrees <- fig$degrees
degree_distr <- fig$degree_distr
# plots on linear scale (i.e. no log scale)
adj_max <- quantile(degrees, 0.999) # throw away the top 1000th elements
fig <- fig + coord_cartesian(xlim=c(0,adj_max), ylim =c(0, max(degree_distr$Freq)*1.05))
return (fig)
}
plot_degree_distr_linear_no_fit <- function(ppi_name="string")
{
# get basic plot
fig <- plot_degree_distr_no_fit(ppi_name)
degrees <- fig$degrees
degree_distr <- fig$degree_distr
# plots on linear scale (i.e. no log scale)
adj_max <- quantile(degrees, 0.99) # throw away the top 1000th elements
fig <- fig + coord_cartesian(xlim=c(0,adj_max), ylim =c(0, max(degree_distr$Freq)*1.05))
return (fig)
}
plot_degree_distr_log <- function(ppi_name="string")
{
# get basic plot
fig <- plot_degree_distr(ppi_name)
degrees <- fig$degrees
degree_distr <- fig$degree_distr
# plots on linear scale (i.e. no log scale)
adj_max <- max(degrees) # throw away the top 1000th elements
y_range <- c(min(degree_distr$Freq)/2, max(degree_distr$Freq) * 2)
fig <- fig + scale_x_log10() + scale_y_log10()
fig <- fig + coord_cartesian(xlim=c(1-0.,adj_max),ylim=y_range)
return (fig)
}
plot_degree_distr_log_no_fit <- function(ppi_name="string")
{
# get basic plot
fig <- plot_degree_distr_no_fit(ppi_name)
degrees <- fig$degrees
degree_distr <- fig$degree_distr
# plots on linear scale (i.e. no log scale)
adj_max <- max(degrees) # throw away the top 1000th elements
y_range <- c(min(degree_distr$Freq)/2, max(degree_distr$Freq) * 2)
fig <- fig + scale_x_log10() + scale_y_log10()
fig <- fig + coord_cartesian(xlim=c(1-0.,adj_max),ylim=y_range)
return (fig)
}
#######################################################################
# string db example #
#######################################################################
plot_example_2 <- function(ppi_name="string")
{
p <- ppi_name
figs <- list()
# plot linear plot (maybe without any distribution lines?)
fig <- plot_degree_distr_linear(p)
fig <- fig + labs(title="Degree Distribution of STRING")
figs <- c(figs, list(fig))
# TODO TODO
# add legend??
figs <- c(figs, list(degr_distr_legend()))
all_figs <- do.call(grid.arrange, figs)
return(all_figs)
}
plot_example_1 <- function(ppi_name="string")
{
# simple linear plot of degree distribution
p <- ppi_name
figs <- list()
fig <- plot_degree_distr_linear_no_fit(p)
fig <- fig + labs(title="Degree Distribution of STRING")
figs <- c(figs, list(fig))
fig <- plot_degree_distr_log_no_fit(p)
fig <- fig + labs(title="... with log-log scaling")
figs <- c(figs, list(fig))
all_figs <- do.call(grid.arrange, c(figs,list(nrow=1)))
return(all_figs)
}
save_example_1 <- function()
{
pdf("../figs/ppi_degr_distr.pdf", width=6.3, height=2.6)
fig <- plot_example_1("string")
print(fig)
dev.off()
}
#######################################################################
# Helper functions #
#######################################################################
explode_degree_distr <- function(degree_distr)
{
max_degr <- max(degree_distr$degree)
expl_degree_distr <- data.frame(degree=1:max_degr, Freq=rep(0, max_degr))
# fill the new distr with the old values where they fit
for (i in 1:length(degree_distr$degree))
{
deg <- degree_distr$degree[i]
frq <- degree_distr$Freq[i]
expl_degree_distr$Freq[deg] = frq
}
return(expl_degree_distr)
}
expected_freq <- function(brks, cdf, cdf_params)
{
est_freqs <- c()
# for all intervals of the breaks
for (i in 2:length(brks))
{
from <- brks[i-1]
to <- brks[i]
from_val <- do.call(cdf, c(from, cdf_params))
to_val <- do.call(cdf, c(to, cdf_params))
est_freqs <- c(est_freqs, to_val - from_val)
}
return(est_freqs)
}
# returns the breaks for quantile binning
quantile_brks <- function(x, q=0.1)
{
brks <- unique(quantile(x, probs=seq(0,1, by=q)))
return(brks)
}
# creates quantiled bins and returns a data frame of frequencies
quantile_binning <- function(x, q=0.1)
{
brks <- quantile_brks(x, q)
c <- cut(x, breaks=brks, include.lowest=TRUE)
t <- as.data.frame(table(c))
return (t)
}
test_fits <- function(ppi_name="string")
{
# get the data
data <- get_node_properties(ppi_name)
# get the degrees
degree <- as.integer(data$degree)
# degree_distr <- as.data.frame(table(degree))
# degree_distr$degree <- as.integer(levels(degree_distr$degree))
#
# degree_distr <- explode_degree_distr(degree_distr)
# TODO: output table of fits per PPI (all are bad fits, but most are much
# much worse than powerlaw)
# TODO: only fit for tail of degree distribution (i.e. ignoring low degree
# nodes in the evaluation of fit) [HOW? do i calc the freq for only the tail?]
brks <- quantile_brks(degree)
t <- quantile_binning(degree)
result <- list()
result[["ppi"]] <- ppi_name
# get binomial random model
bin_expected_freq <- expected_freq(brks, pbinom, fit_binom(degree))
fit <- chisq.test(t$Freq, p=bin_expected_freq, rescale.p=TRUE)
result$binomial_chi <- fit$statistic
result$binomial_p <- fit$p.value
# poisson model
bin_expected_freq <- expected_freq(brks, ppois, fit_poisson(degree))
fit <- chisq.test(t$Freq, p=bin_expected_freq, rescale.p=TRUE)
result$poisson_chi <- fit$statistic
result$poisson_p <- fit$p.value
# exponential distribution
bin_expected_freq <- expected_freq(brks, pexp, fit_exp(degree))
fit <- chisq.test(t$Freq, p=bin_expected_freq, rescale.p=TRUE)
result$exp_chi <- fit$statistic
result$exp_p <- fit$p.value
# power law fit
bin_expected_freq <- expected_freq(brks, ppowerlaw, fit_powerlaw(degree))
fit <- chisq.test(t$Freq, p=bin_expected_freq, rescale.p=TRUE)
result$powerlaw_chi <- fit$statistic
result$powerlaw_p <- fit$p.value
# power law fit (PLfit: tail only)
params <- fit_powerlaw_tail(degree)
# resample breaks by filtered degrees
deg_over_xmin <- degree[which(degree >= params$xmin)]
brks <- quantile_brks(deg_over_xmin)
t <- quantile_binning(deg_over_xmin)
bin_expected_freq <- expected_freq(brks, ppowerlaw, params)
fit <- chisq.test(t$Freq, p=bin_expected_freq, rescale.p=TRUE)
result$powerlaw_tail_chi <- fit$statistic
result$powerlaw_tail_p <- fit$p.value
return(result)
}
fit_test_table <- function()
{
df <- data.frame()
for (p in get_ppis())
{
chi_fits <- test_fits(p)
#chi_fits$ppi <- to_short_ppi_name(p)
df <- rbind(df, as.data.frame(chi_fits))
#df$ppi[length(df$ppi)] <- to_short_ppi_name(p)
}
# now transpose the data frame
dft <- as.data.frame(t(df[,2:ncol(df)]))
colnames(dft) <- df[,1]
df <- dft
return(df)
}
#######################################################################
# all ppis distr #
#######################################################################
plot_all_ppis_degr_distr <- function()
{
figs <- list()
for (p in get_ppis())
{
fig <- plot_degree_distr_log(p)
fig <- fig + labs(title=to_ppi_name(p))
figs <- c(figs, list(fig))
}
figs <- c(figs, list(degr_distr_legend()))
all_figs <- do.call(grid.arrange, figs)
return(all_figs)
}
save_all_ppi_degr_distr <- function()
{
pdf("../figs/all_ppi_degr_distr.pdf", width=6, height=7)
all_figs <- plot_all_ppis_degr_distr()
print(all_figs)
dev.off()
}
# TODO: plot clustering coeff against p (N-1) [ see barabasi book ]