# 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):


import tempfile
import logging
import cytoolz
import numpy as np
import pysam
import pandas as pd
import xarray as xr
from collections import OrderedDict, Counter
from mitty.benchmarking.alignmentscore import score_alignment_error, correct_tlen, load_qname_sidecar, parse_qname

  :param titer:
  :return:
  """
  long_qname_table = load_qname_sidecar(sidecar_fname) if sidecar_fname is not None else None

  for template in titer:
    ri = parse_qname(
        template[0].qname,
        long_qname_table=long_qname_table
    ) if long_qname_table is not None else [None, None]
    yield tuple(
      {
        'read': mate,
        'read_info': ri[1 if mate.is_read2 else 0]
      }
      for mate in template
    )


@cytoolz.curry
def compute_derr(titer, max_d=200):
  """Mutates dictionary: adds d_err field to it. Requires qname parsing step

  :param max_d:
  :param riter:
  :return:
  """
  for template in titer:
    for mate in template:

# 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/bamfilters.py
import tempfile
import logging
import cytoolz
import numpy as np
import pysam
import pandas as pd
import xarray as xr
from collections import OrderedDict, Counter
from mitty.benchmarking.alignmentscore import score_alignment_error, correct_tlen, load_qname_sidecar, parse_qname


  :param titer:
  :return:
  """
  long_qname_table = load_qname_sidecar(sidecar_fname) if sidecar_fname is not None else None

  for template in titer:
    ri = parse_qname(
        template[0].qname,
        long_qname_table=long_qname_table
    ) if long_qname_table is not None else [None, None]
    yield tuple(
      {
        'read': mate,
        'read_info': ri[1 if mate.is_read2 else 0]
      }
      for mate in template
    )


@cytoolz.curry
def compute_derr(titer, max_d=200):
  """Mutates dictionary: adds d_err field to it. Requires qname parsing step

  :param max_d:
  :param riter:
  :return:
  """
  for template in titer:
    for mate in template:

      mate['d_err'] = score_alignment_error(r=mate['read'], ri=mate['read_info'], max_d=max_d)