3
|
1 rm(list=ls())
|
|
2 ###################################################################################################
|
|
3 # R-code: Multi-bubble graph generation from SAINTexpress output
|
|
4 # Author: Brent Kuenzi
|
|
5 ###################################################################################################
|
12
|
6 ins_check_run <- function(){
|
|
7 if ('dplyr' %in% rownames(installed.packages())){}
|
|
8 else {
|
|
9 install.packages('dplyr', repos='http://cran.us.r-project.org')
|
|
10 }
|
|
11 if ('tidyr' %in% rownames(installed.packages())){}
|
|
12 else {
|
|
13 install.packages('tidyr', repos='http://cran.us.r-project.org')
|
|
14 }
|
|
15 if ('ggplot2' %in% rownames(installed.packages())){}
|
|
16 else {
|
|
17 install.packages('ggplot2', repos='http://cran.us.r-project.org')
|
|
18 }
|
|
19 }
|
10
|
20 ins_check_run()
|
3
|
21 library(dplyr); library(tidyr); library(ggplot2)
|
|
22 ###################################################################################################
|
|
23 ### Run program ###
|
|
24
|
|
25 ## REQUIRED INPUT ##
|
|
26 # 1) listfile: SAINTexpress generated "list.txt" file
|
|
27 # 2) preyfile: SAINT pre-processing generated "prey.txt" file used to run SAINTexpress
|
|
28 ## OPTIONAL INPUT ##
|
|
29 # 3) crapome: raw output from crapome Workflow 1 query (http://www.crapome.org)
|
|
30 # 4) color: bubble color (default = "red")
|
|
31 # - color= "crapome": color bubbles based on Crapome(%)
|
|
32 # - Also recognizes any color within R's built-in colors() vector
|
|
33 # 5) label: Adds gene name labels to bubbles within the "zoomed in" graphs (default = FALSE)
|
|
34 # 6) cutoff: Saintscore cutoff to be assigned for filtering the "zoomed in" graphs (default = 0.8)
|
|
35 ###################################################################################################
|
|
36 main <- function(listfile, preyfile , crapome=FALSE, color="red", label=FALSE, cutoff=0.8, type="SC", inc_file = "None", exc_file = "None" ) {
|
|
37 cutoff_check(cutoff)
|
|
38 listfile <- list_type(listfile, inc_file, exc_file)
|
|
39 if(type == "SC") {
|
|
40 df <- merge_files_sc(listfile, preyfile, crapome)
|
|
41 }
|
|
42 if(type == "MQ") {
|
|
43 df <- merge_files_mq(listfile, preyfile, crapome)
|
|
44 }
|
|
45 bubble_NSAF(df,color)
|
|
46 bubble_SAINT(df,color)
|
|
47 bubble_zoom_SAINT(df, color, label, cutoff)
|
|
48 bubble_zoom_NSAF(df, color, label, cutoff)
|
|
49 write.table(df,"output.txt",sep="\t",quote=FALSE, row.names=FALSE)
|
|
50 }
|
|
51
|
|
52 list_type <- function(df, inc_file, exc_file) {
|
|
53 Saint <- read.delim(df, stringsAsFactors=FALSE)
|
|
54 if (inc_file != "None") {
|
|
55 if (exc_file == "None"){
|
|
56 inc_prots <- read.delim(inc_file, sep='\t', header=FALSE, stringsAsFactors=FALSE)
|
|
57 print(inc_prots[,1])
|
|
58 print(Saint$Prey)
|
|
59 filtered_df = subset(Saint, Saint$Prey == inc_prots[,1])
|
|
60 }
|
|
61 else {
|
|
62 inc_prots <- read.delim(inc_file, sep='\t', header=FALSE, stringsAsFactors=FALSE)
|
|
63 exc_prots <- read.delim(exc_file, sep='\t', header=FALSE, stringsAsFactors=FALSE)
|
|
64 filtered_df = subset(Saint, Saint$Prey == inc_prots[,1])
|
|
65 filtered_df = subset(filtered_df, filtered_df$Prey != exc_prots[,1])
|
|
66 }
|
|
67 }
|
|
68 else if (exc_file != "None") {
|
|
69 exc_prots <- read.delim(exc_file, sep='\t', header=FALSE, stringsAsFactors=FALSE)
|
|
70 filtered_df = subset(Saint, Saint$Prey != exc_prots[,1])
|
|
71 }
|
|
72 else {
|
|
73 filtered_df = Saint
|
|
74 }
|
|
75 return(filtered_df)
|
|
76
|
|
77 }
|
|
78 ###################################################################################################
|
|
79 # Merge input files and caculate Crapome(%) and NSAF for each protein for each bait
|
|
80 ###################################################################################################
|
|
81 merge_files_mq <- function(SAINT, prey_DF, crapome=FALSE) {
|
|
82 #SAINT <- read.table(SAINT_DF, sep='\t', header=TRUE)
|
|
83 prey <- read.table(prey_DF, sep='\t', header=FALSE); colnames(prey) <- c("Prey", "Length", "PreyGene")
|
|
84 DF <- merge(SAINT,prey)
|
|
85 DF$SpecSum <- log2(DF$SpecSum)
|
|
86
|
|
87 if(crapome!=FALSE) {
|
|
88 crapome <- read.table(crapome, sep='\t', header=TRUE)
|
|
89 colnames(crapome) <- c("Prey", "Symbol", "Num.of.Exp", "Ave.SC", "Max.SC")
|
|
90 DF1 <- merge(DF, crapome); as.character(DF1$Num.of.Exp); DF1$Symbol <- NULL;
|
|
91 DF1$Ave.SC <- NULL; DF1$Max.SC <- NULL #remove unnecessary columns
|
|
92 DF1$Num.of.Exp <- sub("^$", "0 / 1", DF1$Num.of.Exp ) #replace blank values with 0 / 1
|
|
93 DF <- DF1 %>% separate(Num.of.Exp, c("NumExp", "TotalExp"), " / ") #split into 2 columns
|
|
94 DF$CrapomePCT <- 100 - (as.integer(DF$NumExp) / as.integer(DF$TotalExp) * 100) #calculate crapome %
|
|
95 }
|
|
96 DF$SAF <- DF$AvgSpec / DF$Length
|
|
97 DF2 = DF %>% group_by(Bait) %>% mutate(NSAF = SAF/sum(SAF))
|
|
98 DF$NSAF = DF2$NSAF
|
|
99 return(DF)
|
|
100 }
|
|
101
|
|
102 merge_files_sc <- function(SAINT, prey_DF, crapome=FALSE) {
|
|
103 #SAINT <- read.table(SAINT_DF, sep='\t', header=TRUE)
|
|
104 prey <- read.table(prey_DF, sep='\t', header=FALSE); colnames(prey) <- c("Prey", "Length", "PreyGene")
|
|
105 DF <- merge(SAINT,prey)
|
|
106
|
|
107 if(crapome!=FALSE) {
|
|
108 crapome <- read.table(crapome, sep='\t', header=TRUE)
|
|
109 colnames(crapome) <- c("Prey", "Symbol", "Num.of.Exp", "Ave.SC", "Max.SC")
|
|
110 DF1 <- merge(DF, crapome); as.character(DF1$Num.of.Exp); DF1$Symbol <- NULL;
|
|
111 DF1$Ave.SC <- NULL; DF1$Max.SC <- NULL #remove unnecessary columns
|
|
112 DF1$Num.of.Exp <- sub("^$", "0 / 1", DF1$Num.of.Exp ) #replace blank values with 0 / 1
|
|
113 DF <- DF1 %>% separate(Num.of.Exp, c("NumExp", "TotalExp"), " / ") #split into 2 columns
|
|
114 DF$CrapomePCT <- 100 - (as.integer(DF$NumExp) / as.integer(DF$TotalExp) * 100) #calculate crapome %
|
|
115 }
|
|
116 DF$SAF <- DF$AvgSpec / DF$Length
|
|
117 DF2 = DF %>% group_by(Bait) %>% mutate(NSAF = SAF/sum(SAF))
|
|
118 DF$NSAF = DF2$NSAF
|
|
119 return(DF)
|
|
120 }
|
|
121 ###################################################################################################
|
|
122 # Plot all proteins for each bait by x=ln(NSAF), y=Log2(FoldChange)
|
|
123 ###################################################################################################
|
|
124 bubble_NSAF <- function(data, color) {
|
|
125 if(color=="crapome") {
|
|
126 a <- subset(data, CrapomePCT <80, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait))
|
|
127 b <- subset(data, CrapomePCT>=80, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait))
|
|
128 p <- qplot(x=log(NSAF), y=log2(FoldChange), data=a, colour=I("tan"),size=SpecSum) + scale_size(range=c(1,10)) +
|
|
129 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a)
|
|
130 if(length(levels(a$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")} # multiple graphs if multiple baits
|
|
131 p <- p + geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum, color=CrapomePCT), data=b) +
|
|
132 scale_colour_gradient(limits=c(80, 100), low="tan", high="red") +
|
|
133 labs(colour="CRAPome Probability \nof Specific Interaction (%)", x="ln(NSAF)") +
|
|
134 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=b)
|
|
135 return(ggsave(p, width=8,height=4,filename = "bubble_NSAF.png"))
|
|
136 }
|
|
137 if(color != "crapome") {
|
|
138 p <- qplot(x=log(NSAF), y=log2(FoldChange), data=data, colour=I(color),size=SpecSum) + scale_size(range=c(1,10)) +
|
|
139 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=data) + # add bubble outlines
|
|
140 labs(x="ln(NSAF)")
|
|
141 if(length(levels(data$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
142 return(ggsave(p, width=8,height=4,filename = "bubble_NSAF.png"))
|
|
143 }
|
|
144 }
|
|
145 ###################################################################################################
|
|
146 # Plot all proteins for each bait by x=Saintscore, y=Log2(FoldChange)
|
|
147 ###################################################################################################
|
|
148 bubble_SAINT <- function(data, color) {
|
|
149 if(color=="crapome") {
|
|
150 a <- subset(data, CrapomePCT <80, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait)) #filter on CRAPome
|
|
151 b <- subset(data, CrapomePCT >=80, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait))
|
|
152 p <- qplot(x=SaintScore, y=log2(FoldChange), data=a, colour=I("tan"),size=SpecSum) +
|
|
153 scale_size(range=c(1,10)) + geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a)
|
|
154 if(length(levels(a$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
155 p <- p + geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum, color=CrapomePCT), data=b) +
|
|
156 scale_colour_gradient(limits=c(80, 100), low="tan", high="red") +
|
|
157 labs(colour="CRAPome Probability \nof Specific Interaction (%)") +
|
|
158 geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=b)
|
|
159 return(ggsave(p, width=8,height=4,filename = "bubble_SAINT.png"))
|
|
160 }
|
|
161 if(color != "crapome") {
|
|
162 p <- qplot(x=SaintScore, y=log2(FoldChange), data=data, colour=I(color),size=SpecSum) +
|
|
163 scale_size(range=c(1,10)) + geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=data)
|
|
164 if(length(levels(data$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
165 return(ggsave(p, width=8,height=4,filename = "bubble_SAINT.png"))
|
|
166 }
|
|
167 }
|
|
168 ###################################################################################################
|
|
169 # Filter proteins on Saintscore cutoff and plot for each bait x=Saintscore, y=Log2(FoldChange)
|
|
170 ###################################################################################################
|
|
171 bubble_zoom_SAINT <- function(data, color, label=FALSE, cutoff=0.8) {
|
|
172 if(color=="crapome") {
|
|
173 a <- subset(data, CrapomePCT <80 & SaintScore>=cutoff, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait, PreyGene))
|
|
174 b <- subset(data, CrapomePCT >=80 & SaintScore >=cutoff, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait, PreyGene))
|
|
175 p <- qplot(x=SaintScore, y=log2(FoldChange), data=a, colour=I("tan"),size=SpecSum) +
|
|
176 scale_size(range=c(1,10)) + ggtitle("Filtered on SAINT score")+geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a)
|
|
177 if(label==TRUE & length(a$NSAF!=0)) {
|
|
178 p <- p + geom_text(data=a, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black")
|
|
179 }
|
|
180 if(length(levels(a$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
181 p <- p + geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum, color=CrapomePCT), data=b) +
|
|
182 scale_colour_gradient(limits=c(80, 100), low="tan", high="red") +
|
|
183 labs(colour="CRAPome Probability \nof Specific Interaction (%)") +
|
|
184 geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=b)
|
|
185 if(label==TRUE & length(b$NSAF!=0)) {
|
|
186 p <- p + geom_text(data=b, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black", show_guide=FALSE)
|
|
187 }
|
|
188 return(ggsave(p, width=8,height=4,filename = "bubble_zoom_SAINT.png"))
|
|
189 }
|
|
190 if(color != "crapome") {
|
|
191 a <- subset(data, SaintScore>=cutoff, select = c(NSAF,SpecSum, FoldChange, SaintScore, Bait, PreyGene))
|
|
192 p <- qplot(x=SaintScore, y=log2(FoldChange), data=a, colour=I(color),size=SpecSum) +
|
|
193 scale_size(range=c(1,10)) + ggtitle("Filtered on SAINT score") +
|
|
194 geom_point(aes(x=SaintScore,y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a)
|
|
195 if(label==TRUE & length(a$NSAF!=0)) {
|
|
196 p <- p + geom_text(data=a, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black", show_guide=FALSE)
|
|
197 }
|
|
198 if(length(levels(data$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
199 return(ggsave(p, width=8,height=4,filename = "bubble_zoom_SAINT.png"))
|
|
200 }
|
|
201 }
|
|
202 ###################################################################################################
|
|
203 # Filter proteins on Saintscore cutoff and plot for each bait x=log(NSAF), y=Log2(FoldChange)
|
|
204 ###################################################################################################
|
|
205 bubble_zoom_NSAF <- function(data, color, label=FALSE, cutoff=0.8) {
|
|
206 if(color=="crapome") {
|
|
207 a <- subset(data, CrapomePCT <80 & SaintScore>=cutoff, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait, PreyGene))
|
|
208 b <- subset(data, CrapomePCT >=80 & SaintScore >=cutoff, select = c(NSAF,SpecSum, CrapomePCT, FoldChange, SaintScore, Bait, PreyGene))
|
|
209 p <- qplot(x=log(NSAF), y=log2(FoldChange), data=a, colour=I("tan"),size=SpecSum) +
|
|
210 scale_size(range=c(1,10)) + ggtitle("Filtered on SAINT score") +
|
|
211 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a)
|
|
212 if(label==TRUE & length(a$NSAF!=0)) {
|
|
213 p <- p + geom_text(data=a, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black")
|
|
214 }
|
|
215 if(length(levels(a$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
216 p <- p + geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum, color=CrapomePCT), data=b) +
|
|
217 scale_colour_gradient(limits=c(80, 100), low="tan", high="red") +
|
|
218 labs(colour="CRAPome Probability \nof Specific Interaction (%)", x="ln(NSAF)") +
|
|
219 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=b)
|
|
220 if(label==TRUE & length(b$NSAF!=0)) {
|
|
221 p <- p + geom_text(data=b, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black", show_guide=FALSE)
|
|
222 }
|
|
223 return(ggsave(p, width=8,height=4,filename = "bubble_zoom_NSAF.png"))
|
|
224 }
|
|
225 if(color != "crapome") {
|
|
226 a <- subset(data, SaintScore>=cutoff, select = c(NSAF,SpecSum, FoldChange, SaintScore, Bait, PreyGene))
|
|
227 p <- qplot(x=log(NSAF), y=log2(FoldChange), data=a, colour=I(color), size=SpecSum) +
|
|
228 scale_size(range=c(1,10)) + ggtitle("Filtered on SAINT score") +
|
|
229 geom_point(aes(x=log(NSAF),y=log2(FoldChange), size=SpecSum), colour="black", shape=21, data=a) +
|
|
230 labs(x="ln(NSAF)")
|
|
231 if(label==TRUE & length(a$NSAF!=0)) {
|
|
232 p <- p + geom_text(data=a, aes(label=PreyGene, size=10, vjust=0, hjust=0),colour="black", show_guide=FALSE)
|
|
233 }
|
|
234 if(length(levels(data$Bait) > 1)) {p <- p + facet_wrap(~Bait, scales="free_y")}
|
|
235 return(ggsave(p, width=8,height=4,filename = "bubble_zoom_NSAF.png"))
|
|
236 }
|
|
237 }
|
|
238 ###################################################################################################
|
|
239 # Check Saintscore cutoff and stop program if not between 0 and 1
|
|
240 ###################################################################################################
|
|
241 cutoff_check <- function(cutoff){
|
|
242 if( any(cutoff < 0 | cutoff > 1) ) stop('SAINT score cutoff not between 0 and 1. Please correct and try again')
|
|
243 }
|
11
|
244
|
12
|
245
|
3
|
246
|
4
|
247 args <- commandArgs(trailingOnly = TRUE)
|
|
248 main(args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9])
|
3
|
249
|
|
250 #main("test_list.txt", "preytest.txt", crapome="craptest.txt", color="crapome", label=TRUE)
|
|
251 #main("Crizo_list.txt", "prey_cr.txt", crapome = "crizo_crap.txt", color="crapome", label=TRUE, cutoff=0.7)
|
|
252 #main("test_list.txt", "preytest.txt", crapome=FALSE, color="magenta", label=FALSE, cutoff=1.1) |