@@ -5,94 +5,22 @@ log <- file(snakemake@log[[1]], open = "wt")
55sink(log , type = " message" )
66sink(log , type = " output" )
77
8- library(ape )
9- library(pegas )
10- library(future.apply )
118library(tidyverse )
129library(jsonlite )
1310library(logger )
1411log_threshold(INFO )
1512
16- # Pi calculation
17- nucleotide.diversity <- function (dna_object , record.names , sample.size ) {
18- sample <- sample(record.names , sample.size , replace = FALSE )
19- dna_subset <- dna_object [record.names %in% sample ]
20- nuc.div(dna_subset )
21- }
22-
23- # Parallel bootstrapping
24- boot.nd.parallel <- function (aln , sample.size = 12 , reps = 100 ) {
25- record.names <- names(aln )
26- future_sapply(
27- 1 : reps ,
28- function (x ) {
29- nucleotide.diversity(aln , record.names , sample.size )
30- },
31- future.seed = TRUE
32- )
33- }
34-
3513# Import file with plots style
3614source(snakemake @ params [[" design" ]])
3715
38- # Outgroup/context alignment
39- gene_ex <- read.dna(
40- snakemake @ input [[" context_fasta" ]],
41- format = " fasta" ,
42- as.matrix = FALSE
43- )
44- gene_ex <- gene_ex [
45- ! startsWith(names(gene_ex ), snakemake @ config [[" ALIGNMENT_REFERENCE" ]])
46- ]
47-
48- # Study alignment
49- study_aln <- read.dna(
50- snakemake @ input [[" study_fasta" ]],
51- format = " fasta" ,
52- as.matrix = FALSE
53- )
54- study_aln <- study_aln [
55- ! startsWith(names(study_aln ), snakemake @ config [[" ALIGNMENT_REFERENCE" ]])
56- ]
57-
58- # Diversity value for our samples
59- log_info(" Calculating diversity value for studied samples" )
60- diversity <- nuc.div(study_aln )
61-
62-
63- # Perform bootstrap
64- log_info(" Performing calculation for nucleotide diversity in context samples" )
65- plan(multisession , workers = snakemake @ threads )
66- divs <- boot.nd.parallel(
67- gene_ex ,
68- length(study_aln ),
69- snakemake @ params [[" bootstrap_reps" ]]
70- )
71- plan(sequential )
72-
73- # Test normality
74- log_info(" Normality test for nucleotide diversity values" )
75- st <- shapiro.test(divs )
76-
77- # Calculate p-value (assuming normal distribution)
78-
79- log_info(" Calculating p-value (assuming normal distribution)" )
80-
81- test <- t.test(
82- divs ,
83- alternative = " greater" ,
84- mu = diversity ,
85- conf.level = 0.95
86- )
87- pvalue.norm <- test $ p.value
88-
89- # Estimate p-value empirically
90- log_info(" Estimating p-value empirically" )
91- empirical.probs <- ecdf(divs )
92- pvalue.emp <- empirical.probs(diversity )
16+ # Read data
17+ log_info(" Reading diversity results" )
18+ divs <- read_lines(snakemake @ input $ divs ) %> %
19+ as.numeric()
20+ json <- read_json(snakemake @ input $ json )
9321
9422# Plot and save
95- log_info(" Plotting diversity plot " )
23+ log_info(" Plotting" )
9624p <- data.frame (pi = divs ) %> %
9725 ggplot() +
9826 geom_density(
@@ -103,7 +31,7 @@ p <- data.frame(pi = divs) %>%
10331 color = " #eae2b7"
10432 ) +
10533 geom_vline(
106- aes( xintercept = diversity ) ,
34+ xintercept = json $ diversity ,
10735 color = " #d62828"
10836 ) +
10937 stat_function(
@@ -117,33 +45,10 @@ p <- data.frame(pi = divs) %>%
11745 )
11846
11947ggsave(
120- filename = snakemake @ output [[" fig " ]],
48+ filename = snakemake @ output [[" plot " ]],
12149 plot = p ,
122- width = snakemake @ params [[" plot_width " ]],
123- height = snakemake @ params [[" plot_height " ]],
50+ width = snakemake @ params [[" plot_width_mm " ]],
51+ height = snakemake @ params [[" plot_height_mm " ]],
12452 units = " mm" ,
12553 dpi = 250
12654)
127-
128- # DATA JSON #####
129- p.value <- ifelse(st $ p.value > = 0.05 , pvalue.norm , pvalue.emp )
130-
131- list.div <- list (
132- " diversity" = format(diversity , scientific = TRUE ),
133- " p.value" = ifelse(p.value > = 0.001 , p.value , " < 0.001" ),
134- " normal.pvalue" = ifelse(st $ p.value > = 0.001 , p.value , " < 0.001" ),
135- " norm.text" = ifelse(st $ p.value > = 0.05 , " " , " not" ),
136- " type.test" = ifelse(st $ p.value > = 0.05 , " " , " empirical" ),
137- " boot.reps" = snakemake @ params [[" bootstrap_reps" ]],
138- " sample.size" = length(study_aln )
139- )
140-
141- json <- toJSON(list.div )
142- write(json , snakemake @ output [[" json" ]])
143-
144- # PLOT TABLES
145- data.frame (
146- pi = divs ,
147- prop.value = diversity
148- ) %> %
149- write.csv(snakemake @ output [[" table" ]], row.names = FALSE )
0 commit comments