Beispiel #1
0
    def test_compressed_output(self):
        'It writes a gziped file'
        in_fhand = self.make_fasta()
        cat_bin = os.path.join(BIN_DIR, 'cat_seqs')

        # bgzf does not work with STDOUT
        try:
            stderr = NamedTemporaryFile()
            check_output([cat_bin, '-Z', in_fhand.name], stderr=stderr)
            self.fail('CalledProcessError expected')
        except CalledProcessError:
            msg = 'bgzf is only available for seekable files'
            assert msg in open(stderr.name).read()

        # bgzf
        out_fhand = NamedTemporaryFile()
        check_output([cat_bin, '-Z', '-o', out_fhand.name, in_fhand.name])
        result = GzipFile(out_fhand.name).read()
        assert '\nACTATCATGGCAGATA\n' in result

        # gzip
        result = check_output([cat_bin, '-z', in_fhand.name])
        result = GzipFile(fileobj=StringIO(result)).read()
        assert '\nACTATCATGGCAGATA\n' in result

        # bzip2
        result = check_output([cat_bin, '-B', in_fhand.name])
        result = BZ2File(StringIO(result)).read()
        assert '\nACTATCATGGCAGATA\n' in result
Beispiel #2
0
    def test_bzipped2_input(self):
        # we need a compressed file
        in_fhand = self.make_fasta()
        cat_bin = os.path.join(BIN_DIR, 'cat_seqs')
        out_fhand = NamedTemporaryFile()
        check_output([cat_bin, '-B', '-o', out_fhand.name, in_fhand.name])
        result = BZ2File(out_fhand.name).read()
        assert '\nACTATCATGGCAGATA\n' in result

        result = check_output([cat_bin, out_fhand.name])
        assert '>seq1\nACTATCATGGCAGATA\n' in result
Beispiel #3
0
def uncompress_if_required(fhand):
    'It returns a uncompressed handle if required'
    magic = peek_chunk_from_file(fhand, 2)
    if magic == '\037\213':
        fhand = GzipFile(fileobj=fhand)
    elif magic == 'BZ':
        try:
            fhand = BZ2File(fhand)
        except NameError:
            raise OptionalRequirementError(BZIP_ERROR)
    return fhand
Beispiel #4
0
def compress_fhand(fhand, compression_kind=None):
    'Compresses the file if required'
    if compression_kind == BGZF:
        if fhand_is_seekable(fhand):
            fhand = BgzfWriter(fileobj=fhand)
        else:
            raise RuntimeError('bgzf is only available for seekable files')
    elif compression_kind == GZIP:
        fhand = GzipFile(fileobj=fhand)
    elif compression_kind == BZIP2:
        mode = 'w' if 'w' in fhand.mode else 'r'
        try:
            fhand = BZ2File(fhand, mode=mode)
        except NameError:
            raise OptionalRequirementError(BZIP_ERROR)
    return fhand