Commit 891114fbe3c0e003d8419445048dccc70512afcd

Authored by Efrain Gonzalez
1 parent db3a0d69f1
Exists in master

Don't use this code yet

Update to cgeneID call of function
Showing 1 changed file with 2 additions and 2 deletions   Show diff stats
1 ######################################################################## 1 ########################################################################
2 # Don't Use This Code Just Yet # 2 # Don't Use This Code Just Yet #
3 ######################################################################## 3 ########################################################################
4 #Efrain H. Gonzalez 4 #Efrain H. Gonzalez
5 #6/16/2017 5 #6/16/2017
6 6
7 #Libraries required to run the code 7 #Libraries required to run the code
8 library(pryr) 8 library(pryr)
9 library(MASS) 9 library(MASS)
10 library(dplyr) 10 library(dplyr)
11 library(tidyr) 11 library(tidyr)
12 library(readr) 12 library(readr)
13 library(stringr) 13 library(stringr)
14 14
15 15
16 #Necessary Functions 16 #Necessary Functions
17 #1#Function for handling the changing of row names and column names 17 #1#Function for handling the changing of row names and column names
18 chngrownm <- function(mat){ 18 chngrownm <- function(mat){
19 row <- dim(mat)[1] 19 row <- dim(mat)[1]
20 col <- dim(mat)[2] 20 col <- dim(mat)[2]
21 e <- 1 21 e <- 1
22 r <- 1 22 r <- 1
23 a <- 1 23 a <- 1
24 h <- 1 24 h <- 1
25 g <- 1 25 g <- 1
26 o <- 1 26 o <- 1
27 for(e in 1:col){ 27 for(e in 1:col){
28 if("!Sample_source_name_ch1"==mat[1,e]){ 28 if("!Sample_source_name_ch1"==mat[1,e]){
29 colnames(mat)[e] <- "Brain_Region" 29 colnames(mat)[e] <- "Brain_Region"
30 } else if("!Sample_title" == mat[1,e]){ 30 } else if("!Sample_title" == mat[1,e]){
31 colnames(mat)[e] <- "Title" 31 colnames(mat)[e] <- "Title"
32 } else if("!Sample_geo_accession" == mat[1,e]){ 32 } else if("!Sample_geo_accession" == mat[1,e]){
33 colnames(mat)[e] <- "ID_REF" 33 colnames(mat)[e] <- "ID_REF"
34 } else{ 34 } else{
35 if(grepl("Sex|gender|Gender|sex",mat[2,e])==TRUE){ 35 if(grepl("Sex|gender|Gender|sex",mat[2,e])==TRUE){
36 colnames(mat)[e] <- paste0("Sex",r) 36 colnames(mat)[e] <- paste0("Sex",r)
37 r = r + 1 37 r = r + 1
38 } 38 }
39 if(grepl("postmorteminterval|PMI|pmi|interval",mat[2,e])==TRUE){ 39 if(grepl("postmorteminterval|PMI|pmi|interval",mat[2,e])==TRUE){
40 colnames(mat)[e] <- paste0("PMI",a) 40 colnames(mat)[e] <- paste0("PMI",a)
41 a = a + 1 41 a = a + 1
42 } 42 }
43 if(grepl("age|Age|AGE",mat[2,e])==TRUE){ 43 if(grepl("age|Age|AGE",mat[2,e])==TRUE){
44 colnames(mat)[e] <- paste0("Age",h) 44 colnames(mat)[e] <- paste0("Age",h)
45 h = h + 1 45 h = h + 1
46 } 46 }
47 if(grepl("braak|b&b",mat[2,e])==TRUE){ 47 if(grepl("braak|b&b",mat[2,e])==TRUE){
48 colnames(mat)[e] <- paste0("Braak",g) 48 colnames(mat)[e] <- paste0("Braak",g)
49 g = g + 1 49 g = g + 1
50 } 50 }
51 if(grepl("group|disease|control|AD|normal|diagnosis|Alzheimer|Control|Normal",mat[2,e])==TRUE){ 51 if(grepl("group|disease|control|AD|normal|diagnosis|Alzheimer|Control|Normal",mat[2,e])==TRUE){
52 colnames(mat)[e] <- paste0("Group",o) 52 colnames(mat)[e] <- paste0("Group",o)
53 o = o + 1 53 o = o + 1
54 } 54 }
55 55
56 } 56 }
57 e = e + 1 57 e = e + 1
58 } 58 }
59 mat 59 mat
60 } 60 }
61 61
62 #2#Function for reorganizing information within the columns 62 #2#Function for reorganizing information within the columns
63 cinfo <- function(mat){ 63 cinfo <- function(mat){
64 col <- dim(mat)[2] 64 col <- dim(mat)[2]
65 j <-2 65 j <-2
66 for(j in 2:col){ 66 for(j in 2:col){
67 if(grepl("Group",colnames(mat)[j]) == TRUE){ 67 if(grepl("Group",colnames(mat)[j]) == TRUE){
68 mat[,j] <- gsub(".+:\\s|\\s.+;.+","",mat[,j]) 68 mat[,j] <- gsub(".+:\\s|\\s.+;.+","",mat[,j])
69 } else if(grepl("Age",colnames(mat)[j])==TRUE){ 69 } else if(grepl("Age",colnames(mat)[j])==TRUE){
70 mat[,j] <- gsub("\\D","",mat[,j])%>% 70 mat[,j] <- gsub("\\D","",mat[,j])%>%
71 as.integer() 71 as.integer()
72 } else if(grepl("Sex",colnames(mat)[j])==TRUE){ 72 } else if(grepl("Sex",colnames(mat)[j])==TRUE){
73 mat[,j] <- gsub(".+:\\s","",mat[,j]) 73 mat[,j] <- gsub(".+:\\s","",mat[,j])
74 } else if(grepl("PMI",colnames(mat)[j])==TRUE){ 74 } else if(grepl("PMI",colnames(mat)[j])==TRUE){
75 mat[,j] <- gsub("[^0-9\\.]","",mat[,j])%>% 75 mat[,j] <- gsub("[^0-9\\.]","",mat[,j])%>%
76 as.numeric() 76 as.numeric()
77 } else if(grepl("Braak",colnames(mat)[j])==TRUE){ 77 } else if(grepl("Braak",colnames(mat)[j])==TRUE){
78 mat[,j]<-gsub(".+:\\s","",mat[,j])%>% 78 mat[,j]<-gsub(".+:\\s","",mat[,j])%>%
79 as.roman()%>% 79 as.roman()%>%
80 as.integer() 80 as.integer()
81 } 81 }
82 j=j+1 82 j=j+1
83 } 83 }
84 mat 84 mat
85 } 85 }
86 86
87 #3#Function for labeling the gene IDs without names 87 #3#Function for labeling the gene IDs without names
88 NAFIXING <- function(GIDNAM){ 88 NAFIXING <- function(GIDNAM){
89 row <- dim(GIDNAM)[1] 89 row <- dim(GIDNAM)[1]
90 i <- 1 90 i <- 1
91 for(i in 1:row){ 91 for(i in 1:row){
92 if(grepl("^NA\\s*$",GIDNAM[i,2])==TRUE||is.na(GIDNAM[i,2])==TRUE){ 92 if(grepl("^NA\\s*$",GIDNAM[i,2])==TRUE||is.na(GIDNAM[i,2])==TRUE){
93 GIDNAM[i,2] <- GIDNAM[i,1] 93 GIDNAM[i,2] <- GIDNAM[i,1]
94 } 94 }
95 i <- i + 1 95 i <- i + 1
96 } 96 }
97 GIDNAM 97 GIDNAM
98 } 98 }
99 99
100 #4#Function for changing the gene ID to gene name 100 #4#Function for changing the gene ID to gene name
101 cgeneID <- function(GeneName,DATA){ 101 cgeneID <- function(GeneName,DATA){
102 nj <- t(GeneName) 102 nj <- t(GeneName)
103 nq <- t(DATA) 103 nq <- t(DATA)
104 colGene <- dim(nj)[2] 104 colGene <- dim(nj)[2]
105 colDATA <- dim(nq)[2] 105 colDATA <- dim(nq)[2]
106 j <- 1 106 j <- 1
107 for(j in 1:colDATA){ 107 for(j in 1:colDATA){
108 #where is that gene id located within the GPL file 108 #where is that gene id located within the GPL file
109 chngreq <- grep(paste0("^",nq[1,j],"$"),nj[1,]) 109 chngreq <- grep(paste0("^",nq[1,j],"$"),nj[1,])
110 if(is.na(sum(chngreq))==FALSE){ 110 if(is.na(sum(chngreq))==FALSE){
111 if(sum(chngreq) > 0){ 111 if(sum(chngreq) > 0){
112 nq[1,j] <- gsub(paste0("^",nq[1,j],"$"),nj[2,chngreq],nq[1,j]) 112 nq[1,j] <- gsub(paste0("^",nq[1,j],"$"),nj[2,chngreq],nq[1,j])
113 } 113 }
114 } 114 }
115 j <- j + 1 115 j <- j + 1
116 } 116 }
117 nq 117 nq
118 } 118 }
119 #cgeneID <- function(GeneName,DATA){ 119 #cgeneID <- function(GeneName,DATA){
120 # colGene <- dim(GeneName)[2] 120 # colGene <- dim(GeneName)[2]
121 # j <- 1 121 # j <- 1
122 # for(j in 1:colGene){ 122 # for(j in 1:colGene){
123 # chngsreq <- grep(paste0("^",GeneName[1,j],"$"),DATA[1,]) 123 # chngsreq <- grep(paste0("^",GeneName[1,j],"$"),DATA[1,])
124 # if(is.na(sum(chngsreq))==FALSE){ 124 # if(is.na(sum(chngsreq))==FALSE){
125 # if(sum(chngsreq) > 0){ 125 # if(sum(chngsreq) > 0){
126 # DATA[1,chngsreq] <- gsub(paste0("^",GeneName[1,j]),GeneName[2,j],DATA[1,chngsreq]) 126 # DATA[1,chngsreq] <- gsub(paste0("^",GeneName[1,j]),GeneName[2,j],DATA[1,chngsreq])
127 # } 127 # }
128 # } 128 # }
129 # j = j+1 129 # j = j+1
130 # } 130 # }
131 # DATA 131 # DATA
132 #} 132 #}
133 133
134 #5#Function for adjusting the gene names 134 #5#Function for adjusting the gene names
135 gcnames <- function(DiData,usecol=1){ 135 gcnames <- function(DiData,usecol=1){
136 nuruns <- dim(DiData)[2] 136 nuruns <- dim(DiData)[2]
137 i = 1 137 i = 1
138 nwnam <- rep("0",length.out=nuruns) 138 nwnam <- rep("0",length.out=nuruns)
139 for(i in 1:nuruns){ 139 for(i in 1:nuruns){
140 if(length(strsplit(colnames(DiData)[i],"///")[[1]]) >= usecol){ 140 if(length(strsplit(colnames(DiData)[i],"///")[[1]]) >= usecol){
141 nwnam[i]=str_trim(strsplit(colnames(DiData)[i],"///")[[1]][usecol]) 141 nwnam[i]=str_trim(strsplit(colnames(DiData)[i],"///")[[1]][usecol])
142 } else{ 142 } else{
143 nwnam[i]=str_trim(strsplit(colnames(DiData)[i],"///")[[1]][1]) 143 nwnam[i]=str_trim(strsplit(colnames(DiData)[i],"///")[[1]][1])
144 } 144 }
145 145
146 } 146 }
147 nwnam 147 nwnam
148 148
149 } 149 }
150 150
151 #6# Function for discretizing the data 151 #6# Function for discretizing the data
152 dndat <- function(NDATA){ 152 dndat <- function(NDATA){
153 rownd <- dim(NDATA)[1] 153 rownd <- dim(NDATA)[1]
154 colnd <- dim(NDATA)[2] 154 colnd <- dim(NDATA)[2]
155 DDATA <- matrix(0,nrow=rownd,ncol=colnd) 155 DDATA <- matrix(0,nrow=rownd,ncol=colnd)
156 colnames(DDATA) <- colnames(NDATA) 156 colnames(DDATA) <- colnames(NDATA)
157 i <- 1 157 i <- 1
158 for(i in 1:rownd){ 158 for(i in 1:rownd){
159 j <- 1 159 j <- 1
160 for(j in 1:colnd){ 160 for(j in 1:colnd){
161 if(is.na(NDATA[i,j])==FALSE){ 161 if(is.na(NDATA[i,j])==FALSE){
162 162
163 if(NDATA[i,j] < -1){ 163 if(NDATA[i,j] < -1){
164 DDATA[i,j]=0L 164 DDATA[i,j]=0L
165 } else if(NDATA[i,j] > 1){ 165 } else if(NDATA[i,j] > 1){
166 DDATA[i,j]=2L 166 DDATA[i,j]=2L
167 } else if(-1 <= NDATA[i,j] && NDATA[i,j] < 1){ 167 } else if(-1 <= NDATA[i,j] && NDATA[i,j] < 1){
168 DDATA[i,j]=1L 168 DDATA[i,j]=1L
169 } 169 }
170 } else{ 170 } else{
171 DDATA[i,j] = NDATA[i,j] 171 DDATA[i,j] = NDATA[i,j]
172 } 172 }
173 j = j + 1 173 j = j + 1
174 } 174 }
175 i = i + 1 175 i = i + 1
176 } 176 }
177 DDATA 177 DDATA
178 } 178 }
179 179
180 180
181 #MajorFunction#This is the function that does everything else 181 #MajorFunction#This is the function that does everything else
182 THEFT <- function(){ 182 THEFT <- function(){
183 #Set working directory based on the directory of the series matrix file Currently only works for windows 183 #Set working directory based on the directory of the series matrix file Currently only works for windows
184 wd <- getwd() 184 wd <- getwd()
185 #list.files() 185 #list.files()
186 #gsub("wd",wd,"Do you want to clean all data files in the directory wd?") 186 #gsub("wd",wd,"Do you want to clean all data files in the directory wd?")
187 numDAT <- switch(EXPR = menu(choices = c("Yes","No"),title = gsub("wd",wd,"Do you want to clean all data files in the directory wd?")) + 1,cat("Nothing done\n"),1L,2L) 187 numDAT <- switch(EXPR = menu(choices = c("Yes","No"),title = gsub("wd",wd,"Do you want to clean all data files in the directory wd?")) + 1,cat("Nothing done\n"),1L,2L)
188 GSEfileloc <- grep("^GSE.+\\.txt\\.gz$",list.files()) 188 GSEfileloc <- grep("^GSE.+\\.txt\\.gz$",list.files())
189 GSEfloc <- list.files()[GSEfileloc] 189 GSEfloc <- list.files()[GSEfileloc]
190 #ALL DATA FILES WILL BE CLEANED 190 #ALL DATA FILES WILL BE CLEANED
191 if(numDAT == 1){ 191 if(numDAT == 1){
192 #indexing the data files 192 #indexing the data files
193 n <- 1 193 n <- 1
194 for(n in 1: length(GSEfloc)){ 194 for(n in 1: length(GSEfloc)){
195 alz <- GSEfloc[n] 195 alz <- GSEfloc[n]
196 196
197 #Working with the wordy part of the document 197 #Working with the wordy part of the document
198 alzword <- alz %>% 198 alzword <- alz %>%
199 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>% 199 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>%
200 filter(grepl("!Sample",X1))%>% 200 filter(grepl("!Sample",X1))%>%
201 filter(!grepl("!Sample_contact",X1)) 201 filter(!grepl("!Sample_contact",X1))
202 202
203 #Getting the GPL file 203 #Getting the GPL file
204 genena <- grep("_platform_id",alzword$X1) %>% 204 genena <- grep("_platform_id",alzword$X1) %>%
205 alzword$X2[.] %>% 205 alzword$X2[.] %>%
206 str_trim(.) %>% 206 str_trim(.) %>%
207 paste0("^",.,"\\D") %>% 207 paste0("^",.,"\\D") %>%
208 grep(.,list.files()) %>% 208 grep(.,list.files()) %>%
209 list.files()[.] 209 list.files()[.]
210 210
211 #Find out if it is a soft GPL file or not 211 #Find out if it is a soft GPL file or not
212 soft <- strsplit(genena,"[\\|/]") %>% 212 soft <- strsplit(genena,"[\\|/]") %>%
213 .[[1]] %>% 213 .[[1]] %>%
214 .[length(.)] %>% 214 .[length(.)] %>%
215 grepl("soft",.) 215 grepl("soft",.)
216 216
217 ##Changing row names and column names: 217 ##Changing row names and column names:
218 ALZWORD <- t(alzword) 218 ALZWORD <- t(alzword)
219 rownames(ALZWORD)=NULL 219 rownames(ALZWORD)=NULL
220 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE) 220 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE)
221 ALZWORD <- chngrownm(ALZWORD)[-1,] 221 ALZWORD <- chngrownm(ALZWORD)[-1,]
222 ALZWORD <- ALZWORD%>% 222 ALZWORD <- ALZWORD%>%
223 as.data.frame()%>% 223 as.data.frame()%>%
224 dplyr::select(-starts_with("col")) 224 dplyr::select(-starts_with("col"))
225 225
226 ##Reorganizing information within the columns and final clinical data 226 ##Reorganizing information within the columns and final clinical data
227 ALZWORDF <- cinfo(ALZWORD) 227 ALZWORDF <- cinfo(ALZWORD)
228 228
229 229
230 #Working with Actual Data part of file 230 #Working with Actual Data part of file
231 alzdat <- alz %>% 231 alzdat <- alz %>%
232 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1) 232 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1)
233 ALZDAT <- t(alzdat[,-1]) 233 ALZDAT <- t(alzdat[,-1])
234 rownames(ALZDAT)=NULL 234 rownames(ALZDAT)=NULL
235 235
236 ##Is there a clean version of the GPL file available? 236 ##Is there a clean version of the GPL file available?
237 gplnum <- strsplit(genena,"[\\|/]") %>% 237 gplnum <- strsplit(genena,"[\\|/]") %>%
238 .[[1]] %>% 238 .[[1]] %>%
239 .[length(.)] %>% 239 .[length(.)] %>%
240 gsub("\\D","",.) 240 gsub("\\D","",.)
241 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files())) 241 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files()))
242 if(clfileex >= 1){ 242 if(clfileex >= 1){
243 #use the clean version 243 #use the clean version
244 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>% 244 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>%
245 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!") 245 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!")
246 246
247 } else if(clfileex == 0){ 247 } else if(clfileex == 0){
248 ##Lets Create a clean version 248 ##Lets Create a clean version
249 249
250 ##Gene ID to Gene Name 250 ##Gene ID to Gene Name
251 if(soft == TRUE){ 251 if(soft == TRUE){
252 #Check to see if there is already a file containing information on soft files 252 #Check to see if there is already a file containing information on soft files
253 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files())) 253 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files()))
254 if(fileex == 1){ 254 if(fileex == 1){
255 #Check to see if this GPL soft file has been used before 255 #Check to see if this GPL soft file has been used before
256 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 256 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
257 .$GPL_FILE_NUM%>% 257 .$GPL_FILE_NUM%>%
258 grepl(gplnum,.) %>% 258 grepl(gplnum,.) %>%
259 sum() 259 sum()
260 if(IDF == 1){ 260 if(IDF == 1){
261 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 261 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
262 .$GPL_FILE_NUM%>% 262 .$GPL_FILE_NUM%>%
263 grep(gplnum,.) 263 grep(gplnum,.)
264 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 264 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
265 .$LOC_ID %>% 265 .$LOC_ID %>%
266 .[IDLOCAL] 266 .[IDLOCAL]
267 geneIDNam <- genena %>% 267 geneIDNam <- genena %>%
268 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>% 268 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>%
269 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 269 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
270 } else if(IDF == 0){ 270 } else if(IDF == 0){
271 #No information on this particular GPL file 271 #No information on this particular GPL file
272 idLOCGPL <- genena %>% 272 idLOCGPL <- genena %>%
273 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 273 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
274 t(.) %>% 274 t(.) %>%
275 grep("^ID\\s*$",.) %>% 275 grep("^ID\\s*$",.) %>%
276 -1 276 -1
277 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>% 277 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>%
278 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE) 278 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE)
279 geneIDNam <- genena %>% 279 geneIDNam <- genena %>%
280 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 280 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
281 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 281 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
282 } 282 }
283 } else if(fileex == 0){ 283 } else if(fileex == 0){
284 #We must create a file that we can access for later use 284 #We must create a file that we can access for later use
285 idLOCGPL <- genena %>% 285 idLOCGPL <- genena %>%
286 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 286 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
287 t(.) %>% 287 t(.) %>%
288 grep("^ID\\s*$",.) %>% 288 grep("^ID\\s*$",.) %>%
289 -1 289 -1
290 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL)) 290 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL))
291 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID") 291 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID")
292 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE) 292 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE)
293 geneIDNam <- genena %>% 293 geneIDNam <- genena %>%
294 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 294 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
295 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 295 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
296 } 296 }
297 } else if(soft == FALSE){ 297 } else if(soft == FALSE){
298 geneIDNam <- genena %>% 298 geneIDNam <- genena %>%
299 read_delim(delim="\t",comment = "#")%>% 299 read_delim(delim="\t",comment = "#")%>%
300 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 300 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
301 } 301 }
302 302
303 ##Labeling the gene IDs without names 303 ##Labeling the gene IDs without names
304 geneIDNam <- NAFIXING(geneIDNam) 304 geneIDNam <- NAFIXING(geneIDNam)
305 305
306 ##remove the whitespace 306 ##remove the whitespace
307 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,]))) 307 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,])))
308 308
309 ##Here is the clean version 309 ##Here is the clean version
310 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE) 310 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE)
311 } 311 }
312 312
313 313
314 314
315 ##Changing the gene ID to gene name 315 ##Changing the gene ID to gene name
316 ALZDAT1 <- cgeneID(t(geneIDNam),t(alzdat)) 316 ALZDAT1 <- cgeneID(geneIDNam,alzdat)
317 colnames(ALZDAT) = ALZDAT1[1,] 317 colnames(ALZDAT) = ALZDAT1[1,]
318 318
319 319
320 ##Adjusting the column names aka the gene names 320 ##Adjusting the column names aka the gene names
321 colnames(ALZDAT) <- gcnames(ALZDAT) 321 colnames(ALZDAT) <- gcnames(ALZDAT)
322 322
323 323
324 #Full RAW Data 324 #Full RAW Data
325 Fullalzdwr <- ALZDAT %>% 325 Fullalzdwr <- ALZDAT %>%
326 as.data.frame() %>% 326 as.data.frame() %>%
327 cbind(ALZWORDF,.) 327 cbind(ALZWORDF,.)
328 328
329 #Raw file is output 329 #Raw file is output
330 nfnaex <- strsplit(alz,"[\\]") %>% 330 nfnaex <- strsplit(alz,"[\\]") %>%
331 .[[1]] %>% 331 .[[1]] %>%
332 .[length(.)] %>% 332 .[length(.)] %>%
333 gsub("\\D","",.) %>% 333 gsub("\\D","",.) %>%
334 c("GSE",.,"aftexcel.txt") %>% 334 c("GSE",.,"aftexcel.txt") %>%
335 paste(collapse = "") 335 paste(collapse = "")
336 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t") 336 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t")
337 337
338 338
339 339
340 #Now for the discretization part 340 #Now for the discretization part
341 ##get the wordy part again 341 ##get the wordy part again
342 rawword <- t(ALZWORDF) 342 rawword <- t(ALZWORDF)
343 343
344 ##where is ID_REF located 344 ##where is ID_REF located
345 hereim <- grep("ID_REF",rownames(rawword)) 345 hereim <- grep("ID_REF",rownames(rawword))
346 346
347 ##Subject Names GSM... 347 ##Subject Names GSM...
348 subjnam <- rawword[hereim,] 348 subjnam <- rawword[hereim,]
349 349
350 ##Getting the names for the rows 350 ##Getting the names for the rows
351 namedarows <- rownames(rawword)[-hereim] %>% 351 namedarows <- rownames(rawword)[-hereim] %>%
352 as.data.frame() 352 as.data.frame()
353 RAWWORD <- rawword[-hereim,] %>% 353 RAWWORD <- rawword[-hereim,] %>%
354 as.data.frame() %>% 354 as.data.frame() %>%
355 bind_cols(namedarows,.) 355 bind_cols(namedarows,.)
356 z <- 1 356 z <- 1
357 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE) 357 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE)
358 for(z in 1:dim(RAWWORD)[1]){ 358 for(z in 1:dim(RAWWORD)[1]){
359 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,]))) 359 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,])))
360 z <- z + 1 360 z <- z + 1
361 } 361 }
362 362
363 colnames(naroww) <- "ROW_NAs" 363 colnames(naroww) <- "ROW_NAs"
364 RAWWORD <- bind_cols(RAWWORD,naroww) 364 RAWWORD <- bind_cols(RAWWORD,naroww)
365 365
366 366
367 roALZna <- t(ALZDAT) %>% 367 roALZna <- t(ALZDAT) %>%
368 rownames(.) %>% 368 rownames(.) %>%
369 as.data.frame(.) 369 as.data.frame(.)
370 colnames(roALZna) <- "ID_REF" 370 colnames(roALZna) <- "ID_REF"
371 371
372 RAWDAT <- t(ALZDAT) %>% 372 RAWDAT <- t(ALZDAT) %>%
373 as.data.frame(.) 373 as.data.frame(.)
374 colnames(RAWDAT) <- NULL 374 colnames(RAWDAT) <- NULL
375 rownames(RAWDAT) <- NULL 375 rownames(RAWDAT) <- NULL
376 376
377 RAWDAT2 <- RAWDAT %>% 377 RAWDAT2 <- RAWDAT %>%
378 cbind(roALZna,.) %>% 378 cbind(roALZna,.) %>%
379 dplyr::arrange(.,ID_REF) 379 dplyr::arrange(.,ID_REF)
380 380
381 ##Editing the file for R processing 381 ##Editing the file for R processing
382 RAWDATID <- RAWDAT2[,1] %>% 382 RAWDATID <- RAWDAT2[,1] %>%
383 as.matrix(.) 383 as.matrix(.)
384 384
385 RAWDATNUM <- RAWDAT2[,-1] %>% 385 RAWDATNUM <- RAWDAT2[,-1] %>%
386 mapply(.,FUN = as.numeric) %>% 386 mapply(.,FUN = as.numeric) %>%
387 t(.) 387 t(.)
388 388
389 ##Consolidating genes with the same name 389 ##Consolidating genes with the same name
390 ###create empty matrix of size equal to tabRDATID 390 ###create empty matrix of size equal to tabRDATID
391 tabRDATID <- table(RAWDATID) 391 tabRDATID <- table(RAWDATID)
392 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID)) 392 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID))
393 j <- 1 393 j <- 1
394 for(j in 1:length(tabRDATID)){ 394 for(j in 1:length(tabRDATID)){
395 ##Putting the ones without duplicates in their new homes 395 ##Putting the ones without duplicates in their new homes
396 if(tabRDATID[j] == 1){ 396 if(tabRDATID[j] == 1){
397 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])] 397 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])]
398 } else if(tabRDATID[j] > 1){ 398 } else if(tabRDATID[j] > 1){
399 ##Averaging duplicates and putting them in their new homes 399 ##Averaging duplicates and putting them in their new homes
400 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE) 400 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE)
401 } 401 }
402 j <- j + 1 402 j <- j + 1
403 } 403 }
404 404
405 ##Scaling the Data 405 ##Scaling the Data
406 scrawdat <- NuRDATN%>% 406 scrawdat <- NuRDATN%>%
407 scale() 407 scale()
408 attr(scrawdat,"scaled:center") <- NULL 408 attr(scrawdat,"scaled:center") <- NULL
409 attr(scrawdat,"scaled:scale") <- NULL 409 attr(scrawdat,"scaled:scale") <- NULL
410 colnames(scrawdat) <- rownames(tabRDATID) 410 colnames(scrawdat) <- rownames(tabRDATID)
411 411
412 ##Discretized the Data 412 ##Discretized the Data
413 dialzdat <- scrawdat %>% 413 dialzdat <- scrawdat %>%
414 dndat(.) %>% 414 dndat(.) %>%
415 t()%>% 415 t()%>%
416 as.data.frame(.) 416 as.data.frame(.)
417 colnames(dialzdat) <- rownames(RAWDATNUM) 417 colnames(dialzdat) <- rownames(RAWDATNUM)
418 418
419 ##setting "ID_REF" as a new variable 419 ##setting "ID_REF" as a new variable
420 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1)) 420 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1))
421 colnames(geneNAM) <- "ID_REF" 421 colnames(geneNAM) <- "ID_REF"
422 rownames(dialzdat) <- NULL 422 rownames(dialzdat) <- NULL
423 dialzdat <-bind_cols(geneNAM,dialzdat) 423 dialzdat <-bind_cols(geneNAM,dialzdat)
424 424
425 ##NAs in a column 425 ##NAs in a column
426 x <- 2 426 x <- 2
427 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE) 427 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE)
428 nacol[1,1] = "COL_NAs" 428 nacol[1,1] = "COL_NAs"
429 for(x in 2:dim(dialzdat)[2]){ 429 for(x in 2:dim(dialzdat)[2]){
430 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x]))) 430 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x])))
431 x <- x + 1 431 x <- x + 1
432 } 432 }
433 colnames(nacol) <- colnames(dialzdat) 433 colnames(nacol) <- colnames(dialzdat)
434 dialzdat <- bind_rows(dialzdat,nacol) 434 dialzdat <- bind_rows(dialzdat,nacol)
435 435
436 ##NAs in a row 436 ##NAs in a row
437 y <- 1 437 y <- 1
438 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE) 438 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE)
439 for(y in 1:dim(dialzdat)[1]){ 439 for(y in 1:dim(dialzdat)[1]){
440 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,]))) 440 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,])))
441 y <- y + 1 441 y <- y + 1
442 } 442 }
443 colnames(narowd) <- "ROW_NAs" 443 colnames(narowd) <- "ROW_NAs"
444 dialzdat <- bind_cols(dialzdat,narowd) 444 dialzdat <- bind_cols(dialzdat,narowd)
445 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam 445 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam
446 colnames(RAWWORD) <- colnames(dialzdat) 446 colnames(RAWWORD) <- colnames(dialzdat)
447 ##converting to character so that the clinical can be brought together with discrete data 447 ##converting to character so that the clinical can be brought together with discrete data
448 k <- 2 448 k <- 2
449 for(k in 2:dim(dialzdat)[2]-1){ 449 for(k in 2:dim(dialzdat)[2]-1){
450 dialzdat[,k] <- as.character(dialzdat[,k]) 450 dialzdat[,k] <- as.character(dialzdat[,k])
451 k <- k + 1 451 k <- k + 1
452 } 452 }
453 #The End the full data 453 #The End the full data
454 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat) 454 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat)
455 455
456 #Produces Discrete file 456 #Produces Discrete file
457 nfnaex2 <- strsplit(alz,"[\\|/]") %>% 457 nfnaex2 <- strsplit(alz,"[\\|/]") %>%
458 .[[1]] %>% 458 .[[1]] %>%
459 .[length(.)] %>% 459 .[length(.)] %>%
460 gsub("\\D","",.) %>% 460 gsub("\\D","",.) %>%
461 c("GSE",.,"dscrt.txt") %>% 461 c("GSE",.,"dscrt.txt") %>%
462 paste(collapse = "") 462 paste(collapse = "")
463 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE) 463 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE)
464 n <- n +1 464 n <- n +1
465 } 465 }
466 } else if(numDAT == 2){ 466 } else if(numDAT == 2){
467 #CHOOSE A DATA FILE TO CLEAN OR SEVERAL DATA FILES TO CLEAN 467 #CHOOSE A DATA FILE TO CLEAN OR SEVERAL DATA FILES TO CLEAN
468 468
469 #All the files you want to analyze 469 #All the files you want to analyze
470 ANDIS <- select.list(choices = list.files()[GSEfileloc],multiple = TRUE, title = "Choose the file/files you want to analyze:") 470 ANDIS <- select.list(choices = list.files()[GSEfileloc],multiple = TRUE, title = "Choose the file/files you want to analyze:")
471 if(length(ANDIS) == 0){ 471 if(length(ANDIS) == 0){
472 #Spit out a warning 472 #Spit out a warning
473 warning("You did not select any files and so no cleaning will be performed") 473 warning("You did not select any files and so no cleaning will be performed")
474 } else{ 474 } else{
475 #indexing the data files 475 #indexing the data files
476 n <- 1 476 n <- 1
477 for(n in 1: length(ANDIS)){ 477 for(n in 1: length(ANDIS)){
478 alz <- ANDIS[n] 478 alz <- ANDIS[n]
479 479
480 #Working with the wordy part of the document 480 #Working with the wordy part of the document
481 alzword <- alz %>% 481 alzword <- alz %>%
482 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>% 482 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>%
483 filter(grepl("!Sample",X1))%>% 483 filter(grepl("!Sample",X1))%>%
484 filter(!grepl("!Sample_contact",X1)) 484 filter(!grepl("!Sample_contact",X1))
485 485
486 #Getting the GPL file 486 #Getting the GPL file
487 genena <- grep("_platform_id",alzword$X1) %>% 487 genena <- grep("_platform_id",alzword$X1) %>%
488 alzword$X2[.] %>% 488 alzword$X2[.] %>%
489 str_trim(.) %>% 489 str_trim(.) %>%
490 paste0("^",.,"\\D") %>% 490 paste0("^",.,"\\D") %>%
491 grep(.,list.files()) %>% 491 grep(.,list.files()) %>%
492 list.files()[.] 492 list.files()[.]
493 493
494 #Find out if it is a soft GPL file or not 494 #Find out if it is a soft GPL file or not
495 soft <- strsplit(genena,"[\\|/]") %>% 495 soft <- strsplit(genena,"[\\|/]") %>%
496 .[[1]] %>% 496 .[[1]] %>%
497 .[length(.)] %>% 497 .[length(.)] %>%
498 grepl("soft",.) 498 grepl("soft",.)
499 499
500 ##Changing row names and column names: 500 ##Changing row names and column names:
501 ALZWORD <- t(alzword) 501 ALZWORD <- t(alzword)
502 rownames(ALZWORD)=NULL 502 rownames(ALZWORD)=NULL
503 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE) 503 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE)
504 ALZWORD <- chngrownm(ALZWORD)[-1,] 504 ALZWORD <- chngrownm(ALZWORD)[-1,]
505 ALZWORD <- ALZWORD%>% 505 ALZWORD <- ALZWORD%>%
506 as.data.frame()%>% 506 as.data.frame()%>%
507 dplyr::select(-starts_with("col")) 507 dplyr::select(-starts_with("col"))
508 508
509 ##Reorganizing information within the columns and final clinical data 509 ##Reorganizing information within the columns and final clinical data
510 ALZWORDF <- cinfo(ALZWORD) 510 ALZWORDF <- cinfo(ALZWORD)
511 511
512 512
513 #Working with Actual Data part of file 513 #Working with Actual Data part of file
514 alzdat <- alz %>% 514 alzdat <- alz %>%
515 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1) 515 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1)
516 ALZDAT <- t(alzdat[,-1]) 516 ALZDAT <- t(alzdat[,-1])
517 rownames(ALZDAT)=NULL 517 rownames(ALZDAT)=NULL
518 518
519 ##Is there a clean version of the GPL file available? 519 ##Is there a clean version of the GPL file available?
520 gplnum <- strsplit(genena,"[\\|/]") %>% 520 gplnum <- strsplit(genena,"[\\|/]") %>%
521 .[[1]] %>% 521 .[[1]] %>%
522 .[length(.)] %>% 522 .[length(.)] %>%
523 gsub("\\D","",.) 523 gsub("\\D","",.)
524 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files())) 524 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files()))
525 if(clfileex >= 1){ 525 if(clfileex >= 1){
526 #use the clean version 526 #use the clean version
527 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>% 527 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>%
528 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!") 528 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!")
529 529
530 } else if(clfileex == 0){ 530 } else if(clfileex == 0){
531 ##Lets Create a clean version 531 ##Lets Create a clean version
532 532
533 ##Gene ID to Gene Name 533 ##Gene ID to Gene Name
534 if(soft == TRUE){ 534 if(soft == TRUE){
535 #Check to see if there is already a file containing information on soft files 535 #Check to see if there is already a file containing information on soft files
536 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files())) 536 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files()))
537 if(fileex == 1){ 537 if(fileex == 1){
538 #Check to see if this GPL soft file has been used before 538 #Check to see if this GPL soft file has been used before
539 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 539 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
540 .$GPL_FILE_NUM%>% 540 .$GPL_FILE_NUM%>%
541 grepl(gplnum,.) %>% 541 grepl(gplnum,.) %>%
542 sum() 542 sum()
543 if(IDF == 1){ 543 if(IDF == 1){
544 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 544 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
545 .$GPL_FILE_NUM%>% 545 .$GPL_FILE_NUM%>%
546 grep(gplnum,.) 546 grep(gplnum,.)
547 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 547 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
548 .$LOC_ID %>% 548 .$LOC_ID %>%
549 .[IDLOCAL] 549 .[IDLOCAL]
550 geneIDNam <- genena %>% 550 geneIDNam <- genena %>%
551 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>% 551 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>%
552 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 552 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
553 } else if(IDF == 0){ 553 } else if(IDF == 0){
554 #No information on this particular GPL file 554 #No information on this particular GPL file
555 idLOCGPL <- genena %>% 555 idLOCGPL <- genena %>%
556 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 556 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
557 t(.) %>% 557 t(.) %>%
558 grep("^ID\\s*$",.) %>% 558 grep("^ID\\s*$",.) %>%
559 -1 559 -1
560 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>% 560 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>%
561 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE) 561 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE)
562 geneIDNam <- genena %>% 562 geneIDNam <- genena %>%
563 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 563 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
564 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 564 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
565 } 565 }
566 } else if(fileex == 0){ 566 } else if(fileex == 0){
567 #We must create a file that we can access for later use 567 #We must create a file that we can access for later use
568 idLOCGPL <- genena %>% 568 idLOCGPL <- genena %>%
569 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 569 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
570 t(.) %>% 570 t(.) %>%
571 grep("^ID\\s*$",.) %>% 571 grep("^ID\\s*$",.) %>%
572 -1 572 -1
573 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL)) 573 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL))
574 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID") 574 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID")
575 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE) 575 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE)
576 geneIDNam <- genena %>% 576 geneIDNam <- genena %>%
577 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 577 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
578 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 578 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
579 } 579 }
580 } else if(soft == FALSE){ 580 } else if(soft == FALSE){
581 geneIDNam <- genena %>% 581 geneIDNam <- genena %>%
582 read_delim(delim="\t",comment = "#")%>% 582 read_delim(delim="\t",comment = "#")%>%
583 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 583 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
584 } 584 }
585 585
586 ##Labeling the gene IDs without names 586 ##Labeling the gene IDs without names
587 geneIDNam <- NAFIXING(geneIDNam) 587 geneIDNam <- NAFIXING(geneIDNam)
588 588
589 ##remove the whitespace 589 ##remove the whitespace
590 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,]))) 590 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,])))
591 591
592 ##Here is the clean version 592 ##Here is the clean version
593 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE) 593 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE)
594 } 594 }
595 595
596 596
597 597
598 ##Changing the gene ID to gene name 598 ##Changing the gene ID to gene name
599 ALZDAT1 <- cgeneID(t(geneIDNam),t(alzdat)) 599 ALZDAT1 <- cgeneID(geneIDNam,alzdat)
600 colnames(ALZDAT) = ALZDAT1[1,] 600 colnames(ALZDAT) = ALZDAT1[1,]
601 601
602 602
603 ##Adjusting the column names aka the gene names 603 ##Adjusting the column names aka the gene names
604 colnames(ALZDAT) <- gcnames(ALZDAT) 604 colnames(ALZDAT) <- gcnames(ALZDAT)
605 605
606 606
607 #Full RAW Data 607 #Full RAW Data
608 Fullalzdwr <- ALZDAT %>% 608 Fullalzdwr <- ALZDAT %>%
609 as.data.frame() %>% 609 as.data.frame() %>%
610 cbind(ALZWORDF,.) 610 cbind(ALZWORDF,.)
611 611
612 #Raw file is output 612 #Raw file is output
613 nfnaex <- strsplit(alz,"[\\]") %>% 613 nfnaex <- strsplit(alz,"[\\]") %>%
614 .[[1]] %>% 614 .[[1]] %>%
615 .[length(.)] %>% 615 .[length(.)] %>%
616 gsub("\\D","",.) %>% 616 gsub("\\D","",.) %>%
617 c("GSE",.,"aftexcel.txt") %>% 617 c("GSE",.,"aftexcel.txt") %>%
618 paste(collapse = "") 618 paste(collapse = "")
619 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t") 619 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t")
620 620
621 621
622 622
623 #Now for the discretization part 623 #Now for the discretization part
624 ##get the wordy part again 624 ##get the wordy part again
625 rawword <- t(ALZWORDF) 625 rawword <- t(ALZWORDF)
626 626
627 ##where is ID_REF located 627 ##where is ID_REF located
628 hereim <- grep("ID_REF",rownames(rawword)) 628 hereim <- grep("ID_REF",rownames(rawword))
629 629
630 ##Subject Names GSM... 630 ##Subject Names GSM...
631 subjnam <- rawword[hereim,] 631 subjnam <- rawword[hereim,]
632 632
633 ##Getting the names for the rows 633 ##Getting the names for the rows
634 namedarows <- rownames(rawword)[-hereim] %>% 634 namedarows <- rownames(rawword)[-hereim] %>%
635 as.data.frame() 635 as.data.frame()
636 RAWWORD <- rawword[-hereim,] %>% 636 RAWWORD <- rawword[-hereim,] %>%
637 as.data.frame() %>% 637 as.data.frame() %>%
638 bind_cols(namedarows,.) 638 bind_cols(namedarows,.)
639 z <- 1 639 z <- 1
640 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE) 640 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE)
641 for(z in 1:dim(RAWWORD)[1]){ 641 for(z in 1:dim(RAWWORD)[1]){
642 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,]))) 642 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,])))
643 z <- z + 1 643 z <- z + 1
644 } 644 }
645 645
646 colnames(naroww) <- "ROW_NAs" 646 colnames(naroww) <- "ROW_NAs"
647 RAWWORD <- bind_cols(RAWWORD,naroww) 647 RAWWORD <- bind_cols(RAWWORD,naroww)
648 648
649 649
650 roALZna <- t(ALZDAT) %>% 650 roALZna <- t(ALZDAT) %>%
651 rownames(.) %>% 651 rownames(.) %>%
652 as.data.frame(.) 652 as.data.frame(.)
653 colnames(roALZna) <- "ID_REF" 653 colnames(roALZna) <- "ID_REF"
654 654
655 RAWDAT <- t(ALZDAT) %>% 655 RAWDAT <- t(ALZDAT) %>%
656 as.data.frame(.) 656 as.data.frame(.)
657 colnames(RAWDAT) <- NULL 657 colnames(RAWDAT) <- NULL
658 rownames(RAWDAT) <- NULL 658 rownames(RAWDAT) <- NULL
659 659
660 RAWDAT2 <- RAWDAT %>% 660 RAWDAT2 <- RAWDAT %>%
661 cbind(roALZna,.) %>% 661 cbind(roALZna,.) %>%
662 dplyr::arrange(.,ID_REF) 662 dplyr::arrange(.,ID_REF)
663 663
664 ##Editing the file for R processing 664 ##Editing the file for R processing
665 RAWDATID <- RAWDAT2[,1] %>% 665 RAWDATID <- RAWDAT2[,1] %>%
666 as.matrix(.) 666 as.matrix(.)
667 667
668 RAWDATNUM <- RAWDAT2[,-1] %>% 668 RAWDATNUM <- RAWDAT2[,-1] %>%
669 mapply(.,FUN = as.numeric) %>% 669 mapply(.,FUN = as.numeric) %>%
670 t(.) 670 t(.)
671 671
672 ##Consolidating genes with the same name 672 ##Consolidating genes with the same name
673 ###create empty matrix of size equal to tabRDATID 673 ###create empty matrix of size equal to tabRDATID
674 tabRDATID <- table(RAWDATID) 674 tabRDATID <- table(RAWDATID)
675 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID)) 675 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID))
676 j <- 1 676 j <- 1
677 for(j in 1:length(tabRDATID)){ 677 for(j in 1:length(tabRDATID)){
678 ##Putting the ones without duplicates in their new homes 678 ##Putting the ones without duplicates in their new homes
679 if(tabRDATID[j] == 1){ 679 if(tabRDATID[j] == 1){
680 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])] 680 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])]
681 } else if(tabRDATID[j] > 1){ 681 } else if(tabRDATID[j] > 1){
682 ##Averaging duplicates and putting them in their new homes 682 ##Averaging duplicates and putting them in their new homes
683 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE) 683 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE)
684 } 684 }
685 j <- j + 1 685 j <- j + 1
686 } 686 }
687 687
688 ##Scaling the Data 688 ##Scaling the Data
689 scrawdat <- NuRDATN%>% 689 scrawdat <- NuRDATN%>%
690 scale() 690 scale()
691 attr(scrawdat,"scaled:center") <- NULL 691 attr(scrawdat,"scaled:center") <- NULL
692 attr(scrawdat,"scaled:scale") <- NULL 692 attr(scrawdat,"scaled:scale") <- NULL
693 colnames(scrawdat) <- rownames(tabRDATID) 693 colnames(scrawdat) <- rownames(tabRDATID)
694 694
695 ##Discretized the Data 695 ##Discretized the Data
696 dialzdat <- scrawdat %>% 696 dialzdat <- scrawdat %>%
697 dndat(.) %>% 697 dndat(.) %>%
698 t()%>% 698 t()%>%
699 as.data.frame(.) 699 as.data.frame(.)
700 colnames(dialzdat) <- rownames(RAWDATNUM) 700 colnames(dialzdat) <- rownames(RAWDATNUM)
701 701
702 ##setting "ID_REF" as a new variable 702 ##setting "ID_REF" as a new variable
703 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1)) 703 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1))
704 colnames(geneNAM) <- "ID_REF" 704 colnames(geneNAM) <- "ID_REF"
705 rownames(dialzdat) <- NULL 705 rownames(dialzdat) <- NULL
706 dialzdat <-bind_cols(geneNAM,dialzdat) 706 dialzdat <-bind_cols(geneNAM,dialzdat)
707 707
708 ##NAs in a column 708 ##NAs in a column
709 x <- 2 709 x <- 2
710 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE) 710 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE)
711 nacol[1,1] = "COL_NAs" 711 nacol[1,1] = "COL_NAs"
712 for(x in 2:dim(dialzdat)[2]){ 712 for(x in 2:dim(dialzdat)[2]){
713 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x]))) 713 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x])))
714 x <- x + 1 714 x <- x + 1
715 } 715 }
716 colnames(nacol) <- colnames(dialzdat) 716 colnames(nacol) <- colnames(dialzdat)
717 dialzdat <- bind_rows(dialzdat,nacol) 717 dialzdat <- bind_rows(dialzdat,nacol)
718 718
719 ##NAs in a row 719 ##NAs in a row
720 y <- 1 720 y <- 1
721 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE) 721 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE)
722 for(y in 1:dim(dialzdat)[1]){ 722 for(y in 1:dim(dialzdat)[1]){
723 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,]))) 723 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,])))
724 y <- y + 1 724 y <- y + 1
725 } 725 }
726 colnames(narowd) <- "ROW_NAs" 726 colnames(narowd) <- "ROW_NAs"
727 dialzdat <- bind_cols(dialzdat,narowd) 727 dialzdat <- bind_cols(dialzdat,narowd)
728 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam 728 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam
729 colnames(RAWWORD) <- colnames(dialzdat) 729 colnames(RAWWORD) <- colnames(dialzdat)
730 ##converting to character so that the clinical can be brought together with discrete data 730 ##converting to character so that the clinical can be brought together with discrete data
731 k <- 2 731 k <- 2
732 for(k in 2:dim(dialzdat)[2]-1){ 732 for(k in 2:dim(dialzdat)[2]-1){
733 dialzdat[,k] <- as.character(dialzdat[,k]) 733 dialzdat[,k] <- as.character(dialzdat[,k])
734 k <- k + 1 734 k <- k + 1
735 } 735 }
736 #The End the full data 736 #The End the full data
737 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat) 737 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat)
738 738
739 #Produces Discrete file 739 #Produces Discrete file
740 nfnaex2 <- strsplit(alz,"[\\|/]") %>% 740 nfnaex2 <- strsplit(alz,"[\\|/]") %>%
741 .[[1]] %>% 741 .[[1]] %>%
742 .[length(.)] %>% 742 .[length(.)] %>%
743 gsub("\\D","",.) %>% 743 gsub("\\D","",.) %>%
744 c("GSE",.,"dscrt.txt") %>% 744 c("GSE",.,"dscrt.txt") %>%
745 paste(collapse = "") 745 paste(collapse = "")
746 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE) 746 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE)
747 747
748 748
749 n <- n + 1 749 n <- n + 1
750 } 750 }
751 } 751 }
752 } 752 }
753 } 753 }
754 #The Rest of this code will be used every time you want to change a data set 754 #The Rest of this code will be used every time you want to change a data set
755 THEFT() 755 THEFT()