Assembly API¶
Module for full-length TCR sequence assembly.
Overview¶
The assembly module builds full-length TCR sequences from CDR3 and V/J gene information. It supports:
- Leader sequences: Per-chain configuration - extract from contig FASTAs or use standard signal peptides
- Constant regions: Fetch from Ensembl (TRAC, TRBC1, TRBC2) or use built-in sequences
- 2A linkers: Join alpha and beta chains with self-cleaving peptides (T2A, P2A, E2A, F2A)
- Single-chain constructs: Generate β-linker-α format for expression
Leader Sequence Options¶
Each chain (alpha and beta) can have its own leader configuration:
| Option | Description |
|---|---|
None |
No leader sequence |
"from_contig" |
Extract native leader from CellRanger FASTA |
"CD8A", "CD28", etc. |
Use a standard signal peptide |
Default configuration: CD28 on alpha chain, CD8A on beta chain (distinct leaders for identification).
Available Leader Sequences¶
| Leader | Source | Species | Sequence |
|---|---|---|---|
| CD8A | CD8A signal peptide (UniProt P01732) | Human | MALPVTALLLPLALLLHAARP |
| CD28 | CD28 signal peptide (UniProt P10747) | Human | MLRLLLALNLFPSIQVTG |
| IgK | IgGκ light chain signal peptide | Mouse | METDTLLLWVLLLWVPGSTG |
| TRAC | TCR alpha constant signal peptide | Human | MAGTWLLLLLALGCPALPTG |
| TRBC | TCR beta constant signal peptide | Human | MGTSLLCWMALCLLGADHADG |
Available Linkers¶
| Linker | Source | Sequence |
|---|---|---|
| T2A | Thosea asigna virus | EGRGSLLTCGDVEENPGP |
| P2A | Porcine teschovirus-1 | GSGATNFSLLKQAGDVEENPGP |
| E2A | Equine rhinitis A virus | QCTNYALLKLAGDVESNPGP |
| F2A | Foot-and-mouth disease virus | VKQTLNFDLLKLAGDVESNPGP |
Usage Examples¶
from tcrsift import assemble_full_sequences
# Default: CD28 on alpha, CD8A on beta
assembled = assemble_full_sequences(clonotypes)
# Custom leaders
assembled = assemble_full_sequences(
clonotypes,
alpha_leader="CD8A",
beta_leader="CD28",
)
# Leader only on beta chain (first in 2A construct)
assembled = assemble_full_sequences(
clonotypes,
alpha_leader=None,
beta_leader="CD8A",
)
# Extract native leaders from contigs
assembled = assemble_full_sequences(
clonotypes,
contigs_dir="/path/to/contigs",
alpha_leader="from_contig",
beta_leader="from_contig",
)
# No leaders at all
assembled = assemble_full_sequences(
clonotypes,
alpha_leader=None,
beta_leader=None,
)
API Reference¶
assemble ¶
Full-length TCR sequence assembly for TCRsift.
Builds complete TCR sequences including leader peptides and constant regions.
CODON_TABLE
module-attribute
¶
CODON_TABLE = {'ATA': 'I', 'ATC': 'I', 'ATT': 'I', 'ATG': 'M', 'ACA': 'T', 'ACC': 'T', 'ACG': 'T', 'ACT': 'T', 'AAC': 'N', 'AAT': 'N', 'AAA': 'K', 'AAG': 'K', 'AGC': 'S', 'AGT': 'S', 'AGA': 'R', 'AGG': 'R', 'CTA': 'L', 'CTC': 'L', 'CTG': 'L', 'CTT': 'L', 'CCA': 'P', 'CCC': 'P', 'CCG': 'P', 'CCT': 'P', 'CAC': 'H', 'CAT': 'H', 'CAA': 'Q', 'CAG': 'Q', 'CGA': 'R', 'CGC': 'R', 'CGG': 'R', 'CGT': 'R', 'GTA': 'V', 'GTC': 'V', 'GTG': 'V', 'GTT': 'V', 'GCA': 'A', 'GCC': 'A', 'GCG': 'A', 'GCT': 'A', 'GAC': 'D', 'GAT': 'D', 'GAA': 'E', 'GAG': 'E', 'GGA': 'G', 'GGC': 'G', 'GGG': 'G', 'GGT': 'G', 'TCA': 'S', 'TCC': 'S', 'TCG': 'S', 'TCT': 'S', 'TTC': 'F', 'TTT': 'F', 'TTA': 'L', 'TTG': 'L', 'TAC': 'Y', 'TAT': 'Y', 'TAA': '*', 'TAG': '*', 'TGC': 'C', 'TGT': 'C', 'TGA': '*', 'TGG': 'W'}
LINKERS
module-attribute
¶
LINKERS = {'T2A': {'dna': 'GAGGGCAGAGGAAGTCTGCTAACATGCGGTGACGTCGAGGAGAATCCTGGCCCG', 'aa': 'EGRGSLLTCGDVEENPGP', 'source': 'Thosea asigna virus'}, 'P2A': {'dna': 'GGAAGCGGAGCTACTAACTTCAGCCTGCTGAAGCAGGCTGGAGACGTGGAGGAGAACCCTGGACCT', 'aa': 'GSGATNFSLLKQAGDVEENPGP', 'source': 'Porcine teschovirus-1'}, 'E2A': {'dna': 'CAGTGTACTAATTATGCTCTCTTGAAATTGGCTGGAGATGTTGAGAGCAACCCAGGTCCC', 'aa': 'QCTNYALLKLAGDVESNPGP', 'source': 'Equine rhinitis A virus'}, 'F2A': {'dna': 'GTGAAACAGACTTTGAATTTTGACCTTCTCAAGTTGGCGGGAGACGTGGAGTCCAACCCAGGGCCC', 'aa': 'VKQTLNFDLLKLAGDVESNPGP', 'source': 'Foot-and-mouth disease virus'}}
DEFAULT_LEADERS
module-attribute
¶
DEFAULT_LEADERS = {'CD8A': {'aa': 'MALPVTALLLPLALLLHAARP', 'dna': 'ATGGCCCTGCCTGTGACAGCCCTGCTGCTGCCTCTGGCTCTGCTGCTGCATGCCGCTAGACCC', 'source': 'Human CD8A signal peptide (UniProt P01732)', 'species': 'human'}, 'CD28': {'aa': 'MLRLLLALNLFPSIQVTG', 'dna': 'ATGCTCCGCCTGCTGCTGGCCCTGAACCTGTTCCCCAGCATCCAGGTGACCGGC', 'source': 'Human CD28 signal peptide (UniProt P10747)', 'species': 'human'}, 'IgK': {'aa': 'METDTLLLWVLLLWVPGSTG', 'dna': 'ATGGAGACAGACACACTCCTGCTATGGGTACTGCTGCTCTGGGTTCCAGGTTCCACTGGT', 'source': 'Murine IgGκ light chain signal peptide', 'species': 'mouse', 'note': 'Widely used for high secretion efficiency in mammalian expression'}, 'TRAC': {'aa': 'MAGTWLLLLLALGCPALPTG', 'dna': 'ATGGCTGGCACCTGGCTGCTGCTGCTGCTGGCCCTGGGATGCCCAGCACTGCCCACAGGC', 'source': 'Human TRAC native signal peptide', 'species': 'human'}, 'TRBC': {'aa': 'MGTSLLCWMALCLLGADHADG', 'dna': 'ATGGGCACCAGCCTGCTGTGCTGGATGGCCCTGTGCCTGCTGGGAGCAGACCACGCCGATGGC', 'source': 'Human TRBC native signal peptide', 'species': 'human'}}
assemble_full_sequences ¶
assemble_full_sequences(clonotypes: DataFrame, contigs_dir: str | Path | None = None, alpha_leader: str | None = 'CD28', beta_leader: str | None = 'CD8A', include_constant: bool = True, constant_source: str = 'ensembl', linker: str = 'T2A', trac_allele: str = 'auto', trbc1_allele: str = 'auto', trbc2_allele: str = 'auto', stop_codons: tuple[str, ...] = ('TAA', 'TGA'), verbose: bool = True, show_progress: bool = True, sample_name_from: str = 'parent', cellranger_dir: str | Path | None = None, sample_sheet: DataFrame | None = None, leader_fallback: str = 'germline', force_alpha_leader: str | None = None, force_beta_leader: str | None = None, secondary_alpha_leader: str | None = None, secondary_beta_leader: str | None = None, codon_optimization: str = 'focal', max_codon_repeats: int = 1, avoid_enzymes: tuple[str, ...] = ()) -> pd.DataFrame
Assemble full-length TCR sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clonotypes
|
DataFrame
|
Clonotype DataFrame with VDJ sequences (from fwr1/cdr1/fwr2/cdr2/fwr3/cdr3/fwr4) |
required |
contigs_dir
|
str or Path
|
Directory with CellRanger contig FASTA files. Required if alpha_leader or beta_leader is set to "from_contig". |
None
|
alpha_leader
|
str or None
|
Leader sequence for alpha chain. Options: - None: No leader sequence - "from_contig": Extract native leader from contig FASTA (requires contigs_dir) - Key from DEFAULT_LEADERS: "CD8A", "CD28", "IgK", "TRAC", "TRBC" Default is "CD28" to provide distinct sequences from beta chain. |
'CD28'
|
beta_leader
|
str or None
|
Leader sequence for beta chain. Same options as alpha_leader. Default is "CD8A" to provide distinct sequences from alpha chain. |
'CD8A'
|
leader_fallback
|
str or None
|
Curated signal peptide (CD8A/CD28/IgK/TRAC/TRBC) to substitute when a
|
'germline'
|
include_constant
|
bool
|
Include constant region sequences. |
True
|
constant_source
|
str
|
Source for constant regions:
|
'ensembl'
|
linker
|
str
|
Linker sequence for single-chain constructs: "T2A", "P2A", "E2A", "F2A" |
'T2A'
|
trac_allele
|
str
|
Per-gene allele selection (#113). Default |
'auto'
|
trbc1_allele
|
str
|
Per-gene allele selection (#113). Default |
'auto'
|
trbc2_allele
|
str
|
Per-gene allele selection (#113). Default |
'auto'
|
stop_codons
|
tuple of str
|
Stop codons appended to the codon-optimized constant CDS.
Default uses two non-redundant stops (recognized by
different release factors → reduced read-through in
synthesized constructs). Pass |
``("TAA", "TGA")``
|
verbose
|
bool
|
Print progress information |
True
|
show_progress
|
bool
|
Show progress bar |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Clonotypes with full sequences added. Allele audit columns emitted per clone (#113):
Constant NT triad emitted per chain (#116):
The same triad is also exposed for |
Examples:
>>> # Default: CD28 on alpha, CD8A on beta (distinct leaders)
>>> assembled = assemble_full_sequences(clonotypes)
>>> # No leader sequences
>>> assembled = assemble_full_sequences(clonotypes, alpha_leader=None, beta_leader=None)
>>> # Leader only on beta chain (first in 2A construct)
>>> assembled = assemble_full_sequences(clonotypes, alpha_leader=None, beta_leader="CD8A")
>>> # Extract native leaders from contig FASTAs
>>> assembled = assemble_full_sequences(
... clonotypes,
... contigs_dir="/path/to/contigs",
... alpha_leader="from_contig",
... beta_leader="from_contig",
... )
Source code in tcrsift/assemble.py
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 | |
translate_dna ¶
Translate DNA sequence to amino acids.
Returns:
| Type | Description |
|---|---|
tuple
|
(amino_acid_sequence, ragged_3p_nucleotides) |
Source code in tcrsift/assemble.py
find_longest_orf ¶
Find and translate the longest open reading frame.
Returns:
| Type | Description |
|---|---|
tuple
|
(amino_acid_sequence, start_offset, ragged_3p_nucleotides) |
Source code in tcrsift/assemble.py
parse_fasta ¶
Parse a FASTA file.
Returns:
| Type | Description |
|---|---|
dict
|
Mapping from sequence ID to sequence |
Source code in tcrsift/assemble.py
load_contigs ¶
load_contigs(contig_dir: str | Path | None = None, *, sample_name_from: str = 'parent', cellranger_dir: str | Path | None = None, sample_sheet: DataFrame | None = None) -> dict[str, dict[str, str]]
Load contig sequences from a CellRanger-style output tree (#124).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contig_dir
|
str or Path
|
Root directory to scan for |
None
|
sample_name_from
|
('parent', 'grandparent', 'sheet')
|
How to derive the sample name from each discovered FASTA's path:
|
'parent'
|
cellranger_dir
|
str or Path
|
Shorthand for |
None
|
sample_sheet
|
DataFrame
|
Required when |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Nested dict: |
Source code in tcrsift/assemble.py
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 | |
get_constant_region_sequences ¶
Return human TCR constant-region CDS (DNA) via codon-aware back- translation.
Sources NT from :data:HUMAN_CONSTANT_REGIONS_AA and
:func:optimize_codons (motif-aware — avoids 5+ mononucleotide
runs and common Type-II restriction sites; see #116). The earlier
pyensembl-backed implementation read the full mRNA at frame
offset 2 and silently truncated TRAC / TRBC1 / TRBC2 to 2–11
residues for every assembled clonotype (#66). Hardcoding
eliminates the frame bug, drops the pyensembl dependency, and
matches the canonical sequences that downstream cloning
constructs need (#67).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop_codons
|
tuple of str
|
Stop codons to append to each CDS. Default is the two non-redundant stops (different release factors → reduces read-through in synthesized constructs). |
``("TAA", "TGA")``
|
Returns:
| Type | Description |
|---|---|
dict
|
Gene name → CDS (DNA, post-junction-codon-aligned … stop). Each CDS is the codon-optimized translation of the canonical AA plus the requested stop codons. |
Source code in tcrsift/assemble.py
validate_sequences ¶
validate_sequences(df: DataFrame, strict: bool = False, fix: bool = False) -> list[ValidationMessage]
Validate assembled sequences end-to-end per row (#67, #68).
Per-chain checks on each full_{chain}_aa row:
- Length window (200–450 aa).
- CDR3 substring present in the assembled sequence.
- Constant region's canonical C-terminus (from
:data:
CONSTANT_REGION_ENDINGS). - Constant region's canonical start (first 8+ residues of
:data:
HUMAN_CONSTANT_REGIONS_AA). - Constant length floor (:data:
CONSTANT_AA_FLOOR). - No premature stop codon (
*mid-chain). - Methionine start (when a leader was included).
- Standard residue alphabet only (:data:
_VALID_AA_CHARS). - Byte-for-byte equality of
leader + vdj + constantand the assembledfull_{chain}_aa, when all three parts are present.
Cross-row checks:
- β-chain J→C in-cis parity:
TRBJ1*pairs with TRBC1,TRBJ2*with TRBC2. - Single-chain integrity: linker present in
single_chain_aa. - Per-row
qc_warnings(stashed by_add_constant_regions) are surfaced.
Returns a flat list of :class:ValidationMessage (a str
subclass with .idx and .severity attributes). When
strict is True, raises :class:TCRsiftValidationError if any
load-bearing checks fail. Informational notes ("didn't have enough
info to check") are returned but never raise — distinguishing
those from a silent pass was the gap that hid #66.
When fix is True, autocorrect β J→C parity mismatches in-place
via :func:fix_jc_parity before validating (#89). Autocorrection
notes are prepended to the returned messages and the input frame
is mutated unconditionally — the mutation survives even when a
later check fails and strict=True raises.
Source code in tcrsift/assemble.py
4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 | |
export_fasta ¶
Export sequences to FASTA format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame with sequences |
required |
output_path
|
str or Path
|
Output file path |
required |
sequence_col
|
str
|
Column containing sequences to export |
'single_chain_aa'
|