1
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("data.table"))
|
|
4 suppressPackageStartupMessages(library("optparse"))
|
|
5
|
|
6 option_list <- list(
|
12
|
7 make_option(c("--chrom_bed_input"), action="store", dest="chrom_bed_input", defaul=NULL, help="Chromosome windows positions file"),
|
14
|
8 make_option(c("--exclude_bed_input"), action="store", dest="exclude_bed_input", defaul=NULL, help="File(s) containing regions to exclude"),
|
3
|
9 make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", default=NULL, help="Chromosome lengths file"),
|
5
|
10 make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"),
|
3
|
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"),
|
5
|
13 make_option(c("--chromosome_windows"), action="store", dest="chromosome_windows", default=NULL, help="Windows positions by chroms config file"),
|
3
|
14 make_option(c("--window_size"), action="store", dest="window_size", type="integer", default=NULL, help="Window size in base pairs")
|
1
|
15 )
|
|
16
|
|
17 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
18 args <- parse_args(parser, positional_arguments=TRUE)
|
|
19 opt <- args$options
|
|
20
|
22
|
21 tmp_dir = "tmp";
|
27
|
22 cbi_file = "chrom_bed_input.bed";
|
22
|
23
|
27
|
24 if (is.null(opt$chrom_bed_input)) {
|
|
25 # Create a chromosome windows positions file
|
|
26 # using the received chromosome lengths file
|
|
27 # and the window size.
|
|
28 cmd = paste("bedtools makewindows -g", opt$chrom_len_file, "-w", opt$window_size, ">", cbi_file, sep=" ");
|
|
29 system(cmd);
|
|
30 } else {
|
|
31 if (!is.null(opt$exclude_bed_input)) {
|
|
32 # Copy the received chrom_bed_input
|
|
33 # since we will alter it.
|
|
34 file.copy(opt$chrom_bed_input, cbi_file);
|
|
35 } else {
|
|
36 cbi_file = opt$chrom_bed_input;
|
|
37 }
|
|
38 }
|
|
39 # Exclude regions if necessary.
|
|
40 if (!is.null(opt$exclude_bed_input)) {
|
|
41 exclude_bed_inputs = as.character(opt$exclude_bed_input);
|
|
42 exclude_bed_files = strsplit(exclude_bed_inputs, ",");
|
|
43 tmp_file = paste("tmp", cbi_file, sep="_");
|
|
44 for (exclude_bed_file in exclude_bed_files) {
|
|
45 cmd = paste("bedtools subtract -a", cbi_file, "-b", exclude_bed_file, ">", tmp_file, sep=" ");
|
|
46 system(cmd);
|
|
47 cmd = paste("mv", tmp_file, cbi_file, sep=" ");
|
|
48 system(cmd);
|
|
49 }
|
|
50 }
|
|
51 # Read the chromosome windows positions file
|
|
52 # to get the smallest window size in the file
|
|
53 # (i.e., the minimum of column 3 - column 2.
|
|
54 cbi = fread(cbi_file);
|
|
55 min_window_size = min(cbi[,3]-cbi[,2]);
|
5
|
56 # Read the ideaspre_input_config text file which has this format:
|
1
|
57 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
|
5
|
58 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config));
|
27
|
59 # Process data to windows mean.
|
|
60 for (i in 1:dim(ideaspre_input_config)[1]) {
|
|
61 file_path = ideaspre_input_config[i, 3]
|
|
62 file_name = ideaspre_input_config[i, 4]
|
|
63 datatype = ideaspre_input_config[i, 5]
|
|
64 if (datatype == "bam") {
|
|
65 cmd = paste("samtools index", file_path);
|
|
66 system(cmd);
|
|
67 bigwig_file_name = paste(file_name, "bw", sep=".");
|
|
68 cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", min_window_size);
|
|
69 system(cmd);
|
|
70 } else {
|
|
71 bigwig_file_name = file_path;
|
|
72 }
|
|
73 bed_file_name = paste(file_name, "bed", sep=".");
|
|
74 bed_file_path = paste("tmp", bed_file_name, sep="/");
|
|
75 cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_path);
|
|
76 system(cmd);
|
|
77 cmd = paste("gzip -f", bed_file_path);
|
|
78 system(cmd);
|
14
|
79 }
|
9
|
80 # Create file1.txt.
|
5
|
81 cmd = paste("cut -d' '", opt$ideaspre_input_config, "-f1,2 > file1.txt", sep=" ");
|
3
|
82 system(cmd);
|
22
|
83 # Compress the bed files in the tmp directory.
|
|
84 tmp_gzipped_files = paste(tmp_dir, "*.bed.gz", sep="/");
|
|
85 # Create file2.txt.
|
|
86 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" ");
|
3
|
87 system(cmd);
|
12
|
88 # Create IDEAS_input_config.txt with the format required by IDEAS.
|
|
89 ideas_input_config = "IDEAS_input_config.txt"
|
|
90 cmd = paste("paste -d' ' file1.txt file2.txt >", ideas_input_config, sep=" " );
|
3
|
91 system(cmd);
|
12
|
92 # Move IDEAS_input_config.txt to the output directory.
|
|
93 to_path = paste(opt$output_files_path, ideas_input_config, sep="/");
|
|
94 file.rename(ideas_input_config, to_path);
|
22
|
95 # Archive the tmp directory.
|
|
96 cmd = "tar -cvf tmp.tar tmp";
|
|
97 system(cmd);
|
25
|
98 # Compress the archive.
|
|
99 cmd = "gzip tmp.tar";
|
|
100 system(cmd);
|
22
|
101 # Move the tmp archive to the output directory.
|
25
|
102 to_path = paste(opt$output_files_path, "tmp.tar.gz", sep="/");
|
|
103 file.rename("tmp.tar.gz", to_path);
|
27
|
104 # Handle file names for display in the primary dataset if necessary.
|
29
|
105 to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/");
|
|
106 if (is.null(opt$chrom_bed_input) {
|
|
107 # Move cbi_file to the output directory,
|
|
108 # naming it chromosomes.bed.
|
|
109 file.rename(cbi_file, to_path);
|
|
110 } else {
|
|
111 # Copy opt$chrom_bed_input to the output
|
|
112 # directory, naming it chromosomes.bed.
|
15
|
113 file.copy(opt$chrom_bed_input, to_path);
|
29
|
114 }
|
|
115 if (!is.null(opt$chromosome_windows)) {
|
12
|
116 # Move chromosome_windows.txt to the output directory.
|
5
|
117 to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/");
|
|
118 file.rename(opt$chromosome_windows, to_path);
|
3
|
119 }
|