Пример #1
0
def test_liftover_chain_file():
    '''
    This is just a smoke test. (NB: it loads actual hg17-to-hg18.over.chain, attempting to download it to cache).
    '''
    # Simple example
    data = example_1 + example_2
    chains = LiftOverChainFile._load_chains(StringIO(data))
    assert len(chains) == 2
    index = LiftOverChainFile._index_chains(chains)
    assert len(index) == 1
    assert len(index['chrY']) == 9+4
    assert len(index['chrY'].query(25985406)) == 2
    assert len(index['chrY'].query(25985403)) == 1
    assert len(index['chrY'].query(25985402)) == 0
    assert len(index['chrY'].query(25985405)) == 1
    assert index['chrY'].query(25985405)[0][2][2] == chains[0]
    cf = LiftOverChainFile(StringIO(data))
    assert len(cf.query('chrY', 25985406)) == 2
    assert len(cf.query('chrY', 25985403)) == 1
    assert len(cf.query('chrY', 25985402)) == 0
    assert len(cf.query('chrY', 25985405)) == 1
    assert cf.query('chrY', 25985405)[0][2][2] == cf.chains[0]
    assert cf.query('chrZ', 25985405) is None
    
    # hg17-to-hg18 example
    f = open_liftover_chain_file('hg17', 'hg18')
    chains = LiftOverChainFile._load_chains(f)
    assert len(chains) > 1000
    f.close()
    index = LiftOverChainFile._index_chains(chains)
    assert len(index) >= 22
Пример #2
0
def test_liftover_chain_file():
    '''
    This is just a smoke test. (NB: it loads actual hg17-to-hg18.over.chain, attempting to download it to cache).
    '''
    # Simple example
    data = example_1 + example_2
    chains = LiftOverChainFile._load_chains(StringIO(data))
    assert len(chains) == 2
    index = LiftOverChainFile._index_chains(chains)
    assert len(index) == 1
    assert len(index['chrY']) == 9 + 4
    assert len(index['chrY'].query(25985406)) == 2
    assert len(index['chrY'].query(25985403)) == 1
    assert len(index['chrY'].query(25985402)) == 0
    assert len(index['chrY'].query(25985405)) == 1
    assert index['chrY'].query(25985405)[0][2][2] == chains[0]
    cf = LiftOverChainFile(StringIO(data))
    assert len(cf.query('chrY', 25985406)) == 2
    assert len(cf.query('chrY', 25985403)) == 1
    assert len(cf.query('chrY', 25985402)) == 0
    assert len(cf.query('chrY', 25985405)) == 1
    assert cf.query('chrY', 25985405)[0][2][2] == cf.chains[0]
    assert cf.query('chrZ', 25985405) is None

    # hg17-to-hg18 example
    f = open_liftover_chain_file('hg17', 'hg18')
    chains = LiftOverChainFile._load_chains(f)
    assert len(chains) > 1000
    f.close()
    index = LiftOverChainFile._index_chains(chains)
    assert len(index) >= 22
