From dda1608f856027baa9c01226a91c0fef89a318e1 Mon Sep 17 00:00:00 2001
From: nservant <nicolas.servant@curie.fr>
Date: Thu, 24 Feb 2022 10:57:52 +0100
Subject: [PATCH] [DSL2] add merge_stats

---
 conf/modules.config                           | 17 +++++++++-
 modules/local/balance.nf~                     | 31 -------------------
 .../local/{ => hicpro}/dnase_mapping_stats.nf |  0
 modules/local/hicpro/merge_stats.nf           | 20 ++++++++++++
 .../local/hicpro/merge_valid_interaction.nf   |  2 +-
 modules/local/makebins.nf~                    | 30 ------------------
 modules/local/merge_stats.nf                  | 30 ------------------
 modules/local/{ => trash}/cooler_balance.nf   |  0
 modules/local/{ => trash}/cooler_raw.nf       |  0
 modules/local/{ => trash}/cooler_zoomify.nf   |  0
 modules/local/{ => trash}/makeBowtie2Index.nf |  0
 nextflow.config                               |  2 +-
 subworkflows/local/cooler.nf                  | 15 +++++++--
 subworkflows/local/hicpro.nf                  | 26 +++++++++++++---
 subworkflows/local/hicpro_mapping.nf          |  6 +++-
 15 files changed, 77 insertions(+), 102 deletions(-)
 delete mode 100644 modules/local/balance.nf~
 rename modules/local/{ => hicpro}/dnase_mapping_stats.nf (100%)
 create mode 100644 modules/local/hicpro/merge_stats.nf
 delete mode 100644 modules/local/makebins.nf~
 delete mode 100644 modules/local/merge_stats.nf
 rename modules/local/{ => trash}/cooler_balance.nf (100%)
 rename modules/local/{ => trash}/cooler_raw.nf (100%)
 rename modules/local/{ => trash}/cooler_zoomify.nf (100%)
 rename modules/local/{ => trash}/makeBowtie2Index.nf (100%)

