10X bam文件按barcode分割

最近遇到一個需求是將10X單細胞測序數據按照barcode分割,一般分割文件我們首先想到bamtools split,具體用法可以參考之前記錄過的bamtools分割bam文件,但是由于bamtools同時打開并記錄的文件數量有限制,所以用下面的分割方式會報memory error。

bamtools split -in tmp.bam -tag CB

因此,查了一下,有人提出了一種解決方案,即將bam文件按barcode排序,然后按相同的barcode將reads取出,代碼(轉自herrinca )如下:
原文鏈接:https://github.com/pezmaster31/bamtools/issues/135

samtools sort -t CB unsorted.bam -o sorted_tags.bam
##### Code has not been tested on unsorted bam files, sort on barcode (CB):
##### samtools sort -t CB unsorted.bam  -o sorted_tags.bam
###
##### INPUT: .bam file to be sorted and output directory to place split BC
##### OUTPUT: .bam file for each unique barcode, best to make a new directory

### Python 3.6.8
import pysam

### Input varibles to set
# file to split on
unsplit_file = "/path/to/dir/of/data/sorted_tags.bam"
# where to place output files
out_dir = "/path/to/dir/of/out_data/"

# variable to hold barcode index
CB_hold = 'unset'
itr = 0
# read in upsplit file and loop reads by line
samfile = pysam.AlignmentFile( unsplit_file, "rb")
for read in samfile.fetch( until_eof=True):
    # barcode itr for current read
    CB_itr = read.get_tag( 'CB')
    # if change in barcode or first line; open new file  
    if( CB_itr!=CB_hold or itr==0):
        # close previous split file, only if not first read in file
        if( itr!=0):
            split_file.close()
        CB_hold = CB_itr
        itr+=1
        split_file = pysam.AlignmentFile( out_dir + "CB_{}.bam".format( itr), "wb", template=samfile)

    # write read with same barcode to file
    split_file.write( read)
split_file.close()
samfile.close()

歡迎關注~


公眾號二維碼.jpg
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容