From f2122760ab8b9ef07f9161544c25b7da3e82cd94 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Tue, 16 Jan 2024 16:36:31 -0500 Subject: [PATCH 1/8] Fixes Different Json creation when using annotations --- R/gGnome.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/R/gGnome.R b/R/gGnome.R index 8a77fb65..8c37d2ab 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -5888,7 +5888,7 @@ gGraph = R6::R6Class("gGraph", ed$annotation = .dtstring(ed[, intersect(names(ed), annotations), with = FALSE]) } ed$from = private$pnodes$snode.id[ed$from] - ed$to = -private$pnodes$snode.id[ed$to] + ed$to = private$pnodes$snode.id[ed$to] } } @@ -5901,9 +5901,7 @@ gGraph = R6::R6Class("gGraph", if (yf %in% names(private$pedges)) ed$weight = private$pedges[.(ed$sedge.id), yf, with = FALSE] - } - else - { + } else { no.y = TRUE } @@ -5957,10 +5955,10 @@ gGraph = R6::R6Class("gGraph", # KSK: temporary fix for old-style notation (large values instead of negatives) # of whether edge is going out/coming in from left or right side of node # so that json is rendered correctly in gGnome.js: - # max.node.id <- max(self$dt$node.id) - # ed[from > max.node.id, from := (from - max.node.id)*-1] - # ed[to <= max.node.id, to := -1*to] - # ed[to > max.node.id, to := (to - max.node.id)] + max.node.id <- max(self$dt$node.id) + ed[from > max.node.id, from := (from - max.node.id)*-1] + ed[to <= max.node.id, to := -1*to] + ed[to > max.node.id, to := (to - max.node.id)] # KSK end ## TODO: do not assume things are paired up From d2a4819a00852e303636986c7fadd682b5ca9255 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Mon, 22 Jan 2024 13:42:08 -0500 Subject: [PATCH 2/8] Fixes walks json by adding unique identifiers for each node of a walk --- R/gGnome.R | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/R/gGnome.R b/R/gGnome.R index 8c37d2ab..9e543115 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -7878,17 +7878,33 @@ gWalk = R6::R6Class("gWalk", ## GWALKS warning(sprintf('Invalid efields value/s provided: "%s". These fields were not found in the gWalk and since will be ignored.', paste(missing_efields, collapse = '" ,"'))) efields = intersect(efields, names(self$edges$dt)) } - +############ temporary fix for json unique iids/cids - SC start sedu = dunlist(self$sedge.id) - cids = lapply(unname(split(cbind(data.table(cid = sedu$V1, - source = self$graph$edges[sedu$V1]$left$dt$snode.id, - sink = -self$graph$edges[sedu$V1]$right$dt$snode.id, # notice that we need to add negative sign here to meet the gGnome.js expectations - title = "", - weight = 1), - self$graph$edges[sedu$V1]$dt[, ..efields] - ), sedu$listid)), + sedu[,cid := 1:.N, by = "listid"] + sedu[,source := self$graph$edges[sedu$V1]$left$dt$snode.id] + sedu[,sink := -self$graph$edges[sedu$V1]$right$dt$snode.id] + sedu[,source2 := ifelse(source < 0, -cid, cid)] + ## sedu[,sink2 := ifelse(sink < 0, -cid, cid)] + sedu[,sink2 := ifelse(sink < 0, (-cid - 1), (cid + 1))] + ## old method of creating cids + ## cids = lapply(unname(split(cbind(data.table(cid = sedu$V1, + ## source = self$graph$edges[sedu$V1]$left$dt$snode.id, + ## sink = -self$graph$edges[sedu$V1]$right$dt$snode.id, # notice that we need to add negative sign here to meet the gGnome.js expectations + ## title = "", + ## weight = 1), + ## self$graph$edges[sedu$V1]$dt[, ..efields] + ## ), sedu$listid)), + ## function(x) unname(split(x, 1:nrow(x)))) + cids = lapply(unname(split(cbind(data.table(cid = sedu$cid, + source = sedu$source2, + sink = sedu$sink2, # notice that we need to add negative sign here to meet the gGnome.js expectations + title = "", + weight = 1), + self$graph$edges[sedu$V1]$dt[, ..efields] + ), sedu$listid)), function(x) unname(split(x, 1:nrow(x)))) - + #browser() +############ temporary fix for json unique iids/cids - SC end snu = dunlist(self$snode.id) snu$ys = gGnome:::draw.paths.y(self$grl) %>% unlist @@ -7904,8 +7920,13 @@ gWalk = R6::R6Class("gWalk", ## GWALKS warning(sprintf('Invalid nfields value/s provided: "%s". These fields were not found in the gWalk and since will be ignored.', paste(missing_nfields, collapse = '" ,"'))) nfields = intersect(nfields, names(self$edges$dt)) } + #########temporary fix for json unique iids - SC start + snu[,iid := 1:.N, by = "listid"] + #############SC end + ## iids = lapply(unname(split(cbind( + ## data.table(iid = abs(snu$V1)), iids = lapply(unname(split(cbind( - data.table(iid = abs(snu$V1)), + data.table(iid = abs(snu$iid)), #modified this to iid - SC self$graph$nodes[snu$V1]$dt[, .(chromosome = seqnames, startPoint = start, @@ -7913,15 +7934,13 @@ gWalk = R6::R6Class("gWalk", ## GWALKS y = snu$ys, type = "interval", strand = ifelse(snu$V1 > 0, "+", "-"), - title = abs(snu$V1))], + title = abs(snu$iid))],#title = abs(snu$V1))], self$graph$nodes[snu$V1]$dt[,..nfields]), snu$listid)), function(x) unname(split(x, 1:nrow(x)))) - walks.js = lapply(1:length(self), function(x) c(as.list(pids[[x]]), list(cids = rbindlist(cids[[x]])), list(iids = rbindlist(iids[[x]])))) - if (include.graph){ out = c(graph.js, list(walks = walks.js)) } else { From 562f56c59efff0181e68567ccf326341e9b4e6b8 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Tue, 30 Jan 2024 12:10:03 -0500 Subject: [PATCH 3/8] Speeds up creation of arrow files by not printing every single bin --- R/jsUtils.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/jsUtils.R b/R/jsUtils.R index 79e73132..06d484e0 100644 --- a/R/jsUtils.R +++ b/R/jsUtils.R @@ -1112,7 +1112,8 @@ get_pgv_data_dir = function(outdir, patient.id = NA){ cov2cov.js = function(cov, meta.js = NULL, js.type = 'gGnome.js', field = 'ratio', bin.width = NA, ref = NULL, cov.color.field = NULL, convert.to.cn = TRUE, ncn.gr = NA, gg = NA){ - message(paste0("reading in file: ", cov)) + message(paste0("reading in file")) + #message(paste0("reading in file: ", cov)) x = readCov(cov) overlap.seqnames = seqlevels(x) From 5453a5e857458e883529fa198e8a59c53c8f78e1 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Wed, 31 Jan 2024 13:46:58 -0500 Subject: [PATCH 4/8] Fixes get(cid.field) error when the genome graph only has loose ends and no other edges --- R/gGnome.R | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/R/gGnome.R b/R/gGnome.R index 9e543115..a1c03239 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -5934,9 +5934,11 @@ gGraph = R6::R6Class("gGraph", ## remove zero or NA weight loose ends loose.ed = loose.ed[!is.na(weight), ][weight>0, ] ## add cid values for loose ends - max.cid = max(ed[, get(cid.field)], na.rm = T) - loose.ed[, (cid.field) := 1:.N + max.cid] - loose.ed[, class := ''] + if(nrow(ed) > 0) { #added this fix for genomes with only loose ends + max.cid = max(ed[, get(cid.field)], na.rm = T) + loose.ed[, (cid.field) := 1:.N + max.cid] + loose.ed[, class := ''] + } if (!is.null(annotations)) loose.ed$annotation = '' @@ -5969,9 +5971,12 @@ gGraph = R6::R6Class("gGraph", ## if any edge left, process if (nrow(ed)>0){ - ## EDGE.JSON + ## EDGE.JSON ed[is.na(weight), weight := 0] - + if(!(cid.field %in% names(ed))) { + ed[,cid.field] = ed$from #sc - added this fix for genome with no edges other than loose ends + } + ed.json = ed[, .(cid = get(cid.field), source = from, From 039559af70ce1f0564bd13b393d318f1a3203f24 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Mon, 12 Feb 2024 19:39:05 -0500 Subject: [PATCH 5/8] Coerces col (for the ggraph json function), which is used in gtrack to color nodes, to color which is the name to color nodes in pgv --- R/gGnome.R | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/R/gGnome.R b/R/gGnome.R index a1c03239..a757b519 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -5838,8 +5838,9 @@ gGraph = R6::R6Class("gGraph", if (length(nfields)) { - nfields = setdiff(intersect(nfields, names(self$nodes$dt)), names(node.json)) - node.json = cbind(node.json, gr2dt(self$nodes$gr)[, nfields, with = FALSE]) + nfields = setdiff(intersect(nfields, names(self$nodes$dt)), names(node.json)) + node.json = cbind(node.json, gr2dt(self$nodes$gr)[, nfields, with = FALSE]) + } .dtstring = function(dt) dt[, gsub('\\|+', '|', gsub('\\|+$', '', gsub('^\\|+', '', do.call(paste, c(lapply(names(.SD), function(x) ifelse(!is.na(.SD[[x]]), paste0(x, '=', .SD[[x]]), '')), sep = '|')))))] @@ -5847,14 +5848,14 @@ gGraph = R6::R6Class("gGraph", if (!is.null(annotations)) { nodes.overlap.annotations = intersect(annotations, names(values(self$nodes$gr))) - if (length(nodes.overlap.annotations) == 0){ + if (length(nodes.overlap.annotations) == 0) { warning('There is no overlap between the provided annotations and the annotaions available in the nodes in your gGraph.') node.json$annotation = '' } else { node.json = cbind(node.json, data.table(annotation = .dtstring(as.data.table(values(self$nodes$gr))[, intersect(annotations, names(values(self$nodes$gr))), with = FALSE]))) } } - + if (is.null(cid.field)){ cid.field = 'sedge.id' } else { @@ -5968,7 +5969,6 @@ gGraph = R6::R6Class("gGraph", node.json$title = as.character(node.json$iid) node.json[, type := "interval"] node.json[, strand := "*"] - ## if any edge left, process if (nrow(ed)>0){ ## EDGE.JSON @@ -6011,7 +6011,13 @@ gGraph = R6::R6Class("gGraph", weight = numeric(0)) } - ## append list of node metadata features if nfields is specified + ## append list of node metadata features if nfields is specified + #convert name from col (used in gtrack) to color for the nodes + if("col" %in% names(node.json)) { + node.json[, color := col] + node.json[, col := NULL] + nfields = gsub("\\bcol\\b", "color",nfields) #replace only entries that start and end with col + } if (length(nfields)) { .fix = function(x) as.list(x)[!is.na(unlist(x))] @@ -7908,7 +7914,6 @@ gWalk = R6::R6Class("gWalk", ## GWALKS self$graph$edges[sedu$V1]$dt[, ..efields] ), sedu$listid)), function(x) unname(split(x, 1:nrow(x)))) - #browser() ############ temporary fix for json unique iids/cids - SC end snu = dunlist(self$snode.id) snu$ys = gGnome:::draw.paths.y(self$grl) %>% unlist From 62531fdbb7422dfb45278cf0ad2a722c149a575b Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Fri, 8 Mar 2024 11:33:11 -0500 Subject: [PATCH 6/8] make genome json work with objects without edges and weights --- R/gGnome.R | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/R/gGnome.R b/R/gGnome.R index a757b519..ed63a6b9 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -5892,8 +5892,8 @@ gGraph = R6::R6Class("gGraph", ed$to = private$pnodes$snode.id[ed$to] } } - yf = NULL + if (!no.y && !is.null(yf <- self$meta$y.field) && yf %in% names(values(self$nodes$gr))) { settings$y.axis = yf @@ -5905,7 +5905,7 @@ gGraph = R6::R6Class("gGraph", } else { no.y = TRUE } - + ## make loose end nodes if (length(self$loose)>0) { @@ -5933,7 +5933,10 @@ gGraph = R6::R6Class("gGraph", )[, type := 'LOOSE'] ## remove zero or NA weight loose ends - loose.ed = loose.ed[!is.na(weight), ][weight>0, ] + if("weight" %in% names(loose.ed)) { #fix for ggraphs to be plotted as intervals + loose.ed = loose.ed[!is.na(weight), ][weight>0, ] + } + ## add cid values for loose ends if(nrow(ed) > 0) { #added this fix for genomes with only loose ends max.cid = max(ed[, get(cid.field)], na.rm = T) @@ -5972,7 +5975,11 @@ gGraph = R6::R6Class("gGraph", ## if any edge left, process if (nrow(ed)>0){ ## EDGE.JSON - ed[is.na(weight), weight := 0] + if("weight" %in% names(loose.ed)) { #fix for ggraphs to be plotted as intervals + ed[is.na(weight), weight := 0] + } else { + ed$weight = 0 + } if(!(cid.field %in% names(ed))) { ed[,cid.field] = ed$from #sc - added this fix for genome with no edges other than loose ends } From 200ec4487cc69185ca39f93155c8cf7267699929 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Mon, 11 Mar 2024 18:48:50 -0400 Subject: [PATCH 7/8] adds ability for offset for allelic genome graphs. temporary fix. In the future will want to consider overlap only if major and minor have the same cn --- R/gGnome.R | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/R/gGnome.R b/R/gGnome.R index ed63a6b9..4a8eefe3 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -5801,7 +5801,8 @@ gGraph = R6::R6Class("gGraph", settings = list(y_axis = list(title = "copy number", visible = TRUE)), cid.field = NULL, - no.y = FALSE) + no.y = FALSE, + offset = FALSE) { ## Make sure that our nodes are not empty before visualizing if (is.null(private$pnodes) || length(private$pnodes) == 0) { @@ -6051,6 +6052,21 @@ gGraph = R6::R6Class("gGraph", if (!is.null(settings)){ gg.js = c(list(settings = settings), gg.js) } + ## tempory offset addition for allelic graphs - will probably want to be smart and only add it when major and minor overlap + if(offset) { + for(x in 1:length(gg.js$intervals)) { + col1 = gg.js$intervals[[x]]$metadata$color + if(gg.js$intervals[[x]]$y != 0) { + if(col1 == "#0000FF80") { + gg.js$intervals[[x]]$y = gg.js$intervals[[x]]$y + 0.1 + } else if(col1 == "#FF000080") { + gg.js$intervals[[x]]$y = gg.js$intervals[[x]]$y - 0.1 + } + } + } + gg.js$connections = gg.js$connections[weight != 0,] + } + if (save){ if (verbose) From 93f01b60b830680eb53875781e76f81bcc28f044 Mon Sep 17 00:00:00 2001 From: Stanley Clarke Date: Tue, 16 Apr 2024 09:01:39 -0400 Subject: [PATCH 8/8] updates --- R/gGnome.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/gGnome.R b/R/gGnome.R index 4a8eefe3..e0c338c7 100644 --- a/R/gGnome.R +++ b/R/gGnome.R @@ -6064,8 +6064,9 @@ gGraph = R6::R6Class("gGraph", } } } - gg.js$connections = gg.js$connections[weight != 0,] } + gg.js$connections = gg.js$connections[weight != 0,] + if (save){