annotate 0.5.0/lib/routines.nf @ 0:97cd2f532efe

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