annotate 0.5.0/lib/routines.nf @ 1:365849f031fd

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