2
|
1 ###################################################################################################
|
|
2 # R-code: APOSTL Global Variables
|
|
3 # Author: Brent Kuenzi
|
|
4
|
|
5 ###################################################################################################
|
|
6 # This program performs the file merging as well as a serious of calculations
|
|
7 # Following merging the following parameters will be calculated:
|
|
8 # 1) CRAPomePCT
|
|
9 # 2) NSAF
|
|
10 # 3) NSAFscore
|
|
11 # The resulting table will be exported. This is performed as its own tool and should not be used
|
|
12 # for input into the interactive analysis tool or the standalone bubble graph tool
|
|
13 ################################## Dependencies ###################################################
|
|
14 library(dplyr); library(tidyr)
|
|
15 ################################# Read in Data ####################################################
|
|
16 ## REQUIRED INPUTS ##
|
|
17 ## 1) listfile (filename)
|
|
18 #listfile <- "EGFR_list.txt"
|
|
19 ## 2) Prey File (filename)
|
|
20 #preyfile <- "EGFR_prey.txt"
|
|
21 ## 3) crapome File (filename or FALSE)
|
|
22 #crapfile <- "EGFR_crap.txt"
|
|
23 ## 4) Inter File (filename)
|
|
24 #interfile <- "inter.txt"
|
|
25 ################################# Create Table ####################################################
|
|
26 merge_files <- function(SAINT_DF, prey_DF, crapome=FALSE) {
|
|
27 SAINT <- read.table(SAINT_DF, sep='\t', header=TRUE)
|
|
28 prey <- read.table(prey_DF, sep='\t', header=FALSE); colnames(prey) <- c("Prey", "Length", "PreyGene")
|
|
29 DF <- merge(SAINT,prey)
|
|
30
|
|
31 if(crapome!=FALSE) {
|
|
32 crapome <- read.table(crapome, sep='\t', header=TRUE)
|
|
33 colnames(crapome) <- c("Prey", "Symbol", "Num.of.Exp", "Ave.SC", "Max.SC")
|
|
34 DF1 <- merge(DF, crapome); as.character(DF1$Num.of.Exp); DF1$Symbol <- NULL;
|
|
35 DF1$Ave.SC <- NULL; DF1$Max.SC <- NULL #remove unnecessary columns
|
|
36 DF1$Num.of.Exp <- sub("^$", "0 / 1", DF1$Num.of.Exp ) #replace blank values with 0 / 1
|
|
37 DF <- DF1 %>% separate(Num.of.Exp, c("NumExp", "TotalExp"), " / ") #split into 2 columns
|
|
38 DF$CrapomePCT <- round(100 - (as.integer(DF$NumExp) / as.integer(DF$TotalExp) * 100), digits=2) #calculate crapome %
|
|
39
|
|
40 }
|
|
41 DF$FoldChange <- round(log2(DF$FoldChange),digits=2)
|
|
42 colnames(DF)[(colnames(DF)=="FoldChange")] <- "log2(FoldChange)"
|
|
43
|
|
44 DF$SAF <- DF$AvgSpec / DF$Length
|
|
45 by_bait <- DF %>% group_by(Bait) %>% mutate("NSAF" = SAF/sum(SAF))
|
|
46 by_bait$SAF <- NULL
|
|
47 return(by_bait[!duplicated(by_bait),])
|
|
48 }
|
|
49
|
|
50 main <- function(listfile, preyfile, crapfile, interfile) {
|
|
51 working <- as.data.frame(merge_files(listfile, preyfile, crapfile))
|
|
52 inter_df <- read.table(interfile, sep='\t', header=FALSE)
|
|
53 working$temp <- strsplit(as.character(working$ctrlCounts),"[|]")
|
|
54 cnt <- 0
|
|
55 for(i in working$temp){
|
|
56 cnt <- cnt+1
|
|
57 working$ctrl_mean[cnt] <- mean(as.numeric(unlist(i)))
|
|
58 working$ctrl_number[cnt] <- length(i)}
|
|
59 working$ctrl_SAF <- working$ctrl_mean / working$Length
|
|
60 main.data <- working %>% group_by(Bait) %>% mutate("control_NSAF" = ctrl_SAF/sum(ctrl_SAF))
|
|
61 ctrl_SAF_constant <- 1/mean(main.data$ctrl_SAF)
|
|
62 # add ctrl_SAF_constant to prevent dividing by 0
|
|
63 cnt <- 0
|
|
64 for(i in main.data$control_NSAF){
|
|
65 cnt <- cnt + 1
|
|
66 main.data$nsafScore[cnt] <- ((main.data$NSAF[cnt])+ctrl_SAF_constant)/((i/main.data$ctrl_number[cnt])+ctrl_SAF_constant)
|
|
67 }
|
|
68 main.data$NSAF <- log(main.data$NSAF)
|
|
69 main.data$nsafScore <- log(main.data$nsafScore)
|
|
70 main.data <- filter(main.data, NSAF > -Inf)
|
|
71 colnames(main.data)[colnames(main.data)=="NSAF"] <- "ln(NSAF)"
|
|
72 colnames(main.data)[colnames(main.data)=="nsafScore"] <- "NSAFScore"
|
|
73 main.data$SAF <- NULL; main.data$ctrl_SAF <- NULL
|
|
74 main.data$control_NSAF <- NULL; main.data$temp <- NULL
|
|
75 main.data$ctrl_mean <- NULL
|
|
76 write.table(main.data,file="SaintTable.txt",sep="\t",row.names=FALSE,quote=FALSE)
|
|
77 }
|
|
78
|
|
79 args <- commandArgs(trailingOnly = TRUE)
|
|
80 main(args[1], args[2], args[3], args[4])
|