-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepping_meth_with_normals.R
165 lines (109 loc) · 4.88 KB
/
Prepping_meth_with_normals.R
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
setwd("~/Bioinformatics Work/Meth & RNA/Meth_overview")
##############
#### This file cleans and preps the meth file inlcuding the normal samples
### Including the normals will result in changes to the designation of the tertile etc
### which might effect the results, hence keeping two seperate files
##### Read in exp & clin file
exp <- read.table("Exp_inc_normals.txt", sep = "\t", header = TRUE, row.names = 1)
exp <- as.data.frame(t(exp))
clin <- read.table("Clinical_final_normals.txt", sep = "\t", header = TRUE, row.names = 1)
### Use the meth file that already has duplicate removed
Meth1 <- read.table("Fang_TSS200_all.txt", sep = "\t", header = TRUE)
######## read in the normal file
Norm1<- read.table("Fang_TSS200_all_norm.txt", sep = "\t", header = TRUE, row.names = 1)
##### check the colnames are matching and remove excess
list <- intersect(colnames(Meth1), colnames(Norm1))
Meth3 <- Meth1[,list]
### bind together as a set
Meth4 <- rbind(Norm1, Meth3)
##### Match rownames to exp
#### check samples match between files and trim
list <- intersect(row.names(Meth4), row.names(exp))
Meth4 <- Meth4[list,]
clin <- clin[list,]
exp <- exp[list,]
#### save out the new meth file
write.table(Meth4, "Fang_TSS200_all_norm_final.txt", sep = "\t")
######## set up the colmeans for meth set
# center with 'colMeans()'
center_colmeans <- function(x) {
xcenter = colMeans(x)
x - rep(xcenter, rep.int(nrow(x), ncol(x)))
}
# apply it
CentreMeth<- center_colmeans(Meth4)
Meth_Ave <- as.data.frame(rowMeans(CentreMeth))
rownames(Meth_Ave) <- rownames(Meth4)
colnames(Meth_Ave)[1] <- "MethScore"
#write.table(Meth_Ave, "Meth_beta_islands_ave.txt", sep = "\t")
### for 4 quartiles
brks <- with(Meth_Ave, quantile(MethScore, probs = c(0, 0.25, 0.5, 0.75, 1)))
Values <- within(Meth_Ave, quartile <- cut(MethScore, breaks = brks, labels = 1:4,
include.lowest = TRUE))
brks <- with(Meth_Ave, quantile(MethScore, probs = c(0, 0.33, 0.66, 1)))
Terts <- within(Meth_Ave, terts <- cut(MethScore, breaks = brks, labels = 1:3,
include.lowest = TRUE))
count(Values$quartile)
count(Terts$terts)
########### Set up dataframe for exports
look <- c("DNMT1", "DNMT3A", "DNMT3L", "DNMT3B", "UHRF1","MTHFR")
list <- match(look, names(exp))
exp_set <- exp[,list]
exp_set$Pam50 <- clin$PAM50Call_RNAseq
exp_set$Survival_time <- clin$OS_Time_nature2012
exp_set$Survival_event <- clin$OS_event_nature2012
exp_set$MethScore <- Meth_Ave$MethScore
exp_set$Quartile <- Values$quartile
exp_set$Tertile <- Terts$terts
write.table(exp_set, "Fang_TSS200_all_norm_exp_set.txt", sep = "\t")
#######################################################################################
### Use the meth file that already has duplicate removed
Meth1 <- read.table("Fang_TSS1500_all.txt", sep = "\t", header = TRUE)
######## read in the normal file
Norm1<- read.table("Fang_TSS1500_all_norm.txt", sep = "\t", header = TRUE, row.names = 1)
##### check the colnames are matching and remove excess
list <- intersect(colnames(Meth1), colnames(Norm1))
Meth3 <- Meth1[,list]
### bind together as a set
Meth4 <- rbind(Norm1, Meth3)
##### Match rownames to exp
#### check samples match between files and trim
list <- intersect(row.names(Meth4), row.names(exp))
Meth4 <- Meth4[list,]
clin <- clin[list,]
exp <- exp[list,]
#### save out the new meth file
write.table(Meth4, "Fang_TSS1500_all_norm_final.txt", sep = "\t")
######## set up the colmeans for meth set
# center with 'colMeans()'
center_colmeans <- function(x) {
xcenter = colMeans(x)
x - rep(xcenter, rep.int(nrow(x), ncol(x)))
}
# apply it
CentreMeth<- center_colmeans(Meth4)
Meth_Ave <- as.data.frame(rowMeans(CentreMeth))
rownames(Meth_Ave) <- rownames(Meth4)
colnames(Meth_Ave)[1] <- "MethScore"
#write.table(Meth_Ave, "Meth_beta_islands_ave.txt", sep = "\t")
### for 4 quartiles
brks <- with(Meth_Ave, quantile(MethScore, probs = c(0, 0.25, 0.5, 0.75, 1)))
Values <- within(Meth_Ave, quartile <- cut(MethScore, breaks = brks, labels = 1:4,
include.lowest = TRUE))
brks <- with(Meth_Ave, quantile(MethScore, probs = c(0, 0.33, 0.66, 1)))
Terts <- within(Meth_Ave, terts <- cut(MethScore, breaks = brks, labels = 1:3,
include.lowest = TRUE))
count(Values$quartile)
count(Terts$terts)
########### Set up dataframe for exports
look <- c("DNMT1", "DNMT3A", "DNMT3L", "DNMT3B", "UHRF1","MTHFR")
list <- match(look, names(exp))
exp_set <- exp[,list]
exp_set$Pam50 <- clin$PAM50Call_RNAseq
exp_set$Survival_time <- clin$OS_Time_nature2012
exp_set$Survival_event <- clin$OS_event_nature2012
exp_set$MethScore <- Meth_Ave$MethScore
exp_set$Quartile <- Values$quartile
exp_set$Tertile <- Terts$terts
write.table(exp_set, "Fang_TSS1500_all_norm_exp_set.txt", sep = "\t")
#######################################################################################