simplesam 0.1.4.2__tar.gz → 0.1.4.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/.github/workflows/deploy.yml +1 -1
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/PKG-INFO +1 -1
- simplesam-0.1.4.3/README.md +100 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/docs/conf.py +7 -7
- simplesam-0.1.4.3/docs/index.rst +47 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.egg-info/PKG-INFO +1 -1
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.egg-info/SOURCES.txt +6 -1
- simplesam-0.1.4.3/simplesam.egg-info/scm_file_list.json +23 -0
- simplesam-0.1.4.3/simplesam.egg-info/scm_version.json +8 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.py +19 -18
- simplesam-0.1.4.3/tests/conftest.py +15 -0
- simplesam-0.1.4.3/tests/test_sam.py +42 -0
- simplesam-0.1.4.3/tests/test_sam_tags.py +10 -0
- simplesam-0.1.4.2/README.md +0 -144
- simplesam-0.1.4.2/docs/index.rst +0 -20
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/.github/workflows/tests.yml +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/.gitignore +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/CODE_OF_CONDUCT.md +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/LICENSE +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/__init__.py +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/docs/Makefile +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/docs/requirements.txt +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/scripts/pileup.py +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/setup.cfg +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/setup.py +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.egg-info/dependency_links.txt +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.egg-info/requires.txt +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/simplesam.egg-info/top_level.txt +0 -0
- {simplesam-0.1.4.2 → simplesam-0.1.4.3}/tests/__init__.py +0 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
[](https://pypi.org/project/simplesam/)
|
|
2
|
+
[](https://github.com/mdshw5/simplesam/actions/workflows/tests.yml)
|
|
3
|
+
[](https://github.com/mdshw5/simplesam/actions/workflows/deploy.yml)
|
|
4
|
+
[](https://simplesam.readthedocs.io/en/latest/?badge=latest)
|
|
5
|
+
|
|
6
|
+
# Simple SAM parsing
|
|
7
|
+
|
|
8
|
+
simplesam is a Python library for reading SAM alignments and working with
|
|
9
|
+
individual records. Reading BAM files additionally requires `samtools` on
|
|
10
|
+
`PATH`; Python dependencies are installed by pip.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install simplesam
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quickstart
|
|
19
|
+
|
|
20
|
+
`Reader` iterates over `Sam` records. The following example uses an in-memory
|
|
21
|
+
SAM stream, but a text file opened with `open("input.sam")` works too:
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from io import StringIO
|
|
25
|
+
from simplesam import Reader
|
|
26
|
+
|
|
27
|
+
data = (
|
|
28
|
+
"@HD\tVN:1.6\tSO:coordinate\n"
|
|
29
|
+
"@SQ\tSN:chr1\tLN:1000\n"
|
|
30
|
+
"read1\t0\tchr1\t10\t60\t2M1I2M1D1M\t*\t0\t0\tACGTTG\tIIIIII\tNM:i:2\n"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
with Reader(StringIO(data)) as reader:
|
|
34
|
+
record = next(reader)
|
|
35
|
+
print(record.qname, record.rname, record.pos) # read1 chr1 10
|
|
36
|
+
print(record.cigars) # ((2, 'M'), (1, 'I'), (2, 'M'), (1, 'D'), (1, 'M'))
|
|
37
|
+
print(record.gapped("seq")) # ACTT-G
|
|
38
|
+
print(list(record.coords)) # [10, 11, 12, 13, 14, 15]
|
|
39
|
+
print(record["NM"]) # 2
|
|
40
|
+
print(list(reader.seqs)) # ['chr1']
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For mapped records, `pos` and `coords` use 1-based reference coordinates. `len(record)` is the
|
|
44
|
+
reference span, including deletions and reference skips but excluding
|
|
45
|
+
insertions and soft clips. `gapped()` removes insertions and soft clips and
|
|
46
|
+
represents deletions and reference skips with `-` by default.
|
|
47
|
+
|
|
48
|
+
## Reading and writing files
|
|
49
|
+
|
|
50
|
+
Pass a text-mode SAM file to `Reader` and a writable text file to `Writer`.
|
|
51
|
+
Pass the reader's header to preserve it in the output:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from simplesam import Reader, Writer
|
|
55
|
+
|
|
56
|
+
with Reader(open("input.sam", "r")) as reader:
|
|
57
|
+
with Writer(open("output.sam", "w"), reader.header) as writer:
|
|
58
|
+
for record in reader:
|
|
59
|
+
writer.write(record)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Closing a SAM `Reader` or a `Writer` also closes its underlying file or stream.
|
|
63
|
+
Without a header, `Writer` emits a default `@HD` header. BAM writing is not
|
|
64
|
+
implemented; write SAM and use `samtools view -b` to convert it to BAM.
|
|
65
|
+
|
|
66
|
+
For BAM input, open a `.bam` file and pass it to `Reader`; `samtools view`
|
|
67
|
+
is used to read it. For example, an indexed, coordinate-sorted BAM can be
|
|
68
|
+
queried as follows:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from simplesam import Reader
|
|
72
|
+
|
|
73
|
+
with open("input.bam", "rb") as bam:
|
|
74
|
+
with Reader(bam, regions="chr1:1-1000") as reader:
|
|
75
|
+
for record in reader:
|
|
76
|
+
print(record.qname)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The BAM file handle is managed separately from the reader's samtools process.
|
|
80
|
+
If the `.bam.bai` index is missing, the reader attempts to create it with
|
|
81
|
+
`samtools index`. `samtools_path` can be passed to `Reader` to select a
|
|
82
|
+
different executable. Region queries are not supported for SAM input.
|
|
83
|
+
|
|
84
|
+
## Optional tags
|
|
85
|
+
|
|
86
|
+
`record.tags` is a dictionary, and individual values can be read or changed
|
|
87
|
+
with `record["NM"]`. Tags of type `A`, `i`, `f`, and `Z` are decoded;
|
|
88
|
+
`H` and `B` tags raise `NotImplementedError` when accessed. When a record is
|
|
89
|
+
written, tag types are inferred from Python values: strings are written as
|
|
90
|
+
`Z`, integers as `i`, and floats as `f`. Consequently an `A` tag is written
|
|
91
|
+
back as `Z`.
|
|
92
|
+
|
|
93
|
+
## Example script
|
|
94
|
+
|
|
95
|
+
The installed `pileup.py` script produces a pileup-like tabular summary from a
|
|
96
|
+
sorted/indexed BAM file with MD tags. It skips unmapped, duplicate, and
|
|
97
|
+
secondary alignments. Use `pileup.py --help` for available options; `-c` adds
|
|
98
|
+
mismatch-base-count columns and `-i` writes mismatch statistics.
|
|
99
|
+
|
|
100
|
+
For the API reference, see the [documentation](https://simplesam.readthedocs.io/en/latest/).
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
# add these directories to sys.path here. If the directory is relative to the
|
|
17
17
|
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
|
18
18
|
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
# -- General configuration ------------------------------------------------
|
|
@@ -55,16 +55,16 @@ author = u'Matt Shirley'
|
|
|
55
55
|
# built documents.
|
|
56
56
|
#
|
|
57
57
|
# The short X.Y version.
|
|
58
|
-
version =
|
|
58
|
+
version = ''
|
|
59
59
|
# The full version, including alpha/beta/rc tags.
|
|
60
|
-
release =
|
|
60
|
+
release = ''
|
|
61
61
|
|
|
62
62
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
|
63
63
|
# for a list of supported languages.
|
|
64
64
|
#
|
|
65
65
|
# This is also used if you do content translation via gettext catalogs.
|
|
66
66
|
# Usually you set "language" from the command line for these cases.
|
|
67
|
-
language =
|
|
67
|
+
language = 'en'
|
|
68
68
|
|
|
69
69
|
# List of patterns, relative to source directory, that match files and
|
|
70
70
|
# directories to ignore when looking for source files.
|
|
@@ -101,7 +101,7 @@ html_theme_options = {
|
|
|
101
101
|
# Add any paths that contain custom static files (such as style sheets) here,
|
|
102
102
|
# relative to this directory. They are copied after the builtin static files,
|
|
103
103
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
|
104
|
-
html_static_path = [
|
|
104
|
+
html_static_path = []
|
|
105
105
|
|
|
106
106
|
# Custom sidebar templates, must be a dictionary that maps document names
|
|
107
107
|
# to template names.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Simplesam
|
|
2
|
+
=========
|
|
3
|
+
Simple pure Python SAM parser and objects for working with SAM records
|
|
4
|
+
|
|
5
|
+
Installation
|
|
6
|
+
------------
|
|
7
|
+
|
|
8
|
+
Install with ``pip install simplesam``. BAM reading additionally requires
|
|
9
|
+
``samtools``; BAM writing is not implemented.
|
|
10
|
+
|
|
11
|
+
Usage
|
|
12
|
+
-----
|
|
13
|
+
|
|
14
|
+
``Reader`` iterates over records in a text-mode SAM file or a BAM file. Use
|
|
15
|
+
``Writer`` with a writable text file and the reader's header to write SAM::
|
|
16
|
+
|
|
17
|
+
from simplesam import Reader, Writer
|
|
18
|
+
|
|
19
|
+
with Reader(open("input.sam", "r")) as reader:
|
|
20
|
+
with Writer(open("output.sam", "w"), reader.header) as writer:
|
|
21
|
+
for record in reader:
|
|
22
|
+
writer.write(record)
|
|
23
|
+
|
|
24
|
+
A SAM reader and writer close their underlying file or stream. For BAM input,
|
|
25
|
+
manage the input file handle separately; the reader closes its samtools
|
|
26
|
+
process. BAM region queries require an indexed BAM file and ``samtools``.
|
|
27
|
+
The reader attempts to create a missing ``.bam.bai`` index. For mapped
|
|
28
|
+
records, ``Sam.pos`` and ``Sam.coords`` use 1-based reference coordinates.
|
|
29
|
+
|
|
30
|
+
Optional tags of type ``A``, ``i``, ``f``, and ``Z`` are decoded; ``H`` and
|
|
31
|
+
``B`` raise ``NotImplementedError`` when accessed. On output, tag types are
|
|
32
|
+
inferred from Python values, so an ``A`` tag is written as ``Z``.
|
|
33
|
+
|
|
34
|
+
API reference
|
|
35
|
+
-------------
|
|
36
|
+
|
|
37
|
+
.. automodule:: simplesam
|
|
38
|
+
:members:
|
|
39
|
+
:special-members:
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Indices and tables
|
|
43
|
+
==================
|
|
44
|
+
|
|
45
|
+
* :ref:`genindex`
|
|
46
|
+
* :ref:`modindex`
|
|
47
|
+
* :ref:`search`
|
|
@@ -17,5 +17,10 @@ simplesam.egg-info/PKG-INFO
|
|
|
17
17
|
simplesam.egg-info/SOURCES.txt
|
|
18
18
|
simplesam.egg-info/dependency_links.txt
|
|
19
19
|
simplesam.egg-info/requires.txt
|
|
20
|
+
simplesam.egg-info/scm_file_list.json
|
|
21
|
+
simplesam.egg-info/scm_version.json
|
|
20
22
|
simplesam.egg-info/top_level.txt
|
|
21
|
-
tests/__init__.py
|
|
23
|
+
tests/__init__.py
|
|
24
|
+
tests/conftest.py
|
|
25
|
+
tests/test_sam.py
|
|
26
|
+
tests/test_sam_tags.py
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": [
|
|
3
|
+
".github/workflows/deploy.yml",
|
|
4
|
+
".github/workflows/tests.yml",
|
|
5
|
+
".gitignore",
|
|
6
|
+
"CODE_OF_CONDUCT.md",
|
|
7
|
+
"LICENSE",
|
|
8
|
+
"README.md",
|
|
9
|
+
"__init__.py",
|
|
10
|
+
"docs/Makefile",
|
|
11
|
+
"docs/conf.py",
|
|
12
|
+
"docs/index.rst",
|
|
13
|
+
"docs/requirements.txt",
|
|
14
|
+
"scripts/pileup.py",
|
|
15
|
+
"setup.cfg",
|
|
16
|
+
"setup.py",
|
|
17
|
+
"simplesam.py",
|
|
18
|
+
"tests/__init__.py",
|
|
19
|
+
"tests/conftest.py",
|
|
20
|
+
"tests/test_sam.py",
|
|
21
|
+
"tests/test_sam_tags.py"
|
|
22
|
+
]
|
|
23
|
+
}
|
|
@@ -230,7 +230,7 @@ class Reader(object):
|
|
|
230
230
|
|
|
231
231
|
|
|
232
232
|
class Writer(object):
|
|
233
|
-
""" Write SAM
|
|
233
|
+
""" Write a SAM format file from :class:`.Sam` objects. """
|
|
234
234
|
def __init__(self, f, header=None):
|
|
235
235
|
try:
|
|
236
236
|
_, ext = os.path.splitext(f.name)
|
|
@@ -335,8 +335,8 @@ class Sam(GenomicOrder):
|
|
|
335
335
|
return "Sam({0}:{1}:{2})".format(self.rname, self.pos, self.qname)
|
|
336
336
|
|
|
337
337
|
def __len__(self):
|
|
338
|
-
"""
|
|
339
|
-
|
|
338
|
+
""" Return the reference span of the CIGAR, including deletions and reference skips.
|
|
339
|
+
A ``*`` CIGAR has length 0. Insertions and soft clips do not contribute.
|
|
340
340
|
|
|
341
341
|
>>> x = Sam(cigar='8M2I4M1D3M4S')
|
|
342
342
|
>>> len(x)
|
|
@@ -345,8 +345,7 @@ class Sam(GenomicOrder):
|
|
|
345
345
|
return sum(c[0] for c in self.cigars if c[1] in self._cigar_ref)
|
|
346
346
|
|
|
347
347
|
def __getitem__(self, tag):
|
|
348
|
-
"""
|
|
349
|
-
data type of the tag is interpreted as the proper Python object type.
|
|
348
|
+
""" Retrieve the value of the SAM tag named ``tag`` as a Python object.
|
|
350
349
|
|
|
351
350
|
>>> x = Sam(tags=['NM:i:0', 'ZZ:Z:xyz'])
|
|
352
351
|
>>> x['NM']
|
|
@@ -395,10 +394,10 @@ class Sam(GenomicOrder):
|
|
|
395
394
|
raise ValueError("CIGAR operation %s in record %s is invalid." % (op[1], self.qname))
|
|
396
395
|
|
|
397
396
|
def gapped(self, attr, gap_char='-'):
|
|
398
|
-
""" Return a :class:`.Sam` sequence attribute or tag with
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
be
|
|
397
|
+
""" Return a :class:`.Sam` sequence attribute or tag with deletions and
|
|
398
|
+
reference skips represented by ``gap_char`` and insertions and soft
|
|
399
|
+
clips removed. A sequence could
|
|
400
|
+
be ``Sam.seq``, ``Sam.qual``, or any :class:`.Sam` tag that
|
|
402
401
|
represents an aligned sequence, such as a methylation tag for bisulfite
|
|
403
402
|
sequencing libraries.
|
|
404
403
|
|
|
@@ -430,7 +429,7 @@ class Sam(GenomicOrder):
|
|
|
430
429
|
return ''.join(gapped)
|
|
431
430
|
|
|
432
431
|
def parse_md(self):
|
|
433
|
-
""" Return the
|
|
432
|
+
""" Return the reference sequence as a list, reconstructed from the MD tag and CIGAR.
|
|
434
433
|
"""
|
|
435
434
|
try:
|
|
436
435
|
return self._cache['parse_md']
|
|
@@ -553,22 +552,24 @@ def encode_tag(tag, data):
|
|
|
553
552
|
|
|
554
553
|
def decode_tag(tag_string):
|
|
555
554
|
""" Parse a SAM format tag to a (tag, type, data) tuple. Python object
|
|
556
|
-
types for data are set using the type code. Supported
|
|
555
|
+
types for data are set using the type code. Supported types are A, i, f, and Z;
|
|
556
|
+
H and B raise ``NotImplementedError``.
|
|
557
557
|
|
|
558
558
|
>>> decode_tag('YM:Z:#""9O"1@!J')
|
|
559
559
|
('YM', 'Z', '#""9O"1@!J')
|
|
560
|
+
>>> decode_tag('RG:Z:HLJJNADXX:1:GCCAAT.1')
|
|
561
|
+
('RG', 'Z', 'HLJJNADXX:1:GCCAAT.1')
|
|
562
|
+
>>> decode_tag('XX:A::')
|
|
563
|
+
('XX', 'A', ':')
|
|
560
564
|
>>> decode_tag('XS:i:5')
|
|
561
565
|
('XS', 'i', 5)
|
|
562
566
|
>>> decode_tag('XF:f:100.5')
|
|
563
567
|
('XF', 'f', 100.5)
|
|
564
568
|
"""
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
tag = match.group(1)
|
|
570
|
-
data_type = match.group(2)
|
|
571
|
-
data = match.group(3)
|
|
569
|
+
parts = tag_string.split(':', 2)
|
|
570
|
+
if len(parts) != 3:
|
|
571
|
+
raise NotImplementedError("Tag {0} cannot be parsed.".format(tag_string))
|
|
572
|
+
tag, data_type, data = parts
|
|
572
573
|
if data_type == 'i':
|
|
573
574
|
return (tag, data_type, int(data))
|
|
574
575
|
elif data_type == 'Z':
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture(scope="session")
|
|
8
|
+
def simplesam_module():
|
|
9
|
+
spec = importlib.util.spec_from_file_location(
|
|
10
|
+
"simplesam_module",
|
|
11
|
+
Path(__file__).resolve().parents[1] / "simplesam.py",
|
|
12
|
+
)
|
|
13
|
+
module = importlib.util.module_from_spec(spec)
|
|
14
|
+
spec.loader.exec_module(module)
|
|
15
|
+
return module
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from io import StringIO
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
SAM_TEXT = (
|
|
5
|
+
'@HD\tVN:1.6\tSO:coordinate\n'
|
|
6
|
+
'@SQ\tSN:chr1\tLN:1000\n'
|
|
7
|
+
'read1\t99\tchr1\t10\t60\t4M\t=\t30\t24\tACGT\tIIII\tNM:i:1\tRG:Z:group:1\n'
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_reader_writer_round_trip(tmp_path, simplesam_module):
|
|
12
|
+
reader = simplesam_module.Reader(StringIO(SAM_TEXT))
|
|
13
|
+
record = next(reader)
|
|
14
|
+
|
|
15
|
+
assert list(reader.seqs) == ['chr1']
|
|
16
|
+
assert record.qname == 'read1'
|
|
17
|
+
assert record.tags == {'NM': 1, 'RG': 'group:1'}
|
|
18
|
+
reader.close()
|
|
19
|
+
|
|
20
|
+
output_path = tmp_path / 'roundtrip.sam'
|
|
21
|
+
with output_path.open('w') as output:
|
|
22
|
+
writer = simplesam_module.Writer(output, reader.header)
|
|
23
|
+
writer.write(record)
|
|
24
|
+
|
|
25
|
+
assert output_path.read_text() == SAM_TEXT
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_alignment_cigar_and_coordinates(simplesam_module):
|
|
29
|
+
record = simplesam_module.Sam(
|
|
30
|
+
qname='read2',
|
|
31
|
+
flag=0,
|
|
32
|
+
rname='chr1',
|
|
33
|
+
pos=10,
|
|
34
|
+
cigar='2M1I2M1D1M',
|
|
35
|
+
seq='ACGTTG',
|
|
36
|
+
qual='IIIIII',
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
assert record.cigars == ((2, 'M'), (1, 'I'), (2, 'M'), (1, 'D'), (1, 'M'))
|
|
40
|
+
assert len(record) == 6
|
|
41
|
+
assert record.gapped('seq') == 'ACTT-G'
|
|
42
|
+
assert list(record.coords) == [10, 11, 12, 13, 14, 15]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
def test_decode_tag_preserves_colons_in_string_value(simplesam_module):
|
|
2
|
+
assert simplesam_module.decode_tag('RG:Z:HLJJNADXX:1:GCCAAT.1') == (
|
|
3
|
+
'RG',
|
|
4
|
+
'Z',
|
|
5
|
+
'HLJJNADXX:1:GCCAAT.1',
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_decode_tag_accepts_colon_as_character_value(simplesam_module):
|
|
10
|
+
assert simplesam_module.decode_tag('XX:A::') == ('XX', 'A', ':')
|
simplesam-0.1.4.2/README.md
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
[](https://pypi.python.org/pypi/simplesam)
|
|
2
|
-
[](https://github.com/mdshw5/simplesam/actions/workflows/tests.yml)
|
|
3
|
-
[](https://github.com/mdshw5/simplesam/actions/workflows/deploy.yml)
|
|
4
|
-
[](http://simplesam.readthedocs.io/en/latest/?badge=latest)
|
|
5
|
-
|
|
6
|
-
# Simple SAM parsing
|
|
7
|
-
Requiring no external dependencies (except a samtools installation for BAM reading)
|
|
8
|
-
|
|
9
|
-
# Installation
|
|
10
|
-
`pip install simplesam`
|
|
11
|
-
|
|
12
|
-
# Usage
|
|
13
|
-
For complete module documentation visit [ReadTheDocs](http://simplesam.readthedocs.io/en/latest/).
|
|
14
|
-
|
|
15
|
-
## Quickstart
|
|
16
|
-
|
|
17
|
-
```python
|
|
18
|
-
>>> from simplesam import Reader, Writer
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Read from SAM/BAM files
|
|
22
|
-
```python
|
|
23
|
-
# can also read BAM
|
|
24
|
-
>>> in_file = open('data/NA18510.sam', 'r')
|
|
25
|
-
>>> in_sam = Reader(in_file)
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Access alignments using an iterator interface
|
|
29
|
-
```python
|
|
30
|
-
>>> x = next(in_sam)
|
|
31
|
-
>>> type(x)
|
|
32
|
-
<class 'simplesam.Sam'>
|
|
33
|
-
>>> x
|
|
34
|
-
Sam(1:2:SRR011051.1022326)
|
|
35
|
-
>>> x.qname
|
|
36
|
-
'SRR011051.1022326'
|
|
37
|
-
>>> x.rname
|
|
38
|
-
'1'
|
|
39
|
-
>>> x.pos
|
|
40
|
-
2
|
|
41
|
-
>>> x.seq
|
|
42
|
-
'AACCCTAACCCCTAACCCTAACCCTAACCCTACCCCTAACCCTACCCCTCC'
|
|
43
|
-
>>> x.qual
|
|
44
|
-
'?<:;;=;>;<<<>96;<;;99;<=3;4<<:(;,<;;/;57<;%6,=:,((3'
|
|
45
|
-
>>> x.cigar
|
|
46
|
-
'8M1I42M'
|
|
47
|
-
>>> x.cigars
|
|
48
|
-
((8, 'M'), (1, 'I'), (42, 'M'))
|
|
49
|
-
>>> x.gapped('seq')
|
|
50
|
-
'AACCCTAACCCTAACCCTAACCCTAACCCTACCCCTAACCCTACCCCTCC'
|
|
51
|
-
>>> len(x)
|
|
52
|
-
50
|
|
53
|
-
>>> x.flag
|
|
54
|
-
35
|
|
55
|
-
>>> x.mapped
|
|
56
|
-
True
|
|
57
|
-
>>> x.paired
|
|
58
|
-
True
|
|
59
|
-
>>> x.duplicate
|
|
60
|
-
False
|
|
61
|
-
>>> x.secondary
|
|
62
|
-
False
|
|
63
|
-
>>> x.coords
|
|
64
|
-
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
|
|
65
|
-
>>> x.tags
|
|
66
|
-
{'H1': 0, 'UQ': 33, 'RG': 'SRR011051', 'H0': 0, 'MF': 130, 'Aq': 25, 'NM': 2}
|
|
67
|
-
>>> str(x)
|
|
68
|
-
'SRR011051.1022326\t35\t1\t2\t255\t8M1I42M\t*\t0\t0\tAACCCTAACCCCTAACCCTAACCCTAACCCTACCCCTAACCCTACCCCTCC\t?<:;;=;>;<<<>96;<;;99;<=3;4<<:(;,<;;/;57<;%6,=:,((3\tAq:i:25\tH0:i:0\tH1:i:0\tMF:i:130\tNM:i:2\tRG:Z:SRR011051\tUQ:i:33\n'
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
Read the SAM sequence header structure
|
|
72
|
-
```python
|
|
73
|
-
>>> from pprint import pprint
|
|
74
|
-
>>> pprint(in_sam.header)
|
|
75
|
-
{'@HD': OrderedDict([('VN:1.0', ['GO:none', 'SO:coordinate'])]),
|
|
76
|
-
'@SQ': {'SN:1': ['LN:247249719'],
|
|
77
|
-
'SN:2': ['LN:242951149'],
|
|
78
|
-
'SN:3': ['LN:199501827'],
|
|
79
|
-
'SN:4': ['LN:191273063'],
|
|
80
|
-
'SN:5': ['LN:180857866'],
|
|
81
|
-
'SN:6': ['LN:170899992'],
|
|
82
|
-
'SN:7': ['LN:158821424'],
|
|
83
|
-
'SN:8': ['LN:146274826'],
|
|
84
|
-
...
|
|
85
|
-
'@RG': {'ID:SRR011049': ['PL:ILLUMINA',
|
|
86
|
-
'PU:BI.PE.080626_SL-XAN_0002_FC304CDAAXX.080630_SL-XAN_0007_FC304CDAAXX.5',
|
|
87
|
-
'LB:Solexa-5112',
|
|
88
|
-
'PI:330',
|
|
89
|
-
'SM:NA18510',
|
|
90
|
-
'CN:BI'],
|
|
91
|
-
'ID:SRR011050': ['PL:ILLUMINA',
|
|
92
|
-
'PU:BI.PE.080626_SL-XAN_0002_FC304CDAAXX.080630_SL-XAN_0007_FC304CDAAXX.6',
|
|
93
|
-
'LB:Solexa-5112',
|
|
94
|
-
'PI:330',
|
|
95
|
-
'SM:NA18510',
|
|
96
|
-
'CN:BI'],
|
|
97
|
-
...}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
Write SAM files from `Sam` objects
|
|
103
|
-
```python
|
|
104
|
-
# Reader and Writer can also use the context handler (with: statement)
|
|
105
|
-
>>> out_file = open('test.sam', 'w')
|
|
106
|
-
>>> out_sam = Writer(out_file, in_sam.header)
|
|
107
|
-
>>> out_sam.write(x)
|
|
108
|
-
>>> out_sam.close()
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
Write SAM files from `Sam` objects to stdout (allows `samtools view` compression)
|
|
112
|
-
```python
|
|
113
|
-
>>> from sys import stdout
|
|
114
|
-
>>> stdout_sam = Writer(stdout, in_sam.header)
|
|
115
|
-
>>> stdout_sam.write(x)
|
|
116
|
-
>>> stdout_sam.close()
|
|
117
|
-
```
|
|
118
|
-
```bash
|
|
119
|
-
$ python my_script_that_uses_simplesam.py | samtools view -hbo test.bam
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
# Example scripts
|
|
123
|
-
An example script [`pileup.py`](https://github.com/mdshw5/simplesam/blob/master/scripts/pileup.py) is installed with this module.
|
|
124
|
-
This script will generate an output that is similar to `samtools pileup` with the addition of several optional columns that summarize
|
|
125
|
-
counts for individual nucleotides (ACTGN) and deletions with respect to the reference (-). This script leverages the `Sam.gapped()` and
|
|
126
|
-
`Sam.parse_md()` methods to reconstruct position-specific counts from SAM alignment records.
|
|
127
|
-
|
|
128
|
-
```bash
|
|
129
|
-
$ pileup.py -h
|
|
130
|
-
usage: pileup [-h] [--version] [-c] [-i STATS] bam pileup
|
|
131
|
-
|
|
132
|
-
generate a simple pileup-like file from a sorted/indexed BAM file
|
|
133
|
-
|
|
134
|
-
positional arguments:
|
|
135
|
-
bam sorted/indexed BAM file
|
|
136
|
-
pileup pileup output file
|
|
137
|
-
|
|
138
|
-
optional arguments:
|
|
139
|
-
-h, --help show this help message and exit
|
|
140
|
-
--version show program's version number and exit
|
|
141
|
-
-c, --counts display counts for A/C/T/G/N/- separately (default: False)
|
|
142
|
-
-i STATS, --stats STATS
|
|
143
|
-
tabulate mismatches to output file
|
|
144
|
-
```
|
simplesam-0.1.4.2/docs/index.rst
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
Simplesam
|
|
2
|
-
=========
|
|
3
|
-
Simple pure Python SAM parser and objects for working with SAM records
|
|
4
|
-
|
|
5
|
-
.. toctree::
|
|
6
|
-
:maxdepth: 2
|
|
7
|
-
|
|
8
|
-
index
|
|
9
|
-
|
|
10
|
-
.. automodule:: simplesam
|
|
11
|
-
:members:
|
|
12
|
-
:special-members:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Indices and tables
|
|
16
|
-
==================
|
|
17
|
-
|
|
18
|
-
* :ref:`genindex`
|
|
19
|
-
* :ref:`modindex`
|
|
20
|
-
* :ref:`search`
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|