1
|
1 #!/usr/bin/env Rscript
|
|
2
|
14
|
3 # TODO: implement support for the following:
|
|
4 # 1. Scenario where user did not select chrom_bed_input
|
|
5 # 2. --exclude_bed_input
|
|
6 # 3. --bychr
|
|
7 # 4. --chrom_len_file
|
|
8 # 5. --reads_per_bp
|
|
9 # 6. --restrict_to_chroms
|
|
10 # 7. --standardize_datasets
|
|
11 # 8. Scenario where --window_size is NULL and need to handle bamCoverage - see TODO near line # 57.
|
|
12
|
|
13
|
1
|
14 suppressPackageStartupMessages(library("data.table"))
|
|
15 suppressPackageStartupMessages(library("optparse"))
|
|
16
|
|
17 option_list <- list(
|
12
|
18 make_option(c("--chrom_bed_input"), action="store", dest="chrom_bed_input", defaul=NULL, help="Chromosome windows positions file"),
|
14
|
19 make_option(c("--exclude_bed_input"), action="store", dest="exclude_bed_input", defaul=NULL, help="File(s) containing regions to exclude"),
|
3
|
20 make_option(c("--bychr"), action="store_true", dest="bychr", defaul=FALSE, help="Separate files by chromosome"),
|
|
21 make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", default=NULL, help="Chromosome lengths file"),
|
5
|
22 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"),
|
3
|
23 make_option(c("--output"), action="store", dest="output", help="Primary output dataset"),
|
|
24 make_option(c("--output_files_path"), action="store", dest="output_files_path", help="Primary output dataset extra files path"),
|
|
25 make_option(c("--reads_per_bp"), action="store", dest="reads_per_bp", type="integer", help="Calculate the signal in each genomic window"),
|
|
26 make_option(c("--restrict_to_chroms"), action="store", dest="restrict_to_chroms", default=NULL, help="Restrict processing to specified chromosomes"),
|
|
27 make_option(c("--standardize_datasets"), action="store_true", dest="standardize_datasets", default=FALSE, help="Standardize all datasets"),
|
5
|
28 make_option(c("--chromosome_windows"), action="store", dest="chromosome_windows", default=NULL, help="Windows positions by chroms config file"),
|
3
|
29 make_option(c("--window_size"), action="store", dest="window_size", type="integer", default=NULL, help="Window size in base pairs")
|
1
|
30 )
|
|
31
|
|
32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
33 args <- parse_args(parser, positional_arguments=TRUE)
|
|
34 opt <- args$options
|
|
35
|
18
|
36 create_primary_html = function(output, output_files_path) {
|
5
|
37 files = list.files(path=output_files_path);
|
1
|
38 s <- paste('<html><head></head><body>', sep="\n");
|
18
|
39 s <- paste(s, '<h3>Files prepared for IDEAS</h3>\n', sep="");
|
1
|
40 s <- paste(s, '<ul>\n', sep="");
|
3
|
41 for (i in 1:length(files)) {
|
|
42 s <- paste(s, '<li><a href="', files[i], '">', files[i], '</a></li>\n', sep="");
|
1
|
43 }
|
21
|
44 s <- paste(s, '</ul></body></html>', sep="");
|
1
|
45 cat(s, file=output);
|
|
46 }
|
|
47
|
5
|
48 # Read the ideaspre_input_config text file which has this format:
|
1
|
49 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
|
5
|
50 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config));
|
14
|
51
|
|
52 # TODO: fix this
|
|
53 window_size = opt$window_size
|
|
54 if (is.null(opt$window_size)) {
|
|
55 window_size = 500;
|
|
56 }
|
1
|
57 # Process data to windows mean.
|
12
|
58 if (!is.null(opt$chrom_bed_input)) {
|
|
59 for (i in 1:dim(ideaspre_input_config)[1]) {
|
|
60 file_path = ideaspre_input_config[i, 3]
|
|
61 file_name = ideaspre_input_config[i, 4]
|
|
62 datatype = ideaspre_input_config[i, 5]
|
|
63 if (datatype == "bam") {
|
|
64 cmd = paste("samtools index", file_path);
|
|
65 system(cmd);
|
|
66 bigwig_file_name = paste(file_name, "bw", sep=".");
|
14
|
67
|
|
68 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", window_size);
|
12
|
69 system(cmd);
|
|
70 } else {
|
|
71 bigwig_file_name = file_path;
|
|
72 }
|
|
73 bed_file_name = paste(file_name, "bed", sep=".");
|
21
|
74 cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_name);
|
3
|
75 system(cmd);
|
21
|
76 cmd = paste("gzip -f", bed_file_name);
|
12
|
77 system(cmd);
|
1
|
78 }
|
|
79 }
|
|
80
|
9
|
81 # Create file1.txt.
|
5
|
82 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" ");
|
3
|
83 system(cmd);
|
21
|
84 # Compress the bed files and create file2.txt.
|
|
85 cmd = "ls *.bed.gz > file2.txt";
|
3
|
86 system(cmd);
|
12
|
87 # Create IDEAS_input_config.txt with the format required by IDEAS.
|
|
88 ideas_input_config = "IDEAS_input_config.txt"
|
|
89 cmd = paste("paste -d' ' file1.txt file2.txt >", ideas_input_config, sep=" " );
|
3
|
90 system(cmd);
|
12
|
91 # Move IDEAS_input_config.txt to the output directory.
|
|
92 to_path = paste(opt$output_files_path, ideas_input_config, sep="/");
|
|
93 file.rename(ideas_input_config, to_path);
|
21
|
94 # Handle optional chrom_bed_input.txt and chromosomes.bed files.
|
12
|
95 if (!is.null(opt$chrom_bed_input) && !is.null(opt$chromosome_windows)) {
|
|
96 # Renane opt$chrom_bed_input to be chromosomes.bed
|
|
97 # and make a copy of it in the output directory.
|
|
98 to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/");
|
15
|
99 file.copy(opt$chrom_bed_input, to_path);
|
12
|
100 # Move chromosome_windows.txt to the output directory.
|
5
|
101 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/");
|
|
102 file.rename(opt$chromosome_windows, to_path);
|
3
|
103 }
|
1
|
104 # Create the primary HTML dataset.
|
18
|
105 create_primary_html(opt$output, opt$output_files_path);
|