# Path: mitty/benchmarking/plot/byvsize.py
# def plot_panels(ax, num, den=None, bin_size=None,
#                 color='k', linestyle='-', label='',
#                 yscale='linear', yticks=None, ylim=None, show_ax_label=False,
#                 ci=0.05):
#   """Interpret y (an array of length 2 * N + 3) as follows:
#     The indexes represent variant sizes from
# 
#     DEL len > N
#     DEL len from N to 1
#     SNPs
#     INS len from 1 to N
#     INS len > N
# 
#   :param ax:
#   :param y: success rate
#   :param cnt: number of counts in bin
#   :param color:
#   :param linestyle:
#   :param label:
#   :param yscale:
#   :param yticks:
#   :param ylim:
#   :param show_ax_label:
#   :return:
#   """
#   parts = bin_data_by_variant_size(num, den, bin_size)
#   x1, x0 = parts[-1]['x'][0], parts[0]['x'][0]
#   x_lim = [x0 - .1 *(x1 - x0), x1 + .1 *(x1 - x0)]
#   xticks, xticklabels = [], []
#   for part in parts:
#     line_with_ci(ax, part, color=color, linestyle=linestyle)
#     xticks += part['xticks']
#     xticklabels += part['xticklabels']
# 
#   # The fixed reference lines
#   ax.axvline(x=parts[0]['x'], color='k', linestyle=':')
#   ax.axvline(x=parts[2]['x'], color='k', linestyle=':')
#   ax.axvline(x=parts[4]['x'], color='k', linestyle=':')
# 
#   ax.set_xticks(xticks)
#   ax.set_xticklabels(xticklabels if show_ax_label else [])
# 
#   ax.set_yscale(yscale)
#   if yticks is not None:
#     ax.set_yticks(yticks)
#   if ylim is not None:
#     ax.set_ylim(ylim)
#   ax.get_yaxis().get_major_formatter().labelOnlyBase = False
#   ax.set_xlim(x_lim)
# 
#   return mpatches.Patch(color=color, linestyle=linestyle, label=label)  # patch_for_legend


import numpy as np
import pysam
import matplotlib
import matplotlib.pyplot as plt
from mitty.benchmarking.plot.byvsize import plot_panels
"""Program to go through a VCF and compute the distribution of variant sizes"""


matplotlib.use('Agg')


def main(vcf_fname, sample_name=None, max_size=50):
  data = np.zeros(shape=(2 * max_size + 1 + 2), dtype=int)
  offset, max_n = max_size + 1, 2 * max_size + 2
  mode = 'rb' if vcf_fname.endswith('bcf') else 'r'
  vcf_in = pysam.VariantFile(vcf_fname, mode)
  if sample_name is not None:
    vcf_in.subset_samples([sample_name])
  for v in vcf_in:
    for alt in v.alts:
      sz = len(alt) - len(v.ref)
      data[min(max_n, max(0, offset + sz))] += 1
  return data


def plot(data, fig_fname, bin_size=5, title='Variant size distribution'):
  fig = plt.figure(figsize=(11, 6))
  plt.subplots_adjust(bottom=0.05, top=0.95, hspace=0.01)

  ax1 = plt.subplot(111)
  n_max = 10 ** np.ceil(np.log10(data.max()))
  n_min = 10 ** int(np.log10(data.min() + 1))

# Path: mitty/benchmarking/plot/byvsize.py
# def plot_panels(ax, num, den=None, bin_size=None,
#                 color='k', linestyle='-', label='',
#                 yscale='linear', yticks=None, ylim=None, show_ax_label=False,
#                 ci=0.05):
#   """Interpret y (an array of length 2 * N + 3) as follows:
#     The indexes represent variant sizes from
# 
#     DEL len > N
#     DEL len from N to 1
#     SNPs
#     INS len from 1 to N
#     INS len > N
# 
#   :param ax:
#   :param y: success rate
#   :param cnt: number of counts in bin
#   :param color:
#   :param linestyle:
#   :param label:
#   :param yscale:
#   :param yticks:
#   :param ylim:
#   :param show_ax_label:
#   :return:
#   """
#   parts = bin_data_by_variant_size(num, den, bin_size)
#   x1, x0 = parts[-1]['x'][0], parts[0]['x'][0]
#   x_lim = [x0 - .1 *(x1 - x0), x1 + .1 *(x1 - x0)]
#   xticks, xticklabels = [], []
#   for part in parts:
#     line_with_ci(ax, part, color=color, linestyle=linestyle)
#     xticks += part['xticks']
#     xticklabels += part['xticklabels']
# 
#   # The fixed reference lines
#   ax.axvline(x=parts[0]['x'], color='k', linestyle=':')
#   ax.axvline(x=parts[2]['x'], color='k', linestyle=':')
#   ax.axvline(x=parts[4]['x'], color='k', linestyle=':')
# 
#   ax.set_xticks(xticks)
#   ax.set_xticklabels(xticklabels if show_ax_label else [])
# 
#   ax.set_yscale(yscale)
#   if yticks is not None:
#     ax.set_yticks(yticks)
#   if ylim is not None:
#     ax.set_ylim(ylim)
#   ax.get_yaxis().get_major_formatter().labelOnlyBase = False
#   ax.set_xlim(x_lim)
# 
#   return mpatches.Patch(color=color, linestyle=linestyle, label=label)  # patch_for_legend


# Path: mitty/benchmarking/vsizedistrib.py
import numpy as np
import pysam
import matplotlib
import matplotlib.pyplot as plt
from mitty.benchmarking.plot.byvsize import plot_panels

"""Program to go through a VCF and compute the distribution of variant sizes"""


matplotlib.use('Agg')


def main(vcf_fname, sample_name=None, max_size=50):
  data = np.zeros(shape=(2 * max_size + 1 + 2), dtype=int)
  offset, max_n = max_size + 1, 2 * max_size + 2
  mode = 'rb' if vcf_fname.endswith('bcf') else 'r'
  vcf_in = pysam.VariantFile(vcf_fname, mode)
  if sample_name is not None:
    vcf_in.subset_samples([sample_name])
  for v in vcf_in:
    for alt in v.alts:
      sz = len(alt) - len(v.ref)
      data[min(max_n, max(0, offset + sz))] += 1
  return data


def plot(data, fig_fname, bin_size=5, title='Variant size distribution'):
  fig = plt.figure(figsize=(11, 6))
  plt.subplots_adjust(bottom=0.05, top=0.95, hspace=0.01)

  ax1 = plt.subplot(111)
  n_max = 10 ** np.ceil(np.log10(data.max()))
  n_min = 10 ** int(np.log10(data.min() + 1))

  r_p = plot_panels(ax1,