def test_open_liftover_chain_file():
    if SKIP_TEST:
        return
    # No-web, no-cache, must return none
    f = open_liftover_chain_file(from_db, to_db, search_dir=None, cache_dir=None, use_web=False, write_cache=False)
    assert f is None
    assert not os.path.exists(cache_dir)
    
    # No-web, cache, nonexistent search dir, must return none
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=False, write_cache=True)
    assert f is None
    assert not os.path.exists(cache_dir)

    # Web, no write cache, nonexistent search dir, must open temp file
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=True, write_cache=False)
    assert f is not None
    assert not os.path.exists(cache_dir)
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()
    os.unlink(f.name)
    
    # Web, nonexistent db. Must not modify cache.
    f = open_liftover_chain_file(from_db, 'blablabla', search_dir=search_dir, cache_dir=cache_dir, use_web=True, write_cache=True)
    assert f is None
    assert not os.path.exists(cache_dir)
    
    # Web, write cache, nonexistent search dir, must save to cache, creating cache dir
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=True, write_cache=True)
    assert f is not None
    assert os.path.exists(cache_dir)
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()

    # No web, file in cache, nonexistent search dir, must exist
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=False, write_cache=True)
    assert f is not None
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()
    
    # No web, no file in cache, nonexistent db
    f = open_liftover_chain_file(from_db, 'blablabla', search_dir=search_dir, cache_dir=cache_dir, use_web=False, write_cache=True)
    assert f is None
    
    os.mkdir(search_dir)
    # File both in cache and search_dir. Assert search_dir is used and uncompressed file is preferred
    filename = '%sTo%s.over.chain' % (from_db, to_db[0].upper() + to_db[1:])
    filename = os.path.join(search_dir, filename)
    fout = open(filename, 'wb')
    fout.write(b'test-test')
    fout.close()
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=True, write_cache=True)
    assert f is not None
    assert f.readline() == b'test-test'
    f.close()
    
    # gzipped file is preferred
    fout = gzip.open(filename + '.gz', 'w')
    fout.write(b'test-test-gzip')
    fout.close()
    f = open_liftover_chain_file(from_db, to_db, search_dir=search_dir, cache_dir=cache_dir, use_web=True, write_cache=True)
    assert f is not None
    assert f.readline() == b'test-test-gzip'
    f.close()
    os.unlink(filename)
    os.unlink(filename + '.gz')
def test_open_liftover_chain_file():
    if SKIP_TEST:
        return
    # No-web, no-cache, must return none
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=None,
                                 cache_dir=None,
                                 use_web=False,
                                 write_cache=False)
    assert f is None
    assert not os.path.exists(cache_dir)

    # No-web, cache, nonexistent search dir, must return none
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=False,
                                 write_cache=True)
    assert f is None
    assert not os.path.exists(cache_dir)

    # Web, no write cache, nonexistent search dir, must open temp file
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=True,
                                 write_cache=False)
    assert f is not None
    assert not os.path.exists(cache_dir)
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()
    os.unlink(f.name)

    # Web, nonexistent db. Must not modify cache.
    f = open_liftover_chain_file(from_db,
                                 'blablabla',
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=True,
                                 write_cache=True)
    assert f is None
    assert not os.path.exists(cache_dir)

    # Web, write cache, nonexistent search dir, must save to cache, creating cache dir
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=True,
                                 write_cache=True)
    assert f is not None
    assert os.path.exists(cache_dir)
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()

    # No web, file in cache, nonexistent search dir, must exist
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=False,
                                 write_cache=True)
    assert f is not None
    ln = f.readline()
    assert ln.startswith(b'##matrix')
    f.close()

    # No web, no file in cache, nonexistent db
    f = open_liftover_chain_file(from_db,
                                 'blablabla',
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=False,
                                 write_cache=True)
    assert f is None

    os.mkdir(search_dir)
    # File both in cache and search_dir. Assert search_dir is used and uncompressed file is preferred
    filename = '%sTo%s.over.chain' % (from_db, to_db[0].upper() + to_db[1:])
    filename = os.path.join(search_dir, filename)
    fout = open(filename, 'wb')
    fout.write(b'test-test')
    fout.close()
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=True,
                                 write_cache=True)
    assert f is not None
    assert f.readline() == b'test-test'
    f.close()

    # gzipped file is preferred
    fout = gzip.open(filename + '.gz', 'w')
    fout.write(b'test-test-gzip')
    fout.close()
    f = open_liftover_chain_file(from_db,
                                 to_db,
                                 search_dir=search_dir,
                                 cache_dir=cache_dir,
                                 use_web=True,
                                 write_cache=True)
    assert f is not None
    assert f.readline() == b'test-test-gzip'
    f.close()
    os.unlink(filename)
    os.unlink(filename + '.gz')