comparison all stuff/Difference finder for GalaxyP 4-18-2019.R @ 6:8fa6b79a2f19 draft

Uploaded
author jfb
date Fri, 19 Apr 2019 16:41:00 -0400
parents
children 4dd15c41d9e7
comparison
equal deleted inserted replaced
5:f375051b0fcd 6:8fa6b79a2f19
1 #I should make an SOP for this. Problems we encountered: no x in the xY motif, and the kilodemon
2 #the output files have both Y and xY, they shouldn't why is that happening? make it not happen
3 #make sure that accession numbers stay locked to each motif, somehow
4 #output should look just like the KALIP input
5
6 #ff you want ONLY FULL MOTIFS, put "YES" here, please use all caps
7 FullMotifsOnly_questionmark<-"NO"
8 #If you want ONLY TRUNCATED MOTIFS, put "YES" here, please use all caps
9 TruncatedMotifsOnly_questionmark<-"NO"
10 #if you want to find the overlap, put a "YES" here (all caps), if you want to find the non-overlap, put "NO" (all caps)
11 Are_You_Looking_For_Commonality<-"NO"
12
13
14 #put the names of your input files here
15 FirstSubstrateSet<- read.csv("S1.csv", stringsAsFactors=FALSE)
16 Firstsubbackfreq<- read.csv("SBF1.csv", header=FALSE, stringsAsFactors=FALSE)
17
18 SecondSubstrateSet<- read.csv("S2.csv", stringsAsFactors=FALSE)
19 Secondsubbackfreq<- read.csv("SBF2.csv", header=FALSE, stringsAsFactors=FALSE)
20
21 # ThirdSubstrateSet<- read.csv("Galaxy69-BTK_PLUS_R3_Substrates.csv", stringsAsFactors=FALSE)
22 # Thirdsubbackfreq<- read.csv("Galaxy70-BTK_PLUS_R3_SubstrateBackgroundFrequency.csv", header=FALSE, stringsAsFactors=FALSE)
23
24 #then put the names of your output files here
25 # Shared_motifs_table<-"180719_GALAXY-BTK-plus-rep-OVLP-7to7-substrates.csv"
26 # Shared_subbackfreq_table<-"180719_GALAXY-BTK-plus-rep-OVLP-7to7-SubBackFreq.csv"
27
28 # Shared_motifs_table<-"Shared motifs 7-27-17.csv"
29 # Shared_subbackfreq_table<-"SubstrateBackgrounFrequency-for-shared-motifs 4 7-27-17.csv"
30
31 First_unshared_motifs_table<-"1RS.csv"
32 First_unshared_subbackfreq<-"1RSBF.csv"
33
34 Second_unshared_motifs_table<-"2SR.csv"
35 Second_unshared_subbackfreq<-"2RSBF.csv"
36
37 # Third_unshared_motifs_table<-"R3 subs.csv"
38 # Third_unshared_subbackfreq<-"R3 SBF.csv"
39
40 #final note, this code is going to be unworkable if you want to make a Venn diagram of more than 3 circles. I think I'll poke around
41 #other languages to see if any of them can do it.
42 ####################################################################################################################################
43
44
45
46
47
48 # grepl(pattern = "S", x=asdf, ignore.case = TRUE)
49
50 FirstCentralLetters<-FirstSubstrateSet[,11]
51 SecondCentralLetters<-SecondSubstrateSet[,11]
52
53 FirstEsses<-sapply(FirstCentralLetters, grepl, pattern="S", ignore.case=TRUE)
54 FirstTees<-sapply(FirstCentralLetters, grepl, pattern="T", ignore.case=TRUE)
55 FirstWys<-sapply(FirstCentralLetters, grepl, pattern="Y", ignore.case=TRUE)
56
57 SecondEsses<-sapply(SecondCentralLetters, grepl, pattern="S", ignore.case=TRUE)
58 SecondTees<-sapply(SecondCentralLetters, grepl, pattern="T", ignore.case=TRUE)
59 SecondWys<-sapply(SecondCentralLetters, grepl, pattern="Y", ignore.case=TRUE)
60
61 FirstCentralLetters<-replace(FirstCentralLetters,FirstEsses,"xS")
62 FirstCentralLetters<-replace(FirstCentralLetters,FirstTees,"xT")
63 FirstCentralLetters<-replace(FirstCentralLetters,FirstWys,"xY")
64
65 SecondCentralLetters<-replace(SecondCentralLetters,SecondEsses,"xS")
66 SecondCentralLetters<-replace(SecondCentralLetters,SecondTees,"xT")
67 SecondCentralLetters<-replace(SecondCentralLetters,SecondWys,"xY")
68
69 FirstCentralLetters->FirstSubstrateSet[,11]
70 SecondCentralLetters->SecondSubstrateSet[,11]
71
72 ####################################################################################################################################
73 ####################################################################################################################################
74 # better version of this code written in C: what happens when two kinases share a motif, but they found that motif in two
75 # separate proteins thus two separate accession numbers?
76 # It should actually output the shared motif and BOTH accession numbers. Right now it does not, it only maps out the second
77 # accession number. So that needs to be fixed BUT you need to keep the commonality between a motif and its accession number
78 ####################################################################################################################################
79 ####################################################################################################################################
80 ####################################################################################################################################
81 ####################################################################################################################################
82
83 #Create the motif sets, deciding wether or not you're looking for truncated or full here
84 #full only
85
86
87 if (Are_You_Looking_For_Commonality=="NO"){
88
89 ###############################################
90 #ALL motifs, full and truncated
91
92 if (FullMotifsOnly_questionmark!="YES"&&TruncatedMotifsOnly_questionmark!="YES"){
93 FTLwtmotifs=matrix(,nrow = nrow(FirstSubstrateSet),ncol=1)
94 FTLwtAccessionNumbers=matrix(,nrow = nrow(FirstSubstrateSet),ncol=1)
95
96 for (i in 1:nrow(FirstSubstrateSet)){
97 FTLwtletters<-FirstSubstrateSet[i,4:18]
98 FTLwtletters<-FTLwtletters[FTLwtletters !="XXXXX"]
99 FTLwtletters<-paste(FTLwtletters, sep="", collapse="")
100 leftspaces<-c()
101 rightspaces<-c()
102
103 YYYmotif <- unlist(strsplit(FTLwtletters, split = ""))
104 YYYposition <- match(x = "x", table = YYYmotif)
105 #position itself tells me how much is to the left of that X by what it's number is. x at position 4 tells me that there are
106 #just 3 letters to the left of x
107
108 YYYLettersToTheLeft <- YYYposition - 1
109 #how many letters to the right SHOULD just be length(motif)-position-1 if it's 5 long and x is at 3 then Y is at 4 and there is
110 #just 1 spot to the right of Y so LettersToTheRight<-1 because 5-3-1=1
111 YYYLettersToTheRight <- length(YYYmotif) - YYYposition - 1
112 #then sanity check, we're currently looking only at +/-4, but this spot allows for up to +/- 7 as well, just depends on what the
113 #variable the user puts in is
114
115
116 if (YYYLettersToTheLeft < 7 | YYYLettersToTheRight < 7) {
117 leftspaces<-rep(" ",times=(7-YYYLettersToTheLeft))
118 rightspaces<-rep(" ",times=7-(YYYLettersToTheRight))
119 #add blank spaces if the motif has less than 4 letters to the left/right
120 motif<-c(leftspaces,YYYmotif,rightspaces)
121 #save that motif, which is the Y and +/- 4 amino acids, including truncation
122 motif<-motif[!motif %in% "x"]
123 motif<-paste(motif, sep="", collapse="")
124 FTLwtletters<-motif
125 FTLwtmotifs[i,1]<-FTLwtletters
126 FTLwtAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
127 }
128
129 if(YYYLettersToTheLeft>6 && YYYLettersToTheRight>6){
130 motif<-YYYmotif
131 #add blank spaces if the motif has less than 4 letters to the left/right
132 motif<-c(leftspaces,YYYmotif,rightspaces)
133 #save that motif, which is the Y and +/- 4 amino acids, including truncation
134 motif<-motif[!motif %in% "x"]
135 motif<-paste(motif, sep="", collapse="")
136 FTLwtletters<-motif
137 FTLwtmotifs[i,1]<-FTLwtletters
138 FTLwtAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
139
140
141 }
142
143 }
144
145 D835Ymotifs=matrix(,nrow = nrow(SecondSubstrateSet),ncol=1)
146 D835YAccessionNumbers<-matrix(,nrow = nrow(SecondSubstrateSet),ncol = 1)
147
148 for (i in 1:nrow(SecondSubstrateSet)){
149 D835letters<-SecondSubstrateSet[i,4:18]
150 D835letters<-D835letters[D835letters !="XXXXX"]
151 D835letters<-paste(D835letters, sep="", collapse="")
152 leftspaces<-c()
153 rightspaces<-c()
154
155 YYYmotif <- unlist(strsplit(D835letters, split = ""))
156 YYYposition <- match(x = "x", table = YYYmotif)
157 #position itself tells me how much is to the left of that X by what it's number is. x at position 4 tells me that there are
158 #just 3 letters to the left of x
159
160 YYYLettersToTheLeft <- YYYposition - 1
161 #how many letters to the right SHOULD just be length(motif)-position-1 if it's 5 long and x is at 3 then Y is at 4 and there is
162 #just 1 spot to the right of Y so LettersToTheRight<-1 because 5-3-1=1
163 YYYLettersToTheRight <- length(YYYmotif) - YYYposition - 1
164 #then sanity check, we're currently looking only at +/-4, but this spot allows for up to +/- 7 as well, just depends on what the
165 #variable the user puts in is
166 if (YYYLettersToTheLeft < 7 | YYYLettersToTheRight < 7) {
167 leftspaces<-rep(" ",times=(7-YYYLettersToTheLeft))
168 rightspaces<-rep(" ",times=7-(YYYLettersToTheRight))
169 #add blank spaces if the motif has less than 4 letters to the left/right
170 motif<-c(leftspaces,YYYmotif,rightspaces)
171 #save that motif, which is the Y and +/- 4 amino acids, including truncation
172 motif<-motif[!motif %in% "x"]
173 motif<-paste(motif, sep="", collapse="")
174 D835letters<-motif
175 D835Ymotifs[i,1]<-D835letters
176 D835YAccessionNumbers[i,1]<-SecondSubstrateSet[i,3]
177 }
178
179 if(YYYLettersToTheLeft>6 && YYYLettersToTheRight>6){
180 motif<-YYYmotif
181 #add blank spaces if the motif has less than 4 letters to the left/right
182 motif<-c(leftspaces,YYYmotif,rightspaces)
183 #save that motif, which is the Y and +/- 4 amino acids, including truncation
184 motif<-motif[!motif %in% "x"]
185 motif<-paste(motif, sep="", collapse="")
186 D835letters<-motif
187 D835Ymotifs[i,1]<-D835letters
188 D835YAccessionNumbers[i,1]<-SecondSubstrateSet[i,3]
189 }
190 }
191
192 names(FTLwtmotifs)<-FTLwtAccessionNumbers
193 names(D835Ymotifs)<-D835YAccessionNumbers
194
195 }
196
197
198 FTLwtmotifsFINAL<-FTLwtmotifs[!FTLwtmotifs %in% D835Ymotifs]
199 FTLwtmotifsFINAL<-FTLwtmotifsFINAL[!duplicated(FTLwtmotifsFINAL)]
200
201 D835YmotifsFINAL<-D835Ymotifs[!D835Ymotifs %in% FTLwtmotifs]
202 D835YmotifsFINAL<-D835YmotifsFINAL[!duplicated(D835YmotifsFINAL)]
203
204
205 columnalheader<-c(rep(NA,36))
206 FTLFinalMatrix<-matrix(data =columnalheader,nrow = 1)
207
208 for (k in 1:length(FTLwtmotifsFINAL)) {
209 AN<-00000
210 #I don't remember why, but I felt it necessary to destroy the accession number multiple times to ensure it is
211 #destroyed immediately after use
212 for (m in 1:ncol(Firstsubbackfreq)) {
213 AN <- as.character(Firstsubbackfreq[1, m])
214 if (grepl(pattern = AN,
215 x = names(FTLwtmotifsFINAL[k]),
216 fixed = TRUE) == TRUE) {
217 outputmatrix <- as.character(Firstsubbackfreq[, m])
218 outputmatrix <- matrix(outputmatrix, nrow = 1)
219 #with that accession number, find a match in the subbackfreq file and save it here
220 FTLFinalMatrix<-rbind(FTLFinalMatrix,outputmatrix)
221 }
222 }
223 }
224 FTLFinalMatrix<-FTLFinalMatrix[!duplicated(FTLFinalMatrix),]
225 FTLFinalMatrix<-FTLFinalMatrix[2:nrow(FTLFinalMatrix),]
226
227 columnalheader<-c(rep(NA,36))
228 D835YFinalMatrix<-matrix(data =columnalheader,nrow = 1)
229
230 for (k in 1:length(D835YmotifsFINAL)) {
231 #I don't remember why, but I felt it necessary to destroy the accession number multiple times to ensure it is
232 #destroyed immediately after use
233 for (m in 1:ncol(Secondsubbackfreq)) {
234 AN <- as.character(Secondsubbackfreq[1, m])
235 if (grepl(pattern = AN,
236 x = names(D835YmotifsFINAL[k]),
237 fixed = TRUE) == TRUE) {
238 outputmatrix <- as.character(Secondsubbackfreq[, m])
239 outputmatrix <- matrix(outputmatrix, nrow = 1)
240 #with that accession number, find a match in the subbackfreq file and save it here
241 D835YFinalMatrix<-rbind(D835YFinalMatrix,outputmatrix)
242 }
243 }
244 }
245 D835YFinalMatrix<-D835YFinalMatrix[!duplicated(D835YFinalMatrix),]
246 D835YFinalMatrix<-D835YFinalMatrix[2:nrow(D835YFinalMatrix),]
247
248 FTLoutputmatrix<-matrix(data=c(FTLwtmotifsFINAL,names(FTLwtmotifsFINAL)),ncol = 2)
249
250
251 FLTheader<-c("Substrate","Species","Reference","-7","-6","-5","-4","-3","-2","-1","0","1","2","3","4","5","6","7","Phosphite")
252 # FLTheader<-unlist(FLTheader)
253 lefthandFLT<-matrix(data = rep(NA,times=2*nrow(FTLoutputmatrix)),nrow=nrow(FTLoutputmatrix))
254 righthandFLT<-matrix(data = rep(NA,times=1*nrow(FTLoutputmatrix)),nrow=nrow(FTLoutputmatrix))
255 FLTaccessionset<-FTLoutputmatrix[,2]
256 FTLmeat<-sapply(FTLoutputmatrix[,1], strsplit, "")
257 FTLmeat<-sapply(FTLmeat, unlist)
258 colnames(FTLmeat)<-NULL
259 FTLmeat<-t(FTLmeat)
260
261 FTLoutputmatrix2<-cbind(lefthandFLT,FLTaccessionset,FTLmeat,righthandFLT)
262 colnames(FTLoutputmatrix2)<-NULL
263 rownames(FTLoutputmatrix2)<-NULL
264 colnames(FLTheader)<-NULL
265 rownames(FLTheader)<-NULL
266
267
268 FirstCentralLettersAGAIN<-FTLoutputmatrix2[,11]
269
270 FirstEsses<-sapply(FirstCentralLettersAGAIN, grepl, pattern="S", ignore.case=TRUE)
271 FirstTees<-sapply(FirstCentralLettersAGAIN, grepl, pattern="T", ignore.case=TRUE)
272 FirstWys<-sapply(FirstCentralLettersAGAIN, grepl, pattern="Y", ignore.case=TRUE)
273
274 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstEsses,"xS")
275 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstTees,"xT")
276 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstWys,"xY")
277
278 FirstCentralLettersAGAIN->FTLoutputmatrix2[,11]
279
280 FTLoutputmatrix2<-rbind(FLTheader,FTLoutputmatrix2)
281
282 write.table(x=FTLoutputmatrix2,
283 file=First_unshared_motifs_table,
284 quote=FALSE, sep=",",
285 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
286
287 columnalheader<-c("Accession Numbers",as.character(Firstsubbackfreq[1:35,1]))
288 columnalheader<-matrix(columnalheader,nrow = 1)
289 write.table(x=columnalheader,
290 file=First_unshared_subbackfreq,
291 quote=FALSE, sep=",",
292 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
293
294 write.table(x=FTLFinalMatrix,
295 file=First_unshared_subbackfreq,
296 quote=FALSE, sep=",",
297 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
298
299 ############################################################################################################
300
301 D835Youtputmatrix<-matrix(data=c(D835YmotifsFINAL,names(D835YmotifsFINAL)),ncol = 2)
302
303 D835Yheader<-c("Substrate","Species","Reference","-7","-6","-5","-4","-3","-2","-1","0","1","2","3","4","5","6","7","Phosphite")
304 # D835Yheader<-unlist(D835Yheader)
305 lefthandD835<-matrix(data = rep(NA,times=2*nrow(D835Youtputmatrix)),nrow=nrow(D835Youtputmatrix))
306 righthandD835<-matrix(data = rep(NA,times=1*nrow(D835Youtputmatrix)),nrow=nrow(D835Youtputmatrix))
307 D835Yaset<-D835Youtputmatrix[,2]
308 D835meat<-sapply(D835Youtputmatrix[,1], strsplit, "")
309 D835meat<-sapply(D835meat, unlist)
310 colnames(D835meat)<-NULL
311 D835meat<-t(D835meat)
312
313 D835Youtputmatrix2<-cbind(lefthandD835,D835Yaset,D835meat,righthandD835)
314 colnames(D835Youtputmatrix2)<-NULL
315 rownames(D835Youtputmatrix2)<-NULL
316 colnames(D835Yheader)<-NULL
317 rownames(D835Yheader)<-NULL
318
319
320 SecondCentralLettersAGAIN<-D835Youtputmatrix2[,11]
321
322 SecondEsses<-sapply(SecondCentralLettersAGAIN, grepl, pattern="S", ignore.case=TRUE)
323 SecondTees<-sapply(SecondCentralLettersAGAIN, grepl, pattern="T", ignore.case=TRUE)
324 SecondWys<-sapply(SecondCentralLettersAGAIN, grepl, pattern="Y", ignore.case=TRUE)
325
326 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,SecondEsses,"xS")
327 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,SecondTees,"xT")
328 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,SecondWys,"xY")
329
330 SecondCentralLettersAGAIN->D835Youtputmatrix2[,11]
331
332 D835Youtputmatrix2<-rbind(D835Yheader,D835Youtputmatrix2)
333
334 write.table(x=D835Youtputmatrix2,
335 file=Second_unshared_motifs_table,
336 quote=FALSE, sep=",",
337 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
338
339 columnalheader<-c("Accession Numbers",as.character(Firstsubbackfreq[1:35,1]))
340 columnalheader<-matrix(columnalheader,nrow = 1)
341 write.table(x=columnalheader,
342 file=Second_unshared_subbackfreq,
343 quote=FALSE, sep=",",
344 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
345
346 write.table(x=D835YFinalMatrix,
347 file=Second_unshared_subbackfreq,
348 quote=FALSE, sep=",",
349 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
350 }
351