# 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


import os.path
import shutil
import sys
import pysam
from subprocess import check_call, CalledProcessError
from tempfile import NamedTemporaryFile
from crumbs.bam.flag import create_flag
from crumbs.settings import get_setting
from crumbs.utils.bin_utils import get_num_threads


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')
    intervals_fhand = NamedTemporaryFile(suffix='.intervals')
    stderr = NamedTemporaryFile(suffix='picard.stderr')
    stdout = NamedTemporaryFile(suffix='picard.stdout')
    cmd = ['java', '-jar', gatk_jar, '-T', 'RealignerTargetCreator',
           '-I', bam_fpath, '-R', reference_fpath, '-o', intervals_fhand.name]
    check_call(cmd, stderr=stderr, stdout=stdout)

    # the realignment itself
    cmd = ['java', '-jar', gatk_jar, '-I', bam_fpath, '-R', reference_fpath,
           '-T', 'IndelRealigner', '-targetIntervals', intervals_fhand.name,
           '-o', out_bam_fpath]

    if threads and threads > 1:

# 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/bam/bam_tools.py
import os.path
import shutil
import sys
import pysam
from subprocess import check_call, CalledProcessError
from tempfile import NamedTemporaryFile
from crumbs.bam.flag import create_flag
from crumbs.settings import get_setting
from crumbs.utils.bin_utils import get_num_threads



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')
    intervals_fhand = NamedTemporaryFile(suffix='.intervals')
    stderr = NamedTemporaryFile(suffix='picard.stderr')
    stdout = NamedTemporaryFile(suffix='picard.stdout')
    cmd = ['java', '-jar', gatk_jar, '-T', 'RealignerTargetCreator',
           '-I', bam_fpath, '-R', reference_fpath, '-o', intervals_fhand.name]
    check_call(cmd, stderr=stderr, stdout=stdout)

    # the realignment itself
    cmd = ['java', '-jar', gatk_jar, '-I', bam_fpath, '-R', reference_fpath,
           '-T', 'IndelRealigner', '-targetIntervals', intervals_fhand.name,
           '-o', out_bam_fpath]

    if threads and threads > 1:

        cmd.extend(['-nt', str(get_num_threads(threads))])