# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))
# 
# Path: crumbs/utils/file_utils.py
# def compress_with_bgzip(in_fhand, compressed_fhand):
#     '''It compresses the input fhand.
# 
#     The input fhand has to be a real file with a name.
#     '''
#     cmd = ['bgzip', '-c', in_fhand.name]
#     check_call(cmd, stdout=compressed_fhand)
# 
# def uncompress_gzip(in_fhand, uncompressed_fhand):
#     '''It compresses the input fhand.
# 
#     The input fhand has to be a real file with a name.
#     '''
#     cmd = ['gzip', '-dc', in_fhand.name]
#     check_call(cmd, stdout=uncompressed_fhand)
# 
# 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 wrap_in_buffered_reader(fhand, force_wrap=False,
#                             buffering=DEF_FILE_BUFFER):
#     '''It wraps the given file in a peekable BufferedReader.
# 
#     If the file is seekable it doesn't do anything.
#     '''
#     if not force_wrap and fhand_is_seekable(fhand):
#         return fhand
#     else:
#         fhand = io.open(fhand.fileno(), mode='rb',
#                         buffering=buffering)  # with text there's no peek
# 
#     return fhand
# 
# def index_vcf_with_tabix(in_fpath):
#     cmd = ['tabix', '-p', 'vcf', in_fpath]
#     tabix = Popen(cmd, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = tabix.communicate()
#     if tabix.returncode:
#         msg = 'Some problem indexing with tabix.\n'
#         msg += 'stdout: ' + stdout
#         msg += 'stderr: ' + stderr
#         raise RuntimeError(msg)


import unittest
import gzip
from tempfile import NamedTemporaryFile
from os.path import exists
from os.path import join as pjoin
from os import remove
from StringIO import StringIO
from crumbs.utils.test_utils import TEST_DATA_DIR
from crumbs.utils.file_utils import (compress_with_bgzip, uncompress_gzip,
                                     fhand_is_seekable,
                                     wrap_in_buffered_reader,
                                     index_vcf_with_tabix)
##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral_Allele">
##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership,build_129">
##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2_membership">
##FILTER=<ID=q10,Description="Quality_below_10">
##FILTER=<ID=s50,Description="Less_than_50%_of_samples_have_data">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype_Quality">
##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read_Depth">
##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype_Quality">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001 NA00002 NA00003
20 14370 rs6054257 G A 29 PASS NS=3;DP=14;AF=0.5;DB;H2 GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5:.,.
20 17330 . T A 3 q10 NS=3;DP=11;AF=0.017 GT:GQ:DP:HQ 0|0:49:3:58,50 0|1:3:5:65,3 0/0:41:3
'''


class SeekableFileTest(unittest.TestCase):

    def test_is_seekable(self):
        'It tests wether the fhands are seekable or not'

        # StringIO
        fhand = StringIO('hola')
        assert fhand_is_seekable(fhand)

        # standard file
        fhand = NamedTemporaryFile()
        fhand.seek(0)
        assert fhand_is_seekable(fhand)

        # a wrapped BufferedReader

# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))
# 
# Path: crumbs/utils/file_utils.py
# def compress_with_bgzip(in_fhand, compressed_fhand):
#     '''It compresses the input fhand.
# 
#     The input fhand has to be a real file with a name.
#     '''
#     cmd = ['bgzip', '-c', in_fhand.name]
#     check_call(cmd, stdout=compressed_fhand)
# 
# def uncompress_gzip(in_fhand, uncompressed_fhand):
#     '''It compresses the input fhand.
# 
#     The input fhand has to be a real file with a name.
#     '''
#     cmd = ['gzip', '-dc', in_fhand.name]
#     check_call(cmd, stdout=uncompressed_fhand)
# 
# 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 wrap_in_buffered_reader(fhand, force_wrap=False,
#                             buffering=DEF_FILE_BUFFER):
#     '''It wraps the given file in a peekable BufferedReader.
# 
#     If the file is seekable it doesn't do anything.
#     '''
#     if not force_wrap and fhand_is_seekable(fhand):
#         return fhand
#     else:
#         fhand = io.open(fhand.fileno(), mode='rb',
#                         buffering=buffering)  # with text there's no peek
# 
#     return fhand
# 
# def index_vcf_with_tabix(in_fpath):
#     cmd = ['tabix', '-p', 'vcf', in_fpath]
#     tabix = Popen(cmd, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = tabix.communicate()
#     if tabix.returncode:
#         msg = 'Some problem indexing with tabix.\n'
#         msg += 'stdout: ' + stdout
#         msg += 'stderr: ' + stderr
#         raise RuntimeError(msg)


# Path: test/test_utils.py
import unittest
import gzip
from tempfile import NamedTemporaryFile
from os.path import exists
from os.path import join as pjoin
from os import remove
from StringIO import StringIO
from crumbs.utils.test_utils import TEST_DATA_DIR
from crumbs.utils.file_utils import (compress_with_bgzip, uncompress_gzip,
                                     fhand_is_seekable,
                                     wrap_in_buffered_reader,
                                     index_vcf_with_tabix)

##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral_Allele">
##INFO=<ID=DB,Number=0,Type=Flag,Description="dbSNP membership,build_129">
##INFO=<ID=H2,Number=0,Type=Flag,Description="HapMap2_membership">
##FILTER=<ID=q10,Description="Quality_below_10">
##FILTER=<ID=s50,Description="Less_than_50%_of_samples_have_data">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=GQ,Number=1,Type=Integer,Description="Genotype_Quality">
##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read_Depth">
##FORMAT=<ID=HQ,Number=2,Type=Integer,Description="Haplotype_Quality">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT NA00001 NA00002 NA00003
20 14370 rs6054257 G A 29 PASS NS=3;DP=14;AF=0.5;DB;H2 GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5:.,.
20 17330 . T A 3 q10 NS=3;DP=11;AF=0.017 GT:GQ:DP:HQ 0|0:49:3:58,50 0|1:3:5:65,3 0/0:41:3
'''


class SeekableFileTest(unittest.TestCase):

    def test_is_seekable(self):
        'It tests wether the fhands are seekable or not'

        # StringIO
        fhand = StringIO('hola')
        assert fhand_is_seekable(fhand)

        # standard file
        fhand = NamedTemporaryFile()
        fhand.seek(0)
        assert fhand_is_seekable(fhand)

        # a wrapped BufferedReader

        fhand2 = wrap_in_buffered_reader(fhand)