annotate NMF/NMF.R @ 5:5edbfbeba354 draft default tip

Uploaded
author jfb
date Tue, 14 Jul 2020 20:01:02 -0400
parents a098e1274f63
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
1 NAMEOFOUTPUTFILE<-"output1.csv"
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
2
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
3 SuperAwesometrial <- read.delim2("input1.tabular", header=FALSE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
4 #once you've used the other script to turn the FASFA into a CSV, copypaste the filepath and name
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
5 #of the csv into this line between the quote marks.
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
6
1
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
7 SBF<-read.csv("input3.csv", stringsAsFactors = FALSE, header = FALSE)
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
8 SBF<-t(SBF)
0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
9
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
10 PositiveMotifs <- read.csv("input2.csv", stringsAsFactors=FALSE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
11 #because of R reasons, it is required that the motifs in this file have blank cells instead of spaces where there is no letter in
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
12 #the motif
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
13
1
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
14 YsToim<-rep("xY",times=nrow(PositiveMotifs))
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
15 PositiveMotifs[,11]<-YsToim
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
16
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
17
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
18
0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
19 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
20 #I have to paste them, then split and unlist them, then find the x and paste again
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
21 Positive9Letters<-PositiveMotifs[,4:18]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
22 #head(Positive9Letters)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
23 PositiveTrueMotifs<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
24
1
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
25 AccessionNumbers<-as.character(SBF[2:nrow(SBF),1])
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
26 AccessionNumbers<-AccessionNumbers[!is.na(AccessionNumbers)]
0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
27 ALLPOSSIBLE<-SuperAwesometrial[,1]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
28 ALLPOSSIBLE<-as.character(ALLPOSSIBLE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
29 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
30
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
31 for (q in 1:nrow(Positive9Letters)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
32 LeftJust<-0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
33 RightJust<-0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
34
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
35 motifmotif<-Positive9Letters[q,]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
36 motifmotif<-paste(motifmotif, collapse = "",sep = "")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
37
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
38 motifmotif<-unlist(strsplit(motifmotif, split = ""))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
39
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
40 position <- match(x = "x", table = motifmotif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
41 LeftJust<-position-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
42 RightJust<-length(motifmotif)-position-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
43
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
44 LeftSpaces<-rep(x=" ", times=(7-LeftJust))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
45 RightSpaces<-rep(x=" ", times=(7-RightJust))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
46
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
47 motifmotif<-motifmotif[!motifmotif %in% c("x")]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
48
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
49 motifmotif<-c(LeftSpaces,motifmotif,RightSpaces)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
50 motifmotif<-paste(motifmotif, collapse = "",sep = "")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
51 PositiveTrueMotifs<-c(PositiveTrueMotifs,motifmotif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
52 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
53
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
54
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
55 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
56 allmotifs<-matrix(data=c("Motifs"),nrow=1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
57 thenames<-matrix(data=c("AccessionNumbers"),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
58 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
59
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
60 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
61
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
62 #TrueMotifNums<-which(ALLPOSSIBLE %in% AccessionNumbers)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
63 #fihlodeANs<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
64 for (q in 1:length(AccessionNumbers)) {
1
a098e1274f63 Uploaded
jfb
parents: 0
diff changeset
65 patterno<-as.character(AccessionNumbers[q])
0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
66 location<-sapply(ALLPOSSIBLE, grepl, pattern=patterno, fixed=TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
67 if (sum(location)>0){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
68 whereisit<-which(location %in% TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
69 for (u in 1:length(whereisit)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
70 i<-whereisit[u]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
71 name<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
72 data<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
73 name<-as.character(SuperAwesometrial[i,1])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
74 #the name of each protein is the first column
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
75 name<-sub(x=name, pattern=",", replacement="")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
76 #the names may contain commas, remove them
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
77 data<-as.character(SuperAwesometrial[i,3])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
78 #the amino acids are stored in the third column
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
79 data<-strsplit(data,"")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
80 #split them into their component letters
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
81 data<-unlist(data)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
82 #turn them into a vector
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
83 motif<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
84 for (j in 1:length(data)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
85 if ("Y" %in% data[j]){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
86 #if there is a Y aka Tyrosine in the data
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
87 #allmotifs=rbind(allmotifs,data[(i-4):(i+4)])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
88 a<-j-7
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
89 if (a<1){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
90 a<-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
91 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
92 b<-j+7
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
93 if (b>length(data)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
94 b<-length(data)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
95 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
96 #take the motif that is +/- 4 from that Y, sanity checks so that values are never off the grid from the protein
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
97
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
98 LeftSide<-7-(j-a)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
99 RightSide<-7-(b-j)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
100 #how is the motif justified? Does it have exactly 4 letters to the left/right, or does it not?
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
101
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
102 leftspaces<-rep(" ",times=LeftSide)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
103 rightspaces<-rep(" ",times=RightSide)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
104 #add blank spaces if the motif has less than 4 letters to the left/right
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
105
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
106
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
107 motif<-(data[(a):(b)])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
108 motif<-c(leftspaces,motif,rightspaces)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
109 #save that motif, which is the Y and +/- 4 amino acids, including truncation
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
110
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
111 # lens<-c(lens,length(motif))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
112 # leni<-c(leni,i)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
113 # lenj<-c(lenj,j)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
114
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
115 motif<-paste(motif, sep="", collapse="")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
116 #the 4 amino acids, put them back together into a single string
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
117 motif<-matrix(data=c(motif),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
118 namesss<-matrix(data=c(name),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
119 #keep this motif and separately keep the name of the protein it came from
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
120 allmotifs<-rbind(allmotifs,motif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
121 thenames<-rbind(thenames,namesss)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
122 #add names and motifs to a growing list
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
123
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
124 # write.table(motif, file="TRIALTIALRIAALSKFDJSD.csv", quote=FALSE, sep=",",
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
125 # row.names=FALSE,col.names = FALSE, na="", append=TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
126 #and then write it into a csv, the sep is needed so that the two pieces of the data frame are separated
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
127 #append has 1to equal true because this thing will loop around many times adding more and more data points
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
128 #you must create a new filename/filepath with each new data you run
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
129 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
130 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
131 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
132 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
133 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
134
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
135
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
136
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
137
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
138 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
139 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
140 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
141
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
142
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
143 # for (i in 1:nrow(SuperAwesometrial)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
144 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
145 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
146
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
147 names(allmotifs)<-thenames
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
148
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
149 truemotifs<-allmotifs[!duplicated(allmotifs)]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
150 #truenames<-thenames[!duplicated(thenames)]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
151 #remove duplicates from the motifs and names
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
152
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
153 #make the motifs and names into matrices
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
154
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
155 # for (w in 1:nrow(truemotifs)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
156 # for (e in 1:length(PositiveTrueMotifs)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
157 # if (grepl(pattern=PositiveTrueMotifs[e], x=truemotifs[w,1],ignore.case = TRUE)==TRUE){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
158 # truemotifs[w,1]<-NA
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
159 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
160 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
161 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
162
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
163 truemotifs<-truemotifs[!truemotifs %in% PositiveTrueMotifs]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
164
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
165 # truemotifs<-matrix(data = truemotifs,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
166 # truenames<-matrix(data=truenames,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
167 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
168 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
169 # #program only works if there are more motifs than names, fuck it
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
170 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
171 # rowsrows<-nrow(truemotifs)-nrow(truenames)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
172 # nanas<-rep(NA,times=rowsrows)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
173 # nanas<-matrix(data = nanas,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
174 # truenames<-rbind(truenames,nanas)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
175 # #to turn the motifs and names into a single output matrix, add enough rows of NAs so the two initial matrices are equivalent,
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
176 # #then put them together columnwise
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
177
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
178 outputfile<-cbind(names(truemotifs),truemotifs)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
179
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
180 outputfile <- gsub(",","",outputfile)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
181
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
182 write.table(outputfile, file=NAMEOFOUTPUTFILE, quote=FALSE, sep=",",
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
183 row.names=FALSE,col.names = FALSE, na="", append=TRUE)