From 88d6463e64cc1c3d222a49ed01c40f9e7dcd7420 Mon Sep 17 00:00:00 2001
From: Mia Croiset <mia.croiset@ens-lyon.fr>
Date: Fri, 23 Jun 2023 09:36:24 +0200
Subject: [PATCH] TOFIX filter picard for hicpro correction

---
 conf/modules.config                  | 16 ++++++------
 subworkflows/local/hicpro.nf         | 24 +++--------------
 subworkflows/local/hicpro_mapping.nf | 39 +++++++++++++++++++++++++---
 subworkflows/local/hicstuff_sub.nf   |  5 ++++
 workflows/hic.nf                     |  8 ++++++
 5 files changed, 61 insertions(+), 31 deletions(-)

diff --git a/conf/modules.config b/conf/modules.config
index aea9de9..01715bf 100644
--- a/conf/modules.config
+++ b/conf/modules.config
@@ -389,14 +389,14 @@ process {
         ext.prefix = { "${cool.baseName}" }
     }
 
-//  withName: 'HIC_PLOT_MATRIX' {
-//      publishDir = [
-//          path: { "${params.outdir}/contact_maps/cool/" },
-//          mode: 'copy'
-//      ]
-//      ext.args = '--log --perChromosome'
-//      ext.prefix = { "${cool.baseName}" }
-//  }
+    withName: 'HIC_PLOT_MATRIX' {
+      publishDir = [
+          path: { "${params.outdir}/matrices/" },
+          mode: 'copy'
+        ]
+      ext.args = '--log --perChromosome'
+      ext.prefix = { "${cool.baseName}" }
+    }
 
     //********************************
     // FILTER_PCR_DUP
