-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
352 lines (327 loc) · 11.2 KB
/
server.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
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
if (!("pacman" %in% rownames(installed.packages()))) {install.packages("pacman")}
pacman::p_load(shiny,
tidytable,
tidyverse,
data.table,
htmlwidgets,
DT,
kableExtra,
reshape2,
dtplyr,
rio,
janitor)
options(shiny.maxRequestSize=50*1024^2)
'%!in%' <- function(x,y)!('%in%'(x,y))
findoverlapping_segments <- function(dataset, cM = 7, name = NULL, exclude=NULL){
dataset %>%
lazy_dt() %>%
filter(CENTIMORGANS > cM) %>%
filter(MATCHNAME %!in% exclude) %>%
as.data.table() ->
dataset
setkey(dataset, CHROMOSOME, `START LOCATION`, `END LOCATION`)
if((is_empty(name)) == T){
olaps = foverlaps(dataset, dataset, type="any", which=FALSE) %>%
lazy_dt() %>%
filter(MATCHNAME != i.MATCHNAME) %>%
select(1:9) %>%
distinct() %>%
as.data.table()
}else{
dataset_name <- dataset %>%
lazy_dt() %>%
filter(MATCHNAME %in% name) %>%
as.data.table()
olaps = foverlaps(dataset_name, dataset, type="any", which=FALSE)
olaps %>%
lazy_dt() %>%
filter(MATCHNAME != i.MATCHNAME) %>%
as.data.table() -> olaps
olaps[,c(1:9)] -> olaps1
olaps[,c(1,10:17)] -> olaps2
colnames(olaps2) <- colnames(olaps1)
olaps2 %>% lazy_dt() %>% full_join(olaps1) %>% distinct() %>%
#mutate(sorter = !(MATCHNAME %in% name)) %>%
as.data.table -> olaps
setkey(olaps, CHROMOSOME, `START LOCATION`, #sorter,
`END LOCATION`)
olaps %>% #select.(-sorter) %>%
as.data.table() -> olaps
}
olaps %>%
lazy_dt() %>%
mutate(CHROMOSOME = CHROMOSOME %>% factor(labels = c(1:22, "X") %>% as.character(),
levels = c(1:22, "X") %>% as.character(),
ordered = T)) %>%
arrange(CHROMOSOME,
`START LOCATION`) %>%
select(NAME,
MATCHNAME,
CHROMOSOME,
`START LOCATION`,
`END LOCATION`,
CENTIMORGANS,
`MATCHING SNPS`,
`Shared cM`,
`Longest Block`) %>%
as.data.table() -> output
return(output)
}
# overlap_in_lists <- function(out){if(length(unique(out$NAME)) > 1){
# out %>%
# lazy_dt() %>%
# distinct(MATCHNAME, NAME) %>%
# group_by(MATCHNAME) %>%
# summarise(n = n()) %>%
# ungroup() %>%
# filter(n > 1) %>%
# as.data.table() %>%
# pull(MATCHNAME) %>%
# c(unique(out$NAME)) ->
# uniques_shared_matches
#
# out <- out %>% lazy_dt() %>% filter(MATCHNAME %in% uniques_shared_matches) %>% as.data.table()
# out}
# else{out}}
import_custom <- function(x){
# 10 cols = MyHeritage
# 6 cols = FTDNA
# ? cols = DNAGedcom FTDNA
# ? cols = DNAGedcom MyHeritage
# ? cols = Gedmatch
imported <- import(x, encoding = "UTF-8", setclass="data.table", blank.lines.skip = TRUE)
if(ncol(imported) == 6) {
imported %>%
lazy_dt() %>%
mutate_at(1:2, trimws) %>%
mutate(Name = NA_character_) %>%
transmute(NAME = Name,
MATCHNAME = `Match Name`,
CHROMOSOME = Chromosome,
`START LOCATION` = `Start Location`,
`END LOCATION` = `End Location`,
CENTIMORGANS = Centimorgans,
`MATCHING SNPS` = `Matching SNPs`) %>%
as.data.table() ->
imported
}
if(ncol(imported) == 10){
imported %>%
lazy_dt() %>%
mutate_at(1:3, trimws) %>%
select(2, 3, 4, 5, 6, 9, 10) %>%
distinct() %>%
as.data.table() ->
imported
colnames(imported) <- c("NAME",
"MATCHNAME",
"CHROMOSOME",
"START LOCATION",
"END LOCATION",
"CENTIMORGANS",
"MATCHING SNPS")
imported %>%
lazy_dt() %>%
filter(!is.na(CHROMOSOME)) %>%
mutate(CHROMOSOME = as.character(CHROMOSOME)) %>%
as.data.table() ->
imported
}
return(imported)
}
shinyServer(function(input, output, session) {
inFile <- reactive({
if (is.null(input$file)) {
return(NULL)
} else {
input$file
}
})
inFile2 <- reactive({
if (is.null(input$file2)) {
return(NULL)
} else {
input$file2
}
})
importData <- reactive({
if (is.null(inFile())) {
return(NULL)
} else {
rbindlist(lapply(inFile()$datapath, import_custom)) ->
dat1
data.table(MATCHNAME = unique(dat1$MATCHNAME)) %>%
lazy_dt() %>%
mutate(MATCHNAME2 = MATCHNAME %>% str_replace_all(" ", " ") %>% str_replace_all(" ", " ") %>% str_replace_all(" ", " ")) %>%
full_join(., dat1) %>%
select(NAME, everything()) %>%
select(-MATCHNAME) %>%
rename(MATCHNAME = MATCHNAME2) %>%
as.data.table() %>%
#nest_by.(NAME) %>%
#lazy_dt() %>%
#mutate(NAME = NAME %>% str_remove("\"")) %>%
#as.data.table() %>%
#unnest.() %>%
lazy_dt() %>%
group_by(#NAME,
MATCHNAME) %>%
mutate(`Shared cM`= sum(CENTIMORGANS*(CHROMOSOME != "X")),
`Longest Block` = max(CENTIMORGANS*(CHROMOSOME != "X"))) %>%
ungroup() %>%
mutate(`Shared cM` = `Shared cM` %>% round(2),
`Longest Block` = `Longest Block` %>% round(2)) %>%
as.data.table() -> out
# out <- overlap_in_lists(out)
out}
})
## DEPRECATED
# matchesData <- reactive({
# if (is.null(input$file2)) {
# return(NULL)
# } else {rbindlist(lapply(inFile2()$datapath, import,
# #`Match Date` = col_date(format = "%m/%d/%Y"),
# encoding = "UTF-8")) %>% #na = c("N/A","")
# # group_by(`Full Name`) %>% dplyr::filter(`Match Date` == min(`Match Date`)) %>% ungroup %>% #Unnecessary
# mutate(MATCHNAME=`Full Name` %>%
# gsub(" "," ", x = .) %>% gsub(" "," ", x = .) %>% gsub(" "," ", x = .)) %>%
# select(MATCHNAME, `Match Date`,`Relationship Range`,`Suggested Relationship`,`Shared cM`,`Longest Block`,`Email`,`Ancestral Surnames`,`Y-DNA Haplogroup`,`mtDNA Haplogroup`) %>%
# mutate(`Shared cM`=`Shared cM` %>% signif(digits=2),
# `Longest Block`=`Longest Block` %>% signif(digits=2))}})
#
# names <- reactive({
# uniques <- data.frame(names=unique(importData()$NAME))
# if(1<nrow(uniques))
# {uniques$names}
# else
# {NULL}
# }
# )
segments <- reactive({
if (is.null(inFile())) {
return(NULL)
} else {#if(is.null(matchesData())){
findoverlapping_segments(dataset = importData(),
cM=input$cM,
name = input$name %>% as.vector(),
exclude = input$exclude %>% as.vector()) %>%
lazy_dt() %>%
transmute(NAME,
MATCHNAME,
CHR=CHROMOSOME,
START = `START LOCATION`,
END = `END LOCATION`,
CENTIMORGANS,
`MATCHING SNPS`) %>%
as.data.table() ->
out
#out <- overlap_in_lists(out)
out#}
# else{
# findoverlapping_segments(dataset = importData(),
# cM = input$cM,
# name = input$name %>% as.vector(),
# exclude = input$exclude %>% as.vector()) %>%
# lazy_dt() %>%
# transmute(NAME,
# MATCHNAME,
# CHR=CHROMOSOME,
# START = `START LOCATION`,
# END = `END LOCATION`,
# CENTIMORGANS,
# `MATCHING SNPS`,
# `Shared cM`,
# `Longest Block`) %>%
# left_join(matchesData()) %>%
# select(-`Ancestral Surnames`,
# -`Y-DNA Haplogroup`,
# -`mtDNA Haplogroup`,
# -`Shared cM`,
# -`Longest Block`,
# -`Suggested Relationship`,
# -`Shared cM`,
# -`Longest Block`) %>%
# as.data.table() ->
# out
# out <- overlap_in_lists(out)
# out}
}
})
segments_out <- reactive({
if (is.null(inFile())) {
return(NULL)
} else {if (is.null(matchesData())) {
findoverlapping_segments(dataset = importData(),
cM = input$cM,
name = input$name %>% as.vector(),
exclude = input$exclude %>% as.vector()) %>%
transmute(NAME,
MATCHNAME,
CHROMOSOME,
`START LOCATION`,
`END LOCATION`,
CENTIMORGANS,
`MATCHING SNPS`)}else{
out <- importData() %>%
findoverlapping_segments(cM = input$cM,
name = input$name %>% as.vector(),
exclude = input$exclude %>% as.vector()) %>%
lazy_dt() %>%
left_join(matchesData()) %>%
select(-`Ancestral Surnames`,
-`Y-DNA Haplogroup`,
-`mtDNA Haplogroup`,
-`Shared cM`,
-`Longest Block`,
-`Suggested Relationship`) %>%
as.data.table() -> out
#out <- overlap_in_lists(out)
out}
}
})
observe({updateSelectizeInput(
session,
"name",
choices=importData()$MATCHNAME, #selected = names(),
server = TRUE)})
observe({updateSelectizeInput(
session,
"exclude",
choices=importData()$MATCHNAME, server = TRUE)})
observe({
output$table <- DT::renderDataTable({ if (is.null(inFile())) {
return(NULL)
} else {DT::datatable(
segments(),
filter = 'top', extensions = c('Buttons', 'Scroller'),
options = list(scrollY = 650,
scrollX = 500,
deferRender = TRUE,
scroller = TRUE,
buttons = list('excel', "csv"),
dom = 'lBfrtip',
fixedColumns = TRUE),
rownames = FALSE,
selection = list(mode="multiple", target = "row", selected = which(segments()$MATCHNAME %in% {input$name %>% as.vector()})))}}, server = TRUE)})
# observe({
# output$downloadData_csv <- downloadHandler(
# filename = "overlapping segments.csv",
# content = function(file) {
# write.csv(segments_out(),
# file,
# row.names =
# FALSE, eol = "\r\n")
# }
# )
# })
# observe({output$downloadData_xlsx <- downloadHandler(
# filename="overlapping segments.xlsx",
# content = function(file){
# xlsx::write.xlsx(segments_out(),
# file,
# sheetName = "Overlapping segments",
# row.names = FALSE)
# }
# )
# })
})