# 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/simulation/readgenerate.py
# SEED_MAX = (1 << 32) - 1  # Used for seeding rng
# def process_multi_threaded(fasta_fname, vcf_fname, sample_name, bed_fname,
#                            read_module, model, coverage,
#                            fastq1_fname, sidecar_fname, fastq2_fname,
#                            truncate_to=None,
#                            unpair=False,
#                            threads=2, seed=7):
# def get_data_for_workers(model, vcf, seed):
# def read_generating_worker(worker_id, fasta_fname, sample_name, read_module, read_model, in_queue, out_queue):
# def validate_templates_from_read_model(tplt):
# 
# Path: mitty/simulation/sequencing/writefastq.py
# def load_qname_sidecar(sidecar_fn):
#   def parse_sidecar_file(_fn):
#     with open(_fn, 'r') as fp:
#       for ln in fp:
#         yield ln.split('|', 1)[0][1:], ln[1:-1]
#   return {
#     k: v
#     for k, v in parse_sidecar_file(sidecar_fn)
#   }
# 
# def parse_qname(qname, long_qname_table=None):
#   """Given a Mitty qname return us the POS and CIGAR as we would put in a BAM. There is also a special_cigar
#   which is set for reads completely inside long insertions
# 
#   @index|sn|chrom:copy|strand:pos:cigar:v1,v2,...:MD|strand:pos:cigar:v1,v2,...:MD*
# 
#   :param qname:
#   :param long_qname_table: If present, is a map of qname index and qname for just the long qnames
#   :return: pos, cigar, special_cigar
#   """
#   def _parse_(_cigar, _v_list):
#     """
#     Parse cigar to extract special_cigar if needed
#     Parse v_list
# 
#     :param _cigar:
#     :param _v_list:
#     :return:
#     """
#     if _cigar[0] == '>':  # This is a special_cigar for a read from inside an insertion
#       _special_cigar = _cigar
#       _cigar = _cigar.split('+')[-1]
#     else:
#       _special_cigar = None
# 
#     return _cigar, _special_cigar, [int(v) for v in _v_list.split(',') if v is not '']
# 
#   def _split_(_r):
#     strand, pos, cigar, v_list, md = _r.split(':')
#     return (int(strand), int(pos)) + _parse_(cigar, v_list) + (md,)
# 
#   if qname[-1] != '*':  # Truncated qname
#     if long_qname_table is None:
#       raise ValueError('Long qname with no table lookup')  # It's the caller's responsibility to handle this error
#     _qname = long_qname_table.get(qname.split('|', 1)[0], None)
#     if _qname is None:
#       raise ValueError('Long qname with no table lookup: {}, {}'.format(qname.split('|', 1)[0], qname))
#       # It's the caller's responsibility to handle this error
#     qname = _qname
# 
#   d = qname[:-1].split('|')  # Strip out the terminal '*'
#   serial, sample = d[:2]
#   chrom, cpy = d[2].split(':')
#   cpy = int(cpy)
#   return [
#     ri(serial, sample, chrom, cpy, *_split_(r))
#     for r in d[3:]
#   ]
# 
# Path: mitty/version.py



import logging
import os
import sys
import time
import pysam
from multiprocessing import Process, Queue
from mitty.lib.cigars import cigarv2_v1
from mitty.simulation.readgenerate import DNA_complement
from mitty.simulation.sequencing.writefastq import load_qname_sidecar, parse_qname
from mitty.version import __version__



logger = logging.getLogger(__name__)

__process_stop_code__ = 'SETECASTRONOMY'


def construct_header(fasta_ann, rg_id, sample='S', platform='Illumina'):
  return {
    'HD': {'VN': '1.0'},
    'PG': [{'CL': ' '.join(sys.argv),
            'ID': 'mitty-god-aligner',
            'PN': 'god-aligner',

# 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/simulation/readgenerate.py
# SEED_MAX = (1 << 32) - 1  # Used for seeding rng
# def process_multi_threaded(fasta_fname, vcf_fname, sample_name, bed_fname,
#                            read_module, model, coverage,
#                            fastq1_fname, sidecar_fname, fastq2_fname,
#                            truncate_to=None,
#                            unpair=False,
#                            threads=2, seed=7):
# def get_data_for_workers(model, vcf, seed):
# def read_generating_worker(worker_id, fasta_fname, sample_name, read_module, read_model, in_queue, out_queue):
# def validate_templates_from_read_model(tplt):
# 
# Path: mitty/simulation/sequencing/writefastq.py
# def load_qname_sidecar(sidecar_fn):
#   def parse_sidecar_file(_fn):
#     with open(_fn, 'r') as fp:
#       for ln in fp:
#         yield ln.split('|', 1)[0][1:], ln[1:-1]
#   return {
#     k: v
#     for k, v in parse_sidecar_file(sidecar_fn)
#   }
# 
# def parse_qname(qname, long_qname_table=None):
#   """Given a Mitty qname return us the POS and CIGAR as we would put in a BAM. There is also a special_cigar
#   which is set for reads completely inside long insertions
# 
#   @index|sn|chrom:copy|strand:pos:cigar:v1,v2,...:MD|strand:pos:cigar:v1,v2,...:MD*
# 
#   :param qname:
#   :param long_qname_table: If present, is a map of qname index and qname for just the long qnames
#   :return: pos, cigar, special_cigar
#   """
#   def _parse_(_cigar, _v_list):
#     """
#     Parse cigar to extract special_cigar if needed
#     Parse v_list
# 
#     :param _cigar:
#     :param _v_list:
#     :return:
#     """
#     if _cigar[0] == '>':  # This is a special_cigar for a read from inside an insertion
#       _special_cigar = _cigar
#       _cigar = _cigar.split('+')[-1]
#     else:
#       _special_cigar = None
# 
#     return _cigar, _special_cigar, [int(v) for v in _v_list.split(',') if v is not '']
# 
#   def _split_(_r):
#     strand, pos, cigar, v_list, md = _r.split(':')
#     return (int(strand), int(pos)) + _parse_(cigar, v_list) + (md,)
# 
#   if qname[-1] != '*':  # Truncated qname
#     if long_qname_table is None:
#       raise ValueError('Long qname with no table lookup')  # It's the caller's responsibility to handle this error
#     _qname = long_qname_table.get(qname.split('|', 1)[0], None)
#     if _qname is None:
#       raise ValueError('Long qname with no table lookup: {}, {}'.format(qname.split('|', 1)[0], qname))
#       # It's the caller's responsibility to handle this error
#     qname = _qname
# 
#   d = qname[:-1].split('|')  # Strip out the terminal '*'
#   serial, sample = d[:2]
#   chrom, cpy = d[2].split(':')
#   cpy = int(cpy)
#   return [
#     ri(serial, sample, chrom, cpy, *_split_(r))
#     for r in d[3:]
#   ]
# 
# Path: mitty/version.py



# Path: mitty/benchmarking/god_aligner.py
import logging
import os
import sys
import time
import pysam
from multiprocessing import Process, Queue
from mitty.lib.cigars import cigarv2_v1
from mitty.simulation.readgenerate import DNA_complement
from mitty.simulation.sequencing.writefastq import load_qname_sidecar, parse_qname
from mitty.version import __version__




logger = logging.getLogger(__name__)

__process_stop_code__ = 'SETECASTRONOMY'


def construct_header(fasta_ann, rg_id, sample='S', platform='Illumina'):
  return {
    'HD': {'VN': '1.0'},
    'PG': [{'CL': ' '.join(sys.argv),
            'ID': 'mitty-god-aligner',
            'PN': 'god-aligner',

            'VN': __version__}],