# Path: crumbs/bam/flag.py
# def create_flag(bit_tags):
#     'It returns the integer corresponding to the bitwise or of the tags'
#     return reduce(or_, [SAM_FLAG_BITS[t] for t in bit_tags])
# 
# Path: crumbs/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/bin_utils.py
# def get_num_threads(threads):
#     """It returns num of threads to use in parallel.
# 
#     You can pass to the funaction the  memory you want to use each thread.
#     It calculates the number of treads
#     In megabytes
#     """
#     phisical_threads = os.sysconf('SC_NPROCESSORS_ONLN')
#     if not threads:
#         return 1
#     elif isinstance(threads, bool):
#         return phisical_threads
#     else:
#         return threads
# 
# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):


import os.path
import shutil
import sys
from subprocess import check_call, CalledProcessError
from tempfile import NamedTemporaryFile
from array import array
from crumbs.bam.flag import create_flag
from crumbs.settings import get_setting
from crumbs.utils.bin_utils import get_num_threads
from crumbs.utils.optional_modules import (AlignmentFile, view, index, faidx,
                                           calmd)
        out_bam_fpath = in_bam_fpath

    if out_bam_fpath == in_bam_fpath:
        realigned_fhand = NamedTemporaryFile(suffix='.realigned.bam',
                                             delete=False)
        temp_out_fpath = realigned_fhand.name
    else:
        temp_out_fpath = out_bam_fpath

    _realign_bam(in_bam_fpath, reference_fpath, temp_out_fpath, threads=False)
    sort_bam(temp_out_fpath)

    if temp_out_fpath != out_bam_fpath:
        shutil.move(temp_out_fpath, out_bam_fpath)


def _realign_bam(bam_fpath, reference_fpath, out_bam_fpath, threads=False):
    'It realigns the bam using GATK Local realignment around indels'
    # reference sam index
    _create_sam_reference_index(reference_fpath)

    # reference picard dict
    _create_picard_dict(reference_fpath)

    # bam index
    index_bam(bam_fpath)

    # the intervals to realign
#     gatk_dir = get_setting("GATK_DIR")
#     gatk_jar = os.path.join(gatk_dir, 'GenomeAnalysisTK.jar')

# Path: crumbs/bam/flag.py
# def create_flag(bit_tags):
#     'It returns the integer corresponding to the bitwise or of the tags'
#     return reduce(or_, [SAM_FLAG_BITS[t] for t in bit_tags])
# 
# Path: crumbs/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/bin_utils.py
# def get_num_threads(threads):
#     """It returns num of threads to use in parallel.
# 
#     You can pass to the funaction the  memory you want to use each thread.
#     It calculates the number of treads
#     In megabytes
#     """
#     phisical_threads = os.sysconf('SC_NPROCESSORS_ONLN')
#     if not threads:
#         return 1
#     elif isinstance(threads, bool):
#         return phisical_threads
#     else:
#         return threads
# 
# Path: crumbs/utils/optional_modules.py
# MSG = 'A python package to run this executable is required,'
# BIO = 'biopython'
# BIO_BGZF = 'biopython with Bgzf support'
#     NCBIXML = create_fake_class(MSG + BIO)
#     NCBIWWW = create_fake_class(MSG + BIO)
# def create_fake_class(msg):
#         def __init__(self, *args, **kwargs):
# def create_fake_funct(msg):
#     def FakeRequiredfunct(*args, **kwargs):
#     class FakePythonRequiredClass(object):


# Path: crumbs/bam/bam_tools.py
import os.path
import shutil
import sys
from subprocess import check_call, CalledProcessError
from tempfile import NamedTemporaryFile
from array import array
from crumbs.bam.flag import create_flag
from crumbs.settings import get_setting
from crumbs.utils.bin_utils import get_num_threads
from crumbs.utils.optional_modules import (AlignmentFile, view, index, faidx,
                                           calmd)

        out_bam_fpath = in_bam_fpath

    if out_bam_fpath == in_bam_fpath:
        realigned_fhand = NamedTemporaryFile(suffix='.realigned.bam',
                                             delete=False)
        temp_out_fpath = realigned_fhand.name
    else:
        temp_out_fpath = out_bam_fpath

    _realign_bam(in_bam_fpath, reference_fpath, temp_out_fpath, threads=False)
    sort_bam(temp_out_fpath)

    if temp_out_fpath != out_bam_fpath:
        shutil.move(temp_out_fpath, out_bam_fpath)


def _realign_bam(bam_fpath, reference_fpath, out_bam_fpath, threads=False):
    'It realigns the bam using GATK Local realignment around indels'
    # reference sam index
    _create_sam_reference_index(reference_fpath)

    # reference picard dict
    _create_picard_dict(reference_fpath)

    # bam index
    index_bam(bam_fpath)

    # the intervals to realign
#     gatk_dir = get_setting("GATK_DIR")
#     gatk_jar = os.path.join(gatk_dir, 'GenomeAnalysisTK.jar')

    gatk_jar = get_setting('GATK_JAR')