0512_tadaA_devel
In [1]:
library(tadaA)
setwd("/project/wgs/analysis/TADA/TADA-A")

Adjust mutation rate

In [2]:
TADA_A_adjust_mutrate(window_file = 'test_data/test_windows.txt',
                      mut_file = 'test_data/test_simulated_DNM.bed',
                      genes = "all",sample_size = 5000,
                      mutrate_mode = "regular",
                      scaling_file_name = "test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt",
                      scale_features = c("GC_content", "div_score"))
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a test_data/test_simulated_DNM.bed -b tmp/129031944_temp_windows.bed 
Warning message in if (!is.na(scale_features)) {:
“the condition has length > 1 and only the first element will be used”
A matrix: 5 × 4 of type dbl
EstimateStd. Errorz valuePr(>|z|)
(Intercept) -0.01781306 0.03297785-0.540152355.890920e-01
coding 0.17063881 0.09523129 1.791835517.315932e-02
promoter-15.57912267160.47707125-0.097080059.226628e-01
GC_content 0.02344618 0.03182928 0.736623084.613516e-01
div_score -0.32024953 0.03596375-8.904786845.348873e-19

Read in DNM data, annotation data and mutation rate data

In [3]:
system.time(compact_data <- TADA_A_read_info(
mut_files = c("test_data/test_simulated_DNM_with_allele_info.txt"),
window_file = "test_data/test_windows.txt",
mutrate_scaling_files = c("test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt"),
sample_sizes = c(5000),
gene_prior_file = "test_data/test_gene_prior.txt",
nonAS_noncoding_annotations = "test_data/test_non_allele_specific_annotation_1.bed",
AS_noncoding_annotations = NA,
report_proportion = 2000/18665,
chunk_partition_num =1,
node_n = 1,
mutrate_ref_files = c("test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_A.mutrate.bw", 
                      "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_C.mutrate.bw",
                      "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_G.mutrate.bw" ,
                      "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_T.mutrate.bw")))
[1] "Finished reading window file test_data/test_windows.txt. Time consumed: 0.014s"
[1] "Finished reading mutrate scaling file test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt. Time consumed: 0.003s"
[1] "Finished reading gene prior file test_data/test_gene_prior.txt. Time consumed: 0.003s"
[1] "Choose the top 10.72 % genes for parameter estimation."
[1] "Finished partitioning base-level coordinates data at Round 1. Time consumed: 12.215s"
[1] "Write out tmp/1290363181_temp_for_mutrate.bed. Time consumed: 0.261s"
[1] "Finished obtaining base-level mutation rate for alt allele A. Time consumed: 4.139s"
[1] "Finished obtaining base-level mutation rate for alt allele C. Time consumed: 4.781s"
[1] "Finished obtaining base-level mutation rate for alt allele G. Time consumed: 4.67s"
[1] "Finished obtaining base-level mutation rate for alt allele T. Time consumed: 4.724s"
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a test_data/test_non_allele_specific_annotation_1.bed -b tmp/1290363181_temp_for_mutrate.bed 
[1] "Finished reading non-allele specific noncoding annotations 1. Time consumed: 9.436s"

[1] "Begin collapsing data based on noncoding anotation configuration......"
[1] "Finished reading test_data/test_simulated_DNM_with_allele_info.txt"

[1] "Alt allele A:"
[1] "Finished configurating noncoding annotation for alt allele A. Time consumed: 0.003s"
[1] "Finished removing bases that have adjusted_mutrate_base 0 and computing likehood valid. Time consumed: 0.533s"
[1] "Finished write tmp/1290363181_temp_mut_allele.bed"
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a tmp/1290363181_temp_mut_allele.bed -b tmp/1290363181_temp_for_mutrate.bed 
[1] "Finished obtaining noncoding mutation rate. Time consumed: 8.177s"
[1] "Collapse data......"
[1] "remove bases that don't have any non-coding features. Time cosumed: 0.007s"
[1] "partition by gene for the current chunk. Time cosumed: 0.01s"
[1] "partition by feature configuration for each gene in the current chunk. Time cosumed: 0.089s"
[1] "add compact data and release memory."
[1] "Finished read in mutation data and make them into the compact format for Study 1 and allele A"

[1] "Alt allele C:"
[1] "Finished configurating noncoding annotation for alt allele C. Time consumed: 0.003s"
[1] "Finished removing bases that have adjusted_mutrate_base 0 and computing likehood valid. Time consumed: 0.217s"
[1] "Finished write tmp/1290363181_temp_mut_allele.bed"
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a tmp/1290363181_temp_mut_allele.bed -b tmp/1290363181_temp_for_mutrate.bed 
[1] "Finished obtaining noncoding mutation rate. Time consumed: 7.619s"
[1] "Collapse data......"
[1] "remove bases that don't have any non-coding features. Time cosumed: 0.007s"
[1] "partition by gene for the current chunk. Time cosumed: 0.009s"
[1] "partition by feature configuration for each gene in the current chunk. Time cosumed: 0.082s"
[1] "add compact data and release memory."
[1] "Finished read in mutation data and make them into the compact format for Study 1 and allele C"

[1] "Alt allele G:"
[1] "Finished configurating noncoding annotation for alt allele G. Time consumed: 0.003s"
[1] "Finished removing bases that have adjusted_mutrate_base 0 and computing likehood valid. Time consumed: 0.055s"
[1] "Finished write tmp/1290363181_temp_mut_allele.bed"
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a tmp/1290363181_temp_mut_allele.bed -b tmp/1290363181_temp_for_mutrate.bed 
[1] "Finished obtaining noncoding mutation rate. Time consumed: 8.269s"
[1] "Collapse data......"
[1] "remove bases that don't have any non-coding features. Time cosumed: 0.007s"
[1] "partition by gene for the current chunk. Time cosumed: 0.009s"
[1] "partition by feature configuration for each gene in the current chunk. Time cosumed: 0.08s"
[1] "add compact data and release memory."
[1] "Finished read in mutation data and make them into the compact format for Study 1 and allele G"

[1] "Alt allele T:"
[1] "Finished configurating noncoding annotation for alt allele T. Time consumed: 0.083s"
[1] "Finished removing bases that have adjusted_mutrate_base 0 and computing likehood valid. Time consumed: 0.133s"
[1] "Finished write tmp/1290363181_temp_mut_allele.bed"
 * Processing input (1): a
 * Processing input (2): b
   bedtools coverage -a tmp/1290363181_temp_mut_allele.bed -b tmp/1290363181_temp_for_mutrate.bed 
[1] "Finished obtaining noncoding mutation rate. Time consumed: 7.929s"
[1] "Collapse data......"
[1] "remove bases that don't have any non-coding features. Time cosumed: 0.006s"
[1] "partition by gene for the current chunk. Time cosumed: 0.008s"
[1] "partition by feature configuration for each gene in the current chunk. Time cosumed: 0.089s"
[1] "add compact data and release memory."
[1] "Finished read in mutation data and make them into the compact format for Study 1 and allele T"

[1] "Temp files cleaned and data recording finished!"
   user  system elapsed 
 68.993  10.839  73.977 
In [17]:
length(compact_data$base_info)
100
In [16]:
compact_data$base_info$ACTL8
$`1` =
$feature_vector
1
$sum_mut_rate_count
0
$sum_mut_rate
0.132618820975927
$sum_mut_count
0
$log_fcount
0

Estimate relative risks

In [4]:
RR = TADA_A_RR_estimate(data = compact_data$base_info,
                    selected_annotations = c(1), gene_prior_file = "test_data/test_gene_prior.txt",
                    optimization_iteration = 2000)
In [5]:
RR
$mle
$par
2.60243512738338
$value
-301.828299056853
$counts
function
<NA>
gradient
<NA>
$convergence
0
$message
NULL
$hessian
A matrix: 1 × 1 of type dbl
-18.45476
$rr_report
A data.frame: 1 × 3
relative_risklower_boundupper_bournd
<dbl><dbl><dbl>
2.6024352.1461863.058684

Bayesian FDR control

In [18]:
Bayesian_FDR(BF=c(seq(1,10)), pi0=0.05, alpha=0.05)
$FDR
  1. 0.05
  2. 0.0378205128205129
  3. 0.0309608016504568
  4. 0.0264673544845959
  5. 0.02325721692101
  6. 0.0208302894631605
  7. 0.0189206319065896
  8. 0.0173725463823182
  9. 0.0160882582829909
  10. 0.0150029926641159
$ND
1

Bayes factor calculation

  • This function is for calculating Bayes factors for based on noncoding annotations and then combine with coding Bayes factors in order to predict risk genes.
  • calculate the Bayes factor for each gene based on informative noncodng annotations.

parameters:

  • [data] is the [base_info] returned from [TADA_A_read_info], each element of the list has compact base-level information for each gene.
  • [selected_annotations] determines which features in the [data] are going to be used for calculating bayes factors. e.g., if [selected_annotations] is c(1,3). Then the first and third element of feature_vector in the [base_info] of [data] will be selected.
  • [rr] is the estimated relative risks of [selected_annotations]. RRs are estimated from [TADA_A_RR_estimate].
  • RRs are in the log scale
  • [additional_BF_file], a file that has additional Bayes factors that could be uesd along with noncoding-derived BFs to increase the power of risk gene mapping. e.g., "../data/Example_gene_coding_BF.txt"
  • [TADA_p0] the proportion of genes being non-risk genes. Default = 0.94 for in the context of ASD risk gene mapping

Note:

%*% : non-conformable arguments

  • There is a safer method for handling the "matrix-multiplication" of vectors, the crossprod function.
In [76]:
TADA_A_get_BFs(data = compact_data$base_info,
               selected_annotations = c(1), 
               rr = RR$rr_report$relative_risk, 
               additional_BF_file = "tmp/tmp_gene_ad_bf", 
               TADA_p0 = 0.94)
$gene_BF_table
A data.table: 27 × 9
genenamelogBF_noncodinglogBF_codinglogBF_allBF_noncodingBF_codingBF_allFDR_codingFDR_all
<chr><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
TMEM201 26.3415048-0.6931472 25.64835772.754040e+110.51.377020e+110.96907221.137722e-10
RAB42 4.5764454-0.6931472 3.88329829.716838e+010.54.858419e+010.96907221.219180e-01
PINK1 3.0806472-0.6931472 2.38750012.177249e+010.51.088624e+010.96907222.779509e-01
MASP2 0.0000000-0.6931472 -0.69314721.000000e+000.55.000000e-010.96907224.507312e-01
SLC25A34 0.0000000-0.6931472 -0.69314721.000000e+000.55.000000e-010.96907225.543994e-01
NMNAT1 -0.4162573-0.6931472 -1.10940446.595106e-010.53.297553e-010.96907226.252305e-01
HTR1D -0.4973874-0.6931472 -1.19053456.081174e-010.53.040587e-010.96907226.760492e-01
SCNN1D -0.6722488-0.6931472 -1.36539605.105591e-010.52.552796e-010.96907227.145389e-01
KLHL17 -1.4585325-0.6931472 -2.15167962.325773e-010.51.162887e-010.96907227.454381e-01
TMEM52 -1.5645019-0.6931472 -2.25764912.091922e-010.51.045961e-010.96907227.702311e-01
RPA2 -1.6240115-0.6931472 -2.31715871.971064e-010.59.855321e-020.96907227.905509e-01
TNFRSF8 -3.3036202-0.6931472 -3.99676743.674988e-020.51.837494e-020.96907228.079074e-01
KDM1A -3.4164941-0.6931472 -4.10964133.282732e-020.51.641366e-020.96907228.226032e-01
HNRNPR -4.3147014-0.6931472 -5.00784861.337054e-020.56.685270e-030.96907228.352439e-01
C1orf63 -4.4626645-0.6931472 -5.15581171.153160e-020.55.765798e-030.96907228.462032e-01
ACTL8 -4.6400952-0.6931472 -5.33324239.656779e-030.54.828389e-030.96907228.557962e-01
ARHGEF16 -8.5388493-0.6931472 -9.23199651.957153e-040.59.785767e-050.96907228.642784e-01
SAMD11 -9.1412459-0.6931472 -9.83439311.071537e-040.55.357687e-050.96907228.718183e-01
FBLIM1 -9.6799499-0.6931472-10.37309706.252464e-050.53.126232e-050.96907228.785646e-01
SYTL1 -11.0467424-0.6931472-11.73988961.593899e-050.57.969494e-060.96907228.846363e-01
CA6 -11.9420453-0.6931472-12.63519256.510819e-060.53.255410e-060.96907228.901298e-01
SPEN -13.6725320-0.6931472-14.36567921.153705e-060.55.768525e-070.96907228.951239e-01
EPHB2 -15.1021680-0.6931472-15.79531522.761924e-070.51.380962e-070.96907228.996838e-01
SKI -16.4397442-0.6931472-17.13289147.249529e-080.53.624765e-080.96907229.038636e-01
PLEKHG5 -16.8431250-0.6931472-17.53627214.843104e-080.52.421552e-080.96907229.077091e-01
AHDC1 -23.0606776-0.6931472-23.75382489.657728e-110.54.828864e-110.96907229.112587e-01
ECE1 -30.9736921-0.6931472-31.66683923.534243e-140.51.767122e-140.96907229.145454e-01
$genes_with_no_epi
  1. 'MASP2'
  2. 'SLC25A34'

TADA_A_DNM_generator

In [ ]:
window_file = 'test_data/test_windows.txt',
                      mut_file = 'test_data/test_simulated_DNM.bed',
                      genes = "all",sample_size = 5000,
                      mutrate_mode = "regular",
                      scaling_file_name = "test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt",
                      scale_features = c("GC_content", "div_score")
  • [data] is the [base_info] returned from [TADA_A_read_info], each element of the list has compact base-level information for each gene.
  • [selected_annotations] determines which features in the [data] are going to be used for calculating bayes factors. e.g., if [selected_annotations] is c(1,3). Then the first and third element of feature_vector in the [base_info] of [data] will be selected.
  • [rr] is the estimated relative risks of [selected_annotations]. RRs are estimated from [TADA_A_RR_estimate].
  • RRs are in the log scale
  • [additional_BF_file], a file that has additional Bayes factors that could be uesd along with noncoding-derived BFs to increase the power of risk gene mapping. e.g., "../data/Example_gene_coding_BF.txt"
  • [TADA_p0] the proportion of genes being non-risk genes. Default = 0.94 for in the context of ASD risk gene mapping
In [103]:
TADA_A_DNM_generator(window_file =  'test_data/test_windows.txt',
                     mutrate_scaling_files = "test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt",
                     sample_sizes = c(27),
                     gene_prior_file = "test_data/test_gene_prior.txt",
                     nonAS_noncoding_annotations = "test_data/test_non_allele_specific_annotation_1.bed",
                     AS_noncoding_annotations = NA,
                     report_proportion = 100/18665,
                     chunk_partition_num =1 ,
                     node_n = 6,
                     mutrate_ref_files = c("test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_A.mutrate.bw",
                                           "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_C.mutrate.bw",
                                           "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_G.mutrate.bw",
                                           "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_T.mutrate.bw"),
                     rr = RR$rr_report$relative_risk,
                     output_allele_info_files = "test_data/output_allele_info" ,
                     output_bed_files = "test_data/bed_file",
                     output_risk_genes_file = "test_data/riskgenes",
                     compact_mut_output = NA,
                                 MPI = 1)
Error in coverage[[i]]: subscript out of bounds
Traceback:

1. TADA_A_DNM_generator(window_file = "test_data/test_windows.txt", 
 .     mutrate_scaling_files = "test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt", 
 .     sample_sizes = c(27), gene_prior_file = "test_data/test_gene_prior.txt", 
 .     nonAS_noncoding_annotations = "test_data/test_non_allele_specific_annotation_1.bed", 
 .     AS_noncoding_annotations = NA, report_proportion = 100/18665, 
 .     chunk_partition_num = 1, node_n = 6, mutrate_ref_files = c("test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_A.mutrate.bw", 
 .         "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_C.mutrate.bw", 
 .         "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_G.mutrate.bw", 
 .         "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_T.mutrate.bw"), 
 .     rr = RR$rr_report$relative_risk, output_allele_info_files = "test_data/output_allele_info", 
 .     output_bed_files = "test_data/bed_file", output_risk_genes_file = "test_data/riskgenes", 
 .     compact_mut_output = NA, MPI = 1)
2. rbindlist(parApply(cl, coverage[[i]], 1, window_expansion))
3. parApply(cl, coverage[[i]], 1, window_expansion)
In [104]:
window_file =  'test_data/test_windows.txt'
mutrate_scaling_files = "test_data/test_simulated_windows_mutrate_with_div_score_scaling_file_for_test_DNM.txt"
sample_sizes = c(27)
gene_prior_file = "test_data/test_gene_prior.txt"
nonAS_noncoding_annotations = "test_data/test_non_allele_specific_annotation_1.bed"
AS_noncoding_annotations = NA
report_proportion = 100/18665
chunk_partition_num =1 
node_n = 6
mutrate_ref_files = c("test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_A.mutrate.bw",
               "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_C.mutrate.bw",
               "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_G.mutrate.bw",
               "test_data/test_windows_extended_1bp_for_getting_base_level_mutrate.bed.fasta.tri.alt_T.mutrate.bw")
rr = c(1, 0 ,0.5)
output_allele_info_files = "test_data/output_allele_info" 
output_bed_files = "test_data/bed_file"
output_risk_genes_file = "test_data/riskgenes"
compact_mut_output = NA
MPI = 1
In [109]:
base_ID = genename = mut_count = risk = NULL
  prefix <- prefix <- as.integer((as.double(Sys.time())*1000+Sys.getpid()) %% 2^31) # prefix for temporary files that will be deleted at the end of the pipeline
  prefix <- paste("tmp/", prefix, MPI, sep = "")

  # make a tmp folder for tmp files
  system("mkdir -p tmp")

  #mut <- fread(mut_file, header = FALSE, sep = "\t", stringsAsFactors = FALSE)
  windows <- fread(window_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  coverage <- windows

  # the number of genomic windows in [mutrate_scaling] is less than the number of windows in [windows] because there are a few windows with mutration rate equal to 0, and thus removed.
  for(i in 1:length(mutrate_scaling_files)){
    mutrate_scaling <- fread(mutrate_scaling_files[i], header = TRUE, sep = "\t", stringsAsFactors = FALSE)
    system(paste("echo \"Finished reading mutrate scaling file ", mutrate_scaling_files[i], ".\"", sep = ""))
    system("date")
    coverage <- coverage[mutrate_scaling, on = "site_index"]
    coverage <- coverage[complete.cases(coverage)] # release memory
    colnames(coverage)[length(colnames(coverage))] <- paste("scaling_factor_study_", i, sep = "")
    rm(mutrate_scaling) # release memory
  }

  # get the piror probability of genes.
  gene_prior <- fread(gene_prior_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE)

  # merge gene prior info
  coverage <- coverage[gene_prior, on = "genename"]
  coverage <-coverage[complete.cases(coverage)]
In [110]:
head(coverage)
A data.table: 6 × 12
chrstartendsite_indexgenenamemutratecodingpromoterGC_contentdiv_scorescaling_factor_study_1prior
<chr><int><int><int><chr><dbl><int><int><dbl><dbl><dbl><dbl>
chr121606183216062335928ECE17.23582e-07010.680.060186022.114796e-070.3825655
chr121606233216062835929ECE15.33951e-07010.540.060186022.062536e-070.3825655
chr121606283216063335930ECE11.10488e-06010.420.060186022.018770e-070.3825655
chr121606333216063835931ECE14.73064e-07010.480.060186022.040536e-070.3825655
chr121606383216064335932ECE15.22207e-07010.560.060186022.069922e-070.3825655
chr121606433216064835933ECE17.07512e-07010.600.060186022.084773e-070.3825655
In [108]:
# select genes based on TADA prior probability and [report_proportion]
  genes_for_report_with_prior <- gene_prior[order(gene_prior[,2],decreasing = TRUE),]
  genes_for_report_with_prior <- genes_for_report_with_prior[1:floor(nrow(genes_for_report_with_prior)*report_proportion),]
  # randomly assign risk genes based on prior probability
  genes_for_report_with_prior$risk <- rbinom(nrow(genes_for_report_with_prior), 1, genes_for_report_with_prior$prior)
  risk_genes <- genes_for_report_with_prior[risk == 1,]$genename
  nonrisk_genes <- genes_for_report_with_prior[risk == 0,]$genename
  coverage_risk <- coverage[is.element(genename, risk_genes)]
  coverage_risk$risk <- 1
  coverage_nonrisk <- coverage[is.element(genename, nonrisk_genes)]
  coverage_nonrisk$risk <- 0

  coverage <- rbind(coverage_risk, coverage_nonrisk)

  # now need to extropolate window mutation file to base level mutation file
  coverage <-coverage[,c("chr","start","end",paste("scaling_factor_study_", seq(1,length(mutrate_scaling_files)), sep = ""),"genename","risk"),with = FALSE]

  coverage$ID <- paste(coverage$genename, coverage$start, sep = "_")

  total_rows <- nrow(coverage)
  interval <- floor(total_rows/chunk_partition_num)
  data_bins <- c(rep(seq(1,chunk_partition_num), each = interval),rep(chunk_partition_num, total_rows -interval*chunk_partition_num))

  # split into 20 different chunks, then for each chunk split by genes, then by feature configuration. If split by gene at the first level, implementation would take too long
  coverage <- split(coverage, data_bins)

  #funtion to expand windows to bases
  window_expansion <- function(table_row){
    start <- seq(as.numeric(table_row[2]),as.numeric(table_row[3])-1)
    data.frame(table_row[1], start, start+1, paste(table_row["genename"],start,sep = "_"), table_row["ID"])
  }


  bed_extension <- function(table_row){
    data.frame(chr = rep(table_row["chr"], table_row["mut_count"]), start = rep(table_row["start"], table_row["mut_count"]), end = rep(table_row["end"], table_row["mut_count"]))
  }

  options(warn=-1)
  if(node_n != 1){
    cl <- makeCluster(node_n)
  }
  # use parallel computing and rbindlist to increase the speed by thousands of times.
  environment(window_expansion) <- .GlobalEnv
  environment(bed_extension) <- .GlobalEnv
  # get nonAS feature number
  if(is.na(nonAS_noncoding_annotations[1])){
    nonAS_feature_number <- 0
  }else{
    nonAS_feature_number <- length(nonAS_noncoding_annotations)
  }
  # get AS feature number
  if(is.na(AS_noncoding_annotations[1])){
    AS_feature_number <- 0
  }else{
    AS_feature_number <- length(AS_noncoding_annotations)
  }

  # get total feature number
  feature_number = nonAS_feature_number + AS_feature_number
  # function to get effective information of each element of partition_by_gene
  # These information are those necessary to compute log-likelihood in the optimization function
  partition_feature <- function(pbg){
    # input is one element of the list of partition_by_gene
    pbg_split <- split(pbg, pbg[,4:(4 + feature_number - 1)],drop = TRUE)
    feature_combination_number <- length(pbg_split)
    # this function below is different from the function used in dealing with dataset without reading by chunk. Here, prior is not incoporated at this step.
    info_for_each_feature <- function(feature_set){
      list(feature_vector = c(as.numeric(feature_set[1,4:(4 + feature_number - 1)])), sum_mut_rate_count = sum(feature_set$mut_count*log(feature_set$adjusted_base_mutrate)), sum_mut_rate = sum(feature_set$adjusted_base_mutrate), sum_mut_count = sum(feature_set$mut_count), log_fcount = sum(log(factorial(feature_set$mut_count))))
    }
    sapply(pbg_split, info_for_each_feature,simplify = FALSE)
  }


  alt_letters <- c("A","C","G","T")
  mut_output_list <- list()
  data_partition <- list()
  for(m in 1:length(mutrate_scaling_files)){
    mut_output_list[[m]] <- data.table(chr = character(), start = integer(), end = integer(), ref = character(), alt = character())
  }

  # here only deals with the noncoding parts that are within 10kb of TSSs of genes. Will deal with
  for(i in 1:chunk_partition_num){
    # split coverage_noncoding to 10 parts, and for each part, generate feature table (which will be used for )
    if(node_n != 1){
      coverage_noncoding_for_base_mutrate <- rbindlist(parApply(cl, coverage[[i]], 1, window_expansion))
    }else{
      coverage_noncoding_for_base_mutrate <- rbindlist(apply(coverage[[i]], 1, window_expansion))
    }
    system(paste("echo \"Finished partitioning base-level coordinates data at Round ", i, ".\"", sep = ""))
    system("date")
    colnames(coverage_noncoding_for_base_mutrate) <- c("chr","start","end","base_ID","ID")
    coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[!duplicated(base_ID)]
    coverage_noncoding_for_base_mutrate$start <- as.integer(coverage_noncoding_for_base_mutrate$start)
    coverage_noncoding_for_base_mutrate$end <- as.integer(coverage_noncoding_for_base_mutrate$end)

    # write out a bed file to get base-level mutation rates
    fwrite(coverage_noncoding_for_base_mutrate[,1:4],paste(prefix, "_temp_for_mutrate.bed", sep = ""), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
    # read in allele-specific base-level mutation rate
    for(j in 1:length(mutrate_ref_files)){
      command <- paste("../external_tools/bigWigAverageOverBed ", mutrate_ref_files[j], " ", paste(prefix, "_temp_for_mutrate.bed", sep = ""), " ", paste(prefix, "_temp_for_mutrate.bed.mutrate", sep = "" ), sep = "")
      system(command)
      command <- paste("awk {'print $1\"\t\"$4'} ", paste(prefix, "_temp_for_mutrate.bed.mutrate", sep = ""), " > ", paste(prefix, "_temp_for_mutrate.bed.mutrate.txt", sep = ""), sep = "")
      system(command)
      coverage_noncoding_base_mutrate <-fread(paste(prefix, "_temp_for_mutrate.bed.mutrate.txt", sep = ""), header = FALSE, stringsAsFactors = FALSE, sep = "\t")
      colnames(coverage_noncoding_base_mutrate) <- c("base_ID",paste("base_mutrate_alt_", alt_letters[j], sep = ""))
      coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[coverage_noncoding_base_mutrate, on = "base_ID"]
      system(paste("echo \"Finished obtaining base-level mutation rate for alt allele ", alt_letters[j], ".\"", sep = ""))
      system("date")
    }

    # read in non allele-specific epigenomic annotations
    epi_ID = 1
    if (!is.na(nonAS_noncoding_annotations)[1]){ # then epigenomic_marks must be a vector of epigenomic bed files that need to be compard with the mutation data
      for(epi in nonAS_noncoding_annotations){
        command <- paste("../external_tools/bedtools-2.17.0/bin/bedtools coverage -a ", epi, " -b ", paste(prefix, "_temp_for_mutrate.bed", sep = ""),  " > ", paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""),sep = "")
        system(command)
        base_in_epi <- fread(paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""), header = FALSE, sep = "\t", stringsAsFactors = FALSE)
        base_in_epi <- base_in_epi[,c("V4","V5"), with = FALSE]
        colnames(base_in_epi) <- c("base_ID", paste("Anno",epi_ID, sep = "_"))
        coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[base_in_epi, on = "base_ID"]
        system(paste("echo \"Finished reading non-allele specific noncoding annotations ", epi_ID, ".\"", sep = ""))
        system("date")
        epi_ID <- epi_ID + 1
      }
    }

    # read in allele-specific epigenomic annotations, now the base assignment for such noncoding annotations is based on the 50-bp windows.
    # Should not be a big problem as distal introns are assigned based on refseq_ID for intron regions, should be consistent with assignment by spidex.
    # though there is a small chance that a gene's distal intron, which is close to the promoter of another gene and within 10 kb from the TSSs of the latter gene, may be mis-assigned to the latter gene.
    # To completely correct for this issue, need to allow epigenetic annotation to take its own gene assignment, which might be necessary in some situations under strict criteria, such as splicing annotaion.

    if (!is.na(AS_noncoding_annotations)[1]){ # then epigenomic_marks must be a vector of epigenomic bed files that need to be compard with the mutation data
      for(epi in AS_noncoding_annotations){
        for(k in 1:length(epi)){
          command <- paste("../external_tools/bedtools-2.17.0/bin/bedtools coverage -a ", epi[k], " -b ", paste(prefix, "_temp_for_mutrate.bed", sep = ""),  " > ", paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""),sep = "")
          system(command)
          base_in_epi <- fread(paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""), header = FALSE, sep = "\t", stringsAsFactors = FALSE)
          base_in_epi <- base_in_epi[,c("V4","V5"), with = FALSE]
          colnames(base_in_epi) <- c("base_ID", paste("Anno",epi_ID, alt_letters[k], sep = "_"))
          coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[base_in_epi, on = "base_ID"]
        }
        system(paste("echo \"Finished reading allele specific noncoding annotations ", epi_ID, ".\"", sep = ""))
        system("date")
        epi_ID <- epi_ID + 1
      }
    }

    # now for each sample size, generate mut data.
    for(m in 1:length(mutrate_scaling_files)){
      mut_output_temp <- data.table(chr = character(), start = integer(), end = integer(), ref = character(), alt = character())
      for(letter in alt_letters){
        if(!is.na(nonAS_noncoding_annotations)[1] & !is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(1, nonAS_feature_number), sep = ""), paste("Anno_", seq(nonAS_feature_number +1, nonAS_feature_number + AS_feature_number), "_", letter ,sep = ""), "chr", "start", "end"), with = FALSE]
        }else if(!is.na(nonAS_noncoding_annotations)[1] & is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(1, nonAS_feature_number), sep = ""), "chr", "start", "end"), with = FALSE]
        }else if(is.na(nonAS_noncoding_annotations)[1] & !is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(nonAS_feature_number +1, nonAS_feature_number + AS_feature_number), "_", letter ,sep = ""), "chr", "start", "end"), with = FALSE]
        }
        # a very important step here is removing bases that have adjusted_mutrate_base 0. This happens when the allele of the mutrate we are using is just the reference allele.
        # By doing this, we make the computation of likelihood valid, also we automatically removed bases with nonAS annotations but with mutant allele the same with ref allele at this step
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[coverage_noncoding_for_base_mutrate_temp[[paste("base_mutrate_alt_", letter, sep = "")]] !=0]
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[coverage[[i]][,c(paste("scaling_factor_study_", m, sep = ""), "genename","ID","risk"), with = FALSE],on = "ID"]
        # removing sites with allele-specific mutrate == 0 will result in incomplete rows after merging.
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[complete.cases(coverage_noncoding_for_base_mutrate_temp)]
        # derive the adjusted mutation rate for bases of risk genes and nonrisk genes.
        # first multiplies sample size and scaling factor, adjusted_base_mutrate will be used for generating data in a compact format.
        coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate <- coverage_noncoding_for_base_mutrate_temp[, paste("base_mutrate_alt_", letter, sep = ""), with = FALSE] * coverage_noncoding_for_base_mutrate_temp[, paste("scaling_factor_study_", m, sep = ""), with = FALSE] * 2 * sample_sizes[m]
        # Then multiplies effect size after taking exponential, to generate a mutrate that has taken into account RRs, will be used for simulating mutations
        #
        coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate_rr <- exp(as.matrix(coverage_noncoding_for_base_mutrate_temp[,4 : (3 + feature_number)]) %*% rr * coverage_noncoding_for_base_mutrate_temp$risk) * coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate
        # generate random allele-specific mutations based on adjusted mutation rate.
        coverage_noncoding_for_base_mutrate_temp$mut_count <- rpois(nrow(coverage_noncoding_for_base_mutrate_temp), coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate_rr)

        if(!is.na(compact_mut_output)){
          # have to collpase data at this point, otherwise, the I will run out of RAM if process 1000 genes every time.
          anno_count <- rep(0, nrow(coverage_noncoding_for_base_mutrate_temp))
          for(p in 1:feature_number){
            anno_count <- anno_count + coverage_noncoding_for_base_mutrate_temp[[(4 + p -1)]]
          }
          # remove rows(bases) that don't have any non-coding features, this could save a lot of RAM, so I could use smaller partition number which would greatly accelerate speed when read in data for all genes.
          coverage_noncoding_for_base_mutrate_temp1 <- coverage_noncoding_for_base_mutrate_temp[anno_count >0,]

          # first partition by gene for the current chunk
          coverage_noncoding_for_base_mutrate_temp1<- split(coverage_noncoding_for_base_mutrate_temp1, coverage_noncoding_for_base_mutrate_temp1$genename)
          # then partition by feature configuration for each gene in the current chunk
          coverage_noncoding_for_base_mutrate_temp1 <- sapply(coverage_noncoding_for_base_mutrate_temp1, partition_feature, simplify = FALSE)
          # add compact data
          data_partition <- append(data_partition, coverage_noncoding_for_base_mutrate_temp1)
          rm(coverage_noncoding_for_base_mutrate_temp1) # release memory
          system(paste("echo \"Finished read in mutation data and make them into the compact format for Study ", m, " and allele ", letter, ".\"", sep = ""))
          system("date")
        }
        # then generate bed file format, this is a pseudobed file as I will put N at the 4th column as the ref allele is not relevant and mutant allele at the last column.
        # remove rows(bases) that don't have any DNMs
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[mut_count >0,]
        if(node_n != 1){
          mut_bed <- rbindlist(parApply(cl, coverage_noncoding_for_base_mutrate_temp, 1, bed_extension))
        }else{
          mut_bed <- rbindlist(apply(coverage_noncoding_for_base_mutrate_temp, 1, bed_extension))
        }
        mut_bed$ref <- "N"
        mut_bed$alt <- letter
        mut_bed$start <- as.integer(as.character(mut_bed$start))
        mut_bed$end <- as.integer(as.character(mut_bed$end))
        mut_output_temp <- rbind(mut_output_temp, mut_bed)
      }
      mut_output_list[[m]] <- rbind(mut_output_list[[m]], mut_output_temp)
    }
  }

  if(node_n != 1){
    stopCluster(cl)
  }
  options(warn = 0)
  # remove temporary files
  system(paste("rm ", prefix, "_temp*", sep = ""))
  print(paste("echo \"Temp files cleaned and mutation generation finished finished!\""))
  system("date")
  # now write mutation file and mutation with allele info file
  for(m in 1:length(mutrate_scaling_files)){
    fwrite(mut_output_list[[m]], output_allele_info_files[m], col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
    fwrite(mut_output_list[[m]][,1:3], output_bed_files[m], col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
  }
  write.table(risk_genes, output_risk_genes_file, col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
  if(!is.na(compact_mut_output)){
    saveRDS(data_partition, compact_mut_output)
  }
Error in coverage[gene_prior, on = "genename"]: incorrect number of dimensions
Traceback:
In [107]:
library(parallel)
In [ ]:
TADA_A_DNM_generatora <- function(window_file,
                                 mutrate_scaling_files,
                                 sample_sizes,
                                 gene_prior_file,
                                 nonAS_noncoding_annotations,
                                 AS_noncoding_annotations = NA,
                                 report_proportion,
                                 chunk_partition_num = 1,
                                 node_n = 6,
                                 mutrate_ref_files,
                                 rr,
                                 output_allele_info_files,
                                 output_bed_files,
                                 output_risk_genes_file,
                                 compact_mut_output = NA,
                                 MPI = 1){
  base_ID = genename = mut_count = risk = NULL
  prefix <- prefix <- as.integer((as.double(Sys.time())*1000+Sys.getpid()) %% 2^31) # prefix for temporary files that will be deleted at the end of the pipeline
  prefix <- paste("tmp/", prefix, MPI, sep = "")

  # make a tmp folder for tmp files
  system("mkdir -p tmp")

  #mut <- fread(mut_file, header = FALSE, sep = "\t", stringsAsFactors = FALSE)
  windows <- fread(window_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
  coverage <- windows

  # the number of genomic windows in [mutrate_scaling] is less than the number of windows in [windows] because there are a few windows with mutration rate equal to 0, and thus removed.
  for(i in 1:length(mutrate_scaling_files)){
    mutrate_scaling <- fread(mutrate_scaling_files[i], header = TRUE, sep = "\t", stringsAsFactors = FALSE)
    system(paste("echo \"Finished reading mutrate scaling file ", mutrate_scaling_files[i], ".\"", sep = ""))
    system("date")
    coverage <- coverage[mutrate_scaling, on = "site_index"]
    coverage <- coverage[complete.cases(coverage)] # release memory
    colnames(coverage)[length(colnames(coverage))] <- paste("scaling_factor_study_", i, sep = "")
    rm(mutrate_scaling) # release memory
  }


  # get the piror probability of genes.
  gene_prior <- fread(gene_prior_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE)

  # merge gene prior info
  coverage <- coverage[gene_prior, on = "genename"]
  coverage <-coverage[complete.cases(coverage)]

  # select genes based on TADA prior probability and [report_proportion]
  genes_for_report_with_prior <- gene_prior[order(gene_prior[,2],decreasing = TRUE),]
  genes_for_report_with_prior <- genes_for_report_with_prior[1:floor(nrow(genes_for_report_with_prior)*report_proportion),]
  # randomly assign risk genes based on prior probability
  genes_for_report_with_prior$risk <- rbinom(nrow(genes_for_report_with_prior), 1, genes_for_report_with_prior$prior)
  risk_genes <- genes_for_report_with_prior[risk == 1,]$genename
  nonrisk_genes <- genes_for_report_with_prior[risk == 0,]$genename
  coverage_risk <- coverage[is.element(genename, risk_genes)]
  coverage_risk$risk <- 1
  coverage_nonrisk <- coverage[is.element(genename, nonrisk_genes)]
  coverage_nonrisk$risk <- 0

  coverage <- rbind(coverage_risk, coverage_nonrisk)

  # now need to extropolate window mutation file to base level mutation file
  coverage <-coverage[,c("chr","start","end",paste("scaling_factor_study_", seq(1,length(mutrate_scaling_files)), sep = ""),"genename","risk"),with = FALSE]

  coverage$ID <- paste(coverage$genename, coverage$start, sep = "_")

  total_rows <- nrow(coverage)
  interval <- floor(total_rows/chunk_partition_num)
  data_bins <- c(rep(seq(1,chunk_partition_num), each = interval),rep(chunk_partition_num, total_rows -interval*chunk_partition_num))

  # split into 20 different chunks, then for each chunk split by genes, then by feature configuration. If split by gene at the first level, implementation would take too long
  coverage <- split(coverage, data_bins)

  #funtion to expand windows to bases
  window_expansion <- function(table_row){
    start <- seq(as.numeric(table_row[2]),as.numeric(table_row[3])-1)
    data.frame(table_row[1], start, start+1, paste(table_row["genename"],start,sep = "_"), table_row["ID"])
  }


  bed_extension <- function(table_row){
    data.frame(chr = rep(table_row["chr"], table_row["mut_count"]), start = rep(table_row["start"], table_row["mut_count"]), end = rep(table_row["end"], table_row["mut_count"]))
  }

  options(warn=-1)
  if(node_n != 1){
    cl <- makeCluster(node_n)
  }
  # use parallel computing and rbindlist to increase the speed by thousands of times.
  environment(window_expansion) <- .GlobalEnv
  environment(bed_extension) <- .GlobalEnv
  # get nonAS feature number
  if(is.na(nonAS_noncoding_annotations[1])){
    nonAS_feature_number <- 0
  }else{
    nonAS_feature_number <- length(nonAS_noncoding_annotations)
  }
  # get AS feature number
  if(is.na(AS_noncoding_annotations[1])){
    AS_feature_number <- 0
  }else{
    AS_feature_number <- length(AS_noncoding_annotations)
  }

  # get total feature number
  feature_number = nonAS_feature_number + AS_feature_number
  # function to get effective information of each element of partition_by_gene
  # These information are those necessary to compute log-likelihood in the optimization function
  partition_feature <- function(pbg){
    # input is one element of the list of partition_by_gene
    pbg_split <- split(pbg, pbg[,4:(4 + feature_number - 1)],drop = TRUE)
    feature_combination_number <- length(pbg_split)
    # this function below is different from the function used in dealing with dataset without reading by chunk. Here, prior is not incoporated at this step.
    info_for_each_feature <- function(feature_set){
      list(feature_vector = c(as.numeric(feature_set[1,4:(4 + feature_number - 1)])), sum_mut_rate_count = sum(feature_set$mut_count*log(feature_set$adjusted_base_mutrate)), sum_mut_rate = sum(feature_set$adjusted_base_mutrate), sum_mut_count = sum(feature_set$mut_count), log_fcount = sum(log(factorial(feature_set$mut_count))))
    }
    sapply(pbg_split, info_for_each_feature,simplify = FALSE)
  }


  alt_letters <- c("A","C","G","T")
  mut_output_list <- list()
  data_partition <- list()
  for(m in 1:length(mutrate_scaling_files)){
    mut_output_list[[m]] <- data.table(chr = character(), start = integer(), end = integer(), ref = character(), alt = character())
  }

  # here only deals with the noncoding parts that are within 10kb of TSSs of genes. Will deal with
  for(i in 1:chunk_partition_num){
    # split coverage_noncoding to 10 parts, and for each part, generate feature table (which will be used for )
    if(node_n != 1){
      coverage_noncoding_for_base_mutrate <- rbindlist(parApply(cl, coverage[[i]], 1, window_expansion))
    }else{
      coverage_noncoding_for_base_mutrate <- rbindlist(apply(coverage[[i]], 1, window_expansion))
    }
    system(paste("echo \"Finished partitioning base-level coordinates data at Round ", i, ".\"", sep = ""))
    system("date")
    colnames(coverage_noncoding_for_base_mutrate) <- c("chr","start","end","base_ID","ID")
    coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[!duplicated(base_ID)]
    coverage_noncoding_for_base_mutrate$start <- as.integer(coverage_noncoding_for_base_mutrate$start)
    coverage_noncoding_for_base_mutrate$end <- as.integer(coverage_noncoding_for_base_mutrate$end)

    # write out a bed file to get base-level mutation rates
    fwrite(coverage_noncoding_for_base_mutrate[,1:4],paste(prefix, "_temp_for_mutrate.bed", sep = ""), col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
    # read in allele-specific base-level mutation rate
    for(j in 1:length(mutrate_ref_files)){
      command <- paste("../external_tools/bigWigAverageOverBed ", mutrate_ref_files[j], " ", paste(prefix, "_temp_for_mutrate.bed", sep = ""), " ", paste(prefix, "_temp_for_mutrate.bed.mutrate", sep = "" ), sep = "")
      system(command)
      command <- paste("awk {'print $1\"\t\"$4'} ", paste(prefix, "_temp_for_mutrate.bed.mutrate", sep = ""), " > ", paste(prefix, "_temp_for_mutrate.bed.mutrate.txt", sep = ""), sep = "")
      system(command)
      coverage_noncoding_base_mutrate <-fread(paste(prefix, "_temp_for_mutrate.bed.mutrate.txt", sep = ""), header = FALSE, stringsAsFactors = FALSE, sep = "\t")
      colnames(coverage_noncoding_base_mutrate) <- c("base_ID",paste("base_mutrate_alt_", alt_letters[j], sep = ""))
      coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[coverage_noncoding_base_mutrate, on = "base_ID"]
      system(paste("echo \"Finished obtaining base-level mutation rate for alt allele ", alt_letters[j], ".\"", sep = ""))
      system("date")
    }

    # read in non allele-specific epigenomic annotations
    epi_ID = 1
    if (!is.na(nonAS_noncoding_annotations)[1]){ # then epigenomic_marks must be a vector of epigenomic bed files that need to be compard with the mutation data
      for(epi in nonAS_noncoding_annotations){
        command <- paste("../external_tools/bedtools-2.17.0/bin/bedtools coverage -a ", epi, " -b ", paste(prefix, "_temp_for_mutrate.bed", sep = ""),  " > ", paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""),sep = "")
        system(command)
        base_in_epi <- fread(paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""), header = FALSE, sep = "\t", stringsAsFactors = FALSE)
        base_in_epi <- base_in_epi[,c("V4","V5"), with = FALSE]
        colnames(base_in_epi) <- c("base_ID", paste("Anno",epi_ID, sep = "_"))
        coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[base_in_epi, on = "base_ID"]
        system(paste("echo \"Finished reading non-allele specific noncoding annotations ", epi_ID, ".\"", sep = ""))
        system("date")
        epi_ID <- epi_ID + 1
      }
    }

    # read in allele-specific epigenomic annotations, now the base assignment for such noncoding annotations is based on the 50-bp windows.
    # Should not be a big problem as distal introns are assigned based on refseq_ID for intron regions, should be consistent with assignment by spidex.
    # though there is a small chance that a gene's distal intron, which is close to the promoter of another gene and within 10 kb from the TSSs of the latter gene, may be mis-assigned to the latter gene.
    # To completely correct for this issue, need to allow epigenetic annotation to take its own gene assignment, which might be necessary in some situations under strict criteria, such as splicing annotaion.

    if (!is.na(AS_noncoding_annotations)[1]){ # then epigenomic_marks must be a vector of epigenomic bed files that need to be compard with the mutation data
      for(epi in AS_noncoding_annotations){
        for(k in 1:length(epi)){
          command <- paste("../external_tools/bedtools-2.17.0/bin/bedtools coverage -a ", epi[k], " -b ", paste(prefix, "_temp_for_mutrate.bed", sep = ""),  " > ", paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""),sep = "")
          system(command)
          base_in_epi <- fread(paste(prefix,"_temp_for_mutrate_overlap_epi.bed", sep = ""), header = FALSE, sep = "\t", stringsAsFactors = FALSE)
          base_in_epi <- base_in_epi[,c("V4","V5"), with = FALSE]
          colnames(base_in_epi) <- c("base_ID", paste("Anno",epi_ID, alt_letters[k], sep = "_"))
          coverage_noncoding_for_base_mutrate <- coverage_noncoding_for_base_mutrate[base_in_epi, on = "base_ID"]
        }
        system(paste("echo \"Finished reading allele specific noncoding annotations ", epi_ID, ".\"", sep = ""))
        system("date")
        epi_ID <- epi_ID + 1
      }
    }

    # now for each sample size, generate mut data.
    for(m in 1:length(mutrate_scaling_files)){
      mut_output_temp <- data.table(chr = character(), start = integer(), end = integer(), ref = character(), alt = character())
      for(letter in alt_letters){
        if(!is.na(nonAS_noncoding_annotations)[1] & !is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(1, nonAS_feature_number), sep = ""), paste("Anno_", seq(nonAS_feature_number +1, nonAS_feature_number + AS_feature_number), "_", letter ,sep = ""), "chr", "start", "end"), with = FALSE]
        }else if(!is.na(nonAS_noncoding_annotations)[1] & is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(1, nonAS_feature_number), sep = ""), "chr", "start", "end"), with = FALSE]
        }else if(is.na(nonAS_noncoding_annotations)[1] & !is.na(AS_noncoding_annotations)[1]){
          coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate[, c("base_ID", "ID",paste("base_mutrate_alt_", letter, sep = ""), paste("Anno_", seq(nonAS_feature_number +1, nonAS_feature_number + AS_feature_number), "_", letter ,sep = ""), "chr", "start", "end"), with = FALSE]
        }
        # a very important step here is removing bases that have adjusted_mutrate_base 0. This happens when the allele of the mutrate we are using is just the reference allele.
        # By doing this, we make the computation of likelihood valid, also we automatically removed bases with nonAS annotations but with mutant allele the same with ref allele at this step
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[coverage_noncoding_for_base_mutrate_temp[[paste("base_mutrate_alt_", letter, sep = "")]] !=0]
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[coverage[[i]][,c(paste("scaling_factor_study_", m, sep = ""), "genename","ID","risk"), with = FALSE],on = "ID"]
        # removing sites with allele-specific mutrate == 0 will result in incomplete rows after merging.
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[complete.cases(coverage_noncoding_for_base_mutrate_temp)]
        # derive the adjusted mutation rate for bases of risk genes and nonrisk genes.
        # first multiplies sample size and scaling factor, adjusted_base_mutrate will be used for generating data in a compact format.
        coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate <- coverage_noncoding_for_base_mutrate_temp[, paste("base_mutrate_alt_", letter, sep = ""), with = FALSE] * coverage_noncoding_for_base_mutrate_temp[, paste("scaling_factor_study_", m, sep = ""), with = FALSE] * 2 * sample_sizes[m]
        # Then multiplies effect size after taking exponential, to generate a mutrate that has taken into account RRs, will be used for simulating mutations
        #
        coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate_rr <- exp(as.matrix(coverage_noncoding_for_base_mutrate_temp[,4 : (3 + feature_number)]) %*% rr * coverage_noncoding_for_base_mutrate_temp$risk) * coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate
        # generate random allele-specific mutations based on adjusted mutation rate.
        coverage_noncoding_for_base_mutrate_temp$mut_count <- rpois(nrow(coverage_noncoding_for_base_mutrate_temp), coverage_noncoding_for_base_mutrate_temp$adjusted_base_mutrate_rr)

        if(!is.na(compact_mut_output)){
          # have to collpase data at this point, otherwise, the I will run out of RAM if process 1000 genes every time.
          anno_count <- rep(0, nrow(coverage_noncoding_for_base_mutrate_temp))
          for(p in 1:feature_number){
            anno_count <- anno_count + coverage_noncoding_for_base_mutrate_temp[[(4 + p -1)]]
          }
          # remove rows(bases) that don't have any non-coding features, this could save a lot of RAM, so I could use smaller partition number which would greatly accelerate speed when read in data for all genes.
          coverage_noncoding_for_base_mutrate_temp1 <- coverage_noncoding_for_base_mutrate_temp[anno_count >0,]

          # first partition by gene for the current chunk
          coverage_noncoding_for_base_mutrate_temp1<- split(coverage_noncoding_for_base_mutrate_temp1, coverage_noncoding_for_base_mutrate_temp1$genename)
          # then partition by feature configuration for each gene in the current chunk
          coverage_noncoding_for_base_mutrate_temp1 <- sapply(coverage_noncoding_for_base_mutrate_temp1, partition_feature, simplify = FALSE)
          # add compact data
          data_partition <- append(data_partition, coverage_noncoding_for_base_mutrate_temp1)
          rm(coverage_noncoding_for_base_mutrate_temp1) # release memory
          system(paste("echo \"Finished read in mutation data and make them into the compact format for Study ", m, " and allele ", letter, ".\"", sep = ""))
          system("date")
        }
        # then generate bed file format, this is a pseudobed file as I will put N at the 4th column as the ref allele is not relevant and mutant allele at the last column.
        # remove rows(bases) that don't have any DNMs
        coverage_noncoding_for_base_mutrate_temp <- coverage_noncoding_for_base_mutrate_temp[mut_count >0,]
        if(node_n != 1){
          mut_bed <- rbindlist(parApply(cl, coverage_noncoding_for_base_mutrate_temp, 1, bed_extension))
        }else{
          mut_bed <- rbindlist(apply(coverage_noncoding_for_base_mutrate_temp, 1, bed_extension))
        }
        mut_bed$ref <- "N"
        mut_bed$alt <- letter
        mut_bed$start <- as.integer(as.character(mut_bed$start))
        mut_bed$end <- as.integer(as.character(mut_bed$end))
        mut_output_temp <- rbind(mut_output_temp, mut_bed)
      }
      mut_output_list[[m]] <- rbind(mut_output_list[[m]], mut_output_temp)
    }
  }

  if(node_n != 1){
    stopCluster(cl)
  }
  options(warn = 0)
  # remove temporary files
  system(paste("rm ", prefix, "_temp*", sep = ""))
  print(paste("echo \"Temp files cleaned and mutation generation finished finished!\""))
  system("date")
  # now write mutation file and mutation with allele info file
  for(m in 1:length(mutrate_scaling_files)){
    fwrite(mut_output_list[[m]], output_allele_info_files[m], col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
    fwrite(mut_output_list[[m]][,1:3], output_bed_files[m], col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
  }
  write.table(risk_genes, output_risk_genes_file, col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
  if(!is.na(compact_mut_output)){
    saveRDS(data_partition, compact_mut_output)
  }
}

retrieve_mutation_info

In [ ]:
retrieve_mutation_info(data_partition, 
                       genename, 
                       noncoding_annotation)