bioalign 0.1.0__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.
- bioalign-0.1.0/LICENSE +21 -0
- bioalign-0.1.0/PKG-INFO +108 -0
- bioalign-0.1.0/README.md +82 -0
- bioalign-0.1.0/bioalign/__init__.py +35 -0
- bioalign-0.1.0/bioalign/cli.py +183 -0
- bioalign-0.1.0/bioalign/core.py +442 -0
- bioalign-0.1.0/bioalign/export.py +146 -0
- bioalign-0.1.0/bioalign/phylo.py +268 -0
- bioalign-0.1.0/bioalign/remote.py +148 -0
- bioalign-0.1.0/bioalign.egg-info/PKG-INFO +108 -0
- bioalign-0.1.0/bioalign.egg-info/SOURCES.txt +17 -0
- bioalign-0.1.0/bioalign.egg-info/dependency_links.txt +1 -0
- bioalign-0.1.0/bioalign.egg-info/entry_points.txt +2 -0
- bioalign-0.1.0/bioalign.egg-info/requires.txt +1 -0
- bioalign-0.1.0/bioalign.egg-info/top_level.txt +1 -0
- bioalign-0.1.0/pyproject.toml +52 -0
- bioalign-0.1.0/setup.cfg +4 -0
- bioalign-0.1.0/setup.py +4 -0
- bioalign-0.1.0/tests/test_bioalign.py +107 -0
bioalign-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alper Karagöl and Taner Karagöl
|
|
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.
|
bioalign-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bioalign
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Homology search, multiple sequence alignment, Shannon conservation analytics, Neighbor-Joining phylogenetics, and multi-format MSA conversion
|
|
5
|
+
Author-email: Alper Karagöl <akaragol@mit.edu>, Taner Karagöl <karagolt@mit.edu>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.karagolresearch.com/bioalign
|
|
8
|
+
Project-URL: Documentation, https://www.karagolresearch.com/bioalign
|
|
9
|
+
Project-URL: Preprint, https://doi.org/10.1101/2024.08.01.606148
|
|
10
|
+
Keywords: bioinformatics,multiple-sequence-alignment,phylo,neighbor-joining,conservation,blast,clustal-omega,mafft
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
21
|
+
Requires-Python: >=3.8
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: numpy>=1.20.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# bioalign
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/bioalign/) [](https://opensource.org/licenses/MIT)
|
|
30
|
+
|
|
31
|
+
`bioalign` is the standalone Python library and command-line interface for the [BioAlign](https://www.karagolresearch.com/bioalign) workbench. It combines local affine-gap progressive alignment (BLOSUM62 and nucleotide scoring), optional EMBL-EBI Job Dispatcher remote search and alignment (`ncbiblast`, `clustalo`, `muscle`, `mafft`, `tcoffee`, `kalign`), per-column Shannon entropy and conservation profiling, Poisson-corrected distance matrices, Neighbor-Joining and UPGMA tree reconstruction, and multi-format alignment conversion (`FASTA`, `Clustal`, `PHYLIP`, `Stockholm`, `MSF`, `NEXUS`, `Newick`, `SVG`, `CSV`, and `JSON`).
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install bioalign
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Command-Line Usage
|
|
40
|
+
|
|
41
|
+
### 1. Align Unaligned Sequences (Offline Progressive Needleman-Wunsch or Remote EMBL-EBI)
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
bioalign align unaligned.fasta -o aligned.fasta
|
|
45
|
+
bioalign align unaligned.fasta --engine clustalo --email researcher@institute.edu -o aligned.fasta
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 2. Compute Conservation, Shannon Entropy, Consensus, and Pairwise Identity
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
bioalign stats aligned.fasta -o alignment_summary.json --csv per_column_metrics.csv
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 3. Reconstruct Neighbor-Joining or UPGMA Trees (Newick + Publication SVG)
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
bioalign tree aligned.fasta --method nj -o tree.nwk --svg tree.svg
|
|
58
|
+
bioalign tree aligned.fasta --method upgma --cladogram --svg cladogram.svg
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 4. Convert Alignments Across Standard Formats
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
bioalign convert aligned.fasta --to clustal -o aligned.aln
|
|
65
|
+
bioalign convert aligned.fasta --to stockholm -o aligned.sto
|
|
66
|
+
bioalign convert aligned.fasta --to phylip -o aligned.phy
|
|
67
|
+
bioalign convert aligned.fasta --to nexus -o aligned.nex
|
|
68
|
+
bioalign convert aligned.fasta --to msf -o aligned.msf
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 5. Run Homolog Search + Alignment Pipeline via EMBL-EBI Job Dispatcher
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
bioalign search query.fasta --email researcher@institute.edu --align clustalo -o results_dir
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Python API
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from bioalign import (
|
|
81
|
+
align_sequences,
|
|
82
|
+
build_tree,
|
|
83
|
+
compute_alignment_stats,
|
|
84
|
+
format_alignment,
|
|
85
|
+
parse_fasta,
|
|
86
|
+
to_newick,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
records = parse_fasta("unaligned.fasta")
|
|
90
|
+
aligned = align_sequences(records)
|
|
91
|
+
stats = compute_alignment_stats(aligned)
|
|
92
|
+
tree = build_tree(
|
|
93
|
+
[r.id for r in aligned],
|
|
94
|
+
stats["identity_matrix"],
|
|
95
|
+
method="nj",
|
|
96
|
+
)
|
|
97
|
+
print("Consensus:", stats["consensus"])
|
|
98
|
+
print("Newick:", to_newick(tree))
|
|
99
|
+
print(format_alignment(aligned, fmt="clustal"))
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Citation
|
|
103
|
+
|
|
104
|
+
Karagöl, A., Karagöl, T. An Evolutionary Statistics Toolkit for Simplified Sequence Analysis on Web with Client-Side Processing. *bioRxiv* (2024). https://doi.org/10.1101/2024.08.01.606148
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Released under the MIT License.
|
bioalign-0.1.0/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# bioalign
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/bioalign/) [](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
`bioalign` is the standalone Python library and command-line interface for the [BioAlign](https://www.karagolresearch.com/bioalign) workbench. It combines local affine-gap progressive alignment (BLOSUM62 and nucleotide scoring), optional EMBL-EBI Job Dispatcher remote search and alignment (`ncbiblast`, `clustalo`, `muscle`, `mafft`, `tcoffee`, `kalign`), per-column Shannon entropy and conservation profiling, Poisson-corrected distance matrices, Neighbor-Joining and UPGMA tree reconstruction, and multi-format alignment conversion (`FASTA`, `Clustal`, `PHYLIP`, `Stockholm`, `MSF`, `NEXUS`, `Newick`, `SVG`, `CSV`, and `JSON`).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install bioalign
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Command-Line Usage
|
|
14
|
+
|
|
15
|
+
### 1. Align Unaligned Sequences (Offline Progressive Needleman-Wunsch or Remote EMBL-EBI)
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bioalign align unaligned.fasta -o aligned.fasta
|
|
19
|
+
bioalign align unaligned.fasta --engine clustalo --email researcher@institute.edu -o aligned.fasta
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 2. Compute Conservation, Shannon Entropy, Consensus, and Pairwise Identity
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bioalign stats aligned.fasta -o alignment_summary.json --csv per_column_metrics.csv
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 3. Reconstruct Neighbor-Joining or UPGMA Trees (Newick + Publication SVG)
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
bioalign tree aligned.fasta --method nj -o tree.nwk --svg tree.svg
|
|
32
|
+
bioalign tree aligned.fasta --method upgma --cladogram --svg cladogram.svg
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 4. Convert Alignments Across Standard Formats
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bioalign convert aligned.fasta --to clustal -o aligned.aln
|
|
39
|
+
bioalign convert aligned.fasta --to stockholm -o aligned.sto
|
|
40
|
+
bioalign convert aligned.fasta --to phylip -o aligned.phy
|
|
41
|
+
bioalign convert aligned.fasta --to nexus -o aligned.nex
|
|
42
|
+
bioalign convert aligned.fasta --to msf -o aligned.msf
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 5. Run Homolog Search + Alignment Pipeline via EMBL-EBI Job Dispatcher
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
bioalign search query.fasta --email researcher@institute.edu --align clustalo -o results_dir
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Python API
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from bioalign import (
|
|
55
|
+
align_sequences,
|
|
56
|
+
build_tree,
|
|
57
|
+
compute_alignment_stats,
|
|
58
|
+
format_alignment,
|
|
59
|
+
parse_fasta,
|
|
60
|
+
to_newick,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
records = parse_fasta("unaligned.fasta")
|
|
64
|
+
aligned = align_sequences(records)
|
|
65
|
+
stats = compute_alignment_stats(aligned)
|
|
66
|
+
tree = build_tree(
|
|
67
|
+
[r.id for r in aligned],
|
|
68
|
+
stats["identity_matrix"],
|
|
69
|
+
method="nj",
|
|
70
|
+
)
|
|
71
|
+
print("Consensus:", stats["consensus"])
|
|
72
|
+
print("Newick:", to_newick(tree))
|
|
73
|
+
print(format_alignment(aligned, fmt="clustal"))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Citation
|
|
77
|
+
|
|
78
|
+
Karagöl, A., Karagöl, T. An Evolutionary Statistics Toolkit for Simplified Sequence Analysis on Web with Client-Side Processing. *bioRxiv* (2024). https://doi.org/10.1101/2024.08.01.606148
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
Released under the MIT License.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
|
|
5
|
+
from .core import (
|
|
6
|
+
BioAlignError,
|
|
7
|
+
SequenceRecord,
|
|
8
|
+
align_sequences,
|
|
9
|
+
compute_alignment_stats,
|
|
10
|
+
detect_sequence_type,
|
|
11
|
+
parse_fasta,
|
|
12
|
+
poisson_distance_matrix,
|
|
13
|
+
)
|
|
14
|
+
from .export import SUPPORTED_FORMATS, format_alignment
|
|
15
|
+
from .phylo import PhyloNode, build_tree, render_tree_svg, to_newick
|
|
16
|
+
from .remote import run_ebi_blast, run_ebi_msa
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"__version__",
|
|
20
|
+
"BioAlignError",
|
|
21
|
+
"SequenceRecord",
|
|
22
|
+
"PhyloNode",
|
|
23
|
+
"SUPPORTED_FORMATS",
|
|
24
|
+
"parse_fasta",
|
|
25
|
+
"detect_sequence_type",
|
|
26
|
+
"align_sequences",
|
|
27
|
+
"compute_alignment_stats",
|
|
28
|
+
"poisson_distance_matrix",
|
|
29
|
+
"build_tree",
|
|
30
|
+
"to_newick",
|
|
31
|
+
"render_tree_svg",
|
|
32
|
+
"format_alignment",
|
|
33
|
+
"run_ebi_msa",
|
|
34
|
+
"run_ebi_blast",
|
|
35
|
+
]
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import csv
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Optional, Sequence
|
|
9
|
+
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .core import (
|
|
12
|
+
BioAlignError,
|
|
13
|
+
SequenceRecord,
|
|
14
|
+
align_sequences,
|
|
15
|
+
compute_alignment_stats,
|
|
16
|
+
parse_fasta,
|
|
17
|
+
)
|
|
18
|
+
from .export import SUPPORTED_FORMATS, format_alignment
|
|
19
|
+
from .phylo import build_tree, render_tree_svg, to_newick
|
|
20
|
+
from .remote import EBI_MSA_ENGINES, run_ebi_blast, run_ebi_msa
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
24
|
+
ap = argparse.ArgumentParser(
|
|
25
|
+
prog="bioalign",
|
|
26
|
+
description="Homology search, multiple sequence alignment, Shannon conservation analytics, and Neighbor-Joining phylogenetics.",
|
|
27
|
+
)
|
|
28
|
+
ap.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
29
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
30
|
+
|
|
31
|
+
p_aln = sub.add_parser("align", help="Align unaligned FASTA sequences locally or via EMBL-EBI")
|
|
32
|
+
p_aln.add_argument("input", help="Input unaligned FASTA path")
|
|
33
|
+
p_aln.add_argument(
|
|
34
|
+
"--engine",
|
|
35
|
+
choices=("local", *EBI_MSA_ENGINES),
|
|
36
|
+
default="local",
|
|
37
|
+
help="Alignment engine [default: local progressive affine-gap Needleman-Wunsch]",
|
|
38
|
+
)
|
|
39
|
+
p_aln.add_argument("--email", default="", help="Contact email (required for remote EMBL-EBI engines)")
|
|
40
|
+
p_aln.add_argument("--format", choices=SUPPORTED_FORMATS, default="fasta", help="Output alignment format [default: fasta]")
|
|
41
|
+
p_aln.add_argument("-o", "--output", default="", help="Output file path (prints to stdout if omitted)")
|
|
42
|
+
|
|
43
|
+
p_st = sub.add_parser("stats", help="Compute conservation (%), Shannon entropy (bits), consensus, and identity matrix")
|
|
44
|
+
p_st.add_argument("input", help="Aligned FASTA path")
|
|
45
|
+
p_st.add_argument("-o", "--output", default="", help="Output summary JSON path (prints to stdout if omitted)")
|
|
46
|
+
p_st.add_argument("--csv", default="", help="Optional per-column CSV output path")
|
|
47
|
+
|
|
48
|
+
p_tr = sub.add_parser("tree", help="Build Neighbor-Joining or UPGMA phylogenetic tree from aligned FASTA")
|
|
49
|
+
p_tr.add_argument("input", help="Aligned FASTA path")
|
|
50
|
+
p_tr.add_argument("--method", choices=("nj", "upgma"), default="nj", help="Tree algorithm [default: nj]")
|
|
51
|
+
p_tr.add_argument("-o", "--output", default="", help="Output Newick (.nwk) path (prints to stdout if omitted)")
|
|
52
|
+
p_tr.add_argument("--svg", default="", help="Optional publication SVG tree path")
|
|
53
|
+
p_tr.add_argument("--cladogram", action="store_true", help="Render equal-step cladogram branch layout in SVG")
|
|
54
|
+
|
|
55
|
+
p_cv = sub.add_parser("convert", help="Convert aligned FASTA into Clustal, PHYLIP, Stockholm, MSF, NEXUS, or FASTA")
|
|
56
|
+
p_cv.add_argument("input", help="Aligned FASTA path")
|
|
57
|
+
p_cv.add_argument("--to", required=True, choices=SUPPORTED_FORMATS, help="Target MSA format")
|
|
58
|
+
p_cv.add_argument("-o", "--output", default="", help="Output file path (prints to stdout if omitted)")
|
|
59
|
+
|
|
60
|
+
p_sr = sub.add_parser("search", help="Run NCBI BLAST+ homolog search via EMBL-EBI and align retrieved homologs")
|
|
61
|
+
p_sr.add_argument("input", help="Query FASTA path (first sequence is used as query)")
|
|
62
|
+
p_sr.add_argument("--email", required=True, help="Contact email required by EMBL-EBI Job Dispatcher")
|
|
63
|
+
p_sr.add_argument("--program", choices=("blastp", "blastn"), default="blastp", help="BLAST program [default: blastp]")
|
|
64
|
+
p_sr.add_argument("--database", default="uniprotkb_swissprot", help="Target database [default: uniprotkb_swissprot]")
|
|
65
|
+
p_sr.add_argument("--max-hits", type=int, default=20, help="Maximum homologs to retrieve [default: 20]")
|
|
66
|
+
p_sr.add_argument("--align", choices=("local", *EBI_MSA_ENGINES), default="clustalo", help="MSA engine [default: clustalo]")
|
|
67
|
+
p_sr.add_argument("-o", "--outdir", required=True, help="Output directory for hits CSV, aligned FASTA, Newick tree, and stats JSON")
|
|
68
|
+
return ap
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def main(argv: Optional[Sequence[str]] = None) -> None:
|
|
72
|
+
ap = build_parser()
|
|
73
|
+
args = ap.parse_args(argv)
|
|
74
|
+
try:
|
|
75
|
+
if args.cmd == "align":
|
|
76
|
+
recs = parse_fasta(Path(args.input))
|
|
77
|
+
if args.engine == "local":
|
|
78
|
+
aligned = align_sequences(recs)
|
|
79
|
+
else:
|
|
80
|
+
aligned = run_ebi_msa(recs, engine=args.engine, email=args.email)
|
|
81
|
+
rendered = format_alignment(aligned, fmt=args.format)
|
|
82
|
+
if args.output:
|
|
83
|
+
Path(args.output).write_text(rendered, encoding="utf-8")
|
|
84
|
+
print(f"Aligned {len(aligned)} sequences ({len(aligned[0].seq)} columns) -> {args.output}")
|
|
85
|
+
else:
|
|
86
|
+
sys.stdout.write(rendered)
|
|
87
|
+
|
|
88
|
+
elif args.cmd == "stats":
|
|
89
|
+
recs = parse_fasta(Path(args.input))
|
|
90
|
+
stats = compute_alignment_stats(recs)
|
|
91
|
+
if args.csv:
|
|
92
|
+
cons = stats["conservation_pct"]
|
|
93
|
+
ent = stats["shannon_entropy_bits"]
|
|
94
|
+
gaps = stats["column_gap_pct"]
|
|
95
|
+
consensus = str(stats["consensus"])
|
|
96
|
+
with open(args.csv, "w", newline="", encoding="utf-8") as fh:
|
|
97
|
+
w = csv.writer(fh)
|
|
98
|
+
w.writerow(["column", "consensus_residue", "conservation_pct", "shannon_entropy_bits", "gap_pct"])
|
|
99
|
+
for idx in range(int(stats["alignment_length"])):
|
|
100
|
+
w.writerow([idx + 1, consensus[idx], cons[idx], ent[idx], gaps[idx]])
|
|
101
|
+
payload = json.dumps(stats, indent=2)
|
|
102
|
+
if args.output:
|
|
103
|
+
Path(args.output).write_text(payload + "\n", encoding="utf-8")
|
|
104
|
+
print(f"Wrote alignment statistics -> {args.output}")
|
|
105
|
+
else:
|
|
106
|
+
print(payload)
|
|
107
|
+
|
|
108
|
+
elif args.cmd == "tree":
|
|
109
|
+
recs = parse_fasta(Path(args.input))
|
|
110
|
+
stats = compute_alignment_stats(recs)
|
|
111
|
+
tree = build_tree(
|
|
112
|
+
[r.id for r in recs],
|
|
113
|
+
stats["identity_matrix"], # type: ignore[arg-type]
|
|
114
|
+
method=args.method,
|
|
115
|
+
)
|
|
116
|
+
nwk = to_newick(tree)
|
|
117
|
+
if args.svg:
|
|
118
|
+
label = "Neighbor-Joining" if args.method == "nj" else "UPGMA"
|
|
119
|
+
svg = render_tree_svg(tree, method_label=label, cladogram=args.cladogram)
|
|
120
|
+
Path(args.svg).write_text(svg, encoding="utf-8")
|
|
121
|
+
if args.output:
|
|
122
|
+
Path(args.output).write_text(nwk + "\n", encoding="utf-8")
|
|
123
|
+
print(f"Wrote {args.method.upper()} Newick tree -> {args.output}")
|
|
124
|
+
else:
|
|
125
|
+
print(nwk)
|
|
126
|
+
|
|
127
|
+
elif args.cmd == "convert":
|
|
128
|
+
recs = parse_fasta(Path(args.input))
|
|
129
|
+
out_txt = format_alignment(recs, fmt=args.to)
|
|
130
|
+
if args.output:
|
|
131
|
+
Path(args.output).write_text(out_txt, encoding="utf-8")
|
|
132
|
+
print(f"Converted {len(recs)} sequences to {args.to} -> {args.output}")
|
|
133
|
+
else:
|
|
134
|
+
sys.stdout.write(out_txt)
|
|
135
|
+
|
|
136
|
+
elif args.cmd == "search":
|
|
137
|
+
queries = parse_fasta(Path(args.input))
|
|
138
|
+
query = queries[0]
|
|
139
|
+
hits = run_ebi_blast(
|
|
140
|
+
query=query,
|
|
141
|
+
email=args.email,
|
|
142
|
+
program=args.program,
|
|
143
|
+
database=args.database,
|
|
144
|
+
max_hits=args.max_hits,
|
|
145
|
+
)
|
|
146
|
+
outdir = Path(args.outdir)
|
|
147
|
+
outdir.mkdir(parents=True, exist_ok=True)
|
|
148
|
+
with open(outdir / "blast_homolog_hits.csv", "w", newline="", encoding="utf-8") as fh:
|
|
149
|
+
w = csv.DictWriter(
|
|
150
|
+
fh,
|
|
151
|
+
fieldnames=["id", "accession", "description", "identity_pct", "evalue", "sequence"],
|
|
152
|
+
)
|
|
153
|
+
w.writeheader()
|
|
154
|
+
w.writerows(hits)
|
|
155
|
+
|
|
156
|
+
pool = [query]
|
|
157
|
+
seen = {query.id}
|
|
158
|
+
for h in hits:
|
|
159
|
+
hid = str(h["id"])
|
|
160
|
+
if hid not in seen:
|
|
161
|
+
seen.add(hid)
|
|
162
|
+
pool.append(SequenceRecord(id=hid, seq=str(h["sequence"]), description=str(h["description"])))
|
|
163
|
+
|
|
164
|
+
if len(pool) >= 2:
|
|
165
|
+
if args.align == "local":
|
|
166
|
+
aligned = align_sequences(pool)
|
|
167
|
+
else:
|
|
168
|
+
aligned = run_ebi_msa(pool, engine=args.align, email=args.email)
|
|
169
|
+
(outdir / "aligned.fasta").write_text(format_alignment(aligned, "fasta"), encoding="utf-8")
|
|
170
|
+
stats = compute_alignment_stats(aligned)
|
|
171
|
+
(outdir / "alignment_stats.json").write_text(json.dumps(stats, indent=2) + "\n", encoding="utf-8")
|
|
172
|
+
tree = build_tree([r.id for r in aligned], stats["identity_matrix"], method="nj") # type: ignore[arg-type]
|
|
173
|
+
(outdir / "tree_nj.nwk").write_text(to_newick(tree) + "\n", encoding="utf-8")
|
|
174
|
+
(outdir / "tree_nj.svg").write_text(render_tree_svg(tree), encoding="utf-8")
|
|
175
|
+
print(f"Completed BioAlign homolog pipeline ({len(hits)} hits) -> {outdir}")
|
|
176
|
+
|
|
177
|
+
except (BioAlignError, OSError) as exc:
|
|
178
|
+
print(f"[bioalign] Error: {exc}", file=sys.stderr)
|
|
179
|
+
sys.exit(1)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
if __name__ == "__main__":
|
|
183
|
+
main()
|