From a6974060cc73ed95d26e63ad7f86dac292d0d17f Mon Sep 17 00:00:00 2001
From: Emmanuel Labaronne <emmanuel.labaronne@ens-lyon.fr>
Date: Tue, 10 Nov 2020 19:45:09 +0100
Subject: [PATCH] RNAseq.nf : fix beug on normalized coverage

---
 src/RNAseq.config |  20 +++++--
 src/RNAseq.nf     | 143 +++++++++++++++++++++++++++++++---------------
 2 files changed, 114 insertions(+), 49 deletions(-)

diff --git a/src/RNAseq.config b/src/RNAseq.config
index 5a4cf78f..00c35fde 100644
--- a/src/RNAseq.config
+++ b/src/RNAseq.config
@@ -203,7 +203,7 @@ profiles {
         container = "lbmc/fastqc:0.11.5"
         cpus = 4
       }
-      withName: adaptor_removal {
+      withName: trimming {
         container = "lbmc/cutadapt:2.1"
         cpus = 4
       }
@@ -235,7 +235,7 @@ profiles {
         container = "gcr.io/broad-cga-aarong-gtex/rnaseqc:latest"
         cpus = 1
       }
-      withName: hisat2_postGenomic {
+      withName: hisat2_postgenome {
         cpus = 4
         container = "lbmc/hisat2:2.1.0"
       }
@@ -251,9 +251,21 @@ profiles {
         container = "lbmc/multiqc:1.7"
         cpus = 1
       }
+      withName: calc_scalingFactor{
+          container = "lbmc/hisat2:2.1.0"
+          cpus = 1
+      }
       withName: coverage {
-        container = "lbmc/deeptools:3.0.2"
-        cpus = 8
+          container = "lbmc/deeptools:3.0.2"
+          cpus = 8
+      }
+      withName: calc_scalingFactor_with_postgenome{
+          container = "lbmc/hisat2:2.1.0"
+          cpus = 1
+      }
+      withName: coverage_with_postgenome {
+          container = "lbmc/deeptools:3.0.2"
+          cpus = 16
       }
     }
   }
diff --git a/src/RNAseq.nf b/src/RNAseq.nf
index 65ed93b2..a8b41781 100644
--- a/src/RNAseq.nf
+++ b/src/RNAseq.nf
@@ -462,7 +462,7 @@ rnaseqc ${gtf} ${bam[0]} -s ${file_id} ./ \
 /* HISAT2 POST_GENOMIC */
 /////////////////////////
 
