changeset 12:ab0f306504a3 draft

Uploaded
author greg
date Wed, 24 Jan 2018 13:47:23 -0500
parents 26eb979cf8d9
children 4d542da396a7
files ideas_preprocessor.R
diffstat 1 files changed, 33 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/ideas_preprocessor.R	Wed Jan 24 13:47:16 2018 -0500
+++ b/ideas_preprocessor.R	Wed Jan 24 13:47:23 2018 -0500
@@ -4,11 +4,10 @@
 suppressPackageStartupMessages(library("optparse"))
 
 option_list <- list(
-        make_option(c("--bed_input"), action="store", dest="bed_input", defaul=NULL, help="Chromosome windows positions file"),
+        make_option(c("--chrom_bed_input"), action="store", dest="chrom_bed_input", defaul=NULL, help="Chromosome windows positions file"),
         make_option(c("--exclude_input"), action="store", dest="exclude_input", defaul=NULL, help="File(s) containing regions to exclude"),
         make_option(c("--bychr"), action="store_true", dest="bychr", defaul=FALSE, help="Separate files by chromosome"),
         make_option(c("--chrom_len_file"), action="store", dest="chrom_len_file", default=NULL, help="Chromosome lengths file"),
-        make_option(c("--ideas_input_config"), action="store", dest="ideas_input_config", help="Preprocessing output config file"),
         make_option(c("--ideaspre_input_config"), action="store", dest="ideaspre_input_config", help="Preprocessing input config file"),
         make_option(c("--output"), action="store", dest="output", help="Primary output dataset"),
         make_option(c("--output_files_path"), action="store", dest="output_files_path", help="Primary output dataset extra files path"),
@@ -44,25 +43,28 @@
 # "cell type name" "epigenetic factor name" "file path" "file name" "datatype"
 ideaspre_input_config = as.matrix(read.table(opt$ideaspre_input_config));
 # Process data to windows mean.
-for (i in 1:dim(ideaspre_input_config)[1]) {
-    file_path = ideaspre_input_config[i, 3]
-    file_name = ideaspre_input_config[i, 4]
-    datatype = ideaspre_input_config[i, 5]
-    if (datatype == "bam") {
-        cmd = paste("samtools index", file_path);
-        system(cmd);
-        bigwig_file_name = paste(file_name, "bw", sep=".");
-        cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size);
+# TODO: implement scenario where user did not select chrom_bed_input.
+if (!is.null(opt$chrom_bed_input)) {
+    for (i in 1:dim(ideaspre_input_config)[1]) {
+        file_path = ideaspre_input_config[i, 3]
+        file_name = ideaspre_input_config[i, 4]
+        datatype = ideaspre_input_config[i, 5]
+        if (datatype == "bam") {
+            cmd = paste("samtools index", file_path);
+            system(cmd);
+            bigwig_file_name = paste(file_name, "bw", sep=".");
+            cmd = paste("bamCoverage --bam", file_path, "-o", bigwig_file_name, "--binSize", opt$window_size);
+            system(cmd);
+        } else {
+            bigwig_file_name = file_path;
+        }
+        bed_file_name = paste(file_name, "bed", sep=".");
+        bed_file_path = paste("tmp", bed_file_name, sep="/");
+        cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$chrom_bed_input, "stdout | cut -f5 >", bed_file_path);
         system(cmd);
-    } else {
-        bigwig_file_name = file_path;
+        cmd = paste("gzip -f", bed_file_path);
+        system(cmd);
     }
-    bed_file_name = paste(file_name, "bed", sep=".");
-    bed_file_path = paste("tmp", bed_file_name, sep="/");
-    cmd = paste("bigWigAverageOverBed", bigwig_file_name, opt$bed_input, "stdout | cut -f5 >", bed_file_path);
-    system(cmd);
-    cmd = paste("gzip -f", bed_file_path);
-    system(cmd);
 }
 
 # Create file1.txt.
@@ -73,12 +75,13 @@
 # Create file2.txt.
 cmd = paste("ls", tmp_gzipped_files, "> file2.txt", sep=" ");
 system(cmd);
-# Create the ideas_input_config with the format required by IDEAS.
-cmd = paste("paste -d' ' file1.txt file2.txt >", opt$ideas_input_config, sep=" ");
+# Create IDEAS_input_config.txt  with the format required by IDEAS.
+ideas_input_config = "IDEAS_input_config.txt"
+cmd = paste("paste -d' ' file1.txt file2.txt >", ideas_input_config, sep=" " );
 system(cmd);
-# Move the ideas_input_config to the output directory.
-to_path = paste(opt$output_files_path, opt$ideas_input_config, sep="/");
-file.rename(opt$ideas_input_config, to_path);
+# Move IDEAS_input_config.txt to the output directory.
+to_path = paste(opt$output_files_path, ideas_input_config, sep="/");
+file.rename(ideas_input_config, to_path);
 # Move the compressed bed files in the tmp
 # directory to the output tmp directory.
 tmp_files = list.files(path=tmp_dir);
@@ -87,8 +90,12 @@
     to_path = paste(output_tmp_dir, tmp_files[i], sep="/");
     file.rename(from_path, to_path);
 }
-if (!is.null(opt$chromosome_windows)) {
-    # Move the chromosome_windows to the output directory.
+if (!is.null(opt$chrom_bed_input) && !is.null(opt$chromosome_windows)) {
+    # Renane opt$chrom_bed_input to be chromosomes.bed
+    # and make a copy of it in the output directory.
+    to_path = paste(opt$output_files_path, "chromosomes.bed", sep="/");
+    file.copy(opt$chromosome_windows, to_path);
+    # Move chromosome_windows.txt to the output directory.
     to_path = paste(opt$output_files_path, opt$chromosome_windows, sep="/");
     file.rename(opt$chromosome_windows, to_path);
 }