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