<commit_before>#!/usr/bin/env python

import os
import sys
import argparse
import vcf
import pysam
import logging
import copy
from collections import defaultdict, OrderedDict

def lift_vcfs(vcfs, out_vcf, reference):
  logger = logging.getLogger(lift_vcfs.__name__)

  if not vcfs:
    logger.info("No VCFs to lift")
    return

  contigs = []

  if reference:
    fasta_handle = pysam.FastaFile(reference)
    contigs = [(contig, fasta_handle.get_reference_length(contig)) for contig in fasta_handle.references]
    fasta_handle.close()

  vcf_template_reader = vcf.Reader(open(vcfs[0], "r"))

  if reference:
    vcf_template_reader.metadata["reference"] = reference
    vcf_template_reader.contigs = OrderedDict([(contig_name, vcf.parser._Contig(contig_name, contig_length)) for (contig_name, contig_length) in contigs])
  vcf_writer = vcf.Writer(open(out_vcf, "w"), vcf_template_reader)
  for index, vcf_ in enumerate(vcfs):
    with open(vcf_) as vcf_fd:
      vcf_reader = vcf.Reader(vcf_fd)
      for record in vcf_reader:
        original_fields = record.CHROM.rsplit("_", 2)
        original_chrom = original_fields[0]
        original_start = int(original_fields[1])
        info = None
        try:
          #info = vcf_template_reader._parse_info(record.INFO)
          info = copy.deepcopy(record.INFO)
          if "END" in info:
            info["END"] += original_start
          if "CHR2" in info:
            chr2_fields = info["CHR2"].rsplit("_", 2)
            chr2_start = int(chr2_fields[1])
            info["CHR2"] = chr2_fields[0]
            info["POS2"] += chr2_start
            info["END2"] += chr2_start
        except ValueError:
          logger.error("Failed to process INFO %s" % str(record.INFO))

        new_record = vcf.model._Record(original_chrom, record.POS + original_start, record.ID, record.REF, record.ALT, record.QUAL, record.FILTER, info, record.FORMAT, record._sample_indexes, record.samples)
        vcf_writer.write_record(new_record)
  vcf_writer.close()

  logger.info("Finished liftover of VCF to original reference")
  return out_vcf


def lift_maps(maps, out_map):
  logger = logging.getLogger(lift_maps.__name__)

  if not maps:
    logger.info("No MAP files to lift")
    return<commit_msg>Handle case when overlapping contigs from overlapping regions<commit_after>#!/usr/bin/env python

import os
import sys
import argparse
import vcf
import pysam
import logging
import copy
from collections import defaultdict, OrderedDict

def lift_vcfs(vcfs, out_vcf, reference):
  logger = logging.getLogger(lift_vcfs.__name__)

  if not vcfs:
    logger.info("No VCFs to lift")
    return

  contigs = []

  if reference:
    fasta_handle = pysam.FastaFile(reference)
    contigs = [(contig, fasta_handle.get_reference_length(contig)) for contig in fasta_handle.references]
    fasta_handle.close()

  vcf_template_reader = vcf.Reader(open(vcfs[0], "r"))

  if reference:
    vcf_template_reader.metadata["reference"] = reference
    vcf_template_reader.contigs = OrderedDict([(contig_name, vcf.parser._Contig(contig_name, contig_length)) for (contig_name, contig_length) in contigs])

  vcf_records = []
  for index, vcf_ in enumerate(vcfs):
    with open(vcf_) as vcf_fd:
      vcf_reader = vcf.Reader(vcf_fd)
      for record in vcf_reader:
        original_fields = record.CHROM.rsplit("_", 2)
        original_chrom = original_fields[0]
        original_start = int(original_fields[1])
        info = None
        try:
          #info = vcf_template_reader._parse_info(record.INFO)
          info = copy.deepcopy(record.INFO)
          if "END" in info:
            info["END"] += original_start
          if "CHR2" in info:
            chr2_fields = info["CHR2"].rsplit("_", 2)
            chr2_start = int(chr2_fields[1])
            info["CHR2"] = chr2_fields[0]
            info["POS2"] += chr2_start
            info["END2"] += chr2_start
        except ValueError:
          logger.error("Failed to process INFO %s" % str(record.INFO))

        new_record = vcf.model._Record(original_chrom, record.POS + original_start, record.ID, record.REF, record.ALT, record.QUAL, record.FILTER, info, record.FORMAT, record._sample_indexes, record.samples)
        vcf_records.append(new_record)

  vcf_records.sort(key = lambda record: (record.CHROM, record.POS, record.REF, record.ALT))

  # Write while removing non-unique entries after liftover
  vcf_writer = vcf.Writer(open(out_vcf, "w"), vcf_template_reader)
  for index in xrange(len(vcf_records)):
      curr_record = vcf_records[index]
      if index:
          prev_record = vcf_records[index - 1]
          if (curr_record.CHROM, curr_record.POS, curr_record.REF, curr_record.ALT) == (prev_record.CHROM, prev_record.POS, prev_record.REF, prev_record.ALT):
              continue
      vcf_writer.write_record(record)
  vcf_writer.close()

  logger.info("Finished liftover of VCF to original reference")
  return out_vcf


def lift_maps(maps, out_map):
  logger = logging.getLogger(lift_maps.__name__)

  if not maps:
    logger.info("No MAP files to lift")
    return
{'fraction_non_alphanumeric': 0.10600985221674877, 'fraction_numerical': 0.00729064039408867, 'mean_word_length': 3.421602787456446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4529126', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1665, 'n_words': 371}