main.nf 22.9 KB
Newer Older
1 2
#!/usr/bin/env nextflow

3 4 5 6 7 8 9 10 11
/*

BICF ATAC-seq Analysis Workflow
#### Homepage / Documentation
https://git.biohpc.swmed.edu/BICF/Astrocyte/atacseq_analysis/
Licensed under MIT (https://git.biohpc.swmed.edu/BICF/Astrocyte/atacseq_analysis/LICENSE.md)


*/
12 13 14 15

// Define Input variables
params.reads = "$baseDir/../test_data/*.fastq.gz"
params.pairedEnd = false
Holly Ruess's avatar
Holly Ruess committed
16
params.blacklist = false
17
params.designFile = "$baseDir/../test_data/design_ENCSR265ZXX_SE.txt"
18
params.genome = 'GRCh38'
19
params.genomes = []
Holly Ruess's avatar
Holly Ruess committed
20
params.astrocyte = false
Holly Ruess's avatar
Holly Ruess committed
21
params.outDir= "${baseDir}/output"
Holly Ruess's avatar
Holly Ruess committed
22
params.references = "${baseDir}/../docs/references.md"
Holly Ruess's avatar
Holly Ruess committed
23
params.multiqc =  "${baseDir}/conf/multiqc_config.yaml"
Venkat Malladi's avatar
Venkat Malladi committed
24
params.ci = false
Venkat Malladi's avatar
Venkat Malladi committed
25
params.dev = false
26

27 28 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
// Assign variables if astrocyte
if (params.astrocyte) {
  print("Running under astrocyte")
  referenceLocation = "/project/shared/bicf_workflow_ref"
  if (params.genome == 'GRCh38') {
    params.bwaIndex = "$referenceLocation/$params.genome"
    params.chromSizes = "$referenceLocation/$params.genome/genomefile.txt"
    params.gtfFile = "$referenceLocation/$params.genome/gencode.v25.chr_patch_hapl_scaff.annotation.gtf"
    params.geneNames = "$referenceLocation/$params.genome/genenames.txt"
    params.blacklistFile = "$referenceLocation/$params.genome/ENCFF356LFX.bed"
    params.genomeSize = 'hs'
  } else if (params.genome == 'GRCm38') {
    params.bwaIndex = "$referenceLocation/$params.genome"
    params.chromSizes = "$referenceLocation/$params.genome/genomefile.txt"
    params.gtfFile = "$referenceLocation/$params.genome/gencode.vM20.annotation.gtf"
    params.geneNames = "$referenceLocation/$params.genome/genenames.txt"
    params.blacklistFile = "$referenceLocation/$params.genome/ENCFF999QPV.bed"
    params.genomeSize = 'mm'
  }
} else {
  params.bwaIndex = params.genome ? params.genomes[ params.genome ].bwa ?: false : false
  params.genomeSize = params.genome ? params.genomes[ params.genome ].genomesize ?: false : false
  params.chromSizes = params.genome ? params.genomes[ params.genome ].chromsizes ?: false : false
  params.gtfFile = params.genome ? params.genomes[ params.genome ].gtffile ?: false : false
  params.blacklistFile = params.genome ? params.genomes[ params.genome ].blacklistfile ?: false : false
  params.geneNames = params.genome ? params.genomes[ params.genome ].geneNames ?: false : false
}

55
// Check inputs
Venkat Malladi's avatar
Venkat Malladi committed
56 57 58
if(params.bwaIndex) {
  bwaIndex = Channel
    .fromPath(params.bwaIndex)
59 60 61 62 63 64 65
    .ifEmpty { exit 1, "BWA index not found: ${params.bwaIndex}" }
} else {
  exit 1, "No reference genome specified."
}

// Define List of Files
readsList = Channel
Holly Ruess's avatar
Holly Ruess committed
66
  .fromPath(params.reads)
67
  .flatten()
Holly Ruess's avatar
Holly Ruess committed
68 69
  .map { file -> [file.getFileName().toString(), file.toString()].join("\t") }
  .collectFile(name: 'fileList.tsv', newLine: true)
70 71 72

