tview 0.1.dev4__py3-none-any.whl
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.
- tview/__init__.py +27 -0
- tview/_version.py +34 -0
- tview/cli.py +61 -0
- tview/tview.py +352 -0
- tview-0.1.dev4.dist-info/METADATA +293 -0
- tview-0.1.dev4.dist-info/RECORD +10 -0
- tview-0.1.dev4.dist-info/WHEEL +5 -0
- tview-0.1.dev4.dist-info/entry_points.txt +2 -0
- tview-0.1.dev4.dist-info/licenses/LICENSE +21 -0
- tview-0.1.dev4.dist-info/top_level.txt +1 -0
tview/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""tview — Publication-quality alignment viewer."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
4
|
+
|
|
5
|
+
from tview.tview import (
|
|
6
|
+
Panel,
|
|
7
|
+
fasta_panel,
|
|
8
|
+
bam_panel,
|
|
9
|
+
read_fasta,
|
|
10
|
+
render_panels,
|
|
11
|
+
NT_COLORS,
|
|
12
|
+
AA_COLORS,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("tview")
|
|
17
|
+
except PackageNotFoundError:
|
|
18
|
+
__version__ = "0.0.0"
|
|
19
|
+
__all__ = [
|
|
20
|
+
"Panel",
|
|
21
|
+
"fasta_panel",
|
|
22
|
+
"bam_panel",
|
|
23
|
+
"read_fasta",
|
|
24
|
+
"render_panels",
|
|
25
|
+
"NT_COLORS",
|
|
26
|
+
"AA_COLORS",
|
|
27
|
+
]
|
tview/_version.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# file generated by setuptools-scm
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
TYPE_CHECKING = False
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
from typing import Union
|
|
17
|
+
|
|
18
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
20
|
+
else:
|
|
21
|
+
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
23
|
+
|
|
24
|
+
version: str
|
|
25
|
+
__version__: str
|
|
26
|
+
__version_tuple__: VERSION_TUPLE
|
|
27
|
+
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
30
|
+
|
|
31
|
+
__version__ = version = '0.1.dev4'
|
|
32
|
+
__version_tuple__ = version_tuple = (0, 1, 'dev4')
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = None
|
tview/cli.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Click CLI for tview."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
import click
|
|
6
|
+
|
|
7
|
+
from tview.tview import bam_panel, fasta_panel, render_panels
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _expand_stdin(paths: list[str]) -> list[str]:
|
|
11
|
+
"""If paths is ['-'], read file paths from stdin (one per line)."""
|
|
12
|
+
if paths and len(paths) == 1 and paths[0] == "-":
|
|
13
|
+
return [line.strip() for line in sys.stdin if line.strip()]
|
|
14
|
+
return list(paths)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@click.command(
|
|
18
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
19
|
+
epilog="Use '-' to read file paths from stdin, e.g.:\n\n"
|
|
20
|
+
" find . -name '*.fasta' | tview --fasta - --palette aa -o out.png",
|
|
21
|
+
)
|
|
22
|
+
@click.option("--bam", multiple=True, help="BAM file(s) — each becomes a panel. Use '-' for stdin.")
|
|
23
|
+
@click.option("--ref", type=click.Path(exists=True), help="Reference FASTA (required for BAM mode).")
|
|
24
|
+
@click.option("--region", help="Genomic region chr:start-end (required for BAM mode).")
|
|
25
|
+
@click.option("--fasta", multiple=True, help="Aligned FASTA file(s) — each becomes a panel. Use '-' for stdin.")
|
|
26
|
+
@click.option("--columns", help="Column range for FASTA, 1-based inclusive (e.g. 1-120).")
|
|
27
|
+
@click.option("-o", "--output", default="alignment.png", show_default=True, help="Output image path.")
|
|
28
|
+
@click.option("--palette", type=click.Choice(["nt", "aa"]), default="nt", show_default=True, help="Color palette.")
|
|
29
|
+
@click.option("--dpi", type=int, default=300, show_default=True, help="Image resolution.")
|
|
30
|
+
@click.option("--fontsize", type=int, default=7, show_default=True, help="Base font size in points.")
|
|
31
|
+
@click.option("--cell", type=float, default=0.14, show_default=True, help="Cell size in inches.")
|
|
32
|
+
def main(bam, ref, region, fasta, columns, output, palette, dpi, fontsize, cell):
|
|
33
|
+
"""Publication-quality alignment viewer (BAM or FASTA).
|
|
34
|
+
|
|
35
|
+
Supports BAM files (with reference FASTA), pre-aligned FASTA (e.g. MAFFT
|
|
36
|
+
output), and stacking multiple inputs into a single figure.
|
|
37
|
+
"""
|
|
38
|
+
bam_paths = _expand_stdin(list(bam))
|
|
39
|
+
fasta_paths = _expand_stdin(list(fasta))
|
|
40
|
+
|
|
41
|
+
if not bam_paths and not fasta_paths:
|
|
42
|
+
raise click.UsageError("Provide --bam and/or --fasta")
|
|
43
|
+
|
|
44
|
+
panels = []
|
|
45
|
+
|
|
46
|
+
if bam_paths:
|
|
47
|
+
if not ref or not region:
|
|
48
|
+
raise click.UsageError("--ref and --region are required for BAM input")
|
|
49
|
+
for bam_path in bam_paths:
|
|
50
|
+
panels.append(bam_panel(bam_path, ref, region))
|
|
51
|
+
|
|
52
|
+
if fasta_paths:
|
|
53
|
+
col_start, col_end = None, None
|
|
54
|
+
if columns:
|
|
55
|
+
parts = columns.replace(",", "").split("-")
|
|
56
|
+
col_start = int(parts[0])
|
|
57
|
+
col_end = int(parts[1]) if len(parts) > 1 else None
|
|
58
|
+
for fasta_path in fasta_paths:
|
|
59
|
+
panels.append(fasta_panel(fasta_path, col_start, col_end))
|
|
60
|
+
|
|
61
|
+
render_panels(panels, output, fontsize=fontsize, dpi=dpi, palette=palette, cell=cell)
|
tview/tview.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
tview_image.py — Publication-quality alignment viewer.
|
|
4
|
+
|
|
5
|
+
Supports BAM files (with ref FASTA) and pre-aligned FASTA (e.g. MAFFT output).
|
|
6
|
+
Multiple inputs can be stacked vertically in a single figure.
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
# Single BAM
|
|
10
|
+
python tview_image.py --bam sample.bam --ref ref.fa --region chr1:1-50 -o out.png
|
|
11
|
+
|
|
12
|
+
# Stacked BAMs (shared ref + region)
|
|
13
|
+
python tview_image.py --bam a.bam b.bam --ref ref.fa --region chr1:1-50 -o out.png
|
|
14
|
+
|
|
15
|
+
# Aligned FASTA (first sequence = reference)
|
|
16
|
+
python tview_image.py --fasta aligned.fasta -o out.png --palette aa
|
|
17
|
+
|
|
18
|
+
# Stacked FASTAs
|
|
19
|
+
python tview_image.py --fasta group1.fasta group2.fasta -o out.png --palette aa
|
|
20
|
+
|
|
21
|
+
# FASTA with column range (1-based, inclusive)
|
|
22
|
+
python tview_image.py --fasta aligned.fasta --columns 1-120 -o out.png
|
|
23
|
+
|
|
24
|
+
# Mix BAM and FASTA panels
|
|
25
|
+
python tview_image.py --bam a.bam --ref ref.fa --region chr1:1-50 --fasta aln.fasta -o out.png
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import sys
|
|
31
|
+
from collections import defaultdict
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
import matplotlib
|
|
35
|
+
matplotlib.use("Agg")
|
|
36
|
+
import matplotlib.pyplot as plt
|
|
37
|
+
import matplotlib.font_manager as fm
|
|
38
|
+
|
|
39
|
+
# ── Color schemes ─────────────────────────────────────────────────
|
|
40
|
+
NT_COLORS = {
|
|
41
|
+
"A": "#4CAF50", "C": "#2196F3", "G": "#FF9800", "T": "#F44336",
|
|
42
|
+
"N": "#9E9E9E", "-": "#9E9E9E",
|
|
43
|
+
}
|
|
44
|
+
AA_COLORS = {
|
|
45
|
+
"A": "#2196F3", "V": "#2196F3", "L": "#2196F3", "I": "#2196F3",
|
|
46
|
+
"M": "#2196F3", "F": "#2196F3", "W": "#2196F3", "P": "#2196F3",
|
|
47
|
+
"K": "#F44336", "R": "#F44336", "H": "#F44336",
|
|
48
|
+
"D": "#E040FB", "E": "#E040FB",
|
|
49
|
+
"S": "#4CAF50", "T": "#4CAF50", "N": "#4CAF50", "Q": "#4CAF50",
|
|
50
|
+
"G": "#FF9800", "C": "#FF9800", "Y": "#FF9800",
|
|
51
|
+
"*": "#9E9E9E", "-": "#9E9E9E", "X": "#9E9E9E",
|
|
52
|
+
}
|
|
53
|
+
MISMATCH_BG = "#FFEB3B55"
|
|
54
|
+
INS_BG = "#CE93D833"
|
|
55
|
+
GAP_COLOR = "#9E9E9E"
|
|
56
|
+
FWD_ALPHA = 1.0
|
|
57
|
+
REV_ALPHA = 0.55
|
|
58
|
+
SEPARATOR_COLOR = "#BDBDBD"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# ═════════════════════════════════════════════════════════════════
|
|
62
|
+
# Data structures — each input becomes a Panel
|
|
63
|
+
# ═════════════════════════════════════════════════════════════════
|
|
64
|
+
class Panel:
|
|
65
|
+
"""One horizontal alignment block: a reference row + read/sequence rows."""
|
|
66
|
+
def __init__(self, label, ref_row, seq_rows, total_cols, col_labels,
|
|
67
|
+
ins_columns=None):
|
|
68
|
+
self.label = label # panel title
|
|
69
|
+
self.ref_row = ref_row # list[str] length = total_cols
|
|
70
|
+
self.seq_rows = seq_rows # list[(name, list[str], is_reverse)]
|
|
71
|
+
self.total_cols = total_cols
|
|
72
|
+
self.col_labels = col_labels # list[(col_idx, label_str)] for x-ticks
|
|
73
|
+
self.ins_columns = ins_columns or set() # set of col indices that are insertion cols
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# ═════════════════════════════════════════════════════════════════
|
|
77
|
+
# FASTA panel builder
|
|
78
|
+
# ═════════════════════════════════════════════════════════════════
|
|
79
|
+
def read_fasta(path):
|
|
80
|
+
"""Parse FASTA → list[(name, sequence)]."""
|
|
81
|
+
seqs = []
|
|
82
|
+
name, buf = None, []
|
|
83
|
+
for line in open(path):
|
|
84
|
+
if line.startswith(">"):
|
|
85
|
+
if name is not None:
|
|
86
|
+
seqs.append((name, "".join(buf)))
|
|
87
|
+
name = line[1:].strip()
|
|
88
|
+
buf = []
|
|
89
|
+
else:
|
|
90
|
+
buf.append(line.strip())
|
|
91
|
+
if name is not None:
|
|
92
|
+
seqs.append((name, "".join(buf)))
|
|
93
|
+
return seqs
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def fasta_panel(path, col_start=None, col_end=None) -> Panel:
|
|
97
|
+
"""
|
|
98
|
+
Build a Panel from an aligned FASTA. First sequence = reference.
|
|
99
|
+
col_start / col_end are 1-based inclusive column indices into the
|
|
100
|
+
alignment (after MAFFT gaps).
|
|
101
|
+
"""
|
|
102
|
+
seqs = read_fasta(path)
|
|
103
|
+
if not seqs:
|
|
104
|
+
raise ValueError(f"No sequences in {path}")
|
|
105
|
+
|
|
106
|
+
ref_name, ref_seq = seqs[0]
|
|
107
|
+
|
|
108
|
+
# Slice columns if requested (1-based inclusive)
|
|
109
|
+
if col_start is not None or col_end is not None:
|
|
110
|
+
cs = (col_start or 1) - 1
|
|
111
|
+
ce = col_end or len(ref_seq)
|
|
112
|
+
ref_seq = ref_seq[cs:ce]
|
|
113
|
+
seqs = [(n, s[cs:ce]) for n, s in seqs]
|
|
114
|
+
else:
|
|
115
|
+
cs = 0
|
|
116
|
+
|
|
117
|
+
aln_len = len(ref_seq)
|
|
118
|
+
ref_row = list(ref_seq.upper())
|
|
119
|
+
|
|
120
|
+
seq_rows = []
|
|
121
|
+
for name, seq in seqs[1:]:
|
|
122
|
+
row = list(seq.upper()[:aln_len])
|
|
123
|
+
# Pad if shorter
|
|
124
|
+
row += ["-"] * (aln_len - len(row))
|
|
125
|
+
seq_rows.append((name, row, False))
|
|
126
|
+
|
|
127
|
+
# Column labels: 1-based position in the reference (skip gap columns)
|
|
128
|
+
# Count non-gap ref positions for labeling
|
|
129
|
+
col_labels = []
|
|
130
|
+
ref_pos = 0
|
|
131
|
+
for i, base in enumerate(ref_row):
|
|
132
|
+
if base != "-":
|
|
133
|
+
ref_pos += 1
|
|
134
|
+
if ref_pos == 1 or ref_pos % 10 == 0:
|
|
135
|
+
col_labels.append((i, str(ref_pos)))
|
|
136
|
+
|
|
137
|
+
label = Path(path).stem
|
|
138
|
+
return Panel(label, ref_row, seq_rows, aln_len, col_labels)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# ═════════════════════════════════════════════════════════════════
|
|
142
|
+
# BAM panel builder
|
|
143
|
+
# ═════════════════════════════════════════════════════════════════
|
|
144
|
+
def build_read_row(read, ref_start, ref_end):
|
|
145
|
+
aligned = {}
|
|
146
|
+
inserts = defaultdict(list)
|
|
147
|
+
qpos, rpos = 0, read.reference_start
|
|
148
|
+
for op, length in read.cigartuples:
|
|
149
|
+
if op in (0, 7, 8):
|
|
150
|
+
for _ in range(length):
|
|
151
|
+
if ref_start <= rpos < ref_end:
|
|
152
|
+
aligned[rpos] = read.query_sequence[qpos].upper()
|
|
153
|
+
qpos += 1; rpos += 1
|
|
154
|
+
elif op == 1:
|
|
155
|
+
anchor = rpos - 1
|
|
156
|
+
if ref_start <= anchor < ref_end:
|
|
157
|
+
for j in range(length):
|
|
158
|
+
inserts[anchor].append(read.query_sequence[qpos + j].upper())
|
|
159
|
+
qpos += length
|
|
160
|
+
elif op == 2:
|
|
161
|
+
for _ in range(length):
|
|
162
|
+
if ref_start <= rpos < ref_end:
|
|
163
|
+
aligned[rpos] = "-"
|
|
164
|
+
rpos += 1
|
|
165
|
+
elif op == 3:
|
|
166
|
+
rpos += length
|
|
167
|
+
elif op == 4:
|
|
168
|
+
qpos += length
|
|
169
|
+
return aligned, inserts
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def bam_panel(bam_path, ref_path, region) -> Panel:
|
|
173
|
+
import pysam
|
|
174
|
+
|
|
175
|
+
chrom, rest = region.split(":")
|
|
176
|
+
start, end = [int(x) for x in rest.replace(",", "").split("-")]
|
|
177
|
+
|
|
178
|
+
fasta = pysam.FastaFile(ref_path)
|
|
179
|
+
ref_seq = fasta.fetch(chrom, start, end).upper()
|
|
180
|
+
fasta.close()
|
|
181
|
+
|
|
182
|
+
samfile = pysam.AlignmentFile(bam_path, "rb")
|
|
183
|
+
reads = [r for r in samfile.fetch(chrom, start, end)
|
|
184
|
+
if not r.is_unmapped and r.cigartuples]
|
|
185
|
+
samfile.close()
|
|
186
|
+
reads.sort(key=lambda r: (r.reference_start, r.is_reverse))
|
|
187
|
+
|
|
188
|
+
# Find max insertion at each ref position
|
|
189
|
+
max_ins = defaultdict(int)
|
|
190
|
+
read_data = []
|
|
191
|
+
for read in reads:
|
|
192
|
+
aligned, inserts = build_read_row(read, start, end)
|
|
193
|
+
read_data.append((read, aligned, inserts))
|
|
194
|
+
for rpos, bases in inserts.items():
|
|
195
|
+
max_ins[rpos] = max(max_ins[rpos], len(bases))
|
|
196
|
+
|
|
197
|
+
# Build column map
|
|
198
|
+
col_map = {}
|
|
199
|
+
ins_col_set = set()
|
|
200
|
+
col = 0
|
|
201
|
+
for rpos in range(start, end):
|
|
202
|
+
col_map[rpos] = col
|
|
203
|
+
col += 1
|
|
204
|
+
n_ins = max_ins.get(rpos, 0)
|
|
205
|
+
for j in range(n_ins):
|
|
206
|
+
ins_col_set.add(col + j)
|
|
207
|
+
col += n_ins
|
|
208
|
+
total_cols = col
|
|
209
|
+
|
|
210
|
+
# Build ref row with '-' in insertion columns
|
|
211
|
+
ref_row = []
|
|
212
|
+
for rpos in range(start, end):
|
|
213
|
+
ref_row.append(ref_seq[rpos - start])
|
|
214
|
+
for _ in range(max_ins.get(rpos, 0)):
|
|
215
|
+
ref_row.append("-")
|
|
216
|
+
|
|
217
|
+
# Build sequence rows
|
|
218
|
+
seq_rows = []
|
|
219
|
+
for read, aligned, inserts in read_data:
|
|
220
|
+
row = [" "] * total_cols
|
|
221
|
+
for rpos in range(start, end):
|
|
222
|
+
c = col_map[rpos]
|
|
223
|
+
if rpos in aligned:
|
|
224
|
+
row[c] = aligned[rpos]
|
|
225
|
+
if rpos in aligned or rpos in inserts:
|
|
226
|
+
n_ins = max_ins.get(rpos, 0)
|
|
227
|
+
read_ins = inserts.get(rpos, [])
|
|
228
|
+
for j in range(n_ins):
|
|
229
|
+
if j < len(read_ins):
|
|
230
|
+
row[c + 1 + j] = read_ins[j]
|
|
231
|
+
else:
|
|
232
|
+
row[c + 1 + j] = "-"
|
|
233
|
+
seq_rows.append((read.query_name, row, read.is_reverse))
|
|
234
|
+
|
|
235
|
+
# Column labels: 1-based relative, ticks at 1, 10, 20…
|
|
236
|
+
ref_width = end - start
|
|
237
|
+
tick_1based = [1] + list(range(10, ref_width + 1, 10))
|
|
238
|
+
col_labels = [(col_map[start + p - 1], str(p))
|
|
239
|
+
for p in tick_1based if (start + p - 1) < end]
|
|
240
|
+
|
|
241
|
+
label = Path(bam_path).stem
|
|
242
|
+
return Panel(label, ref_row, seq_rows, total_cols, col_labels, ins_col_set)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
# ═════════════════════════════════════════════════════════════════
|
|
246
|
+
# Renderer
|
|
247
|
+
# ═════════════════════════════════════════════════════════════════
|
|
248
|
+
def render_panels(panels, out_path="alignment.png", fontsize=7, dpi=300,
|
|
249
|
+
palette="nt", cell=0.14):
|
|
250
|
+
colors = AA_COLORS if palette == "aa" else NT_COLORS
|
|
251
|
+
mono = fm.FontProperties(family="monospace", size=fontsize)
|
|
252
|
+
mono_sm = fm.FontProperties(family="monospace", size=fontsize - 1)
|
|
253
|
+
sans_sm = {"fontsize": fontsize - 1, "fontfamily": "sans-serif"}
|
|
254
|
+
|
|
255
|
+
# Compute total height: for each panel, 1 ref row + N seq rows + 0.6 separator
|
|
256
|
+
max_cols = max(p.total_cols for p in panels)
|
|
257
|
+
total_rows = 0
|
|
258
|
+
panel_y_offsets = []
|
|
259
|
+
for i, p in enumerate(panels):
|
|
260
|
+
panel_y_offsets.append(total_rows)
|
|
261
|
+
total_rows += 1 + len(p.seq_rows) # ref + seqs
|
|
262
|
+
if i < len(panels) - 1:
|
|
263
|
+
total_rows += 1 # separator gap
|
|
264
|
+
|
|
265
|
+
fig_w = max(4, max_cols * cell + 0.5)
|
|
266
|
+
fig_h = max(1.0, total_rows * cell + 0.6)
|
|
267
|
+
fig, ax = plt.subplots(figsize=(fig_w, fig_h))
|
|
268
|
+
ax.set_xlim(-0.5, max_cols - 0.5)
|
|
269
|
+
ax.set_ylim(total_rows - 0.5, -0.5)
|
|
270
|
+
ax.set_aspect("equal")
|
|
271
|
+
|
|
272
|
+
for pi, panel in enumerate(panels):
|
|
273
|
+
y0 = panel_y_offsets[pi]
|
|
274
|
+
|
|
275
|
+
# ── Shade insertion columns ───────────────────────────────
|
|
276
|
+
n_panel_rows = 1 + len(panel.seq_rows)
|
|
277
|
+
for ic in panel.ins_columns:
|
|
278
|
+
ax.add_patch(plt.Rectangle(
|
|
279
|
+
(ic - 0.5, y0 - 0.5), 1, n_panel_rows,
|
|
280
|
+
facecolor=INS_BG, edgecolor="none", zorder=0))
|
|
281
|
+
|
|
282
|
+
# ── Reference row ─────────────────────────────────────────
|
|
283
|
+
for c, base in enumerate(panel.ref_row):
|
|
284
|
+
clr = GAP_COLOR if base == "-" else colors.get(base, "#9E9E9E")
|
|
285
|
+
ax.text(c, y0, base, ha="center", va="center",
|
|
286
|
+
fontproperties=mono, color=clr, fontweight="bold")
|
|
287
|
+
|
|
288
|
+
# ── Sequence rows ─────────────────────────────────────────
|
|
289
|
+
for ri, (name, row, is_rev) in enumerate(panel.seq_rows):
|
|
290
|
+
y = y0 + 1 + ri
|
|
291
|
+
alpha = REV_ALPHA if is_rev else FWD_ALPHA
|
|
292
|
+
strand_char = "," if is_rev else "."
|
|
293
|
+
|
|
294
|
+
for c, base in enumerate(row):
|
|
295
|
+
if base == " ":
|
|
296
|
+
continue
|
|
297
|
+
ref_base = panel.ref_row[c] if c < len(panel.ref_row) else "-"
|
|
298
|
+
|
|
299
|
+
if base == "-":
|
|
300
|
+
ax.text(c, y, "-", ha="center", va="center",
|
|
301
|
+
fontproperties=mono, color=GAP_COLOR, alpha=alpha)
|
|
302
|
+
elif base == ref_base:
|
|
303
|
+
ax.text(c, y, strand_char, ha="center", va="center",
|
|
304
|
+
fontproperties=mono_sm, color="#BDBDBD", alpha=alpha)
|
|
305
|
+
else:
|
|
306
|
+
ax.add_patch(plt.Rectangle((c - 0.5, y - 0.5), 1, 1,
|
|
307
|
+
facecolor=MISMATCH_BG,
|
|
308
|
+
edgecolor="none"))
|
|
309
|
+
display = base.lower() if is_rev else base
|
|
310
|
+
ax.text(c, y, display, ha="center", va="center",
|
|
311
|
+
fontproperties=mono,
|
|
312
|
+
color=colors.get(base, "#000000"),
|
|
313
|
+
fontweight="bold", alpha=alpha)
|
|
314
|
+
|
|
315
|
+
# ── Panel label (left side) ───────────────────────────────
|
|
316
|
+
if len(panels) > 1:
|
|
317
|
+
ax.text(-1.5, y0 + n_panel_rows / 2 - 0.5, panel.label,
|
|
318
|
+
ha="right", va="center", fontsize=fontsize,
|
|
319
|
+
fontfamily="sans-serif", fontstyle="italic",
|
|
320
|
+
color="#616161")
|
|
321
|
+
|
|
322
|
+
# ── Separator line between panels ─────────────────────────
|
|
323
|
+
if pi < len(panels) - 1:
|
|
324
|
+
sep_y = y0 + n_panel_rows + 0.0
|
|
325
|
+
ax.axhline(y=sep_y, color=SEPARATOR_COLOR, lw=0.5, ls="-",
|
|
326
|
+
xmin=0, xmax=1)
|
|
327
|
+
|
|
328
|
+
# ── X-axis from first panel, placed on top ────────────────────
|
|
329
|
+
ax.xaxis.set_label_position("top")
|
|
330
|
+
ax.xaxis.tick_top()
|
|
331
|
+
first = panels[0]
|
|
332
|
+
tick_idx = [ci for ci, _ in first.col_labels]
|
|
333
|
+
tick_lbl = [lb for _, lb in first.col_labels]
|
|
334
|
+
ax.set_xticks(tick_idx)
|
|
335
|
+
ax.set_xticklabels(tick_lbl, rotation=0, ha="center", **sans_sm)
|
|
336
|
+
ax.set_yticks([])
|
|
337
|
+
ax.tick_params(axis="x", length=0, pad=2)
|
|
338
|
+
ax.tick_params(axis="y", length=0)
|
|
339
|
+
for spine in ax.spines.values():
|
|
340
|
+
spine.set_visible(False)
|
|
341
|
+
|
|
342
|
+
plt.subplots_adjust(left=0.01, right=0.99, top=0.92, bottom=0.01)
|
|
343
|
+
plt.savefig(out_path, dpi=dpi, bbox_inches="tight", pad_inches=0.05,
|
|
344
|
+
facecolor="white", transparent=False)
|
|
345
|
+
plt.close()
|
|
346
|
+
print(f"Saved: {out_path} ({dpi} dpi, {len(panels)} panel(s), "
|
|
347
|
+
f"{max_cols} cols)")
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
if __name__ == "__main__":
|
|
351
|
+
from tview.cli import main
|
|
352
|
+
main()
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tview
|
|
3
|
+
Version: 0.1.dev4
|
|
4
|
+
Summary: Publication-quality alignment viewer for nucleotide and amino acid sequences
|
|
5
|
+
Author-email: Troy Sincomb <troysincomb@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/MurrellGroup/tview
|
|
8
|
+
Project-URL: Repository, https://github.com/MurrellGroup/tview
|
|
9
|
+
Project-URL: Issues, https://github.com/MurrellGroup/tview/issues
|
|
10
|
+
Keywords: bioinformatics,alignment,samtools,tview,visualization,BAM,FASTA
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: matplotlib>=3.5
|
|
27
|
+
Requires-Dist: click>=8.0
|
|
28
|
+
Requires-Dist: pysam>=0.20; sys_platform != "win32"
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
31
|
+
Requires-Dist: hypothesis>=6.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# tview
|
|
35
|
+
|
|
36
|
+
Publication-quality alignment viewer for nucleotide and amino acid sequences. A lightweight alternative to `samtools tview` that produces clean, stable image output.
|
|
37
|
+
|
|
38
|
+
Supports **BAM files** (with reference FASTA), **pre-aligned FASTA** (e.g. MAFFT output), and **stacking** multiple inputs into a single figure.
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+
*BAM mode — SNP (yellow), 3bp deletion, 2bp insertion (purple columns), reverse-strand insertion*
|
|
42
|
+
|
|
43
|
+

|
|
44
|
+
*FASTA mode — HIV Env protein alignment (HxB2 reference), amino acid palette*
|
|
45
|
+
|
|
46
|
+

|
|
47
|
+
*Stacked mode — two BAM files sharing a reference and region*
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install tview
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Installs `matplotlib`, `click`, and `pysam`.
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Quick Start
|
|
62
|
+
|
|
63
|
+
### BAM file
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
tview \
|
|
67
|
+
--bam aligned.bam \
|
|
68
|
+
--ref reference.fa \
|
|
69
|
+
--region chr1:100-200 \
|
|
70
|
+
-o alignment.png
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Aligned FASTA (e.g. MAFFT output)
|
|
74
|
+
|
|
75
|
+
The first sequence in the file is treated as the reference.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
tview \
|
|
79
|
+
--fasta env_protein_aligned.fasta \
|
|
80
|
+
--palette aa \
|
|
81
|
+
-o env_alignment.png
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Subset columns from a FASTA alignment
|
|
85
|
+
|
|
86
|
+
Use `--columns` with 1-based inclusive range to window into long alignments.
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
tview \
|
|
90
|
+
--fasta aligned.fasta \
|
|
91
|
+
--columns 1-120 \
|
|
92
|
+
--palette aa \
|
|
93
|
+
-o first_120_cols.png
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Stacking Multiple Panels
|
|
99
|
+
|
|
100
|
+
Each input file becomes a vertically stacked panel separated by a thin line. Panels are labeled on the left with the filename stem.
|
|
101
|
+
|
|
102
|
+
### Multiple BAMs (shared reference and region)
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
tview \
|
|
106
|
+
--bam sample1.bam --bam sample2.bam --bam sample3.bam \
|
|
107
|
+
--ref reference.fa \
|
|
108
|
+
--region chr1:100-200 \
|
|
109
|
+
-o stacked.png
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Multiple FASTAs
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
tview \
|
|
116
|
+
--fasta group1_aligned.fasta --fasta group2_aligned.fasta \
|
|
117
|
+
--palette aa \
|
|
118
|
+
--columns 1-120 \
|
|
119
|
+
-o comparison.png
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Mix BAM and FASTA panels
|
|
123
|
+
|
|
124
|
+
`--ref` and `--region` apply only to BAM panels; `--columns` applies only to FASTA panels.
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
tview \
|
|
128
|
+
--bam reads.bam \
|
|
129
|
+
--ref reference.fa \
|
|
130
|
+
--region chr1:100-200 \
|
|
131
|
+
--fasta protein_aligned.fasta \
|
|
132
|
+
--columns 1-120 \
|
|
133
|
+
-o mixed.png
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
BAM panels are rendered first (top), FASTA panels below.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Piping from stdin
|
|
141
|
+
|
|
142
|
+
Pass `-` to read file paths from stdin (one per line). Each path becomes its own panel.
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
# find → stacked panels
|
|
146
|
+
find ./alignments -name "*.fasta" -type f | \
|
|
147
|
+
tview --fasta - --palette aa --columns 1-120 -o all.png
|
|
148
|
+
|
|
149
|
+
# ls with pattern
|
|
150
|
+
ls samples/*.bam | \
|
|
151
|
+
tview --bam - --ref ref.fa --region chr1:100-200 -o all_samples.png
|
|
152
|
+
|
|
153
|
+
# single file via echo
|
|
154
|
+
echo "my_alignment.fasta" | \
|
|
155
|
+
tview --fasta - --palette aa -o out.png
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Python API
|
|
161
|
+
|
|
162
|
+
The core functions are available as a Python library:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from tview import fasta_panel, bam_panel, render_panels
|
|
166
|
+
|
|
167
|
+
# FASTA alignment
|
|
168
|
+
panel = fasta_panel("aligned.fasta", col_start=1, col_end=120)
|
|
169
|
+
render_panels([panel], "output.png", palette="aa")
|
|
170
|
+
|
|
171
|
+
# BAM alignment
|
|
172
|
+
panel = bam_panel("sample.bam", "reference.fa", "chr1:100-200")
|
|
173
|
+
render_panels([panel], "output.png")
|
|
174
|
+
|
|
175
|
+
# Stack multiple panels
|
|
176
|
+
panels = [
|
|
177
|
+
bam_panel("sample1.bam", "ref.fa", "chr1:100-200"),
|
|
178
|
+
bam_panel("sample2.bam", "ref.fa", "chr1:100-200"),
|
|
179
|
+
]
|
|
180
|
+
render_panels(panels, "stacked.png", dpi=300, fontsize=7, cell=0.14)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Visual Conventions
|
|
186
|
+
|
|
187
|
+
| Element | Symbol | Style |
|
|
188
|
+
|---------|--------|-------|
|
|
189
|
+
| Match (forward) | `.` | light grey |
|
|
190
|
+
| Match (reverse) | `,` | light grey, reduced opacity |
|
|
191
|
+
| Mismatch | `A` `T` etc. | colored, yellow highlight, bold |
|
|
192
|
+
| Mismatch (reverse) | `a` `t` etc. | lowercase, colored, yellow highlight |
|
|
193
|
+
| Deletion | `-` | grey dash |
|
|
194
|
+
| Insertion | colored bases | purple column shading |
|
|
195
|
+
| Gap (ref in insertion col) | `-` | grey dash |
|
|
196
|
+
| Gap (FASTA alignment) | `-` | grey dash |
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## Color Palettes
|
|
201
|
+
|
|
202
|
+
### `--palette nt` (default) — Nucleotides
|
|
203
|
+
|
|
204
|
+
| Base | Color |
|
|
205
|
+
|------|-------|
|
|
206
|
+
| A | green `#4CAF50` |
|
|
207
|
+
| C | blue `#2196F3` |
|
|
208
|
+
| G | orange `#FF9800` |
|
|
209
|
+
| T | red `#F44336` |
|
|
210
|
+
|
|
211
|
+
### `--palette aa` — Amino Acids (Clustal-inspired)
|
|
212
|
+
|
|
213
|
+
| Group | Residues | Color |
|
|
214
|
+
|-------|----------|-------|
|
|
215
|
+
| Hydrophobic | A V L I M F W P | blue `#2196F3` |
|
|
216
|
+
| Positive charge | K R H | red `#F44336` |
|
|
217
|
+
| Negative charge | D E | magenta `#E040FB` |
|
|
218
|
+
| Polar uncharged | S T N Q | green `#4CAF50` |
|
|
219
|
+
| Special | G C Y | orange `#FF9800` |
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Full Argument Reference
|
|
224
|
+
|
|
225
|
+
```
|
|
226
|
+
Usage: tview [OPTIONS]
|
|
227
|
+
|
|
228
|
+
Publication-quality alignment viewer (BAM or FASTA).
|
|
229
|
+
|
|
230
|
+
Options:
|
|
231
|
+
--bam TEXT BAM file(s) — each becomes a panel. Use '-' for stdin.
|
|
232
|
+
--ref PATH Reference FASTA (required for BAM mode).
|
|
233
|
+
--region TEXT Genomic region chr:start-end (required for BAM mode).
|
|
234
|
+
--fasta TEXT Aligned FASTA file(s) — each becomes a panel. Use '-' for stdin.
|
|
235
|
+
--columns TEXT Column range for FASTA, 1-based inclusive (e.g. 1-120).
|
|
236
|
+
-o, --output TEXT Output image path. [default: alignment.png]
|
|
237
|
+
--palette [nt|aa] Color palette. [default: nt]
|
|
238
|
+
--dpi INTEGER Image resolution. [default: 300]
|
|
239
|
+
--fontsize INTEGER Base font size in points. [default: 7]
|
|
240
|
+
--cell FLOAT Cell size in inches. [default: 0.14]
|
|
241
|
+
-h, --help Show this message and exit.
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
| Argument | Description | Default |
|
|
245
|
+
|----------|-------------|---------|
|
|
246
|
+
| `--bam` | BAM file(s), each becomes a panel. Use `-` for stdin. | — |
|
|
247
|
+
| `--ref` | Reference FASTA (required for BAM mode) | — |
|
|
248
|
+
| `--region` | Genomic region `chr:start-end` (required for BAM) | — |
|
|
249
|
+
| `--fasta` | Aligned FASTA file(s), each becomes a panel. Use `-` for stdin. | — |
|
|
250
|
+
| `--columns` | Column range for FASTA, 1-based inclusive (e.g. `1-120`) | full alignment |
|
|
251
|
+
| `-o, --output` | Output image path | `alignment.png` |
|
|
252
|
+
| `--palette` | Color palette: `nt` or `aa` | `nt` |
|
|
253
|
+
| `--dpi` | Image resolution | `300` |
|
|
254
|
+
| `--fontsize` | Base font size in points | `7` |
|
|
255
|
+
| `--cell` | Cell size in inches (controls spacing) | `0.14` |
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Tips for Publication Figures
|
|
260
|
+
|
|
261
|
+
- Use `--dpi 300` (default) for print, `--dpi 150` for drafts.
|
|
262
|
+
- Use `--cell 0.10` for denser layouts with many sequences, `--cell 0.18` for fewer.
|
|
263
|
+
- Use `--fontsize 5` or `6` when displaying wide alignments (>100 columns).
|
|
264
|
+
- The output format is determined by the file extension: `.png`, `.pdf`, `.svg` all work.
|
|
265
|
+
- For Nature-style figures, `.pdf` or `.svg` output preserves vector text.
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
# Vector output for publication
|
|
269
|
+
tview \
|
|
270
|
+
--fasta aligned.fasta \
|
|
271
|
+
--palette aa \
|
|
272
|
+
--columns 1-120 \
|
|
273
|
+
--cell 0.12 \
|
|
274
|
+
--fontsize 6 \
|
|
275
|
+
-o figure_2a.pdf
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## FASTA Input Format
|
|
281
|
+
|
|
282
|
+
The FASTA input must be **pre-aligned** (e.g. by MAFFT, MUSCLE, Clustal). The first sequence is used as the reference for comparison. Gap characters (`-`) in the alignment are preserved and rendered as grey dashes.
|
|
283
|
+
|
|
284
|
+
```
|
|
285
|
+
>HxB2_reference
|
|
286
|
+
MRVK---EKYQHLWRWGWRWGTMLLGMLMICS...
|
|
287
|
+
>sample_001
|
|
288
|
+
MRVKGIRKNAQHL----WRGGTLLLGMLMICS...
|
|
289
|
+
>sample_002
|
|
290
|
+
--------------------------MLMICS...
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
The x-axis labels count non-gap positions in the reference sequence (1, 10, 20, ...), so position numbers always correspond to the reference residue numbering regardless of gap columns.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
tview/__init__.py,sha256=m12Gy5pG-H4LN7v6-NqJztUCNos3IB9SPGDG7xphDZU,485
|
|
2
|
+
tview/_version.py,sha256=CCBWn0dt9vGR2xCuvN0-pJiL_EfW8sCq3mWcvm7dyfA,712
|
|
3
|
+
tview/cli.py,sha256=B5vFIHqvA6E9-P1_gjyHQVN_SfDdGC95mrhOrH2gwDE,2792
|
|
4
|
+
tview/tview.py,sha256=cY4PTjrg_FJDwmwVx_R7ZCWOa3XCIvgMY4iNwAOZ-6I,14511
|
|
5
|
+
tview-0.1.dev4.dist-info/licenses/LICENSE,sha256=PRvBVRcqmEyg2k5TekU4LBP_BQTdVALzv5F12ofRXbE,1069
|
|
6
|
+
tview-0.1.dev4.dist-info/METADATA,sha256=NG0vZCYVU14vA4VJpW6zVJzOqjaLXmzEuT68D0mEHh0,8610
|
|
7
|
+
tview-0.1.dev4.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
|
|
8
|
+
tview-0.1.dev4.dist-info/entry_points.txt,sha256=HhRjQleNNn0kcsGQlz4SJKCDUdleTr1TYZGO4VGNnj0,41
|
|
9
|
+
tview-0.1.dev4.dist-info/top_level.txt,sha256=hgiJ_IUu49L4gILDZLAQB0d2APmCCSGsLfGoganQZ4Y,6
|
|
10
|
+
tview-0.1.dev4.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Troy Sincomb
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tview
|