88# '
99# ' @export
1010# ' @return Data frame
11- add_metadata <- function (dat , cols = c(" sample_size" , " ncase" , " ncontrol" , " unit" , " sd" ))
12- {
11+ add_metadata <- function (dat , cols = c(" sample_size" , " ncase" , " ncontrol" , " unit" , " sd" )) {
1312 stopifnot(is.data.frame(dat ))
1413 stopifnot(" id.exposure" %in% names(dat ) | " id.outcome" %in% names(dat ))
15- get_info <- function (id , what = " exposure" , cols )
16- {
14+ get_info <- function (id , what = " exposure" , cols ) {
1715 info <- ieugwasr :: gwasinfo(id )
18- if (nrow(info ) == 0 )
19- {
16+ if (nrow(info ) == 0 ) {
2017 message(what , " : none of the IDs found in database" )
2118 return (NULL )
2219 }
23- for (col in cols )
24- {
25- if (! col %in% names(info ))
26- {
20+ for (col in cols ) {
21+ if (! col %in% names(info )) {
2722 info [[col ]] <- NA
2823 }
2924 }
3025 info <- subset(info , select = c(" id" , cols ))
3126 names(info ) <- paste0(names(info ), " ." , what )
3227 names(info )[names(info ) == paste0(" sample_size." , what )] <- paste0(" samplesize." , what )
33- if (" sample_size" %in% cols )
34- {
28+ if (" sample_size" %in% cols ) {
3529 index <- grepl(" ukb-d" , info $ id ) & is.na(info [[paste0(" samplesize." , what )]])
3630 info [[paste0(" samplesize." , what )]][index ] <- 300000
3731 }
@@ -40,42 +34,32 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit
4034
4135 order_col <- random_string()
4236 dat [[order_col ]] <- seq_len(nrow(dat ))
43- if (" id.exposure" %in% names(dat ))
44- {
37+ if (" id.exposure" %in% names(dat )) {
4538 exposure_id <- unique(dat [[" id.exposure" ]])
4639 info <- get_info(id = exposure_id , what = " exposure" , cols = cols )
47- if (! is.null(info ))
48- {
49- for (x in names(info ))
50- {
51- if (! x %in% names(dat ))
52- {
40+ if (! is.null(info )) {
41+ for (x in names(info )) {
42+ if (! x %in% names(dat )) {
5343 dat [[x ]] <- NA
5444 }
5545
56- for (id in unique(info [[" id.exposure" ]]))
57- {
46+ for (id in unique(info [[" id.exposure" ]])) {
5847 dat [[x ]][is.na(dat [[x ]]) & dat [[" id.exposure" ]] == id ] <- info [[x ]][info [[" id.exposure" ]] == id ]
5948 }
6049 }
6150 }
6251 }
6352
64- if (" id.outcome" %in% names(dat ))
65- {
53+ if (" id.outcome" %in% names(dat )) {
6654 outcome_id <- unique(dat [[" id.outcome" ]])
6755 info <- get_info(id = outcome_id , what = " outcome" , cols = cols )
68- if (! is.null(info ))
69- {
70- for (x in names(info ))
71- {
72- if (! x %in% names(dat ))
73- {
56+ if (! is.null(info )) {
57+ for (x in names(info )) {
58+ if (! x %in% names(dat )) {
7459 dat [[x ]] <- NA
7560 }
7661
77- for (id in unique(info [[" id.outcome" ]]))
78- {
62+ for (id in unique(info [[" id.outcome" ]])) {
7963 dat [[x ]][is.na(dat [[x ]]) & dat [[" id.outcome" ]] == id ] <- info [[x ]][info [[" id.outcome" ]] == id ]
8064 }
8165 }
0 commit comments