11# ' binmat
2+ # ' \%lifecycle{stable}
23# ' Enables creation of a binary matrix from a maf file with
34# ' a predefined list of patients (rows are patients and columns are genes)
45# ' @param patients a character vector that let's the user specify the patients to be used to create the matrix.
1516# ' Default is NULL.
1617# ' @param cna.binary A boolean argument specifying if the cna events should be enforced as binary. In which case separate columns for
1718# ' amplifications and deletions will be created.
18- # ' @param cna.relax for cna data only enables to count both gains and shallow deletions as amplifications and deletions respectively.
19+ # ' @param cna.relax By default this argument is set to FALSE, where only deep deletions (-2) and amplifications (2) will be annotated as events.
20+ # ' When set to FTRUE all deletions (-1 shallow and -2 deep) are counted as an event same for all gains (1 gain, 2 amplification) as an event.
1921# ' @param specify.plat boolean specifying if specific IMPACT platforms should be considered. When TRUE NAs will fill the cells for genes
2022# ' of patients that were not sequenced on that plaform. Default is TRUE.
2123# ' @param set.plat character argument specifying which IMPACT platform the data should be reduced to if specify.plat is set to TRUE.
2224# ' Options are "341" and "410". Default is NULL.
2325# ' @param rm.empty boolean specifying if columns with no events founds should be removed. Default is TRUE.
2426# ' @param pathway boolean specifying if pathway annotation should be applied. If TRUE, the function will return a supplementary binary
2527# ' dataframe with columns being each pathway and each row being a sample. Default is FALSE.
28+ # ' @param recode.aliases bolean specifying if automated gene name alias matching should be done. Default is TRUE. When TRUE
29+ # ' the function will check for genes that may have more than 1 name in your data using the aliases im gnomeR::impact_gene_info alias column
2630# ' @param col.names character vector of the necessary columns to be used. By default: col.names = c(Tumor_Sample_Barcode = NULL,
2731# ' Hugo_Symbol = NULL, Variant_Classification = NULL, Mutation_Status = NULL, Variant_Type = NULL)
2832# ' @param oncokb boolean specfiying if maf file should be oncokb annotated. Default is FALSE.
5660binmat <- function (patients = NULL , maf = NULL , mut.type = " SOMATIC" ,SNP.only = FALSE ,include.silent = FALSE ,
5761 fusion = NULL ,cna = NULL ,cna.binary = TRUE ,cna.relax = FALSE , specify.plat = TRUE ,
5862 set.plat = NULL ,rm.empty = TRUE , pathway = FALSE ,
63+ recode.aliases = TRUE ,
5964 col.names = c(Tumor_Sample_Barcode = NULL , Hugo_Symbol = NULL ,
6065 Variant_Classification = NULL , Mutation_Status = NULL , Variant_Type = NULL ),
6166 oncokb = FALSE , keep_onco = c(" Oncogenic" ," Likely Oncogenic" ," Predicted Oncogenic" ), token = ' ' ,... ){
@@ -121,7 +126,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
121126 if (! is.null(maf )){
122127
123128 # quick data checks #
124- maf <- check_maf_input(maf )
129+ maf <- check_maf_input(maf , recode.aliases = recode.aliases )
125130 # if(is.na(match("Tumor_Sample_Barcode",colnames(maf))))
126131 # stop("The MAF file inputted is missing a patient name column. (Tumor_Sample_Barcode)")
127132 # if(is.na(match("Hugo_Symbol",colnames(maf))))
@@ -149,7 +154,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
149154 maf <- structure(maf ,class = c(" data.frame" ," maf" ))
150155 # getting mutation binary matrix #
151156 mut <- createbin(obj = maf , patients = patients , mut.type = mut.type , cna.binary = cna.binary ,cna.relax = cna.relax ,
152- SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat )
157+ SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat , recode.aliases = recode.aliases )
153158
154159 }
155160
@@ -164,7 +169,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
164169 # filter/define patients #
165170 # if(is.null(patients)) patients <- as.character(unique(fusion$Tumor_Sample_Barcode))
166171 fusion <- createbin(obj = fusion , patients = patients , mut.type = mut.type , cna.binary = cna.binary ,
167- SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat )
172+ SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat , recode.aliases = recode.aliases )
168173 if (! is.null(mut )){
169174 mut <- as.data.frame(cbind(mut ,fusion ))
170175 rownames(mut ) <- patients }
@@ -205,14 +210,15 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
205210 cna <- structure(cna ,class = c(" data.frame" ," cna" ))
206211 # if(is.null(patients)) patients <- gsub("\\.","-",as.character(colnames(cna)))[-1]
207212 cna <- createbin(obj = cna , patients = patients , mut.type = mut.type , cna.binary = cna.binary ,cna.relax = cna.relax ,
208- SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat )
213+ SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat , recode.aliases = recode.aliases )
209214
210215 }
211216
212217 else {
213218 # if(is.null(patients)) patients <- unique(cna$sampleId)
214219 cna <- createbin(obj = cna , patients = patients , mut.type = mut.type , cna.binary = cna.binary ,cna.relax = cna.relax ,
215- SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat )
220+ SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat ,
221+ recode.aliases = recode.aliases )
216222 }
217223 }
218224
@@ -285,7 +291,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
285291 # colnames(cna) <- gsub("\\.","-",colnames(cna))
286292 # }
287293 cna <- createbin(obj = cna , patients = patients , mut.type = mut.type , cna.binary = cna.binary ,cna.relax = cna.relax ,
288- SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat )
294+ SNP.only = SNP.only , include.silent = include.silent , specify.plat = specify.plat , recode.aliases = recode.aliases )
289295 }
290296 if (! is.null(mut )){
291297 mut <- as.data.frame(cbind(mut ,cna ))
@@ -375,8 +381,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
375381 unique_genes <- unique(gsub(" .Del|.Amp|.fus|.cna" ," " ,colnames(mut )))
376382 missing_genes <- unique_genes [which(! (unique_genes %in% impact_gene_info $ hugo_symbol ))]
377383 if (length(missing_genes ) > 0 && specify.plat )
378- warning(paste0(" Some genes in the final matrix were not part of the official IMPACT panel and thus couldn't be annotate
379- for missing status. If you wish to have a complete list of genes in IMPACT please see 'impact_gene_info'. " ,
384+ warning(paste0(" The following genes in the final matrix were not part of the official IMPACT panel and thus couldn't be annotated for missing status. To see a complete list of genes in IMPACT please see 'impact_gene_info': " ,
380385 paste0(missing_genes ,collapse = " , " )
381386 )
382387 )
@@ -389,7 +394,7 @@ binmat <- function(patients=NULL, maf = NULL, mut.type = "SOMATIC",SNP.only = FA
389394# #############################################
390395
391396
392- createbin <- function (obj , patients , mut.type , cna.binary , SNP.only ,include.silent , cna.relax , specify.plat ){
397+ createbin <- function (obj , patients , mut.type , cna.binary , SNP.only ,include.silent , cna.relax , specify.plat , recode.aliases ){
393398 UseMethod(" createbin" )
394399}
395400
@@ -402,7 +407,7 @@ createbin.default <- function(obj) {
402407# ############ MUTATION MATRIX ################
403408# #############################################
404409
405- createbin.maf <- function (obj , patients , mut.type , cna.binary , SNP.only , include.silent , cna.relax , specify.plat ){
410+ createbin.maf <- function (obj , patients , mut.type , cna.binary , SNP.only , include.silent , cna.relax , specify.plat , recode.aliases = recode.aliases ){
406411 maf <- as_tibble(obj )
407412 maf $ Hugo_Symbol <- as.character(maf $ Hugo_Symbol )
408413
@@ -444,7 +449,9 @@ createbin.maf <- function(obj, patients, mut.type, cna.binary, SNP.only, include
444449# ############ FUSION MATRIX ###############
445450# ##########################################
446451
447- createbin.fusion <- function (obj , patients , mut.type ,cna.binary , SNP.only ,include.silent , cna.relax , specify.plat ){
452+ createbin.fusion <- function (obj , patients , mut.type ,cna.binary ,
453+ SNP.only ,include.silent , cna.relax ,
454+ specify.plat , recode.aliases ){
448455 fusion <- as_tibble(obj )
449456 # quick data checks #
450457 if (length(match(" Tumor_Sample_Barcode" ,colnames(fusion ))) == 0 )
@@ -457,10 +464,28 @@ createbin.fusion <- function(obj, patients, mut.type,cna.binary, SNP.only,includ
457464 alias_table <- tidyr :: unnest(impact_gene_info , cols = alias ) %> %
458465 select(hugo_symbol , alias )
459466
460- # recode aliases
461- fusion $ Hugo_Symbol_Old <- fusion $ Hugo_Symbol
462- fusion $ Hugo_Symbol <- purrr :: map_chr(fusion $ Hugo_Symbol , ~ resolve_alias(.x ,
463- alias_table = alias_table ))
467+ # recode aliases ---
468+ if (recode.aliases == TRUE ) {
469+
470+ fusion $ Hugo_Symbol_Old <- fusion $ Hugo_Symbol
471+ fusion $ Hugo_Symbol <- purrr :: map_chr(fusion $ Hugo_Symbol , ~ resolve_alias(.x ,
472+ alias_table = alias_table ))
473+
474+ message <- fusion %> %
475+ dplyr :: filter(Hugo_Symbol_Old != Hugo_Symbol ) %> %
476+ dplyr :: select(Hugo_Symbol_Old , Hugo_Symbol ) %> %
477+ dplyr :: distinct()
478+
479+ if (nrow(message ) > 0 ) {
480+ warning(paste0(" FUSION DATA: To ensure gene with multiple names/aliases are correctly grouped together, the
481+ following genes in your fusion data have been recoded. You can supress this with recode.aliases = FALSE \n \n " ,
482+ purrr :: map2(message $ Hugo_Symbol_Old ,
483+ message $ Hugo_Symbol ,
484+ ~ paste0(.x , " recoded to " , .y , " \n " ))))
485+ }
486+ }
487+
488+
464489
465490 fusion <- as_tibble(fusion ) %> %
466491 filter(.data $ Tumor_Sample_Barcode %in% patients )
@@ -472,8 +497,10 @@ createbin.fusion <- function(obj, patients, mut.type,cna.binary, SNP.only,includ
472497
473498 for (i in patients ){
474499 genes <- fusion $ Hugo_Symbol [fusion $ Tumor_Sample_Barcode %in% i ]
475- if (length(genes ) != 0 ){fusion.out [match(i ,rownames(fusion.out )),
476- match(unique(as.character(genes )),colnames(fusion.out ))] <- 1 }
500+ if (length(genes ) != 0 )
501+ fusion.out [match(i ,rownames(fusion.out )),
502+ match(unique(as.character(genes )),colnames(fusion.out ))] <- 1
503+
477504 }
478505 colnames(fusion.out ) <- paste0(colnames(fusion.out )," .fus" )
479506 return (fusion.out )
@@ -484,7 +511,9 @@ createbin.fusion <- function(obj, patients, mut.type,cna.binary, SNP.only,includ
484511# ############ COPY NUMBER MATRIX ###############
485512# ###############################################
486513
487- createbin.cna <- function (obj , patients , mut.type ,cna.binary , SNP.only ,include.silent , cna.relax , specify.plat ){
514+ createbin.cna <- function (obj , patients , mut.type ,cna.binary ,
515+ SNP.only ,include.silent , cna.relax ,
516+ specify.plat , recode.aliases ){
488517 cna <- obj
489518 cna <- as.data.frame(tibble :: as_tibble(cna ))
490519 cna $ Hugo_Symbol <- as.character(cna $ Hugo_Symbol )
@@ -493,10 +522,28 @@ createbin.cna <- function(obj, patients, mut.type,cna.binary, SNP.only,include.s
493522 alias_table <- tidyr :: unnest(impact_gene_info , cols = alias ) %> %
494523 select(hugo_symbol , alias )
495524
496- # recode aliases
497- # cna$Hugo_Symbol_Old <- cna$Hugo_Symbol
498- cna $ Hugo_Symbol <- purrr :: map_chr(cna $ Hugo_Symbol , ~ resolve_alias(.x ,
499- alias_table = alias_table ))
525+ # recode aliases ---
526+ if (recode.aliases == TRUE ) {
527+
528+ cna $ Hugo_Symbol_Old <- cna $ Hugo_Symbol
529+ cna $ Hugo_Symbol <- purrr :: map_chr(cna $ Hugo_Symbol , ~ resolve_alias(.x ,
530+ alias_table = alias_table ))
531+
532+ message <- cna %> %
533+ dplyr :: filter(Hugo_Symbol_Old != Hugo_Symbol ) %> %
534+ dplyr :: select(Hugo_Symbol_Old , Hugo_Symbol ) %> %
535+ dplyr :: distinct()
536+
537+ if (nrow(message ) > 0 ) {
538+ warning(paste0(" CNA DATA: To ensure gene with multiple names/aliases are correctly grouped together, the
539+ following genes in your CNA data have been recoded. You can supress this with recode.aliases = FALSE. \n \n " ,
540+ purrr :: map2(message $ Hugo_Symbol_Old ,
541+ message $ Hugo_Symbol ,
542+ ~ paste0(.x , " recoded to " , .y , " \n " ))))
543+ }
544+ }
545+
546+
500547 dups <- cna $ Hugo_Symbol [duplicated(cna $ Hugo_Symbol )]
501548 if (length(dups ) > 0 ){
502549 for (i in dups ){
@@ -572,7 +619,8 @@ createbin.cna <- function(obj, patients, mut.type,cna.binary, SNP.only,include.s
572619}
573620
574621# ## cna from API ###
575- createbin.api <- function (obj , patients , mut.type ,cna.binary , SNP.only ,include.silent , cna.relax , specify.plat ){
622+ createbin.api <- function (obj , patients , mut.type ,cna.binary ,
623+ SNP.only ,include.silent , cna.relax , specify.plat , recode.aliases ){
576624 cna <- as.data.frame(obj )
577625
578626 cna <- as.data.frame(tibble :: as_tibble(cna ))
0 commit comments