Helper script that creates a set of the stain names created by stripping off parts of the filename.
    Hopefully handles different naming conventions (e.g. 2015-SEQ-001_S1_L001_R1_001.fastq(.gz),
    2015-SEQ-001_R1_001.fastq.gz, 2015-SEQ-001_R1.fastq.gz, 2015-SEQ-001_1.fastq.gz, and 2015-SEQ-001_1.fastq.gz
    all become 2015-SEQ-001)
    :param filelist: List of files to parse
    :param extension: the file extension to use. Default value is 'fastq
    :param returndict: type BOOL: Option to return a dictionary of file name: fastq files associated with that name
    rather than a set of the file names

def filer(filelist, extension='fastq', returndict=False):
    """
    Helper script that creates a set of the stain names created by stripping off parts of the filename.
    Hopefully handles different naming conventions (e.g. 2015-SEQ-001_S1_L001_R1_001.fastq(.gz),
    2015-SEQ-001_R1_001.fastq.gz, 2015-SEQ-001_R1.fastq.gz, 2015-SEQ-001_1.fastq.gz, and 2015-SEQ-001_1.fastq.gz
    all become 2015-SEQ-001)
    :param filelist: List of files to parse
    :param extension: the file extension to use. Default value is 'fastq
    :param returndict: type BOOL: Option to return a dictionary of file name: fastq files associated with that name
    rather than a set of the file names
    """
    # Initialise the variables
    fileset = set()
    filedict = dict()
    for seqfile in filelist:
        # Search for the conventional motifs present following strain names
        # _S\d+_L001_R\d_001.fastq(.gz) is a typical unprocessed Illumina fastq file
        if re.search("_S\\d+_L001", seqfile):
            file_name = re.split("_S\\d+_L001", seqfile)[0]
        # Files with _R\d_001.fastq(.gz) are created in the SPAdes assembly pipeline
        elif re.search("_R\\d_001", seqfile):
            file_name = re.split("_R\\d_001", seqfile)[0]
        # _R\d.fastq(.gz) represents a simple naming scheme for paired end reads
        elif re.search("R\\d.{}".format(extension), seqfile):
            file_name = re.split("_R\\d.{}".format(extension), seqfile)[0]
        # _\d.fastq is always possible
        elif re.search("[-_]\\d.{}".format(extension), seqfile):
            file_name = re.split("[-_]\\d.{}".format(extension), seqfile)[0]
        # .fastq is the last option
        else:
            file_name = re.split(".{}".format(extension), seqfile)[0]
        # Add the calculated file name to the set
        fileset.add(file_name)
        # Populate the dictionary with the file name: seq file pair
        try:
            filedict[file_name].append(seqfile)
        except KeyError:
            filedict[file_name] = [seqfile]
    # Return the appropriate variable
    if not returndict:
        return fileset
    else:
        return filedict