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