Exemple #1
0
def snpsA(seed, iid_count, sid_count, use_distributed):
    import numpy as np
    from pysnptools.snpreader import Bed
    from pysnptools.snpreader import DistributedBed
    from pysnptools.snpreader import SnpGen

    chrom_count = 10
    global top_cache
    if use_distributed:
        test_snp_path = (
            cache_top /
            f"snpsA_{seed}_{chrom_count}_{iid_count}_{sid_count}_db")
    else:
        test_snp_path = (
            cache_top /
            f"snpsA_{seed}_{chrom_count}_{iid_count}_{sid_count}.bed")
    count_A1 = False
    if not test_snp_path.exists():
        snpgen = SnpGen(
            seed=seed,
            iid_count=iid_count,
            sid_count=sid_count,
            chrom_count=chrom_count,
            block_size=1000,
        )
        if use_distributed:
            test_snps = DistributedBed.write(str(test_snp_path), snpgen)
        else:
            test_snps = Bed.write(str(test_snp_path),
                                  snpgen.read(dtype="float32"),
                                  count_A1=count_A1)
    else:
        if use_distributed:
            test_snps = DistributedBed(str(test_snp_path))
        else:
            test_snps = Bed(str(test_snp_path), count_A1=count_A1)
    from pysnptools.snpreader import SnpData

    np.random.seed(seed)
    pheno = SnpData(
        iid=test_snps.iid,
        sid=["pheno"],
        val=np.random.randn(test_snps.iid_count, 1) * 3 + 2,
    )
    covar = SnpData(
        iid=test_snps.iid,
        sid=["covar1", "covar2"],
        val=np.random.randn(test_snps.iid_count, 2) * 2 - 3,
    )

    return test_snps, pheno, covar
Exemple #2
0
        pstutil.create_directory_if_necessary(local, isfile=True)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)

    if False:
        from pysnptools.util.filecache import PeerToPeer, ip_address

        def id_and_path_function():
            ip = ip_address()
            return ip, 'peertopeer1/{0}'.format(ip)

        file_cache = PeerToPeer(common_directory='peertopeer1/common',
                                id_and_path_function=id_and_path_function)
        file_cache
        #PeerToPeer('peertopeer1/common',id_and_path_function=...')
        file_cache.rmtree()

        from pysnptools.snpreader import SnpGen, Dense
        snp_gen = SnpGen(seed=123, iid_count=1000, sid_count=5000)
        with file_cache.open_write(
                'r123.1000x5000.dense.txt') as local_filename:
            Dense.write(local_filename, snp_gen.read())
        list(file_cache.walk())
        #['r123.1000x5000.dense.txt']

    import doctest
    doctest.testmod(optionflags=doctest.ELLIPSIS)
    # There is also a unit test case in 'pysnptools\test.py' that calls this doc test