# 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 time
import logging
import pysam
from mitty.benchmarking.alignmentscore import load_qname_sidecar
"""Given a FASTQ (or BAM) and a long-qnames file trancate the qnames to
whatever length we want and push the too-long qnames to the overflow
file. Also convert any old style qnames (not ending in a *) to new style
ones with a proper termination character"""



logger = logging.getLogger(__name__)


def main(mainfile_in, sidecar_in, mainfile_out, sidecar_out, truncate_to=240, file_type=None):
  """

  :param mainfile_in:
  :param sidecar_in:
  :param mainfile_out:
  :param sidecar_out:
  :param truncate_to:
  :param file_type: If supplied ("BAM" or "FASTQ") then we will not auto detect
  :return:
  """
  ft = {
    'BAM': 1,
    'FASTQ': 0
  }.get(file_type or auto_detect(mainfile_in))
  fp_in = pysam.AlignmentFile(mainfile_in, mode='rb') if ft else pysam.FastxFile(mainfile_in)
  fp_out = pysam.AlignmentFile(mainfile_out, mode='wb', header=fp_in.header) if ft else open(mainfile_out, 'w')
  side_car_fp = open(sidecar_out, 'w')

  logger.debug('Starting conversion ...')

# 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/empirical/qnametruncate.py
import time
import logging
import pysam
from mitty.benchmarking.alignmentscore import load_qname_sidecar

"""Given a FASTQ (or BAM) and a long-qnames file trancate the qnames to
whatever length we want and push the too-long qnames to the overflow
file. Also convert any old style qnames (not ending in a *) to new style
ones with a proper termination character"""



logger = logging.getLogger(__name__)


def main(mainfile_in, sidecar_in, mainfile_out, sidecar_out, truncate_to=240, file_type=None):
  """

  :param mainfile_in:
  :param sidecar_in:
  :param mainfile_out:
  :param sidecar_out:
  :param truncate_to:
  :param file_type: If supplied ("BAM" or "FASTQ") then we will not auto detect
  :return:
  """
  ft = {
    'BAM': 1,
    'FASTQ': 0
  }.get(file_type or auto_detect(mainfile_in))
  fp_in = pysam.AlignmentFile(mainfile_in, mode='rb') if ft else pysam.FastxFile(mainfile_in)
  fp_out = pysam.AlignmentFile(mainfile_out, mode='wb', header=fp_in.header) if ft else open(mainfile_out, 'w')
  side_car_fp = open(sidecar_out, 'w')

  logger.debug('Starting conversion ...')

  long_qname_table = load_qname_sidecar(sidecar_in)