gtaTemplate.R 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/env R
  2. # GTA (GoTermAveraging)
  3. # Your output may not be reproducible as org.Sc.sgd.db is uploaded from Bioconductor R library and changes
  4. #
  5. # Updated 240724 Bryan C Roessler to improve file operations and portability
  6. # NOTE: The script now has 2 additional OPTIONAL arguments:
  7. # 1. Path to SGD terms file (go.terms.tab)
  8. # 2. Path to SGD features file (gene_association.sgd)
  9. library("stringr")
  10. library("org.Sc.sgd.db")
  11. library("plyr")
  12. # Parse arguments
  13. args <- commandArgs(TRUE)
  14. exp_name <- args[1]
  15. if (length(args) >= 2) {
  16. zscores_file <- args[2]
  17. } else {
  18. zscores_file <- "zscores/zscores_interaction.csv" # https://downloads.yeastgenome.org/curation/chromosomal_feature/gene_association.sgd
  19. }
  20. if (length(args) >= 3) {
  21. sgd_terms_file <- args[3]
  22. } else {
  23. sgd_terms_file <- "go_terms.tab"
  24. }
  25. if (length(args) >= 4) {
  26. sgd_features_file <- args[4]
  27. } else {
  28. sgd_features_file <- "gene_association.sgd" # https://downloads.yeastgenome.org/curation/chromosomal_feature/gene_association.sgd
  29. }
  30. if (length(args) >= 5) {
  31. output_dir <- args[5]
  32. } else {
  33. output_dir <- "../../out/gta" # https://downloads.yeastgenome.org/curation/chromosomal_feature/gene_association.sgd
  34. }
  35. # # Set SGDgeneList file path
  36. # if (length(args) > 4) {
  37. # SGDgeneList <- args[4]
  38. # } else {
  39. # SGDgeneList <- "../Code/SGD_features.tab"
  40. # Begin for loop for experiments in this study
  41. # ZScores_Interaction.csv
  42. for (m in 1:length(zscores_file)) {
  43. # zscores_file <- paste(Wstudy,"/",expName[m],'/ZScores/ZScores_Interaction.csv',sep="") #ArgsScore[1]
  44. X <- read.csv(file = zscores_file[m], stringsAsFactors = FALSE, header = TRUE)
  45. if (colnames(X)[1] == "OrfRep") {
  46. colnames(X)[1] <- "ORF"
  47. }
  48. #Terms is the GO term list
  49. Terms <- read.delim(file = sgd_terms_file, header = FALSE, quote = "",
  50. col.names = c("GO_ID", "GO_Term", "GO_Aspect", "GO_Term_Definition"))
  51. #all ORFs associated with GO term
  52. GO2ALLORFs <- as.list(org.Sc.sgdGO2ALLORFS)
  53. #Gene_Association is the gene association to GO term file
  54. Gene_Association <- read.delim(sgd_features_file, skip = 8, header = FALSE, quote = "",
  55. col.names = c("Database", "Database_Object_ID", "Database_Object_Symbol", "NOT", "GO_ID",
  56. "Database_Reference", "Evidence", "With_or_From", "Aspect", "Database_Object_Name",
  57. "Database_Object_Synonym", "Database_Object_Type", "taxon", "Date", "Assigned_By", "OtherInfo", "Empty"
  58. )
  59. )
  60. #Get the ORF names associated with each gene/GO term
  61. Gene_Association$ORF <- str_split_fixed(as.character(Gene_Association$Database_Object_Synonym), "\\|", 2)[, 1]
  62. #Get the numeric GO ID for matching
  63. Gene_Association$GO_ID_Numeric <- as.integer(str_split_fixed(as.character(Gene_Association$GO_ID), "\\:", 2)[, 2])
  64. #Get all unique GO terms
  65. GO_Terms <- unique(Gene_Association$GO_ID)
  66. #Create a character vector with just the ColNames of the input file to store the scores for each GO term
  67. Col_Names_X <- colnames(X)
  68. #Create a data_frame with header from input_file
  69. GO_Term_Averages <- X[0, ]
  70. #Fill table with NAs same length as number of GO terms
  71. GO_Term_Averages[1:length(GO_Terms), ] <- NA
  72. #Change the first and second col names to GO_ID and Term
  73. colnames(GO_Term_Averages)[1] <- "GO_ID"
  74. colnames(GO_Term_Averages)[2] <- "Term"
  75. # Create new columns
  76. GO_Term_Averages$Ontology <- NA
  77. GO_Term_Averages$NumGenes <- NA
  78. GO_Term_Averages$AllPossibleGenes <- NA
  79. GO_Term_Averages$Genes <- NA
  80. GO_Term_Averages$ORFs <- NA
  81. # Create a data.frame for the standard deviation info
  82. GO_Term_SD <- X[0, ]
  83. GO_Term_SD[1:length(GO_Terms), ] <- NA
  84. colnames(GO_Term_SD)[1] <- "GO_ID"
  85. colnames(GO_Term_SD)[2] <- "Term"
  86. # Loop for each GO term to get an average L and K Z score
  87. for (i in 1:length(GO_Terms)) {
  88. # Get the GO_Term
  89. ID <- GO_Terms[i]
  90. # Get data.frame for all genes associated to the GO Term
  91. ID_AllGenes <- Gene_Association[Gene_Association$GO_ID == ID, ]
  92. # Get a vector of just the gene names
  93. ID_AllGenes_vector <- as.vector(GO2ALLORFs[as.character(ID)][[1]])
  94. if (length(unique(ID_AllGenes_vector)) > 4000) {
  95. next()
  96. }
  97. # Get the GO term character description where numeric Terms ID matches GO_Term's ID
  98. GO_Description_Term <- as.character(Terms[Terms$GO_ID %in% ID_AllGenes$GO_ID_Numeric, ]$GO_Term[1])
  99. # Get the Z scores for all genes in the GO_ID
  100. Zscores_For_ID <- X[X$ORF %in% ID_AllGenes_vector, ]
  101. # Get the Gene names and ORFs for the term
  102. GO_Term_Averages$Genes[i] <- paste(unique(Zscores_For_ID$Gene), collapse = " | ")
  103. GO_Term_Averages$ORFs[i] <- paste(unique(Zscores_For_ID$ORF), collapse = " | ")
  104. # Dataframe to report the averages for a GO term
  105. # Get the GO ID
  106. GO_Term_Averages$GO_ID[i] <- as.character(ID)
  107. # Get the term name
  108. GO_Term_Averages$Term[i] <- GO_Description_Term
  109. # Get total number of genes annotated to the Term that we have in our library
  110. GO_Term_Averages$NumGenes[i] <- length(unique(Zscores_For_ID$ORF))
  111. # Get total number of genes annotated to the Term in SGD
  112. GO_Term_Averages$AllPossibleGenes[i] <- length(unique(ID_AllGenes_vector))
  113. # Get the ontology of the term
  114. GO_Term_Averages$Ontology[i] <- as.character(ID_AllGenes$Aspect[1])
  115. # Calculate the average score for every column
  116. for (j in 3:length(X[1, ])) {
  117. GO_Term_Averages[i, j] <- mean(Zscores_For_ID[, j], na.rm = TRUE)
  118. # GO_Scores <- colMeans(Zscores_For_ID[,3:length(X[1,])])
  119. }
  120. # Also calculate same values for the SD
  121. GO_Term_SD$GO_ID[i] <- as.character(ID)
  122. # Get the term name
  123. GO_Term_SD$Term[i] <- GO_Description_Term
  124. # Calculate column scores for SD
  125. for (j in 3:length(X[1, ])) {
  126. GO_Term_SD[i, j] <- sd(Zscores_For_ID[, j], na.rm = TRUE)
  127. # GO_Scores <- colMeans(Zscores_For_ID[,3:length(X[1,])])
  128. }
  129. }
  130. # Add either _Avg or _SD depending on if the calculated score is an average or SD
  131. colnames(GO_Term_Averages) <- paste(colnames(GO_Term_Averages), "Avg", sep = "_")
  132. colnames(GO_Term_SD) <- paste(colnames(GO_Term_SD), "SD", sep = "_")
  133. # Combine the averages with the SDs to make one big data.frame
  134. X2 <- cbind(GO_Term_Averages, GO_Term_SD)
  135. # Test[ , order(names(test))]
  136. X2 <- X2[, order(names(X2))]
  137. X2 <- X2[!is.na(X2$Z_lm_L_Avg), ]
  138. # Create output file
  139. write.csv(X2, file.path(output_dir, expName[m], "Average_GOTerms_All.csv"), row.names = FALSE)
  140. # Remove NAs
  141. X3 <- X2[!is.na(X2$Z_lm_L_Avg), ]
  142. # Identify redundant GO terms
  143. for (i in 1:length(X3[, 1])) {
  144. # Loop through each GO term - get term
  145. GO_term_ID <- as.character(X3$GO_ID_Avg[i])
  146. # Get term in the X3
  147. X3_Temp <- X3[X3$GO_ID_Avg == GO_term_ID, ]
  148. # Get anywhere that has the same number K_Avg value
  149. X3_Temp2 <- X3[X3$Z_lm_K_Avg %in% X3_Temp, ]
  150. if (length(X3_Temp2[, 1]) > 1) {
  151. if (length(unique(X3_Temp2$Genes_Avg)) == 1) {
  152. X3_Temp2 <- X3_Temp2[1, ]
  153. }
  154. }
  155. if (i == 1) {
  156. Y <- X3_Temp2
  157. }
  158. if (i > 1) {
  159. Y <- rbind(Y, X3_Temp2)
  160. }
  161. }
  162. Y1 <- unique(Y)
  163. write.csv(Y1, file.path(output_dir, exp_name, "Average_GOTerms_All_NonRedundantTerms.csv"), row.names = FALSE)
  164. Y2 <- Y1[Y1$Z_lm_L_Avg >= 2 | Y1$Z_lm_L_Avg <= -2, ]
  165. Y2 <- Y2[!is.na(Y2$Z_lm_L_Avg), ]
  166. write.csv(Y2, file.path(output_dir, exp_name, "Average_GOTerms_NonRedundantTerms_Above2SD_L.csv"), row.names = FALSE)
  167. Y3 <- Y2[Y2$NumGenes_Avg > 2, ]
  168. write.csv(Y3, file.path(output_dir, exp_name, "Average_GOTerms_NonRedundantTerms_Above2SD_L_Above2Genes.csv"), row.names = FALSE)
  169. Y4 <- Y1[Y1$Z_lm_K_Avg >= 2 | Y1$Z_lm_K_Avg <= -2, ]
  170. Y4 <- Y4[!is.na(Y4$Z_lm_K_Avg), ]
  171. write.csv(Y4, file.path(output_dir, exp_name, "Average_GOTerms_NonRedundantTerms_Above2SD_K.csv"), row.names = FALSE)
  172. Y5 <- Y4[Y4$NumGenes_Avg > 2, ]
  173. write.csv(Y5, file.path(output_dir, exp_name, "Average_GOTerms_NonRedundantTerms_Above2SD_K_Above2Genes.csv"), row.names = FALSE)
  174. }