Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 61 additions & 45 deletions R/trackplot.R
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ read_coldata = function(bws = NULL, sample_names = NULL, build = "hg38", input_t
#' oct4_loci = "chr6:31125776-31144789"
#' t = track_extract(colData = cd, loci = oct4_loci, build = "hg19")
#' @export
track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10, nthreads = 1, query_ucsc = TRUE, gtf = NULL, build = "hg38", padding = 0, ideoTblName = "cytoBand"){
track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10, nthreads = 1, query_ucsc = TRUE, gtf = NULL, build = "hg38", padding = 0, ideoTblName = "cytoBand", verbose = FALSE){

if(is.null(colData)){
stop("Missing colData. Use read_coldata() to generate one.")
Expand Down Expand Up @@ -161,18 +161,22 @@ track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10,
end = max(unlist(lapply(etbl,function(x) attr(x, "end"))))
chr = unique(unlist(lapply(etbl,function(x) attr(x, "chr"))))
}else if(query_ucsc){
message("Querying UCSC genome browser for gene model and cytoband..")
etbl = .extract_geneModel_ucsc_bySymbol(genesymbol = gene, refBuild = build)
if(verbose) {
message("Querying UCSC genome browser for gene model and cytoband..")
}
etbl = .extract_geneModel_ucsc_bySymbol(genesymbol = gene, refBuild = build, verbose = FALSE)
chr = unique(as.character(etbl$chr)); start = min(as.numeric(etbl$start)); end = max(as.numeric(etbl$end))
if(length(chr) > 1){
message("Multiple chromosomes found! Using the first one ", chr[1])
if(verbose) {
message("Multiple chromosomes found! Using the first one ", chr[1])
}
etbl = etbl[chr %in% chr[1]]
chr = unique(as.character(etbl$chr)); start = min(as.numeric(etbl$start)); end = max(as.numeric(etbl$end))
}
if(!is.null(etbl)){
etbl = .make_exon_tbl(gene_models = etbl)
}
cyto = .extract_cytoband(chr = chr, refBuild = build)
cyto = .extract_cytoband(chr = chr, refBuild = build, verbose = FALSE)
loci = paste0(chr, ":", start, "-", end)
}else{
cyto = etbl = NA
Expand All @@ -181,24 +185,28 @@ track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10,
stop("No transcript models found for ", gene)
}
}else{
message("Parsing loci..")
if(verbose) {message("Parsing loci..")}
loci_p = .parse_loci(loci = loci)
chr = loci_p$chr; start = loci_p$start; end = loci_p$end
if(start >= end){
stop("End must be larger than Start!")
}
message(" Queried region: ", chr, ":", start, "-", end, " [", end-start, " bps]")
if(verbose) {
message(" Queried region: ", chr, ":", start, "-", end, " [", end-start, " bps]")
}
#Extract gene models for this region
if(!is.null(gtf)){
etbl = .parse_gtf(gtf = gtf, chr = chr, start = start, end = end)
cyto = NA
}else if(query_ucsc){
message("Querying UCSC genome browser for gene model and cytoband..")
etbl = .extract_geneModel_ucsc(chr, start = start, end = end, refBuild = build, txname = NULL, genename = NULL)
if(verbose) {
message("Querying UCSC genome browser for gene model and cytoband..")
}
etbl = .extract_geneModel_ucsc(chr, start = start, end = end, refBuild = build, txname = NULL, genename = NULL, verbose = FALSE)
if(!is.null(etbl)){
etbl = .make_exon_tbl(gene_models = etbl)
}
cyto = .extract_cytoband(chr = chr, refBuild = build, tblName = ideoTblName)
cyto = .extract_cytoband(chr = chr, refBuild = build, tblName = ideoTblName, verbose = FALSE)
}else{
cyto = etbl = NA
}
Expand All @@ -212,7 +220,7 @@ track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10,
custom_names = colData$bw_sample_names

if(input_bw){
windows = .gen_windows(chr = chr, start = start, end = end, window_size = binsize, op_dir = op_dir)
windows = .gen_windows(chr = chr, start = start, end = end, window_size = binsize, op_dir = op_dir, verbose = FALSE)
track_summary = .get_summaries(bedSimple = windows, bigWigs = input_files, op_dir = op_dir, nthreads = nthreads)
}else{
track_summary = .get_summaries_narrowPeaks(bigWigs = input_files, nthreads = nthreads, chr, start, end)
Expand All @@ -221,7 +229,7 @@ track_extract = function(colData = NULL, loci = NULL, gene = NULL, binsize = 10,
names(track_summary) = custom_names

attr(track_summary, "meta") = list(etbl = etbl, cyto = cyto, loci = loci)
message("OK!")
if(verbose) { message("OK!") }

list(data = track_summary, colData = colData)
}
Expand Down Expand Up @@ -812,7 +820,7 @@ track_plot = function(summary_list = NULL,
#' @export

profile_extract = function(colData = NULL, bed = NULL, ucsc_assembly = TRUE, startFrom = "start", binSize = 50,
up = 2500, down = 2500, pc_genes = TRUE, nthreads = 4){
up = 2500, down = 2500, pc_genes = TRUE, nthreads = 4, verbose = FALSE){
.check_windows()
.check_bwtool(warn = FALSE)
.check_dt()
Expand Down Expand Up @@ -842,8 +850,7 @@ profile_extract = function(colData = NULL, bed = NULL, ucsc_assembly = TRUE, sta
bed = .make_bed(bed = bed, op_dir = op_dir, up = as.numeric(up), down = as.numeric(up), tss = startFrom, for_profile = TRUE)
bed_annot = NA
}

message("Extracting signals..")
if(verbose) { message("Extracting signals..") }
mats = parallel::mclapply(bigWigs, function(x){
.bwt_mats(bw = x, binSize = binSize, bed = bed, size = paste0(up, ":", down), startFrom = startFrom, op_dir = op_dir)
}, mc.cores = nthreads)
Expand Down Expand Up @@ -872,7 +879,7 @@ profile_extract = function(colData = NULL, bed = NULL, ucsc_assembly = TRUE, sta
#' @param condition column name with conditions in `colData`. If provided summarizes signals from samples belonging to same group or condition
#' @seealso \code{\link{profile_extract}} \code{\link{profile_plot}} \code{\link{profile_heatmap}}
#' @export
profile_summarize = function(sig_list = NULL, stat = "mean", condition = NULL){
profile_summarize = function(sig_list = NULL, stat = "mean", condition = NULL, verbose = FALSE){

if(is.null(sig_list)){
stop("Missing input! Use profile_extract() to generate one.")
Expand All @@ -891,7 +898,7 @@ profile_summarize = function(sig_list = NULL, stat = "mean", condition = NULL){
collapse_replicates = TRUE
}

message("Summarizing..")
if(verbose) { message("Summarizing..") }
sig_summary = .summarizeMats(mats = sig_list$data, group = condition, collapse_reps = collapse_replicates, summarizeBy = stat)
attr(sig_summary, "args") = attr(sig_list$data, "args")
list(data = sig_summary, colData = colData)
Expand Down Expand Up @@ -1701,9 +1708,9 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
lo_h_ord[lord]
}

.gen_windows = function(chr = NA, start, end, window_size = 50, op_dir = getwd()){
.gen_windows = function(chr = NA, start, end, window_size = 50, op_dir = getwd(), verbose = FALSE){
#chr = "chr19"; start = 15348301; end = 15391262; window_size = 50; op_dir = getwd()
message(paste0("Generating windows ", "[", window_size, " bp window size]"))
if(verbose) { message(paste0("Generating windows ", "[", window_size, " bp window size]")) }

window_dat = data.table::data.table()
#temp = start;
Expand All @@ -1714,7 +1721,9 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
window_dat$chr = chr
window_dat = window_dat[, .(chr, start, end)]

print(window_dat)
if(verbose) {
print(window_dat)
}

op_dir = paste0(op_dir, "/")

Expand All @@ -1728,19 +1737,18 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
}


.get_summaries = function(bedSimple, bigWigs, op_dir = getwd(), nthreads = 1){
.get_summaries = function(bedSimple, bigWigs, op_dir = getwd(), nthreads = 1, verbose = FALSE){
#bedSimple = temp_op_bed; bigWigs = list.files(path = "./", pattern = "bw"); op_dir = getwd(); nthreads = 1
op_dir = paste0(op_dir, "/")

if(!dir.exists(paths = op_dir)){
dir.create(path = op_dir, showWarnings = FALSE, recursive = TRUE)
}

message(paste0("Extracting signals"))
if(verbose) { message(paste0("Extracting signals")) }

summaries = parallel::mclapply(bigWigs, FUN = function(bw){
bn = gsub(pattern = "\\.bw$|\\.bigWig$", replacement = "", x = basename(bw))
message(paste0(" Processing ", bn, " .."))
if(verbose) { message(paste0(" Processing ", bn, " ..")) }
cmd = paste("bwtool summary -with-sum -keep-bed -header", bedSimple, bw, paste0(op_dir, bn, ".summary"))
system(command = cmd, intern = TRUE)
paste0(op_dir, bn, ".summary")
Expand All @@ -1767,9 +1775,9 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
}


.extract_geneModel = function(ucsc_tbl = NULL, chr = NULL, start = NULL, end = NULL, txname = txname, genename = genename){
.extract_geneModel = function(ucsc_tbl = NULL, chr = NULL, start = NULL, end = NULL, txname = txname, genename = genename, verbose = FALSE){

message("Parsing UCSC file..")
if(verbose) { message("Parsing UCSC file..") }
if(is(object = ucsc_tbl, class2 = "data.frame")){
ucsc = data.table::as.data.table(ucsc_tbl)
}else if(file.exists(ucsc_tbl)){
Expand All @@ -1790,8 +1798,10 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
gene_models = data.table::foverlaps(x = query, y = ucsc, type = "any", nomatch = NULL)

if(nrow(gene_models) == 0){
message("No features found within the requested loci! If you are not sure why..\n 1.Make sure there are no discripancies in chromosome names i.e, chr prefixes\n")
return(NULL)
if(verbose) {
message("No features found within the requested loci! If you are not sure why..\n 1.Make sure there are no discripancies in chromosome names i.e, chr prefixes\n")
}
return(NULL)
}else{
if(!is.null(txname)){
gene_models = gene_models[name %in% txname]
Expand All @@ -1813,22 +1823,25 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
}
}

.extract_cytoband = function(chr = NULL, refBuild = "hg19", tblName = "cytoBand"){
.extract_cytoband = function(chr = NULL, refBuild = "hg19", tblName = "cytoBand", verbose = FALSE){

if(!grepl(pattern = "^chr", x = chr)){
message("Adding chr prefix to target chromosome for UCSC query..")
if(verbose) {
message("Adding chr prefix to target chromosome for UCSC query..")
}
chr = paste0("chr", chr)
}

cmd = paste0(
"mysql --user genome --host genome-mysql.soe.ucsc.edu -NAD ",
"mysql --user genome --host genome-mysql.soe.ucsc.edu --skip-ssl -NAD ",
refBuild,
" -e 'select chrom, chromStart, chromEnd, name, gieStain from ", tblName, " WHERE chrom =\"",
chr,
"\"'"
)
message(paste0("Extracting cytobands from UCSC:\n", " chromosome: ", chr, "\n", " build: ", refBuild, "\n query: ", cmd))

if(verbose){
message(paste0("Extracting cytobands from UCSC:\n", " chromosome: ", chr, "\n", " build: ", refBuild, "\n query: ", cmd))
}
cyto = data.table::fread(cmd = cmd, colClasses = c("character", "numeric", "numeric", "character", "character"))
colnames(cyto) = c("chr", "start", "end", "band", "stain")
data.table::setkey(x = cyto, chr, start, end)
Expand Down Expand Up @@ -1897,7 +1910,7 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =

.check_mysql()

cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu -NAD ", refBuild, " -e 'select chrom, chromStart, chromEnd, name from ", tbl, " WHERE chrom =\"", tar_chr, "\"'")
cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu --skip-ssl -NAD ", refBuild, " -e 'select chrom, chromStart, chromEnd, name from ", tbl, " WHERE chrom =\"", tar_chr, "\"'")
message(paste0("Extracting chromHMM from UCSC:\n", " chromosome: ", tar_chr, "\n", " build: ", refBuild, "\n query: ", cmd))
#system(command = cmd)
ucsc = data.table::fread(cmd = cmd)
Expand All @@ -1917,16 +1930,17 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
data.table::foverlaps(x = query, y = ucsc, type = "any", nomatch = NULL)[,.(chr, start, end, name)]
}

.extract_geneModel_ucsc_bySymbol = function(genesymbol, refBuild){
.extract_geneModel_ucsc_bySymbol = function(genesymbol, refBuild, verbose = FALSE){
.check_mysql()
op_file = tempfile(pattern = "ucsc", fileext = ".tsv")

cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2, exonStarts, exonEnds from refGene WHERE name2 =\"", genesymbol, "\"'")
message(paste0("Extracting gene models from UCSC:\n", " Gene: ", genesymbol, "\n", " build: ", refBuild, "\n query: ", cmd))

cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu --skip-ssl -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2, exonStarts, exonEnds from refGene WHERE name2 =\"", genesymbol, "\"'")
if(verbose) {
message(paste0("Extracting gene models from UCSC:\n", " Gene: ", genesymbol, "\n", " build: ", refBuild, "\n query: ", cmd))
}
ucsc = data.table::fread(cmd = cmd, sep = "\t")
if(nrow(ucsc) == 0){
message("No features found within the requested loci!")
if(verbose) { message("No features found within the requested loci!") }
return(NULL)
}

Expand All @@ -1935,19 +1949,21 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
ucsc
}

.extract_geneModel_ucsc = function(chr, start = NULL, end = NULL, refBuild = "hg19", txname = NULL, genename = NULL){
.extract_geneModel_ucsc = function(chr, start = NULL, end = NULL, refBuild = "hg19", txname = NULL, genename = NULL, verbose = FALSE){
.check_mysql()
op_file = tempfile(pattern = "ucsc", fileext = ".tsv")

if(!grepl(pattern = "^chr", x = chr)){
message("Adding chr prefix to target chromosome for UCSC query..")
if(verbose) { message("Adding chr prefix to target chromosome for UCSC query..") }
tar_chr = paste0("chr", chr)
}else{
tar_chr = chr
}

cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2, exonStarts, exonEnds from refGene WHERE chrom =\"", tar_chr, "\"'")
message(paste0("Extracting gene models from UCSC:\n", " chromosome: ", tar_chr, "\n", " build: ", refBuild, "\n query: ", cmd))
cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu --skip-ssl -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2, exonStarts, exonEnds from refGene WHERE chrom =\"", tar_chr, "\"'")
if(verbose) {
message(paste0("Extracting gene models from UCSC:\n", " chromosome: ", tar_chr, "\n", " build: ", refBuild, "\n query: ", cmd))
}
#system(command = cmd)
ucsc = data.table::fread(cmd = cmd)
if(nrow(ucsc) == 0){
Expand Down Expand Up @@ -2035,7 +2051,7 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =
}

.collapse_tx = function(exon_tbls){
message("Collapsing transcripts..")
#message("Collapsing transcripts..")
tx_tbl = lapply(exon_tbls, function(x){
xdt = data.table::data.table(start = x[[1]], end = x[[2]])
xdt$tx = attr(x = x, which = "tx")
Expand Down Expand Up @@ -2159,7 +2175,7 @@ summarize_homer_annots = function(anno, sample_names = NULL, legend_font_size =

temp_op_bed = tempfile(pattern = "profileplot_ucsc", tmpdir = op_dir, fileext = ".bed")

cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2 from refGene'")
cmd = paste0("mysql --user genome --host genome-mysql.soe.ucsc.edu --skip-ssl -NAD ", refBuild, " -e 'select chrom, txStart, txEnd, strand, name, name2 from refGene'")
message(paste0("Extracting gene models from UCSC:\n", " build: ", refBuild, "\n query: ", cmd))
#system(command = cmd)
ucsc = data.table::fread(cmd = cmd)
Expand Down
19 changes: 19 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
## trackplot - Fast and easy visualisation of bigWig files in R

## About this fork

This fork [JackieMium/trackplot](https://github.com/JackieMium/trackplot) was created to make it work for me. The most important change was adding a `--skip-ssl` to the internal `mysql` calls, fixing the issue that queries to servers were refused due to this check. Another change made was adding a `verbose = FALSE` argument to many internal calls for quite function calls and clean output.

This package requires the following packacges / softwares at runtime:

- MySQL or MariaDB database client for outgoing `mysql` calls to the UCSC server
- bwtool for manipulating bigWig files, check also my fork at [JackieMium/bwtool](https://github.com/JackieMium/bwtool)
- and the [data.table](https://cran.r-project.org/web/packages/data.table/index.html) R package

To install this fork, run in a R session:

```
remotes::install_github("JackieMium/trackplot")
```

Below is the original README comes with the package.
------------

<!-- badges: start -->
[![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/poisonalien/trackplot.svg)](https://github.com/poisonalien/trackplot/issues)
<!-- badges: end -->
Expand Down