# 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 os
import time
import logging
import pysam
from mitty.benchmarking.alignmentscore import ri, load_qname_sidecar, parse_qname, score_alignment_error, tag_alignment


def partition_by_mapped(rl, *args):
  """

  :param rl:
  :param args: rest are ignored
  :return:
  """
  return independent_partitions(rl, lambda r: not r.is_unmapped)


def partition_by_p_diff(rl, x, *args):
  """ Treating A as a gold standard, consider p_diff as the difference in alignment to A.
  Reads that are unmapped in A are discarded. Basically, modify the reads in place by
  filling out tag Xd then simply call partition_by_d_err for mapped reads. For unmapped
  reads return none

  :param rl: a list of read objects [0, 1, 2, ...]
  :param x: threshold
  :return:
  """
  r0 = rl[0]
  if r0.is_unmapped:
    logger.debug('{} is unmapped. Discarding for p_diff calcs'.format(r0.qname))
    return None

  # ['index', 'sample', 'chrom', 'cpy', 'strand', 'pos', 'cigar', 'special_cigar', 'v_list', 'md']
  r0_ri = ri(None, None, r0.reference_name, 0, 0, r0.pos, r0.cigarstring, None, None, None)
  for r in rl:

# 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/benchmarking/partition_bams.py
import os
import time
import logging
import pysam
from mitty.benchmarking.alignmentscore import ri, load_qname_sidecar, parse_qname, score_alignment_error, tag_alignment



def partition_by_mapped(rl, *args):
  """

  :param rl:
  :param args: rest are ignored
  :return:
  """
  return independent_partitions(rl, lambda r: not r.is_unmapped)


def partition_by_p_diff(rl, x, *args):
  """ Treating A as a gold standard, consider p_diff as the difference in alignment to A.
  Reads that are unmapped in A are discarded. Basically, modify the reads in place by
  filling out tag Xd then simply call partition_by_d_err for mapped reads. For unmapped
  reads return none

  :param rl: a list of read objects [0, 1, 2, ...]
  :param x: threshold
  :return:
  """
  r0 = rl[0]
  if r0.is_unmapped:
    logger.debug('{} is unmapped. Discarding for p_diff calcs'.format(r0.qname))
    return None

  # ['index', 'sample', 'chrom', 'cpy', 'strand', 'pos', 'cigar', 'special_cigar', 'v_list', 'md']
  r0_ri = ri(None, None, r0.reference_name, 0, 0, r0.pos, r0.cigarstring, None, None, None)
  for r in rl:

    tag_alignment(r, r0_ri)