# Path: mitty/simulation/sequencing/writefastq.py
# def writer(fastq1_out, side_car_out, fastq2_out=None, data_queue=None, max_qname_len=__max_qname_len__):
# def parse_qname(qname, long_qname_table=None):
#   def _parse_(_cigar, _v_list):
#   def _split_(_r):
# def load_qname_sidecar(sidecar_fn):
#   def parse_sidecar_file(_fn):
# 
# Path: mitty/lib/cigars.py
# def cigarv2_v1(cigar_v2):
#   """Convert a V2 cigar_v1 string to V1
# 
#   :param cigarstring:
#   :return:
#   """
#   values = pattern.split(cigar_v2.replace('=', 'M').replace('X', 'M'))
#   cigar_v1 = []
#   last_op, op_cnt = values[1], 0
#   for op in zip(values[::2], values[1::2]):
#     if op[1] == last_op:
#       op_cnt += int(op[0])
#     else:
#       cigar_v1 += [str(op_cnt), last_op]
#       last_op, op_cnt = op[1], int(op[0])
#   cigar_v1 += [str(op_cnt), last_op]
#   return ''.join(cigar_v1)


import re
import pysam
from mitty.simulation.sequencing.writefastq import ri, load_qname_sidecar, parse_qname
from mitty.lib.cigars import cigarv2_v1
"""Code for assessing alignment accuracy"""
# good to have these together. Code that uses score_alignment_error is very likely to use
# ri, load_qname_sidecar and parse_qname


cigar_parser = re.compile(r'(\d+)(\D)')


def is_simple_case(cigar1, cigar2):
  _, _, op1, _ = cigar_parser.split(cigar1, maxsplit=1)
  _, _, op2, _ = cigar_parser.split(cigar2, maxsplit=1)
  return op1 in ['=', 'M', 'X'] and op2 in ['M', 'X', '=']


def find_first_common_reference_matching_base_positions(r1, r2):
  return next(
    filter(
      lambda x: x[0] is not None and x[1] is not None,
      zip(r1.get_reference_positions(full_length=True),
          r2.get_reference_positions(full_length=True))),
    (None, None))



# Path: mitty/simulation/sequencing/writefastq.py
# def writer(fastq1_out, side_car_out, fastq2_out=None, data_queue=None, max_qname_len=__max_qname_len__):
# def parse_qname(qname, long_qname_table=None):
#   def _parse_(_cigar, _v_list):
#   def _split_(_r):
# def load_qname_sidecar(sidecar_fn):
#   def parse_sidecar_file(_fn):
# 
# Path: mitty/lib/cigars.py
# def cigarv2_v1(cigar_v2):
#   """Convert a V2 cigar_v1 string to V1
# 
#   :param cigarstring:
#   :return:
#   """
#   values = pattern.split(cigar_v2.replace('=', 'M').replace('X', 'M'))
#   cigar_v1 = []
#   last_op, op_cnt = values[1], 0
#   for op in zip(values[::2], values[1::2]):
#     if op[1] == last_op:
#       op_cnt += int(op[0])
#     else:
#       cigar_v1 += [str(op_cnt), last_op]
#       last_op, op_cnt = op[1], int(op[0])
#   cigar_v1 += [str(op_cnt), last_op]
#   return ''.join(cigar_v1)


# Path: mitty/benchmarking/alignmentscore.py
import re
import pysam
from mitty.simulation.sequencing.writefastq import ri, load_qname_sidecar, parse_qname
from mitty.lib.cigars import cigarv2_v1

"""Code for assessing alignment accuracy"""
# good to have these together. Code that uses score_alignment_error is very likely to use
# ri, load_qname_sidecar and parse_qname


cigar_parser = re.compile(r'(\d+)(\D)')


def is_simple_case(cigar1, cigar2):
  _, _, op1, _ = cigar_parser.split(cigar1, maxsplit=1)
  _, _, op2, _ = cigar_parser.split(cigar2, maxsplit=1)
  return op1 in ['=', 'M', 'X'] and op2 in ['M', 'X', '=']


def find_first_common_reference_matching_base_positions(r1, r2):
  return next(
    filter(
      lambda x: x[0] is not None and x[1] is not None,
      zip(r1.get_reference_positions(full_length=True),
          r2.get_reference_positions(full_length=True))),
    (None, None))



def score_alignment_error(r, ri, max_d=200, strict=False):