comparison all stuff/Difference finderMADE 7 TO 7 1-15-2019.R @ 0:23eea82f5192 draft

Uploaded
author jfb
date Wed, 16 Jan 2019 13:55:22 -0500
parents
children 110a74dff8e1
comparison
equal deleted inserted replaced
-1:000000000000 0:23eea82f5192
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<-"1R1 substrates.csv"
32 First_unshared_subbackfreq<-"1R1 SBF.csv"
33
34 Second_unshared_motifs_table<-"1R2 subs.csv"
35 Second_unshared_subbackfreq<-"1R2 SBf.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(FirstCentralLetters, grepl, pattern="S", ignore.case=TRUE)
58 SecondTees<-sapply(FirstCentralLetters, grepl, pattern="T", ignore.case=TRUE)
59 SecondWys<-sapply(FirstCentralLetters, 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,FirstEsses,"xS")
66 SecondCentralLetters<-replace(SecondCentralLetters,FirstTees,"xT")
67 SecondCentralLetters<-replace(SecondCentralLetters,FirstWys,"xY")
68
69 ####################################################################################################################################
70 ####################################################################################################################################
71 # better version of this code written in C: what happens when two kinases share a motif, but they found that motif in two
72 # separate proteins thus two separate accession numbers?
73 # It should actually output the shared motif and BOTH accession numbers. Right now it does not, it only maps out the second
74 # accession number. So that needs to be fixed BUT you need to keep the commonality between a motif and its accession number
75 ####################################################################################################################################
76 ####################################################################################################################################
77 ####################################################################################################################################
78 ####################################################################################################################################
79
80 #Create the motif sets, deciding wether or not you're looking for truncated or full here
81 #full only
82
83
84 if (Are_You_Looking_For_Commonality=="NO"){
85
86 ###############################################
87 #ALL motifs, full and truncated
88
89 if (FullMotifsOnly_questionmark!="YES"&&TruncatedMotifsOnly_questionmark!="YES"){
90 FTLwtmotifs=matrix(,nrow = nrow(FirstSubstrateSet),ncol=1)
91 FTLwtAccessionNumbers=matrix(,nrow = nrow(FirstSubstrateSet),ncol=1)
92
93 for (i in 1:nrow(FirstSubstrateSet)){
94 FTLwtletters<-FirstSubstrateSet[i,4:18]
95 FTLwtletters<-FTLwtletters[FTLwtletters !="XXXXX"]
96 FTLwtletters<-paste(FTLwtletters, sep="", collapse="")
97 leftspaces<-c()
98 rightspaces<-c()
99
100 YYYmotif <- unlist(strsplit(FTLwtletters, split = ""))
101 YYYposition <- match(x = "x", table = YYYmotif)
102 #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
103 #just 3 letters to the left of x
104
105 YYYLettersToTheLeft <- YYYposition - 1
106 #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
107 #just 1 spot to the right of Y so LettersToTheRight<-1 because 5-3-1=1
108 YYYLettersToTheRight <- length(YYYmotif) - YYYposition - 1
109 #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
110 #variable the user puts in is
111
112
113 if (YYYLettersToTheLeft < 7 | YYYLettersToTheRight < 7) {
114 leftspaces<-rep(" ",times=(7-YYYLettersToTheLeft))
115 rightspaces<-rep(" ",times=7-(YYYLettersToTheRight))
116 #add blank spaces if the motif has less than 4 letters to the left/right
117 motif<-c(leftspaces,YYYmotif,rightspaces)
118 #save that motif, which is the Y and +/- 4 amino acids, including truncation
119 motif<-motif[!motif %in% "x"]
120 motif<-paste(motif, sep="", collapse="")
121 FTLwtletters<-motif
122 FTLwtmotifs[i,1]<-FTLwtletters
123 FTLwtAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
124 }
125
126 if(YYYLettersToTheLeft>6 && YYYLettersToTheRight>6){
127 motif<-YYYmotif
128 #add blank spaces if the motif has less than 4 letters to the left/right
129 motif<-c(leftspaces,YYYmotif,rightspaces)
130 #save that motif, which is the Y and +/- 4 amino acids, including truncation
131 motif<-motif[!motif %in% "x"]
132 motif<-paste(motif, sep="", collapse="")
133 FTLwtletters<-motif
134 FTLwtmotifs[i,1]<-FTLwtletters
135 FTLwtAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
136
137
138 }
139
140 }
141
142 D835Ymotifs=matrix(,nrow = nrow(SecondSubstrateSet),ncol=1)
143 D835YAccessionNumbers<-matrix(,nrow = nrow(SecondSubstrateSet),ncol = 1)
144
145 for (i in 1:nrow(SecondSubstrateSet)){
146 D835letters<-SecondSubstrateSet[i,4:18]
147 D835letters<-D835letters[D835letters !="XXXXX"]
148 D835letters<-paste(D835letters, sep="", collapse="")
149 leftspaces<-c()
150 rightspaces<-c()
151
152 YYYmotif <- unlist(strsplit(D835letters, split = ""))
153 YYYposition <- match(x = "x", table = YYYmotif)
154 #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
155 #just 3 letters to the left of x
156
157 YYYLettersToTheLeft <- YYYposition - 1
158 #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
159 #just 1 spot to the right of Y so LettersToTheRight<-1 because 5-3-1=1
160 YYYLettersToTheRight <- length(YYYmotif) - YYYposition - 1
161 #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
162 #variable the user puts in is
163 if (YYYLettersToTheLeft < 7 | YYYLettersToTheRight < 7) {
164 leftspaces<-rep(" ",times=(7-YYYLettersToTheLeft))
165 rightspaces<-rep(" ",times=7-(YYYLettersToTheRight))
166 #add blank spaces if the motif has less than 4 letters to the left/right
167 motif<-c(leftspaces,YYYmotif,rightspaces)
168 #save that motif, which is the Y and +/- 4 amino acids, including truncation
169 motif<-motif[!motif %in% "x"]
170 motif<-paste(motif, sep="", collapse="")
171 D835letters<-motif
172 D835Ymotifs[i,1]<-D835letters
173 D835YAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
174 }
175
176 if(YYYLettersToTheLeft>6 && YYYLettersToTheRight>6){
177 motif<-YYYmotif
178 #add blank spaces if the motif has less than 4 letters to the left/right
179 motif<-c(leftspaces,YYYmotif,rightspaces)
180 #save that motif, which is the Y and +/- 4 amino acids, including truncation
181 motif<-motif[!motif %in% "x"]
182 motif<-paste(motif, sep="", collapse="")
183 D835letters<-motif
184 D835Ymotifs[i,1]<-D835letters
185 D835YAccessionNumbers[i,1]<-FirstSubstrateSet[i,3]
186 }
187 }
188
189 names(FTLwtmotifs)<-FTLwtAccessionNumbers
190 names(D835Ymotifs)<-D835YAccessionNumbers
191
192 }
193
194
195 FTLwtmotifsFINAL<-FTLwtmotifs[!FTLwtmotifs %in% D835Ymotifs]
196 FTLwtmotifsFINAL<-FTLwtmotifsFINAL[!duplicated(FTLwtmotifsFINAL)]
197
198 D835YmotifsFINAL<-D835Ymotifs[!D835Ymotifs %in% FTLwtmotifs]
199 D835YmotifsFINAL<-D835YmotifsFINAL[!duplicated(D835YmotifsFINAL)]
200
201
202 columnalheader<-c(rep(NA,36))
203 FTLFinalMatrix<-matrix(data =columnalheader,nrow = 1)
204
205 for (k in 1:length(FTLwtmotifsFINAL)) {
206 AN<-00000
207 #I don't remember why, but I felt it necessary to destroy the accession number multiple times to ensure it is
208 #destroyed immediately after use
209 for (m in 1:ncol(Firstsubbackfreq)) {
210 AN <- as.character(Firstsubbackfreq[1, m])
211 if (grepl(pattern = AN,
212 x = names(FTLwtmotifsFINAL[k]),
213 fixed = TRUE) == TRUE) {
214 outputmatrix <- as.character(Firstsubbackfreq[, m])
215 outputmatrix <- matrix(outputmatrix, nrow = 1)
216 #with that accession number, find a match in the subbackfreq file and save it here
217 FTLFinalMatrix<-rbind(FTLFinalMatrix,outputmatrix)
218 }
219 }
220 }
221 FTLFinalMatrix<-FTLFinalMatrix[!duplicated(FTLFinalMatrix),]
222 FTLFinalMatrix<-FTLFinalMatrix[2:nrow(FTLFinalMatrix),]
223
224 columnalheader<-c(rep(NA,36))
225 D835YFinalMatrix<-matrix(data =columnalheader,nrow = 1)
226
227 for (k in 1:length(D835YmotifsFINAL)) {
228 #I don't remember why, but I felt it necessary to destroy the accession number multiple times to ensure it is
229 #destroyed immediately after use
230 for (m in 1:ncol(Secondsubbackfreq)) {
231 AN <- as.character(Secondsubbackfreq[1, m])
232 if (grepl(pattern = AN,
233 x = names(D835YmotifsFINAL[k]),
234 fixed = TRUE) == TRUE) {
235 outputmatrix <- as.character(Secondsubbackfreq[, m])
236 outputmatrix <- matrix(outputmatrix, nrow = 1)
237 #with that accession number, find a match in the subbackfreq file and save it here
238 D835YFinalMatrix<-rbind(D835YFinalMatrix,outputmatrix)
239 }
240 }
241 }
242 D835YFinalMatrix<-D835YFinalMatrix[!duplicated(D835YFinalMatrix),]
243 D835YFinalMatrix<-D835YFinalMatrix[2:nrow(D835YFinalMatrix),]
244
245 FTLoutputmatrix<-matrix(data=c(FTLwtmotifsFINAL,names(FTLwtmotifsFINAL)),ncol = 2)
246
247
248 FLTheader<-c("Substrate","Species","Reference","-7","-6","-5","-4","-3","-2","-1","0","1","2","3","4","5","6","7","Phosphite")
249 # FLTheader<-unlist(FLTheader)
250 lefthandFLT<-matrix(data = rep(NA,times=2*nrow(FTLoutputmatrix)),nrow=nrow(FTLoutputmatrix))
251 righthandFLT<-matrix(data = rep(NA,times=1*nrow(FTLoutputmatrix)),nrow=nrow(FTLoutputmatrix))
252 FLTaccessionset<-FTLoutputmatrix[,2]
253 FTLmeat<-sapply(FTLoutputmatrix[,1], strsplit, "")
254 FTLmeat<-sapply(FTLmeat, unlist)
255 colnames(FTLmeat)<-NULL
256 FTLmeat<-t(FTLmeat)
257
258 FTLoutputmatrix2<-cbind(lefthandFLT,FLTaccessionset,FTLmeat,righthandFLT)
259 colnames(FTLoutputmatrix2)<-NULL
260 rownames(FTLoutputmatrix2)<-NULL
261 colnames(FLTheader)<-NULL
262 rownames(FLTheader)<-NULL
263
264
265 FirstCentralLettersAGAIN<-FTLoutputmatrix2[,11]
266
267 FirstEsses<-sapply(FirstCentralLettersAGAIN, grepl, pattern="S", ignore.case=TRUE)
268 FirstTees<-sapply(FirstCentralLettersAGAIN, grepl, pattern="T", ignore.case=TRUE)
269 FirstWys<-sapply(FirstCentralLettersAGAIN, grepl, pattern="Y", ignore.case=TRUE)
270
271 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstEsses,"xS")
272 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstTees,"xT")
273 FirstCentralLettersAGAIN<-replace(FirstCentralLettersAGAIN,FirstWys,"xY")
274
275 FirstCentralLettersAGAIN->FTLoutputmatrix2[,11]
276
277 FTLoutputmatrix2<-rbind(FLTheader,FTLoutputmatrix2)
278
279 write.table(x=FTLoutputmatrix2,
280 file=First_unshared_motifs_table,
281 quote=FALSE, sep=",",
282 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
283
284 columnalheader<-c("Accession Numbers",as.character(Firstsubbackfreq[1:35,1]))
285 columnalheader<-matrix(columnalheader,nrow = 1)
286 write.table(x=columnalheader,
287 file=First_unshared_subbackfreq,
288 quote=FALSE, sep=",",
289 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
290
291 write.table(x=FTLFinalMatrix,
292 file=First_unshared_subbackfreq,
293 quote=FALSE, sep=",",
294 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
295
296 ############################################################################################################
297
298 D835Youtputmatrix<-matrix(data=c(D835YmotifsFINAL,names(D835YmotifsFINAL)),ncol = 2)
299
300 D835Yheader<-c("Substrate","Species","Reference","-7","-6","-5","-4","-3","-2","-1","0","1","2","3","4","5","6","7","Phosphite")
301 # D835Yheader<-unlist(D835Yheader)
302 lefthandD835<-matrix(data = rep(NA,times=2*nrow(D835Youtputmatrix)),nrow=nrow(D835Youtputmatrix))
303 righthandD835<-matrix(data = rep(NA,times=1*nrow(D835Youtputmatrix)),nrow=nrow(D835Youtputmatrix))
304 D835Yaset<-D835Youtputmatrix[,2]
305 D835meat<-sapply(D835Youtputmatrix[,1], strsplit, "")
306 D835meat<-sapply(D835meat, unlist)
307 colnames(D835meat)<-NULL
308 D835meat<-t(D835meat)
309
310 D835Youtputmatrix2<-cbind(lefthandD835,D835Yaset,D835meat,righthandD835)
311 colnames(D835Youtputmatrix2)<-NULL
312 rownames(D835Youtputmatrix2)<-NULL
313 colnames(D835Yheader)<-NULL
314 rownames(D835Yheader)<-NULL
315
316
317 SecondCentralLettersAGAIN<-D835Youtputmatrix2[,11]
318
319 SecondEsses<-sapply(SecondCentralLettersAGAIN, grepl, pattern="S", ignore.case=TRUE)
320 SecondTees<-sapply(SecondCentralLettersAGAIN, grepl, pattern="T", ignore.case=TRUE)
321 SecondWys<-sapply(SecondCentralLettersAGAIN, grepl, pattern="Y", ignore.case=TRUE)
322
323 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,FirstEsses,"xS")
324 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,FirstTees,"xT")
325 SecondCentralLettersAGAIN<-replace(SecondCentralLettersAGAIN,FirstWys,"xY")
326
327 SecondCentralLettersAGAIN->D835Youtputmatrix2[,11]
328
329 D835Youtputmatrix2<-rbind(D835Yheader,D835Youtputmatrix2)
330
331 write.table(x=D835Youtputmatrix2,
332 file=Second_unshared_motifs_table,
333 quote=FALSE, sep=",",
334 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
335
336 columnalheader<-c("Accession Numbers",as.character(Firstsubbackfreq[1:35,1]))
337 columnalheader<-matrix(columnalheader,nrow = 1)
338 write.table(x=columnalheader,
339 file=Second_unshared_subbackfreq,
340 quote=FALSE, sep=",",
341 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
342
343 write.table(x=D835YFinalMatrix,
344 file=Second_unshared_subbackfreq,
345 quote=FALSE, sep=",",
346 row.names=FALSE,col.names = FALSE, na="", append=TRUE)
347 }
348