2017-04-01 33 views
1

我存儲在bed file在numpy的這樣提取特殊的數據:如何從如R numpy的

>>> t 
array([['chr1', '2488152', '2488153'], 
     ['chr1', '2488397', '2488398'], 
     ['chr1', '2491262', '2491417'], 
     ..., 
     ['chrX', '153628144', '153628282'], 
     ['chrX', '154292795', '154292796'], 
     ['chrX', '154294899', '154294900']], 
     dtype='|S9') 

通常情況下,我做這個工作有R

library(dplyr) 
filter(t, chrom=='chr1') 

我怎麼能得到相同的結果與numpy的?並有沒有更好的方法來存儲牀文件並提取我需要的特殊線?

感謝您的任何幫助。

+1

你可能想看看'pandas'。 – cel

+0

您可能也有興趣['pybedtools'](https://daler.github.io/pybedtools/)。 – bli

回答

0

t每一行的第一列比較一些值創建一個boolean mask,然後應用口罩t

>>> t[:,0] 
array([b'chr1', b'chr1', b'chr1', b'chrX', b'chrX', b'chrX'], 
     dtype='|S9') 
>>> mask = t[:,0]==b'chr1' 
>>> mask 
array([ True, True, True, False, False, False], dtype=bool) 
>>> t[mask] 
array([[b'chr1', b'2488152', b'2488153'], 
     [b'chr1', b'2488397', b'2488398'], 
     [b'chr1', b'2491262', b'2491417']], 
     dtype='|S9')