Commit edf2baf695fe2ac66eaaf97eb24011b5cddaaee6

Authored by Efrain Gonzalez
1 parent c2b2c096e9
Exists in master

Don't use this code yet

Update to the amount of digits displayed in files
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/21/2017
6 6 options(digits = 11)
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(geneIDNam,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 if(sum(is.na(RAWWORD[z,])) > 0){ 359 if(sum(is.na(RAWWORD[z,])) > 0){
360 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,]))) 360 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,])))
361 } 361 }
362 if(length(grep("NA",RAWWORD[z,])) > 0){ 362 if(length(grep("NA",RAWWORD[z,])) > 0){
363 naroww[z,1] <- as.integer(length(grep("NA",RAWWORD[z,]))) + naroww[z,1] 363 naroww[z,1] <- as.integer(length(grep("NA",RAWWORD[z,]))) + naroww[z,1]
364 } 364 }
365 z <- z + 1 365 z <- z + 1
366 } 366 }
367 367
368 colnames(naroww) <- "ROW_NAs" 368 colnames(naroww) <- "ROW_NAs"
369 RAWWORD <- bind_cols(RAWWORD,naroww) 369 RAWWORD <- bind_cols(RAWWORD,naroww)
370 370
371 371
372 roALZna <- t(ALZDAT) %>% 372 roALZna <- t(ALZDAT) %>%
373 rownames(.) %>% 373 rownames(.) %>%
374 as.data.frame(.) 374 as.data.frame(.)
375 colnames(roALZna) <- "ID_REF" 375 colnames(roALZna) <- "ID_REF"
376 376
377 RAWDAT <- t(ALZDAT) %>% 377 RAWDAT <- t(ALZDAT) %>%
378 as.data.frame(.) 378 as.data.frame(.)
379 colnames(RAWDAT) <- NULL 379 colnames(RAWDAT) <- NULL
380 rownames(RAWDAT) <- NULL 380 rownames(RAWDAT) <- NULL
381 381
382 RAWDAT2 <- RAWDAT %>% 382 RAWDAT2 <- RAWDAT %>%
383 cbind(roALZna,.) %>% 383 cbind(roALZna,.) %>%
384 dplyr::arrange(.,ID_REF) 384 dplyr::arrange(.,ID_REF)
385 385
386 ##Editing the file for R processing 386 ##Editing the file for R processing
387 RAWDATID <- RAWDAT2[,1] %>% 387 RAWDATID <- RAWDAT2[,1] %>%
388 as.matrix(.) 388 as.matrix(.)
389 389
390 RAWDATNUM <- RAWDAT2[,-1] %>% 390 RAWDATNUM <- RAWDAT2[,-1] %>%
391 mapply(.,FUN = as.numeric) %>% 391 mapply(.,FUN = as.numeric) %>%
392 t(.) 392 t(.)
393 393
394 ##Consolidating genes with the same name 394 ##Consolidating genes with the same name
395 ###create empty matrix of size equal to tabRDATID 395 ###create empty matrix of size equal to tabRDATID
396 tabRDATID <- table(RAWDATID) 396 tabRDATID <- table(RAWDATID)
397 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID)) 397 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID))
398 j <- 1 398 j <- 1
399 for(j in 1:length(tabRDATID)){ 399 for(j in 1:length(tabRDATID)){
400 ##Putting the ones without duplicates in their new homes 400 ##Putting the ones without duplicates in their new homes
401 if(tabRDATID[j] == 1){ 401 if(tabRDATID[j] == 1){
402 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])] 402 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])]
403 } else if(tabRDATID[j] > 1){ 403 } else if(tabRDATID[j] > 1){
404 ##Averaging duplicates and putting them in their new homes 404 ##Averaging duplicates and putting them in their new homes
405 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE) 405 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE)
406 } 406 }
407 j <- j + 1 407 j <- j + 1
408 } 408 }
409 409
410 ##Scaling the Data 410 ##Scaling the Data
411 scrawdat <- NuRDATN%>% 411 scrawdat <- NuRDATN%>%
412 scale() 412 scale()
413 attr(scrawdat,"scaled:center") <- NULL 413 attr(scrawdat,"scaled:center") <- NULL
414 attr(scrawdat,"scaled:scale") <- NULL 414 attr(scrawdat,"scaled:scale") <- NULL
415 colnames(scrawdat) <- rownames(tabRDATID) 415 colnames(scrawdat) <- rownames(tabRDATID)
416 416
417 ##Discretized the Data 417 ##Discretized the Data
418 dialzdat <- scrawdat %>% 418 dialzdat <- scrawdat %>%
419 dndat(.) %>% 419 dndat(.) %>%
420 t()%>% 420 t()%>%
421 as.data.frame(.) 421 as.data.frame(.)
422 colnames(dialzdat) <- rownames(RAWDATNUM) 422 colnames(dialzdat) <- rownames(RAWDATNUM)
423 423
424 ##setting "ID_REF" as a new variable 424 ##setting "ID_REF" as a new variable
425 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1)) 425 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1))
426 colnames(geneNAM) <- "ID_REF" 426 colnames(geneNAM) <- "ID_REF"
427 rownames(dialzdat) <- NULL 427 rownames(dialzdat) <- NULL
428 dialzdat <-bind_cols(geneNAM,dialzdat) 428 dialzdat <-bind_cols(geneNAM,dialzdat)
429 429
430 ##NAs in a column 430 ##NAs in a column
431 x <- 2 431 x <- 2
432 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE) 432 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE)
433 nacol[1,1] = "COL_NAs" 433 nacol[1,1] = "COL_NAs"
434 for(x in 2:dim(dialzdat)[2]){ 434 for(x in 2:dim(dialzdat)[2]){
435 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x]))) 435 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x])))
436 x <- x + 1 436 x <- x + 1
437 } 437 }
438 colnames(nacol) <- colnames(dialzdat) 438 colnames(nacol) <- colnames(dialzdat)
439 dialzdat <- bind_rows(dialzdat,nacol) 439 dialzdat <- bind_rows(dialzdat,nacol)
440 440
441 ##NAs in a row 441 ##NAs in a row
442 y <- 1 442 y <- 1
443 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE) 443 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE)
444 for(y in 1:dim(dialzdat)[1]){ 444 for(y in 1:dim(dialzdat)[1]){
445 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,]))) 445 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,])))
446 y <- y + 1 446 y <- y + 1
447 } 447 }
448 colnames(narowd) <- "ROW_NAs" 448 colnames(narowd) <- "ROW_NAs"
449 dialzdat <- bind_cols(dialzdat,narowd) 449 dialzdat <- bind_cols(dialzdat,narowd)
450 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam 450 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam
451 colnames(RAWWORD) <- colnames(dialzdat) 451 colnames(RAWWORD) <- colnames(dialzdat)
452 ##converting to character so that the clinical can be brought together with discrete data 452 ##converting to character so that the clinical can be brought together with discrete data
453 k <- 2 453 k <- 2
454 for(k in 2:dim(dialzdat)[2]-1){ 454 for(k in 2:dim(dialzdat)[2]-1){
455 dialzdat[,k] <- as.character(dialzdat[,k]) 455 dialzdat[,k] <- as.character(dialzdat[,k])
456 k <- k + 1 456 k <- k + 1
457 } 457 }
458 #The End the full data 458 #The End the full data
459 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat) 459 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat)
460 460
461 #Produces Discrete file 461 #Produces Discrete file
462 nfnaex2 <- strsplit(alz,"[\\|/]") %>% 462 nfnaex2 <- strsplit(alz,"[\\|/]") %>%
463 .[[1]] %>% 463 .[[1]] %>%
464 .[length(.)] %>% 464 .[length(.)] %>%
465 gsub("\\D","",.) %>% 465 gsub("\\D","",.) %>%
466 c("GSE",.,"dscrt.txt") %>% 466 c("GSE",.,"dscrt.txt") %>%
467 paste(collapse = "") 467 paste(collapse = "")
468 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE) 468 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE)
469 n <- n +1 469 n <- n +1
470 } 470 }
471 } else if(numDAT == 2){ 471 } else if(numDAT == 2){
472 #CHOOSE A DATA FILE TO CLEAN OR SEVERAL DATA FILES TO CLEAN 472 #CHOOSE A DATA FILE TO CLEAN OR SEVERAL DATA FILES TO CLEAN
473 473
474 #All the files you want to analyze 474 #All the files you want to analyze
475 ANDIS <- select.list(choices = list.files()[GSEfileloc],multiple = TRUE, title = "Choose the file/files you want to analyze:") 475 ANDIS <- select.list(choices = list.files()[GSEfileloc],multiple = TRUE, title = "Choose the file/files you want to analyze:")
476 if(length(ANDIS) == 0){ 476 if(length(ANDIS) == 0){
477 #Spit out a warning 477 #Spit out a warning
478 warning("You did not select any files and so no cleaning will be performed") 478 warning("You did not select any files and so no cleaning will be performed")
479 } else{ 479 } else{
480 #indexing the data files 480 #indexing the data files
481 n <- 1 481 n <- 1
482 for(n in 1: length(ANDIS)){ 482 for(n in 1: length(ANDIS)){
483 alz <- ANDIS[n] 483 alz <- ANDIS[n]
484 484
485 #Working with the wordy part of the document 485 #Working with the wordy part of the document
486 alzword <- alz %>% 486 alzword <- alz %>%
487 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>% 487 read_delim(delim ="\t",comment = "!Series",col_names = FALSE)%>%
488 filter(grepl("!Sample",X1))%>% 488 filter(grepl("!Sample",X1))%>%
489 filter(!grepl("!Sample_contact",X1)) 489 filter(!grepl("!Sample_contact",X1))
490 490
491 #Getting the GPL file 491 #Getting the GPL file
492 genena <- grep("_platform_id",alzword$X1) %>% 492 genena <- grep("_platform_id",alzword$X1) %>%
493 alzword$X2[.] %>% 493 alzword$X2[.] %>%
494 str_trim(.) %>% 494 str_trim(.) %>%
495 paste0("^",.,"\\D") %>% 495 paste0("^",.,"\\D") %>%
496 grep(.,list.files()) %>% 496 grep(.,list.files()) %>%
497 list.files()[.] 497 list.files()[.]
498 498
499 #Find out if it is a soft GPL file or not 499 #Find out if it is a soft GPL file or not
500 soft <- strsplit(genena,"[\\|/]") %>% 500 soft <- strsplit(genena,"[\\|/]") %>%
501 .[[1]] %>% 501 .[[1]] %>%
502 .[length(.)] %>% 502 .[length(.)] %>%
503 grepl("soft",.) 503 grepl("soft",.)
504 504
505 ##Changing row names and column names: 505 ##Changing row names and column names:
506 ALZWORD <- t(alzword) 506 ALZWORD <- t(alzword)
507 rownames(ALZWORD)=NULL 507 rownames(ALZWORD)=NULL
508 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE) 508 colnames(ALZWORD) <- colnames(ALZWORD,do.NULL=FALSE)
509 ALZWORD <- chngrownm(ALZWORD)[-1,] 509 ALZWORD <- chngrownm(ALZWORD)[-1,]
510 ALZWORD <- ALZWORD%>% 510 ALZWORD <- ALZWORD%>%
511 as.data.frame()%>% 511 as.data.frame()%>%
512 dplyr::select(-starts_with("col")) 512 dplyr::select(-starts_with("col"))
513 513
514 ##Reorganizing information within the columns and final clinical data 514 ##Reorganizing information within the columns and final clinical data
515 ALZWORDF <- cinfo(ALZWORD) 515 ALZWORDF <- cinfo(ALZWORD)
516 516
517 517
518 #Working with Actual Data part of file 518 #Working with Actual Data part of file
519 alzdat <- alz %>% 519 alzdat <- alz %>%
520 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1) 520 read_delim(delim="\t",col_names=TRUE,comment = "!",skip=1)
521 ALZDAT <- t(alzdat[,-1]) 521 ALZDAT <- t(alzdat[,-1])
522 rownames(ALZDAT)=NULL 522 rownames(ALZDAT)=NULL
523 523
524 ##Is there a clean version of the GPL file available? 524 ##Is there a clean version of the GPL file available?
525 gplnum <- strsplit(genena,"[\\|/]") %>% 525 gplnum <- strsplit(genena,"[\\|/]") %>%
526 .[[1]] %>% 526 .[[1]] %>%
527 .[length(.)] %>% 527 .[length(.)] %>%
528 gsub("\\D","",.) 528 gsub("\\D","",.)
529 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files())) 529 clfileex <- sum(grepl(paste0("Clean_GPL",gplnum),list.files()))
530 if(clfileex >= 1){ 530 if(clfileex >= 1){
531 #use the clean version 531 #use the clean version
532 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>% 532 geneIDNam <- paste0("Clean_GPL",gplnum,".txt") %>%
533 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!") 533 read_delim(delim="\t",col_names = c("ID","Symbol"), comment = "!")
534 534
535 } else if(clfileex == 0){ 535 } else if(clfileex == 0){
536 ##Lets Create a clean version 536 ##Lets Create a clean version
537 537
538 ##Gene ID to Gene Name 538 ##Gene ID to Gene Name
539 if(soft == TRUE){ 539 if(soft == TRUE){
540 #Check to see if there is already a file containing information on soft files 540 #Check to see if there is already a file containing information on soft files
541 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files())) 541 fileex <- sum(grepl("GPL_ID_LOC.txt",list.files()))
542 if(fileex == 1){ 542 if(fileex == 1){
543 #Check to see if this GPL soft file has been used before 543 #Check to see if this GPL soft file has been used before
544 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 544 IDF <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
545 .$GPL_FILE_NUM%>% 545 .$GPL_FILE_NUM%>%
546 grepl(gplnum,.) %>% 546 grepl(gplnum,.) %>%
547 sum() 547 sum()
548 if(IDF == 1){ 548 if(IDF == 1){
549 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 549 IDLOCAL <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
550 .$GPL_FILE_NUM%>% 550 .$GPL_FILE_NUM%>%
551 grep(gplnum,.) 551 grep(gplnum,.)
552 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>% 552 idlocgpl <- read_delim("GPL_ID_LOC.txt",delim = "\t",col_names = TRUE) %>%
553 .$LOC_ID %>% 553 .$LOC_ID %>%
554 .[IDLOCAL] 554 .[IDLOCAL]
555 geneIDNam <- genena %>% 555 geneIDNam <- genena %>%
556 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>% 556 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idlocgpl) %>%
557 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 557 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
558 } else if(IDF == 0){ 558 } else if(IDF == 0){
559 #No information on this particular GPL file 559 #No information on this particular GPL file
560 idLOCGPL <- genena %>% 560 idLOCGPL <- genena %>%
561 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 561 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
562 t(.) %>% 562 t(.) %>%
563 grep("^ID\\s*$",.) %>% 563 grep("^ID\\s*$",.) %>%
564 -1 564 -1
565 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>% 565 cbind(as.integer(gplnum),as.integer(idLOCGPL)) %>%
566 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE) 566 cat(file="GPL_ID_LOC.txt",sep = "\t", fill = TRUE, append = TRUE)
567 geneIDNam <- genena %>% 567 geneIDNam <- genena %>%
568 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 568 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
569 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 569 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
570 } 570 }
571 } else if(fileex == 0){ 571 } else if(fileex == 0){
572 #We must create a file that we can access for later use 572 #We must create a file that we can access for later use
573 idLOCGPL <- genena %>% 573 idLOCGPL <- genena %>%
574 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>% 574 read_delim(delim="\t",col_names = FALSE, comment = "!", n_max = 1000) %>%
575 t(.) %>% 575 t(.) %>%
576 grep("^ID\\s*$",.) %>% 576 grep("^ID\\s*$",.) %>%
577 -1 577 -1
578 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL)) 578 Firstval <- cbind(as.integer(gplnum),as.integer(idLOCGPL))
579 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID") 579 colnames(Firstval) <- c("GPL_FILE_NUM","LOC_ID")
580 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE) 580 write.table(Firstval,file = "GPL_ID_LOC.txt", sep = "\t",row.names = FALSE, col.names = TRUE)
581 geneIDNam <- genena %>% 581 geneIDNam <- genena %>%
582 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>% 582 read_delim(delim="\t",col_names = TRUE, comment = "!", skip = idLOCGPL) %>%
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 } else if(soft == FALSE){ 585 } else if(soft == FALSE){
586 geneIDNam <- genena %>% 586 geneIDNam <- genena %>%
587 read_delim(delim="\t",comment = "#")%>% 587 read_delim(delim="\t",comment = "#")%>%
588 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.))) 588 dplyr::select(.,ID,grep("Symbol|^ORF\\s*$|^gene_assignment\\s*$",colnames(.)))
589 } 589 }
590 590
591 ##Labeling the gene IDs without names 591 ##Labeling the gene IDs without names
592 geneIDNam <- NAFIXING(geneIDNam) 592 geneIDNam <- NAFIXING(geneIDNam)
593 593
594 ##remove the whitespace 594 ##remove the whitespace
595 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,]))) 595 geneIDNam <- t(rbind(str_trim(t(geneIDNam)[1,]),str_trim(t(geneIDNam)[2,])))
596 596
597 ##Here is the clean version 597 ##Here is the clean version
598 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE) 598 write.table(geneIDNam,file = paste0("Clean_GPL",gplnum,".txt"),sep = "\t",row.names = FALSE, col.names = FALSE)
599 } 599 }
600 600
601 601
602 602
603 ##Changing the gene ID to gene name 603 ##Changing the gene ID to gene name
604 ALZDAT1 <- cgeneID(geneIDNam,alzdat) 604 ALZDAT1 <- cgeneID(geneIDNam,alzdat)
605 colnames(ALZDAT) = ALZDAT1[1,] 605 colnames(ALZDAT) = ALZDAT1[1,]
606 606
607 607
608 ##Adjusting the column names aka the gene names 608 ##Adjusting the column names aka the gene names
609 colnames(ALZDAT) <- gcnames(ALZDAT) 609 colnames(ALZDAT) <- gcnames(ALZDAT)
610 610
611 611
612 #Full RAW Data 612 #Full RAW Data
613 Fullalzdwr <- ALZDAT %>% 613 Fullalzdwr <- ALZDAT %>%
614 as.data.frame() %>% 614 as.data.frame() %>%
615 cbind(ALZWORDF,.) 615 cbind(ALZWORDF,.)
616 616
617 #Raw file is output 617 #Raw file is output
618 nfnaex <- strsplit(alz,"[\\]") %>% 618 nfnaex <- strsplit(alz,"[\\]") %>%
619 .[[1]] %>% 619 .[[1]] %>%
620 .[length(.)] %>% 620 .[length(.)] %>%
621 gsub("\\D","",.) %>% 621 gsub("\\D","",.) %>%
622 c("GSE",.,"aftexcel.txt") %>% 622 c("GSE",.,"aftexcel.txt") %>%
623 paste(collapse = "") 623 paste(collapse = "")
624 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t") 624 write.table(t(Fullalzdwr), file = nfnaex, sep = "\t")
625 625
626 626
627 627
628 #Now for the discretization part 628 #Now for the discretization part
629 ##get the wordy part again 629 ##get the wordy part again
630 rawword <- t(ALZWORDF) 630 rawword <- t(ALZWORDF)
631 631
632 ##where is ID_REF located 632 ##where is ID_REF located
633 hereim <- grep("ID_REF",rownames(rawword)) 633 hereim <- grep("ID_REF",rownames(rawword))
634 634
635 ##Subject Names GSM... 635 ##Subject Names GSM...
636 subjnam <- rawword[hereim,] 636 subjnam <- rawword[hereim,]
637 637
638 ##Getting the names for the rows 638 ##Getting the names for the rows
639 namedarows <- rownames(rawword)[-hereim] %>% 639 namedarows <- rownames(rawword)[-hereim] %>%
640 as.data.frame() 640 as.data.frame()
641 RAWWORD <- rawword[-hereim,] %>% 641 RAWWORD <- rawword[-hereim,] %>%
642 as.data.frame() %>% 642 as.data.frame() %>%
643 bind_cols(namedarows,.) 643 bind_cols(namedarows,.)
644 z <- 1 644 z <- 1
645 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE) 645 naroww <- as.data.frame(rep(0,dim(RAWWORD)[1]),stringsAsFactors = FALSE)
646 for(z in 1:dim(RAWWORD)[1]){ 646 for(z in 1:dim(RAWWORD)[1]){
647 if(sum(is.na(RAWWORD[z,])) > 0){ 647 if(sum(is.na(RAWWORD[z,])) > 0){
648 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,]))) 648 naroww[z,1] <- as.integer(sum(is.na(RAWWORD[z,])))
649 } 649 }
650 if(length(grep("NA",RAWWORD[z,])) > 0){ 650 if(length(grep("NA",RAWWORD[z,])) > 0){
651 naroww[z,1] <- as.integer(length(grep("NA",RAWWORD[z,]))) + naroww[z,1] 651 naroww[z,1] <- as.integer(length(grep("NA",RAWWORD[z,]))) + naroww[z,1]
652 } 652 }
653 z <- z + 1 653 z <- z + 1
654 } 654 }
655 655
656 colnames(naroww) <- "ROW_NAs" 656 colnames(naroww) <- "ROW_NAs"
657 RAWWORD <- bind_cols(RAWWORD,naroww) 657 RAWWORD <- bind_cols(RAWWORD,naroww)
658 658
659 659
660 roALZna <- t(ALZDAT) %>% 660 roALZna <- t(ALZDAT) %>%
661 rownames(.) %>% 661 rownames(.) %>%
662 as.data.frame(.) 662 as.data.frame(.)
663 colnames(roALZna) <- "ID_REF" 663 colnames(roALZna) <- "ID_REF"
664 664
665 RAWDAT <- t(ALZDAT) %>% 665 RAWDAT <- t(ALZDAT) %>%
666 as.data.frame(.) 666 as.data.frame(.)
667 colnames(RAWDAT) <- NULL 667 colnames(RAWDAT) <- NULL
668 rownames(RAWDAT) <- NULL 668 rownames(RAWDAT) <- NULL
669 669
670 RAWDAT2 <- RAWDAT %>% 670 RAWDAT2 <- RAWDAT %>%
671 cbind(roALZna,.) %>% 671 cbind(roALZna,.) %>%
672 dplyr::arrange(.,ID_REF) 672 dplyr::arrange(.,ID_REF)
673 673
674 ##Editing the file for R processing 674 ##Editing the file for R processing
675 RAWDATID <- RAWDAT2[,1] %>% 675 RAWDATID <- RAWDAT2[,1] %>%
676 as.matrix(.) 676 as.matrix(.)
677 677
678 RAWDATNUM <- RAWDAT2[,-1] %>% 678 RAWDATNUM <- RAWDAT2[,-1] %>%
679 mapply(.,FUN = as.numeric) %>% 679 mapply(.,FUN = as.numeric) %>%
680 t(.) 680 t(.)
681 681
682 ##Consolidating genes with the same name 682 ##Consolidating genes with the same name
683 ###create empty matrix of size equal to tabRDATID 683 ###create empty matrix of size equal to tabRDATID
684 tabRDATID <- table(RAWDATID) 684 tabRDATID <- table(RAWDATID)
685 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID)) 685 NuRDATN <- matrix(0, nrow = dim(RAWDATNUM)[1], ncol = length(tabRDATID))
686 j <- 1 686 j <- 1
687 for(j in 1:length(tabRDATID)){ 687 for(j in 1:length(tabRDATID)){
688 ##Putting the ones without duplicates in their new homes 688 ##Putting the ones without duplicates in their new homes
689 if(tabRDATID[j] == 1){ 689 if(tabRDATID[j] == 1){
690 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])] 690 NuRDATN[,j] <- RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])]
691 } else if(tabRDATID[j] > 1){ 691 } else if(tabRDATID[j] > 1){
692 ##Averaging duplicates and putting them in their new homes 692 ##Averaging duplicates and putting them in their new homes
693 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE) 693 NuRDATN[,j] <- rowMeans(RAWDATNUM[,which(RAWDATID==rownames(tabRDATID)[j])],na.rm = TRUE)
694 } 694 }
695 j <- j + 1 695 j <- j + 1
696 } 696 }
697 697
698 ##Scaling the Data 698 ##Scaling the Data
699 scrawdat <- NuRDATN%>% 699 scrawdat <- NuRDATN%>%
700 scale() 700 scale()
701 attr(scrawdat,"scaled:center") <- NULL 701 attr(scrawdat,"scaled:center") <- NULL
702 attr(scrawdat,"scaled:scale") <- NULL 702 attr(scrawdat,"scaled:scale") <- NULL
703 colnames(scrawdat) <- rownames(tabRDATID) 703 colnames(scrawdat) <- rownames(tabRDATID)
704 704
705 ##Discretized the Data 705 ##Discretized the Data
706 dialzdat <- scrawdat %>% 706 dialzdat <- scrawdat %>%
707 dndat(.) %>% 707 dndat(.) %>%
708 t()%>% 708 t()%>%
709 as.data.frame(.) 709 as.data.frame(.)
710 colnames(dialzdat) <- rownames(RAWDATNUM) 710 colnames(dialzdat) <- rownames(RAWDATNUM)
711 711
712 ##setting "ID_REF" as a new variable 712 ##setting "ID_REF" as a new variable
713 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1)) 713 geneNAM <- as.data.frame(as.matrix(rownames(dialzdat),ncol=1))
714 colnames(geneNAM) <- "ID_REF" 714 colnames(geneNAM) <- "ID_REF"
715 rownames(dialzdat) <- NULL 715 rownames(dialzdat) <- NULL
716 dialzdat <-bind_cols(geneNAM,dialzdat) 716 dialzdat <-bind_cols(geneNAM,dialzdat)
717 717
718 ##NAs in a column 718 ##NAs in a column
719 x <- 2 719 x <- 2
720 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE) 720 nacol <- as.data.frame(t(rep(0,dim(dialzdat)[2])),stringsAsFactors = FALSE)
721 nacol[1,1] = "COL_NAs" 721 nacol[1,1] = "COL_NAs"
722 for(x in 2:dim(dialzdat)[2]){ 722 for(x in 2:dim(dialzdat)[2]){
723 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x]))) 723 nacol[1,x] <- as.integer(sum(is.na(dialzdat[,x])))
724 x <- x + 1 724 x <- x + 1
725 } 725 }
726 colnames(nacol) <- colnames(dialzdat) 726 colnames(nacol) <- colnames(dialzdat)
727 dialzdat <- bind_rows(dialzdat,nacol) 727 dialzdat <- bind_rows(dialzdat,nacol)
728 728
729 ##NAs in a row 729 ##NAs in a row
730 y <- 1 730 y <- 1
731 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE) 731 narowd <- as.data.frame(rep(0,dim(dialzdat)[1]),stringsAsFactors = FALSE)
732 for(y in 1:dim(dialzdat)[1]){ 732 for(y in 1:dim(dialzdat)[1]){
733 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,]))) 733 narowd[y,1] <- as.integer(sum(is.na(dialzdat[y,])))
734 y <- y + 1 734 y <- y + 1
735 } 735 }
736 colnames(narowd) <- "ROW_NAs" 736 colnames(narowd) <- "ROW_NAs"
737 dialzdat <- bind_cols(dialzdat,narowd) 737 dialzdat <- bind_cols(dialzdat,narowd)
738 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam 738 colnames(dialzdat)[2:(dim(dialzdat)[2]-1)] <- subjnam
739 colnames(RAWWORD) <- colnames(dialzdat) 739 colnames(RAWWORD) <- colnames(dialzdat)
740 ##converting to character so that the clinical can be brought together with discrete data 740 ##converting to character so that the clinical can be brought together with discrete data
741 k <- 2 741 k <- 2
742 for(k in 2:dim(dialzdat)[2]-1){ 742 for(k in 2:dim(dialzdat)[2]-1){
743 dialzdat[,k] <- as.character(dialzdat[,k]) 743 dialzdat[,k] <- as.character(dialzdat[,k])
744 k <- k + 1 744 k <- k + 1
745 } 745 }
746 #The End the full data 746 #The End the full data
747 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat) 747 Dscrtalzdw <- bind_rows(RAWWORD,dialzdat)
748 748
749 #Produces Discrete file 749 #Produces Discrete file
750 nfnaex2 <- strsplit(alz,"[\\|/]") %>% 750 nfnaex2 <- strsplit(alz,"[\\|/]") %>%
751 .[[1]] %>% 751 .[[1]] %>%
752 .[length(.)] %>% 752 .[length(.)] %>%
753 gsub("\\D","",.) %>% 753 gsub("\\D","",.) %>%
754 c("GSE",.,"dscrt.txt") %>% 754 c("GSE",.,"dscrt.txt") %>%
755 paste(collapse = "") 755 paste(collapse = "")
756 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE) 756 write.table(Dscrtalzdw, file = nfnaex2, sep = "\t",col.names = TRUE,row.names = FALSE)
757 757
758 758
759 n <- n + 1 759 n <- n + 1
760 } 760 }
761 } 761 }
762 } 762 }
763 } 763 }
764 #The Rest of this code will be used every time you want to change a data set 764 #The Rest of this code will be used every time you want to change a data set
765 THEFT() 765 THEFT()