Skip to content

Commit 1d403a1

Browse files
authored
Fix #208 and #209 (#210)
* Fix and clean up docs * Update test and fix * Update pkgdown index manually * Fix some logic, including #208
1 parent ef7833b commit 1d403a1

File tree

7 files changed

+97
-13
lines changed

7 files changed

+97
-13
lines changed

R/annotations.R

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ annotate_with_manifest <- function(manifest, ignore_na = TRUE, ignore_blank = TR
5353
#' @param select Vector of properties to selectively copy if present on the entity.
5454
#' If not specified, will copy over everything, which may not be desirable.
5555
#' @param update Whether to immediately update or return annotation objects only.
56+
#' @param as_list Only used when `update=FALSE`; for backwards-compatibility or when
57+
#' downstream usage of `copy_annotations` expects an R list, return as an R list.
5658
#' @export
5759
copy_annotations <- function(entity_from,
5860
entity_to,
5961
select = NULL,
60-
update = FALSE) {
62+
update = FALSE,
63+
as_list = TRUE) {
6164

6265
.check_login()
6366

@@ -74,7 +77,29 @@ copy_annotations <- function(entity_from,
7477
for(k in select) {
7578
to_annotations[k] <- from_annotations[k]
7679
}
77-
if(update) .syn$set_annotations(to_annotations) else return(to_annotations)
80+
if(update) {
81+
.syn$set_annotations(to_annotations)
82+
} else if (as_list) {
83+
return(.dict_to_list(to_annotations))
84+
} else {
85+
return(to_annotations)
86+
}
7887
}
7988
}
8089

90+
91+
#' Convert a flat Python Dict to R list
92+
#'
93+
#' An internal function used to convert Annotations objects returned by `get_annotations`.
94+
#'
95+
#' @param dict A flat Python Dict object.
96+
.dict_to_list <- function(dict) {
97+
if (is.null(names(dict))) {
98+
stop("Input must be a named list representing a flat Python dictionary.")
99+
}
100+
l <- list()
101+
for(k in names(dict)) {
102+
l[[k]] <- dict[k]
103+
}
104+
l
105+
}

R/nextflow_annotation_utils.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ annotation_rule <- function(outputFrom, which = c("format_as", "annotate_as", "t
2828
"featureCounts" = list(format_as = function(x) { "txt" }, annotate_as = "annotate_quantified_expression", template = "bts:ProcessedExpressionTemplate"),
2929
"SAMtools" = list(format_as = function(x) { substring(x, nchar(x)-2, nchar(x)) }, annotate_as = "annotate_aligned_reads", template = "bts:ProcessedAlignedReadsTemplate"),
3030
"CNVkit" = list(format_as = function(x) { substring(x, nchar(x)-2, nchar(x)) }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
31-
"DeepVariant" = list(format_as = function(x) { "vcf" }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
32-
"Strelka2" = list(format_as = function(x) { "vcf" }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
33-
"Mutect2" = list(format_as = function(x) { "vcf" }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
34-
"FreeBayes" = list(format_as = function(x) { "vcf" }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"))
31+
"DeepVariant" = list(format_as = function(x) { ifelse(grepl("tbi$", x), "tbi", "vcf") }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
32+
"Strelka2" = list(format_as = function(x) { ifelse(grepl("tbi$", x), "tbi", "vcf") }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
33+
"Mutect2" = list(format_as = function(x) { ifelse(grepl("tbi$", x), "tbi", "vcf") }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"),
34+
"FreeBayes" = list(format_as = function(x) { ifelse(grepl("tbi$", x), "tbi", "vcf") }, annotate_as = "annotate_called_variants", template = "bts:ProcessedVariantCallsTemplate"))
3535

3636
switch(
3737
which,
@@ -230,7 +230,7 @@ map_sample_output_sarek <- function(syn_out,
230230

231231
index_fun <- function(x) {
232232
caller_index <- grep("cnvkit|deepvariant|strelka|mutect|freebayes", x, ignore.case = TRUE)[1]
233-
if(!length(caller_index)) stop("Issue with figuring out sample output organization. Is there non-standard output?")
233+
if(!length(caller_index)) stop("Issue with inferring sample output organization. Is this non-standard output?")
234234
file_index <- length(x)
235235
if((file_index - caller_index) == 1) {
236236
file_index - 2 # i.e. `VariantCalling/<SAMPLE>/<CALLER>`
@@ -243,7 +243,10 @@ map_sample_output_sarek <- function(syn_out,
243243
# For nf-sarek tumor somatic variant calling, sample references tumor vs normal from same indiv
244244
# The sample assigned to the processed data is the tumor sample
245245
result[, sample := path_extract(path, index_fun = index_fun)]
246-
if(grepl("_vs_", first(result$output_name))) result[, sample := gsub("_vs.*", "", sample)]
246+
if(.output %in% c("Strelka2", "FreeBayes", "Mutect2") && any(grepl("_vs_", result$sample))) {
247+
message(" - Somatic data present.")
248+
result[, sample := gsub("_vs.*", "", sample)]
249+
}
247250

248251
results[[.output]] <- result
249252
setattr(results[[.output]], "outputFrom", .output)
@@ -450,6 +453,8 @@ annotate_called_variants <- function(metadata,
450453
"AnnotatedSomaticVariants"
451454
} else if(!grepl("_vs_", name) && format == "maf") {
452455
"AnnotatedGermlineVariants"
456+
} else if(format == "tbi") {
457+
"dataIndex"
453458
} else if(format %in% c("cns", "cnn", "cnr", "bed", "pdf", "png")) {
454459
"CopyNumberVariants"
455460
} else {
@@ -504,7 +509,6 @@ annotate_with_samtools_stats <- function(meta,
504509
#' Wrapper for all steps to get manifest for processed product
505510
#'
506511
#' @inheritParams map_sample_io
507-
#' @param
508512
#' @param workflow_link Workflow link.
509513
#' @export
510514
#' @return List `manifest` with manifests for each processed dataset,

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ reference:
165165
description: Mostly meant to be internal or experimental stuff
166166
- contents:
167167
- .delim_string_to_vector
168+
- .dict_to_list
168169
- .replace_string_column_with_stringlist_column
169170
- .store_rows
170171
- missing_annotation_email

man/copy_annotations.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dot-dict_to_list.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extract_syn_id_from_ss.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_copy_annotations.R

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
test_that("Copy annotations works", {
1+
test_that("Copy annotations works to apply an immediate copy of annotations from one entity to another (update=TRUE)
2+
as well as when simply getting a copy of the annotations present as a default R list to work with (update=FALSE)", {
23
skip_if_no_synapseclient()
34
skip_if_no_login()
45

@@ -18,12 +19,24 @@ test_that("Copy annotations works", {
1819
parent = PARENT_TEST_PROJECT)
1920
entity_c <- .syn$store(entity_c)
2021

21-
# when copying all annotations from A->B (default)
22+
# when getting a copy of all annotations from A->B (default)
23+
copy_of_a_b <- copy_annotations(entity_from = entity_a$properties$id,
24+
entity_to = entity_b$properties$id,
25+
select = NULL,
26+
update = FALSE)
27+
28+
# when immediately copying all annotations from A->B (default)
2229
copy_annotations(entity_from = entity_a$properties$id,
2330
entity_to = entity_b$properties$id,
2431
select = NULL,
2532
update = TRUE)
2633

34+
# when getting a copy of selective annotations from A->C
35+
copy_of_a_c <- copy_annotations(entity_from = entity_a$properties$id,
36+
entity_to = entity_c$properties$id,
37+
select = c("favorites", "key_not_on_a"),
38+
update = FALSE)
39+
2740
# when copying selective annotations from A->C
2841
copy_annotations(entity_from = entity_a$properties$id,
2942
entity_to = entity_c$properties$id,
@@ -35,6 +48,8 @@ test_that("Copy annotations works", {
3548
.syn$delete(entity_a)
3649
.syn$delete(entity_b)
3750
.syn$delete(entity_c)
51+
testthat::expect_equal(copy_of_a_b, list(after_a = TRUE, favorites = c("raindrops", "whiskers"), foo = "bar"))
52+
testthat::expect_equal(copy_of_a_c, list(favorites = c("raindrops", "whiskers")))
3853
testthat::expect_equal(result_b$foo, "bar")
3954
testthat::expect_equal(result_b$favorites, c("raindrops", "whiskers"))
4055
testthat::expect_equal(result_b$after_a, TRUE)
@@ -44,4 +59,3 @@ test_that("Copy annotations works", {
4459

4560
})
4661

47-

0 commit comments

Comments
 (0)