Skip to content

Instantly share code, notes, and snippets.

@TTTPOB
Created November 2, 2021 11:26
Show Gist options
  • Save TTTPOB/54fcf5223f01d41e16381d98c1c698b9 to your computer and use it in GitHub Desktop.
Save TTTPOB/54fcf5223f01d41e16381d98c1c698b9 to your computer and use it in GitHub Desktop.
generate random genome position given chromosome_size.tsv
#!python3
import click
import random
def read_chrom_size(chrom_size):
chrom_size_dict = {}
with open(chrom_size) as f:
for line in f:
line = line.strip().split()
chrom_size_dict[line[0]] = int(line[1])
return chrom_size_dict
@click.command()
@click.option("--chrom-size", "-cs", default=None, help="Chromosome size file")
def main(chrom_size):
"""
first read chromosome size
then generate random chromosome position with possibilities adjusted to
chromosome length
then use click.echo to print it
"""
chrom_size_dict = read_chrom_size(chrom_size)
# random.seed(42)
which_chr = random.choices(
list(chrom_size_dict.keys()), weights=list(chrom_size_dict.values())
)
which_pos = random.randint(1, chrom_size_dict[which_chr[0]] + 1)
click.echo("{}:{}".format(which_chr[0], which_pos))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment