1
|
1 #!/usr/bin/env Rscript
|
|
2
|
14
|
3 # TODO: implement support for the following:
|
|
4 # 1. Scenario where user did not select chrom_bed_input
|
|
5 # 2. --exclude_bed_input
|
|
6 # 3. --bychr
|
|
7 # 4. --chrom_len_file
|
|
8 # 5. --reads_per_bp
|
|
9 # 6. --restrict_to_chroms
|
|
10 # 7. --standardize_datasets
|
|
11 # 8. Scenario where --window_size is NULL and need to handle bamCoverage - see TODO near line # 57.
|
|
12
|
|
13
|
1
|
14 suppressPackageStartupMessages(library("data.table"))
|
|
15 suppressPackageStartupMessages(library("optparse"))
|
|
16
|
|
17 option_list <- list(
|
12
|
18 make_option(c("--chrom_bed_input"), action="store", dest="chrom_bed_input", defaul=NULL, help="Chromosome windows positions file"),
|
14
|
19 make_option(c("--exclude_bed_input"), action="store", dest="exclude_bed_input", defaul=NULL, help="File(s) containing regions to exclude"),
|
3
|
20 make_option(c("--bychr"), action="store_true", dest="bychr", defaul=FALSE, help="Separate files by chromosome"),
|
|
21 make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", default=NULL, help="Chromosome lengths file"),
|
5
|
22 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"),
|
3
|
23 make_option(c("--output"), action="store", dest="output", help="Primary output dataset"),
|
|
24 make_option(c("--output_files_path"), action="store", dest="output_files_path", help="Primary output dataset extra files path"),
|
|
25 make_option(c("--reads_per_bp"), action="store", dest="reads_per_bp", type="integer", help="Calculate the signal in each genomic window"),
|
|
26 make_option(c("--restrict_to_chroms"), action="store", dest="restrict_to_chroms", default=NULL, help="Restrict processing to specified chromosomes"),
|
|
27 make_option(c("--standardize_datasets"), action="store_true", dest="standardize_datasets", default=FALSE, help="Standardize all datasets"),
|
5
|
28 make_option(c("--chromosome_windows"), action="store", dest="chromosome_windows", default=NULL, help="Windows positions by chroms config file"),
|
3
|
29 make_option(c("--window_size"), action="store", dest="window_size", type="integer", default=NULL, help="Window size in base pairs")
|
1
|
30 )
|
|
31
|
|
32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
33 args <- parse_args(parser, positional_arguments=TRUE)
|
|
34 opt <- args$options
|
|
35
|
22
|
36 tmp_dir = "tmp";
|
|
37
|
5
|
38 # Read the ideaspre_input_config text file which has this format:
|
1
|
39 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
|
5
|
40 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config));
|
14
|
41
|
|
42 # TODO: fix this
|
|
43 window_size = opt$window_size
|
|
44 if (is.null(opt$window_size)) {
|
|
45 window_size = 500;
|
|
46 }
|
1
|
47 # Process data to windows mean.
|
12
|
48 if (!is.null(opt$chrom_bed_input)) {
|
|
49 for (i in 1:dim(ideaspre_input_config)[1]) {
|
|
50 file_path = ideaspre_input_config[i, 3]
|
|
51 file_name = ideaspre_input_config[i, 4]
|
|
52 datatype = ideaspre_input_config[i, 5]
|
|
53 if (datatype == "bam") {
|
|
54 cmd = paste("samtools index", file_path);
|
|
55 system(cmd);
|
|
56 bigwig_file_name = paste(file_name, "bw", sep=".");
|
14
|
57
|
|
58 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", window_size);
|
12
|
59 system(cmd);
|
|
60 } else {
|
|
61 bigwig_file_name = file_path;
|
|
62 }
|
|
63 bed_file_name = paste(file_name, "bed", sep=".");
|
22
|
64 bed_file_path = paste("tmp", bed_file_name, sep="/");
|
|
65 cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_path);
|
3
|
66 system(cmd);
|
22
|
67 cmd = paste("gzip -f", bed_file_path);
|
12
|
68 system(cmd);
|
1
|
69 }
|
|
70 }
|
|
71
|
9
|
72 # Create file1.txt.
|
5
|
73 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" ");
|
3
|
74 system(cmd);
|
22
|
75 # Compress the bed files in the tmp directory.
|
|
76 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/");
|
|
77 # Create file2.txt.
|
|
78 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" ");
|
3
|
79 system(cmd);
|
12
|
80 # Create IDEAS_input_config.txt with the format required by IDEAS.
|
|
81 ideas_input_config = "IDEAS_input_config.txt"
|
|
82 cmd = paste("paste -d' ' file1.txt file2.txt >", ideas_input_config, sep=" " );
|
3
|
83 system(cmd);
|
12
|
84 # Move IDEAS_input_config.txt to the output directory.
|
|
85 to_path = paste(opt$output_files_path, ideas_input_config, sep="/");
|
|
86 file.rename(ideas_input_config, to_path);
|
22
|
87 # Archive the tmp directory.
|
|
88 cmd = "tar -cvf tmp.tar tmp";
|
|
89 system(cmd);
|
25
|
90 # Compress the archive.
|
|
91 cmd = "gzip tmp.tar";
|
|
92 system(cmd);
|
22
|
93 # Move the tmp archive to the output directory.
|
25
|
94 to_path = paste(opt$output_files_path, "tmp.tar.gz", sep="/");
|
|
95 file.rename("tmp.tar.gz", to_path);
|
22
|
96
|
12
|
97 if (!is.null(opt$chrom_bed_input) && !is.null(opt$chromosome_windows)) {
|
|
98 # Renane opt$chrom_bed_input to be chromosomes.bed
|
|
99 # and make a copy of it in the output directory.
|
|
100 to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/");
|
15
|
101 file.copy(opt$chrom_bed_input, to_path);
|
12
|
102 # Move chromosome_windows.txt to the output directory.
|
5
|
103 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/");
|
|
104 file.rename(opt$chromosome_windows, to_path);
|
3
|
105 }
|
25
|
106
|