Zettelkasten

source(file.path("..", "_preloader.r"), verbose=TRUE)


################################################################
# Load previous data (E.g. EdgeR or Limma results)             #
################################################################
tt_result_1 <- readr::read_csv(
		file.path(results.dir, "results_1.csv")
	) %>%
	dplyr::filter(
		FDR <= 0.05, # FDR cutoff
    	abs(logFC) > log2(2), # some logFC cutoff
		!is.na(hgnc_symbol)
	)

tt_result_2 <- readr::read_csv(
		file.path(results.dir, "results_2.csv")
	) %>%
	dplyr::filter(
		FDR <= 0.05,
		!is.na(hgnc_symbol)
	)

ics.sensitive.genes <- readr::read_csv(
		file.path(results.dir, "ics_sensitive_genes.csv")
	) %>%
	dplyr::filter(
		Meta.FDR.bonferroni <= 0.05,
		!is.na(hgnc_symbol)
	)

################################################################
# Venn Diagram - All                                           #
################################################################
genes_all <- list(
	`Healthy vs Severe`=tt_result_1 %>%
		dplyr::pull(hgnc_symbol),
	`Healthy vs Mild`=tt_result_2 %>%
		dplyr::pull(hgnc_symbol),
	`ICS sensitive`=ics.sensitive.genes %>%
		dplyr::pull(hgnc_symbol)
) %>% remove.empty.lists()

if (length(genes_all) >=2) {
	venn_plot_all <- genes_all %>%
		ggVennDiagram::ggVennDiagram(
			color="black",
			size=1
		) +
		ggplot2::scale_fill_gradient(
			low="white",
			high="grey"
		)

	ggplot2::ggsave(
	  plot = venn_plot_all,
	  filename = file.path(out.dir, "venn_plot_all.png")
	)

	saveRDS(
	  venn_plot_all,
	  file = file.path(out.dir, "venn_plot_all.rds")
	)
}

################################################################
# Venn Diagram - Pos                                           #
################################################################
genes_pos <- list(
	`Healthy vs Severe`=tt_result_1 %>%
	    dplyr::filter(logFC > 0) %>%
		dplyr::pull(hgnc_symbol),
	`Healthy vs Mild`=tt_result_2 %>%
	    dplyr::filter(logFC > 0) %>%
		dplyr::pull(hgnc_symbol),
	`ICS sensitive`=ics.sensitive.genes %>%
	    dplyr::filter(zmeta > 0) %>%
		dplyr::pull(hgnc_symbol)
) %>% remove.empty.lists()

if (length(genes_pos) >=2) {
	venn_plot_pos <- genes_pos %>%
		ggVennDiagram::ggVennDiagram(
			color="black",
			size=1
		) +
		ggplot2::scale_fill_gradient(
			low="white",
			high="grey"
		)

	ggplot2::ggsave(
	  plot = venn_plot_pos,
	  filename = file.path(out.dir, "venn_plot_pos.png")
	)

	saveRDS(
	  venn_plot_pos,
	  file = file.path(out.dir, "venn_plot_pos.rds")
	)
}

################################################################
# Venn Diagram - Neg                                           #
################################################################
genes_neg <- list(
	`Healthy vs Severe`=tt_result_1 %>%
	    dplyr::filter(logFC < 0) %>%
		dplyr::pull(hgnc_symbol),
	`Healthy vs Mild`=tt_result_2 %>%
	    dplyr::filter(logFC < 0) %>%
		dplyr::pull(hgnc_symbol),
	`ICS sensitive`=ics.sensitive.genes %>%
	    dplyr::filter(zmeta < 0) %>%
		dplyr::pull(hgnc_symbol)
) %>% remove.empty.lists()

if (length(genes_neg) >=2) {
	venn_plot_neg <- genes_neg %>%
		ggVennDiagram::ggVennDiagram(
			color="black",
			size=1
		) +
		ggplot2::scale_fill_gradient(
			low="white",
			high="grey"
		)

	ggplot2::ggsave(
	  plot = venn_plot_neg,
	  filename = file.path(out.dir, "venn_plot_neg.png")
	)

	saveRDS(
	  venn_plot_neg,
	  file = file.path(out.dir, "venn_plot_neg.rds")
	)
}

################################################################
# Write overlapping lists to files                             #
################################################################
empty.list.if.one.list <- function(lists) {
	if (length(lists) <= 1) {
		list()
	} else {
		lists
	}
}

all_unique_intersect <- genes_all %>%
	intersect.all() %>%
	as.data.frame() %>%
	dplyr::filter(
		!(. %in% (genes_pos %>% empty.list.if.one.list() %>% intersect.all())) &
		!(. %in% (genes_neg %>% empty.list.if.one.list() %>% intersect.all()))
	) %>%
	dplyr::pull(.) %>%
	readr::write_lines(
		file.path(out.dir, "all_unique_intersect.txt")
	)

pos_intersect <- genes_pos %>%
	empty.list.if.one.list() %>% 
	intersect.all() %>%
	readr::write_lines(
		file.path(out.dir, "pos_intersect.txt")
	)

neg_intersect <- genes_neg %>%
	empty.list.if.one.list() %>% 
	intersect.all() %>%
	readr::write_lines(
		file.path(out.dir, "neg_intersect.txt")
	)


pos_unique <- genes_pos %>%
	empty.list.if.one.list() %>% 
	unique.all() %>%
	readr::write_lines(
		file.path(out.dir, "pos_unique.txt")
	)

neg_unique <- genes_neg %>%
	empty.list.if.one.list() %>% 
	unique.all() %>%
	readr::write_lines(
		file.path(out.dir, "neg_unique.txt")
	)

tt_result_1 %>%
	dplyr::filter(
		hgnc_symbol %in% c(neg_unique, pos_unique)
	) %>%
	readr::write_csv(
		file.path(out.dir, "unique_results.csv")
	)