comparison ideas_preprocessor.R @ 1:91c5dbb14a13 draft

Uploaded
author greg
date Mon, 22 Jan 2018 14:35:28 -0500
parents
children e97851e8951b
comparison
equal deleted inserted replaced
0:f060a0fbd4fe 1:91c5dbb14a13
1 #!/usr/bin/env Rscript
2
3 suppressPackageStartupMessages(library("data.table"))
4 suppressPackageStartupMessages(library("optparse"))
5
6 option_list <- list(
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("--reads_per_bp"), action="store", dest="reads_per_bp", type="integer", help="Calculate the signal in each genomic window"),
15 make_option(c("--restrict_to_chroms"), action="store", dest="restrict_to_chroms", default=NULL, help="Restrict processing to specified chromosomes"),
16 make_option(c("--standardize_datasets"), action="store_true", dest="standardize_datasets", default=FALSE, help="Standardize all datasets"),
17 make_option(c("--window_size"), action="store", dest="window_size", type="integer", default=NULL, help="Window size in base pairs")
18 )
19
20 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
21 args <- parse_args(parser, positional_arguments=TRUE)
22 opt <- args$options
23
24 create_primary_html = function(output, output_files_path) {
25 tmp_files = list.files(path=output_files_path);
26 s <- paste('<html><head></head><body>', sep="\n");
27 s <- paste(s, '<h3>Contents of directory required by IDEAS</h3>\n', sep="");
28 s <- paste(s, '<ul>\n', sep="");
29 s <- paste(s, '<li><a href="prep_output_config.txt">', prep_output_config.txt, '</a></li>\n', sep="");
30 for (i in 1:length(tmp_files)) {
31 s <- paste(s, '<li><a href="', 'tmp/', tmp_files[i], '">', tmp_files[i], '</a></li>\n', sep="");
32 }
33 s <- paste(s, '</ul>\n</body>\n</html>', sep="");
34 cat(s, file=output);
35 }
36
37 # Create the directories that will contain all of the output files.
38 dir.create(opt$output_files_path, showWarnings=FALSE);
39 dir.create(paste(opt$output_files_path, "tmp", sep="/"), showWarnings=FALSE);
40
41 # Read the prep_input_config text file which has this format:
42 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
43 prep_input_config_matrix = as.matrix(read.table(opt$prep_input_config));
44 # Process data to windows mean.
45 for (i in 1:dim(prep_input_config_matrix)[1]) {
46 file_path = prep_input_config_matrix[i, 3]
47 file_name = prep_input_config_matrix[i, 4]
48 datatype = prep_input_config_matrix[i, 5]
49 if (datatype == "bam") {
50 system(paste("samtools index", file_path));
51 bw = paste(file_name, "bw", sep=".");
52 system(paste("bamCoverage --bam", file_path, "-o", bw, "--binSize", opt$window_size));
53 } else {
54 bw = file_path;
55 }
56 bd = paste(output_files_path, "tmp", file_name, ".bed", sep="/");
57 system(paste("bigWigAverageOverBed", bw, opt$bed_input, "stdout | cut -f5 >", bd));
58 system(paste("gzip -f", bd));
59 }
60
61 # Create the primary HTML dataset.
62 create_primary_html(opt$output, opt$output_files_path);