# 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/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/file_utils.py
# def fhand_is_seekable(fhand):
#     'It returns True if the fhand is seekable'
#     try:
#         try:
#             # The stdin stream in some instances has seek, has no seekable
#             fhand.tell()
#         except IOError:
#             return False
#         try:
#             if fhand.seekable():
#                 return True
#             else:
#                 return False
#         except AttributeError:
#             return True
#     except AttributeError:
#         return False
# 
# def peek_chunk_from_file(fhand, chunk_size):
#     'It returns the beginning of a file without moving the pointer'
#     if fhand_is_seekable(fhand):
#         fhand.seek(0)
#         chunk = fhand.read(chunk_size)
#         fhand.seek(0)
#     else:
#         chunk = fhand.peek(chunk_size)
#     return chunk
# 
# Path: crumbs/exceptions.py
# class UnknownFormatError(Exception):
#     'Raised when the format of a sequence file cannot be guessed'
#     pass
# 
# class UndecidedFastqVersionError(Exception):
#     'The file is Fastq, but the version is difficult to guess'
#     pass
# 
# class FileIsEmptyError(Exception):
#     pass


import cStringIO
import hashlib
from array import array
from crumbs.utils.optional_modules import FastqGeneralIterator
from crumbs.settings import get_setting
from crumbs.utils.file_utils import fhand_is_seekable, peek_chunk_from_file
from crumbs.exceptions import (UnknownFormatError, UndecidedFastqVersionError,
                               FileIsEmptyError)
    return lengths, None, chunk  # don't know if it's sanger


def _guess_fastq_version(fhand, force_file_as_non_seek):
    '''It guesses the format of fastq files.

    It ignores the solexa fastq version.
    '''
    lengths, is_sanger, chunk = _get_some_qual_and_lengths(fhand,
                                                        force_file_as_non_seek)
    if is_sanger:
        fmt = 'fastq'
    elif is_sanger is False:
        fmt = 'fastq-illumina'
    else:
        fmt = None
    if fmt:
        return fmt

    longest_expected_illumina = get_setting('LONGEST_EXPECTED_ILLUMINA_READ')
    n_long_seqs = [l for l in lengths if l > longest_expected_illumina]
    if n_long_seqs:
        msg = 'It was not possible to guess the format of '
        if hasattr(fhand, 'name'):
            msg += 'the file ' + fhand.name
        else:
            msg += 'a file '
        msg = '\n. The quality values could be Illumina, but there are '
        msg += 'sequences longer than %i bp.'
        msg %= longest_expected_illumina

# 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/settings.py
# def get_setting(key):
#     'It returns the value for one setting'
#     return _settings[key]
# 
# Path: crumbs/utils/file_utils.py
# def fhand_is_seekable(fhand):
#     'It returns True if the fhand is seekable'
#     try:
#         try:
#             # The stdin stream in some instances has seek, has no seekable
#             fhand.tell()
#         except IOError:
#             return False
#         try:
#             if fhand.seekable():
#                 return True
#             else:
#                 return False
#         except AttributeError:
#             return True
#     except AttributeError:
#         return False
# 
# def peek_chunk_from_file(fhand, chunk_size):
#     'It returns the beginning of a file without moving the pointer'
#     if fhand_is_seekable(fhand):
#         fhand.seek(0)
#         chunk = fhand.read(chunk_size)
#         fhand.seek(0)
#     else:
#         chunk = fhand.peek(chunk_size)
#     return chunk
# 
# Path: crumbs/exceptions.py
# class UnknownFormatError(Exception):
#     'Raised when the format of a sequence file cannot be guessed'
#     pass
# 
# class UndecidedFastqVersionError(Exception):
#     'The file is Fastq, but the version is difficult to guess'
#     pass
# 
# class FileIsEmptyError(Exception):
#     pass


# Path: crumbs/seq/utils/file_formats.py
import cStringIO
import hashlib
from array import array
from crumbs.utils.optional_modules import FastqGeneralIterator
from crumbs.settings import get_setting
from crumbs.utils.file_utils import fhand_is_seekable, peek_chunk_from_file
from crumbs.exceptions import (UnknownFormatError, UndecidedFastqVersionError,
                               FileIsEmptyError)

    return lengths, None, chunk  # don't know if it's sanger


def _guess_fastq_version(fhand, force_file_as_non_seek):
    '''It guesses the format of fastq files.

    It ignores the solexa fastq version.
    '''
    lengths, is_sanger, chunk = _get_some_qual_and_lengths(fhand,
                                                        force_file_as_non_seek)
    if is_sanger:
        fmt = 'fastq'
    elif is_sanger is False:
        fmt = 'fastq-illumina'
    else:
        fmt = None
    if fmt:
        return fmt

    longest_expected_illumina = get_setting('LONGEST_EXPECTED_ILLUMINA_READ')
    n_long_seqs = [l for l in lengths if l > longest_expected_illumina]
    if n_long_seqs:
        msg = 'It was not possible to guess the format of '
        if hasattr(fhand, 'name'):
            msg += 'the file ' + fhand.name
        else:
            msg += 'a file '
        msg = '\n. The quality values could be Illumina, but there are '
        msg += 'sequences longer than %i bp.'
        msg %= longest_expected_illumina

        raise UndecidedFastqVersionError(msg)