-process hisat2_postGenomic {
+process hisat2_postgenome {
   tag "$file_id"
   publishDir "${params.output}/05_post_genome_hisat2/", mode: 'copy'
 
@@ -569,52 +569,105 @@ process fastqc_postgenome {
 /* Coverage */
 //////////////
 
-process coverage_postgenome {
- tag "$file_id"
- publishDir "${params.output}/07_coverage/", mode: 'copy'
-
- input:
- set file_id, file(bam) from HISAT_ALIGNED_COV
- set file_id_2, file(post_genome) from POSTGENOME_ALIGNED_COV
-
- output:
- file "*.bigwig" into COVERAGE_OUTPUT
-
- when:
- params.do_postgenome
-
- shell:
- '''
- genome=$(samtools view !{bam[0]} | awk '{print $1}' | sort | uniq | wc -l)
- postgenome=$(samtools view !{post_genome[0]} | awk '{print $1}' | sort | uniq | wc -l)
- total=$(($genome + $postgenome))
- factor=$(awk -v c=$total 'BEGIN {print 1000000/c}')
- bamCoverage -p !{task.cpus} --binSize 1 -b !{bam[0]} -o !{file_id}_genome.bigwig
- bamCoverage -p !{task.cpus} --binSize 1 -b !{post_genome[0]} -o !{file_id}_postgenome.bigwig
- '''
-}
-
-process coverage {
- tag "$file_id"
- publishDir "${params.output}/07_coverage/", mode: 'copy'
-
- input:
- set file_id, file(bam) from HISAT_ALIGNED_COV_2
-
- output:
- file "*.bigwig" into COVERAGE_OUTPUT_2
-
- when:
- params.do_postgenome==false
- shell:
- '''
- genome=$(samtools view !{bam[0]} | awk '{print $1} | sort | uniq | wc -l)
- factor=$(awk -v c=$genome 'BEGIN {print 1000000/c}')
- bamCoverage -p !{task.cpus} --binSize 1 -b !{bam[0]} -o !{file_id}.bigwig
- '''
+if (params.do_postgenome){
+	HISAT_ALIGNED_COV.join(POSTGENOME_ALIGNED_COV)
+			 .into{COVERAGE_MERGED_VALUES;COVERAGE_MERGED_BAM}
+
+	process calc_scalingFactor_with_postgenome{
+	 tag "$file_id"
+
+	 input:
+	 set file_id, file(genome), file(post_genome) from COVERAGE_MERGED_VALUES
+ 
+	 output:
+	 set file_id, env(genome), env(postgenome), env(factor) into COVERAGE_MERGED_FACTOR
+
+	 shell:
+	 '''
+	 genome=$(samtools view !{genome[0]} | awk '{print $1}' | sort | uniq | wc -l)
+	 postgenome=$(samtools view !{post_genome[0]} | awk '{print $1}' | sort | uniq | wc -l)
+	 total=$(($genome + $postgenome))
+	 factor=$(awk -v c=$total 'BEGIN {print 1000000/c}')
+	 '''
+	}
+
+	COVERAGE_MERGED_FACTOR.join(COVERAGE_MERGED_BAM)
+			      .set{COVERAGE_MERGED_BIGWIG}
+
+	process coverage_with_postgenome{
+	 tag "$file_id"
+	 publishDir "${params.output}/07_coverage/", mode: 'copy'
+
+	 input:
+	 set file_id, val(genome), val(postgenome), val (factor),  file(genome_bam), file(post_genome_bam) from COVERAGE_MERGED_BIGWIG
+
+	 output:
+	 file "*.bigwig" into COVERAGE_OUTPUT
+	 file "*.txt" into COVERAGE_LOG
+
+	 shell:
+	 '''
+	 total=$((!{genome} + !{postgenome}))
+	 echo "genome aligment : !{genome} counts" >> !{file_id}.txt
+	 echo "postgenome aligment : !{postgenome} counts" >> !{file_id}.txt
+	 echo "total counts : $total" >> !{file_id}.txt
+	 echo "scaling factor : !{factor}" >> !{file_id}.txt
+	 if [ !{genome} -gt 0 ]
+	 then 
+	   bamCoverage -p !{task.cpus} --binSize 1  --scaleFactor !{factor} -b !{genome_bam[0]} -o !{file_id}_genome.bigwig
+	 fi
+	 if [ !{postgenome} -gt 0 ]
+	 then
+	   bamCoverage -p !{task.cpus} --binSize 1  --scaleFactor !{factor} -b !{post_genome_bam[0]} -o !{file_id}_postgenome.bigwig
+	 fi
+	 '''
+	}
+} else {
+	HISAT_ALIGNED_COV.into{COVERAGE_MERGED_VALUES;COVERAGE_MERGED_BAM}
+	
+	process calc_scalingFactor{
+	 tag "$file_id"
+
+	 input:
+	 set file_id, file(genome) from COVERAGE_MERGED_VALUES
+ 
+	 output:
+	 set file_id, env(genome), env(factor) into COVERAGE_MERGED_FACTOR
+
+	 shell:
+	 '''
+	 genome=$(samtools view !{genome[0]} | awk '{print $1}' | sort | uniq | wc -l)
+	 factor=$(awk -v c=$genome 'BEGIN {print 1000000/c}')
+	 '''
+	}
+
+	COVERAGE_MERGED_FACTOR.join(COVERAGE_MERGED_BAM)
+			      .set{COVERAGE_MERGED_BIGWIG}
+
+
+	process coverage {
+	 tag "$file_id"
+	 publishDir "${params.output}/07_coverage/", mode: 'copy'
+
+	 input:
+	 set file_id, val(genome), val (factor),  file(genome_bam) from COVERAGE_MERGED_BIGWIG
+
+	 output:
+	 file "*.bigwig" into COVERAGE_OUTPUT
+	 file "*.txt" into COVERAGE_LOG
+
+	 shell:
+	 '''
+	 echo "genome aligment : !{genome} counts" >> !{file_id}.txt
+	 echo "scaling factor : !{factor}" >> !{file_id}.txt
+	 if [ !{genome} -gt 0 ]
+	 then 
+	   bamCoverage -p !{task.cpus} --binSize 1  --scaleFactor !{factor} -b !{genome_bam[0]} -o !{file_id}_genome.bigwig
+	 fi
+	 '''
+	}
 }
 
-
 /////////////
 /* MultiQC */
 /////////////
-- 
GitLab