annotate NMF/NMF.R @ 0:dd301fc4b54e draft

Uploaded
author jfb
date Fri, 23 Feb 2018 16:37:29 -0500
parents
children a098e1274f63
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 #this is the name of the file you will create
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
3
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
4
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
5
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
6 SuperAwesometrial <- read.delim2("input1.tabular", header=FALSE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
7 #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
8 #of the csv into this line between the quote marks.
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
9
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
10 SBF<-read.csv("input3.csv", stringsAsFactors = FALSE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
11
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
12
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
13 PositiveMotifs <- read.csv("input2.csv", stringsAsFactors=FALSE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
14 #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
15 #the motif
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
16
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
17 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
18 #I have to paste them, then split and unlist them, then find the x and paste again
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
19 Positive9Letters<-PositiveMotifs[,4:18]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
20 #head(Positive9Letters)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
21 PositiveTrueMotifs<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
22
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
23 AccessionNumbers<-SBF[,1]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
24 ALLPOSSIBLE<-SuperAwesometrial[,1]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
25 ALLPOSSIBLE<-as.character(ALLPOSSIBLE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
26 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
27
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
28 for (q in 1:nrow(Positive9Letters)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
29 LeftJust<-0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
30 RightJust<-0
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
31
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
32 motifmotif<-Positive9Letters[q,]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
33 motifmotif<-paste(motifmotif, collapse = "",sep = "")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
34
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
35 motifmotif<-unlist(strsplit(motifmotif, split = ""))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
36
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
37 position <- match(x = "x", table = motifmotif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
38 LeftJust<-position-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
39 RightJust<-length(motifmotif)-position-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
40
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
41 LeftSpaces<-rep(x=" ", times=(7-LeftJust))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
42 RightSpaces<-rep(x=" ", times=(7-RightJust))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
43
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
44 motifmotif<-motifmotif[!motifmotif %in% c("x")]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
45
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
46 motifmotif<-c(LeftSpaces,motifmotif,RightSpaces)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
47 motifmotif<-paste(motifmotif, collapse = "",sep = "")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
48 PositiveTrueMotifs<-c(PositiveTrueMotifs,motifmotif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
49 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
50
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
51
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
52 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
53 allmotifs<-matrix(data=c("Motifs"),nrow=1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
54 thenames<-matrix(data=c("AccessionNumbers"),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
55 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
56
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
57 ################################################################################################################################
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
58
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
59 #TrueMotifNums<-which(ALLPOSSIBLE %in% AccessionNumbers)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
60 #fihlodeANs<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
61 for (q in 1:length(AccessionNumbers)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
62 patterno<-AccessionNumbers[q]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
63 location<-sapply(ALLPOSSIBLE, grepl, pattern=patterno, fixed=TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
64 if (sum(location)>0){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
65 whereisit<-which(location %in% TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
66 for (u in 1:length(whereisit)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
67 i<-whereisit[u]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
68 name<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
69 data<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
70 name<-as.character(SuperAwesometrial[i,1])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
71 #the name of each protein is the first column
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
72 name<-sub(x=name, pattern=",", replacement="")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
73 #the names may contain commas, remove them
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
74 data<-as.character(SuperAwesometrial[i,3])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
75 #the amino acids are stored in the third column
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
76 data<-strsplit(data,"")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
77 #split them into their component letters
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
78 data<-unlist(data)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
79 #turn them into a vector
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
80 motif<-c()
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
81 for (j in 1:length(data)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
82 if ("Y" %in% data[j]){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
83 #if there is a Y aka Tyrosine in the data
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
84 #allmotifs=rbind(allmotifs,data[(i-4):(i+4)])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
85 a<-j-7
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
86 if (a<1){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
87 a<-1
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
88 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
89 b<-j+7
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
90 if (b>length(data)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
91 b<-length(data)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
92 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
93 #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
94
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
95 LeftSide<-7-(j-a)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
96 RightSide<-7-(b-j)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
97 #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
98
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
99 leftspaces<-rep(" ",times=LeftSide)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
100 rightspaces<-rep(" ",times=RightSide)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
101 #add blank spaces if the motif has less than 4 letters to the left/right
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
102
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
103
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
104 motif<-(data[(a):(b)])
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
105 motif<-c(leftspaces,motif,rightspaces)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
106 #save that motif, which is the Y and +/- 4 amino acids, including truncation
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
107
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
108 # lens<-c(lens,length(motif))
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
109 # leni<-c(leni,i)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
110 # lenj<-c(lenj,j)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
111
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
112 motif<-paste(motif, sep="", collapse="")
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
113 #the 4 amino acids, put them back together into a single string
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
114 motif<-matrix(data=c(motif),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
115 namesss<-matrix(data=c(name),nrow = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
116 #keep this motif and separately keep the name of the protein it came from
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
117 allmotifs<-rbind(allmotifs,motif)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
118 thenames<-rbind(thenames,namesss)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
119 #add names and motifs to a growing list
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
120
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
121 # write.table(motif, file="TRIALTIALRIAALSKFDJSD.csv", quote=FALSE, sep=",",
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
122 # row.names=FALSE,col.names = FALSE, na="", append=TRUE)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
123 #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
124 #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
125 #you must create a new filename/filepath with each new data you run
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
126 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
127 }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
128 }
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 # for (i in 1:nrow(SuperAwesometrial)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
141 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
142 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
143
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
144 names(allmotifs)<-thenames
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
145
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
146 truemotifs<-allmotifs[!duplicated(allmotifs)]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
147 #truenames<-thenames[!duplicated(thenames)]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
148 #remove duplicates from the motifs and names
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
149
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
150 #make the motifs and names into matrices
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
151
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
152 # for (w in 1:nrow(truemotifs)) {
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
153 # for (e in 1:length(PositiveTrueMotifs)){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
154 # if (grepl(pattern=PositiveTrueMotifs[e], x=truemotifs[w,1],ignore.case = TRUE)==TRUE){
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
155 # truemotifs[w,1]<-NA
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
156 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
157 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
158 # }
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
159
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
160 truemotifs<-truemotifs[!truemotifs %in% PositiveTrueMotifs]
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
161
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
162 # truemotifs<-matrix(data = truemotifs,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
163 # truenames<-matrix(data=truenames,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
164 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
165 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
166 # #program only works if there are more motifs than names, fuck it
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
167 #
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
168 # rowsrows<-nrow(truemotifs)-nrow(truenames)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
169 # nanas<-rep(NA,times=rowsrows)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
170 # nanas<-matrix(data = nanas,ncol = 1)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
171 # truenames<-rbind(truenames,nanas)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
172 # #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
173 # #then put them together columnwise
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
174
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
175 outputfile<-cbind(names(truemotifs),truemotifs)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
176
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
177 outputfile <- gsub(",","",outputfile)
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
178
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
179 write.table(outputfile, file=NAMEOFOUTPUTFILE, quote=FALSE, sep=",",
dd301fc4b54e Uploaded
jfb
parents:
diff changeset
180 row.names=FALSE,col.names = FALSE, na="", append=TRUE)