// Define regular variables
pairedEnd = params.pairedEnd
Holly Ruess's avatar
Holly Ruess committed
73
blacklist = params.blacklist
74
designFile = Channel.fromPath(params.designFile)
75 76
genomeSize = params.genomeSize
chromSizes = params.chromSizes
Holly Ruess's avatar
Holly Ruess committed
77
outDir = params.outDir
Holly Ruess's avatar
Holly Ruess committed
78
references = params.references
Holly Ruess's avatar
Holly Ruess committed
79
multiqc = params.multiqc
Holly Ruess's avatar
Holly Ruess committed
80
gtfFile = params.gtfFile
Holly Ruess's avatar
Holly Ruess committed
81
blacklistFile = params.blacklistFile
Holly Ruess's avatar
Holly Ruess committed
82
geneNames = params.geneNames
83

84 85 86 87

/*
 * trackStart: track start of pipeline
 */
Venkat Malladi's avatar
Venkat Malladi committed
88

89 90 91 92 93 94 95 96 97
process trackStart {
  script:
  """
  hostname
  ulimit -a
  export https_proxy=\${http_proxy}

  curl -H 'Content-Type: application/json' -X PUT -d '{ \
      "sessionId": "${workflow.sessionId}", \
Venkat Malladi's avatar
Venkat Malladi committed
98
      "pipeline": "atacseq_analysis", \
99 100 101
      "start": "${workflow.start}", \
      "astrocyte": ${params.astrocyte}, \
      "status": "started", \
Venkat Malladi's avatar
Venkat Malladi committed
102
      "nextflowVersion": "${workflow.nextflow.version}", \
103
      "pipelineVersion": "2.0.3", \
Venkat Malladi's avatar
Venkat Malladi committed
104 105
      "ci": ${params.ci}, \
      "dev": ${params.dev}}' \
106 107 108 109 110
  "https://xku43pcwnf.execute-api.us-east-1.amazonaws.com/ProdDeploy/pipeline-tracking"
  """
}


111 112
process checkDesignFile {

Holly Ruess's avatar
Holly Ruess committed
113
  publishDir "${outDir}/design", mode: 'copy'
114 115

  input:
Holly Ruess's avatar
Holly Ruess committed
116 117
    designFile
    file readsList
118 119

  output:
Holly Ruess's avatar
Holly Ruess committed
120
    file("design.tsv") into designFilePaths
Holly Ruess's avatar
Holly Ruess committed
121
    file("design_fqlength.tsv") into fqlengthDesign
122 123

  script:
Holly Ruess's avatar
Holly Ruess committed
124 125 126 127 128 129 130 131 132 133 134 135 136
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        python3 ${baseDir}/scripts/check_design.py -d ${designFile} -f ${readsList} -p -a
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        python3 ${baseDir}/scripts/check_design.py -d ${designFile} -f ${readsList} -a
        """
      }
Holly Ruess's avatar
Holly Ruess committed
137 138
    }
    else {
Holly Ruess's avatar
Holly Ruess committed
139 140 141 142 143 144 145 146 147 148
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/check_design.py -d ${designFile} -f ${readsList} -p -a
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/check_design.py -d ${designFile} -f ${readsList} -a
        """
      }
Holly Ruess's avatar
Holly Ruess committed
149
    }
150 151 152 153 154 155 156

}

// Define channel for raw reads
if (pairedEnd) {
  rawReads = designFilePaths
    .splitCsv(sep: '\t', header: true)
Holly Ruess's avatar
Holly Ruess committed
157
    .map { row -> [ row.sample_id, [row.fastq_read1, row.fastq_read2], row.experiment_id, row.replicate, row.fq_length ] }
158
}
Holly Ruess's avatar
Holly Ruess committed
159
else {
160 161
rawReads = designFilePaths
  .splitCsv(sep: '\t', header: true)
Holly Ruess's avatar
Holly Ruess committed
162
  .map { row -> [ row.sample_id, [row.fastq_read1], row.experiment_id, row.replicate, row.fq_length ] }
163 164 165 166 167
}

// Trim raw reads using trimgalore
process trimReads {

Holly Ruess's avatar
Holly Ruess committed
168 169
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}/${sampleId}", mode: 'copy'
170 171

  input:
Holly Ruess's avatar
Holly Ruess committed
172
    set sampleId, reads, experimentId, replicate, fqLength from rawReads
173 174

  output:
Holly Ruess's avatar
Holly Ruess committed
175
    set sampleId, file('*.fq.gz'), experimentId, replicate, fqLength into trimmedReads
Holly Ruess's avatar
Holly Ruess committed
176
    file('*trimming_report.txt') into trimgaloreResults
Holly Ruess's avatar
Holly Ruess committed
177
    file('version_*.txt') into trimReadsVersions
178 179

  script:
Holly Ruess's avatar
Holly Ruess committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load trimgalore/0.4.1
        python3 $baseDir/scripts/trim_reads.py -f ${reads[0]} ${reads[1]} -s ${sampleId} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load trimgalore/0.4.1
        python3 ${baseDir}/scripts/trim_reads.py -f ${reads[0]} -s ${sampleId}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/trim_reads.py -f ${reads[0]} ${reads[1]} -s ${sampleId} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/trim_reads.py -f ${reads[0]} -s ${sampleId}
        """
      }
    }
208 209 210 211 212 213

}

// Align trimmed reads using bwa
process alignReads {

Holly Ruess's avatar
Holly Ruess committed
214 215
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}/${sampleId}", mode: 'copy'
216 217

  input:
Holly Ruess's avatar
Holly Ruess committed
218 219
    set sampleId, reads, experimentId, replicate, fqLength from trimmedReads
    file index from bwaIndex.first()
220 221

  output:
Holly Ruess's avatar
Holly Ruess committed
222
    set sampleId, file('*.bam'), experimentId, replicate into mappedReads
Holly Ruess's avatar
Holly Ruess committed
223
    file '*.flagstat.qc' into mappedReadsStats
Holly Ruess's avatar
Holly Ruess committed
224
    file('version_*.txt') into alignReadsVersions
225 226

  script:
Holly Ruess's avatar
Holly Ruess committed
227 228 229 230 231
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load bwa/intel/0.7.12
Holly Ruess's avatar
Holly Ruess committed
232
        module load samtools/1.4.1
Holly Ruess's avatar
Holly Ruess committed
233 234 235 236 237 238 239 240
        module load sambamba/0.6.6
        python3 ${baseDir}/scripts/map_reads.py -f ${reads[0]} ${reads[1]} -r ${index}/genome.fa -s $sampleId -l ${fqLength} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load bwa/intel/0.7.12
Holly Ruess's avatar
Holly Ruess committed
241
        module load samtools/1.4.1
Holly Ruess's avatar
Holly Ruess committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        module load sambamba/0.6.6
        python3 ${baseDir}/scripts/map_reads.py -f ${reads} -r ${index}/genome.fa -s $sampleId -l ${fqLength}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/map_reads.py -f ${reads[0]} ${reads[1]} -r ${index}/genome.fa -s $sampleId -l ${fqLength} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/map_reads.py -f ${reads} -r ${index}/genome.fa -s $sampleId -l ${fqLength}
        """
      }
    }
259 260 261 262 263 264

}

// Dedup reads using sambamba
process filterReads {

Holly Ruess's avatar
Holly Ruess committed
265 266
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}/${sampleId}", mode: 'copy'
267 268

  input:
Holly Ruess's avatar
Holly Ruess committed
269
    set sampleId, mapped, experimentId, replicate from mappedReads
270 271

  output:
Holly Ruess's avatar
Holly Ruess committed
272 273 274 275 276 277
    set sampleId, file('*.bam'), file('*.bai'), experimentId, replicate into dedupReads
    set sampleId, file('*.bam'), file('*.bai'), experimentId, replicate into tssEnrich
    set sampleId, file('*.bam'), file('*.bai'), experimentId, replicate into convertReads
    file '*flagstat.qc' into dedupReadsStats
    file '*pbc.qc' into dedupReadsComplexity
    file '*dup.qc' into dupReads
Holly Ruess's avatar
Holly Ruess committed
278
    file '*.pctmito.tsv' into pctmitoComplexity
Holly Ruess's avatar
Holly Ruess committed
279
    file('version_*.txt') into filterReadsVersions
280 281

  script:
Holly Ruess's avatar
Holly Ruess committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load samtools/1.4.1
        module load sambamba/0.6.6
        module load bedtools/2.26.0
        python3 ${baseDir}/scripts/map_qc.py -b ${mapped} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load samtools/1.4.1
        module load sambamba/0.6.6
        module load bedtools/2.26.0
        python3 ${baseDir}/scripts/map_qc.py -b ${mapped}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/map_qc.py -b ${mapped} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/map_qc.py -b ${mapped}
        """
      }
    }
314 315 316 317 318

}

// Define channel collecting dedup reads into new design file
dedupReads
Holly Ruess's avatar
Holly Ruess committed
319
.map{ sampleId, bam, bai, experimentId, replicate ->
Holly Ruess's avatar
Holly Ruess committed
320 321
"${sampleId}\t${bam}\t${bai}\t${experimentId}\t${replicate}\n"}
.collectFile(name: 'design_dedup.tsv', seed: "sample_id\tbam_reads\tbam_index\texperiment_id\treplicate\n", storeDir: "${outDir}/design")
Holly Ruess's avatar
Holly Ruess committed
322
.into { preDiffDesign }
323 324 325 326

// Convert reads to bam
process convertReads {

Holly Ruess's avatar
Holly Ruess committed
327 328
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}", mode: 'copy'
329 330

  input:
Holly Ruess's avatar
Holly Ruess committed
331
    set sampleId, deduped, bai, experimentId, replicate from convertReads
332 333

  output:
Holly Ruess's avatar
Holly Ruess committed
334
    set sampleId, file('*.tagAlign.gz'), file('*.bed{pe,se}.gz'), experimentId, replicate into tagReads
Holly Ruess's avatar
Holly Ruess committed
335
    file('version_*.txt') into convertReadsVersions
336 337

  script:
Holly Ruess's avatar
Holly Ruess committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load samtools/1.4.1
        module load bedtools/2.26.0
        python3 ${baseDir}/scripts/convert_reads.py -b ${deduped} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load samtools/1.4.1
        module load bedtools/2.26.0
        python3 ${baseDir}/scripts/convert_reads.py -b ${deduped}
        """
      }
355
    }
Holly Ruess's avatar
Holly Ruess committed
356 357 358 359 360 361 362 363 364 365 366
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/convert_reads.py -b ${deduped} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/convert_reads.py -b ${deduped}
        """
      }
367
    }
368 369 370 371 372 373 374

}


// Calculate Cross-correlation using phantompeaktools
process crossReads {

Holly Ruess's avatar
Holly Ruess committed
375 376
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}/${sampleId}", mode: 'copy'
377 378

  input:
Holly Ruess's avatar
Holly Ruess committed
379
    set sampleId, seTagAlign, tagAlign, experimentId, replicate from tagReads
380 381

  output:
Holly Ruess's avatar
Holly Ruess committed
382
    set sampleId, seTagAlign, tagAlign, file('*.cc.qc'), experimentId, replicate into xcorReads
Holly Ruess's avatar
Holly Ruess committed
383
    set file('*.cc.qc'), file('*.cc.plot.pdf') into xcorReadsStats
Holly Ruess's avatar
Holly Ruess committed
384
    file('version_*.txt') into crossReadsVersions
385 386

  script:
Holly Ruess's avatar
Holly Ruess committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load phantompeakqualtools/1.2
        python3 ${baseDir}/scripts/xcor.py -t ${tagAlign} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load phantompeakqualtools/1.2
        python3 ${baseDir}/scripts/xcor.py -t ${tagAlign}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/xcor.py -t ${tagAlign} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/xcor.py -t ${tagAlign}
        """
      }
    }
415 416 417 418 419

}

// Define channel collecting tagAlign and xcor into design file
xcorDesign = xcorReads
Holly Ruess's avatar
Holly Ruess committed
420 421
              .map{ sampleId, seTagAlign, tagAlign, xcor, experimentId, replicate ->
              "${sampleId}\t${seTagAlign}\t${tagAlign}\t${xcor}\t${experimentId}\t${replicate}\n" }
