comparison ideas_preprocessor.R @ 12:ab0f306504a3 draft

Uploaded
author greg
date Wed, 24 Jan 2018 13:47:23 -0500
parents f45e461b0557
children 20c21d946a8e
comparison
equal deleted inserted replaced
11:26eb979cf8d9 12:ab0f306504a3
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("--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"), 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("--ideas_input_config"), action="store", dest="ideas_input_config", help="Preprocessing output config file"),
12 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"), 11 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"),
13 make_option(c("--output"), action="store", dest="output", help="Primary output dataset"), 12 make_option(c("--output"), action="store", dest="output", help="Primary output dataset"),
14 make_option(c("--output_files_path"), action="store", dest="output_files_path", help="Primary output dataset extra files path"), 13 make_option(c("--output_files_path"), action="store", dest="output_files_path", help="Primary output dataset extra files path"),
15 make_option(c("--output_hid"), action="store", dest="output_hid", help="Primary output dataset hid"), 14 make_option(c("--output_hid"), action="store", dest="output_hid", help="Primary output dataset hid"),
16 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("--reads_per_bp"), action="store", dest="reads_per_bp", type="integer", help="Calculate the signal in each genomic window"),
42 41
43 # Read the ideaspre_input_config text file which has this format: 42 # Read the ideaspre_input_config text file which has this format:
44 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype" 43 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
45 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config)); 44 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config));
46 # Process data to windows mean. 45 # Process data to windows mean.
47 for (i in 1:dim(ideaspre_input_config)[1]) { 46 # TODO: implement scenario where user did not select chrom_bed_input.
48 file_path = ideaspre_input_config[i, 3] 47 if (!is.null(opt$chrom_bed_input)) {
49 file_name = ideaspre_input_config[i, 4] 48 for (i in 1:dim(ideaspre_input_config)[1]) {
50 datatype = ideaspre_input_config[i, 5] 49 file_path = ideaspre_input_config[i, 3]
51 if (datatype == "bam") { 50 file_name = ideaspre_input_config[i, 4]
52 cmd = paste("samtools index", file_path); 51 datatype = ideaspre_input_config[i, 5]
52 if (datatype == "bam") {
53 cmd = paste("samtools index", file_path);
54 system(cmd);
55 bigwig_file_name = paste(file_name, "bw", sep=".");
56 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size);
57 system(cmd);
58 } else {
59 bigwig_file_name = file_path;
60 }
61 bed_file_name = paste(file_name, "bed", sep=".");
62 bed_file_path = paste("tmp", bed_file_name, sep="/");
63 cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_path);
53 system(cmd); 64 system(cmd);
54 bigwig_file_name = paste(file_name, "bw", sep="."); 65 cmd = paste("gzip -f", bed_file_path);
55 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size);
56 system(cmd); 66 system(cmd);
57 } else {
58 bigwig_file_name = file_path;
59 } 67 }
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);
66 } 68 }
67 69
68 # Create file1.txt. 70 # Create file1.txt.
69 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" "); 71 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" ");
70 system(cmd); 72 system(cmd);
71 # Compress the bed files in the tmp directory. 73 # Compress the bed files in the tmp directory.
72 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/"); 74 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/");
73 # Create file2.txt. 75 # Create file2.txt.
74 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" "); 76 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" ");
75 system(cmd); 77 system(cmd);
76 # Create the ideas_input_config with the format required by IDEAS. 78 # Create IDEAS_input_config.txt with the format required by IDEAS.
77 cmd = paste("paste -d' ' file1.txt file2.txt >", opt$ideas_input_config, sep=" "); 79 ideas_input_config = "IDEAS_input_config.txt"
80 cmd = paste("paste -d' ' file1.txt file2.txt >", ideas_input_config, sep=" " );
78 system(cmd); 81 system(cmd);
79 # Move the ideas_input_config to the output directory. 82 # Move IDEAS_input_config.txt to the output directory.
80 to_path = paste(opt$output_files_path, opt$ideas_input_config, sep="/"); 83 to_path = paste(opt$output_files_path, ideas_input_config, sep="/");
81 file.rename(opt$ideas_input_config, to_path); 84 file.rename(ideas_input_config, to_path);
82 # Move the compressed bed files in the tmp 85 # Move the compressed bed files in the tmp
83 # directory to the output tmp directory. 86 # directory to the output tmp directory.
84 tmp_files = list.files(path=tmp_dir); 87 tmp_files = list.files(path=tmp_dir);
85 for (i in 1:length(tmp_files)) { 88 for (i in 1:length(tmp_files)) {
86 from_path = paste(tmp_dir, tmp_files[i], sep="/"); 89 from_path = paste(tmp_dir, tmp_files[i], sep="/");
87 to_path = paste(output_tmp_dir, tmp_files[i], sep="/"); 90 to_path = paste(output_tmp_dir, tmp_files[i], sep="/");
88 file.rename(from_path, to_path); 91 file.rename(from_path, to_path);
89 } 92 }
90 if (!is.null(opt$chromosome_windows)) { 93 if (!is.null(opt$chrom_bed_input) && !is.null(opt$chromosome_windows)) {
91 # Move the chromosome_windows to the output directory. 94 # Renane opt$chrom_bed_input to be chromosomes.bed
95 # and make a copy of it in the output directory.
96 to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/");
97 file.copy(opt$chromosome_windows, to_path);
98 # Move chromosome_windows.txt to the output directory.
92 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/"); 99 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/");
93 file.rename(opt$chromosome_windows, to_path); 100 file.rename(opt$chromosome_windows, to_path);
94 } 101 }
95 # Create the primary HTML dataset. 102 # Create the primary HTML dataset.
96 create_primary_html(opt$output, opt$output_hid, opt$output_files_path); 103 create_primary_html(opt$output, opt$output_hid, opt$output_files_path);