annotate 0.4.0/lib/routines.nf @ 101:ce6d9548fe89

"planemo upload"
author kkonganti
date Thu, 04 Aug 2022 10:45:55 -0400
parents
children
rev   line source
kkonganti@101 1 // Hold methods to print:
kkonganti@101 2 // 1. Colored logo.
kkonganti@101 3 // 2. Summary of parameters.
kkonganti@101 4 // 3. Single dashed line.
kkonganti@101 5 // 4. Double dashed line.
kkonganti@101 6 //
kkonganti@101 7
kkonganti@101 8 import groovy.json.JsonSlurper
kkonganti@101 9 import nextflow.config.ConfigParser
kkonganti@101 10 // import groovy.json.JsonOutput
kkonganti@101 11
kkonganti@101 12 // ASCII logo
kkonganti@101 13 def pipelineBanner() {
kkonganti@101 14
kkonganti@101 15 def padding = (params.pad) ?: 30
kkonganti@101 16 Map fgcolors = getANSIColors()
kkonganti@101 17
kkonganti@101 18 def banner = [
kkonganti@101 19 name: "${fgcolors.magenta}${workflow.manifest.name}${fgcolors.reset}",
kkonganti@101 20 author: "${fgcolors.cyan}${workflow.manifest.author}${fgcolors.reset}",
kkonganti@101 21 // workflow: "${fgcolors.magenta}${params.pipeline}${fgcolors.reset}",
kkonganti@101 22 version: "${fgcolors.green}${workflow.manifest.version}${fgcolors.reset}",
kkonganti@101 23 center: "${fgcolors.green}${params.center}${fgcolors.reset}",
kkonganti@101 24 pad: padding
kkonganti@101 25 ]
kkonganti@101 26
kkonganti@101 27 manifest = addPadding(banner)
kkonganti@101 28
kkonganti@101 29 return """${fgcolors.white}${dashedLine(type: '=')}${fgcolors.magenta}
kkonganti@101 30 (o)
kkonganti@101 31 ___ _ __ _ _ __ ___ ___
kkonganti@101 32 / __|| '_ \\ | || '_ \\ / _ \\/ __|
kkonganti@101 33 | (__ | |_) || || |_) || __/\\__ \\
kkonganti@101 34 \\___|| .__/ |_|| .__/ \\___||___/
kkonganti@101 35 | | | |
kkonganti@101 36 |_| |_|${fgcolors.reset}
kkonganti@101 37 ${dashedLine()}
kkonganti@101 38 ${fgcolors.blue}A collection of modular pipelines at CFSAN, FDA.${fgcolors.reset}
kkonganti@101 39 ${dashedLine()}
kkonganti@101 40 ${manifest}
kkonganti@101 41 ${dashedLine(type: '=')}
kkonganti@101 42 """.stripIndent()
kkonganti@101 43 }
kkonganti@101 44
kkonganti@101 45 // Add padding to keys so that
kkonganti@101 46 // they indent nicely on the
kkonganti@101 47 // terminal
kkonganti@101 48 def addPadding(values) {
kkonganti@101 49
kkonganti@101 50 def pad = (params.pad) ?: 30
kkonganti@101 51 values.pad = pad
kkonganti@101 52
kkonganti@101 53 def padding = values.pad.toInteger()
kkonganti@101 54 def nocapitalize = values.nocapitalize
kkonganti@101 55 def stopnow = values.stopNow
kkonganti@101 56 def help = values.help
kkonganti@101 57
kkonganti@101 58 values.removeAll {
kkonganti@101 59 k, v -> [
kkonganti@101 60 'nocapitalize',
kkonganti@101 61 'pad',
kkonganti@101 62 'stopNow',
kkonganti@101 63 'help'
kkonganti@101 64 ].contains(k)
kkonganti@101 65 }
kkonganti@101 66
kkonganti@101 67 values.keySet().each { k ->
kkonganti@101 68 v = values[k]
kkonganti@101 69 s = params.linewidth - (pad + 5)
kkonganti@101 70 if (v.toString().size() > s && !stopnow) {
kkonganti@101 71 def sen = ''
kkonganti@101 72 v.toString().findAll(/.{1,${s}}\b(?:\W*|\s*)/).each {
kkonganti@101 73 sen += ' '.multiply(padding + 2) + it + '\n'
kkonganti@101 74 }
kkonganti@101 75 values[k] = (
kkonganti@101 76 help ? sen.replaceAll(/^(\n|\s)*/, '') : sen.trim()
kkonganti@101 77 )
kkonganti@101 78 } else {
kkonganti@101 79 values[k] = (help ? v + "\n" : v)
kkonganti@101 80 }
kkonganti@101 81 k = k.replaceAll(/\./, '_')
kkonganti@101 82 }
kkonganti@101 83
kkonganti@101 84 return values.findResults {
kkonganti@101 85 k, v -> nocapitalize ?
kkonganti@101 86 k.padRight(padding) + ': ' + v :
kkonganti@101 87 k.capitalize().padRight(padding) + ': ' + v
kkonganti@101 88 }.join("\n")
kkonganti@101 89 }
kkonganti@101 90
kkonganti@101 91 // Method for error messages
kkonganti@101 92 def stopNow(msg) {
kkonganti@101 93
kkonganti@101 94 Map fgcolors = getANSIColors()
kkonganti@101 95 Map errors = [:]
kkonganti@101 96
kkonganti@101 97 if (msg == null) {
kkonganti@101 98 msg = "Unknown error"
kkonganti@101 99 }
kkonganti@101 100
kkonganti@101 101 errors['stopNow'] = true
kkonganti@101 102 errors["${params.cfsanpipename} - ${params.pipeline} - ERROR"] = """
kkonganti@101 103 ${fgcolors.reset}${dashedLine()}
kkonganti@101 104 ${fgcolors.red}${msg}${fgcolors.reset}
kkonganti@101 105 ${dashedLine()}
kkonganti@101 106 """.stripIndent()
kkonganti@101 107 // println dashedLine() // defaults to stdout
kkonganti@101 108 // log.info addPadding(errors) // prints to stdout
kkonganti@101 109 exit 1, "\n" + dashedLine() +
kkonganti@101 110 "${fgcolors.red}\n" + addPadding(errors)
kkonganti@101 111 }
kkonganti@101 112
kkonganti@101 113 // Method to validate 4 required parameters
kkonganti@101 114 // if input for entry point is FASTQ files
kkonganti@101 115 def validateParamsForFASTQ() {
kkonganti@101 116 switch (params) {
kkonganti@101 117 case { params.metadata == null && params.input == null }:
kkonganti@101 118 stopNow("Either metadata CSV file with 5 required columns\n" +
kkonganti@101 119 "in order: sample, fq1, fq2, strandedness, single_end or \n" +
kkonganti@101 120 "input directory of only FASTQ files (gzipped or unzipped) should be provided\n" +
kkonganti@101 121 "using --metadata or --input options.\n" +
kkonganti@101 122 "None of these two options were provided!")
kkonganti@101 123 break
kkonganti@101 124 case { params.metadata != null && params.input != null }:
kkonganti@101 125 stopNow("Either metadata or input directory of FASTQ files\n" +
kkonganti@101 126 "should be provided using --metadata or --input options.\n" +
kkonganti@101 127 "Using both these options is not allowed!")
kkonganti@101 128 break
kkonganti@101 129 case { params.output == null }:
kkonganti@101 130 stopNow("Please mention output directory to store all results " +
kkonganti@101 131 "using --output option!")
kkonganti@101 132 break
kkonganti@101 133 }
kkonganti@101 134 return 1
kkonganti@101 135 }
kkonganti@101 136
kkonganti@101 137 // Method to print summary of parameters
kkonganti@101 138 // before running
kkonganti@101 139 def summaryOfParams() {
kkonganti@101 140
kkonganti@101 141 def pipeline_specific_config = new ConfigParser().setIgnoreIncludes(true).parse(
kkonganti@101 142 file("${params.workflowsconf}${params.fs}${params.pipeline}.config").text
kkonganti@101 143 )
kkonganti@101 144 Map fgcolors = getANSIColors()
kkonganti@101 145 Map globalparams = [:]
kkonganti@101 146 Map localparams = params.subMap(
kkonganti@101 147 pipeline_specific_config.params.keySet().toList() + params.logtheseparams
kkonganti@101 148 )
kkonganti@101 149
kkonganti@101 150 if (localparams !instanceof Map) {
kkonganti@101 151 stopNow("Need a Map of paramters. We got: " + localparams.getClass())
kkonganti@101 152 }
kkonganti@101 153
kkonganti@101 154 if (localparams.size() != 0) {
kkonganti@101 155 localparams['nocapitalize'] = true
kkonganti@101 156 globalparams['nocapitalize'] = true
kkonganti@101 157 globalparams['nextflow_version'] = "${nextflow.version}"
kkonganti@101 158 globalparams['nextflow_build'] = "${nextflow.build}"
kkonganti@101 159 globalparams['nextflow_timestamp'] = "${nextflow.timestamp}"
kkonganti@101 160 globalparams['workflow_projectDir'] = "${workflow.projectDir}"
kkonganti@101 161 globalparams['workflow_launchDir'] = "${workflow.launchDir}"
kkonganti@101 162 globalparams['workflow_workDir'] = "${workflow.workDir}"
kkonganti@101 163 globalparams['workflow_container'] = "${workflow.container}"
kkonganti@101 164 globalparams['workflow_containerEngine'] = "${workflow.containerEngine}"
kkonganti@101 165 globalparams['workflow_runName'] = "${workflow.runName}"
kkonganti@101 166 globalparams['workflow_sessionId'] = "${workflow.sessionId}"
kkonganti@101 167 globalparams['workflow_profile'] = "${workflow.profile}"
kkonganti@101 168 globalparams['workflow_start'] = "${workflow.start}"
kkonganti@101 169 globalparams['workflow_commandLine'] = "${workflow.commandLine}"
kkonganti@101 170 return """${dashedLine()}
kkonganti@101 171 Summary of the current workflow (${fgcolors.magenta}${params.pipeline}${fgcolors.reset}) parameters
kkonganti@101 172 ${dashedLine()}
kkonganti@101 173 ${addPadding(localparams)}
kkonganti@101 174 ${dashedLine()}
kkonganti@101 175 ${fgcolors.cyan}N E X T F L O W${fgcolors.reset} - ${fgcolors.magenta}${params.cfsanpipename}${fgcolors.reset} - Runtime metadata
kkonganti@101 176 ${dashedLine()}
kkonganti@101 177 ${addPadding(globalparams)}
kkonganti@101 178 ${dashedLine()}""".stripIndent()
kkonganti@101 179 }
kkonganti@101 180 return 1
kkonganti@101 181 }
kkonganti@101 182
kkonganti@101 183 // Method to display
kkonganti@101 184 // Return dashed line either '-'
kkonganti@101 185 // type or '=' type
kkonganti@101 186 def dashedLine(Map defaults = [:]) {
kkonganti@101 187
kkonganti@101 188 Map fgcolors = getANSIColors()
kkonganti@101 189 def line = [color: 'white', type: '-']
kkonganti@101 190
kkonganti@101 191 if (!defaults.isEmpty()) {
kkonganti@101 192 line.putAll(defaults)
kkonganti@101 193 }
kkonganti@101 194
kkonganti@101 195 return fgcolors."${line.color}" +
kkonganti@101 196 "${line.type}".multiply(params.linewidth) +
kkonganti@101 197 fgcolors.reset
kkonganti@101 198 }
kkonganti@101 199
kkonganti@101 200 // Return slurped keys parsed from JSON
kkonganti@101 201 def slurpJson(file) {
kkonganti@101 202 def slurped = null
kkonganti@101 203 def jsonInst = new JsonSlurper()
kkonganti@101 204
kkonganti@101 205 try {
kkonganti@101 206 slurped = jsonInst.parse(new File ("${file}"))
kkonganti@101 207 }
kkonganti@101 208 catch (Exception e) {
kkonganti@101 209 log.error 'Please check your JSON schema. Invalid JSON file: ' + file
kkonganti@101 210 }
kkonganti@101 211
kkonganti@101 212 // Declare globals for the nanofactory
kkonganti@101 213 // workflow.
kkonganti@101 214 return [keys: slurped.keySet().toList(), cparams: slurped]
kkonganti@101 215 }
kkonganti@101 216
kkonganti@101 217 // Default help text in a map if the entry point
kkonganti@101 218 // to a pipeline is FASTQ files.
kkonganti@101 219 def fastqEntryPointHelp() {
kkonganti@101 220
kkonganti@101 221 Map helptext = [:]
kkonganti@101 222 Map fgcolors = getANSIColors()
kkonganti@101 223
kkonganti@101 224 helptext['Workflow'] = "${fgcolors.magenta}${params.pipeline}${fgcolors.reset}"
kkonganti@101 225 helptext['Author'] = "${fgcolors.cyan}${params.workflow_built_by}${fgcolors.reset}"
kkonganti@101 226 helptext['Version'] = "${fgcolors.green}${params.workflow_version}${fgcolors.reset}\n"
kkonganti@101 227 helptext['Usage'] = "cpipes --pipeline ${params.pipeline} [options]\n"
kkonganti@101 228 helptext['Required'] = ""
kkonganti@101 229 helptext['--input'] = "Absolute path to directory containing FASTQ files. " +
kkonganti@101 230 "The directory should contain only FASTQ files as all the " +
kkonganti@101 231 "files within the mentioned directory will be read. " +
kkonganti@101 232 "Ex: --input /path/to/fastq_pass"
kkonganti@101 233 helptext['--output'] = "Absolute path to directory where all the pipeline " +
kkonganti@101 234 "outputs should be stored. Ex: --output /path/to/output"
kkonganti@101 235 helptext['Other options'] = ""
kkonganti@101 236 helptext['--metadata'] = "Absolute path to metadata CSV file containing five " +
kkonganti@101 237 "mandatory columns: sample,fq1,fq2,strandedness,single_end. The fq1 and fq2 " +
kkonganti@101 238 "columns contain absolute paths to the FASTQ files. This option can be used in place " +
kkonganti@101 239 "of --input option. This is rare. Ex: --metadata samplesheet.csv"
kkonganti@101 240 helptext['--fq_suffix'] = "The suffix of FASTQ files (Unpaired reads or R1 reads or Long reads) if " +
kkonganti@101 241 "an input directory is mentioned via --input option. Default: ${params.fq_suffix}"
kkonganti@101 242 helptext['--fq2_suffix'] = "The suffix of FASTQ files (Paired-end reads or R2 reads) if an input directory is mentioned via " +
kkonganti@101 243 "--input option. Default: ${params.fq2_suffix}"
kkonganti@101 244 helptext['--fq_filter_by_len'] = "Remove FASTQ reads that are less than this many bases. " +
kkonganti@101 245 "Default: ${params.fq_filter_by_len}"
kkonganti@101 246 helptext['--fq_strandedness'] = "The strandedness of the sequencing run. This is mostly needed " +
kkonganti@101 247 "if your sequencing run is RNA-SEQ. For most of the other runs, it is probably safe to use " +
kkonganti@101 248 "unstranded for the option. Default: ${params.fq_strandedness}"
kkonganti@101 249 helptext['--fq_single_end'] = "SINGLE-END information will be auto-detected but this option forces " +
kkonganti@101 250 "PAIRED-END FASTQ files to be treated as SINGLE-END so only read 1 information is included in " +
kkonganti@101 251 "auto-generated samplesheet. Default: ${params.fq_single_end}"
kkonganti@101 252 helptext['--fq_filename_delim'] = "Delimiter by which the file name is split to obtain sample name. " +
kkonganti@101 253 "Default: ${params.fq_filename_delim}"
kkonganti@101 254 helptext['--fq_filename_delim_idx'] = "After splitting FASTQ file name by using the --fq_filename_delim option," +
kkonganti@101 255 " all elements before this index (1-based) will be joined to create final sample name." +
kkonganti@101 256 " Default: ${params.fq_filename_delim_idx}"
kkonganti@101 257
kkonganti@101 258 return helptext
kkonganti@101 259 }
kkonganti@101 260
kkonganti@101 261 // Wrap help text with the following options
kkonganti@101 262 def wrapUpHelp() {
kkonganti@101 263
kkonganti@101 264 return [
kkonganti@101 265 'Help options' : "",
kkonganti@101 266 '--help': "Display this message.\n",
kkonganti@101 267 'help': true,
kkonganti@101 268 'nocapitalize': true
kkonganti@101 269 ]
kkonganti@101 270 }
kkonganti@101 271
kkonganti@101 272 // Method to send email on workflow complete.
kkonganti@101 273 def sendMail() {
kkonganti@101 274
kkonganti@101 275 if (params.user_email == null) {
kkonganti@101 276 return 1
kkonganti@101 277 }
kkonganti@101 278
kkonganti@101 279 def pad = (params.pad) ?: 30
kkonganti@101 280 def contact_emails = [
kkonganti@101 281 stakeholder: (params.workflow_blueprint_by ?: 'Not defined'),
kkonganti@101 282 author: (params.workflow_built_by ?: 'Not defined')
kkonganti@101 283 ]
kkonganti@101 284 def msg = """
kkonganti@101 285 ${pipelineBanner()}
kkonganti@101 286 ${summaryOfParams()}
kkonganti@101 287 ${params.cfsanpipename} - ${params.pipeline}
kkonganti@101 288 ${dashedLine()}
kkonganti@101 289 Please check the following directory for N E X T F L O W
kkonganti@101 290 reports. You can view the HTML files directly by double clicking
kkonganti@101 291 them on your workstation.
kkonganti@101 292 ${dashedLine()}
kkonganti@101 293 ${params.tracereportsdir}
kkonganti@101 294 ${dashedLine()}
kkonganti@101 295 Please send any bug reports to CFSAN Dev Team or the author or
kkonganti@101 296 the stakeholder of the current pipeline.
kkonganti@101 297 ${dashedLine()}
kkonganti@101 298 Error messages (if any)
kkonganti@101 299 ${dashedLine()}
kkonganti@101 300 ${workflow.errorMessage}
kkonganti@101 301 ${workflow.errorReport}
kkonganti@101 302 ${dashedLine()}
kkonganti@101 303 Contact emails
kkonganti@101 304 ${dashedLine()}
kkonganti@101 305 ${addPadding(contact_emails)}
kkonganti@101 306 ${dashedLine()}
kkonganti@101 307 Thank you for using ${params.cfsanpipename} - ${params.pipeline}!
kkonganti@101 308 ${dashedLine()}
kkonganti@101 309 """.stripIndent()
kkonganti@101 310
kkonganti@101 311 def mail_cmd = [
kkonganti@101 312 'sendmail',
kkonganti@101 313 '-f', 'cfsan-hpc-noreply@fda.hhs.gov',
kkonganti@101 314 '-F', 'cfsan-hpc-noreply',
kkonganti@101 315 '-t', "${params.user_email}"
kkonganti@101 316 ]
kkonganti@101 317
kkonganti@101 318 def email_subject = "${params.cfsanpipename} - ${params.pipeline}"
kkonganti@101 319 Map fgcolors = getANSIColors()
kkonganti@101 320
kkonganti@101 321 if (workflow.success) {
kkonganti@101 322 email_subject += ' completed successfully!'
kkonganti@101 323 }
kkonganti@101 324 else if (!workflow.success) {
kkonganti@101 325 email_subject += ' has failed!'
kkonganti@101 326 }
kkonganti@101 327
kkonganti@101 328 try {
kkonganti@101 329 ['env', 'bash'].execute() << """${mail_cmd.join(' ')}
kkonganti@101 330 Subject: ${email_subject}
kkonganti@101 331 Mime-Version: 1.0
kkonganti@101 332 Content-Type: text/html
kkonganti@101 333 <pre>
kkonganti@101 334 ${msg.replaceAll(/\x1b\[[0-9;]*m/, '')}
kkonganti@101 335 </pre>
kkonganti@101 336 """.stripIndent()
kkonganti@101 337 } catch (all) {
kkonganti@101 338 def warning_msg = "${fgcolors.yellow}${params.cfsanpipename} - ${params.pipeline} - WARNING"
kkonganti@101 339 .padRight(pad) + ':'
kkonganti@101 340 log.info """
kkonganti@101 341 ${dashedLine()}
kkonganti@101 342 ${warning_msg}
kkonganti@101 343 ${dashedLine()}
kkonganti@101 344 Could not send mail with the sendmail command!
kkonganti@101 345 ${dashedLine()}
kkonganti@101 346 """.stripIndent()
kkonganti@101 347 }
kkonganti@101 348 return 1
kkonganti@101 349 }
kkonganti@101 350
kkonganti@101 351 // Set ANSI colors for any and all
kkonganti@101 352 // STDOUT or STDERR
kkonganti@101 353 def getANSIColors() {
kkonganti@101 354
kkonganti@101 355 Map fgcolors = [:]
kkonganti@101 356
kkonganti@101 357 fgcolors['reset'] = "\033[0m"
kkonganti@101 358 fgcolors['black'] = "\033[0;30m"
kkonganti@101 359 fgcolors['red'] = "\033[0;31m"
kkonganti@101 360 fgcolors['green'] = "\033[0;32m"
kkonganti@101 361 fgcolors['yellow'] = "\033[0;33m"
kkonganti@101 362 fgcolors['blue'] = "\033[0;34m"
kkonganti@101 363 fgcolors['magenta'] = "\033[0;35m"
kkonganti@101 364 fgcolors['cyan'] = "\033[0;36m"
kkonganti@101 365 fgcolors['white'] = "\033[0;37m"
kkonganti@101 366
kkonganti@101 367 return fgcolors
kkonganti@101 368 }