annotate NMF/NMF-working-7-4-2020.R @ 5:5edbfbeba354 draft default tip

Uploaded
author jfb
date Tue, 14 Jul 2020 20:01:02 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
1 NAMEOFOUTPUTFILE<-"output1.csv"
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
2
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
3 SuperAwesometrial <- read.delim2("input1.tabular", header=FALSE)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
4 SBF<-read.csv("input3.csv", stringsAsFactors = FALSE, header = FALSE)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
5 SBF<-t(SBF)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
6 PositiveMotifs <- read.csv("input2.csv", stringsAsFactors=FALSE)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
7
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
8
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
9 YsToim<-rep("xY",times=nrow(PositiveMotifs))
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
10 PositiveMotifs[,11]<-YsToim
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
11
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
12
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
13
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
14 #this code is meant to take a list of proteins, and our list of phosphopeptides, and find which Y-containing peptides could have been found phosphorylated but weren't
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
15
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
16
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
17
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
18 #first then I create the list of phosphopeptides
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
19 Positive9Letters<-PositiveMotifs[,4:18]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
20 PositiveTrueMotifs<-c()
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
21
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
22 #then I take the proteins
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
23 AccessionNumbers<-as.character(SBF[2:nrow(SBF),1])
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
24 AccessionNumbers<-AccessionNumbers[!is.na(AccessionNumbers)]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
25 #the above is only those proteins from which our phosphopeptides sprung, the below is every protein in the human proteome
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
26 ALLPOSSIBLE<-SuperAwesometrial[,1]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
27 ALLPOSSIBLE<-as.character(ALLPOSSIBLE)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
28
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
29 for (q in 1:nrow(Positive9Letters)) {
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
30 LeftJust<-0
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
31 RightJust<-0
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
32
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
33
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
34 motifmotif<-Positive9Letters[q,]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
35 motifmotif<-paste(motifmotif, collapse = "",sep = "")
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
36 motifmotif<-unlist(strsplit(motifmotif, split = ""))
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
37 position <- match(x = "x", table = motifmotif)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
38 LeftJust<-position-1
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
39 RightJust<-length(motifmotif)-position-1
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
40 #find which position was the phospho-amino acid, it is marked with an X
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
41
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
42 LeftSpaces<-rep(x=" ", times=(7-LeftJust))
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
43 RightSpaces<-rep(x=" ", times=(7-RightJust))
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
44 motifmotif<-motifmotif[!motifmotif %in% c("x")]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
45 motifmotif<-c(LeftSpaces,motifmotif,RightSpaces)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
46 motifmotif<-paste(motifmotif, collapse = "",sep = "")
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
47 #put spaces on either side of the motif if the motif does not fill out a -7 to +7 motif
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
48
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
49 PositiveTrueMotifs<-c(PositiveTrueMotifs,motifmotif)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
50 }
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
51
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
52
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
53
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
54 allmotifs<-matrix(data=rep("Motifs", times= 1000000),ncol = 1)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
55 thenames<-matrix(data=rep("AccessionNumbers", times= 1000000),ncol = 1)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
56 #I preallocate vectors for efficiency, but I have no way of knowing how big these particular vectors need to be, so I just make them way bigger
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
57 #than I know they need to be. A vector 1 million long is plenty big
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
58
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
59 MotifNumber<-2
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
60
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
61 locations<-unique(grep(paste(AccessionNumbers,collapse="|"), ALLPOSSIBLE))
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
62
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
63
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
64 if (sum(locations)>0){
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
65 whereisit<-locations
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
66 for (u in 1:length(whereisit)) {
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
67 i<-whereisit[u]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
68 name<-c()
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
69 data<-c()
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
70 name<-as.character(SuperAwesometrial[i,1])
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
71 #the name of each protein is the first column
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
72 name<-sub(x=name, pattern=",", replacement="")
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
73 #the names may contain commas, remove them
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
74 data<-as.character(SuperAwesometrial[i,3])
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
75 #the amino acids are stored in the third column
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
76 data<-strsplit(data,"")
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
77 #split them into their component letters
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
78 data<-unlist(data)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
79 #turn them into a vector
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
80 motif<-c()
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
81
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
82 #this part below is where I can speed things up
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
83 The_Ys<-data=="Y"
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
84 #find any Y in the protein
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
85 if (sum(The_Ys>0)){ #if there is at least one Y
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
86 Where_are_they<-which(The_Ys %in% TRUE)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
87 for (z in 1:length(Where_are_they)) { #then for every Y, make a motif
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
88
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
89 j<-Where_are_they[z]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
90 a <- j-7
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
91 a<-ifelse(a<1, a <- 1, a <- a)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
92 b<-j+7
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
93 b<-ifelse(b>length(data), b <- length(data), b <-
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
94 b)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
95 #take the motif that is +/- 4 from that Y, sanity checks so that values are never off the grid from the protein
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
96
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
97 LeftSide<-7-(j-a)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
98 RightSide<-7-(b-j)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
99 #how is the motif justified? Does it have exactly 4 letters to the left/right, or does it not?
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
100
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
101 leftspaces<-rep(" ",times=LeftSide)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
102 rightspaces<-rep(" ",times=RightSide)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
103 #add blank spaces if the motif has less than 4 letters to the left/right
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
104
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
105
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
106 motif<-(data[(a):(b)])
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
107 motif<-c(leftspaces,motif,rightspaces)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
108 #save that motif, which is the Y and +/- 4 amino acids, including truncation
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
109
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
110 motif<-paste(motif, sep="", collapse="")
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
111 #the 4 amino acids, put them back together into a single string
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
112 motif<-matrix(data=c(motif),nrow = 1)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
113 namesss<-matrix(data=c(name),nrow = 1)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
114 #keep this motif and separately keep the name of the protein it came from
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
115
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
116 allmotifs[MotifNumber,1]<-motif
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
117 thenames[MotifNumber,1]<-namesss
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
118 MotifNumber<-MotifNumber+1
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
119
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
120 }
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
121
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
122 }
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
123 }
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
124 }
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
125
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
126
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
127
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
128
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
129 names(allmotifs)<-thenames
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
130
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
131 truemotifs<-allmotifs[!duplicated(allmotifs)]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
132 #remove duplicates from the motifs and names
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
133
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
134 #make the motifs and names into matrices
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
135 truemotifs<-truemotifs[!truemotifs %in% PositiveTrueMotifs]
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
136 outputfile<-cbind(names(truemotifs),truemotifs)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
137 outputfile <- gsub(",","",outputfile)
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
138 write.table(outputfile, file=NAMEOFOUTPUTFILE, quote=FALSE, sep=",",
5edbfbeba354 Uploaded
jfb
parents:
diff changeset
139 row.names=FALSE,col.names = FALSE, na="", append=TRUE)