Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pysamstats

> 编程语言
Open source

A fast Python and command-line utility for extracting simple statistics against genome positions based on sequence alignments from a SAM or BAM file.

196 stars0 likes0 views
WebsiteGitHub

About

A fast Python and command-line utility for extracting simple statistics against genome positions based on sequence alignments from a SAM or BAM file.

pysamstats

A Python utility for calculating statistics against genome positions based on sequence alignments from a SAM or BAM file.

  • Source: https://github.com/alimanfoo/pysamstats
  • Download: http://pypi.python.org/pypi/pysamstats
  • Release notes: https://github.com/alimanfoo/pysamstats/releases

Installation

The easiest way to install pysamstats is via conda, e.g.:

$ conda install -c bioconda pysamstats

Alternatively, pysamstats can be installed from source via pip.

$ pip install pysamstats

Alternatively, clone the git repo and install:

$ git clone git://github.com/alimanfoo/pysamstats.git
$ cd pysamstats
$ python setup.py install
$ nosetests -v  # optional, run test suite

If you have problems installing pysam, please email the pysam user group.

N.B., some functions also require numpy and pytables to be installed.

Usage

From the command line:

…

From Python:

python
import pysam
import pysamstats

mybam = pysam.AlignmentFile('/path/to/your/bamfile.bam')

# iterate over statistics, one record at a time
for rec in pysamstats.stat_coverage(mybam, chrom='Pf3D7_01_v3', start=10000, end=20000):
    print rec['chrom'], rec['pos'], rec['reads_all'], rec['reads_pp']
    ...

For convenience, functions are provided for loading data directly into numpy arrays, e.g.:

python
import pysam
import pysamstats
import matplotlib.pyplot as plt

mybam = pysam.AlignmentFile('/path/to/your/bamfile.bam')
a = pysamstats.load_coverage(mybam, chrom='Pf3D7_01_v3', start=10000, end=20000)
plt.plot(a.pos, a.reads_all)
plt.show()

For pileup-based statistics functions, note the following:

  • By default a row is emitted for all genome positions covered by reads overlapping the selected region. This means rows will be emitted for positions outside the selected region, but statistics may not be accurate as not all reads overlapping that position will have been counted. To truncate output to exactly the selected region, provide a truncate=True keyword argument.
  • By default a row is only emitted for genome positions covered by at least one read. To emit a row for every genome position, provide a pad=True keyword argument.
  • By default the number of reads in a pileup column is limited to 8000. To increase this limit, provide a max_depth=100000 keyword argument (or whatever number is suitable for your situation).

Field definitions

The suffix _fwd means the field is restricted to reads mapped to the forward strand, and _rev means the field is restricted to reads mapped to the reverse strand. E.g., reads_fwd means the number of reads mapped to the forward strand.

The suffix _pp means the field is restricted to reads flagged as properly paired.

  • chrom - Chromosome name.

  • pos - Position within chromosome. One-based by default when using the command line, zero-based by default when using the python API.

  • reads_all - Number of reads aligned at the position. N.b., this is really the total, i.e., includes reads where the mate is unmapped or otherwise not properly paired.

  • reads_pp - Number of reads flagged as properly paired by the aligner.

  • reads_mate_unmapped - Number of reads where the mate is unmapped.

  • reads_mate_other_chr - Number of reads where the mate is mapped to another chromosome.

  • reads_mate_same_strand - Number of reads where the mate is mapped to the same strand.

  • reads_faceaway - Number of reads where the read and its mate are mapped facing away from each other.

  • reads_softclipped - Number of reads where there is some softclipping at some point in the read's alignment (not necessarily at this position).

  • reads_duplicate - Number of reads that are flagged as duplicate.

  • gc - Percentage GC content in the reference at this position (depends on window length and offset specified).

  • matches - Number of reads where the aligned base matches the reference.

  • mismatches - Number of reads where the aligned base does not match the reference (but is not a deletion).

  • deletions - Number of reads where there is a deletion in the alignment at this position.

  • insertions - Number of reads where there is an insertion in the alignment at this position.

  • A/C/T/G/N - Number of reads where the aligned base is an A/C/T/G/N.

  • mean_tlen - Mean value of outer distance between reads and their mates for paired reads aligned at this position. N.B., leftmost reads in a pair have a positive tlen, rightmost reads have a negative tlen, so if there is no strand bias, this value should be

  • rms_tlen - Root-mean-square value of outer distance between reads and their mates for paired reads aligned at this position.

  • std_tlen - Standard deviation of outer distance between reads and their mates for paired reads aligned at this position.

  • reads_mapq0 - Number of reads where mapping quality is zero.

  • rms_mapq - Root-mean-square mapping quality for reads aligned at this position.

  • max_mapq - Maximum value of mapping quality for reads aligned at this position.

  • rms_baseq - Root-mean-square value of base qualities for bases aligned at this position.

  • rms_baseq_matches - Root-mean-square value of base qualities for bases aligned at this position where the base matches the reference.

  • rms_baseq_mismatches - Root-mean-square value of base qualities for bases aligned at this position where the base does not match the reference.

Release notes

1.1.2

  • Fix missing numpy as install requirement.

1.1.1

  • Fix missing pyproject.toml in source distribution.

1.1.0

  • Appropriate size dtype for chromosome names is now determined dynamically, no need to manually configure for longer chromosome/contig names. By Nick Harding, #72, #74.

  • Expose 'stepper' option via Python and command line API, to allow setting of different pileup behaviours. By Nick Harding, #78, #86.

  • Expose options min_mapq, min_baseq, no_del, no_dup via load_*() functions. By nrkssa, #93.

  • Add pyproject.toml for package build requirements, which means that there is no need to manually install pysam before installing pysamstats via pip. By Michiel Vermeir, #97.

  • Added a regression test to ensure consistent outputs in future package versions. By Nick Harding, #79.

  • Pysam dependency upgraded to 0.15.

1.0.1

  • Changed output of deletions field in variation stats to exclude RNA reads aligned with a splice ("N" in cigar) (#65)

1.0.0

  • Upgrades for compatibility with pysam 0.11.
  • Added options for filtering reads based on mapping quality, base quality, deletion status and duplicate flag.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言