# 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 process_these_reads(part_d, rl, scoring_fn, threshold, long_qname_table):
  p = scoring_fn(rl, threshold, long_qname_table)
  if p is not None:
    # several of these scoring functions mutate reads in rl
    for bam_fp, r in zip(part_d[p]['filehandles'], rl):
      bam_fp.write(r)
    part_d[p]['total'] += 1


def get_partition_label(n, no):
  if no == 0:
    return []
  return [labels[n % 2][no - 1]] + get_partition_label(int(n / 2), no - 1)


# Partition functions ---------------------------------------------------------------
# The functions return a number between 0 and 2^n - 1 indicating the partition the read should
# go into. If the result is None, this read should be discarded (does not contribute to any counts)

def partition_by_d_err(rl, x, long_qname_table):
  """

  :param rl: a list of read objects [0, 1, 2, ...]
  :param x: threshold
  :return:
  """
  ri = parse_qname(rl[0].qname, long_qname_table)[0 if rl[0].is_read1 else 1]
  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 process_these_reads(part_d, rl, scoring_fn, threshold, long_qname_table):
  p = scoring_fn(rl, threshold, long_qname_table)
  if p is not None:
    # several of these scoring functions mutate reads in rl
    for bam_fp, r in zip(part_d[p]['filehandles'], rl):
      bam_fp.write(r)
    part_d[p]['total'] += 1


def get_partition_label(n, no):
  if no == 0:
    return []
  return [labels[n % 2][no - 1]] + get_partition_label(int(n / 2), no - 1)


# Partition functions ---------------------------------------------------------------
# The functions return a number between 0 and 2^n - 1 indicating the partition the read should
# go into. If the result is None, this read should be discarded (does not contribute to any counts)

def partition_by_d_err(rl, x, long_qname_table):
  """

  :param rl: a list of read objects [0, 1, 2, ...]
  :param x: threshold
  :return:
  """
  ri = parse_qname(rl[0].qname, long_qname_table)[0 if rl[0].is_read1 else 1]
  for r in rl:

    tag_alignment(r, ri)