This section serves like TIL (Today I Learned) for code I wrote and often forget.. If you see error contact me via ()[mailto:konrad@konsta.com.pl].

1 Linux

1.1 Compress to .tar.gz with pigz

tar --use-compress-program="pigz --best --recursive" -cf archive.tar.gz ./YourData

2 Generic R

2.1 Run cmd in conda env

# Run cmd in conda env
run_cmd = function(env, cmd, wait=F){
  system(paste0(". ~/.bashrc 2>/dev/null && conda activate ", env, " && ", cmd), wait = wait)
}

2.2 Load data with factor conversion

library(dplyr)
metadane = data.table::fread("metadata.csv") %>% mutate_if(is.character, as.factor)

3 Survival

3.1 KM curve and Cox PH model

library(survival)
library(lubridate)
library(ggsurvfit)
library(gtsummary)
library(tidycmprsk)
# library(condsurv)
library(ggpubr)
library(survminer)
library(dplyr)


temp = metadane
s1 <- survfit(Surv(OS_TIME, OS) ~ Cohort, data = temp)
s1 %>% tbl_survfit(times = c(12,24,60), label_header = "**{time}-month survival**" )
ggsurvplot(s1, pval = T, risk.table = T)
m = coxph(Surv(OS_TIME, OS) ~ Cohort, data = temp)
summary(m)
tbl_regression(m, exponentiate = T) 

3.2 Univariable analysis

library(survminer)
library(survival)
library(gtsummary)
library(caret)

meta2 = meta[,-nearZeroVar(meta)]

univ1 = tbl_uvregression(
  meta2,
  method=coxph,
  y = Surv(time = OS_TIME, event = OS),
  exponentiate = TRUE,
  include = c(-PFS, -PFS_TIME),
  pvalue_fun = function(x) style_pvalue(x, digits = 3)
)
saveRDS(univ1,"univ1.RDS")

4 Bioinformatics

4.1 Counts RNA-seq/miRNA-seq to TPM/TMM

  • Counts - matrix with genes in rownames and samples in colnames.
library(edgeR)

y <- DGEList(counts = counts, group = rep("Unknown", ncol(counts)))
y <- calcNormFactors(y)

# log10TPM
TPM = cpm(y,normalized.lib.sizes = F, log = F)
TPM = log10(TPM+0.001)
write.csv(TPM, "log10TPM.csv")

# TMM
TMM = cpm(y, normalized.lib.sizes = TRUE, log = T)
write.csv(data.frame("Gene"=rownames(TMM),TMM), "TMM.csv", quote = F, row.names = F)
write.table(data.frame("Gene"=rownames(TMM),TMM^2) , "TMM_for_cibersort.tsv", quote = F, sep = "\t", row.names = F)
TMM = as.matrix(TMM)

Copyright © 2022 Konrad Stawiski (konrad@konsta.com.pl; konrad@stawiski.net). Built as OmicApp by Konrad Stawiski.