# 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

  If strict is True: Look at the distance between the simulated P and the aligned P
  If strict is False:
   If from inside an insertion, treat like strict
   Else, find the first base in the read that is placed on the reference for both the
         aligned and correct reads and compute the difference
         If there is no such base, treat like strict

  Look at the aligned CIGAR and compute the difference between exact P and
                     aligned P after accounting for any soft clip at the start of the read

  :param r: aligned read
  :param ri: readinfo for correct alignment
  :param strict: If True, simply compute difference between simulated P and aligned P
                 if False, find first common reference matching base
  :return: -max_d <= d_err <= max_d + 2
           d_err = max_d + 1 if wrong chrom
           d_err = max_d + 2 if unmapped
  """
  if r.is_unmapped:
    d_err = max_d + 2
  elif r.reference_name != ri.chrom:
    d_err = max_d + 1
  else:
    # This is what we score against when we are 'strict' or inside an insertion
    # Or we can't find a common reference matching base in the correct and aligned reads
    correct_pos, aligned_pos = ri.pos - 1, r.pos
    # If we are not strict AND not in an insertion we use first common reference matching base
    if not strict and ri.special_cigar is None and not is_simple_case(ri.cigar, r.cigarstring):
      rc = pysam.AlignedSegment()

# 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


  If strict is True: Look at the distance between the simulated P and the aligned P
  If strict is False:
   If from inside an insertion, treat like strict
   Else, find the first base in the read that is placed on the reference for both the
         aligned and correct reads and compute the difference
         If there is no such base, treat like strict

  Look at the aligned CIGAR and compute the difference between exact P and
                     aligned P after accounting for any soft clip at the start of the read

  :param r: aligned read
  :param ri: readinfo for correct alignment
  :param strict: If True, simply compute difference between simulated P and aligned P
                 if False, find first common reference matching base
  :return: -max_d <= d_err <= max_d + 2
           d_err = max_d + 1 if wrong chrom
           d_err = max_d + 2 if unmapped
  """
  if r.is_unmapped:
    d_err = max_d + 2
  elif r.reference_name != ri.chrom:
    d_err = max_d + 1
  else:
    # This is what we score against when we are 'strict' or inside an insertion
    # Or we can't find a common reference matching base in the correct and aligned reads
    correct_pos, aligned_pos = ri.pos - 1, r.pos
    # If we are not strict AND not in an insertion we use first common reference matching base
    if not strict and ri.special_cigar is None and not is_simple_case(ri.cigar, r.cigarstring):
      rc = pysam.AlignedSegment()

      rc.pos, rc.cigarstring = ri.pos - 1, cigarv2_v1(ri.cigar)