@@ -48,19 +48,24 @@ variants <- left_join(variants, metadata, by = c("SAMPLE" = "ID")) %>%
4848 CollectionDate
4949 ) %> %
5050 mutate(
51- interval = as.numeric(difftime(CollectionDate , min(CollectionDate ), units = " days" ))
51+ interval = as.numeric(difftime(
52+ CollectionDate ,
53+ min(CollectionDate ),
54+ units = " days"
55+ ))
5256 )
5357
5458# Save processed input
5559log_info(" Writing dated and frequency-filled variants" )
56- write_csv (variants , snakemake @ output $ fmt_variants )
60+ write_tsv (variants , snakemake @ output $ fmt_variants )
5761
5862log_info(" Calculating correlations" )
5963log_debug(" Calculating unique SNPs" )
6064# Get list with all different polymorphisms
6165unique.snps <- unique(variants $ VARIANT_NAME )
6266
6367# Create an empty dataframe to be filled
68+ log_debug(" Initializing empty results" )
6469cor.df <- data.frame (
6570 variant = " " ,
6671 min_af = 0 ,
@@ -71,21 +76,30 @@ cor.df <- data.frame(
7176) %> %
7277 filter(p.value != 0 )
7378
74- log_debug (" Calculating correlation using method = {snakemake@params$cor_method} and exact p-value = {snakemake@params$cor_exact}" )
79+ log_info (" Calculating correlation with method= {snakemake@params$cor_method} and exact= {snakemake@params$cor_exact}" )
7580correlations <- lapply(
7681 unique.snps ,
7782 function (snp ) {
83+ log_debug(" Processing {snp}" )
7884 # Select SNP
7985 df <- filter(
8086 variants ,
8187 VARIANT_NAME == snp
8288 )
8389 # Perform calculation
84- test <- cor.test(
85- df $ ALT_FREQ ,
86- df $ interval ,
87- method = snakemake @ params $ cor_method ,
88- exact = snakemake @ params $ cor_exact
90+ test <- tryCatch(
91+ {
92+ cor.test(
93+ df $ ALT_FREQ ,
94+ df $ interval ,
95+ method = snakemake @ params $ cor_method ,
96+ exact = snakemake @ params $ cor_exact
97+ )
98+ },
99+ error = function (e ) {
100+ log_warn(" Cannot run cor.test for {snp}: {e}" )
101+ list (p.value = NA , estimate = NA )
102+ }
89103 )
90104 # Adjust p-value
91105 p.value.adj <- p.adjust(
@@ -122,8 +136,7 @@ significant.variants <- correlations %>%
122136 ) %> %
123137 pull(variant ) %> %
124138 unique()
125-
126- log_info(" Significant: {significant.variants}" )
139+ log_debug(" Significant: {significant.variants}" )
127140
128141log_info(" Selecting variants in positions with more than one alternative allele" )
129142mult.alt.variants <- variants %> %
@@ -137,13 +150,11 @@ mult.alt.variants <- variants %>%
137150 ungroup() %> %
138151 pull(VARIANT_NAME ) %> %
139152 unique()
140-
141- log_info(" Mult all: {mult.alt.variants}" )
153+ log_debug(" Mult all: {mult.alt.variants}" )
142154
143155# Build selected subset to represent
144156variant.selection <- unique(c(significant.variants , mult.alt.variants ))
145-
146- log_info(" Selection: {variant.selection}" )
157+ log_debug(" Selection: {variant.selection}" )
147158
148159log_info(" Writing selected variants subset" )
149160write_lines(variant.selection , snakemake @ output $ subset )
0 commit comments