【问题标题】:IndexError when assigning variables to Neo4jRestClient将变量分配给 Neo4jRestClient 时出现 IndexError
【发布时间】:2019-01-06 17:56:30
【问题描述】:

我正在尝试访问SciKit Allele 中的基因组数据,这是一种用于基因组数据的工具,基于 Numpy。

我对 python 不是很好,但我试图遍历每个变体并提取数组中的相关列,然后使用 Neo4j Rest Client 在 Neo4j 数据库中创建节点。

下面的代码生成一个包含所有变体和所有数据类型的数组:

variants = allel.VariantChunkedTable(callset[chrom]['variants'], names=['AC','AF_AFR', 'AF_AMR', 'AF_ASN', 'AF_EUR', 'AF_MAX', 'CGT', 'CLR', 'CSQ', 'DP', 'DP4', 'ESP_MAF', 'FILTER_LowQual', 'FILTER_MinHWE', 'FILTER_MinVQSLOD', 'FILTER_PASS', 'HWE', 'ICF', 'ID', 'IS', 'PC2', 'PCHI2', 'POS', 'PR', 'QCHI2', 'QUAL', 'REF', 'ALT', 'INDEL', 'SHAPEIT', 'SNP_ID', 'TYPE', 'UGT', 'VQSLOD', 'dbSNPmismatch', 'is_snp', 'numalt', 'svlen'], index='POS')

我(我想我)已将变量声明为数组形式,如下所示:

pos = variants['POS'][:]
alt = variants['ALT'][:]
dp = variants['DP'][:]
ac = variants['AC'][:]
type = variants['TYPE'][:]
svlen = variants['svlen'][:]
qual = variants['QUAL'][:]
vq = variants['VQSLOD'][:]

这些变量创建数组,例如:

In: pos
Out: array([    28590,     50481,     52152, ..., 249225077, 249229702,
       249231222], dtype=int32)

我现在正尝试访问每一行的变量,但似乎不知道如何去做。我当前的尝试如下所示(前 10 行):

for variant in variants[0:10]:
    a1 = db.nodes.create(pos=pos[variant], bp=alt[variant][0], DP=dp[variant], AC=ac[variant][0], type=type[variant][0], svlen=svlen[variant][0], qual=qual[variant], vqslod=vq[variant])
    a1.relationships.create("belongs_to", c1)

不幸的是,这会出现以下错误:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

谁能帮我弄清楚如何获取每个属性的特定变量?

【问题讨论】:

    标签: python numpy variables index-error neo4jrestclient


    【解决方案1】:

    由于没有可重现的示例,我无法遵循您的代码,因此我不得不根据 scikit-allel 文档创建一个:

    https://scikit-allel.readthedocs.io/en/stable/model/chunked.html#variantchunkedtable

    import h5py
    import allel
    import os
    
    #cleanup
    h5_file = 'callset.h5'
    os.remove(h5_file) if os.path.exists(h5_file) else None
    
    chrom = [b'chr1', b'chr1', b'chr2', b'chr2', b'chr3', b'chr3']
    pos = [2, 7, 3, 9, 6, 11]
    dp = [35, 12, 78, 22, 99, 96]
    qd = [4.5, 6.7, 1.2, 4.4, 2.8, 3.2]
    ac = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12)]
    
    with h5py.File(h5_file, mode='w') as h5f:
        h5g = h5f.create_group('/3L/variants')
        h5g.create_dataset('CHROM', data=chrom, chunks=True)
        h5g.create_dataset('POS', data=pos, chunks=True)
        h5g.create_dataset('DP', data=dp, chunks=True)
        h5g.create_dataset('QD', data=qd, chunks=True)
        h5g.create_dataset('AC', data=ac, chunks=True)
    
    
    callset = h5py.File(h5_file, mode='r')
    variants = allel.VariantChunkedTable(callset['/3L/variants'],
                                    names=['CHROM', 'POS', 'AC', 'QD', 'DP'])
    

    所以,variants 变量(在本例中只有 6 行)看起来像这样:

    >>> variants
    <VariantChunkedTable shape=(6,) dtype=[('CHROM', 'S4'), ('POS', '<i8'), ('AC', '<i8', (2,)), ('QD', '<f8'), ('DP', '<i8')] 
    nbytes=264 cbytes=264 cratio=1.0 values=h5py._hl.group.Group>
        CHROM   POS AC      QD  DP
    0   b'chr1' 2   [1 2]   4.5 35
    1   b'chr1' 7   [3 4]   6.7 12
    2   b'chr2' 3   [5 6]   1.2 78
    3   b'chr2' 9   [7 8]   4.4 22
    4   b'chr3' 6   [ 9 10] 2.8 99
    5   b'chr3' 11  [11 12] 3.2 96
    

    您已正确定义了posaltdp 等数组(即pos = variants['POS'][:] 等)

    然后,在您的循环中,我假设您的目标是遍历 variants 变量的前 10 行,从每一行获取一些值,例如pos[variant]ac[variant][0]dp[variant] 并使用这些属性在 GraphDatabase 中创建一个新节点。

    按照您当前编写循环的方式,您在每次迭代时从variants 获得一整行,并尝试将其用作索引以访问来自posalt、...的元素数组,这会引发错误。

    正确的做法是遍历数字索引;在我的示例中,要遍历 variants 变量的所有 6 行,您应该运行:

    for i in range(len(variants)):
        print(f"> Row {i}")
        print(pos[i])
        print(dp[i])
        print(ac[i][0])
    

    然后可以将 pos[i]、dp[i] 等值以name=value 对的形式提供给db.nodes.create。 当然,对于前 10 行,您只需要使用 for i in range(10)

    【讨论】:

    • 谢谢,我知道这是一个相当简单的问题。我对共享任何 VCF 数据有点怀疑,即使这样的数据可以忽略不计。
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多