Skip to content

Commit

Permalink
Implement process labels and configuration #623
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Apr 7, 2018
1 parent 36a8ae0 commit d9a05d8
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 68 deletions.
36 changes: 27 additions & 9 deletions src/main/groovy/nextflow/Session.groovy
Expand Up @@ -716,21 +716,39 @@ class Session implements ISession {
// verifies that all process config names have a match with a defined process
def keys = (config.process as Map).keySet()
for(String key : keys) {
if( !key.startsWith('$') )
continue
def name = key.substring(1)
if( !processNames.contains(name) ) {
def suggestion = processNames.closest(name)
def message = "The config file defines settings for an unknown process: $name"
if( suggestion )
message += " -- Did you mean: ${suggestion.first()}?"
result << message
String name = null
if( key.startsWith('$') ) {
name = key.substring(1)
}
else if( key.startsWith('withName:') ) {
name = key.substring('withName:'.length())
}
if( name && !isValidProcessName(name, processNames, result) )
break
}

return result
}

/**
* Check that the specified name belongs to the list of existing process names
*
* @param name The process name to check
* @param processNames The list of processes declared in the workflow script
* @param errorMessage A list of strings used to return the error message to the caller
* @return {@code true} if the name specified belongs to the list of process names or {@code false} otherwise
*/
protected boolean isValidProcessName(String name, Collection<String> processNames, List<String> errorMessage) {
if( !processNames.contains(name) ) {
def suggestion = processNames.closest(name)
def message = "The config file defines settings for an unknown process: $name"
if( suggestion )
message += " -- Did you mean: ${suggestion.first()}?"
errorMessage << message.toString()
return false
}
return true
}
/**
* Register a shutdown hook to close services when the session terminates
* @param Closure
Expand Down
4 changes: 3 additions & 1 deletion src/main/groovy/nextflow/config/ConfigParser.groovy
Expand Up @@ -156,8 +156,10 @@ class ConfigParser {
// set the required base script
def config = new CompilerConfiguration()
config.scriptBaseClass = ConfigBase.class.name
def params = [:]
if( renderClosureAsString )
config.addCompilationCustomizers(new ASTTransformationCustomizer(ConfigTransform))
params.put('renderClosureAsString', true)
config.addCompilationCustomizers(new ASTTransformationCustomizer(params, ConfigTransform))
grengine = new Grengine(config)
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/groovy/nextflow/config/ConfigTransform.groovy
Expand Up @@ -34,4 +34,11 @@ import org.codehaus.groovy.transform.GroovyASTTransformationClass
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
@GroovyASTTransformationClass(classes = [ConfigTransformImpl])
@interface ConfigTransform { }
@interface ConfigTransform {
/**
* hack to pass a parameter in the {@link ConfigTransformImpl} class -- do not remove
*
* See {@link ConfigTransformImpl#renderClosureAsString}
*/
boolean renderClosureAsString()
}
52 changes: 48 additions & 4 deletions src/main/groovy/nextflow/config/ConfigTransformImpl.groovy
Expand Up @@ -23,6 +23,7 @@ package nextflow.config
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.AnnotationNode
import org.codehaus.groovy.ast.ClassCodeVisitorSupport
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.expr.ArgumentListExpression
Expand All @@ -33,6 +34,7 @@ import org.codehaus.groovy.ast.expr.ConstructorCallExpression
import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.ast.expr.MapEntryExpression
import org.codehaus.groovy.ast.expr.MapExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
Expand All @@ -48,23 +50,65 @@ import org.codehaus.groovy.transform.GroovyASTTransformation
@GroovyASTTransformation(phase = CompilePhase.CONVERSION)
class ConfigTransformImpl implements ASTTransformation {

private boolean renderClosureAsString

@Override
void visit(ASTNode[] astNodes, SourceUnit unit) {
createVisitor(unit).visitClass((ClassNode)astNodes[1])
final annot = (AnnotationNode)astNodes[0]
final clazz = (ClassNode)astNodes[1]
// the following line is mostly an hack to pass a parameter to this xform instance
this.renderClosureAsString = annot.getMember('renderClosureAsString') != null
createVisitor(unit).visitClass(clazz)
}

protected ClassCodeVisitorSupport createVisitor(SourceUnit unit) {
new MyClassCodeVisitorSupport(unit: unit)
return (renderClosureAsString
? new RetainClosureSourceCodeVisitorSupport(unit: unit)
: new DefaultConfigCodeVisitor(unit: unit) )
}


/**
* Nextflow config file visitor support class. Apply default transformations to
* the config object to implements config syntax sugars
*/
@CompileStatic
static class MyClassCodeVisitorSupport extends ClassCodeVisitorSupport {
static class DefaultConfigCodeVisitor extends ClassCodeVisitorSupport {

protected SourceUnit unit

@Override
protected SourceUnit getSourceUnit() { unit }

@Override
void visitExpressionStatement(ExpressionStatement stm) {
if( stm.expression instanceof MethodCallExpression && stm.getStatementLabel() == 'withLabel' ) {
replaceMethodName( stm.expression as MethodCallExpression, 'withLabel' )
}
else if( stm.expression instanceof MethodCallExpression && stm.getStatementLabel() == 'withName' ) {
replaceMethodName( stm.expression as MethodCallExpression, 'withName' )
}
super.visitExpressionStatement(stm)
}

/**
* Replace the name of the invoked method pre-pending with the specified string
*
* @param call A object representing a method call expression
* @param prefix A string to prepend to the name of the invoked method
*/
protected void replaceMethodName(MethodCallExpression call, String prefix) {
call.setMethod( new ConstantExpression(prefix + ":" + call.method.text) )
}

}

/**
* This visitor is only used to render the closure source code
* when is required to visualise the nextflow config content
*/
@CompileStatic
static class RetainClosureSourceCodeVisitorSupport extends DefaultConfigCodeVisitor {

/**
* Visit expression statements replacing a binary assignment such as:
*
Expand Down
56 changes: 40 additions & 16 deletions src/main/groovy/nextflow/processor/ProcessConfig.groovy
Expand Up @@ -19,7 +19,8 @@
*/

package nextflow.processor
import static nextflow.util.CacheHelper.HashMode

import static nextflow.util.CacheHelper.*

import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
Expand All @@ -42,7 +43,6 @@ import nextflow.script.StdInParam
import nextflow.script.StdOutParam
import nextflow.script.ValueInParam
import nextflow.script.ValueOutParam
import nextflow.util.ReadOnlyMap
/**
* Holds the process configuration properties
*
Expand Down Expand Up @@ -71,6 +71,7 @@ class ProcessConfig implements Map<String,Object> {
'ext',
'instanceType',
'queue',
'label',
'maxErrors',
'maxForks',
'maxRetries',
Expand Down Expand Up @@ -99,10 +100,14 @@ class ProcessConfig implements Map<String,Object> {
@Delegate
protected final Map<String,Object> configProperties

private final BaseScript ownerScript
private BaseScript ownerScript

private boolean throwExceptionOnMissingProperty

private boolean allowMultipleModules

private int moduleInvocationCount

private inputs = new InputsList()

private outputs = new OutputsList()
Expand All @@ -114,16 +119,11 @@ class ProcessConfig implements Map<String,Object> {
* @param important The values specified by this map won't be overridden by attributes
* having the same name defined at the task level
*/
ProcessConfig( BaseScript script, Map important = null ) {
ProcessConfig( BaseScript script ) {

ownerScript = script

// parse the attribute as List before adding it to the read-only list
if( important?.containsKey('module') ) {
important.module = parseModule(important.module)
}

configProperties = important ? new ReadOnlyMap(important) : new LinkedHashMap()
configProperties = new LinkedHashMap()
configProperties.echo = false
configProperties.cacheable = true
configProperties.shell = BashWrapperBuilder.BASH
Expand Down Expand Up @@ -153,9 +153,17 @@ class ProcessConfig implements Map<String,Object> {
return value != null && value.toString().toLowerCase() in BOOL_YES
}

/**
* Enable special behavior to allow the configuration object
* invoking directive method from the process DSL
*
* @param value {@code true} enable capture mode, {@code false} otherwise
* @return The object itself
*/
@PackageScope
ProcessConfig throwExceptionOnMissingProperty( boolean value ) {
ProcessConfig enterCaptureMode(boolean value ) {
this.throwExceptionOnMissingProperty = value
this.allowMultipleModules = value
return this
}

Expand Down Expand Up @@ -326,11 +334,11 @@ class ProcessConfig implements Map<String,Object> {
* Defines a special *dummy* input parameter, when no inputs are
* provided by the user for the current task
*/
def void fakeInput() {
void fakeInput() {
new DefaultInParam(this)
}

def void fakeOutput() {
void fakeOutput() {
new DefaultOutParam(this)
}

Expand All @@ -353,18 +361,34 @@ class ProcessConfig implements Map<String,Object> {
configProperties.cache == 'deep' ? HashMode.DEEP : HashMode.STANDARD
}

ProcessConfig label(String lbl) {
if( !lbl ) return this
def allLabels = configProperties.get('label')
if( !allLabels ) {
allLabels = []
configProperties.put('label', allLabels)
}
allLabels << lbl
return this
}

List<String> getLabels() {
(List<String>) configProperties.get('label') ?: Collections.emptyList()
}

ProcessConfig module( moduleName ) {
// when no name is provided, just exit
if( !moduleName )
return this

def list = parseModule(moduleName, configProperties.module)
configProperties.put('module', list)
final current = allowMultipleModules && moduleInvocationCount++ ? configProperties.module : null
final result = parseModule(moduleName, current)
configProperties.put('module', result)
return this
}

@PackageScope
static List parseModule( value, current = null) {
static List parseModule( value, current = null ) {

// if no modules list exist create it
List copy
Expand Down

0 comments on commit d9a05d8

Please sign in to comment.