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