2016-03-03 104 views
0

我一直在使用pytabix來讀取.gz文件。 我不知道這有什麼錯我的代碼,因爲它以前工作:tabix.TabixError:查詢失敗

import tabix 

tb = tabix.open('qcat.gz') 
coor = "chr10:6000-30000" 
record = tb.querys(coor)             
for res in record:   
    print res 

我不斷收到此錯誤:

tabix.TabixError: query failed 

它似乎並不像一個.tbi索引文件正在取得。

回答

1

您將不得不在查詢實際運行之前運行tabix(bgzip,index)的先決條件。它們不包含在tb.query中。

如果你的文件已經拉上,你應該那麼做:

zippedf ='qcat.gz' 
def tabix_index(zippedf) 
    from subprocess import Popen,PIPE 
    import shlex 
    p = Popen(['tabix','-f', zippedf], stdout= PIPE) 
    # or : cmd = "tabix -f " + zippedf 
    # p = Popen(shlex.split(cmd), stdout=PIPE) 
    #(shlex splits the cmd in spaces) 
    p.wait() 

如果你有,你可以連續運行3子過程的非壓縮文件做整理,bgzip和索引:

out_sorted = 'myfile.sorted' 
out_zipped= out_sorted + ".gz" 

with open(out_zipped,'w') as sort_zip_out : 
    cmd="sort -V -k1,1 myfile" 
    p1 = Popen(shlex.split(cmd), stdout=PIPE) 
    p2 = Popen(['bgzip','-c','-f'], stdin=p1.stdout, stdout= sort_zip_out) 
    p1.stdout.close() #finish first subprocess before starting second 
    p1.wait() #wait for results to be written 

#when these two subprocesses are finished, 
tabix_index(out_zipped)