# Path: bipy/utils.py
# def append_stem(filename, word, delim="."):
#     """ returns a filename with word appended to the stem
#     example: /path/to/test.run.sam -> /path/to/test.run.sorted.sam """
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)
#     fsplit.insert(len(fsplit) - 1, word)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# Path: bipy/toolbox/fastqc.py
# def detect_fastq_format(in_file, MAX_RECORDS=1000000):
#     """
#     detects the format of a fastq file
#     will return multiple formats if it could be more than one
#     """
#     logger.info("Detecting FASTQ format on %s." % (in_file))
#     kept = list(_FASTQ_RANGES.keys())
#     with open(in_file) as in_handle:
#         records_read = 0
#         for i, line in enumerate(in_handle):
#             # get the quality line
#             if records_read >= MAX_RECORDS:
#                 break
#             if i % 4 is 3:
#                 records_read += 1
#                 for c in line:
#                     formats = kept
#                     if len(formats) == 1:
#                         return formats
#                     for form in formats:
#                         if (_FASTQ_RANGES[form][0] > ord(c) or
#                             _FASTQ_RANGES[form][1] < ord(c)):
#                             kept.remove(form)
# 
#     return formats


import subprocess
import os
import sh
import logging
from bipy.utils import append_stem
from bipy.toolbox.fastqc import detect_fastq_format

logger = logging.getLogger("bipy")

_FASTQ_TYPE_TO_FLAG = {"sanger": "sanger",
                       "illumina_1.3+": "illumina",
                       "illumina_1.5+": "illumina",
                       "illumina_1.8+": "sanger",
                       "solexa": "solexa"}


def _get_quality_type(in_file):
    """ get fastq quality format. if multiple types are detected,
    pick the first one. no quality type is found assume sanger """

# Path: bipy/utils.py
# def append_stem(filename, word, delim="."):
#     """ returns a filename with word appended to the stem
#     example: /path/to/test.run.sam -> /path/to/test.run.sorted.sam """
#     dirname = os.path.dirname(filename)
#     filename = os.path.basename(filename)
#     fsplit = filename.split(delim)
#     fsplit.insert(len(fsplit) - 1, word)
#     return os.path.join(dirname, delim.join(fsplit))
# 
# Path: bipy/toolbox/fastqc.py
# def detect_fastq_format(in_file, MAX_RECORDS=1000000):
#     """
#     detects the format of a fastq file
#     will return multiple formats if it could be more than one
#     """
#     logger.info("Detecting FASTQ format on %s." % (in_file))
#     kept = list(_FASTQ_RANGES.keys())
#     with open(in_file) as in_handle:
#         records_read = 0
#         for i, line in enumerate(in_handle):
#             # get the quality line
#             if records_read >= MAX_RECORDS:
#                 break
#             if i % 4 is 3:
#                 records_read += 1
#                 for c in line:
#                     formats = kept
#                     if len(formats) == 1:
#                         return formats
#                     for form in formats:
#                         if (_FASTQ_RANGES[form][0] > ord(c) or
#                             _FASTQ_RANGES[form][1] < ord(c)):
#                             kept.remove(form)
# 
#     return formats


# Path: bipy/toolbox/sickle.py
import subprocess
import os
import sh
import logging
from bipy.utils import append_stem
from bipy.toolbox.fastqc import detect_fastq_format


logger = logging.getLogger("bipy")

_FASTQ_TYPE_TO_FLAG = {"sanger": "sanger",
                       "illumina_1.3+": "illumina",
                       "illumina_1.5+": "illumina",
                       "illumina_1.8+": "sanger",
                       "solexa": "solexa"}


def _get_quality_type(in_file):
    """ get fastq quality format. if multiple types are detected,
    pick the first one. no quality type is found assume sanger """

    fastq_format = detect_fastq_format(in_file)