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