# Path: mitty/lib/bedfile.py
# def read_bed(bed_fname):
#   return list(map(lambda x: (x[0], int(x[1]), int(x[2])), map(lambda x: x.split(), open(bed_fname, 'r').readlines())))
# 
# Path: mitty/lib/sanitizeseq.py
# def sanitize(seq):
#   return seq.translate(sanitable)


import time
import pysam
import numpy as np
import logging
from itertools import chain
from mitty.lib.bedfile import read_bed
from mitty.lib.sanitizeseq import sanitize
      fp_out,
      region,
      [
        model_dispatch[model[0]](
          rng, region=region, seq=fasta.fetch(reference=region[0], start=region[1], end=region[2]),
          p=model[1],
          p_het=p_het,
          min_size=model[2], max_size=model[3])
        for model in models
      ])
    t2 = time.time()
    logger.debug('Wrote {} variants in region {} in {:0.2f}s'.format(v_cnt, region, t2 - t1))
    v_cnt_tot += v_cnt

  t2 = time.time()
  logger.debug('Simulated {} variants in {:0.2f}s'.format(v_cnt_tot, t2 - t0))


def generate_header(fasta_fname, sample_name):
  fasta = pysam.FastaFile(fasta_fname)
  return '\n'.join(
    ['##fileformat=VCFv4.2'] + \
    ['##contig=<ID={}, length={}>'.format(i, l) for i, l in zip(fasta.references, fasta.lengths)] + \
    ['##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype string">',
     '#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t{}'.format(sample_name)]) + '\n'


def place_poisson_seq(rng, p, seq):
  """Given a random number generator, a probability and an end point, generate poisson distributed events. Skip bases
  that are 'N'.  For short end_p this may, by chance, generate fewer locations that normal"""

# Path: mitty/lib/bedfile.py
# def read_bed(bed_fname):
#   return list(map(lambda x: (x[0], int(x[1]), int(x[2])), map(lambda x: x.split(), open(bed_fname, 'r').readlines())))
# 
# Path: mitty/lib/sanitizeseq.py
# def sanitize(seq):
#   return seq.translate(sanitable)


# Path: mitty/simulation/genome/simulatevariants.py
import time
import pysam
import numpy as np
import logging
from itertools import chain
from mitty.lib.bedfile import read_bed
from mitty.lib.sanitizeseq import sanitize

      fp_out,
      region,
      [
        model_dispatch[model[0]](
          rng, region=region, seq=fasta.fetch(reference=region[0], start=region[1], end=region[2]),
          p=model[1],
          p_het=p_het,
          min_size=model[2], max_size=model[3])
        for model in models
      ])
    t2 = time.time()
    logger.debug('Wrote {} variants in region {} in {:0.2f}s'.format(v_cnt, region, t2 - t1))
    v_cnt_tot += v_cnt

  t2 = time.time()
  logger.debug('Simulated {} variants in {:0.2f}s'.format(v_cnt_tot, t2 - t0))


def generate_header(fasta_fname, sample_name):
  fasta = pysam.FastaFile(fasta_fname)
  return '\n'.join(
    ['##fileformat=VCFv4.2'] + \
    ['##contig=<ID={}, length={}>'.format(i, l) for i, l in zip(fasta.references, fasta.lengths)] + \
    ['##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype string">',
     '#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t{}'.format(sample_name)]) + '\n'


def place_poisson_seq(rng, p, seq):
  """Given a random number generator, a probability and an end point, generate poisson distributed events. Skip bases
  that are 'N'.  For short end_p this may, by chance, generate fewer locations that normal"""

  if p == 0.0: