# 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)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ngs_crumbs. If not, see <http://www.gnu.org/licenses/>.



# pylint: disable=C0111


def filter_bam(in_fpath, out_fpath, min_mapq=0, required_flag_tags=None,
               filtering_flag_tags=None, regions=None):
    cmd = ['-bh']

    # The following line:
    cmd.append('-o' + out_fpath)
    # should be
    # cmd.extend(['-o', out_fpath])
    # but it is a workaround, take a look at:
    # https://groups.google.com/forum/#!msg/pysam-user-group/ooHgIiNVe4c/CcY06d45rzQJ

    if min_mapq:
        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:

# 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)

# MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ngs_crumbs. If not, see <http://www.gnu.org/licenses/>.



# pylint: disable=C0111


def filter_bam(in_fpath, out_fpath, min_mapq=0, required_flag_tags=None,
               filtering_flag_tags=None, regions=None):
    cmd = ['-bh']

    # The following line:
    cmd.append('-o' + out_fpath)
    # should be
    # cmd.extend(['-o', out_fpath])
    # but it is a workaround, take a look at:
    # https://groups.google.com/forum/#!msg/pysam-user-group/ooHgIiNVe4c/CcY06d45rzQJ

    if min_mapq:
        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)