Mercurial > repos > greg > ideas_preprocessor
comparison ideas_preprocessor.R @ 5:2b4e1bd725f7 draft
Uploaded
author | greg |
---|---|
date | Wed, 24 Jan 2018 08:30:05 -0500 |
parents | e97851e8951b |
children | 92bb33490bb4 |
comparison
equal
deleted
inserted
replaced
4:f577a83e86c4 | 5:2b4e1bd725f7 |
---|---|
1 (.venv) [galaxy@IDEAS ideas_preprocessor]$ cat ideas_preprocessor.R | |
1 #!/usr/bin/env Rscript | 2 #!/usr/bin/env Rscript |
2 | 3 |
3 suppressPackageStartupMessages(library("data.table")) | 4 suppressPackageStartupMessages(library("data.table")) |
4 suppressPackageStartupMessages(library("optparse")) | 5 suppressPackageStartupMessages(library("optparse")) |
5 | 6 |
6 option_list <- list( | 7 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("--chrom_bed_input"), action="store", dest="chrom_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("--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("--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("--chrom_len_file"), action="store", dest="chrom_len_file", default=NULL, help="Chromosome lengths file"), |
12 make_option(c("--ideas_input_config"), action="store", dest="ideas_input_config", help="Preprocessing output config file"), | |
13 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"), | |
11 make_option(c("--output"), action="store", dest="output", help="Primary output dataset"), | 14 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"), | 15 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"), | 16 make_option(c("--output_hid"), action="store", dest="output_hid", help="Primary output dataset hid"), |
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"), | 17 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"), | 18 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"), | 19 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"), | 20 make_option(c("--chromosome_windows"), action="store", dest="chromosome_windows", 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") | 21 make_option(c("--window_size"), action="store", dest="window_size", type="integer", default=NULL, help="Window size in base pairs") |
20 ) | 22 ) |
21 | 23 |
22 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) | 24 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) |
23 args <- parse_args(parser, positional_arguments=TRUE) | 25 args <- parse_args(parser, positional_arguments=TRUE) |
24 opt <- args$options | 26 opt <- args$options |
25 | 27 |
26 create_primary_html = function(output, output_files_path) { | 28 create_primary_html = function(output, output_hid, output_files_path) { |
27 files = list.files(path=output_files_path, pattern="\\.txt"); | 29 files = list.files(path=output_files_path); |
28 s <- paste('<html><head></head><body>', sep="\n"); | 30 s <- paste('<html><head></head><body>', sep="\n"); |
29 s <- paste(s, '<h3>Contents of directory required by IDEAS</h3>\n', sep=""); | 31 s <- paste(s, '<h3>History item ', output_hid, ' files prepared for IDEAS</h3>\n', sep=""); |
30 s <- paste(s, '<ul>\n', sep=""); | 32 s <- paste(s, '<ul>\n', sep=""); |
31 for (i in 1:length(files)) { | 33 for (i in 1:length(files)) { |
32 s <- paste(s, '<li><a href="', files[i], '">', files[i], '</a></li>\n', sep=""); | 34 s <- paste(s, '<li><a href="', files[i], '">', files[i], '</a></li>\n', sep=""); |
33 } | 35 } |
34 s <- paste(s, '<li><a href="tmp">tmp</a></li>\n', sep=""); | |
35 s <- paste(s, '</ul>\n</body>\n</html>', sep=""); | 36 s <- paste(s, '</ul>\n</body>\n</html>', sep=""); |
36 cat(s, file=output); | 37 cat(s, file=output); |
37 } | 38 } |
38 | 39 |
39 tmp_dir = "tmp"; | 40 tmp_dir = "tmp"; |
40 output_tmp_dir = paste(opt$output_files_path, tmp_dir, sep="/"); | 41 output_tmp_dir = paste(opt$output_files_path, tmp_dir, sep="/"); |
41 dir.create(output_tmp_dir, showWarnings=FALSE); | 42 dir.create(output_tmp_dir, showWarnings=FALSE); |
42 | 43 |
43 # Read the prep_input_config text file which has this format: | 44 # Read the ideaspre_input_config text file which has this format: |
44 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype" | 45 # "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 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config)); |
46 # Process data to windows mean. | 47 # Process data to windows mean. |
47 for (i in 1:dim(prep_input_config_matrix)[1]) { | 48 for (i in 1:dim(ideaspre_input_config)[1]) { |
48 file_path = prep_input_config_matrix[i, 3] | 49 file_path = ideaspre_input_config[i, 3] |
49 file_name = prep_input_config_matrix[i, 4] | 50 file_name = ideaspre_input_config[i, 4] |
50 datatype = prep_input_config_matrix[i, 5] | 51 datatype = ideaspre_input_config[i, 5] |
51 if (datatype == "bam") { | 52 if (datatype == "bam") { |
52 cmd = paste("samtools index", file_path); | 53 cmd = paste("samtools index", file_path); |
53 system(cmd); | 54 system(cmd); |
54 bigwig_file_name = paste(file_name, "bw", sep="."); | 55 bigwig_file_name = paste(file_name, "bw", sep="."); |
55 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size); | 56 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size); |
57 } else { | 58 } else { |
58 bigwig_file_name = file_path; | 59 bigwig_file_name = file_path; |
59 } | 60 } |
60 bed_file_name = paste(file_name, "bed", sep="."); | 61 bed_file_name = paste(file_name, "bed", sep="."); |
61 bed_file_path = paste("tmp", bed_file_name, sep="/"); | 62 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 cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_path); |
63 system(cmd); | 64 system(cmd); |
64 cmd = paste("gzip -f", bed_file_path); | 65 cmd = paste("gzip -f", bed_file_path); |
65 system(cmd); | 66 system(cmd); |
66 } | 67 } |
67 | 68 |
68 # Create file1.txt. | 69 # Create temporary file1.txt. |
69 cmd = paste("cut -d' '", opt$prep_input_config, "-f1,2 > file1.txt", sep=" "); | 70 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" "); |
70 system(cmd); | 71 system(cmd); |
71 # Compress the bed files in the tmp directory. | 72 # Compress the bed files in the tmp directory. |
72 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/"); | 73 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/"); |
73 # Create file2.txt. | 74 # Create temporary file2.txt. |
74 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" "); | 75 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" "); |
75 system(cmd); | 76 system(cmd); |
76 # Create the prep_output_config with the format required by IDEAS. | 77 # Create IDEAS_input_config.txt with the format required by IDEAS. |
77 cmd = paste("paste -d' ' file1.txt file2.txt >", opt$prep_output_config, sep=" "); | 78 cmd = paste("paste -d' ' file1.txt file2.txt >", opt$ideas_input_config, sep=" "); |
78 system(cmd); | 79 system(cmd); |
79 # Move the prep_output_config to the output directory. | 80 # Move IDEAS_input_config.txt to the output directory. |
80 to_path = paste(opt$output_files_path, opt$prep_output_config, sep="/"); | 81 to_path = paste(opt$output_files_path, opt$ideas_input_config, sep="/"); |
81 file.rename(opt$prep_output_config, to_path); | 82 file.rename(opt$ideas_input_config, to_path); |
82 # Move the compressed bed files in the tmp | 83 # Move all compressed bed files in the tmp |
83 # directory to the output tmp directory. | 84 # directory to the output tmp directory. |
84 tmp_files = list.files(path=tmp_dir); | 85 tmp_files = list.files(path=tmp_dir); |
85 for (i in 1:length(tmp_files)) { | 86 for (i in 1:length(tmp_files)) { |
86 from_path = paste(tmp_dir, tmp_files[i], sep="/"); | 87 from_path = paste(tmp_dir, tmp_files[i], sep="/"); |
87 to_path = paste(output_tmp_dir, tmp_files[i], sep="/"); | 88 to_path = paste(output_tmp_dir, tmp_files[i], sep="/"); |
88 file.rename(from_path, to_path); | 89 file.rename(from_path, to_path); |
89 } | 90 } |
90 if (!is.null(opt$windows_positions_by_chroms_config)) { | 91 if (!is.nulll(opt$chrom_bed_input) && !is.null(opt$chromosome_windows)) { |
91 # Move the windows_positions_by_chroms_config to the output directory. | 92 # Rename chrom_bed_input to chromosomes.txt |
92 to_path = paste(opt$output_files_path, opt$windows_positions_by_chroms_config, sep="/"); | 93 # and move it to the output directory. |
93 file.rename(opt$windows_positions_by_chroms_config, to_path); | 94 to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/"); |
95 file.rename(opt$chrom_bed_input, to_path) | |
96 # Move chromosome_windows.txt to the output directory. | |
97 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/"); | |
98 file.rename(opt$chromosome_windows, to_path); | |
94 } | 99 } |
95 # Create the primary HTML dataset. | 100 # Create the primary HTML dataset. |
96 create_primary_html(opt$output, opt$output_files_path); | 101 create_primary_html(opt$output, opt$output_hid, opt$output_files_path); |