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