#!/usr/bin/python import numpy #INPUT #list of chromosome lengths - ordered #shift, an integer #chrom and pos for the locus that should be shifted, chrom is a str of the form '1' or 'Chr1' or 'chr1' and pos is an integer #returns new chrom and pos def circular_rand(chrom, pos, shift, chr_lengths): genome_size = sum(chr_lengths) genome_cumsum = list(numpy.cumsum(chr_lengths)) #genome_offset is [0, chr1len, chr1+chr2len, ..., genome_len] genome_offset = genome_cumsum[:] genome_offset.insert(0, 0) genome_offset_dict = {str(i): genome_offset[i-1] for i in range(1, len(chr_lengths)+1)} prefix = '' if 'chr' in chrom.lower(): chrom = chrom[3:] prefix = chrom[0:3] genomic_coord = pos + genome_offset_dict[chrom] ###circular randomisation rand_genomic_coord = (genomic_coord + shift) % genome_size #decompose into chrom and pos for c, offset in enumerate(genome_offset): if rand_genomic_coord - offset <= chr_lengths[c]: new_chrom = prefix + str(c+1) new_pos = rand_genomic_coord - offset break return [new_chrom, new_pos]