将 numpy.searchsorted 方法应用于使用 numpy.loadtxt 从文本文件加载的数组
Apply numpy.searchsorted method to an array loaded from textfile using numpy.loadtxt
我目前在做一个生物信息学项目,需要解决以下问题
我有一个包含两列的文本文件 "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
然后我将使用找到的索引查看第二个参数是真还是假,如果该位置在可映射区域内则能够得出结论。
非常感谢您的帮助!
我们可以用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) 获取数据和mask/boolean数组-
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!
我目前在做一个生物信息学项目,需要解决以下问题
我有一个包含两列的文本文件 "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
然后我将使用找到的索引查看第二个参数是真还是假,如果该位置在可映射区域内则能够得出结论。
非常感谢您的帮助!
我们可以用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) 获取数据和mask/boolean数组-
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!