Source: https://github.com/markziemann/bioinformatics_intro_workshop
library("tictoc")
library("parallel")
library("beeswarm")
Functions are a key concept in computing. By declaring functions, we can execute complex routines with a single command. Functions are the backbone of most computer programs and they can help us with our data analysis projects by providing us reusable modules which we can transfer between projects. For example, I have customised functions that I use for all my RNA-seq studies. Re-using these functions allows me to complete an analysis of RNA-seq datasets in only a few hours of hands-on time, and prevents me from having to type out these long routines again and again.
If you have a bunch of these functions you use regularly, then you can consider turning them into a package. A package doesn’t necessarily need to be in CRAN or Bioconductor, you can keep it private or just within your organisation.
A function is a stored command which can be rerun with different inputs. We define the function using the function() command where the function itself is defined in the {} curly brackets as shown below.
Unlike this example, functions are normally more than one line. Your function can specify more than one input but has to return just one output. return is used to select what data should be returned to the user. If a function provides a lot of output data, then that data typically gets converted to a list format before being returned to the user.
cubed <- function(x) {
x^3
}
cubed(42)
## [1] 74088
cubed(c(1,2,4))
## [1] 1 8 64
So you can consider functions to be “shortcuts”, to help you execute custom codes that you will be reusing again and again.
Functions are a bit like lego blocks in that they are versatile. For example you can use functions inside other functions.
cubedsum <- function(x,y) {
z <- cubed(x) + cubed(y)
return(z)
}
cubedsum(3,4)
## [1] 91
Using this approach, you can make incredibly sophisticated programs out of relatively simple building blocks.
The inputs don’t need to be single numbers, they can be vectors or dataframes. In this example we create a function to automate linear model analysis.
str(cars)
## 'data.frame': 50 obs. of 2 variables:
## $ speed: num 4 4 7 7 8 9 10 10 10 11 ...
## $ dist : num 2 10 4 22 16 10 18 26 34 17 ...
mylm <- lm(cars[,2] ~ cars[,1])
INT=signif(mylm$coefficients[1],3)
INT
## (Intercept)
## -17.6
SLOPE=signif(mylm$coefficients[2],3)
SLOPE
## cars[, 1]
## 3.93
HEADER=paste("Slope:",SLOPE,"Int:",INT)
HEADER
## [1] "Slope: 3.93 Int: -17.6"
myfunc <- function(x,y) {
mylm <- lm(y ~ x)
INT=signif(mylm$coefficients[1],3)
SLOPE=signif(mylm$coefficients[2],3)
HEADER=paste("Slope:",SLOPE,"Int:",INT)
plot(x,y)
abline(mylm,lty=2,lwd=2,col="red")
mtext(HEADER)
}
myfunc(cars[,1],cars[,2])
R functions can be reused. Let’s try with some built-in datasets.
myfunc(women$height, women$weight)
myfunc(ChickWeight$Time, ChickWeight$weight)
myfunc(Loblolly$age, Loblolly$height)
Let’s try to make our own functions with the next 30 mins. Make a function that accepts 2 numerical vectors and does the following:
Student t-test with the syntax t.test(vec2,vec1).
Make a boxplot of the two groups with a subheading that contains the t.test p-value using the mtext() command.
In order to generate two numeric vectors you can use the following:
vec1 <- rnorm(n=10, mean=20, sd=10)
vec2 <- rnorm(n=10, mean=30, sd=15)
Apply this function to calculate the difference in weight between control and treatment groups in the built-in PlantGrowth dataset.
The preferred solution to repeat a task on many inputs is to use vectorisation. Let’s say we want to find the median of three matrices. We create a new object, either a vector or list and then run a function on each element.
Note below the difference in the output type given by vapply, lapply and sapply.
mx1 <- matrix( rbinom(n=100, size=50, prob=0.8), nrow = 10 )
mx2 <- matrix( rbinom(n=100, size=100, prob=0.8), nrow = 10 )
mx3 <- matrix( rbinom(n=100, size=300, prob=0.8), nrow = 10 )
mxs <- list(mx1,mx2,mx3)
str(mxs)
## List of 3
## $ : int [1:10, 1:10] 42 43 36 38 42 36 38 43 43 44 ...
## $ : int [1:10, 1:10] 81 85 82 74 81 88 86 81 81 79 ...
## $ : int [1:10, 1:10] 236 232 239 233 249 230 248 243 241 249 ...
# sapply output is either a vector or matrix
sapply(X = mxs, FUN = mean)
## [1] 39.97 80.02 239.52
# lapply output is always a list
lapply(X = mxs, FUN = mean)
## [[1]]
## [1] 39.97
##
## [[2]]
## [1] 80.02
##
## [[3]]
## [1] 239.52
Take a few minutes now to visit the help page for these three commands.
?sapply
As you can see from the code above, vectorisation leads to shorter and more readable code. Naturally, you can use your custom functions with sapply and other apply family functions.
Here I’ve created a function to calculate the mean correlation in the columns of a matrix.
meancor <- function(x) {
mycor <- cor(x)
mean(mycor)
}
meancor(mx1)
## [1] 0.1561849
sapply(mxs,meancor)
## [1] 0.15618486 0.08751313 0.17517521
Your turn. Use sapply to find the mean and standard deviation of each matrix (mx1,mx2 and mx3).
Sometimes you have a large data table and you want to run the same analysis on each row or column. To do this, use apply. The general syntax is:
apply( x, MARGIN, FUN )
Where x is the object (df or mx) to work on.
MARGIN is either 1 for rows and 2 for columns.
FUN is the function to ron on the data.
For example, here is how you can calculate median of rows or columns in a matrix or dataframe.
# row medians
apply(mx1, 1, median)
## [1] 42.0 40.5 39.0 39.5 42.0 40.0 39.0 38.0 39.0 41.0
# column medians
apply(mx2, 2, median)
## [1] 81.0 78.0 80.5 78.5 83.0 77.0 78.5 79.0 82.0 81.5
In this next example I will create a new function that will calculate the coefficient of variation (CV), and then run this for each of the columns in the mtcars data.
cv <- function(x) {
mymean <- mean(x)
mysd <- sd(x)
cv <- mysd/mymean
signif(cv,3)
}
# run on one vector
cv(mtcars$mpg)
## [1] 0.3
# now run on all columns in the df
apply(X = mtcars, MARGIN = 2 ,FUN = cv)
## mpg cyl disp hp drat wt qsec vs am gear carb
## 0.300 0.289 0.537 0.467 0.149 0.304 0.100 1.150 1.230 0.200 0.574
Your turn. Create a function that reports the difference between the median and mean of a numerical vector, then use this function inside an apply command to report interquartile ranges of rows in the mx1 matrix.
Sometimes we need to repeat some computationally intensive work many times. This can be sped up using the many CPU threads on modern processors.
For example we have some proteomics data with 2000 proteins and want to run a t-test.
First let’s make a synthetic dataset. You don’t need to understand all the details here, just know that this chunk of code generates a synthetic omics dataset with a set number of replicates and known number of up and downregulated proteins with a known fold change.
nreps=5 # number of replicates per group
nanalytes=2000 # number of proteins detected
nup=100 # number of overexpressed proteins
ndn=300 # number of downexpressed proteins
foldchange=2 # fold change of proteins
setseed=42 # set seed so that results are repeatable
set.seed(setseed)
mx1 <- matrix(rnorm(n=nanalytes*10,mean=1000,sd=100),ncol=nreps*2)
colnames(mx1) <- c(paste("ctl",1:nreps,sep=""), paste("trt",1:nreps,sep=""))
rownames(mx1) <- paste("prot",sprintf('%0.5d', 1:nanalytes),sep="")
ups <- sample(x=1:nrow(mx1),size=nup,replace=FALSE)
dns <- sample(x=setdiff(1:nrow(mx1),ups),size=ndn,replace=FALSE)
trt <- mx1[,(nreps+1):(nreps*2)]
trt[ups,] <- trt[ups,] * foldchange
trt[dns,] <- trt[dns,] * 1 / foldchange
ctl <- mx1[,1:nreps]
mx2 <- cbind(ctl,trt)
str(mx2)
## num [1:2000, 1:10] 1137 944 1036 1063 1040 ...
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:2000] "prot00001" "prot00002" "prot00003" "prot00004" ...
## ..$ : chr [1:10] "ctl1" "ctl2" "ctl3" "ctl4" ...
head(mx2)
## ctl1 ctl2 ctl3 ctl4 ctl5 trt1 trt2
## prot00001 1137.0958 1025.0578 985.8191 1017.2832 994.2547 1116.3109 1005.3795
## prot00002 943.5302 972.2076 918.6102 872.7036 975.0965 980.9765 1075.3431
## prot00003 1036.3128 827.5264 967.4459 913.2105 847.5838 971.0544 1024.9850
## prot00004 1063.2863 799.3295 1037.8157 1062.6321 1046.3591 960.1154 955.5895
## prot00005 1040.4268 870.8192 800.5515 989.4369 881.2379 1070.9243 994.9730
## prot00006 989.3875 1036.5838 900.0643 974.3679 1049.4064 837.7353 953.2221
## trt3 trt4 trt5
## prot00001 770.3928 1049.5650 1102.5125
## prot00002 1000.4654 1051.8587 855.0845
## prot00003 838.3659 957.7797 1141.7475
## prot00004 1173.3129 1086.3113 896.4700
## prot00005 932.6323 922.2078 1008.5332
## prot00006 990.5582 1014.7913 1024.5068
str(which(mx2<0))
## int(0)
Run a t-test.
myttest <- function(mx,nctl,ntrt, paired=FALSE) {
tres <- lapply(1:nrow(mx), function(i) {
x <- mx[i,(nctl+1):(nctl+ntrt)]
y <- mx[i,1:nctl]
tres <- t.test(x=x,y=y,alternative = "two.sided",paired=paired)
log2FC <- log2(tres$estimate[[1]]/tres$estimate[[2]])
out=c("pval"=tres$p.value,"log2FC"=log2FC)
return(out)
})
df <- as.data.frame(do.call(rbind,tres))
rownames(df) <- rownames(mx)
df <- df[order(df$pval),]
df$fdr <- p.adjust(df$pval,method="fdr")
return(df)
}
tic()
mytres <- myttest(mx=mx2,nctl=5,ntrt=5)
head(mytres,20)
## pval log2FC fdr
## prot00814 7.398168e-10 -1.0797286 1.479634e-06
## prot01052 1.532119e-09 -0.9760601 1.532119e-06
## prot00572 3.334266e-09 -1.0234160 1.889139e-06
## prot01098 3.778278e-09 1.0153079 1.889139e-06
## prot01631 2.197183e-08 -1.0902399 7.006275e-06
## prot00896 2.501418e-08 -1.1018712 7.006275e-06
## prot01296 3.226786e-08 1.0779454 7.006275e-06
## prot01026 3.277582e-08 -1.0794018 7.006275e-06
## prot00014 3.403796e-08 -0.9539432 7.006275e-06
## prot01450 3.503138e-08 0.9782304 7.006275e-06
## prot00395 4.013973e-08 1.0313873 7.298132e-06
## prot01528 5.337892e-08 -0.9868042 8.896486e-06
## prot00549 6.312392e-08 -0.9908732 9.279924e-06
## prot01601 6.790194e-08 -1.0609102 9.279924e-06
## prot00604 7.553447e-08 -1.1104620 9.279924e-06
## prot01513 7.706188e-08 -1.1032865 9.279924e-06
## prot00494 8.121940e-08 -1.1274091 9.279924e-06
## prot01192 8.351932e-08 -0.9930421 9.279924e-06
## prot01253 8.932811e-08 -1.1095085 9.402959e-06
## prot01321 1.108809e-07 -0.9009982 1.108809e-05
toc()
## 0.524 sec elapsed
That was actually very fast, parallel processing won’t help us that much.
Now test the number of up and downregulated proteins.
nrow(subset(mytres,fdr<0.05 & log2FC>0))
## [1] 105
nrow(subset(mytres,fdr<0.05 & log2FC<0))
## [1] 306
Calculate precision and recall.
Precision = the accuracy of positive instances. AKA: specificity.
Recall = the proportion of positive instances called. AKA: sensitivity.
F1 = harmonic mean of Precision and Recall.
myups <- which(rownames(mx2) %in% rownames(subset(mytres,fdr<0.05 & log2FC>0)))
mydns <- which(rownames(mx2) %in% rownames(subset(mytres,fdr<0.05 & log2FC<0)))
myprec <- c(myups %in% ups, mydns %in% dns)
myprec <- length(which(myprec==TRUE))/length(myprec)
myrec <- c(ups %in% myups, dns %in% mydns)
myrec <- length(which(myrec==TRUE))/length(myrec)
In order to test with different set seeds, we can make 2 functions. The first one simulates the dataset with the included fold changes and the second function runs the t-test, and reports the results.
simdat <- function(nreps=3, nanalytes=2000, nup=100, ndn=300, mymean=1000,
mysd=200, foldchange=2, setseed=42){
set.seed(setseed)
mx1 <- matrix(rnorm(n=nanalytes*nreps*2, mean = mymean, sd = mysd), ncol=nreps*2)
colnames(mx1) <- c(paste("ctl",1:nreps,sep=""), paste("trt",1:nreps,sep=""))
rownames(mx1) <- paste("prot",sprintf('%0.5d', 1:nanalytes),sep="")
ups <- sample(x=1:nrow(mx1),size=nup,replace=FALSE)
dns <- sample(x=setdiff(1:nrow(mx1),ups),size=ndn,replace=FALSE)
trt <- mx1[,(nreps+1):(nreps*2)]
trt[ups,] <- trt[ups,] * foldchange
trt[dns,] <- trt[dns,] * 1 / foldchange
ctl <- mx1[,1:nreps]
mx2 <- cbind(ctl,trt)
out <- list(ups,dns,mx2)
return(out)
}
test_and_report <- function(dat,seed) {
ups <- dat[[1]]
dns <- dat[[2]]
mx <- dat[[3]]
nctl <- length(grep("ctl",colnames(mx)))
ntrt <- length(grep("trt",colnames(mx)))
mytres <- myttest(mx=mx,nctl=nctl,ntrt=ntrt)
myups <- which(rownames(mx2) %in% rownames(subset(mytres,fdr<0.05 & log2FC>0)))
mydns <- which(rownames(mx2) %in% rownames(subset(mytres,fdr<0.05 & log2FC<0)))
myprec <- c(myups %in% ups, mydns %in% dns)
myprec <- length(which(myprec==TRUE))/length(myprec)
myrec <- c(ups %in% myups, dns %in% mydns)
myrec <- length(which(myrec==TRUE))/length(myrec)
res = c("seed"=seed,"precision"=myprec,"recall"=myrec)
return(res)
}
tic()
res3 <- lapply(1:3,function(i) {
seed = i * 100
dat <- simdat(setseed=seed,nreps=3)
test_and_report(dat,seed=seed)
})
toc()
## 1.592 sec elapsed
res3
## [[1]]
## seed precision recall
## 100 NaN 0
##
## [[2]]
## seed precision recall
## 200 NaN 0
##
## [[3]]
## seed precision recall
## 3.0e+02 1.0e+00 7.5e-03
Test whether 4 reps gives better results?
res4 <- lapply(1:3,function(i) {
seed = i * 100
dat <- simdat(setseed=seed,nreps=4)
test_and_report(dat,seed=seed)
})
res4
## [[1]]
## seed precision recall
## 100.0000000 0.9715909 0.4275000
##
## [[2]]
## seed precision recall
## 200.000 0.975 0.390
##
## [[3]]
## seed precision recall
## 300.0000000 0.9865772 0.3675000
Can we save time with parallel processing? Run 10 jobs in series versus parallel.
Use detectCores() to find out how many CPUs are available
Use the parallel CRAN package.
We’ll use n=5 replicates for this test.
tic()
res4 <- lapply(1:10,function(i) {
seed = i * 100
dat <- simdat(setseed=seed,nreps=5)
test_and_report(dat,seed=seed)
})
toc()
## 5.219 sec elapsed
res4
## [[1]]
## seed precision recall
## 100.0000000 0.9799331 0.7325000
##
## [[2]]
## seed precision recall
## 200.00000 0.96875 0.77500
##
## [[3]]
## seed precision recall
## 300.0000000 0.9517685 0.7400000
##
## [[4]]
## seed precision recall
## 400.0000000 0.9813084 0.7875000
##
## [[5]]
## seed precision recall
## 500.0000000 0.9701987 0.7325000
##
## [[6]]
## seed precision recall
## 600.000000 0.959375 0.767500
##
## [[7]]
## seed precision recall
## 700.0000000 0.9801325 0.7400000
##
## [[8]]
## seed precision recall
## 800.0000000 0.9465409 0.7525000
##
## [[9]]
## seed precision recall
## 900.0000000 0.9555556 0.7525000
##
## [[10]]
## seed precision recall
## 1000.0000000 0.9728097 0.8050000
detectCores()
## [1] 16
tic()
res2 <- mclapply(1:10,function(i) {
seed=i*100
dat <- simdat(setseed=seed,nreps=5)
test_and_report(dat,seed=seed)
},mc.cores=10)
toc()
## 0.768 sec elapsed
res2df <- data.frame(do.call(rbind,res2))
res2df
## seed precision recall
## 1 100 0.9799331 0.7325
## 2 200 0.9687500 0.7750
## 3 300 0.9517685 0.7400
## 4 400 0.9813084 0.7875
## 5 500 0.9701987 0.7325
## 6 600 0.9593750 0.7675
## 7 700 0.9801325 0.7400
## 8 800 0.9465409 0.7525
## 9 900 0.9555556 0.7525
## 10 1000 0.9728097 0.8050
res2df$seed=NULL
Now make a chart of the results.
res2df$seed=NULL
boxplot(res2df,col="white",cex=0,ylab="Index")
beeswarm(res2df,add=TRUE,pch=19,cex=1.8,col="black")
Create a function that calculates the cubed root for a number
a <- c(8,27,64)
cubedroot <- function(x) { x^(1/3) }
cubedroot(a)
Use sapply to calculate the cubed root for the numbers 44,77,99,123
sapply(a,cubedroot)
Use apply to get the maximum value in each column of mtcars.
apply(mtcars,2,max)
Use lapply or sapply to draw two numbers from 0 to 9. How frequent it is to draw the same number twice? Run 10,000 replications.
res <- sapply(1:10000, function(i) {
x<- sample(x=0:9,2,replace=TRUE) ; x[1] == x[2]
})
table(res)
How frequent it would be to flip 10 coins simultaneously and get all heads? Use mclapply to simulate 10 million replications. How much time can be saved with parallel execution
tic()
res <- lapply(1:1e7, function(i) {
sum(sample(x=c(0,1),size=10,replace=TRUE))==10
} )
length(which(res==TRUE))
toc()
tic()
res <- mclapply(1:1e7, function(i) {
sum(sample(x=c(0,1),size=10,replace=TRUE))==10
} , mc.cores=16 )
length(which(res==TRUE))
toc()
## 39.3 seconds saved
out <- lapply(1:32,function(threads){
tic()
res <- mclapply(1:1e7, function(i) {
sum(sample(x=c(0,1),size=10,replace=TRUE))==10
} , mc.cores=threads )
out <- toc()
return(out$toc - out$tic)
})
out
For reproducibility.
sessionInfo()
## R version 4.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.5 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Australia/Melbourne
## tzcode source: system (glibc)
##
## attached base packages:
## [1] parallel stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] beeswarm_0.4.0 tictoc_1.2.1
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.39 R6_2.6.1 fastmap_1.2.0 xfun_0.61
## [5] glue_1.8.1 cachem_1.1.0 knitr_1.52 htmltools_0.5.9
## [9] rmarkdown_2.32 lifecycle_1.0.5 cli_3.6.6 vctrs_0.7.3
## [13] sass_0.4.10 jquerylib_0.1.4 compiler_4.6.1 tools_4.6.1
## [17] pillar_1.11.1 evaluate_1.0.5 bslib_0.12.0 yaml_2.3.12
## [21] otel_0.2.0 rlang_1.3.0 jsonlite_2.0.0