【问题标题】:Apply numpy.searchsorted method to an array loaded from textfile using numpy.loadtxt将 numpy.searchsorted 方法应用于使用 numpy.loadtxt 从文本文件加载的数组
【发布时间】:2017-10-03 19:54:58
【问题描述】:

我目前正在做一个生物信息学项目,我需要解决以下问题。

我有一个包含两列的文本文件“chr1.txt”:染色体上的位置和布尔变量 True 或 False。

0 错误
10000 对
10001 真
10005 错误
10007 真
10011 错误
10013 真
10017 错误
10019 错误
10023 错误
10025 真
10029 真
10031 错误
10035 真
10037 错误
....
该数据意味着从 0 到 10000 的区域是重复的或(=unmappable --> false),从 10000 到 10005 是唯一的(=mappable --> true),从 10005 到 10007 是重复的,依此类推。该文件在 248'946'406 位置结束,有 15'948'271 行。为了找到问题的一般解决方案,我想将文件限制为您可以在上面看到的行。

我想将此文本文件加载到由两列组成的 numpy 数组中。为此,我使用了 numpy.loadtxt:

import numpy as np    
with open('chr1.txt','r') as f:
        chr1 = np.loadtxt(f, dtype={'names':('start','mappable'),
        'formats':('i4','S1')})

这是输出:

In [39]: chr1
Out[39]: 
array([(0, b'f'), (10000, b't'), (10001, b't'), (10005, b'f'),
       (10007, b't'), (10011, b'f'), (10013, b't'), (10017, b'f'),
       (10019, b'f'), (10023, b'f'), (10025, b't'), (10029, b't'),
       (10031, b'f'), (10035, b't'), (10037, b'f')], 
      dtype=[('position start', '<i4'), ('mappable', 'S1')])

这对我来说并不完美,因为我希望将第二列识别为布尔类型,但我没有找到这样做的方法。

接下来我想在位置 10000 和 10037 之间扔一个随机数。

In [49]: np.random.randint(10000,10037)
Out[49]: 10012

现在我想将 numpy.searchsorted 方法应用于我的数组的第一列,以查明我的基因组是否可以唯一地映射到该位置。所以在这种情况下我想要的输出是 5(我的数组中元素 (10011, b'f') 的索引)。如果我尝试提取仅包含第一列 - 位置的数组,则会出现错误:

In [21]: chr1[:,0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-21-a63d052f1c5d> in <module>()
----> 1 chr1[:,0]

IndexError: too many indices for array

我猜这是因为我的数组实际上没有两列

In [40]: chr1.shape
Out[40]: (15,)

那么我怎样才能只提取位置并使用我现有的数组对其应用 searchsorted 方法?我是否应该以不同的方式将我的文本文件加载到一个数组中,以便真正有两列,第一列是整数类型,第二列是布尔值?

extracted_array=[0,10000,10001,10005,10007,10011,10013,10017,10019,10023,10025,10029,10031,10035,10037]
np.searchsorted(extracted_array,10012)-1
Out[58]: 5

然后我会用找到的索引查看第二个参数是真还是假,如果该位置在可映射区域内,则能够得出结论。

非常感谢您的帮助!

【问题讨论】:

    标签: python arrays numpy large-data


    【解决方案1】:

    我们可以使用chr1['position start'] 提取与position start 对应的数据,对于第二个字段也是如此。我们将通过与't' 进行比较来获得有效的布尔数组。

    因此,我们将采用一种方法,就像这样 -

    indx = chr1['position start']
    mask = chr1['mappable']=='t'
    rand_num = np.random.randint(10000,10037)
    matched_indx = np.searchsorted(indx, rand_num)-1
    
    if mask[matched_indx]:
        print "It is mappable!"
    else:
        print "It is NOT mappable!"
    

    1) 获取数据和掩码/布尔数组 -

    In [283]: chr1   # Input array
    Out[283]: 
    array([(    0, 'f'), (10000, 't'), (10001, 't'), (10005, 'f'),
           (10007, 't'), (10011, 'f'), (10013, 't'), (10017, 'f'),
           (10019, 'f'), (10023, 'f'), (10025, 't'), (10029, 't'),
           (10031, 'f'), (10035, 't'), (10037, 'f')], 
          dtype=[('position start', '<i4'), ('mappable', 'S1')])
    
    In [284]: indx = chr1['position start']
         ...: mask = chr1['mappable']=='t'
         ...: 
    
    In [285]: indx
    Out[285]: 
    array([    0, 10000, 10001, 10005, 10007, 10011, 10013, 10017, 10019,
           10023, 10025, 10029, 10031, 10035, 10037], dtype=int32)
    
    In [286]: mask
    Out[286]: 
    array([False,  True,  True, False,  True, False,  True, False, False,
           False,  True,  True, False,  True, False], dtype=bool)
    

    2) 获取一个随机数并使用searchsorted 并使用IF-ELSE 部分-

    In [297]: rand_num = 10012 # np.random.randint(10000,10037)
    
    In [298]: matched_indx = np.searchsorted(indx, rand_num)-1
    
    In [299]: matched_indx
    Out[299]: 5
    
    In [300]: if mask[matched_indx]:
         ...:     print "It is mappable!"
         ...: else:
         ...:     print "It is NOT mappable!"
         ...:     
    It is NOT mappable!
    

    【讨论】:

    • 谢谢!这太棒了:)我也会在我的大文件上测试它,然后再回来接受答案!
    猜你喜欢
    • 2021-08-03
    • 2021-09-12
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2016-12-28
    相关资源
    最近更新 更多