annotate 0.3.0/lib/routines.nf @ 92:295c2597a475

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