1
+ # Redirect R output to log
2
+ log <- file(snakemake @ log [[1 ]], open = " wt" )
3
+ sink(log , type = " output" )
4
+ sink(log , type = " message" )
5
+
6
+ library(tidyverse )
7
+ library(cowplot )
8
+
9
+ # Plot bar graph of FRiP values
10
+ total_read_counts <- snakemake @ input [[" total_read_counts" ]]
11
+ peak_read_counts <- snakemake @ input [[" peak_read_count" ]]
12
+
13
+ # df to store FRiP values
14
+ df <- data.frame (sample = character (),
15
+ read_in_peaks = numeric (),
16
+ total_reads = numeric (),
17
+ fraction_of_reads_in_peaks = numeric ())
18
+
19
+ # Add FRiP values to df
20
+ for (i in seq_along(total_read_counts )) {
21
+ sample <- snakemake @ wildcards [[" bg_sample" ]][i ]
22
+ total_reads <- as.numeric(read_lines(total_read_counts [i ]))
23
+ peak_reads <- as.numeric(read_lines(peak_read_counts [i ]))
24
+ frip <- peak_reads / total_reads
25
+
26
+ df <- df %> %
27
+ add_row(sample = sample ,
28
+ read_in_peaks = peak_reads ,
29
+ total_reads = total_reads ,
30
+ fraction_of_reads_in_peaks = frip )
31
+ }
32
+
33
+ # Create plot
34
+ p <- ggplot(df , aes(x = sample ,
35
+ y = fraction_of_reads_in_peaks )) +
36
+ geom_bar(stat = " identity" ,
37
+ fill = " #419179" ,
38
+ colour = " black" ) +
39
+ theme_cowplot(18 ) +
40
+ theme(axis.text.x = element_text(angle = 45 ,
41
+ hjust = 1 )) +
42
+ scale_y_continuous(expand = expansion(mult = c(0 , 0.1 )),
43
+ limits = c(0 , 1 )) +
44
+ labs(title = NULL ,
45
+ x = NULL ,
46
+ y = " Fraction of reads in peaks" ) +
47
+ theme(plot.title = element_text(hjust = 0.5 ))
48
+
49
+ # Save plot
50
+ ggsave(snakemake @ output [[1 ]], p )
51
+
52
+ # Close redirection of output/messages
53
+ sink(log , type = " output" )
54
+ sink(log , type = " message" )
0 commit comments