Created
October 15, 2021 13:53
-
-
Save TTTPOB/e2e8afcbf68a650864527f071e184fa9 to your computer and use it in GitHub Desktop.
when using deeptools computeMatrix, you wont be able to get the central point "central", you need to shift the point half the binsize, and add a new bin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
suppressPackageStartupMessages({ | |
library(tidyverse) | |
}) | |
args <- commandArgs(trailingOnly = TRUE) | |
bed <- args[1] | |
distshift <- args[2] %>% as.numeric() | |
chromsize <- args[3] | |
# debug only | |
# bed <- "ERV1.bed" | |
# distshift <- 2500 | |
# chromsize <- "../GRCm38_2019/GRCm38_2019_tdt/chrNameLength.txt" | |
chromsize <- read_table(chromsize, | |
col_names = c("seqnames", "length"), | |
col_types = cols() | |
) | |
chromsize_ <- chromsize$length | |
names(chromsize_) <- chromsize$seqnames | |
chromsize <- chromsize_ | |
rm(chromsize_) | |
splitandshiftplusminus <- function(bed) { | |
bedname <- bed %>% str_remove(".bed") | |
df <- read_table(bed, col_names = c( | |
"seqnames", | |
"start", | |
"end", | |
"name", | |
"score", | |
"strand" | |
), col_types = cols()) %>% | |
rowwise() %>% | |
mutate( | |
shift_dist = case_when( | |
strand == "+" ~ distshift, | |
strand == "-" ~ -distshift, | |
TRUE ~ distshift | |
), | |
new_start = start + shift_dist, | |
new_start = case_when( | |
new_start < 0 ~ 0, | |
new_start > (chromsize[seqnames] - 1) ~ | |
(chromsize[seqnames] - 1), | |
TRUE ~ new_start | |
), | |
new_end = new_start + 1, | |
strand = factor( | |
strand, | |
levels = c("+", "-"), | |
ordered = TRUE | |
) | |
) %>% | |
select(c( | |
seqnames, | |
new_start, | |
new_end, | |
name, | |
score, | |
strand | |
)) %>% | |
rename(start = new_start, end = new_end) %>% | |
ungroup() | |
df %>% | |
group_split(strand) %>% | |
imap(function(df, name) { | |
if (name == 1) { | |
filename <- paste0(bedname, "_plus.bed") | |
} else if (name == 2) { | |
filename <- paste0(bedname, "_minus.bed") | |
} else { | |
stop("strand not found") | |
} | |
df %>% write_tsv(file = filename, col_names = FALSE) | |
}) | |
return(NULL) | |
} | |
splitandshiftplusminus(bed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment