kkonganti@105: // Define any required imports for this specific workflow kkonganti@105: import java.nio.file.Paths kkonganti@105: import nextflow.file.FileHelper kkonganti@105: kkonganti@105: // Include any necessary methods kkonganti@105: include { \ kkonganti@105: summaryOfParams; stopNow; fastqEntryPointHelp; sendMail; \ kkonganti@105: addPadding; wrapUpHelp } from "${params.routines}" kkonganti@105: include { seqkitrmdupHelp } from "${params.toolshelp}${params.fs}seqkitrmdup" kkonganti@105: include { kraken2Help } from "${params.toolshelp}${params.fs}kraken2" kkonganti@105: include { centrifugeHelp } from "${params.toolshelp}${params.fs}centrifuge" kkonganti@105: include { megahitHelp } from "${params.toolshelp}${params.fs}megahit" kkonganti@105: include { spadesHelp } from "${params.toolshelp}${params.fs}spades" kkonganti@105: include { serotypefinderHelp } from "${params.toolshelp}${params.fs}serotypefinder" kkonganti@105: include { seqsero2Help } from "${params.toolshelp}${params.fs}seqsero2" kkonganti@105: include { mlstHelp } from "${params.toolshelp}${params.fs}mlst" kkonganti@105: include { abricateHelp } from "${params.toolshelp}${params.fs}abricate" kkonganti@105: kkonganti@105: // Exit if help requested before any subworkflows kkonganti@105: if (params.help) { kkonganti@105: log.info help() kkonganti@105: exit 0 kkonganti@105: } kkonganti@105: kkonganti@105: // Include any necessary modules and subworkflows kkonganti@105: include { PROCESS_FASTQ } from "${params.subworkflows}${params.fs}process_fastq" kkonganti@105: include { FASTQC } from "${params.modules}${params.fs}fastqc${params.fs}main" kkonganti@105: include { SEQKIT_RMDUP } from "${params.modules}${params.fs}seqkit${params.fs}rmdup${params.fs}main" kkonganti@105: include { CENTRIFUGE_CLASSIFY } from "${params.modules}${params.fs}centrifuge${params.fs}classify${params.fs}main" kkonganti@105: include { CENTRIFUGE_PROCESS } from "${params.modules}${params.fs}centrifuge${params.fs}process${params.fs}main" kkonganti@105: include { SEQKIT_GREP } from "${params.modules}${params.fs}seqkit${params.fs}grep${params.fs}main" kkonganti@105: include { MEGAHIT_ASSEMBLE } from "${params.modules}${params.fs}megahit${params.fs}assemble${params.fs}main" kkonganti@105: include { SPADES_ASSEMBLE } from "${params.modules}${params.fs}spades${params.fs}assemble${params.fs}main" kkonganti@105: include { KRAKEN2_CLASSIFY } from "${params.modules}${params.fs}kraken2${params.fs}classify${params.fs}main" kkonganti@105: include { KRAKEN2_EXTRACT_CONTIGS } from "${params.modules}${params.fs}kraken2${params.fs}extract_contigs${params.fs}main" kkonganti@105: include { SEROTYPEFINDER } from "${params.modules}${params.fs}serotypefinder${params.fs}main" kkonganti@105: include { SEQSERO2 } from "${params.modules}${params.fs}seqsero2${params.fs}main" kkonganti@105: include { MLST } from "${params.modules}${params.fs}mlst${params.fs}main" kkonganti@105: include { ABRICATE_RUN } from "${params.modules}${params.fs}abricate${params.fs}run${params.fs}main" kkonganti@105: include { ABRICATE_SUMMARY } from "${params.modules}${params.fs}abricate${params.fs}summary${params.fs}main" kkonganti@105: include { TABLE_SUMMARY } from "${params.modules}${params.fs}cat${params.fs}tables${params.fs}main" kkonganti@105: include { MULTIQC } from "${params.modules}${params.fs}multiqc${params.fs}main" kkonganti@105: include { DUMP_SOFTWARE_VERSIONS } from "${params.modules}${params.fs}custom${params.fs}dump_software_versions${params.fs}main" kkonganti@105: kkonganti@105: kkonganti@105: kkonganti@105: /* kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: INPUTS AND ANY CHECKS FOR THE CENTRIFLAKEN-HY WORKFLOW kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: */ kkonganti@105: kkonganti@105: def kraken2_db_dir = file ( "${params.kraken2_db}" ) kkonganti@105: def centrifuge_x = file ( "${params.centrifuge_x}" ) kkonganti@105: def spades_custom_hmm = (params.spades_hmm ? file ( "${params.spades_hmm}" ) : false) kkonganti@105: def reads_platform = 0 kkonganti@105: def abricate_dbs = [ 'ncbiamrplus', 'resfinder', 'megares', 'argannot' ] kkonganti@105: kkonganti@105: reads_platform += (params.input ? 1 : 0) kkonganti@105: kkonganti@105: if (!kraken2_db_dir.exists() || !centrifuge_x.getParent().exists()) { kkonganti@105: stopNow("Please check if the following absolute paths are valid:\n" + kkonganti@105: "${params.kraken2_db}\n${params.centrifuge_x}\n" + kkonganti@105: "Cannot proceed further!") kkonganti@105: } kkonganti@105: kkonganti@105: if (spades_custom_hmm && !spades_custom_hmm.exists()) { kkonganti@105: stopNow("Please check if the following SPAdes' custom HMM directory\n" + kkonganti@105: "path is valid:\n${params.spades_hmm}\nCannot proceed further!") kkonganti@105: } kkonganti@105: kkonganti@105: if (reads_platform < 1 || reads_platform == 0) { kkonganti@105: stopNow("Please mention at least one absolute path to input folder which contains\n" + kkonganti@105: "FASTQ files sequenced using the --input option.\n" + kkonganti@105: "Ex: --input (Illumina or Generic short reads in FASTQ format)") kkonganti@105: } kkonganti@105: kkonganti@105: if (params.centrifuge_extract_bug != params.kraken2_extract_bug) { kkonganti@105: stopNow("Please make sure that the bug to be extracted is same\n" + kkonganti@105: "for both --centrifuge_extract_bug and --kraken2_extract_bug options.") kkonganti@105: } kkonganti@105: kkonganti@105: /* kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: RUN THE CENTRIFLAKEN-HY WORKFLOW kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: */ kkonganti@105: kkonganti@105: workflow CENTRIFLAKEN_HY { kkonganti@105: main: kkonganti@105: ch_asm_filtered_contigs = Channel.empty() kkonganti@105: ch_mqc_custom_tbl = Channel.empty() kkonganti@105: ch_dummy = Channel.fromPath("${params.dummyfile}") kkonganti@105: ch_dummy2 = Channel.fromPath("${params.dummyfile2}") kkonganti@105: kkonganti@105: log.info summaryOfParams() kkonganti@105: kkonganti@105: PROCESS_FASTQ() kkonganti@105: .processed_reads kkonganti@105: .map { kkonganti@105: meta, fastq -> kkonganti@105: meta.centrifuge_x = params.centrifuge_x kkonganti@105: meta.kraken2_db = params.kraken2_db kkonganti@105: [meta, fastq] kkonganti@105: } kkonganti@105: .set { ch_processed_reads } kkonganti@105: kkonganti@105: PROCESS_FASTQ kkonganti@105: .out kkonganti@105: .versions kkonganti@105: .set { software_versions } kkonganti@105: kkonganti@105: FASTQC ( ch_processed_reads ) kkonganti@105: kkonganti@105: if (params.seqkit_rmdup_run) { kkonganti@105: SEQKIT_RMDUP ( ch_processed_reads ) kkonganti@105: kkonganti@105: SEQKIT_RMDUP kkonganti@105: .out kkonganti@105: .fastx kkonganti@105: .set { ch_processed_reads } kkonganti@105: kkonganti@105: software_versions kkonganti@105: .mix ( SEQKIT_RMDUP.out.versions.ifEmpty(null) ) kkonganti@105: .set { software_versions } kkonganti@105: } kkonganti@105: kkonganti@105: CENTRIFUGE_CLASSIFY ( ch_processed_reads ) kkonganti@105: kkonganti@105: CENTRIFUGE_PROCESS ( kkonganti@105: CENTRIFUGE_CLASSIFY.out.report kkonganti@105: .join( CENTRIFUGE_CLASSIFY.out.output ) kkonganti@105: ) kkonganti@105: kkonganti@105: ch_processed_reads.join ( CENTRIFUGE_PROCESS.out.extracted ) kkonganti@105: .set { ch_centrifuge_extracted } kkonganti@105: kkonganti@105: SEQKIT_GREP ( ch_centrifuge_extracted ) kkonganti@105: kkonganti@105: // As of 06/02/2022, with the upcoming newer versions of NextFlow, we will be able to do kkonganti@105: // allowNull: true for both input and output, but until then, we have to use dummy files. kkonganti@105: // and work arounds. kkonganti@105: // https://github.com/nextflow-io/nextflow/pull/2893 kkonganti@105: if (params.spades_run) { kkonganti@105: SPADES_ASSEMBLE ( kkonganti@105: SEQKIT_GREP.out.fastx kkonganti@105: .combine(ch_dummy) kkonganti@105: .combine(ch_dummy2) kkonganti@105: ) kkonganti@105: kkonganti@105: SPADES_ASSEMBLE kkonganti@105: .out kkonganti@105: .assembly kkonganti@105: .set { ch_assembly } kkonganti@105: kkonganti@105: software_versions kkonganti@105: .mix ( SPADES_ASSEMBLE.out.versions.ifEmpty(null) ) kkonganti@105: .set { software_versions } kkonganti@105: } else if (params.megahit_run) { kkonganti@105: MEGAHIT_ASSEMBLE ( kkonganti@105: SEQKIT_GREP.out.fastx kkonganti@105: ) kkonganti@105: kkonganti@105: MEGAHIT_ASSEMBLE kkonganti@105: .out kkonganti@105: .assembly kkonganti@105: .set { ch_assembly } kkonganti@105: kkonganti@105: software_versions kkonganti@105: .mix ( MEGAHIT_ASSEMBLE.out.versions.ifEmpty(null) ) kkonganti@105: .set { software_versions } kkonganti@105: } kkonganti@105: kkonganti@105: ch_assembly kkonganti@105: .map { kkonganti@105: meta, fastq -> kkonganti@105: meta.is_assembly = true kkonganti@105: [meta, fastq] kkonganti@105: } kkonganti@105: .set { ch_assembly } kkonganti@105: kkonganti@105: ch_assembly.ifEmpty { [ false, false ] } kkonganti@105: kkonganti@105: KRAKEN2_CLASSIFY ( ch_assembly ) kkonganti@105: kkonganti@105: KRAKEN2_EXTRACT_CONTIGS ( kkonganti@105: ch_assembly kkonganti@105: .join( KRAKEN2_CLASSIFY.out.kraken_output ), kkonganti@105: params.kraken2_extract_bug kkonganti@105: ) kkonganti@105: kkonganti@105: KRAKEN2_EXTRACT_CONTIGS kkonganti@105: .out kkonganti@105: .asm_filtered_contigs kkonganti@105: .map { kkonganti@105: meta, fastq -> kkonganti@105: meta.organism = params.kraken2_extract_bug.split(/\s+/)[0].capitalize() kkonganti@105: meta.serotypefinder_db = params.serotypefinder_db kkonganti@105: [meta, fastq] kkonganti@105: } kkonganti@105: .set { ch_asm_filtered_contigs } kkonganti@105: kkonganti@105: SEROTYPEFINDER ( ch_asm_filtered_contigs ) kkonganti@105: kkonganti@105: SEQSERO2 ( ch_asm_filtered_contigs ) kkonganti@105: kkonganti@105: MLST ( ch_asm_filtered_contigs ) kkonganti@105: kkonganti@105: ABRICATE_RUN ( kkonganti@105: ch_asm_filtered_contigs, kkonganti@105: abricate_dbs kkonganti@105: ) kkonganti@105: kkonganti@105: ABRICATE_RUN kkonganti@105: .out kkonganti@105: .abricated kkonganti@105: .map { meta, abres -> [ abricate_dbs, abres ] } kkonganti@105: .groupTuple(by: [0]) kkonganti@105: .map { it -> tuple ( it[0], it[1].flatten() ) } kkonganti@105: .set { ch_abricated } kkonganti@105: kkonganti@105: ABRICATE_SUMMARY ( ch_abricated ) kkonganti@105: kkonganti@105: CENTRIFUGE_CLASSIFY.out.kreport kkonganti@105: .map { meta, kreport -> [ kreport ] } kkonganti@105: .flatten() kkonganti@105: .concat ( kkonganti@105: KRAKEN2_CLASSIFY.out.kraken_report kkonganti@105: .map { meta, kreport -> [ kreport ] } kkonganti@105: .flatten(), kkonganti@105: FASTQC.out.zip kkonganti@105: .map { meta, zip -> [ zip ] } kkonganti@105: .flatten() kkonganti@105: ) kkonganti@105: .set { ch_mqc_classify } kkonganti@105: kkonganti@105: if (params.serotypefinder_run) { kkonganti@105: SEROTYPEFINDER kkonganti@105: .out kkonganti@105: .serotyped kkonganti@105: .map { meta, tsv -> [ 'serotypefinder', tsv ] } kkonganti@105: .groupTuple(by: [0]) kkonganti@105: .map { it -> tuple ( it[0], it[1].flatten() ) } kkonganti@105: .set { ch_mqc_custom_tbl } kkonganti@105: } else if (params.seqsero2_run) { kkonganti@105: SEQSERO2 kkonganti@105: .out kkonganti@105: .serotyped kkonganti@105: .map { meta, tsv -> [ 'seqsero2', tsv ] } kkonganti@105: .groupTuple(by: [0]) kkonganti@105: .map { it -> tuple ( it[0], it[1].flatten() ) } kkonganti@105: .set { ch_mqc_custom_tbl } kkonganti@105: } kkonganti@105: kkonganti@105: ch_mqc_custom_tbl kkonganti@105: .concat ( kkonganti@105: ABRICATE_SUMMARY.out.ncbiamrplus.map{ it -> tuple ( it[0], it[1] )}, kkonganti@105: ABRICATE_SUMMARY.out.resfinder.map{ it -> tuple ( it[0], it[1] )}, kkonganti@105: ABRICATE_SUMMARY.out.megares.map{ it -> tuple ( it[0], it[1] )}, kkonganti@105: ABRICATE_SUMMARY.out.argannot.map{ it -> tuple ( it[0], it[1] )}, kkonganti@105: ) kkonganti@105: .groupTuple(by: [0]) kkonganti@105: .map { it -> [ it[0], it[1].flatten() ]} kkonganti@105: .set { ch_mqc_custom_tbl } kkonganti@105: kkonganti@105: TABLE_SUMMARY ( ch_mqc_custom_tbl ) kkonganti@105: kkonganti@105: DUMP_SOFTWARE_VERSIONS ( kkonganti@105: software_versions kkonganti@105: .mix ( kkonganti@105: FASTQC.out.versions, kkonganti@105: CENTRIFUGE_CLASSIFY.out.versions, kkonganti@105: CENTRIFUGE_PROCESS.out.versions, kkonganti@105: SEQKIT_GREP.out.versions, kkonganti@105: KRAKEN2_CLASSIFY.out.versions.ifEmpty(null), kkonganti@105: KRAKEN2_EXTRACT_CONTIGS.out.versions.ifEmpty(null), kkonganti@105: SEROTYPEFINDER.out.versions.ifEmpty(null), kkonganti@105: SEQSERO2.out.versions.ifEmpty(null), kkonganti@105: MLST.out.versions.ifEmpty(null), kkonganti@105: ABRICATE_RUN.out.versions.ifEmpty(null), kkonganti@105: ABRICATE_SUMMARY.out.versions.ifEmpty(null), kkonganti@105: TABLE_SUMMARY.out.versions.ifEmpty(null) kkonganti@105: ) kkonganti@105: .unique() kkonganti@105: .collectFile(name: 'collected_versions.yml') kkonganti@105: ) kkonganti@105: kkonganti@105: DUMP_SOFTWARE_VERSIONS kkonganti@105: .out kkonganti@105: .mqc_yml kkonganti@105: .concat ( kkonganti@105: ch_mqc_classify, kkonganti@105: TABLE_SUMMARY.out.mqc_yml kkonganti@105: ) kkonganti@105: .collect() kkonganti@105: .set { ch_multiqc } kkonganti@105: kkonganti@105: MULTIQC ( ch_multiqc ) kkonganti@105: } kkonganti@105: kkonganti@105: /* kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: ON COMPLETE, SHOW GORY DETAILS OF ALL PARAMS WHICH WILL BE HELPFUL TO DEBUG kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: */ kkonganti@105: kkonganti@105: workflow.onComplete { kkonganti@105: if (workflow.success) { kkonganti@105: // CREATE APPROPRIATE DIRECTORIES AND MOVE AS REQUESTED BY STAKEHOLDER(S) kkonganti@105: // kkonganti@105: // Nextflow's .moveTo will error out if directories contain files and it kkonganti@105: // would be complex to include logic to skip directories kkonganti@105: // kkonganti@105: def final_intermediate_dir = "${params.output}${params.fs}${params.pipeline}-steps" kkonganti@105: def final_results_dir = "${params.output}${params.fs}${params.pipeline}-results" kkonganti@105: def kraken2_ext_contigs = file( "${final_intermediate_dir}${params.fs}kraken2_extract_contigs", type: 'dir' ) kkonganti@105: def final_intermediate = file( final_intermediate_dir, type: 'dir' ) kkonganti@105: def final_results = file( final_results_dir, type: 'dir' ) kkonganti@105: def pipeline_output = file( params.output, type: 'dir' ) kkonganti@105: kkonganti@105: if ( !final_intermediate.exists() ) { kkonganti@105: final_intermediate.mkdirs() kkonganti@105: kkonganti@105: FileHelper.visitFiles(Paths.get("${params.output}"), '*') { kkonganti@105: if ( !(it.name ==~ /^(${params.cfsanpipename}|multiqc|\.nextflow|${workflow.workDir.name}|${params.pipeline}).*/) ) { kkonganti@105: FileHelper.movePath( kkonganti@105: it, Paths.get( "${final_intermediate_dir}${params.fs}${it.name}" ) kkonganti@105: ) kkonganti@105: } kkonganti@105: } kkonganti@105: } kkonganti@105: kkonganti@105: if ( kraken2_ext_contigs.exists() && !final_results.exists() ) { kkonganti@105: final_results.mkdirs() kkonganti@105: kkonganti@105: FileHelper.movePath( kkonganti@105: Paths.get( "${final_intermediate_dir}${params.fs}kraken2_extract_contigs" ), kkonganti@105: Paths.get( "${final_results_dir}${params.fs}kraken2_extract_contigs" ) kkonganti@105: ) kkonganti@105: } kkonganti@105: kkonganti@105: sendMail() kkonganti@105: } kkonganti@105: } kkonganti@105: kkonganti@105: workflow.onError { kkonganti@105: sendMail() kkonganti@105: } kkonganti@105: kkonganti@105: /* kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: HELPER METHODS FOR CENTRIFLAKEN-HY WORKFLOW kkonganti@105: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ kkonganti@105: */ kkonganti@105: kkonganti@105: def help() { kkonganti@105: kkonganti@105: Map helptext = [:] kkonganti@105: kkonganti@105: helptext.putAll ( kkonganti@105: fastqEntryPointHelp() + kkonganti@105: seqkitrmdupHelp(params).text + kkonganti@105: kraken2Help(params).text + kkonganti@105: centrifugeHelp(params).text + kkonganti@105: megahitHelp(params).text + kkonganti@105: spadesHelp(params).text + kkonganti@105: serotypefinderHelp(params).text + kkonganti@105: seqsero2Help(params).text + kkonganti@105: mlstHelp(params).text + kkonganti@105: abricateHelp(params).text + kkonganti@105: wrapUpHelp() kkonganti@105: ) kkonganti@105: kkonganti@105: return addPadding(helptext) kkonganti@105: }