# 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
        cmd.extend(['-q', str(min_mapq)])

    if required_flag_tags:
        flag = create_flag(required_flag_tags)
        cmd.extend(['-f', str(flag)])

    if filtering_flag_tags:
        flag = create_flag(filtering_flag_tags)
        cmd.extend(['-F', str(flag)])

    cmd.extend([in_fpath])

    if regions:
        regions = ['{0}:{1}-{2}'.format(*s) for s in regions.segments]
        cmd.extend(regions)

    pysam.view(*cmd)


def sort_bam(in_bam_fpath, out_bam_fpath=None):

    if out_bam_fpath is None:
        out_bam_fpath = in_bam_fpath

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


# 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

        cmd.extend(['-q', str(min_mapq)])

    if required_flag_tags:
        flag = create_flag(required_flag_tags)
        cmd.extend(['-f', str(flag)])

    if filtering_flag_tags:
        flag = create_flag(filtering_flag_tags)
        cmd.extend(['-F', str(flag)])

    cmd.extend([in_fpath])

    if regions:
        regions = ['{0}:{1}-{2}'.format(*s) for s in regions.segments]
        cmd.extend(regions)

    pysam.view(*cmd)


def sort_bam(in_bam_fpath, out_bam_fpath=None):

    if out_bam_fpath is None:
        out_bam_fpath = in_bam_fpath

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


    picard_jar = get_setting("PICARD_JAR")