All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nextflow.file.FilePatternSplitter.groovy Maven / Gradle / Ivy

Go to download

A DSL modelled around the UNIX pipe concept, that simplifies writing parallel and scalable pipelines in a portable manner

There is a newer version: 18.12.0-edge
Show newest version
/*
 * Copyright (c) 2013-2017, Centre for Genomic Regulation (CRG).
 * Copyright (c) 2013-2017, Paolo Di Tommaso and the respective authors.
 *
 *   This file is part of 'Nextflow'.
 *
 *   Nextflow is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Nextflow is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Nextflow.  If not, see .
 */

package nextflow.file
import java.nio.file.Path
import java.util.regex.Pattern

import groovy.transform.CompileStatic
/**
 * Parse a file path to isolate the parent, file-name and whenever it contains a glob|regex pattern
 *
 * @author Paolo Di Tommaso 
 */
@CompileStatic
class FilePatternSplitter {

    static final public FilePatternSplitter GLOB = glob()

    static enum Syntax { GLOB, REGEX }

    static final public Pattern GLOB_CURLY_BRACKETS = Pattern.compile(/(.*)(\{.*,.*\})(.*)/)

    static final public Pattern GLOB_SQUARE_BRACKETS = Pattern.compile(/(.*)(\[.+\])(.*)/)

    static final private char BACK_SLASH = '\\' as char

    static final private String GLOB_CHARS = '*?[]{}'

    static final private String REGEX_CHARS = '.^$+{}[]|()'

    private boolean pattern

    private final Syntax syntax

    private String scheme

    private String parent

    private String fileName

    boolean isPattern() { pattern }

    String getFileName() { fileName }

    String getParent() { parent }

    String getScheme() { scheme }

    static FilePatternSplitter glob() { new FilePatternSplitter(Syntax.GLOB) }

    static FilePatternSplitter regex() { new FilePatternSplitter(Syntax.REGEX) }

    FilePatternSplitter( Syntax syntax ) {
        this.syntax = syntax
    }

    private String metaChars() {
        syntax == Syntax.GLOB ? GLOB_CHARS : REGEX_CHARS
    }

    private boolean containsMetaChars(String str) {
        final meta = metaChars()

        for( int i=0; i= 0 ) {
                if( filePath[i] == '/' )  {
                    p = i
                    break
                }
            }
        }

        if( p == -1 ) {
            parent = './'
            fileName = filePath
            pattern = found && pairedBrackets(norm)
        }
        else {
            parent = strip(filePath.substring(0,p+1))
            fileName = filePath.substring(p+1)
            pattern = found && pairedBrackets(norm)
        }

        return this
    }


    private boolean pairedBrackets(String str) {
        if( syntax == Syntax.REGEX )
            return true

        if( str.contains('{') || str.contains('}') )
            return GLOB_CURLY_BRACKETS.matcher(str).matches()

        if( str.contains('[') || str.contains(']') )
            return GLOB_SQUARE_BRACKETS.matcher(str).matches()

        return true
    }

    protected String replaceMetaChars( String str, char marker = 0x0 ) {
        // create a version replacing escape meta chars with an 0x0
        final meta = metaChars()
        final result = new StringBuilder()
        int i=0;
        while( i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy