From f40aa5d9285d0cc45d44c07a4211e62f9debd04a Mon Sep 17 00:00:00 2001
From: Laurent Modolo <laurent@modolo.fr>
Date: Mon, 17 Sep 2018 11:07:24 +0200
Subject: [PATCH] BWA: add nf file for paired mapping

---
 src/nf_modules/BWA/indexing.config       | 26 ++++++++++++++++
 src/nf_modules/BWA/indexing.nf           | 29 ++++++++++++++++++
 src/nf_modules/BWA/mapping_paired.config | 26 ++++++++++++++++
 src/nf_modules/BWA/mapping_paired.nf     | 38 ++++++++++++++++++++++++
 src/nf_modules/BWA/tests.sh              | 17 +++++++++++
 5 files changed, 136 insertions(+)
 create mode 100644 src/nf_modules/BWA/indexing.config
 create mode 100644 src/nf_modules/BWA/indexing.nf
 create mode 100644 src/nf_modules/BWA/mapping_paired.config
 create mode 100644 src/nf_modules/BWA/mapping_paired.nf
 create mode 100755 src/nf_modules/BWA/tests.sh

diff --git a/src/nf_modules/BWA/indexing.config b/src/nf_modules/BWA/indexing.config
new file mode 100644
index 0000000..607bda5
--- /dev/null
+++ b/src/nf_modules/BWA/indexing.config
@@ -0,0 +1,26 @@
+profiles {
+  docker {
+    docker.temp = 'auto'
+    docker.enabled = true
+    process {
+      $index_fasta {
+        container = "bwa:0.7.17"
+      }
+    }
+  }
+  sge {
+    process{
+      $index_fasta {
+        beforeScript = "module purge; module load BWA/0.7.17"
+        executor = "sge"
+        cpus = 1
+        memory = "5GB"
+        time = "6h"
+        queueSize = 1000
+        pollInterval = '60sec'
+        queue = 'h6-E5-2667v4deb128'
+        penv = 'openmp8'
+      }
+    }
+  }
+}
diff --git a/src/nf_modules/BWA/indexing.nf b/src/nf_modules/BWA/indexing.nf
new file mode 100644
index 0000000..67ea328
--- /dev/null
+++ b/src/nf_modules/BWA/indexing.nf
@@ -0,0 +1,29 @@
+params.fasta = "$baseDir/data/bam/*.fasta"
+
+log.info "fasta files : ${params.fasta}"
+
+Channel
+  .fromPath( params.fasta )
+  .ifEmpty { error "Cannot find any bam files matching: ${params.fasta}" }
+  .map { it -> [(it.baseName =~ /([^\.]*)/)[0][1], it]}
+  .set { fasta_file }
+
+process index_fasta {
+  tag "$fasta_id"
+  cpus 4
+  publishDir "results/mapping/index/", mode: 'copy'
+
+  input:
+    set fasta_id, file(fasta) from fasta_file
+
+  output:
+    set fasta_id, "${fasta.baseName}.*" into index_files
+    file "*_bwa_report.txt" into index_files_report
+
+  script:
+"""
+bwa index -p ${fasta.baseName} ${fasta} \
+&> ${fasta.baseName}_bwa_report.txt
+"""
+}
+
diff --git a/src/nf_modules/BWA/mapping_paired.config b/src/nf_modules/BWA/mapping_paired.config
new file mode 100644
index 0000000..c0370c3
--- /dev/null
+++ b/src/nf_modules/BWA/mapping_paired.config
@@ -0,0 +1,26 @@
+profiles {
+  docker {
+    docker.temp = 'auto'
+    docker.enabled = true
+    process {
+      $mapping_fastq {
+        container = "bwa:0.7.17"
+      }
+    }
+  }
+  sge {
+    process{
+      $mapping_fastq {
+        beforeScript = "module purge; module load BWA/0.7.17"
+        executor = "sge"
+        cpus = 4
+        memory = "5GB"
+        time = "6h"
+        queueSize = 1000
+        pollInterval = '60sec'
+        queue = 'h6-E5-2667v4deb128'
+        penv = 'openmp8'
+      }
+    }
+  }
+}
diff --git a/src/nf_modules/BWA/mapping_paired.nf b/src/nf_modules/BWA/mapping_paired.nf
new file mode 100644
index 0000000..5ac50ef
--- /dev/null
+++ b/src/nf_modules/BWA/mapping_paired.nf
@@ -0,0 +1,38 @@
+params.fastq = "$baseDir/data/fastq/*_{1,2}.fastq"
+params.index = "$baseDir/data/index/*.index.*"
+
+log.info "fastq files : ${params.fastq}"
+log.info "index files : ${params.index}"
+
+Channel
+  .fromFilePairs( params.fastq )
+  .ifEmpty { error "Cannot find any fastq files matching: ${params.fastq}" }
+  .set { fastq_files }
+Channel
+  .fromPath( params.index )
+  .ifEmpty { error "Cannot find any index files matching: ${params.index}" }
+  .map { it -> [(it.baseName =~ /([^\.]*)/)[0][1], it]}
+  .groupTuple()
+  .set { index_files }
+
+process mapping_fastq {
+  tag "$reads"
+  cpus 4
+  publishDir "results/mapping/sam/", mode: 'copy'
+
+  input:
+  set pair_id, file(reads) from fastq_files
+  set index_id, file(index) from index_files.collect()
+
+  output:
+  file "${pair_id}.sam" into sam_files
+  file "${pair_id}_bwa_report.txt" into mapping_repport_files
+
+  script:
+"""
+bwa mem -t ${task.cpus} \
+${index_id} ${reads[0]} ${reads[1]} \
+-o ${pair_id}.sam &> ${pair_id}_bwa_report.txt
+"""
+}
+
diff --git a/src/nf_modules/BWA/tests.sh b/src/nf_modules/BWA/tests.sh
new file mode 100755
index 0000000..1f3150c
--- /dev/null
+++ b/src/nf_modules/BWA/tests.sh
@@ -0,0 +1,17 @@
+./nextflow src/nf_modules/BWA/indexing.nf \
+  -c src/nf_modules/BWA/indexing.config \
+  -profile docker \
+  --fasta "data/tiny_dataset/fasta/tiny_v2.fasta"
+
+# ./nextflow src/nf_modules/BWA/mapping_single.nf \
+#   -c src/nf_modules/BWA/mapping_single.config \
+#   -profile docker \
+#   --index "results/mapping/index/tiny_v2.index" \
+#   --fastq "data/tiny_dataset/fastq/tiny*_S.fastq"
+
+./nextflow src/nf_modules/BWA/mapping_paired.nf \
+  -c src/nf_modules/BWA/mapping_paired.config \
+  -profile docker \
+  --index "results/mapping/index/tiny_v2*" \
+  --fastq "data/tiny_dataset/fastq/tiny*_R{1,2}.fastq"
+
-- 
GitLab