PairwiseLK.R 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. # This R script performs GTA L and K Pairwise Compares for user specified pairs of Experiments
  2. library("ggplot2")
  3. library("plotly")
  4. library("htmlwidgets")
  5. library("extrafont")
  6. library("grid")
  7. library("ggthemes")
  8. args <- commandArgs(TRUE)
  9. exp1_name <- args[1]
  10. exp1_file <- args[2]
  11. exp2_name <- args[3]
  12. exp2_file <- args[4]
  13. output_dir <- args[5]
  14. pairDirL <- file.path(output_dir, paste0("PairwiseCompareL_", exp1_name, "-", exp2_name))
  15. pairDirK <- file.path(output_dir, paste0("PairwiseCompareK_", exp1_name, "-", exp2_name))
  16. # Pairwise L
  17. # outputPlotly <- "../GTAresults/PairwiseCompareL/" #"/GTAresults/PairwiseCompareL/"
  18. # Theme elements for plots
  19. theme_Publication <- function(base_size = 14, base_family = "sans") {
  20. (theme_foundation(base_size = base_size, base_family = base_family) +
  21. theme(
  22. plot.title = element_text(face = "bold", size = rel(1.2), hjust = 0.5),
  23. text = element_text(),
  24. panel.background = element_rect(colour = NA),
  25. plot.background = element_rect(colour = NA),
  26. panel.border = element_rect(colour = NA),
  27. axis.title = element_text(face = "bold", size = rel(1)),
  28. axis.title.y = element_text(angle = 90, vjust = 2),
  29. axis.title.x = element_text(vjust = -0.2),
  30. axis.text = element_text(),
  31. axis.line = element_line(colour = "black"),
  32. axis.ticks = element_line(),
  33. panel.grid.major = element_line(colour = "#f0f0f0"),
  34. panel.grid.minor = element_blank(),
  35. legend.key = element_rect(colour = NA),
  36. legend.position = "bottom",
  37. legend.direction = "horizontal",
  38. legend.key.size = unit(0.2, "cm"),
  39. legend.spacing = unit(0, "cm"),
  40. legend.title = element_text(face = "italic"),
  41. plot.margin = unit(c(10, 5, 5, 5), "mm"),
  42. strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
  43. strip.text = element_text(face = "bold")
  44. )
  45. )
  46. }
  47. theme_Publication_legend_right <- function(base_size = 14, base_family = "sans") {
  48. (theme_foundation(base_size = base_size, base_family = base_family) +
  49. theme(
  50. plot.title = element_text(face = "bold", size = rel(1.2), hjust = 0.5),
  51. text = element_text(),
  52. panel.background = element_rect(colour = NA),
  53. plot.background = element_rect(colour = NA),
  54. panel.border = element_rect(colour = NA),
  55. axis.title = element_text(face = "bold", size = rel(1)),
  56. axis.title.y = element_text(angle = 90, vjust = 2),
  57. axis.title.x = element_text(vjust = -0.2),
  58. axis.text = element_text(),
  59. axis.line = element_line(colour = "black"),
  60. axis.ticks = element_line(),
  61. panel.grid.major = element_line(colour = "#f0f0f0"),
  62. panel.grid.minor = element_blank(),
  63. legend.key = element_rect(colour = NA),
  64. legend.position = "right",
  65. legend.direction = "vertical",
  66. legend.key.size = unit(0.5, "cm"),
  67. legend.spacing = unit(0, "cm"),
  68. legend.title = element_text(face = "italic"),
  69. plot.margin = unit(c(10, 5, 5, 5), "mm"),
  70. strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
  71. strip.text = element_text(face = "bold")
  72. )
  73. )
  74. }
  75. scale_fill_Publication <- function(...) {
  76. discrete_scale("fill", "Publication", manual_pal(
  77. values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506",
  78. "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")), ...)
  79. }
  80. scale_colour_Publication <- function(...) {
  81. discrete_scale("colour", "Publication", manual_pal(
  82. values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506",
  83. "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")), ...)
  84. }
  85. X1 <- read.csv(file = exp1_file, stringsAsFactors = FALSE, header = TRUE)
  86. X2 <- read.csv(file = exp2_file, stringsAsFactors = FALSE, header = TRUE)
  87. X <- merge(X1, X2, by = "Term_Avg", all = TRUE, suffixes = c("_X1", "_X2"))
  88. gg <- ggplot(data = X, aes(
  89. x = Z_lm_L_Avg_X1,
  90. y = Z_lm_L_Avg_X2,
  91. color = Ontology_Avg_X1,
  92. Term = Term_Avg,
  93. Genes = Genes_Avg_X1,
  94. NumGenes = NumGenes_Avg_X1,
  95. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  96. SD_1 = Z_lm_L_SD_X1,
  97. SD_2 = Z_lm_L_SD_X2
  98. )) +
  99. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  100. geom_rect(
  101. aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2),
  102. color = "grey20",
  103. size = 0.25,
  104. alpha = 0.1,
  105. inherit.aes = FALSE,
  106. fill = NA
  107. ) +
  108. geom_point(shape = 3) +
  109. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  110. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, " vs. ", exp2_name)) +
  111. theme_Publication_legend_right()
  112. pdf(
  113. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOntology.pdf")),
  114. width = 12,
  115. height = 8
  116. )
  117. gg
  118. dev.off()
  119. pgg <- ggplotly(gg)
  120. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOntology.html"))
  121. htmlwidgets::saveWidget(pgg, fname)
  122. # ID aggravators and alleviators, regardless of whether they meet 2SD threshold
  123. X1_Specific_Aggravators <- X[which(X$Z_lm_L_Avg_X1 >= 2 & X$Z_lm_L_Avg_X2 < 2), ]
  124. X1_Specific_Alleviators <- X[which(X$Z_lm_L_Avg_X1 <= -2 & X$Z_lm_L_Avg_X2 > -2), ]
  125. X2_Specific_Aggravators <- X[which(X$Z_lm_L_Avg_X2 >= 2 & X$Z_lm_L_Avg_X1 < 2), ]
  126. X2_Specific_Alleviators <- X[which(X$Z_lm_L_Avg_X2 <= -2 & X$Z_lm_L_Avg_X1 > -2), ]
  127. Overlap_Aggravators <- X[which(X$Z_lm_L_Avg_X1 >= 2 & X$Z_lm_L_Avg_X2 >= 2), ]
  128. Overlap_Alleviators <- X[which(X$Z_lm_L_Avg_X1 <= -2 & X$Z_lm_L_Avg_X2 <= -2), ]
  129. X2_Specific_Aggravators_X1_Alleviatiors <- X[which(X$Z_lm_L_Avg_X2 >= 2 & X$Z_lm_L_Avg_X1 <= -2), ]
  130. X2_Specific_Alleviators_X1_Aggravators <- X[which(X$Z_lm_L_Avg_X2 <= -2 & X$Z_lm_L_Avg_X1 >= 2), ]
  131. X$Overlap_Avg <- NA
  132. try(X[X$Term_Avg %in% X1_Specific_Aggravators$Term_Avg, ]$Overlap_Avg <-
  133. paste(exp1_name, "Specific_Deletion_Enhancers", sep = "_"))
  134. try(X[X$Term_Avg %in% X1_Specific_Alleviators$Term_Avg, ]$Overlap_Avg <-
  135. paste(exp1_name, "Specific_Deletion_Suppresors", sep = "_"))
  136. try(X[X$Term_Avg %in% X2_Specific_Aggravators$Term_Avg, ]$Overlap_Avg <-
  137. paste(exp2_name, "Specific_Deletion_Enhancers", sep = "_"))
  138. try(X[X$Term_Avg %in% X2_Specific_Alleviators$Term_Avg, ]$Overlap_Avg <-
  139. paste(exp2_name, "Specific_Deletion_Suppressors", sep = "_"))
  140. try(X[X$Term_Avg %in% Overlap_Aggravators$Term_Avg, ]$Overlap_Avg <- "Overlapping_Deletion_Enhancers")
  141. try(X[X$Term_Avg %in% Overlap_Alleviators$Term_Avg, ]$Overlap_Avg <- "Overlapping_Deletion_Suppressors")
  142. try(X[X$Term_Avg %in% X2_Specific_Aggravators_X1_Alleviatiors$Term_Avg, ]$Overlap_Avg <-
  143. paste(exp2_name, "Deletion_Enhancers", exp1_name, "Deletion_Suppressors", sep = "_"))
  144. try(X[X$Term_Avg %in% X2_Specific_Alleviators_X1_Aggravators$Term_Avg, ]$Overlap_Avg <-
  145. paste(exp2_name, "Deletion_Suppressors", exp1_name, "Deletion_Enhancers", sep = "_"))
  146. gg <- ggplot(data = X, aes(
  147. x = Z_lm_L_Avg_X1,
  148. y = Z_lm_L_Avg_X2,
  149. color = Overlap_Avg,
  150. Term = Term_Avg,
  151. Genes = Genes_Avg_X1,
  152. NumGenes = NumGenes_Avg_X1,
  153. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  154. SD_1 = Z_lm_L_SD_X1,
  155. SD_2 = Z_lm_L_SD_X2
  156. )) +
  157. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  158. geom_rect(
  159. aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2),
  160. color = "grey20",
  161. size = 0.25,
  162. alpha = 0.1,
  163. inherit.aes = FALSE,
  164. fill = NA
  165. ) +
  166. geom_point(shape = 3) +
  167. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  168. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, " vs. ", exp2_name)) +
  169. theme_Publication_legend_right()
  170. pdf(
  171. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap.pdf")),
  172. width = 12,
  173. height = 8
  174. )
  175. gg
  176. dev.off()
  177. pgg <- ggplotly(gg)
  178. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap.html"))
  179. htmlwidgets::saveWidget(pgg, fname)
  180. x_rem2_gene <- X[X$NumGenes_Avg_X1 >= 2 & X$NumGenes_Avg_X2 >= 2, ]
  181. #3
  182. gg <- ggplot(data = x_rem2_gene, aes(
  183. x = Z_lm_L_Avg_X1,
  184. y = Z_lm_L_Avg_X2,
  185. color = Overlap_Avg,
  186. Term = Term_Avg,
  187. Genes = Genes_Avg_X1,
  188. NumGenes = NumGenes_Avg_X1,
  189. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  190. SD_1 = Z_lm_L_SD_X1,
  191. SD_2 = Z_lm_L_SD_X2
  192. )) +
  193. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  194. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  195. geom_point(shape = 3) +
  196. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  197. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  198. theme_Publication_legend_right()
  199. pdf(
  200. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_above2genes.pdf")),
  201. width = 12,
  202. height = 8
  203. )
  204. gg
  205. dev.off()
  206. pgg <- ggplotly(gg)
  207. #pgg
  208. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap_above2genes.html"))
  209. htmlwidgets::saveWidget(pgg, fname)
  210. #4
  211. X_overlap_nothresold <- X[!(is.na(X$Overlap_Avg)), ]
  212. gg <- ggplot(data = X_overlap_nothresold, aes(
  213. x = Z_lm_L_Avg_X1,
  214. y = Z_lm_L_Avg_X2,
  215. color = Overlap_Avg,
  216. Term = Term_Avg,
  217. Genes = Genes_Avg_X1,
  218. NumGenes = NumGenes_Avg_X1,
  219. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  220. SD_1 = Z_lm_L_SD_X1,
  221. SD_2 = Z_lm_L_SD_X2
  222. )) +
  223. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  224. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  225. geom_point(shape = 3) +
  226. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  227. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  228. theme_Publication_legend_right()
  229. pdf(
  230. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_Above2SD_ByOverlap.pdf")),
  231. width = 12,
  232. height = 8
  233. )
  234. gg
  235. dev.off()
  236. pgg <- ggplotly(gg)
  237. #pgg
  238. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_Above2SD_ByOverlap.html"))
  239. htmlwidgets::saveWidget(pgg, fname)
  240. # Only output GTA terms where average score is still above 2 after subtracting the SD
  241. # Z1 will ID aggravators, Z2 alleviators
  242. Z1 <- X
  243. Z1$L_Subtract_SD_X1 <- Z1$Z_lm_L_Avg_X1 - Z1$Z_lm_L_SD_X1
  244. Z1$L_Subtract_SD_X2 <- Z1$Z_lm_L_Avg_X2 - Z1$Z_lm_L_SD_X2
  245. Z2 <- X
  246. Z2$L_Subtract_SD_X1 <- Z1$Z_lm_L_Avg_X1 + Z1$Z_lm_L_SD_X1
  247. Z2$L_Subtract_SD_X2 <- Z1$Z_lm_L_Avg_X2 + Z1$Z_lm_L_SD_X2
  248. X1_Specific_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X1 >= 2 & Z1$L_Subtract_SD_X2 < 2), ]
  249. X1_Specific_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X1 <= -2 & Z2$L_Subtract_SD_X2 > -2), ]
  250. X2_Specific_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X2 >= 2 & Z1$L_Subtract_SD_X1 < 2), ]
  251. X2_Specific_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z2$L_Subtract_SD_X1 > -2), ]
  252. Overlap_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X1 >= 2 & Z1$L_Subtract_SD_X2 >= 2), ]
  253. Overlap_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z2$L_Subtract_SD_X1 <= -2), ]
  254. X2_Specific_Aggravators2_X1_Alleviatiors2 <- Z1[which(Z1$L_Subtract_SD_X2 >= 2 & Z2$L_Subtract_SD_X1 <= -2), ]
  255. X2_Specific_Alleviators2_X1_Aggravators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z1$L_Subtract_SD_X1 >= 2), ]
  256. X$Overlap <- NA
  257. try(X[X$Term_Avg %in% X1_Specific_Aggravators2$Term_Avg, ]$Overlap <- paste(exp1_name, "Specific_Deletion_Enhancers", sep = "_"))
  258. try(X[X$Term_Avg %in% X1_Specific_Alleviators2$Term_Avg, ]$Overlap <- paste(exp1_name, "Specific_Deletion_Suppresors", sep = "_"))
  259. try(X[X$Term_Avg %in% X2_Specific_Aggravators2$Term_Avg, ]$Overlap <- paste(exp2_name, "Specific_Deletion_Enhancers", sep = "_"))
  260. try(X[X$Term_Avg %in% X2_Specific_Alleviators2$Term_Avg, ]$Overlap <- paste(exp2_name, "Specific_Deletion_Suppressors", sep = "_"))
  261. try(X[X$Term_Avg %in% Overlap_Aggravators2$Term_Avg, ]$Overlap <- "Overlapping_Deletion_Enhancers")
  262. try(X[X$Term_Avg %in% Overlap_Alleviators2$Term_Avg, ]$Overlap <- "Overlapping_Deletion_Suppressors")
  263. try(X[X$Term_Avg %in% X2_Specific_Aggravators2_X1_Alleviatiors2$Term_Avg, ]$Overlap <-
  264. paste(exp2_name, "Deletion_Enhancers", exp1_name, "Deletion_Suppressors", sep = "_"))
  265. try(X[X$Term_Avg %in% X2_Specific_Alleviators2_X1_Aggravators2$Term_Avg, ]$Overlap <-
  266. paste(exp2_name, "Deletion_Suppressors", exp1_name, "Deletion_Enhancers", sep = "_"))
  267. #5
  268. X_abovethreshold <- X[!(is.na(X$Overlap)), ]
  269. gg <- ggplot(data = X_abovethreshold, aes(
  270. x = Z_lm_L_Avg_X1,
  271. y = Z_lm_L_Avg_X2,
  272. color = Overlap,
  273. Term = Term_Avg,
  274. Genes = Genes_Avg_X1,
  275. NumGenes = NumGenes_Avg_X1,
  276. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  277. SD_1 = Z_lm_L_SD_X1,
  278. SD_2 = Z_lm_L_SD_X2
  279. )) +
  280. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  281. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  282. geom_point(shape = 3) +
  283. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  284. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, " vs. ", exp2_name)) +
  285. theme_Publication_legend_right()
  286. pdf(
  287. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold.pdf")),
  288. width = 12,
  289. height = 8
  290. )
  291. gg
  292. dev.off()
  293. pgg <- ggplotly(gg)
  294. #pgg
  295. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold.html"))
  296. htmlwidgets::saveWidget(pgg, fname)
  297. #6
  298. gg <- ggplot(data = X_abovethreshold, aes(
  299. x = Z_lm_L_Avg_X1,
  300. y = Z_lm_L_Avg_X2,
  301. color = Overlap,
  302. Term = Term_Avg,
  303. Genes = Genes_Avg_X1,
  304. NumGenes = NumGenes_Avg_X1,
  305. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  306. SD_1 = Z_lm_L_SD_X1,
  307. SD_2 = Z_lm_L_SD_X2
  308. )) +
  309. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  310. geom_text(aes(label = Term_Avg), nudge_y = 0.25, size = 2) +
  311. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  312. geom_point(shape = 3, size = 3) +
  313. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  314. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  315. theme_Publication_legend_right()
  316. pdf(
  317. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_names.pdf")),
  318. width = 20,
  319. height = 20
  320. )
  321. gg
  322. dev.off()
  323. pgg <- ggplotly(gg)
  324. #pgg
  325. fname <- file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_names.html"))
  326. htmlwidgets::saveWidget(pgg, fname)
  327. X_abovethreshold$X1_Rank <- NA
  328. X_abovethreshold$X1_Rank <- rank(-X_abovethreshold$Z_lm_L_Avg_X1, ties.method = "random")
  329. X_abovethreshold$X2_Rank <- NA
  330. X_abovethreshold$X2_Rank <- rank(-X_abovethreshold$Z_lm_L_Avg_X2, ties.method = "random")
  331. #7
  332. gg <- ggplot(data = X_abovethreshold, aes(
  333. x = Z_lm_L_Avg_X1,
  334. y = Z_lm_L_Avg_X2,
  335. color = Overlap,
  336. Term = Term_Avg,
  337. Genes = Genes_Avg_X1,
  338. NumGenes = NumGenes_Avg_X1,
  339. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  340. SD_1 = Z_lm_L_SD_X1,
  341. SD_2 = Z_lm_L_SD_X2
  342. )) +
  343. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  344. geom_text(aes(label = X1_Rank), nudge_y = 0.25, size = 4) +
  345. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  346. geom_point(shape = 3, size = 3) +
  347. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  348. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  349. theme_Publication_legend_right()
  350. pdf(
  351. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX1.pdf")),
  352. width = 15,
  353. height = 15
  354. )
  355. gg
  356. dev.off()
  357. pgg <- ggplotly(gg)
  358. #pgg
  359. fname <-
  360. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX1.html"))
  361. htmlwidgets::saveWidget(pgg, fname)
  362. #8
  363. gg <- ggplot(data = X_abovethreshold, aes(
  364. x = Z_lm_L_Avg_X1,
  365. y = Z_lm_L_Avg_X2,
  366. color = Overlap,
  367. Term = Term_Avg,
  368. Genes = Genes_Avg_X1,
  369. NumGenes = NumGenes_Avg_X1,
  370. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  371. SD_1 = Z_lm_L_SD_X1,
  372. SD_2 = Z_lm_L_SD_X2
  373. )) +
  374. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  375. geom_text(aes(label = X2_Rank), nudge_y = 0.25, size = 4) +
  376. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  377. geom_point(shape = 3, size = 3) +
  378. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  379. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  380. theme_Publication_legend_right()
  381. pdf(
  382. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX2.pdf")),
  383. width = 15,
  384. height = 15
  385. )
  386. gg
  387. dev.off()
  388. pgg <- ggplotly(gg)
  389. #pgg
  390. fname <-
  391. file.path(pairDirL, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX2.html"))
  392. htmlwidgets::saveWidget(pgg, fname)
  393. write.csv(
  394. x = X,
  395. file.path(pairDirL, paste0("All_GTA_Avg_Scores_", exp1_name, "_vs_", exp2_name, ".csv")),
  396. row.names = FALSE
  397. )
  398. write.csv(
  399. x = X_abovethreshold,
  400. file = file.path(pairDirL, paste0("AboveThreshold_GTA_Avg_Scores_", exp1_name, "_vs_", exp2_name, ".csv")),
  401. row.names = FALSE
  402. )
  403. # Begin Pairwsise K
  404. # Theme elements for plots
  405. theme_Publication <- function(base_size = 14, base_family = "sans") {
  406. (theme_foundation(base_size = base_size, base_family = base_family) +
  407. theme(
  408. plot.title = element_text(face = "bold", size = rel(1.2), hjust = 0.5),
  409. text = element_text(),
  410. panel.background = element_rect(colour = NA),
  411. plot.background = element_rect(colour = NA),
  412. panel.border = element_rect(colour = NA),
  413. axis.title = element_text(face = "bold", size = rel(1)),
  414. axis.title.y = element_text(angle = 90, vjust = 2),
  415. axis.title.x = element_text(vjust = -0.2),
  416. axis.text = element_text(),
  417. axis.line = element_line(colour = "black"),
  418. axis.ticks = element_line(),
  419. panel.grid.major = element_line(colour = "#f0f0f0"),
  420. panel.grid.minor = element_blank(),
  421. legend.key = element_rect(colour = NA),
  422. legend.position = "bottom",
  423. legend.direction = "horizontal",
  424. legend.key.size = unit(0.2, "cm"),
  425. legend.spacing = unit(0, "cm"),
  426. legend.title = element_text(face = "italic"),
  427. plot.margin = unit(c(10, 5, 5, 5), "mm"),
  428. strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
  429. strip.text = element_text(face = "bold")
  430. )
  431. )
  432. }
  433. scale_fill_Publication <- function(...) {
  434. library("scales")
  435. discrete_scale(
  436. "fill",
  437. "Publication",
  438. manual_pal(values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506", "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")),
  439. ...
  440. )
  441. }
  442. scale_colour_Publication <- function(...) {
  443. discrete_scale(
  444. "colour",
  445. "Publication",
  446. manual_pal(values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506", "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")),
  447. ...
  448. )
  449. }
  450. theme_Publication_legend_right <- function(base_size = 14, base_family = "sans") {
  451. (theme_foundation(base_size = base_size, base_family = base_family) +
  452. theme(
  453. plot.title = element_text(face = "bold",
  454. size = rel(1.2), hjust = 0.5),
  455. text = element_text(),
  456. panel.background = element_rect(colour = NA),
  457. plot.background = element_rect(colour = NA),
  458. panel.border = element_rect(colour = NA),
  459. axis.title = element_text(face = "bold", size = rel(1)),
  460. axis.title.y = element_text(angle = 90, vjust = 2),
  461. axis.title.x = element_text(vjust = -0.2),
  462. axis.text = element_text(),
  463. axis.line = element_line(colour = "black"),
  464. axis.ticks = element_line(),
  465. panel.grid.major = element_line(colour = "#f0f0f0"),
  466. panel.grid.minor = element_blank(),
  467. legend.key = element_rect(colour = NA),
  468. legend.position = "right",
  469. legend.direction = "vertical",
  470. legend.key.size = unit(0.5, "cm"),
  471. legend.spacing = unit(0, "cm"),
  472. legend.title = element_text(face = "italic"),
  473. plot.margin = unit(c(10, 5, 5, 5), "mm"),
  474. strip.background = element_rect(colour = "#f0f0f0", fill = "#f0f0f0"),
  475. strip.text = element_text(face = "bold")
  476. )
  477. )
  478. }
  479. scale_fill_Publication <- function(...) {
  480. discrete_scale(
  481. "fill",
  482. "Publication",
  483. manual_pal(values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506", "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")),
  484. ...
  485. )
  486. }
  487. scale_colour_Publication <- function(...) {
  488. discrete_scale(
  489. "colour",
  490. "Publication",
  491. manual_pal(values = c("#386cb0", "#fdb462", "#7fc97f", "#ef3b2c", "#662506", "#a6cee3", "#fb9a99", "#984ea3", "#ffff33")),
  492. ...
  493. )
  494. }
  495. X1 <- read.csv(file = exp1_file, stringsAsFactors = FALSE, header = TRUE)
  496. X2 <- read.csv(file = exp2_file, stringsAsFactors = FALSE, header = TRUE)
  497. #1
  498. X <- merge(X1, X2, by = "Term_Avg", all = TRUE, suffixes = c("_X1", "_X2"))
  499. gg <- ggplot(data = X, aes(
  500. x = Z_lm_K_Avg_X1,
  501. y = Z_lm_K_Avg_X2,
  502. color = Ontology_Avg_X1,
  503. Term = Term_Avg,
  504. Genes = Genes_Avg_X1,
  505. NumGenes = NumGenes_Avg_X1,
  506. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  507. SD_1 = Z_lm_K_SD_X1,
  508. SD_2 = Z_lm_K_SD_X2
  509. )) +
  510. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  511. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  512. geom_point(shape = 3) +
  513. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  514. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  515. theme_Publication_legend_right()
  516. pdf(
  517. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOntology.pdf")),
  518. width = 12,
  519. height = 8
  520. )
  521. gg
  522. dev.off()
  523. pgg <- ggplotly(gg)
  524. #pgg
  525. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOntology.html"))
  526. htmlwidgets::saveWidget(pgg, fname)
  527. #2
  528. # ID aggravators and alleviators, regardless of whether they meet 2SD threshold
  529. X1_Specific_Aggravators <- X[which(X$Z_lm_K_Avg_X1 >= 2 & X$Z_lm_K_Avg_X2 < 2), ]
  530. X1_Specific_Alleviators <- X[which(X$Z_lm_K_Avg_X1 <= -2 & X$Z_lm_K_Avg_X2 > -2), ]
  531. X2_Specific_Aggravators <- X[which(X$Z_lm_K_Avg_X2 >= 2 & X$Z_lm_K_Avg_X1 < 2), ]
  532. X2_Specific_Alleviators <- X[which(X$Z_lm_K_Avg_X2 <= -2 & X$Z_lm_K_Avg_X1 > -2), ]
  533. Overlap_Aggravators <- X[which(X$Z_lm_K_Avg_X1 >= 2 & X$Z_lm_K_Avg_X2 >= 2), ]
  534. Overlap_Alleviators <- X[which(X$Z_lm_K_Avg_X1 <= -2 & X$Z_lm_K_Avg_X2 <= -2), ]
  535. X2_Specific_Aggravators_X1_Alleviatiors <- X[which(X$Z_lm_K_Avg_X2 >= 2 & X$Z_lm_K_Avg_X1 <= -2), ]
  536. X2_Specific_Alleviators_X1_Aggravators <- X[which(X$Z_lm_K_Avg_X2 <= -2 & X$Z_lm_K_Avg_X1 >= 2), ]
  537. X$Overlap_Avg <- NA
  538. try(X[X$Term_Avg %in% X1_Specific_Aggravators$Term_Avg, ]$Overlap_Avg <-
  539. paste(exp1_name, "Specific_Deletion_Suppressors", sep = "_"))
  540. try(X[X$Term_Avg %in% X1_Specific_Alleviators$Term_Avg, ]$Overlap_Avg <-
  541. paste(exp1_name, "Specific_Deletion_Enhancers", sep = "_"))
  542. try(X[X$Term_Avg %in% X2_Specific_Aggravators$Term_Avg, ]$Overlap_Avg <-
  543. paste(exp2_name, "Specific_Deletion_Suppressors", sep = "_"))
  544. try(X[X$Term_Avg %in% X2_Specific_Alleviators$Term_Avg, ]$Overlap_Avg <-
  545. paste(exp2_name, "Specific_Deletion_Enhancers", sep = "_"))
  546. try(X[X$Term_Avg %in% Overlap_Aggravators$Term_Avg, ]$Overlap_Avg <-
  547. "Overlapping_Deletion_Suppressors")
  548. try(X[X$Term_Avg %in% Overlap_Alleviators$Term_Avg, ]$Overlap_Avg <-
  549. "Overlapping_Deletion_Enhancers")
  550. try(X[X$Term_Avg %in% X2_Specific_Aggravators_X1_Alleviatiors$Term_Avg, ]$Overlap_Avg <-
  551. paste(exp2_name, "Deletion_Suppressors", exp1_name, "Deletion_Enhancers", sep = "_"))
  552. try(X[X$Term_Avg %in% X2_Specific_Alleviators_X1_Aggravators$Term_Avg, ]$Overlap_Avg <-
  553. paste(exp2_name, "Deletion_Enhancers", exp1_name, "Deletion_Suppressors", sep = "_"))
  554. plotly_path <- file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap.html"))
  555. gg <- ggplot(data = X, aes(
  556. x = Z_lm_K_Avg_X1,
  557. y = Z_lm_K_Avg_X2,
  558. color = Overlap_Avg,
  559. Term = Term_Avg,
  560. Genes = Genes_Avg_X1,
  561. NumGenes = NumGenes_Avg_X1,
  562. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  563. SD_1 = Z_lm_K_SD_X1,
  564. SD_2 = Z_lm_K_SD_X2
  565. )) +
  566. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  567. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  568. geom_point(shape = 3) +
  569. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  570. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  571. theme_Publication_legend_right()
  572. pdf(
  573. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap.pdf")),
  574. width = 12,
  575. height = 8
  576. )
  577. gg
  578. dev.off()
  579. pgg <- ggplotly(gg)
  580. #2
  581. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap.html"))
  582. htmlwidgets::saveWidget(pgg, fname)
  583. #3
  584. x_rem2_gene <- X[X$NumGenes_Avg_X1 >= 2 & X$NumGenes_Avg_X2 >= 2, ]
  585. plotly_path <- file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap_above2genes.html"))
  586. gg <- ggplot(data = x_rem2_gene, aes(
  587. x = Z_lm_K_Avg_X1,
  588. y = Z_lm_K_Avg_X2,
  589. color = Overlap_Avg,
  590. Term = Term_Avg,
  591. Genes = Genes_Avg_X1,
  592. NumGenes = NumGenes_Avg_X1,
  593. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  594. SD_1 = Z_lm_K_SD_X1,
  595. SD_2 = Z_lm_K_SD_X2
  596. )) +
  597. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  598. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  599. geom_point(shape = 3) +
  600. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  601. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  602. theme_Publication_legend_right()
  603. pdf(
  604. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_above2genes.pdf")),
  605. width = 12,
  606. height = 8
  607. )
  608. gg
  609. dev.off()
  610. pgg <- ggplotly(gg)
  611. #pgg
  612. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_byOverlap_above2genes.html"))
  613. htmlwidgets::saveWidget(pgg, fname)
  614. #4
  615. X_overlap_nothresold <- X[!(is.na(X$Overlap_Avg)), ]
  616. gg <- ggplot(data = X_overlap_nothresold, aes(
  617. x = Z_lm_K_Avg_X1,
  618. y = Z_lm_K_Avg_X2,
  619. color = Overlap_Avg,
  620. Term = Term_Avg,
  621. Genes = Genes_Avg_X1,
  622. NumGenes = NumGenes_Avg_X1,
  623. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  624. SD_1 = Z_lm_K_SD_X1,
  625. SD_2 = Z_lm_K_SD_X2
  626. )) +
  627. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  628. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  629. geom_point(shape = 3) +
  630. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  631. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  632. theme_Publication_legend_right()
  633. pdf(
  634. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_Above2SD_ByOverlap.pdf")),
  635. width = 12,
  636. height = 8
  637. )
  638. gg
  639. dev.off()
  640. pgg <- ggplotly(gg)
  641. #pgg
  642. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_Above2SD_ByOverlap.html"))
  643. htmlwidgets::saveWidget(pgg, fname)
  644. #5
  645. # Only output GTA terms where average score is still above 2 after subtracting the SD
  646. # Z1 will ID aggravators, Z2 alleviators
  647. Z1 <- X
  648. Z1$L_Subtract_SD_X1 <- Z1$Z_lm_K_Avg_X1 - Z1$Z_lm_K_SD_X1
  649. Z1$L_Subtract_SD_X2 <- Z1$Z_lm_K_Avg_X2 - Z1$Z_lm_K_SD_X2
  650. Z2 <- X
  651. Z2$L_Subtract_SD_X1 <- Z1$Z_lm_K_Avg_X1 + Z1$Z_lm_K_SD_X1
  652. Z2$L_Subtract_SD_X2 <- Z1$Z_lm_K_Avg_X2 + Z1$Z_lm_K_SD_X2
  653. X1_Specific_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X1 >= 2 & Z1$L_Subtract_SD_X2 < 2), ]
  654. X1_Specific_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X1 <= -2 & Z2$L_Subtract_SD_X2 > -2), ]
  655. X2_Specific_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X2 >= 2 & Z1$L_Subtract_SD_X1 < 2), ]
  656. X2_Specific_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z2$L_Subtract_SD_X1 > -2), ]
  657. Overlap_Aggravators2 <- Z1[which(Z1$L_Subtract_SD_X1 >= 2 & Z1$L_Subtract_SD_X2 >= 2), ]
  658. Overlap_Alleviators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z2$L_Subtract_SD_X1 <= -2), ]
  659. X2_Specific_Aggravators2_X1_Alleviatiors2 <- Z1[which(Z1$L_Subtract_SD_X2 >= 2 & Z2$L_Subtract_SD_X1 <= -2), ]
  660. X2_Specific_Alleviators2_X1_Aggravators2 <- Z2[which(Z2$L_Subtract_SD_X2 <= -2 & Z1$L_Subtract_SD_X1 >= 2), ]
  661. X$Overlap <- NA
  662. try(X[X$Term_Avg %in% X1_Specific_Aggravators2$Term_Avg, ]$Overlap <-
  663. paste(exp1_name, "Specific_Deletion_Suppressors", sep = "_"))
  664. try(X[X$Term_Avg %in% X1_Specific_Alleviators2$Term_Avg, ]$Overlap <-
  665. paste(exp1_name, "Specific_Deletion_Enhancers", sep = "_"))
  666. try(X[X$Term_Avg %in% X2_Specific_Aggravators2$Term_Avg, ]$Overlap <-
  667. paste(exp2_name, "Specific_Deletion_Suppressors", sep = "_"))
  668. try(X[X$Term_Avg %in% X2_Specific_Alleviators2$Term_Avg, ]$Overlap <-
  669. paste(exp2_name, "Specific_Deletion_Enhancers", sep = "_"))
  670. try(X[X$Term_Avg %in% Overlap_Aggravators2$Term_Avg, ]$Overlap <-
  671. "Overlapping_Deletion_Suppressors")
  672. try(X[X$Term_Avg %in% Overlap_Alleviators2$Term_Avg, ]$Overlap <-
  673. "Overlapping_Deletion_Enhancers")
  674. try(X[X$Term_Avg %in% X2_Specific_Aggravators2_X1_Alleviatiors2$Term_Avg, ]$Overlap <-
  675. paste(exp2_name, "Deletion_Suppressors", exp1_name, "Deletion_Enhancers", sep = "_"))
  676. try(X[X$Term_Avg %in% X2_Specific_Alleviators2_X1_Aggravators2$Term_Avg, ]$Overlap <-
  677. paste(exp2_name, "Deletion_Enhancers", exp1_name, "Deletion_Suppressors", sep = "_"))
  678. X_abovethreshold <- X[!(is.na(X$Overlap)), ]
  679. gg <- ggplot(data = X_abovethreshold, aes(
  680. x = Z_lm_K_Avg_X1,
  681. y = Z_lm_K_Avg_X2,
  682. color = Overlap,
  683. Term = Term_Avg,
  684. Genes = Genes_Avg_X1,
  685. NumGenes = NumGenes_Avg_X1,
  686. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  687. SD_1 = Z_lm_K_SD_X1,
  688. SD_2 = Z_lm_K_SD_X2
  689. )) +
  690. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  691. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  692. geom_point(shape = 3) +
  693. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  694. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  695. theme_Publication_legend_right()
  696. pdf(
  697. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold.pdf")),
  698. width = 12,
  699. height = 8
  700. )
  701. gg
  702. dev.off()
  703. pgg <- ggplotly(gg)
  704. #pgg
  705. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold.html"))
  706. htmlwidgets::saveWidget(pgg, fname)
  707. #6
  708. gg <- ggplot(data = X_abovethreshold, aes(
  709. x = Z_lm_K_Avg_X1,
  710. y = Z_lm_K_Avg_X2,
  711. color = Overlap,
  712. Term = Term_Avg,
  713. Genes = Genes_Avg_X1,
  714. NumGenes = NumGenes_Avg_X1,
  715. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  716. SD_1 = Z_lm_K_SD_X1,
  717. SD_2 = Z_lm_K_SD_X2
  718. )) +
  719. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  720. geom_text(aes(label = Term_Avg), nudge_y = 0.25, size = 2) +
  721. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  722. geom_point(shape = 3, size = 3) +
  723. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  724. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, " vs. ", exp2_name)) +
  725. theme_Publication_legend_right()
  726. pdf(
  727. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_names.pdf")),
  728. width = 20,
  729. height = 20
  730. )
  731. gg
  732. dev.off()
  733. pgg <- ggplotly(gg)
  734. #pgg
  735. fname <- file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_names.html"))
  736. htmlwidgets::saveWidget(pgg, fname)
  737. #7
  738. X_abovethreshold$X1_Rank <- NA
  739. X_abovethreshold$X1_Rank <- rank(-X_abovethreshold$Z_lm_K_Avg_X1, ties.method = "random")
  740. X_abovethreshold$X2_Rank <- NA
  741. X_abovethreshold$X2_Rank <- rank(-X_abovethreshold$Z_lm_K_Avg_X2, ties.method = "random")
  742. gg <- ggplot(data = X_abovethreshold, aes(
  743. x = Z_lm_K_Avg_X1,
  744. y = Z_lm_K_Avg_X2,
  745. color = Overlap,
  746. Term = Term_Avg,
  747. Genes = Genes_Avg_X1,
  748. NumGenes = NumGenes_Avg_X1,
  749. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  750. SD_1 = Z_lm_K_SD_X1,
  751. SD_2 = Z_lm_K_SD_X2
  752. )) +
  753. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  754. geom_text(aes(label = X1_Rank), nudge_y = 0.25, size = 4) +
  755. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  756. geom_point(shape = 3, size = 3) +
  757. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  758. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  759. theme_Publication_legend_right()
  760. pdf(
  761. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX1.pdf")),
  762. width = 15,
  763. height = 15
  764. )
  765. gg
  766. dev.off()
  767. pgg <- ggplotly(gg)
  768. #pgg
  769. fname <-
  770. file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX1.html"))
  771. htmlwidgets::saveWidget(pgg, fname)
  772. #8
  773. gg <- ggplot(data = X_abovethreshold, aes(
  774. x = Z_lm_K_Avg_X1,
  775. y = Z_lm_K_Avg_X2,
  776. color = Overlap,
  777. Term = Term_Avg,
  778. Genes = Genes_Avg_X1,
  779. NumGenes = NumGenes_Avg_X1,
  780. AllPossibleGenes = AllPossibleGenes_Avg_X1,
  781. SD_1 = Z_lm_K_SD_X1,
  782. SD_2 = Z_lm_K_SD_X2
  783. )) +
  784. xlab(paste("GO Term Avg lm Z for", exp1_name)) +
  785. geom_text(aes(label = X2_Rank), nudge_y = 0.25, size = 4) +
  786. geom_rect(aes(xmin = -2, xmax = 2, ymin = -2, ymax = 2), color = "grey20", size = 0.25, alpha = 0.1, inherit.aes = FALSE, fill = NA) +
  787. geom_point(shape = 3, size = 3) +
  788. ylab(paste("GO Term Avg lm Z for", exp2_name)) +
  789. ggtitle(paste("Comparing Average GO Term Z lm for", exp1_name, "vs.", exp2_name)) +
  790. theme_Publication_legend_right()
  791. pdf(
  792. file.path(pairDirK, paste0("Scatter_lm_GTF_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX2.pdf")),
  793. width = 15,
  794. height = 15
  795. )
  796. gg
  797. dev.off()
  798. pgg <- ggplotly(gg)
  799. #pgg
  800. fname <-
  801. file.path(pairDirK, paste0("Scatter_lm_GTA_Averages_", exp1_name, "_vs_", exp2_name, "_All_ByOverlap_AboveThreshold_numberedX2.html"))
  802. htmlwidgets::saveWidget(pgg, fname)
  803. write.csv(
  804. x = X,
  805. file = file.path(pairDirK, paste0("All_GTF_Avg_Scores_", exp1_name, "_vs_", exp2_name, ".csv")),
  806. row.names = FALSE
  807. )
  808. write.csv(
  809. x = X_abovethreshold,
  810. file = file.path(pairDirK, paste0("AboveThreshold_GTF_Avg_Scores_", exp1_name, "_vs_", exp2_name, ".csv")),
  811. row.names = FALSE
  812. )