# Path: mitty/benchmarking/alignmentscore.py
# def is_simple_case(cigar1, cigar2):
# def find_first_common_reference_matching_base_positions(r1, r2):
# def score_alignment_error(r, ri, max_d=200, strict=False):
# def correct_tlen(ri1, ri2, r):
# def tag_alignment(r, ri, max_d=200, strict=False):


from collections import OrderedDict, Counter
from mitty.benchmarking.alignmentscore import score_alignment_error, correct_tlen, load_qname_sidecar, parse_qname
import logging
import cytoolz
import numpy as np
import pysam
import xarray as xr
  def _finalize():
    """given a buffer of data bin it into the histogram
    """
    for pah in aah:
      pah.data += fastmultihist(buf[:idx, :], pah.attrs['bin_edges'])

  assert histogram_def is not None, 'Histogram definition must be passed'
  if not isinstance(histogram_def, (list, tuple)):
    histogram_def = (histogram_def, )

  aah = [initialize_histogram(**hd) for hd in histogram_def]
  max_d_set = set(a.attrs['max_d'] for a in aah)
  max_v_set = set(a.attrs['max_v'] for a in aah if a.attrs['max_v'] is not None)
  max_d = max_d_set.pop()
  max_v = max_v_set.pop() or 1  # We could have a case where we never bin by the max_v

  if len(max_d_set) > 0:
    raise RuntimeError('max_d for each histogram has to be the same')

  if len(max_v_set) > 0:
    raise RuntimeError('max_v for each histogram has to be the same')

  temp_read = pysam.AlignedSegment()

  bs = buf_size
  buf = np.empty(shape=(bs, 8))
  idx = 0

  for tpl in titer:
    # For efficiency this assumes we have PE reads and have paired them up using make_pairs

# Path: mitty/benchmarking/alignmentscore.py
# def is_simple_case(cigar1, cigar2):
# def find_first_common_reference_matching_base_positions(r1, r2):
# def score_alignment_error(r, ri, max_d=200, strict=False):
# def correct_tlen(ri1, ri2, r):
# def tag_alignment(r, ri, max_d=200, strict=False):


# Path: mitty/analysis/bamhist.py
from collections import OrderedDict, Counter
from mitty.benchmarking.alignmentscore import score_alignment_error, correct_tlen, load_qname_sidecar, parse_qname
import logging
import cytoolz
import numpy as np
import pysam
import xarray as xr

  def _finalize():
    """given a buffer of data bin it into the histogram
    """
    for pah in aah:
      pah.data += fastmultihist(buf[:idx, :], pah.attrs['bin_edges'])

  assert histogram_def is not None, 'Histogram definition must be passed'
  if not isinstance(histogram_def, (list, tuple)):
    histogram_def = (histogram_def, )

  aah = [initialize_histogram(**hd) for hd in histogram_def]
  max_d_set = set(a.attrs['max_d'] for a in aah)
  max_v_set = set(a.attrs['max_v'] for a in aah if a.attrs['max_v'] is not None)
  max_d = max_d_set.pop()
  max_v = max_v_set.pop() or 1  # We could have a case where we never bin by the max_v

  if len(max_d_set) > 0:
    raise RuntimeError('max_d for each histogram has to be the same')

  if len(max_v_set) > 0:
    raise RuntimeError('max_v for each histogram has to be the same')

  temp_read = pysam.AlignedSegment()

  bs = buf_size
  buf = np.empty(shape=(bs, 8))
  idx = 0

  for tpl in titer:
    # For efficiency this assumes we have PE reads and have paired them up using make_pairs

    ctl = correct_tlen(tpl[0]['read_info'], tpl[1]['read_info'], temp_read)