Package: bamnostic
PlatformBuild StatusLinuxWindowscondaHostDownloadsPyPIcondaBAMnosticapure Python,OS-agnositicBinary Alignment Map (BAM) file parser
and random access tool.Note:Documentation can be found athereor by going to
this address:http://bamnostic.readthedocs.io. Documentation was made
available throughRead the Docs.InstallationThere are 4 methods of installation available (choose one):Through thecondapackage manager (Anaconda Cloud)# first, add the conda-forge channel to your conda buildcondaconfig--addchannelsconda-forge# now bamnostic is available for installcondainstallbamnosticThrough the Python Package Index (PyPI)pipinstallbamnostic# or, if you don't have superuser accesspipinstall--userbamnosticThrough pip+Github# again, use --user if you don't have superuser accesspipinstall-egit+https://github.com/betteridiot/bamnostic.git#egg=bamnostic# or, if you don't have superuser accesspipinstall--user-egit+https://github.com/betteridiot/bamnostic.git#bamnostic#egg=bamnosticTraditional GitHub clonegitclonehttps://github.com/betteridiot/bamnostic.gitcdbamnosticpipinstall-e.# or, if you don't have superuser accesspipinstall--user-e.QuickstartBamnostic is meant to be a reduced drop-in replacement forpysam. As such it has
much the same API aspysamwith regard to BAM-related operations.Note: thepileup()method is not supported at this time. ###
Importing>>>importbamnosticasbsLoading your BAM file (Note: CRAM format are not supported at this time)Bamnostic comes with an example BAM (and respective BAI) file just to
play around with the output. Note, however, that the example BAM file
does not contain many reference contigs. Therefore, random access is
limited. This example file is made availble throughbamnostic.example_bam, which is a just a string path to the BAM file
within the package.>>>bam=bs.AlignmentFile(bs.example_bam,'rb')Get the headerNote: this will print out the SAM header. If the SAM header is not
in the BAM file, it will print out the dictionary representation of the
BAM header. It is a dictionary of refID keys with contig names and
length tuple values.>>>bam.header{0:('chr1',1575),1:('chr2',1584)}Data validation throughhead()>>>bam.head(n=2)[EAS56_57:6:190:289:8269chr11000*=1000CTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA<<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<;MF:C:192,EAS56_57:6:190:289:82137chr11007335M=1000AGGGGTGCAGAGCCGAGTCACGGGGTTGCCAGCAC<<<<<<;<<<<<<<<<<;<<;<<<<;8<6;9;;2;MF:C:64Aq:C:0NM:C:0UQ:C:0H0:C:1H1:C:0]Getting the first read>>>first_read=next(bam)>>>print(first_read)EAS56_57:6:190:289:8269chr11000*=1000CTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA<<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<;MF:C:192Exploring the read# read name>>>print(first_read.read_name)EAS56_57:6:190:289:82# 0-based position>>>print(first_read.pos)99# nucleotide sequence>>>print(first_read.seq)CTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA# Read FLAG>>>print(first_read.flag)69# decoded FLAG>>>bs.utils.flag_decode(first_read.flag)[(1,'read paired'),(4,'read unmapped'),(64,'first in pair')]Random Access>>>fori,readinenumerate(bam.fetch('chr2',1,100)):...ifi>=3:...break...print(read)B7_591:8:4:841:34073chr219936M*00TTCAAATGAACTTCTGTAATTGAAAAATTCATTTAA<<<<<<<<;<<<<<<<<;<<<<<;<;:<<<<<<<;;MF:C:18Aq:C:77NM:C:0UQ:C:0H0:C:1H1:C:0EAS54_67:4:142:943:58273chr219935M*00TTCAAATGAACTTCTGTAATTGAAAAATTCATTTA<<<<<<;<<<<<<:<<;<<<<;<<<;<<<:;<<<5MF:C:18Aq:C:41NM:C:0UQ:C:0H0:C:1H1:C:0EAS54_67:6:43:859:229153chr216635M*00TTCAAATGAACTTCTGTAATTGAAAAATTCATTTA+37<=<.;<<7.;77<5<<0<<<;<<<27<<<<<<MF:C:32Aq:C:0NM:C:0UQ:C:0H0:C:1H1:C:0IntroductionNext-Generation SequencingThe field of genomics requires sequencing data produced by
Next-Generation sequencing (NGS) platforms (such asIllumina). These data take the form of
millions of short strings that represent the nucleotide sequences (A, T,
C, or G) of the sample fragments processed by the NGS platform. More
information regarding the NGS workflow can be foundhereAn example of a single entry (known as FASTQ) can be seen below (FASTQ
Format):@SRR001666.1071112_SLXA-EAS1_s_7:5:1:817:345length=36GGGTGATGGCCGCTGCCGATGGCGTCAAATCCCACC+SRR001666.1071112_SLXA-EAS1_s_7:5:1:817:345length=36IIIIIIIIIIIIIIIIIIIIIIIIIIIIII9IG9ICEach entry details the read name, lenght, string representation, and
quality of each aligned base along the read. ### SAM/BAM Format The data
from the NGS platforms are often aligned to reference genome. That is,
each entry goes through an alignment algorithm that finds the best
position that the entry matches along a known reference sequence. The
alignment step extends the original entry with a sundry of additional
attributes. A few of the included attributes are contig, position, and
Compact Idiosyncratic Gapped Alignment Report (CIGAR) string. The
modified entry is called the An example Sequence Alignment Map (SAM)
entry can be see below (SAM
format):@HDVN:1.5SO:coordinate@SQSN:refLN:45r00199ref7308M2I4M1D3M=3739TTAGATAAAGGATACTG*r0020ref9303S6M1P1I4M*00AAAAGATAAGGATA*r0030ref9305S6M*00GCCTAAGCTAA*SA:Z:ref,29,-,6H5M,17,0;r0040ref16306M14N5M*00ATAGCTTCAGC*r0032064ref29176H5M*00TAGGC*SA:Z:ref,9,+,5S6M,30,1;r001147ref37309M=7-39CAGCGGCAT*NM:i:1There are many benefits to the SAM format: human-readable, each entry is
contained to a single line (supporting simple stream analysis), concise
description of the read’s quality and position, and a file header
metadata that supports integrity and reproducibility. Additionally, a
compressed form of the SAM format was designed in parallel. It is called
the Binary Alignment Map
(BAM). Using a
series of clever byte encoding of each SAM entry, the data are
compressed into specialized, concatenated GZIP blocks called Blocked GNU
Zip Format (BGZF)
blocks. Each BGZF block contains a finite amount of data (≈65Kb). While
the whole file is GZIP compatible, each individual block is also
independently GZIP compatible. This data structure, ultimately, makes
the file larger than just a normal GZIP file, but it also allow for
random access within the file though the use of a BAM Index file
(BAI).BAIThe BAI file, often produced viasamtools, requires the BAM file
to be sorted prior to indexing. Using a modified R-tree binning
strategy, each reference contig is divided into sequential,
non-overlapping bins. That is a parent bin may contain numerous
children, but none of the children bins overlap another’s assigned
interval. Each BAM entry is then assigned to the bin that fully contains
it. A visual description of the binning strategy can be foundhere. Each bin is
comprised of chunks, and each chunk contains its respective start and
stop byte positions within the BAM file. In addition to the bin index, a
linear index is produced as well. Again, the reference contig is divided
into equally sized windows (covering ≈16Kbp/each). Along those windows,
the start offset of the first read thatoverlapsthat window is
stored. Now, given a region of interest, the first bin that overlaps the
region is looked up. The chunks in the bin are stored asvirtual
offsets. A virtual offset is a 64-bit unsigned integer that is
comprised of the compressed offsetcoffset(indicating the byte
position of the start of the containing BGZF block) and the uncompressed
offsetuoffset(indicating the byte position within the uncompressed
data of the BGZF block that the data starts). A virtual offset is
calculated by:virtual_offset=coffset<<16|uoffsetSimilarly, the complement of the above is as follows:coffset=virtual_offset>>16uoffset=virtual_offset^(coffset<<16)A simple seek call against the BAM file will put the head at the start
of your region of interest.MotivationThe common practice within the field of genomics/genetics when analyzing
BAM files is to use the program known assamtools. The maintainers of
samtools have done a tremendous job of providing distributions that work
on a multitude of operating systems. While samtools is powerful, as a
command line interface, it is also limited in that it doesn’t really
afford the ability to perform real-time dynamic processing of reads
(without requiring many system calls to samtools). Due to its general
nature and inherent readability, a package was written in Python calledpysam. This package
allowed users a very comfortable means to doing such dynamic processing.
However, the foundation of these tools is built on a C-API calledhtsliband htslib cannot be
compiled in a Windows environment. By extension, neither can pysam. In
building a tool for genomic visualization, I wanted it to be platform
agnostic. This is precisely when I found out that the tools I had
planned to use as a backend did not work on Windows…the most prevalent
operation system in the end-user world. So, I wrotebamnostic. As of
this writing, bamnostic is OS-agnostic and written completely in Pure
Python–requiring only the standard library (andpytestfor the test
suite). Special care was taken to ensure that it would run on all
versions of CPython 2.7 or greater. Additionally, it runs in both stable
versions of PyPy. While it may perform slower than its C counterparts,
bamnostic opens up the science to a much greater end-user group. Lastly,
it is lightweight enough to fit into any simple web server
(e.g.Flask), further expanding the
science of genetics/genomics.CitationIf you use bamnostic in your analyses, please consider citingLi et al
(2009)as well.
Regarding the citation for bamnostic, please use the JoSS journal
article (click on the JOSS badge above) or use the following: >Sherman
MD and Mills RE, (2018). BAMnostic: an OS-agnostic toolkit for genomic
sequence analysis . Journal of Open Source Software, 3(28), 826,https://doi.org/10.21105/joss.00826Community Guidelines:Eagerly accepting PRs for improvements, optimizations, or features. For
any questions or issues, please feel free to make a post to bamnostic’sIssue trackeron
github or read over ourCONTRIBUTINGdocumentation.Commmunity Contributors:Below you will find a list of contributors and it acts as a small token
of my gratitude to the community that has helped support this project.
1.@GeekLogan2.@giesselmann3.@olgabot4.@OliverVoogd5.@gmat