kkonganti@0: // Hold methods to print: kkonganti@0: // 1. Colored logo. kkonganti@0: // 2. Summary of parameters. kkonganti@0: // 3. Single dashed line. kkonganti@0: // 4. Double dashed line. kkonganti@0: // kkonganti@0: kkonganti@0: import groovy.json.JsonSlurper kkonganti@0: import nextflow.config.ConfigParser kkonganti@0: // import groovy.json.JsonOutput kkonganti@0: kkonganti@0: // ASCII logo kkonganti@0: def pipelineBanner() { kkonganti@0: kkonganti@0: def padding = (params.pad) ?: 30 kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: kkonganti@0: def banner = [ kkonganti@0: name: "${fgcolors.magenta}${workflow.manifest.name}${fgcolors.reset}", kkonganti@0: author: "${fgcolors.cyan}${workflow.manifest.author}${fgcolors.reset}", kkonganti@0: // workflow: "${fgcolors.magenta}${params.pipeline}${fgcolors.reset}", kkonganti@0: version: "${fgcolors.green}${workflow.manifest.version}${fgcolors.reset}", kkonganti@0: center: "${fgcolors.green}${params.center}${fgcolors.reset}", kkonganti@0: pad: padding kkonganti@0: ] kkonganti@0: kkonganti@0: manifest = addPadding(banner) kkonganti@0: kkonganti@0: return """${fgcolors.white}${dashedLine(type: '=')}${fgcolors.magenta} kkonganti@0: (o) kkonganti@0: ___ _ __ _ _ __ ___ ___ kkonganti@0: / __|| '_ \\ | || '_ \\ / _ \\/ __| kkonganti@0: | (__ | |_) || || |_) || __/\\__ \\ kkonganti@0: \\___|| .__/ |_|| .__/ \\___||___/ kkonganti@0: | | | | kkonganti@0: |_| |_|${fgcolors.reset} kkonganti@0: ${dashedLine()} kkonganti@0: ${fgcolors.blue}A collection of modular pipelines at CFSAN, FDA.${fgcolors.reset} kkonganti@0: ${dashedLine()} kkonganti@0: ${manifest} kkonganti@0: ${dashedLine(type: '=')} kkonganti@0: """.stripIndent() kkonganti@0: } kkonganti@0: kkonganti@0: // Add padding to keys so that kkonganti@0: // they indent nicely on the kkonganti@0: // terminal kkonganti@0: def addPadding(values) { kkonganti@0: kkonganti@0: def pad = (params.pad) ?: 30 kkonganti@0: values.pad = pad kkonganti@0: kkonganti@0: def padding = values.pad.toInteger() kkonganti@0: def nocapitalize = values.nocapitalize kkonganti@0: def stopnow = values.stopNow kkonganti@0: def help = values.help kkonganti@0: kkonganti@0: values.removeAll { kkonganti@0: k, v -> [ kkonganti@0: 'nocapitalize', kkonganti@0: 'pad', kkonganti@0: 'stopNow', kkonganti@0: 'help' kkonganti@0: ].contains(k) kkonganti@0: } kkonganti@0: kkonganti@0: values.keySet().each { k -> kkonganti@0: v = values[k] kkonganti@0: s = params.linewidth - (pad + 5) kkonganti@0: if (v.toString().size() > s && !stopnow) { kkonganti@0: def sen = '' kkonganti@0: v.toString().findAll(/.{1,${s}}\b(?:\W*|\s*)/).each { kkonganti@0: sen += ' '.multiply(padding + 2) + it + '\n' kkonganti@0: } kkonganti@0: values[k] = ( kkonganti@0: help ? sen.replaceAll(/^(\n|\s)*/, '') : sen.trim() kkonganti@0: ) kkonganti@0: } else { kkonganti@0: values[k] = (help ? v + "\n" : v) kkonganti@0: } kkonganti@0: k = k.replaceAll(/\./, '_') kkonganti@0: } kkonganti@0: kkonganti@0: return values.findResults { kkonganti@0: k, v -> nocapitalize ? kkonganti@0: k.padRight(padding) + ': ' + v : kkonganti@0: k.capitalize().padRight(padding) + ': ' + v kkonganti@0: }.join("\n") kkonganti@0: } kkonganti@0: kkonganti@0: // Method for error messages kkonganti@0: def stopNow(msg) { kkonganti@0: kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: Map errors = [:] kkonganti@0: kkonganti@0: if (msg == null) { kkonganti@0: msg = "Unknown error" kkonganti@0: } kkonganti@0: kkonganti@0: errors['stopNow'] = true kkonganti@0: errors["${params.cfsanpipename} - ${params.pipeline} - ERROR"] = """ kkonganti@0: ${fgcolors.reset}${dashedLine()} kkonganti@0: ${fgcolors.red}${msg}${fgcolors.reset} kkonganti@0: ${dashedLine()} kkonganti@0: """.stripIndent() kkonganti@0: // println dashedLine() // defaults to stdout kkonganti@0: // log.info addPadding(errors) // prints to stdout kkonganti@0: exit 1, "\n" + dashedLine() + kkonganti@0: "${fgcolors.red}\n" + addPadding(errors) kkonganti@0: } kkonganti@0: kkonganti@0: // Method to validate 4 required parameters kkonganti@0: // if input for entry point is FASTQ files kkonganti@0: def validateParamsForFASTQ() { kkonganti@0: switch (params) { kkonganti@0: case { params.metadata == null && params.input == null }: kkonganti@0: stopNow("Either metadata CSV file with 5 required columns\n" + kkonganti@0: "in order: sample, fq1, fq2, strandedness, single_end or \n" + kkonganti@0: "input directory of only FASTQ files (gzipped or unzipped) should be provided\n" + kkonganti@0: "using --metadata or --input options.\n" + kkonganti@0: "None of these two options were provided!") kkonganti@0: break kkonganti@0: case { params.metadata != null && params.input != null }: kkonganti@0: stopNow("Either metadata or input directory of FASTQ files\n" + kkonganti@0: "should be provided using --metadata or --input options.\n" + kkonganti@0: "Using both these options is not allowed!") kkonganti@0: break kkonganti@0: case { params.output == null }: kkonganti@0: stopNow("Please mention output directory to store all results " + kkonganti@0: "using --output option!") kkonganti@0: break kkonganti@0: } kkonganti@0: return 1 kkonganti@0: } kkonganti@0: kkonganti@0: // Method to print summary of parameters kkonganti@0: // before running kkonganti@0: def summaryOfParams() { kkonganti@0: kkonganti@0: def pipeline_specific_config = new ConfigParser().setIgnoreIncludes(true).parse( kkonganti@0: file("${params.workflowsconf}${params.fs}${params.pipeline}.config").text kkonganti@0: ) kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: Map globalparams = [:] kkonganti@0: Map localparams = params.subMap( kkonganti@0: pipeline_specific_config.params.keySet().toList() + params.logtheseparams kkonganti@0: ) kkonganti@0: kkonganti@0: if (localparams !instanceof Map) { kkonganti@0: stopNow("Need a Map of paramters. We got: " + localparams.getClass()) kkonganti@0: } kkonganti@0: kkonganti@0: if (localparams.size() != 0) { kkonganti@0: localparams['nocapitalize'] = true kkonganti@0: globalparams['nocapitalize'] = true kkonganti@0: globalparams['nextflow_version'] = "${nextflow.version}" kkonganti@0: globalparams['nextflow_build'] = "${nextflow.build}" kkonganti@0: globalparams['nextflow_timestamp'] = "${nextflow.timestamp}" kkonganti@0: globalparams['workflow_projectDir'] = "${workflow.projectDir}" kkonganti@0: globalparams['workflow_launchDir'] = "${workflow.launchDir}" kkonganti@0: globalparams['workflow_workDir'] = "${workflow.workDir}" kkonganti@0: globalparams['workflow_container'] = "${workflow.container}" kkonganti@0: globalparams['workflow_containerEngine'] = "${workflow.containerEngine}" kkonganti@0: globalparams['workflow_runName'] = "${workflow.runName}" kkonganti@0: globalparams['workflow_sessionId'] = "${workflow.sessionId}" kkonganti@0: globalparams['workflow_profile'] = "${workflow.profile}" kkonganti@0: globalparams['workflow_start'] = "${workflow.start}" kkonganti@0: globalparams['workflow_commandLine'] = "${workflow.commandLine}" kkonganti@0: return """${dashedLine()} kkonganti@0: Summary of the current workflow (${fgcolors.magenta}${params.pipeline}${fgcolors.reset}) parameters kkonganti@0: ${dashedLine()} kkonganti@0: ${addPadding(localparams)} kkonganti@0: ${dashedLine()} kkonganti@0: ${fgcolors.cyan}N E X T F L O W${fgcolors.reset} - ${fgcolors.magenta}${params.cfsanpipename}${fgcolors.reset} - Runtime metadata kkonganti@0: ${dashedLine()} kkonganti@0: ${addPadding(globalparams)} kkonganti@0: ${dashedLine()}""".stripIndent() kkonganti@0: } kkonganti@0: return 1 kkonganti@0: } kkonganti@0: kkonganti@0: // Method to display kkonganti@0: // Return dashed line either '-' kkonganti@0: // type or '=' type kkonganti@0: def dashedLine(Map defaults = [:]) { kkonganti@0: kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: def line = [color: 'white', type: '-'] kkonganti@0: kkonganti@0: if (!defaults.isEmpty()) { kkonganti@0: line.putAll(defaults) kkonganti@0: } kkonganti@0: kkonganti@0: return fgcolors."${line.color}" + kkonganti@0: "${line.type}".multiply(params.linewidth) + kkonganti@0: fgcolors.reset kkonganti@0: } kkonganti@0: kkonganti@0: // Return slurped keys parsed from JSON kkonganti@0: def slurpJson(file) { kkonganti@0: def slurped = null kkonganti@0: def jsonInst = new JsonSlurper() kkonganti@0: kkonganti@0: try { kkonganti@0: slurped = jsonInst.parse(new File ("${file}")) kkonganti@0: } kkonganti@0: catch (Exception e) { kkonganti@0: log.error 'Please check your JSON schema. Invalid JSON file: ' + file kkonganti@0: } kkonganti@0: kkonganti@0: // Declare globals for the nanofactory kkonganti@0: // workflow. kkonganti@0: return [keys: slurped.keySet().toList(), cparams: slurped] kkonganti@0: } kkonganti@0: kkonganti@0: // Default help text in a map if the entry point kkonganti@0: // to a pipeline is FASTQ files. kkonganti@0: def fastqEntryPointHelp() { kkonganti@0: kkonganti@0: Map helptext = [:] kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: kkonganti@0: helptext['Workflow'] = "${fgcolors.magenta}${params.pipeline}${fgcolors.reset}" kkonganti@0: helptext['Author'] = "${fgcolors.cyan}${params.workflow_built_by}${fgcolors.reset}" kkonganti@0: helptext['Version'] = "${fgcolors.green}${params.workflow_version}${fgcolors.reset}\n" kkonganti@0: helptext['Usage'] = "cpipes --pipeline ${params.pipeline} [options]\n" kkonganti@0: helptext['Required'] = "" kkonganti@0: helptext['--input'] = "Absolute path to directory containing FASTQ files. " + kkonganti@0: "The directory should contain only FASTQ files as all the " + kkonganti@0: "files within the mentioned directory will be read. " + kkonganti@0: "Ex: --input /path/to/fastq_pass" kkonganti@0: helptext['--output'] = "Absolute path to directory where all the pipeline " + kkonganti@0: "outputs should be stored. Ex: --output /path/to/output" kkonganti@0: helptext['Other options'] = "" kkonganti@0: helptext['--metadata'] = "Absolute path to metadata CSV file containing five " + kkonganti@0: "mandatory columns: sample,fq1,fq2,strandedness,single_end. The fq1 and fq2 " + kkonganti@0: "columns contain absolute paths to the FASTQ files. This option can be used in place " + kkonganti@0: "of --input option. This is rare. Ex: --metadata samplesheet.csv" kkonganti@0: helptext['--fq_suffix'] = "The suffix of FASTQ files (Unpaired reads or R1 reads or Long reads) if " + kkonganti@0: "an input directory is mentioned via --input option. Default: ${params.fq_suffix}" kkonganti@0: helptext['--fq2_suffix'] = "The suffix of FASTQ files (Paired-end reads or R2 reads) if an input directory is mentioned via " + kkonganti@0: "--input option. Default: ${params.fq2_suffix}" kkonganti@0: helptext['--fq_filter_by_len'] = "Remove FASTQ reads that are less than this many bases. " + kkonganti@0: "Default: ${params.fq_filter_by_len}" kkonganti@0: helptext['--fq_strandedness'] = "The strandedness of the sequencing run. This is mostly needed " + kkonganti@0: "if your sequencing run is RNA-SEQ. For most of the other runs, it is probably safe to use " + kkonganti@0: "unstranded for the option. Default: ${params.fq_strandedness}" kkonganti@0: helptext['--fq_single_end'] = "SINGLE-END information will be auto-detected but this option forces " + kkonganti@0: "PAIRED-END FASTQ files to be treated as SINGLE-END so only read 1 information is included in " + kkonganti@0: "auto-generated samplesheet. Default: ${params.fq_single_end}" kkonganti@0: helptext['--fq_filename_delim'] = "Delimiter by which the file name is split to obtain sample name. " + kkonganti@0: "Default: ${params.fq_filename_delim}" kkonganti@0: helptext['--fq_filename_delim_idx'] = "After splitting FASTQ file name by using the --fq_filename_delim option," + kkonganti@0: " all elements before this index (1-based) will be joined to create final sample name." + kkonganti@0: " Default: ${params.fq_filename_delim_idx}" kkonganti@0: kkonganti@0: return helptext kkonganti@0: } kkonganti@0: kkonganti@0: // Wrap help text with the following options kkonganti@0: def wrapUpHelp() { kkonganti@0: kkonganti@0: return [ kkonganti@0: 'Help options' : "", kkonganti@0: '--help': "Display this message.\n", kkonganti@0: 'help': true, kkonganti@0: 'nocapitalize': true kkonganti@0: ] kkonganti@0: } kkonganti@0: kkonganti@0: // Method to send email on workflow complete. kkonganti@0: def sendMail() { kkonganti@0: kkonganti@0: if (params.user_email == null) { kkonganti@0: return 1 kkonganti@0: } kkonganti@0: kkonganti@0: def pad = (params.pad) ?: 30 kkonganti@0: def contact_emails = [ kkonganti@0: stakeholder: (params.workflow_blueprint_by ?: 'Not defined'), kkonganti@0: author: (params.workflow_built_by ?: 'Not defined') kkonganti@0: ] kkonganti@0: def msg = """ kkonganti@0: ${pipelineBanner()} kkonganti@0: ${summaryOfParams()} kkonganti@0: ${params.cfsanpipename} - ${params.pipeline} kkonganti@0: ${dashedLine()} kkonganti@0: Please check the following directory for N E X T F L O W kkonganti@0: reports. You can view the HTML files directly by double clicking kkonganti@0: them on your workstation. kkonganti@0: ${dashedLine()} kkonganti@0: ${params.tracereportsdir} kkonganti@0: ${dashedLine()} kkonganti@0: Please send any bug reports to CFSAN Dev Team or the author or kkonganti@0: the stakeholder of the current pipeline. kkonganti@0: ${dashedLine()} kkonganti@0: Error messages (if any) kkonganti@0: ${dashedLine()} kkonganti@0: ${workflow.errorMessage} kkonganti@0: ${workflow.errorReport} kkonganti@0: ${dashedLine()} kkonganti@0: Contact emails kkonganti@0: ${dashedLine()} kkonganti@0: ${addPadding(contact_emails)} kkonganti@0: ${dashedLine()} kkonganti@0: Thank you for using ${params.cfsanpipename} - ${params.pipeline}! kkonganti@0: ${dashedLine()} kkonganti@0: """.stripIndent() kkonganti@0: kkonganti@0: def mail_cmd = [ kkonganti@0: 'sendmail', kkonganti@0: '-f', 'cfsan-hpc-noreply@fda.hhs.gov', kkonganti@0: '-F', 'cfsan-hpc-noreply', kkonganti@0: '-t', "${params.user_email}" kkonganti@0: ] kkonganti@0: kkonganti@0: def email_subject = "${params.cfsanpipename} - ${params.pipeline}" kkonganti@0: Map fgcolors = getANSIColors() kkonganti@0: kkonganti@0: if (workflow.success) { kkonganti@0: email_subject += ' completed successfully!' kkonganti@0: } kkonganti@0: else if (!workflow.success) { kkonganti@0: email_subject += ' has failed!' kkonganti@0: } kkonganti@0: kkonganti@0: try { kkonganti@0: ['env', 'bash'].execute() << """${mail_cmd.join(' ')} kkonganti@0: Subject: ${email_subject} kkonganti@0: Mime-Version: 1.0 kkonganti@0: Content-Type: text/html kkonganti@0:
kkonganti@0: ${msg.replaceAll(/\x1b\[[0-9;]*m/, '')}
kkonganti@0: 
kkonganti@0: """.stripIndent() kkonganti@0: } catch (all) { kkonganti@0: def warning_msg = "${fgcolors.yellow}${params.cfsanpipename} - ${params.pipeline} - WARNING" kkonganti@0: .padRight(pad) + ':' kkonganti@0: log.info """ kkonganti@0: ${dashedLine()} kkonganti@0: ${warning_msg} kkonganti@0: ${dashedLine()} kkonganti@0: Could not send mail with the sendmail command! kkonganti@0: ${dashedLine()} kkonganti@0: """.stripIndent() kkonganti@0: } kkonganti@0: return 1 kkonganti@0: } kkonganti@0: kkonganti@0: // Set ANSI colors for any and all kkonganti@0: // STDOUT or STDERR kkonganti@0: def getANSIColors() { kkonganti@0: kkonganti@0: Map fgcolors = [:] kkonganti@0: kkonganti@0: fgcolors['reset'] = "\033[0m" kkonganti@0: fgcolors['black'] = "\033[0;30m" kkonganti@0: fgcolors['red'] = "\033[0;31m" kkonganti@0: fgcolors['green'] = "\033[0;32m" kkonganti@0: fgcolors['yellow'] = "\033[0;33m" kkonganti@0: fgcolors['blue'] = "\033[0;34m" kkonganti@0: fgcolors['magenta'] = "\033[0;35m" kkonganti@0: fgcolors['cyan'] = "\033[0;36m" kkonganti@0: fgcolors['white'] = "\033[0;37m" kkonganti@0: kkonganti@0: return fgcolors kkonganti@0: }