<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[使用pysam处理bam文件]]></title><description><![CDATA[<h2>安装依赖</h2>
<p dir="auto"><code>pip install pysam==0.19.1</code></p>
<h2>使用示例及说明</h2>
<pre><code class="language-python"># 读取文件
with pysam.AlignmentFile(file, mode) as reader:
    for r in reader:
        # 可以通过循环遍历bam中的每一行
        print(r) # r中包含很多属性,具体可以通过调试查看
</code></pre>
<p dir="auto"><strong>注意</strong>:</p>
<ul>
<li>当文件为 .bam 文件时, mode= "rb"</li>
<li>当文件为 .sam 文件时, mode= "r"</li>
<li>当文件为 CRAM 文件时, mode= "rc"</li>
</ul>
<pre><code class="language-python"># 当文件包含索引时,可以读取特定区域
for r in reader.fetch('chrM', 300, 310)：
    print(r)
</code></pre>
<pre><code class="language-python"># 获取比对到指定碱基的结果,且碱基质量&gt;30
for pileupcolumn in reader.pileup("chrM", 300, 301):
    for read in [al for al in pileupcolumn.pileups if al.alignment.mapq&gt;30]:
        if not read.is_del and not read.is_refskip:
            if read.alignment.pos + 1 == 301:
                print(read.alignment.reference_name,\
                      read.alignment.pos + 1,\
                    read.alignment.query_sequence[read.query_position])
</code></pre>
<p dir="auto"><strong>注意</strong>:</p>
<ul>
<li>pysam中碱基位置都是从0开始计算</li>
<li>read.alignment与前述r是相同的对象实例</li>
</ul>
]]></description><link>http://an.forum.genostack.com/topic/772/使用pysam处理bam文件</link><generator>RSS for Node</generator><lastBuildDate>Sat, 13 Jun 2026 12:31:11 GMT</lastBuildDate><atom:link href="http://an.forum.genostack.com/topic/772.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 28 Oct 2022 09:12:18 GMT</pubDate><ttl>60</ttl></channel></rss>