<commit_before>import os

import pybedtools

DIRECTIONS = 'upstream', 'downstream'

def construct_homer_command(foreground_filename, background_filename,
                            homer_flags, findMotifsGenome,
                            out_dir_prefix=None):
    if out_dir_prefix is None:
        out_dir = foreground_filename.replace('.bed', '')
    else:
        out_dir = '{}/{}'.format(
            out_dir_prefix,
            os.path.basename(foreground_filename).replace('.bed', ''))
    command = '{} {} hg19 {} -bg {} {}'.format(
        findMotifsGenome, foreground_filename, out_dir, background_filename,
        homer_flags)
    return command

def unique_regions(bed):
    return pybedtools.BedTool(list(set(x for x in bed)))

def get_flanking_intron(bed, direction, genome, nt):
    if direction == 'downstream':
        intron = bed.flank(l=0, r=nt, s=True, genome=genome)
    elif direction == 'upstream':
        intron = bed.flank(l=nt, r=0, s=True, genome=genome)
    else:
        raise ValueError('Only "downstream" and "upstream" are accepted '
                         'direction values, not "{}"'.format(direction))<commit_msg>make out dir optional, add force option, raise ValueError(is this the right one???) if folder exists and no forcing<commit_after>import os

import pybedtools

DIRECTIONS = 'upstream', 'downstream'

def construct_homer_command(foreground_filename, background_filename,
                            homer_flags, findMotifsGenome,
                            out_dir=None, force=False):
    if out_dir is None:
        out_dir = foreground_filename.replace('.bed', '')
    if force or not os.path.exists(out_dir):
        command = '{} {} hg19 {} -bg {} {}'.format(
            findMotifsGenome, foreground_filename, out_dir, background_filename,
            homer_flags)
        return command
    else:
        raise ValueError('{} already exists, not creating command. To create '
                         'the command anyway, use force=True')


def unique_regions(bed):
    return pybedtools.BedTool(list(set(x for x in bed)))


def get_flanking_intron(bed, direction, genome, nt):
    if direction == 'downstream':
        intron = bed.flank(l=0, r=nt, s=True, genome=genome)
    elif direction == 'upstream':
        intron = bed.flank(l=nt, r=0, s=True, genome=genome)
    else:
        raise ValueError('Only "downstream" and "upstream" are accepted '
                         'direction values, not "{}"'.format(direction))
{'fraction_non_alphanumeric': 0.11478050745066452, 'fraction_numerical': 0.003221908981071285, 'mean_word_length': 2.7750759878419453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '306436', 'n_tokens_mistral': 760, 'n_tokens_neox': 726, 'n_words': 185}