bowtie2.nf 3.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* Bowtie2 :
* Imputs : fastq files
* Imputs : fasta files
* Output : bam files
*/

/*                      fasta indexing                                     */
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}" }
  .set { fasta_file }

process index_fasta {
  tag "$fasta.baseName"
  cpus 4
  publishDir "results/mapping/index/", mode: 'copy'

  input:
    file fasta from fasta_file

  output:
    file "*.index*" into index_files
28
    file "*_report.txt" into indexing_report
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  script:
"""
bowtie2-build --threads ${task.cpus} ${fasta} ${fasta.baseName}.index &> ${fasta.baseName}_bowtie2_report.txt

if grep -q "Error" ${fasta.baseName}_bowtie2_report.txt; then
  exit 1
fi
"""
}


/*
* for paired-end data
*/

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}" }
  .set { index_files }

process mapping_fastq {
Laurent Modolo's avatar
Laurent Modolo committed
61
  tag "$pair_id"
62
63
64
65
  cpus 4
  publishDir "results/mapping/bams/", mode: 'copy'

  input:
Laurent Modolo's avatar
Laurent Modolo committed
66
  set pair_id, file(reads) from fastq_files
67
  file index from index_files.collect()
68
69

  output:
Laurent Modolo's avatar
Laurent Modolo committed
70
71
  set pair_id, "*.bam" into bam_files
  file "*_report.txt" into mapping_report
72
73

  script:
74
75
  index_id = index[0]
  for (index_file in index) {
76
    if (index_file =~ /.*\.1\.bt2/ && !(index_file =~ /.*\.rev\.1\.bt2/)) {
77
78
79
        index_id = ( index_file =~ /(.*)\.1\.bt2/)[0][1]
    }
  }
80
"""
Laurent Modolo's avatar
Laurent Modolo committed
81
82
83
84
bowtie2 --very-sensitive -p ${task.cpus} -x ${index_id} \
-1 ${reads[0]} -2 ${reads[1]} 2> \
${pair_id}_bowtie2_report.txt | \
samtools view -Sb - > ${pair_id}.bam
85

Laurent Modolo's avatar
Laurent Modolo committed
86
if grep -q "Error" ${pair_id}_bowtie2_report.txt; then
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  exit 1
fi
"""
}

/*
* for single-end data
*/

params.fastq = "$baseDir/data/fastq/*.fastq"

log.info "fastq files : ${params.fastq}"
log.info "index files : ${params.index}"

Channel
  .fromPath( params.fastq )
  .ifEmpty { error "Cannot find any fastq files matching: ${params.fastq}" }
104
  .map { it -> [(it.baseName =~ /([^\.]*)/)[0][1], it]}
105
106
107
108
109
110
111
  .set { fastq_files }
Channel
  .fromPath( params.index )
  .ifEmpty { error "Cannot find any index files matching: ${params.index}" }
  .set { index_files }

process mapping_fastq {
112
  tag "$file_id"
113
114
115
116
  cpus 4
  publishDir "results/mapping/bams/", mode: 'copy'

  input:
117
  set file_id, file(reads) from fastq_files
118
  file index from index_files.collect()
119
120

  output:
121
  set file_id, "*.bam" into bam_files
Laurent Modolo's avatar
Laurent Modolo committed
122
  file "*_report.txt" into mapping_report
123
124

  script:
Laurent Modolo's avatar
Laurent Modolo committed
125
126
  index_id = index[0]
  for (index_file in index) {
127
    if (index_file =~ /.*\.1\.bt2/ && !(index_file =~ /.*\.rev\.1\.bt2/)) {
Laurent Modolo's avatar
Laurent Modolo committed
128
129
130
        index_id = ( index_file =~ /(.*)\.1\.bt2/)[0][1]
    }
  }
131
"""
132
bowtie2 --very-sensitive -p ${task.cpus} -x ${index_id} \
133
-U ${reads} 2> \
134
135
${file_id}_bowtie2_report.txt | \
samtools view -Sb - > ${file_id}.bam
136

137
if grep -q "Error" ${file_id}_bowtie2_report.txt; then
138
139
140
141
  exit 1
fi
"""
}