Holly Ruess's avatar
Holly Ruess committed
422
              .collectFile(name:'design_xcor.tsv', seed:"sample_id\tseTagAlign\ttag_align\txcor\texperiment_id\treplicate\n", storeDir:"${outDir}/design")
423 424 425 426

// Make Experiment design files to be read in for downstream analysis
process defineExpDesignFiles {

Holly Ruess's avatar
Holly Ruess committed
427
  publishDir "${outDir}/design", mode: 'copy'
428 429

  input:
Holly Ruess's avatar
Holly Ruess committed
430
    file xcorDesign
431 432

  output:
Holly Ruess's avatar
Holly Ruess committed
433
    file '*.tsv' into experimentObjs mode flatten
434 435

  script:
Holly Ruess's avatar
Holly Ruess committed
436 437 438 439 440 441 442 443 444 445 446
    if (params.astrocyte == true) {
      """
      module load python/3.6.1-2-anaconda
      python3 ${baseDir}/scripts/experiment_design.py -d ${xcorDesign}
      """
    }
    else {
      """
      python3 ${baseDir}/scripts/experiment_design.py -d ${xcorDesign}
      """
    }
447 448 449 450 451 452 453 454 455

}


// Make Experiment design files to be read in for downstream analysis
process poolAndPsuedoReads {


  tag "${experimentObjs.baseName}"
Holly Ruess's avatar
Holly Ruess committed
456
  publishDir "${outDir}/design", mode: 'copy'
457 458

  input:
Holly Ruess's avatar
Holly Ruess committed
459
    file experimentObjs
460 461

  output:
Holly Ruess's avatar
Holly Ruess committed
462
    file '*.tsv' into experimentPoolObjs
463 464

  script:
Holly Ruess's avatar
Holly Ruess committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        python3 ${baseDir}/scripts/pool_and_psuedoreplicate.py -d ${experimentObjs} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        python3 ${baseDir}/scripts/pool_and_psuedoreplicate.py -d ${experimentObjs}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/pool_and_psuedoreplicate.py -d ${experimentObjs} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/pool_and_psuedoreplicate.py -d ${experimentObjs}
        """
      }
    }
491 492 493 494 495 496

}

// Collect list of experiment design files into a single channel
experimentRows = experimentPoolObjs
                .splitCsv(sep:'\t', header:true)
Holly Ruess's avatar
Holly Ruess committed
497
                .map { row -> [ row.sample_id, row.tag_align, row.xcor, row.experiment_id, row.replicate] }
498 499 500 501

// Call Peaks using MACS
process callPeaksMACS {

Holly Ruess's avatar
Holly Ruess committed
502 503
  tag "${sampleId}-${replicate}"
  publishDir "${outDir}/${task.process}/${experimentId}/${replicate}", mode: 'copy'
504 505

  input:
Holly Ruess's avatar
Holly Ruess committed
506
    set sampleId, tagAlign, xcor, experimentId, replicate from experimentRows
507 508

  output:
Holly Ruess's avatar
Holly Ruess committed
509 510 511
    set sampleId, file('*.narrowPeak'), file('*.fc_signal.bw'), file('*.pvalue_signal.bw'), experimentId, replicate into experimentPeaks
    file '*.xls' into callPeaksMACSsummit
    file('version_*.txt') into callPeaksMACSVersions
512 513

  script:
Holly Ruess's avatar
Holly Ruess committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    if (params.astrocyte == true) {
      if (pairedEnd) {
        """
        module load python/3.6.1-2-anaconda
        module load macs/2.1.0-20151222
        module load UCSC_userApps/v317
        module load bedtools/2.26.0
        module load phantompeakqualtools/1.2
        python3 ${baseDir}/scripts/call_peaks_macs.py -t ${tagAlign} -x ${xcor} -s ${sampleId} -g ${genomeSize} -z ${chromSizes} -p
        """
      }
      else {
        """
        module load python/3.6.1-2-anaconda
        module load macs/2.1.0-20151222
        module load UCSC_userApps/v317
        module load bedtools/2.26.0
        module load phantompeakqualtools/1.2
        python3 ${baseDir}/scripts/call_peaks_macs.py -t ${tagAlign} -x ${xcor} -s ${sampleId} -g ${genomeSize} -z ${chromSizes}
        """
      }
    }
    else {
      if (pairedEnd) {
        """
        python3 ${baseDir}/scripts/call_peaks_macs.py -t ${tagAlign} -x ${xcor} -s ${sampleId} -g ${genomeSize} -z ${chromSizes} -p
        """
      }
      else {
        """
        python3 ${baseDir}/scripts/call_peaks_macs.py -t ${tagAlign} -x ${xcor} -s ${sampleId} -g ${genomeSize} -z ${chromSizes}
        """
      }
    }
548 549 550 551 552 553


}

// Define channel collecting peaks into design file
peaksDesign = experimentPeaks
Holly Ruess's avatar
Holly Ruess committed
554
              .map{ sampleId, peak, fcSignal, pvalueSignal, experimentId, replicate ->
Holly Ruess's avatar
Holly Ruess committed
555 556
              "${sampleId}\t${peak}\t${fcSignal}\t${pvalueSignal}\t${experimentId}\t${replicate}\n"}
              .collectFile(name:'design_peak.tsv', seed:"sample_id\tpeaks\tfc_signal\tpvalue_signal\texperiment_id\treplicate\n", storeDir:"${outDir}/design")
557 558 559 560

// Calculate Consensus Peaks
process consensusPeaks {

Holly Ruess's avatar
Holly Ruess committed
561
  publishDir "${outDir}/${task.process}", mode: 'copy'
562 563

  input:
Holly Ruess's avatar
Holly Ruess committed
564 565
    file peaksDesign
    file preDiffDesign
566 567

  output:
Holly Ruess's avatar
Holly Ruess committed
568 569
    file '*replicated*' into consensusPeaks
    file '*rejected*' into rejectedPeaks
Holly Ruess's avatar
Holly Ruess committed
570
    file("design_exQC.tsv") into designExperimentQC
Holly Ruess's avatar
Holly Ruess committed
571
    file('version_*.txt') into consensusPeaksVersions
Holly Ruess's avatar
Holly Ruess committed
572
    file("design_annotatePeaks.tsv") into designAnnotatePeaks
573 574

  script:
Holly Ruess's avatar
Holly Ruess committed
575
    if (params.astrocyte == true) {
Holly Ruess's avatar
Holly Ruess committed
576 577 578
        if (blacklist) {
          """
          module load python/3.6.1-2-anaconda
579
          module load bedtools/2.25.0
Holly Ruess's avatar
Holly Ruess committed
580 581 582 583 584 585
          python3 ${baseDir}/scripts/overlap_peaks.py -d ${peaksDesign} -f ${preDiffDesign} -b ${blacklistFile}
          """
        }
        else {
          """
          module load python/3.6.1-2-anaconda
586
          module load bedtools/2.25.0
Holly Ruess's avatar
Holly Ruess committed
587 588 589
          python3 ${baseDir}/scripts/overlap_peaks.py -d ${peaksDesign} -f ${preDiffDesign}
          """
        }
Holly Ruess's avatar
Holly Ruess committed
590 591
    }
    else {
Holly Ruess's avatar
Holly Ruess committed
592 593 594 595 596 597 598 599 600 601
        if (blacklist) {
          """
          python3 ${baseDir}/scripts/overlap_peaks.py -d ${peaksDesign} -f ${preDiffDesign} -b ${blacklistFile}
          """
        }
        else {
          """
          python3 ${baseDir}/scripts/overlap_peaks.py -d ${peaksDesign} -f ${preDiffDesign}
          """
        }
Holly Ruess's avatar
Holly Ruess committed
602
    }
603 604

}
Holly Ruess's avatar
Holly Ruess committed
605

Holly Ruess's avatar
Holly Ruess committed
606

Holly Ruess's avatar
Holly Ruess committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
// Annotate Peaks
process peakAnnotation {

  publishDir "${outDir}/${task.process}", mode: 'copy'

  input:

  file designAnnotatePeaks

  output:

  file "*chipseeker*" into peakAnnotation
  file('version_*.txt') into peakAnnotationVersions

  script:
    if (params.astrocyte == true) {
      """
      module load R/3.3.2-gccmkl
      Rscript $baseDir/scripts/annotate_peaks.R ${designAnnotatePeaks} ${gtfFile} ${geneNames}
      """
    }
    else {
      """
      Rscript $baseDir/scripts/annotate_peaks.R ${designAnnotatePeaks} ${gtfFile} ${geneNames}
      """
    }

}
Holly Ruess's avatar
Holly Ruess committed
635 636


Holly Ruess's avatar
Holly Ruess committed
637 638 639 640 641 642 643
// Quality Metrics using deeptools
process experimentQC {

  publishDir "${outDir}/${task.process}", mode: 'copy'

  input:
    file designExperimentQC
Holly Ruess's avatar
Holly Ruess committed
644
    file fqlengthDesign
Holly Ruess's avatar
Holly Ruess committed
645 646 647

  output:
    file '*.{pdf,npz}' into deepToolsStats
Holly Ruess's avatar
Holly Ruess committed
648
    file '*.fragment_length_count.txt' optional true into fdlStats
Holly Ruess's avatar
Holly Ruess committed
649
    file('version_*.txt') into experimentQCVersions
Holly Ruess's avatar
Holly Ruess committed
650 651
    file '*.FRiPscore.tsv' into FRiPscore
    file '*.TSSenrichment.tsv' into TSSenrichment
Holly Ruess's avatar
Holly Ruess committed
652 653 654

  script:
    if (params.astrocyte == true) {
Holly Ruess's avatar
Holly Ruess committed
655 656 657 658 659 660 661 662
      if (pairedEnd) {
          """
          module load python/3.6.1-2-anaconda
          module load deeptools/2.5.0.1
          module load samtools/1.4.1
          module load bedtools/2.26.0
          python3 ${baseDir}/scripts/experiment_qc.py -d ${designExperimentQC} -p
          bash ${baseDir}/scripts/make_tss.sh ${gtfFile}
663
          module load singularity/3.0.2
Holly Ruess's avatar
Holly Ruess committed
664 665 666 667 668 669 670 671 672 673 674
          singularity run /project/shared/bicf_workflow_ref/singularity_images/metaseq.simg ${baseDir}/scripts/atac_qc.py -d ${designExperimentQC} -l ${fqlengthDesign} -t gencode.tss -c ${chromSizes}
          """
      }
      else {
          """
          module load python/3.6.1-2-anaconda
          module load deeptools/2.5.0.1
          module load samtools/1.4.1
          module load bedtools/2.26.0
          python3 ${baseDir}/scripts/experiment_qc.py -d ${designExperimentQC}
          bash ${baseDir}/scripts/make_tss.sh ${gtfFile}
675
          module load singularity/3.0.2
Holly Ruess's avatar
Holly Ruess committed
676 677 678
          singularity run /project/shared/bicf_workflow_ref/singularity_images/metaseq.simg ${baseDir}/scripts/atac_qc.py -d ${designExperimentQC} -l ${fqlengthDesign} -t gencode.tss -c ${chromSizes}
          """
      }
Holly Ruess's avatar
Holly Ruess committed
679 680
    }
    else {
Holly Ruess's avatar
Holly Ruess committed
681 682 683 684 685 686 687 688 689 690 691 692 693 694
      if (pairedEnd) {
          """
          python3 ${baseDir}/scripts/experiment_qc.py -d ${designExperimentQC} -p
          bash ${baseDir}/scripts/make_tss.sh ${gtfFile}
          singularity run /project/shared/bicf_workflow_ref/singularity_images/metaseq.simg ${baseDir}/scripts/atac_qc.py -d ${designExperimentQC} -l ${fqlengthDesign} -t gencode.tss -c ${chromSizes}
          """
      }
      else {
          """
          python3 ${baseDir}/scripts/experiment_qc.py -d ${designExperimentQC}
          bash ${baseDir}/scripts/make_tss.sh ${gtfFile}
          singularity run /project/shared/bicf_workflow_ref/singularity_images/metaseq.simg ${baseDir}/scripts/atac_qc.py -d ${designExperimentQC} -l ${fqlengthDesign} -t gencode.tss -c ${chromSizes}
          """
      }
Holly Ruess's avatar
Holly Ruess committed
695 696 697 698 699
    }

}


Holly Ruess's avatar
Holly Ruess committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
// Generate Multiqc Report, gerernate Software Versions and references
process multiqcReport {

  publishDir "${outDir}/${task.process}", mode: 'copy'

  input:
    file ('trimReads_vf/*') from trimReadsVersions.first()
    file ('alignReads_vf/*') from alignReadsVersions.first()
    file ('filterReads_vf/*') from filterReadsVersions.first()
    file ('experimentQC_vf/*') from experimentQCVersions.first()
    file ('convertReads_vf/*') from convertReadsVersions.first()
    file ('crossReads_vf/*') from crossReadsVersions.first()
    file ('callPeaksMACS_vf/*') from callPeaksMACSVersions.first()
    file ('consensusPeaks_vf/*') from consensusPeaksVersions.first()
    file ('trimReads/*') from trimgaloreResults.collect()
    file ('alignReads/*') from mappedReadsStats.collect()
    file ('filterReads/*') from dedupReadsComplexity.collect()
Holly Ruess's avatar
Holly Ruess committed
717
    file ('filterReads/*') from pctmitoComplexity.collect()
Holly Ruess's avatar
Holly Ruess committed
718
    file ('crossReads/*') from xcorReadsStats.collect()
Holly Ruess's avatar
Holly Ruess committed
719 720
    file ('experimentQC/*') from FRiPscore.collect()
    file ('experimentQC/*') from TSSenrichment.collect()
Holly Ruess's avatar
Holly Ruess committed
721 722 723 724 725 726 727 728

  output:
    file('software_versions_mqc.yaml') into softwareVersions
    file('software_references_mqc.yaml') into softwareReferences
    file "multiqc_report.html" into multiqcReport
    file "*_data" into multiqcData

  script:
Holly Ruess's avatar
Holly Ruess committed
729 730 731
    if (params.astrocyte == true) {
      """
      module load python/3.6.1-2-anaconda
Holly Ruess's avatar
Holly Ruess committed
732
      module load singularity/3.0.2
Holly Ruess's avatar
Holly Ruess committed
733 734
      module load pandoc/2.7
      echo $workflow.nextflow.version > version_nextflow.txt
Holly Ruess's avatar
Holly Ruess committed
735
      singularity exec /project/shared/bicf_workflow_ref/singularity_images/bicf-multiqc-2.0.0.img multiqc --version > version_multiqc.txt
Holly Ruess's avatar
Holly Ruess committed
736 737 738
      python --version &> version_python.txt
      python3 ${baseDir}/scripts/generate_references.py -r ${references} -o software_references
      python3 ${baseDir}/scripts/generate_versions.py -o software_versions
Holly Ruess's avatar
Holly Ruess committed
739
      singularity exec /project/shared/bicf_workflow_ref/singularity_images/bicf-multiqc-2.0.0.img multiqc -c ${multiqc} .
Holly Ruess's avatar
Holly Ruess committed
740 741 742 743 744
      """
    }
    else {
      """
      echo $workflow.nextflow.version > version_nextflow.txt
Holly Ruess's avatar
Holly Ruess committed
745
      singularity exec /project/shared/bicf_workflow_ref/singularity_images/bicf-multiqc-2.0.0.img multiqc --version > version_multiqc.txt
Holly Ruess's avatar
Holly Ruess committed
746 747 748
      python --version &> version_python.txt
      python3 ${baseDir}/scripts/generate_references.py -r ${references} -o software_references
      python3 ${baseDir}/scripts/generate_versions.py -o software_versions
Holly Ruess's avatar
Holly Ruess committed
749
      singularity exec /project/shared/bicf_workflow_ref/singularity_images/bicf-multiqc-2.0.0.img multiqc -c ${multiqc} .
Holly Ruess's avatar
Holly Ruess committed
750 751
      """
    }
Holly Ruess's avatar
Holly Ruess committed
752 753

}