# Path: utils.py
# def makedirs(dirs):
#     if type(dirs) == list:
#         for d in dirs:
#             if not os.path.exists(d):
#                 os.makedirs(d)
#     else:
#         if not os.path.exists(dirs):
#             os.makedirs(dirs)


import os
import subprocess
import sys
import numpy
import argparse
import pysam
import vcf
import pybedtools
import logging
from collections import defaultdict, OrderedDict
from utils import makedirs
            if record.POS <= region.start + flank or record.POS + len(record.REF) + flank - 1 >= region.end: continue
            if 'SVTYPE' in record.INFO and record.INFO['SVTYPE'] in ['DEL','INV','DUP'] and record.POS + max(map(abs, record.INFO['SVLEN'])) >= region.end + flank: continue
            record.CHROM = str(region_index) if use_short_contig_names else ("%s_%d_%d" % (str(region.chrom), region.start, region.end))
            # record.POS seems to be zero-based, at least in the infinite wisdom of my version of pysam
            record.POS = record.POS - region.start
            if not new_samples:
                vcf_writer.write_record(record)
                continue
            else:
                snames = []
                sindexes = {}
            for s in new_samples:
                for i in xrange(len(record.samples)):
                    if s == record.samples[i].sample:
                        sindexes[s] = i
                        snames.append(record.samples[i])
            vcfrecord = vcf.model._Record(record.CHROM, record.POS, record.ID, record.REF, record.ALT, record.QUAL, record.FILTER, record.INFO, record.FORMAT, sindexes, snames)
            vcf_writer.write_record(vcfrecord)
    vcf_writer.close()
    pysam.tabix_index(out_vcf, force=True, preset='vcf')
    logger.info("Lifted over the VCF %s to %s" % (in_vcf, out_vcf))

    return "{}.gz".format(out_vcf)


def gen_restricted_ref_and_vcfs(reference, invcfs, regions, samples, outdir, flank=0, short_contig_names=False):
    restricted_fasta = reference
    outvcfs = invcfs

    if regions:

# Path: utils.py
# def makedirs(dirs):
#     if type(dirs) == list:
#         for d in dirs:
#             if not os.path.exists(d):
#                 os.makedirs(d)
#     else:
#         if not os.path.exists(dirs):
#             os.makedirs(dirs)


# Path: generate_small_test_ref.py
import os
import subprocess
import sys
import numpy
import argparse
import pysam
import vcf
import pybedtools
import logging
from collections import defaultdict, OrderedDict
from utils import makedirs

            if record.POS <= region.start + flank or record.POS + len(record.REF) + flank - 1 >= region.end: continue
            if 'SVTYPE' in record.INFO and record.INFO['SVTYPE'] in ['DEL','INV','DUP'] and record.POS + max(map(abs, record.INFO['SVLEN'])) >= region.end + flank: continue
            record.CHROM = str(region_index) if use_short_contig_names else ("%s_%d_%d" % (str(region.chrom), region.start, region.end))
            # record.POS seems to be zero-based, at least in the infinite wisdom of my version of pysam
            record.POS = record.POS - region.start
            if not new_samples:
                vcf_writer.write_record(record)
                continue
            else:
                snames = []
                sindexes = {}
            for s in new_samples:
                for i in xrange(len(record.samples)):
                    if s == record.samples[i].sample:
                        sindexes[s] = i
                        snames.append(record.samples[i])
            vcfrecord = vcf.model._Record(record.CHROM, record.POS, record.ID, record.REF, record.ALT, record.QUAL, record.FILTER, record.INFO, record.FORMAT, sindexes, snames)
            vcf_writer.write_record(vcfrecord)
    vcf_writer.close()
    pysam.tabix_index(out_vcf, force=True, preset='vcf')
    logger.info("Lifted over the VCF %s to %s" % (in_vcf, out_vcf))

    return "{}.gz".format(out_vcf)


def gen_restricted_ref_and_vcfs(reference, invcfs, regions, samples, outdir, flank=0, short_contig_names=False):
    restricted_fasta = reference
    outvcfs = invcfs

    if regions:

        makedirs([outdir])