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