在SAM格式中,每条对齐线表示线段的线性对齐,每条线有11个必填字段,即QNAME、FLAG、RNAME、POS、MAPQ等。
假设我想要一个包含给定BAM文件中所有"QNAMES“的NumPy数组。或者,可以获取几列并将它们导入到Pandas Dataframe中。
使用pysam可以实现此功能吗?
用户可以很自然地使用pysam.AlignmentFile()打开给定的BAM文件,然后使用pysam.AlignmentSegment()访问各个段,例如
seg = AlignmentSegment()
print(seg.qname)但是,您可以将所有的QNAMES保存到NumPy数组中吗?
发布于 2019-12-13 06:16:18
是的,这是可行的。请注意,在使用pysam从BAM文件导入读取内容时,最好使用fetch()函数,该函数在BAM文件中的所有读取内容(pysam.AlignmentSegment()对象)上创建迭代器。然后使用query_name函数检索QNAME:
import pysam
import numpy as np
my_bam_file = '/path/to/your/bam_file.bam'
imported = pysam.AlignmentFile(my_bam_file, mode = 'rb')
bam_it = imported.fetch(until_eof = True)
# Use head(n) instead of fetch(), if you only want to retrieve the first 'n' reads
qnames = [read.query_name for read in bam_it]这里,qnames是BAM文件中所有QNAME的列表。如果您坚持要获取NumPy数组,只需在末尾添加以下行:
qnames = np.asarray(qnames)发布于 2020-05-13 13:42:20
# pip install pyranges
# or
# conda install -c bioconda pyranges
import pyranges
bam_df = pyranges.read_bam(f, sparse=False, as_df=True, mapq=0, required_flag=0, filter_flag=1540)https://stackoverflow.com/questions/38442987
复制相似问题