Esempio n. 1
0
def _build_samples_from_dir(data_fp, filename_fmt, excluded):
    """
    Build a list of samples from a data filepath and filename format.

    :param data_fp: a Path to data files
    :param filename_fmt: a string giving wildcards for {sample} and (optionally)
        {rp}, for read pair (e.g. R1 or R2).
    :param exclude: a list of sample names to exclude
    :returns: A dictionary of samples, with sample names as keys
    """
    files = list(listfiles(str(data_fp / filename_fmt)))
    Samples = {
        t[1]['sample']: {}
        for t in files if t[1]['sample'] not in excluded
    }
    for f in files:
        fpath = f[0]
        wcards = f[1]
        if wcards['sample'] in excluded:
            continue
        rp = wcards['rp'] if 'rp' in wcards.keys() else False
        if rp:
            Samples[wcards['sample']][rp] = fpath
            Samples[wcards['sample']]['paired'] = True
        else:
            Samples[wcards['sample']]['file'] = fpath
            Samples[wcards['sample']]['paired'] = False
    return Samples
Esempio n. 2
0
def find_samples(data_fp, filename_fmt):
    """
    Build a list of samples from a data filepath and filename format.
    :param data_fp: a Path to data files
    :param filename_fmt: a string giving wildcards for {sample} and (optionally)
        {rp}, for read pair (e.g. R1 or R2).
    :returns: A dictionary of samples, with sample names as keys:
       Samples = {
         'sample1': {
           '1': 'path/to/sample1_R1.fastq.gz',
           '2': 'path/to/sample1_R2.fastq.gz', #optional
         }, ...
       }
    """
    files = list(listfiles(str(data_fp / filename_fmt)))
    Samples = {t[1]['sample']: {} for t in files}
    for f in files:
        fpath = f[0]
        wcards = f[1]
        rp = wcards.get('rp')
        if rp:
            if not rp in ['1', '2']:
                raise ValueError(
                    "'{rp}' should capture just '1' or '2' in filename, nothing else"
                )
            Samples[wcards['sample']][rp] = fpath
        else:
            Samples[wcards['sample']]['1'] = fpath
    return Samples
Esempio n. 3
0
 def _locks(self, type):
     return (
         f
         for f, _ in listfiles(
             os.path.join(self._lockdir, "{{n,[0-9]+}}.{}.lock".format(type))
         )
         if not os.path.isdir(f)
     )
Esempio n. 4
0
def clear_generated_files(include_targets=False):
    cleared_files = 0
    for pattern in all_intermediate_files:
        from snakemake.utils import listfiles

        for filename, wildcards in listfiles(pattern):
            if os.path.isfile(filename):
                os.unlink(filename)
                cleared_files += 1
    else:
        if cleared_files > 0:
            print('{} intermediate file(s) were removed.'.format(cleared_files))

    for dir in INTERMEDIATE_DIRS:
        if os.path.islink(dir):
            realpath = os.readlink(dir)
            print('Cleaning a symbolic link:', dir)
            shutil.rmtree(realpath, ignore_errors=True)
            os.unlink(dir)
        elif os.path.isdir(dir):
            print('Cleaning an intermediate directory:', dir)
            shutil.rmtree(dir, ignore_errors=True)

    if not include_targets:
        return

    from itertools import groupby

    files_safe_to_delete = {'.AppleDouble', '.DS_Store'}

    for dirname, files in groupby(sorted(TARGETS), lambda x: x.split(os.sep)[0]):
        for f in files:
            if os.path.exists(f):
                os.unlink(f)

        if os.path.isdir(dirname) and not (
                set(os.listdir(dirname)) - files_safe_to_delete):
            shutil.rmtree(dirname)
Esempio n. 5
0
 def expand_dynamic(self, pattern):
     """ Expand dynamic files. """
     return list(
         listfiles(pattern,
                   restriction=self.wildcards,
                   omit_value=DYNAMIC_FILL))
Esempio n. 6
0
 def expand_dynamic(pattern, restriction=None, omit_value=None):
     """ Expand dynamic files. """
     return list(
         listfiles(pattern, restriction=restriction, omit_value=omit_value))
Esempio n. 7
0
 def expand_dynamic(self, pattern):
     """ Expand dynamic files. """
     return list(listfiles(pattern,
                           restriction=self.wildcards,
                           omit_value=DYNAMIC_FILL))
Esempio n. 8
0
 def expand_dynamic(pattern, restriction=None, omit_value=None):
     """ Expand dynamic files. """
     return list(listfiles(pattern,
                           restriction=restriction,
                           omit_value=omit_value))
Esempio n. 9
0
 def _locks(self, type):
     return (f for f, _ in listfiles(
         os.path.join(self._lockdir, "{{n,[0-9]+}}.{}.lock".format(type)))
             if not os.path.isdir(f))