|
| 1 | +library(data.table) |
| 2 | +library(dplyr) |
| 3 | + |
| 4 | +exc <- function(x) setdiff(x, c('SEX', 'CHIP', paste0('C', 1:8))) |
| 5 | +selected <- function(e){e<-e[!is.na(e)]; exc(unique(names(unlist(sapply(e, `[[`, 'selected')))))} |
| 6 | +selected.single <- function(e){e<-e[!is.na(e)]; paste(exc(unique(names(e$selected))), collapse = ',')} |
| 7 | + |
| 8 | +extract.df <- function(res, features, features.uniq) { |
| 9 | + |
| 10 | + cutoffs <- c(0.7, 0.9) |
| 11 | + pfers <- c(0.1, 1.0) |
| 12 | + |
| 13 | + do.call(rbind.data.frame, lapply(seq_along(res), function(i){ |
| 14 | + |
| 15 | + cutoff <- cutoffs[i] |
| 16 | + |
| 17 | + l <- lapply(seq_along(res[[i]]), function(j){ |
| 18 | + |
| 19 | + pfer <- pfers[j] |
| 20 | + |
| 21 | + feature.map <- data.frame(features=features, |
| 22 | + features.uniq=features.uniq, |
| 23 | + cell.line=sapply(strsplit(features, '\\|'), `[[`, 1), |
| 24 | + tf=sapply(strsplit(features, '\\|'), `[[`, 2), |
| 25 | + treatment=sapply(strsplit(features, '\\|'), `[[`, 3), |
| 26 | + hits=gsub('`', '', sapply(res[[i]][[j]], selected.single)), |
| 27 | + cutoff=cutoff, |
| 28 | + pfer=pfer, |
| 29 | + stringsAsFactors = F) |
| 30 | + |
| 31 | + feature.map <- transform(feature.map, hits=strsplit(hits, ',')) %>% unnest(hits) |
| 32 | + feature.map |
| 33 | + }) |
| 34 | + do.call(rbind.data.frame, l) |
| 35 | + })) |
| 36 | +} |
| 37 | + |
| 38 | +snp.pos <- fread('MS1_functional_positions.gen', data.table=F, col.names = c('snp', 'chr', 'pos')) |
| 39 | +snp.pos <- unique(snp.pos) |
| 40 | + |
| 41 | + |
| 42 | +de1.res <- readRDS('de1-results.Rds') |
| 43 | +de1 <- readRDS('de1.Rds') |
| 44 | +de1.df <- extract.df(de1.res, de1$features, de1$features.uniq) |
| 45 | +de1.df <- merge(de1.df, snp.pos, by.x = 'hits', by.y='snp', all.x = T) |
| 46 | +write.table(de1.df, 'de1-results.tsv', row.names = F, quote = F, sep='\t') |
| 47 | + |
0 commit comments