diff --git a/subworkflows/local/hicpro.nf b/subworkflows/local/hicpro.nf
index 0cea0ff..17543d0 100644
--- a/subworkflows/local/hicpro.nf
+++ b/subworkflows/local/hicpro.nf
@@ -12,7 +12,6 @@ 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'
-include { FILTER_PCR_DUP } from './filter_pcr_dup'
 
 // Remove meta.chunks
 def removeChunks(row){
@@ -39,28 +38,13 @@ workflow HICPRO {
     HICPRO_MAPPING(
         reads,
         index,
-        ligation_site
+        ligation_site,
+        fasta
     )
     ch_versions = ch_versions.mix(HICPRO_MAPPING.out.versions)
 
-    //***************************************
-    // FILTER PCR DUPLICATES
-
-    if (params.filter_pcr_picard && !params.keep_dups){
-        error "Error: cannot filter PCR duplicates with both methods! If filter_pcr_picard is true, keep_dups should be true too"
-    }
-    else if (params.filter_pcr_picard){
-        FILTER_PCR_DUP(
-            HICPRO_MAPPING.out.bam,
-            fasta,
-            index
-        )
-        FILTER_PCR_DUP.out.bam.set{ ch_bam }
-    }
-    else {
-        HICPRO_MAPPING.out.bam.set{ ch_bam }
-    }
-
+    HICPRO_MAPPING.out.bam.set{ ch_bam }
+    ch_bam.view()
 
     //***************************************
     // DIGESTION PROTOCOLS
diff --git a/subworkflows/local/hicpro_mapping.nf b/subworkflows/local/hicpro_mapping.nf
index 0f889e9..3e36cf0 100644
--- a/subworkflows/local/hicpro_mapping.nf
+++ b/subworkflows/local/hicpro_mapping.nf
@@ -9,8 +9,9 @@ include { BOWTIE2_ALIGN as BOWTIE2_ALIGN_TRIMMED } from '../../modules/nf-core/b
 include { MERGE_BOWTIE2 } from '../../modules/local/hicpro/bowtie2_merge'
 include { COMBINE_MATES} from '../../modules/local/hicpro/combine_mates'
 include { MAPPING_STATS_DNASE } from '../../modules/local/hicpro/dnase_mapping_stats'
+include { FILTER_PCR_DUP } from './filter_pcr_dup'
 
-// Paired-end to Single-end 
+// Paired-end to Single-end
 def pairToSingle(row, mates) {
   def meta = row[0].clone()
   meta.single_end = true
@@ -30,6 +31,13 @@ def singleToPair(row){
   return [ meta, row[1] ]
 }
 
+def singleToPairPCR(row){
+    def meta = row[0].clone()
+    meta.remove('mates')
+    meta.single_end = false
+    return [ meta, [row[1], row[3]] ]
+}
+
 
 workflow HICPRO_MAPPING {
 
@@ -37,10 +45,11 @@ workflow HICPRO_MAPPING {
   reads // [meta, read1, read2]
   index // [meta, path]
   ligation_site // value
+  fasta
 
   main:
   ch_versions = Channel.empty()
- 
+
   // Align each mates separetly and add mates information in [meta]
   ch_reads_r1 = reads.map{ it -> pairToSingle(it,"R1") }
   ch_reads_r2 = reads.map{ it -> pairToSingle(it,"R2") }
@@ -82,7 +91,7 @@ workflow HICPRO_MAPPING {
     )
     ch_versions = ch_versions.mix(MERGE_BOWTIE2.out.versions)
     ch_mapping_stats = MERGE_BOWTIE2.out.stats
-    
+
     // Combine mates
     MERGE_BOWTIE2.out.bam
       .map { singleToPair(it) }
@@ -102,6 +111,30 @@ workflow HICPRO_MAPPING {
       .set {ch_bams}
   }
 
+  //***************************************
+    // FILTER PCR DUPLICATES
+
+    if (params.filter_pcr_picard && !params.keep_dups){
+        error "Error: cannot filter PCR duplicates with both methods! If filter_pcr_picard is true, keep_dups should be true too"
+    }
+    else if (params.filter_pcr_picard){
+        if (!params.dnase){
+            MERGE_BOWTIE2.out.bam.set{ch_2filter}
+        }
+        else{
+            BOWTIE2_ALIGN.out.bamx.set{ch_2filter}
+        }
+        FILTER_PCR_DUP(
+            ch_2filter,
+            fasta,
+            index
+        )
+        FILTER_PCR_DUP.out.bam
+            .map { singleToPairPCR(it) }
+            .set {ch_bams}
+    }
+
+
   COMBINE_MATES (
     ch_bams
   )
diff --git a/subworkflows/local/hicstuff_sub.nf b/subworkflows/local/hicstuff_sub.nf
index 28d383e..b3070d5 100644
--- a/subworkflows/local/hicstuff_sub.nf
+++ b/subworkflows/local/hicstuff_sub.nf
@@ -9,6 +9,7 @@ include { FILTER_EVENT } from '../../modules/local/hicstuff/filter_event'
 include { DISTANCE_LAW } from '../../modules/local/hicstuff/distance_law'
 include { FILTER_PCR } from '../../modules/local/hicstuff/filter_pcr'
 include { FILTER_PCR_DUP } from './filter_pcr_dup'
+include { HIC_PLOT_MATRIX} from '../../modules/local/hicexplorer/hicPlotMatrix'
 
 // Paired-end to Single-end
 def pairToSingle(row, mates) {
@@ -126,4 +127,8 @@ workflow HICSTUFF_SUB {
         chromosome_size.collect(),
         ch_idx_pairs
     )
+
+    HIC_PLOT_MATRIX(
+        BUILD_MATRIX_COOL_ALT.out.matrix
+    )
 }
diff --git a/workflows/hic.nf b/workflows/hic.nf
index 1b6b81e..830a4fa 100644
--- a/workflows/hic.nf
+++ b/workflows/hic.nf
@@ -100,6 +100,7 @@ ch_multiqc_custom_methods_description = params.multiqc_methods_description ? fil
 // MODULE: Local to the pipeline
 //
 include { HIC_PLOT_DIST_VS_COUNTS } from '../modules/local/hicexplorer/hicPlotDistVsCounts'
+include { HIC_PLOT_MATRIX } from '../modules/local/hicexplorer/hicPlotMatrix'
 include { MULTIQC } from '../modules/local/multiqc'
 
 //
@@ -213,6 +214,13 @@ workflow HIC {
     ch_versions = ch_versions.mix(HIC_PLOT_DIST_VS_COUNTS.out.versions)
   }
 
+  //
+  // MODULE: HICEXPLORER/HIC_PLOT_MATRIX
+  //
+  HIC_PLOT_MATRIX(
+    COOLER.out.cool
+  )
+
   //
   // SUB-WORKFLOW: COMPARTMENT CALLING
   //
-- 
GitLab