$npx -y skills add GPTomics/bioSkills --skill alignment-ioRead, write, and convert multiple sequence alignment files using Biopython Bio.AlignIO. Supports Clustal, PHYLIP, Stockholm, FASTA, Nexus, and other alignment formats for phylogenetics and conservation analysis. Use when reading, writing, or converting alignment file formats.
| 1 | ## Version Compatibility |
| 2 | |
| 3 | Reference examples tested with: BioPython 1.83+ |
| 4 | |
| 5 | Before using code patterns, verify installed versions match. If versions differ: |
| 6 | - Python: `pip show <package>` then `help(module.function)` to check signatures |
| 7 | |
| 8 | If code throws ImportError, AttributeError, or TypeError, introspect the installed |
| 9 | package and adapt the example to match the actual API rather than retrying. |
| 10 | |
| 11 | # Alignment File I/O |
| 12 | |
| 13 | Read, write, and convert multiple sequence alignment files in various formats. |
| 14 | |
| 15 | ## Required Import |
| 16 | |
| 17 | **Goal:** Load modules for reading, writing, and manipulating multiple sequence alignments. |
| 18 | |
| 19 | **Approach:** Import AlignIO for file I/O and supporting classes for programmatic alignment construction. |
| 20 | |
| 21 | ```python |
| 22 | from Bio import AlignIO |
| 23 | from Bio.Align import MultipleSeqAlignment |
| 24 | from Bio.SeqRecord import SeqRecord |
| 25 | from Bio.Seq import Seq |
| 26 | ``` |
| 27 | |
| 28 | ## Format Coverage Map |
| 29 | |
| 30 | Three Python libraries cover the alignment-format space, with overlapping but non-identical support. Pick by what is actually required. |
| 31 | |
| 32 | | Format | `Bio.AlignIO` | `Bio.Align` (modern) | `pyhmmer.easel` | Notes | |
| 33 | |--------|---------------|----------------------|-----------------|-------| |
| 34 | | Aligned FASTA | R/W | R/W | R/W | Most portable; loses annotations | |
| 35 | | Clustal | R/W | R/W | R | Clustal conservation marks NOT round-tripped | |
| 36 | | PHYLIP (interleaved/sequential/relaxed) | R/W | R/W | R | Strict 10-char names is silent footgun | |
| 37 | | Stockholm | R/W | R/W | R/W | Only format preserving GS/GR/GC/GF annotations | |
| 38 | | NEXUS | R/W | R/W | -- | MrBayes / PAUP* input | |
| 39 | | MAF (Multiple Alignment Format) | R/W | R/W | -- | UCSC whole-genome alignments | |
| 40 | | A2M / A3M | -- (use `'fasta'` parser then post-process) | -- | R/W | HMMER (a2m), HHsuite/ColabFold (a3m) | |
| 41 | | MSF (GCG) | R | -- | -- | GCG legacy | |
| 42 | | EMBOSS / Mauve XMFA / FASTA-m10 | R | partial | -- | One-way: read-only | |
| 43 | |
| 44 | **Formats NOT in BioPython** (use dedicated tools): |
| 45 | |
| 46 | | Format | Tool | Why | |
| 47 | |--------|------|-----| |
| 48 | | HAL | progressiveCactus, halTools | HDF5-backed multi-genome alignments at TB scale | |
| 49 | | chain / net | UCSC Kent tools (`liftOver`, `chainNet`) | Pairwise genome alignment | |
| 50 | | AXT | BLASTZ / lastz native | Pairwise alignment blocks | |
| 51 | | PSL | UCSC Kent tools (`pslPretty`, `blat`) | BLAT alignment summary | |
| 52 | | GFA / rGFA | `vg`, `odgi`, `pggb`, gfatools | Pangenome graph | |
| 53 | | GAF | `vg surject`, `vg call` | Graph alignment format (read-to-graph) | |
| 54 | |
| 55 | Recommend `Bio.Align` (modern API) over `Bio.AlignIO` (legacy) for new code; it returns `Alignment` objects with built-in `.counts()` and `.substitutions` properties. For multi-gigabyte Stockholm databases such as Pfam-A.full, `pyhmmer.easel.MSAFile` streams record-by-record where `Bio.AlignIO.parse` works but at higher per-record cost. |
| 56 | |
| 57 | ## Reading Alignments |
| 58 | |
| 59 | **"Read an alignment file"** -> Parse an alignment file into an alignment object with sequences and metadata accessible. |
| 60 | |
| 61 | **Goal:** Load alignment data from files in various formats (Clustal, PHYLIP, Stockholm, FASTA). |
| 62 | |
| 63 | **Approach:** Use `AlignIO.read()` for single-alignment files or `AlignIO.parse()` for files containing multiple alignments. |
| 64 | |
| 65 | ### Single Alignment File |
| 66 | ```python |
| 67 | from Bio import AlignIO |
| 68 | |
| 69 | alignment = AlignIO.read('alignment.aln', 'clustal') |
| 70 | print(f'Alignment length: {alignment.get_alignment_length()}') |
| 71 | print(f'Number of sequences: {len(alignment)}') |
| 72 | ``` |
| 73 | |
| 74 | ### Multiple Alignments in One File |
| 75 | ```python |
| 76 | for alignment in AlignIO.parse('multi_alignment.sto', 'stockholm'): |
| 77 | print(f'Alignment with {len(alignment)} sequences, length {alignment.get_alignment_length()}') |
| 78 | ``` |
| 79 | |
| 80 | ### Read as List |
| 81 | ```python |
| 82 | alignments = list(AlignIO.parse('alignments.phy', 'phylip')) |
| 83 | print(f'Read {len(alignments)} alignments') |
| 84 | ``` |
| 85 | |
| 86 | ## Writing Alignments |
| 87 | |
| 88 | **Goal:** Save alignment data to files in standard formats for downstream tools or archival. |
| 89 | |
| 90 | **Approach:** Use `AlignIO.write()` with the target format specifier, supporting single or multiple alignments and file handles. |
| 91 | |
| 92 | ### Write Single Alignment |
| 93 | ```python |
| 94 | AlignIO.write(alignment, 'output.fasta', 'fasta') |
| 95 | ``` |
| 96 | |
| 97 | ### Write Multiple Alignments |
| 98 | ```python |
| 99 | alignments = [alignment1, alignment2, alignment3] |
| 100 | count = AlignIO.write(alignments, 'output.sto', 'stockholm') |
| 101 | print(f'Wrote {count} alignments') |
| 102 | ``` |
| 103 | |
| 104 | ### Write to Handle |
| 105 | ```python |
| 106 | with open('output.aln', 'w') as handle: |
| 107 | AlignIO.write(alignment, handle, 'clustal') |
| 108 | ``` |
| 109 | |
| 110 | ## Format Conversion |
| 111 | |
| 112 | **"Convert alignment format"** -> Transform an alignment file from one format to another (e.g., Clustal to PHYLIP). |
| 113 | |
| 114 | **Goal:** Convert alignment files between formats for compatibility with different analysis tools. |
| 115 | |
| 116 | **Approach:** Use `AlignIO.conve |