# 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/lib/evcfparse.py
# def parse_line(v):
#   """Given a variant (line) from the VCF categorize it as a TP, FP, FN or GT
# 
#   :param v:
#   :return:   (category,
#               size)
#   """
#   t, q = v.samples['TRUTH'], v.samples['QUERY']
#   cat = cat_dict.get(t['BD'] + q['BD'], None)
#   if cat is not None and cat is not 'skip':
#     alt = alt_dict[cat](t, q)
#     sz = len(alt) - len(v.ref)
#   else:
#     alt, sz = None, None
# 
#   return cat, sz, alt


import matplotlib
import numpy as np
import pysam
import matplotlib.pyplot as plt
from mitty.benchmarking.plot.byvsize import plot_panels
from mitty.lib.evcfparse import parse_line
# Ignoring bin size for now
def plot(data, fig_fname, bin_size=5, plot_range=None, title='P/R by variant size'):
  max_size = int((data.shape[0] - 3)/2)
  if plot_range is not None and plot_range < max_size:
    n0, n1 = max_size + 1 - (plot_range + 1), max_size + 1 + (plot_range + 1) + 1
    _data = data[n0:n1]
    for k in data.dtype.names:
      _data[0][k] = data[:n0][k].sum()
      _data[-1][k] = data[n1:][k].sum()
    data = _data

  fig = plt.figure(figsize=(6, 11))
  plt.subplots_adjust(bottom=0.05, top=0.95, hspace=0.01)

  ax1 = plt.subplot(411)
  r_p = plot_panels(ax1,
                    num=data['TP'] + data['GT'],
                    den=(data['TP'] + data['FN'] + data['GT']),
                    bin_size=bin_size,
                    yticks=[0.0, 0.5, 1.0], ylim=[-0.05, 1.05],
                    color='b', label='recall')
  p_p = plot_panels(ax1,
                    num=data['TP'] + data['GT'],
                    den=(data['TP'] + data['FP'] + data['GT']),
                    bin_size=bin_size,
                    yticks=[0.0, 0.5, 1.0], ylim=[-0.05, 1.05],
                    color='r', label='precision')
  plt.legend(handles=[r_p, p_p], loc='center left',fontsize=9)

  ax2 = plt.subplot(412)

# 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/lib/evcfparse.py
# def parse_line(v):
#   """Given a variant (line) from the VCF categorize it as a TP, FP, FN or GT
# 
#   :param v:
#   :return:   (category,
#               size)
#   """
#   t, q = v.samples['TRUTH'], v.samples['QUERY']
#   cat = cat_dict.get(t['BD'] + q['BD'], None)
#   if cat is not None and cat is not 'skip':
#     alt = alt_dict[cat](t, q)
#     sz = len(alt) - len(v.ref)
#   else:
#     alt, sz = None, None
# 
#   return cat, sz, alt


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

# Ignoring bin size for now
def plot(data, fig_fname, bin_size=5, plot_range=None, title='P/R by variant size'):
  max_size = int((data.shape[0] - 3)/2)
  if plot_range is not None and plot_range < max_size:
    n0, n1 = max_size + 1 - (plot_range + 1), max_size + 1 + (plot_range + 1) + 1
    _data = data[n0:n1]
    for k in data.dtype.names:
      _data[0][k] = data[:n0][k].sum()
      _data[-1][k] = data[n1:][k].sum()
    data = _data

  fig = plt.figure(figsize=(6, 11))
  plt.subplots_adjust(bottom=0.05, top=0.95, hspace=0.01)

  ax1 = plt.subplot(411)
  r_p = plot_panels(ax1,
                    num=data['TP'] + data['GT'],
                    den=(data['TP'] + data['FN'] + data['GT']),
                    bin_size=bin_size,
                    yticks=[0.0, 0.5, 1.0], ylim=[-0.05, 1.05],
                    color='b', label='recall')
  p_p = plot_panels(ax1,
                    num=data['TP'] + data['GT'],
                    den=(data['TP'] + data['FP'] + data['GT']),
                    bin_size=bin_size,
                    yticks=[0.0, 0.5, 1.0], ylim=[-0.05, 1.05],
                    color='r', label='precision')
  plt.legend(handles=[r_p, p_p], loc='center left',fontsize=9)

  ax2 = plt.subplot(412)

  gt_p = plot_panels(ax2,