-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute_tr_all_overlap.R
233 lines (202 loc) · 10.7 KB
/
compute_tr_all_overlap.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
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
###############
### This file is ment to be run on a HPC cluster.
### Place this file ~/swissrepeat/
### create on cluster:
### /scratch/IAS/AnisGroup/delucmat/swissrepeat/results/
### /scratch/IAS/AnisGroup/delucmat/swissrepeat/data/
### /scratch/IAS/AnisGroup/delucmat/swissrepeat/figures/
### dependencies in swissrepeat directory:
### - local_config.R
### - helpers.R
### dependencies in data directory:
### - tr_annotations.csv
### - swissprot_annotations.tsv
### - modib_coordinates.csv
### results saved in:
### /scratch/IAS/AnisGroup/delucmat/results/tr_all_overlap.csv
###############
###############
### TO DEBUG ON LOCAL MACHINE, UNCOMMENT THIS CHUNCK
### AND CONTINUE WITH PREPROCESSING
###############
# setwd("/home/matteo/polybox/MSc_ACLS/swissrepeat/results")
# source("local_config.R")
# setwd(paste0(local_base_path,"/results"))
#
# rm(list = ls(all = TRUE))
# gc()
# source("helpers.R")
# tr_path = "results/tr_annotations/tr_annotations.csv"
# sp_path = "data/swissprot_annotations.tsv"
# discoor_path = "results/disorder_annotations/mobidb_coordinates.csv"
# tr_all = load_tr_annotations(tr_path)
# sp_all = load_swissprot(sp_path, tr_all)
# discoor_all = load_disorder_annotations(discoor_path)
print("###############")
print("Housekeeping")
print("###############")
# setwd("/cfs/earth/scratch/delucmat/swissrepeat/")
# source("local_config_cluster.R")
# setwd(paste0(local_base_path))
print("Set working dir to local scratch on compute node by sourcing the local config file")
source("local_config_cluster.R")
setwd(local_base_path)
print(paste("working directory set to: ", local_base_path))
rm(list = ls(all = TRUE))
gc()
source("helpers_cluster.R")
print("load required packages and helper functions.")
print("###############")
print("Data Loading")
print("###############")
tr_path = "data/tr_annotations.csv"
tr_all = load_tr_annotations(tr_path)
print(paste("Tandem Repeats are loaded"), str(tr_all))
sp_path = "data/swissprot_annotations.tsv"
sp_all = load_swissprot(sp_path, tr_all)
print(paste("Swissprot Proteins are loaded", str(sp_all)))
discoor_path = "data/mobidb_coordinates.csv"
discoor_all = load_disorder_annotations(discoor_path)
print(paste("Disorder coordinates are loaded", str(discoor_all)))
print("###############")
print("Data Preprocessing")
print("###############")
tr_all_sp = merge(x = tr_all, y = sp_all, by = "ID", all.x = TRUE)
print(paste("meta-data from swissprot data is added to tandem repeats data", str(tr_all_sp)))
print("Add disorder information from modiDB")
discoor_all$disorder_region_length = (discoor_all$end - discoor_all$start) + 1
discoor_all$center = floor(discoor_all$start + (discoor_all$disorder_region_length/2))
discoor_all_sp = merge(x = discoor_all, y = sp_all, by = "ID", all.x = TRUE)
print("Remove eventual regions with incorrect coordinates")
discoor_all_sp = discoor_all_sp[(discoor_all_sp$center<discoor_all_sp$Length),]
# print("Aggregate disordered regions by protein, calculate disorder residues count in a protein")
# sp_protein= ddply(discoor_all, .(ID), summarize,
# disorder_count=(sum(disorder_region_length)))
# sp_protein = merge(x = sp_protein, y = sp_all, by = "ID", all.x = TRUE)
print("add to each TR a unique identification number: TR_id")
tr_all <- tr_all %>%
mutate(TR_id = row_number())
print("add to each TR the position in the protein where the TR sequence ends: end")
tr_all <- tr_all %>%
mutate(end = begin+total_repeat_length)
print("initallize for each TR an overlap column")
tr_all <- tr_all %>%
mutate(tail_overlap = 0,
head_overlap = 0,
DisinTR_overlap = 0,
TRinDis_overlap = 0,
total_overlap = 0)
print("add to each disorder region a unique identification number: DIS_id")
discoor_all <- discoor_all %>%
mutate(DIS_id = row_number())
print("###############")
print("Find disorder-overlaps in tandem repeats")
print("###############")
start_time <- Sys.time()
# determine the overlap of TR regions with disorder regions for each protein
# NOTE: if this works, I can remove all sp_all informations (sp_all, sp_protein, etc...)
for (IDprot in tr_all$ID[1:1000]){ # for each protein in swissprot which has a tandem repeat. Subset tr_all$ID[1:1000] takes about 53min
for (IDtr in tr_all$TR_id[which(tr_all$ID == IDprot)]){ # loop through all TRs of this protein
for (IDdis in discoor_all$DIS_id[which(discoor_all$ID == IDprot)]){ # extract all disorder regions from this protein
if (tr_all$end[which(tr_all$TR_id == IDtr)] > discoor_all$start[which(discoor_all$DIS_id == IDdis)] &
tr_all$begin[which(tr_all$TR_id == IDtr)] < discoor_all$start[which(discoor_all$DIS_id == IDdis)] &
discoor_all$end[which(discoor_all$DIS_id == IDdis)] >= tr_all$end[which(tr_all$TR_id == IDtr)])
{# check for each TR region if it has a tail-overlap with a disorder region
# ## This chunck is for debugging:
# print(paste("TR-id", IDtr,
# # "prot-id", IDprot,
# # "discoor-id", IDdis,
# # "discoor-protid", discoor_all$DIS_id[which(discoor_all$ID == IDprot)],
# "TR-start", tr_all$begin[which(tr_all$TR_id == IDtr)],
# "TR-end", tr_all$end[which(tr_all$TR_id == IDtr)],
# "discorr-start", discoor_all$start[which(discoor_all$DIS_id == IDdis)],
# "discorr-end", discoor_all$end[which(discoor_all$DIS_id == IDdis)],
# "tail_overlap", tr_all$end[which(tr_all$TR_id == IDtr)] - discoor_all$start[which(discoor_all$DIS_id == IDdis)]))
# }}}}
tr_all$tail_overlap[which(tr_all$TR_id == IDtr)] = tr_all$end[which(tr_all$TR_id == IDtr)] - discoor_all$start[which(discoor_all$DIS_id == IDdis)]
}
if (discoor_all$end[which(discoor_all$DIS_id == IDdis)] > tr_all$begin[which(tr_all$TR_id == IDtr)] &
discoor_all$end[which(discoor_all$DIS_id == IDdis)] < tr_all$end[which(tr_all$TR_id == IDtr)] &
discoor_all$start[which(discoor_all$DIS_id == IDdis)] <= tr_all$begin[which(tr_all$TR_id == IDtr)])
{ # or if it has a head-overlap with a disorder region
tr_all$head_overlap[which(tr_all$TR_id == IDtr)] = discoor_all$end[which(discoor_all$DIS_id == IDdis)] - tr_all$begin[which(tr_all$TR_id == IDtr)]
}
if (tr_all$begin[which(tr_all$TR_id == IDtr)] <= discoor_all$start[which(discoor_all$DIS_id == IDdis)] &
discoor_all$end[which(discoor_all$DIS_id == IDdis)] <= tr_all$end[which(tr_all$TR_id == IDtr)])
{ # or if the whole disorder region lies in the TR-region
tr_all$DisinTR_overlap[which(tr_all$TR_id == IDtr)] = discoor_all$disorder_region_length[which(discoor_all$DIS_id == IDdis)]
}
if (tr_all$begin[which(tr_all$TR_id == IDtr)] >= discoor_all$start[which(discoor_all$DIS_id == IDdis)] &
discoor_all$end[which(discoor_all$DIS_id == IDdis)] >= tr_all$end[which(tr_all$TR_id == IDtr)])
{ # or if the whole disorder region is bigger than the whole TR region. i.e. the whole TR region lies in a disorder region.
tr_all$TRinDis_overlap[which(tr_all$TR_id == IDtr)] = tr_all$total_repeat_length[which(tr_all$TR_id == IDtr)]
}
}
}
}
end_time <- Sys.time()
run_time <- end_time-start_time
print(paste("Time used to determine the overlap region of TRs with disorder regions:", run_time))
# check if correct
# tr_all[which(tr_all$TR_id == 172904), ] # No overlap
# discoor_all[which(discoor_all$ID == "P30443"),] # since overlap region of this protein don't fall into the TR above
#
# tr_all[which(tr_all$TR_id == 172906), ] # example for body overlap: [1] "TR-id 172906 TR-start 335 TR-end 356 discorr-start 335 discorr-end 365 tail_overlap 21"
# summarize overlap by TR (if one TR has multiple overlap, these are aggregated here)
tr_all$total_overlap <- rowSums(tr_all[,c("tail_overlap", "head_overlap", "TRinDis_overlap", "DisinTR_overlap")])
# save tr_overlap to file:
write.csv(tr_all, file = paste0(local_base_path, "data/tr_all_overlap.csv"))
print(paste0("TR overlap region information saved in: ", local_base_path, "data/tr_all_overlap.csv"))
#### Compute one-hot encoding for Upset plot
# summarize overlap by protein
overlap_by_protein= ddply(tr_all, .(ID), summarize, #Aggregate disordered regions by protein, calculate disorder residues count in a protein
total_TR_length_prot = sum(total_repeat_length),
total_disorder_length_prot = sum(disordered_overlap),
total_overlap_prot=sum(total_overlap),
tail_overlap_prot=sum(tail_overlap),
head_overlap_prot=sum(head_overlap),
DisinTR_overlap_prot=sum(DisinTR_overlap),
TRinDis_overlap_prot=sum(TRinDis_overlap))
sp_overlap = merge(x = overlap_by_protein, y = sp_all, by = "ID", all.x = TRUE)
listinputAA <- data.frame(matrix(ncol = 7, nrow = 0))
# one-hot-encode each AA by it's set
start_time <- Sys.time()
for (Prot in overlap_by_protein$ID) {
TR <- 0
while (TR < overlap_by_protein$total_TR_length_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),1,0,0,0,0,0))
TR <- TR+1
}
Dis <- 0
while (Dis < overlap_by_protein$total_disorder_length_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),0,1,0,0,0,0))
Dis <- Dis+1
}
headov <- 0
while (headov < overlap_by_protein$head_overlap_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),1,1,1,0,0,0))
headov <- headov+1
}
tailov <- 0
while (tailov < overlap_by_protein$tail_overlap_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),1,1,0,1,0,0))
tailov <- tailov+1
}
DisinTRov <- 0
while (DisinTRov < overlap_by_protein$DisinTR_overlap_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),1,1,0,0,1,0))
DisinTRov <- DisinTRov+1
}
TRinDisov <- 0
while (TRinDisov < overlap_by_protein$TRinDis_overlap_prot[which(overlap_by_protein$ID == Prot)]) {
listinputAA <- rbind(listinputAA, c(as.numeric(as.factor(Prot)),1,1,0,0,0,1))
TRinDisov <- TRinDisov+1
}
}
colnames(listinputAA) <- c("ID", "AA_in_TR", "AA_Disorder", "AA_in_headoverlap", "AA_in_tailoverlap", "AA_in_DisinTRoverlap", "AA_in_TRinDisoverlap")
end_time <- Sys.time()
run_time <- end_time-start_time
print(paste("Time used to one-hot-encode AA:", run_time))
# save listinputAA to file:
write.csv(listinputAA, file = paste0(local_base_path, "data/listinputAA.csv"))
print(paste0("AA one-hot-encoding saved in: ", local_base_path, "data/listinputAA.csv"))