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