diff --git a/conf/modules.config b/conf/modules.config
index ff14448..7f4fcea 100644
--- a/conf/modules.config
+++ b/conf/modules.config
@@ -100,7 +100,22 @@ process {
 
   withName: 'MERGE_VALID_INTERACTION' {
     publishDir = [
-      path: { "${params.outdir}/hicpro/valid_pairs" },
+      [
+        path: { "${params.outdir}/hicpro/stats/" },
+        mode: 'copy',
+	pattern: "*stat"
+      ],
+      [
+        path: { "${params.outdir}/hicpro/valid_pairs" },
+        mode: 'copy',
+	pattern: "*Pairs"
+      ]
+    ]
+  }
+
+  withName: 'MERGE_STATS' {
+    publishDir = [
+      path: { "${params.outdir}/hicpro/stats" },
       mode: 'copy'
     ]
   }
diff --git a/modules/local/balance.nf~ b/modules/local/balance.nf~
deleted file mode 100644
index 728a04e..0000000
--- a/modules/local/balance.nf~
+++ /dev/null
@@ -1,31 +0,0 @@
-process COOLER_BALANCE {
-    tag "$meta.id"
-    label 'process_high'
-
-    conda (params.enable_conda ? "bioconda::cooler=0.8.11" : null)
-    container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ?
-        'https://depot.galaxyproject.org/singularity/cooler:0.8.11--pyh3252c3a_0' :
-        'quay.io/biocontainers/cooler:0.8.11--pyh3252c3a_0' }"
-
-    input:
-    tuple val(meta), path(cool)
-
-    output:
-    tuple val(meta), path("*.cool"), emit: cool
-    path "versions.yml"                           , emit: versions
-
-    script:
-    def args = task.ext.args ?: ''
-    """
-    cp ${cool} ${cool.baseName}_norm.cool
-    cooler balance \\
-        $args \\
-        -p ${task.cpus} \\
-        ${cool.baseName}_norm.cool
-
-    cat <<-END_VERSIONS > versions.yml
-    "${task.process}":
-        cooler: \$(cooler --version 2>&1 | sed 's/cooler, version //')
-    END_VERSIONS
-    """
-}
diff --git a/modules/local/dnase_mapping_stats.nf b/modules/local/hicpro/dnase_mapping_stats.nf
similarity index 100%
rename from modules/local/dnase_mapping_stats.nf
rename to modules/local/hicpro/dnase_mapping_stats.nf
diff --git a/modules/local/hicpro/merge_stats.nf b/modules/local/hicpro/merge_stats.nf
new file mode 100644
index 0000000..2ef759d
--- /dev/null
+++ b/modules/local/hicpro/merge_stats.nf
@@ -0,0 +1,20 @@
+process MERGE_STATS {
+  label 'process_low'
+
+  input:
+  tuple val(meta), path(fstat) 
+
+  output:
+  path("stats/"), emit:mqc_mstats
+  path("*stat"), emit:all_mstats
+
+  script:
+  if ( (fstat =~ /.mapstat/) ){ ext = "mmapstat" }
+  if ( (fstat =~ /.pairstat/) ){ ext = "mpairstat" }
+  if ( (fstat =~ /.RSstat/) ){ ext = "mRSstat" }
+  """
+  merge_statfiles.py -f ${fstat} > ${meta.id}.${ext}
+  mkdir -p stats/${meta.id}
+  cp ${meta.id}.${ext} stats/${meta.id}/
+  """
+}
diff --git a/modules/local/hicpro/merge_valid_interaction.nf b/modules/local/hicpro/merge_valid_interaction.nf
index 0819675..efc29ba 100644
--- a/modules/local/hicpro/merge_valid_interaction.nf
+++ b/modules/local/hicpro/merge_valid_interaction.nf
@@ -8,7 +8,7 @@ process MERGE_VALID_INTERACTION {
    output:
    tuple val(meta), path("*.allValidPairs"), emit: valid_pairs
    path("stats/"), emit:mqc
-   path("*mergestat"), emit:stats
+   tuple val(meta), path("*mergestat"), emit:stats
 
    script:
    prefix = meta.id
diff --git a/modules/local/makebins.nf~ b/modules/local/makebins.nf~
deleted file mode 100644
index 4d3f307..0000000
--- a/modules/local/makebins.nf~
+++ /dev/null
@@ -1,30 +0,0 @@
-process COOLER_MAKEBINS {
-  tag "$meta.id"
-  label 'process_low'
-
-  conda (params.enable_conda ? "bioconda::cooler=0.8.11" : null)
-  container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ?
-      'https://depot.galaxyproject.org/singularity/cooler:0.8.11--pyh3252c3a_0' :
-      'quay.io/biocontainers/cooler:0.8.11--pyh3252c3a_0' }"
-
-  input:
-  tuple path(chromsizes), val(cool_bin)
-
-  output:
-  path ("*.bed")     , emit: bed
-  path "versions.yml", emit: versions
-
-  script:
-  def args = task.ext.args ?: ''
-  """
-  cooler makebins \\
-      $args \\
-      ${chromsizes} \\
-      ${cool_bin} > cooler_bins_${cool_bin}.bed
-
-  cat <<-END_VERSIONS > versions.yml
-  "${task.process}":
-      cooler: \$(cooler --version 2>&1 | sed 's/cooler, version //')
-  END_VERSIONS
-  """
-}
diff --git a/modules/local/merge_stats.nf b/modules/local/merge_stats.nf
deleted file mode 100644
index 336baa3..0000000
--- a/modules/local/merge_stats.nf
+++ /dev/null
@@ -1,30 +0,0 @@
-// Import generic module functions
-include { initOptions; saveFiles; getSoftwareName } from './functions'
-
-params.options = [:]
-options    = initOptions(params.options)
-
-process merge_stats {
-   tag "$ext"
-   label 'process_low'
-   publishDir "${params.outdir}/hicpro/", mode: params.publish_dir_mode,
-               saveAs: {filename -> if (filename.endsWith("stat")) "stats/$filename"}
-
-   input:
-   tuple val(prefix), path(fstat) 
-
-   output:
-   path("stats/"), emit:mqc_mstats
-   path("*stat"), emit:all_mstats
-
-  script:
-  sample = prefix.toString() - ~/(_R1|_R2|_val_1|_val_2|_1|_2)/
-  if ( (fstat =~ /.mapstat/) ){ ext = "mmapstat" }
-  if ( (fstat =~ /.pairstat/) ){ ext = "mpairstat" }
-  if ( (fstat =~ /.RSstat/) ){ ext = "mRSstat" }
-  """
-  merge_statfiles.py -f ${fstat} > ${prefix}.${ext}
-  mkdir -p stats/${sample}
-  cp ${prefix}.${ext} stats/${sample}/
-  """
-}
diff --git a/modules/local/cooler_balance.nf b/modules/local/trash/cooler_balance.nf
similarity index 100%
rename from modules/local/cooler_balance.nf
rename to modules/local/trash/cooler_balance.nf
diff --git a/modules/local/cooler_raw.nf b/modules/local/trash/cooler_raw.nf
similarity index 100%
rename from modules/local/cooler_raw.nf
rename to modules/local/trash/cooler_raw.nf
diff --git a/modules/local/cooler_zoomify.nf b/modules/local/trash/cooler_zoomify.nf
similarity index 100%
rename from modules/local/cooler_zoomify.nf
rename to modules/local/trash/cooler_zoomify.nf
diff --git a/modules/local/makeBowtie2Index.nf b/modules/local/trash/makeBowtie2Index.nf
similarity index 100%
rename from modules/local/makeBowtie2Index.nf
rename to modules/local/trash/makeBowtie2Index.nf
diff --git a/nextflow.config b/nextflow.config
index 586c6b4..1a413b7 100644
--- a/nextflow.config
+++ b/nextflow.config
@@ -68,7 +68,7 @@ params {
     // Contact maps
     save_raw_maps = false
     bin_size = '1000000'
-    res_zoomify = '5000'
+    res_zoomify = false
     hicpro_maps = false
     ice_max_iter = 100
     ice_filter_low_count_perc = 0.02
diff --git a/subworkflows/local/cooler.nf b/subworkflows/local/cooler.nf
index e24aed4..897b057 100644
--- a/subworkflows/local/cooler.nf
+++ b/subworkflows/local/cooler.nf
@@ -28,7 +28,8 @@ workflow COOLER {
   COOLER_MAKEBINS(
     chromsize.combine(cool_bins)
   )
-    
+  ch_versions = ch_versions.mix(COOLER_MAKEBINS.out.versions)
+
   //*****************************************
   // BUILD COOL FILE PER RESOLUTION
   // [meta, pairs, resolution]
@@ -37,14 +38,21 @@ workflow COOLER {
     pairs.combine(cool_bins),
     chromsize.collect()
   )
+  ch_versions = ch_versions.mix(COOLER_CLOAD.out.versions)
 
   COOLER_BALANCE(
     COOLER_CLOAD.out.cool
   )
+  ch_versions = ch_versions.mix(COOLER_BALANCE.out.versions)
 
   // Zoomify at minimum bin resolution
+  if (!params.res_zoomify){
+    ch_res_zoomify = cool_bins.min()
+  }else{
+    ch_res_zoomify = params.res_zoomify
+  }
   COOLER_CLOAD.out.cool
-    .combine(cool_bins.min())
+    .combine(ch_res_zoomify)
     .filter{ it [1] == it[3] }
     .map{it->[it[0], it[2]]}
     .set{ch_cool_zoomify}
@@ -52,6 +60,7 @@ workflow COOLER {
   COOLER_ZOOMIFY(
     ch_cool_zoomify
   )
+  ch_versions = ch_versions.mix(COOLER_ZOOMIFY.out.versions)
 
   //*****************************************
   // DUMP DATA
@@ -60,6 +69,7 @@ workflow COOLER {
   COOLER_DUMP(
     COOLER_BALANCE.out.cool.map{[it[0], "", it[2]]}
   )
+  ch_versions = ch_versions.mix(COOLER_DUMP.out.versions)
 
   //COOLER_DUMP(
   //  COOLER_ZOOMIFY.out.mcool.combine(cool_bins).map{it->[it[0], it[2], it[1]]}
@@ -68,6 +78,7 @@ workflow COOLER {
   SPLIT_COOLER_DUMP(
     COOLER_DUMP.out.bedpe
   )
+  ch_versions = ch_versions.mix(SPLIT_COOLER_DUMP.out.versions)
 
   emit:
   versions = ch_versions
diff --git a/subworkflows/local/hicpro.nf b/subworkflows/local/hicpro.nf
index 7ea1bcc..3869d4d 100644
--- a/subworkflows/local/hicpro.nf
+++ b/subworkflows/local/hicpro.nf
@@ -7,6 +7,7 @@
 include { HICPRO_MAPPING } from './hicpro_mapping'
 include { GET_VALID_INTERACTION } from '../../modules/local/hicpro/get_valid_interaction'
 include { MERGE_VALID_INTERACTION } from '../../modules/local/hicpro/merge_valid_interaction'
+include { MERGE_STATS } from '../../modules/local/hicpro/merge_stats'
 include { HICPRO2PAIRS } from '../../modules/local/hicpro/hicpro2pairs'
 include { BUILD_CONTACT_MAPS } from '../../modules/local/hicpro/build_contact_maps'
 include { ICE_NORMALIZATION } from '../../modules/local/hicpro/run_ice'
@@ -30,26 +31,39 @@ workflow HICPRO {
     index,
     ligation_site
   )
+  ch_versions = ch_versions.mix(HICPRO_MAPPING.out.versions)
 
   // get valid interaction
   GET_VALID_INTERACTION (
     HICPRO_MAPPING.out.bam,
     fragments    
   )
-
+  //TODO ch_versions = ch_versions.mix(GET_VALID_INTERACTION.out.versions)
+  
   // merge valid interactions and remove duplicates
   MERGE_VALID_INTERACTION (
     GET_VALID_INTERACTION.out.valid_pairs
   )
+  // TODO ch_versions = ch_versions.mix(MERGE_VALID_INTERACTION.out.versions)
+
+  // merge stats
+  HICPRO_MAPPING.out.mapstats.groupTuple()
+    .concat(HICPRO_MAPPING.out.pairstats.groupTuple(),
+            GET_VALID_INTERACTION.out.stats.groupTuple())
+    .view()
+    .set{ ch_hicpro_stats }
+
+  MERGE_STATS(
+    ch_hicpro_stats
+  )
+  //TODO ch_versions = ch_versions.mix(MERGE_STATS.out.versions)
 
   // convert to pairs
   HICPRO2PAIRS (
     MERGE_VALID_INTERACTION.out.valid_pairs,
     chrsize
   )
-
-  //merge stats
-  // TODO
+  //TODO ch_versions = ch_versions.mix(HICPRO2PAIRS.out.versions)
 
   if (params.hicpro_maps){
     
@@ -58,11 +72,13 @@ workflow HICPRO {
       MERGE_VALID_INTERACTION.out.valid_pairs.combine(map_res),
       chrsize.collect()
     )
-    
+    //TODO ch_versions = ch_versions.mix(BUILD_CONTACT_MAPS.out.versions)
+
     // run_ice
     ICE_NORMALIZATION(
       BUILD_CONTACT_MAPS.out.maps
     )
+    //TODO ch_versions = ch_versions.mix(ICE_NORMALIZATION.out.versions)
   }
 
   emit:
diff --git a/subworkflows/local/hicpro_mapping.nf b/subworkflows/local/hicpro_mapping.nf
index daa53c6..0add95d 100644
--- a/subworkflows/local/hicpro_mapping.nf
+++ b/subworkflows/local/hicpro_mapping.nf
@@ -84,6 +84,7 @@ workflow HICPRO_MAPPING {
   MERGE_BOWTIE2(
     ch_bowtie2_align
   )
+  //TODO ch_versions = ch_versions.mix(MERGE_BOWTIE2.out.versions)
 
   // Combine mates
   MERGE_BOWTIE2.out.bam
@@ -95,8 +96,11 @@ workflow HICPRO_MAPPING {
   COMBINE_MATES (
     ch_bams
   )
-  
+  //TODO ch_versions = ch_versions.mix(COMBINE_MATES.out.versions)
+
   emit:
   versions = ch_versions
   bam = COMBINE_MATES.out.bam
+  mapstats = MERGE_BOWTIE2.out.stats
+  pairstats = COMBINE_MATES.out.stats
 }
-- 
GitLab