Skip to content

BEDshift Module

class Bedshift(bedfile_path, chrom_sizes=None)

The bedshift object with methods to perturb regions.

Read in a .bed file to a list of lists.

Args: bedfile_path (str): The path to the BED file. chrom_sizes (str): The path to the chrom.sizes file.

  • bedfile_path
  • chrom_lens
  • bed
  • original_num_regions
  • original_bed

def reset_bed()

Reset the stored bedfile to the state before perturbations.

def pick_random_chroms(n)

Utility function to pick a random chromosome.

Args: n (str): The number of random chromosomes to pick.

Returns: zip: Tuples of (chrom_str, chrom_len) containing chromosome number and length.

def add(addrate, addmean, addstdev, valid_bed=None)

Add regions.

Args: addrate (float): The rate to add regions. addmean (float): The mean length of added regions. addstdev (float): The standard deviation of the length of added regions. valid_bed (str): The file with valid regions where new regions can be added.

Returns: int: The number of regions added.

def add_from_file(fp, addrate)

Add regions from another bedfile to this perturbed bedfile.

Args: fp (str): The filepath to the other bedfile. addrate (float): The rate to add regions.

Returns: int: The number of regions added.

def shift(shiftrate, shiftmean, shiftstdev, shift_rows=[])

Shift regions.

Args: shiftrate (float): The rate to shift regions (both the start and end are shifted by the same amount). shiftmean (float): The mean shift distance. shiftstdev (float): The standard deviation of the shift distance. shift_rows (list): Specific rows to shift.

Returns: int: The number of regions shifted.

def shift_from_file(fp, shiftrate, shiftmean, shiftstdev)

Shift regions that overlap the specified file's regions.

Args: fp (str): The file on which to find overlaps. shiftrate (float): The rate to shift regions (both the start and end are shifted by the same amount). shiftmean (float): The mean shift distance. shiftstdev (float): The standard deviation of the shift distance.

Returns: int: The number of regions shifted.

def cut(cutrate)

Cut regions to create two new regions.

Args: cutrate (float): The rate to cut regions into two separate regions.

Returns: int: The number of regions cut.

def merge(mergerate)

Merge two regions into one new region.

Args: mergerate (float): The rate to merge two regions into one.

Returns: int: Number of regions merged.

def drop(droprate)

Drop regions.

Args: droprate (float): The rate to drop/remove regions.

Returns: int: The number of rows dropped.

def drop_from_file(fp, droprate)

Drop regions that overlap between the reference bedfile and the provided bedfile.

Args: fp (str): The filepath to the other bedfile containing regions to be dropped. droprate (float): The rate to drop regions.

Returns: int: The number of regions dropped.

def set_seed(seednum)

Set the random seed for reproducible perturbations.

Args: seednum (int): The seed value.

Raises: ValueError: If seednum cannot be converted to an integer.

def all_perturbations(addrate=0.0, addmean=320.0, addstdev=30.0, addfile=None, valid_regions=None, shiftrate=0.0, shiftmean=0.0, shiftstdev=150.0, shiftfile=None, cutrate=0.0, mergerate=0.0, droprate=0.0, dropfile=None, yaml=None, seed=None)

Perform all five perturbations in the order of shift, add, cut, merge, drop.

Args: addrate (float): The rate (as a proportion of the total number of regions) to add regions. addmean (float): The mean length of added regions. addstdev (float): The standard deviation of the length of added regions. addfile (str): The file containing regions to be added. valid_regions (str): The file containing regions where new regions can be added. shiftrate (float): The rate to shift regions (both the start and end are shifted by the same amount). shiftmean (float): The mean shift distance. shiftstdev (float): The standard deviation of the shift distance. shiftfile (str): The file containing regions to be shifted. cutrate (float): The rate to cut regions into two separate regions. mergerate (float): The rate to merge two regions into one. droprate (float): The rate to drop/remove regions. dropfile (str): The file containing regions to be dropped. yaml (str): The yaml_config filepath. seed (int): A seed for allowing reproducible perturbations.

Returns: int: The number of total regions perturbed.

def to_bed(outfile_name)

Write regions to a BED file.

Args: outfile_name (str): The name of the output BED file.

def read_bed(bedfile_path)

Read a BED file into a list of lists.

Args: bedfile_path (str): The path to the BED file.

Returns: list: A list of lists, each containing [chrom, start, end, mod_flag].