| 1 |
# This program is public domain |
| 2 |
# Author: Paul Kienzle |
| 3 |
""" |
| 4 |
Biomolecule support. |
| 5 |
|
| 6 |
:class:`Molecule` lets you define biomolecules with labile hydrogen atoms |
| 7 |
specified using H[1] in the chemical formula. The biomolecule object creates |
| 8 |
forms with natural isotope ratio, all hydrogen and all deuterium. Density can be |
| 9 |
provided as natural density or cell volume. A %D2O contrast match value is |
| 10 |
computed for matching the molecule SLD in the presence of labile hydrogens. |
| 11 |
:meth:`Molecule.D2Osld` computes the neutron SLD for the solvated molecule in a |
| 12 |
%D2O solvent. |
| 13 |
|
| 14 |
:class:`Sequence` lets you read amino acid and DNA/RNA sequences from FASTA |
| 15 |
files. |
| 16 |
|
| 17 |
Tables for common molecules are provided[1]: |
| 18 |
|
| 19 |
*AMINO_ACID_CODES* : amino acids indexed by FASTA code |
| 20 |
|
| 21 |
*RNA_CODES*, DNA_CODES* : nucleic bases indexed by FASTA code |
| 22 |
|
| 23 |
*RNA_BASES*, DNA_BASES* : individual nucleic acid bases |
| 24 |
|
| 25 |
*NUCLEIC_ACID_COMPONENTS*, *LIPIDS*, *CARBOHYDRATE_RESIDUES* |
| 26 |
|
| 27 |
Neutron SLD for water at 20C is also provided as *H2O_SLD* and *D2O_SLD*. |
| 28 |
|
| 29 |
For unmodified protein an H and an OH are added for terminations. |
| 30 |
|
| 31 |
Assumes that proteins were created in an environment with the usual H/D isotope |
| 32 |
ratio on the nonlabile hydrogen. |
| 33 |
|
| 34 |
The value of residue volumes differs from that used by the bio scattering |
| 35 |
calculators from ISIS and ORSO, which will lead to different values for SLD. |
| 36 |
There are small differences for the number of hydrogen in His and Cys residues, |
| 37 |
where one table considers them present but labile and the other considers them |
| 38 |
absent. |
| 39 |
|
| 40 |
DNA and RNA residues from the source[1] included sodium in the chemical formula, |
| 41 |
but these have been removed and will not appear in the sequence. Volumes for DNA |
| 42 |
and RNA residues come from Buckin (1989) as reported in Durchlag (1997), with |
| 43 |
correction for phosphorylation and dehydration. The correction value of 30.39 |
| 44 |
comes from comparison of the volume given in Harroun (2006) to the volumes of |
| 45 |
the RNA ACGU and DNA T nucleosides given in Buckin (1989) after correcting for |
| 46 |
units. Harroun doesn't give volumes for DNA AGC nucleosides despite them being |
| 47 |
different (especially guanosine). This code uses the values from Buckin for |
| 48 |
these as well, rather than the RNA nucleoside values given in Harroun. Note that |
| 49 |
the computed density for equal parts AGCT is 1.67, compared to the measured |
| 50 |
average of 1.70 given in Arrighi (1970). |
| 51 |
|
| 52 |
[1] Perkins, S.J. (1988). Chapter 6 X-Ray and Neutron Solution Scattering, in: |
| 53 |
New Comprehensive Biochemistry. Elsevier, pp. 143-265. |
| 54 |
https://doi.org/10.1016/S0167-7306(08)60575-X |
| 55 |
|
| 56 |
[2] Buckin, V. A., B. I. Kankiya, and R. L. Kazaryan (1989). Hydration of |
| 57 |
nucleosides in dilute aqueous solutions: ultrasonic velocity and density |
| 58 |
measurements. Biophysical chemistry 34.3 211-223. |
| 59 |
https://doi.org/10.1016/0301-4622(89)80060-2 |
| 60 |
|
| 61 |
[3] Durchschlag, H. and Zipper, P. (1997). Calculation of partial specific |
| 62 |
volumes and other volumetric properties of small molecules and polymers. Journal |
| 63 |
of Applied Chemistry 30 803-807. https://doi.org/10.1107/S0021889897003348 |
| 64 |
|
| 65 |
[4] Harroun, T.A., Wignall, G.D., Katsaras, J. (2006). Neutron Scattering for |
| 66 |
Biology. In: Neutron Scattering in Biology. Springer, Berlin, Heidelberg. |
| 67 |
https://doi.org/10.1007/3-540-29111-3_1 |
| 68 |
|
| 69 |
[5] Arrighi, F.E., Mandel, M., Bergendahl, J. et al. (1970). Buoyant densities |
| 70 |
of DNA of mammals. Biochem Genet 4, 367–376. https://doi.org/10.1007/BF00485753 |
| 71 |
""" |
| 72 |
import warnings |
| 73 |
from pathlib import Path |
| 74 |
# Warning: name clash with Sequence |
| 75 |
from collections.abc import Iterator |
| 76 |
from typing import IO, cast |
| 77 |
|
| 78 |
from .formulas import formula as parse_formula, Formula, FormulaInput |
| 79 |
from .nsf import neutron_sld |
| 80 |
from .xsf import xray_sld |
| 81 |
from .core import default_table, Atom |
| 82 |
from .constants import avogadro_number |
| 83 |
|
| 84 |
# CRUFT 1.5.2: retaining fasta.isotope_substitution for compatibility |
| 85 |
def isotope_substitution(formula: Formula, source: Atom, target: Atom, portion: float=1.0): |
| 86 |
""" |
| 87 |
Substitute one atom/isotope in a formula with another in some proportion. |
| 88 |
|
| 89 |
*formula* is the formula being updated. |
| 90 |
|
| 91 |
*source* is the isotope/element to be substituted. |
| 92 |
|
| 93 |
*target* is the replacement isotope/element. |
| 94 |
|
| 95 |
*portion* is the proportion of source which is substituted for target. |
| 96 |
|
| 97 |
.. deprecated:: 1.5.3 |
| 98 |
Use formula.replace(source, target, portion) instead. |
| 99 |
""" |
| 100 |
return formula.replace(source, target, portion=portion) |
| 101 |
|
| 102 |
# TODO: allow Molecule to be used as compound in formulas.formula() |
| 103 |
class Molecule: |
| 104 |
""" |
| 105 |
Specify a biomolecule by name, chemical formula, cell volume and charge. |
| 106 |
|
| 107 |
Labile hydrogen positions should be coded using H[1] rather than H. |
| 108 |
H[1] will be substituded with H for solutions with natural water |
| 109 |
or D for solutions with heavy water. Any deuterated non-labile hydrogen |
| 110 |
can be marked with D, and they will stay as D regardless of the solvent. |
| 111 |
|
| 112 |
*name* is the molecule name. |
| 113 |
|
| 114 |
*formula* is the chemical formula as string or atom dictionary, with |
| 115 |
H[1] for labile hydrogen. |
| 116 |
|
| 117 |
*cell_volume* is the volume of the molecule. If None, cell volume will |
| 118 |
be inferred from the natural density of the molecule. Cell volume is |
| 119 |
assumed to be independent of isotope. |
| 120 |
|
| 121 |
*density* is the natural density of the molecule. If None, density will |
| 122 |
be inferred from cell volume. |
| 123 |
|
| 124 |
*charge* is the overall charge on the molecule. Note that charge can be |
| 125 |
fractional if the molecule is the average of a statistical ensemble. |
| 126 |
|
| 127 |
**Attributes** |
| 128 |
|
| 129 |
*labile_formula* is the original formula, with H[1] for the labile H. |
| 130 |
You can retrieve the deuterated from using:: |
| 131 |
|
| 132 |
molecule.labile_formula.replace(elements.H[1], elements.D) |
| 133 |
|
| 134 |
*natural_formula* has H substituted for H[1] in *labile_formula*. |
| 135 |
|
| 136 |
*D2Omatch* is percentage of D2O by volume in H2O required to match the |
| 137 |
SLD of the molecule, including substitution of labile hydrogen in |
| 138 |
proportion to the D/H ratio in the solvent. Values will be outside |
| 139 |
the range [0, 100] if the contrast match is impossible. |
| 140 |
|
| 141 |
*sld*/*Dsld* are the the SLDs of the molecule with H[1] replaced by |
| 142 |
naturally occurring H/D ratios and pure D respectively. |
| 143 |
|
| 144 |
*mass*/*Dmass* are the masses for natural H/D and pure D respectively. |
| 145 |
|
| 146 |
*charge* is the charge on the molecule |
| 147 |
|
| 148 |
*cell_volume* is the estimated cell volume for the molecule |
| 149 |
|
| 150 |
*density* is the estimated molecule density |
| 151 |
|
| 152 |
Change 1.5.3: drop *Hmass* and *Hsld*. Move *formula* to *labile_formula*. |
| 153 |
Move *Hnatural* to *formula*. |
| 154 |
""" |
| 155 |
name: str |
| 156 |
cell_volume: float |
| 157 |
sld: float |
| 158 |
Dsld: float |
| 159 |
mass: float |
| 160 |
Dmass: float |
| 161 |
D2Omatch: float |
| 162 |
charge: float # fractional to allow ensemble average molecules |
| 163 |
natural_formula: Formula |
| 164 |
labile_formula: Formula |
| 165 |
formula: Formula |
| 166 |
code: str # fasta code for dna/rna |
| 167 |
|
| 168 |
def __init__( |
| 169 |
self, |
| 170 |
name: str, |
| 171 |
formula: FormulaInput, |
| 172 |
cell_volume: float|None=None, |
| 173 |
density: float|None=None, |
| 174 |
charge: float=0, |
| 175 |
): |
| 176 |
# TODO: fasta does not work with table substitution |
| 177 |
elements = default_table() |
| 178 |
|
| 179 |
# Fill in density or cell_volume. |
| 180 |
M = parse_formula(formula, natural_density=density) |
| 181 |
# CRUFT: use of T rather than H[1] is deprecated since 1.5.3 |
| 182 |
if elements.T in M.atoms: |
| 183 |
warnings.warn("Use of tritium for labile hydrogen is deprecated." |
| 184 |
" Use H[1] instead of T in your formula.") |
| 185 |
M = M.replace(elements.T, elements.H[1]) |
| 186 |
if cell_volume is not None: |
| 187 |
# Note: cell_volume is only zero if there are no components |
| 188 |
M.density = 1e24*M.molecular_mass/cell_volume if cell_volume > 0 else 0 |
| 189 |
#print name, M.molecular_mass, cell_volume, M.density |
| 190 |
else: |
| 191 |
assert M.density is not None, "Need density to compute cell volume" |
| 192 |
cell_volume = 1e24*M.molecular_mass/M.density |
| 193 |
|
| 194 |
H = M.replace(elements.H[1], elements.H) |
| 195 |
D = M.replace(elements.H[1], elements.D) |
| 196 |
|
| 197 |
self.name = name |
| 198 |
self.cell_volume = cell_volume |
| 199 |
self.sld, self.Dsld = neutron_sld(H)[0], neutron_sld(D)[0] |
| 200 |
self.mass, self.Dmass = H.mass, D.mass |
| 201 |
self.D2Omatch = D2Omatch(self.sld, self.Dsld) |
| 202 |
self.charge = charge |
| 203 |
self.natural_formula = H |
| 204 |
self.labile_formula = M |
| 205 |
|
| 206 |
# TODO: formula should be natural_formula to be consistent |
| 207 |
# with sld and mass, which are computed with H-substitution. |
| 208 |
self.formula = self.labile_formula |
| 209 |
|
| 210 |
# TODO: are sld values float or complex? |
| 211 |
def D2Osld(self, volume_fraction: float=1., D2O_fraction: float=0.) -> float: |
| 212 |
""" |
| 213 |
Neutron SLD of the molecule in a deuterated solvent. |
| 214 |
|
| 215 |
Changed 1.5.3: fix errors in SLD calculations. |
| 216 |
""" |
| 217 |
solvent_sld = D2O_fraction*D2O_SLD + (1-D2O_fraction)*H2O_SLD |
| 218 |
solute_sld = D2O_fraction*self.Dsld + (1-D2O_fraction)*self.sld |
| 219 |
return volume_fraction*solute_sld + (1-volume_fraction)*solvent_sld |
| 220 |
|
| 221 |
class Sequence(Molecule): |
| 222 |
""" |
| 223 |
Convert FASTA sequence into chemical formula. |
| 224 |
|
| 225 |
*name* sequence name |
| 226 |
|
| 227 |
*sequence* code string |
| 228 |
|
| 229 |
*type* is one of:: |
| 230 |
|
| 231 |
aa: amino acid sequence |
| 232 |
dna: dna sequence |
| 233 |
rna: rna sequence |
| 234 |
|
| 235 |
Note: rna sequence files treat T as U and dna sequence files treat U as T. |
| 236 |
""" |
| 237 |
sequence: str |
| 238 |
|
| 239 |
@staticmethod |
| 240 |
def loadall(filename: Path|str, type: str|None=None) -> Iterator["Sequence"]: |
| 241 |
""" |
| 242 |
Iterate over sequences in FASTA file, loading each in turn. |
| 243 |
|
| 244 |
Yields one FASTA sequence each cycle. |
| 245 |
""" |
| 246 |
type = _guess_type_from_filename(str(filename), type) |
| 247 |
with open(filename, 'rt') as fh: |
| 248 |
for name, seq in read_fasta(fh): |
| 249 |
yield Sequence(name, seq, type=type) |
| 250 |
|
| 251 |
@staticmethod |
| 252 |
def load(filename: Path|str, type=None) -> "Sequence": |
| 253 |
""" |
| 254 |
Load the first FASTA sequence from a file. |
| 255 |
""" |
| 256 |
type = _guess_type_from_filename(str(filename), type) |
| 257 |
with open(filename, 'rt') as fh: |
| 258 |
name, seq = next(read_fasta(fh)) |
| 259 |
return Sequence(name, seq, type=type) |
| 260 |
|
| 261 |
def __init__(self, name: str, sequence: str, type: str='aa'): |
| 262 |
# TODO: duplicated in Molecule.__init__ |
| 263 |
# TODO: fasta does not work with table substitution |
| 264 |
elements = default_table() |
| 265 |
|
| 266 |
codes = CODE_TABLES[type] |
| 267 |
sequence = sequence.split('*', 1)[0] # stop at first '*' |
| 268 |
sequence = sequence.replace(' ', '') # ignore spaces |
| 269 |
parts = tuple(codes[c] for c in sequence) |
| 270 |
cell_volume = sum(p.cell_volume for p in parts) |
| 271 |
charge = sum(p.charge for p in parts) |
| 272 |
structure = [] |
| 273 |
for p in parts: |
| 274 |
structure.extend(list(p.labile_formula.structure)) |
| 275 |
# Add H + OH terminators to the sequence |
| 276 |
structure.extend(((2, elements.H[1]), (1, elements.O))) |
| 277 |
formula = parse_formula(structure).hill |
| 278 |
|
| 279 |
Molecule.__init__( |
| 280 |
self, name, formula, cell_volume=cell_volume, charge=charge) |
| 281 |
self.sequence = sequence |
| 282 |
|
| 283 |
def _guess_type_from_filename(filename: str, type: str|None) -> str: |
| 284 |
if type is None: |
| 285 |
if filename.endswith('.fna'): |
| 286 |
type = 'dna' |
| 287 |
elif filename.endswith('.ffn'): |
| 288 |
type = 'dna' |
| 289 |
elif filename.endswith('.faa'): |
| 290 |
type = 'aa' |
| 291 |
elif filename.endswith('.frn'): |
| 292 |
type = 'rna' |
| 293 |
else: |
| 294 |
type = 'aa' |
| 295 |
return type |
| 296 |
|
| 297 |
# PAK: Fixed in 1.5.3. Previous calculation used H2O density rather than D2O. |
| 298 |
#: real portion of H2O sld at 20 C |
| 299 |
H2O_SLD = neutron_sld("[email protected]")[0] |
| 300 |
#: real portion of D2O sld at 20 C |
| 301 |
#: Change 1.5.2: Use correct density in SLD calculation |
| 302 |
D2O_SLD = neutron_sld("[email protected]")[0] |
| 303 |
def D2Omatch(Hsld: float, Dsld: float) -> float: |
| 304 |
""" |
| 305 |
Find the D2O% concentration of solvent such that neutron SLD of the |
| 306 |
material matches the neutron SLD of the solvent. |
| 307 |
|
| 308 |
*Hsld*, *Dsld* are the SLDs for the hydrogenated and deuterated forms |
| 309 |
of the material respectively, where *D* includes all the labile protons |
| 310 |
swapped for deuterons. Water SLD is calculated at 20 C. |
| 311 |
|
| 312 |
Note that the resulting percentage is only meaningful between |
| 313 |
0% to 100%. Beyond 100% you will need an additional constrast agent |
| 314 |
in the 100% D2O solvent to increase the SLD enough to match. |
| 315 |
|
| 316 |
.. deprecated:: 1.5.3 |
| 317 |
Use periodictable.nsf.D2O_match(formula) instead. |
| 318 |
|
| 319 |
Change 1.5.3: corrected D2O sld, which will change the computed match point. |
| 320 |
""" |
| 321 |
# SLD(%Dsample + (1-%)Hsample) = SLD(%D2O + (1-%)H2O) |
| 322 |
# => %SLD(Dsample) + (1-%)SLD(Hsample) = %SLD(D2O) + (1-%)SLD(H2O) |
| 323 |
# => %(SLD(Dsample) - SLD(Hsample) + SLD(H2O) - SLD(D2O)) |
| 324 |
# = SLD(H2O) - SLD(Hsample) |
| 325 |
# => % = 100*(SLD(H2O) - SLD(Hsample)) |
| 326 |
# / (SLD(Dsample) - SLD(Hsample) + SLD(H2O) - SLD(D2O)) |
| 327 |
return 100 * (H2O_SLD - Hsld) / (Dsld - Hsld + H2O_SLD - D2O_SLD) |
| 328 |
|
| 329 |
|
| 330 |
def read_fasta(fp: IO[str]) -> Iterator[tuple[str, str]]: |
| 331 |
""" |
| 332 |
Iterate over the sequences in a FASTA file. |
| 333 |
|
| 334 |
Each iteration is a pair (sequence name, sequence codes). |
| 335 |
|
| 336 |
Change 1.5.3: Now uses H[1] rather than T for labile hydrogen. |
| 337 |
""" |
| 338 |
name = "" |
| 339 |
seq: list[str] = [] |
| 340 |
for line in fp: |
| 341 |
line = line.rstrip() |
| 342 |
if line.startswith(">"): |
| 343 |
if name: |
| 344 |
yield name, ''.join(seq) |
| 345 |
name, seq = line, [] |
| 346 |
else: |
| 347 |
seq.append(line) |
| 348 |
if name: |
| 349 |
yield name, ''.join(seq) |
| 350 |
|
| 351 |
def _code_average(bases, code_table) -> tuple[Formula, float, float]: |
| 352 |
""" |
| 353 |
Compute average over possible nucleotides, assuming equal weight if |
| 354 |
precise nucleotide is not known. |
| 355 |
|
| 356 |
Note: averaging can lead to a fractional charge on the returned molecule. |
| 357 |
""" |
| 358 |
n = len(bases) |
| 359 |
formula, cell_volume, charge = parse_formula(), 0., 0. |
| 360 |
for c in bases: |
| 361 |
base = code_table[c] |
| 362 |
formula += base.labile_formula |
| 363 |
cell_volume += base.cell_volume |
| 364 |
charge += base.charge |
| 365 |
if n > 0: |
| 366 |
formula, cell_volume, charge = (1/n) * formula, cell_volume/n, charge/n |
| 367 |
return formula, cell_volume, charge |
| 368 |
|
| 369 |
def _set_amino_acid_average(target: str, codes: str, name: str|None=None) -> None: |
| 370 |
""" |
| 371 |
Fill in partial unknowns for amino acids, such as "B" for aspartic acid or asparagine. |
| 372 |
""" |
| 373 |
formula, cell_volume, charge = _code_average(codes, AMINO_ACID_CODES) |
| 374 |
if name is None: |
| 375 |
name = "/".join(AMINO_ACID_CODES[c].name for c in codes) |
| 376 |
molecule = Molecule(name, formula, cell_volume=cell_volume, charge=charge) |
| 377 |
AMINO_ACID_CODES[target] = molecule |
| 378 |
|
| 379 |
# TODO: importing fasta does work, computing the neutron SLD for each molecule. |
| 380 |
# This triggers nsf.init() which defines the neutron data for each isotope. |
| 381 |
# Further, this does not allow private tables for fasta calculations. |
| 382 |
|
| 383 |
# FASTA code table |
| 384 |
def _(code: str, V: float, formula: str, name: str) -> tuple[str, Molecule]: |
| 385 |
if formula[-1] == '-': |
| 386 |
charge = -1 |
| 387 |
formula = formula[:-1] |
| 388 |
elif formula[-1] == '+': |
| 389 |
charge = +1 |
| 390 |
formula = formula[:-1] |
| 391 |
else: |
| 392 |
charge = 0 |
| 393 |
molecule = Molecule(name, formula, cell_volume=V, charge=charge) |
| 394 |
molecule.code = code # Add code attribute so we can write as well as read |
| 395 |
return code, molecule |
| 396 |
|
| 397 |
# pylint: disable=bad-whitespace |
| 398 |
AMINO_ACID_CODES: dict[str, Molecule] = dict(( |
| 399 |
#code, volume, formula, name |
| 400 |
_("A", 91.5, "C3H4H[1]NO", "alanine"), |
| 401 |
#B: D or N |
| 402 |
_("C", 105.6, "C3H3H[1]NOS", "cysteine"), |
| 403 |
_("D", 124.5, "C4H3H[1]NO3-", "aspartic acid"), |
| 404 |
_("E", 155.1, "C5H5H[1]NO3-", "glutamic acid"), |
| 405 |
_("F", 203.4, "C9H8H[1]NO", "phenylalanine"), |
| 406 |
_("G", 66.4, "C2H2H[1]NO", "glycine"), |
| 407 |
_("H", 167.3, "C6H5H[1]3N3O+", "histidine"), |
| 408 |
_("I", 168.8, "C6H10H[1]NO", "isoleucine"), |
| 409 |
#J: L or I |
| 410 |
_("K", 171.3, "C6H9H[1]4N2O+", "lysine"), |
| 411 |
_("L", 168.8, "C6H10H[1]NO", "leucine"), |
| 412 |
_("M", 170.8, "C5H8H[1]NOS", "methionine"), |
| 413 |
_("N", 135.2, "C4H3H[1]3N2O2", "asparagine"), |
| 414 |
#O: _("O", ???.?, "C12H21N3O3", "pyrrolysine") -- update X below |
| 415 |
_("P", 129.3, "C5H7NO", "proline"), |
| 416 |
_("Q", 161.1, "C5H5H[1]3N2O2", "glutamine"), |
| 417 |
_("R", 202.1, "C6H7H[1]6N4O+", "arginine"), |
| 418 |
_("S", 99.1, "C3H3H[1]2NO2", "serine"), |
| 419 |
_("T", 122.1, "C4H5H[1]2NO2", "threonine"), |
| 420 |
#U: selenocysteine -- update X below |
| 421 |
_("V", 141.7, "C5H8H[1]NO", "valine"), |
| 422 |
_("W", 237.6, "C11H8H[1]2N2O", "tryptophan"), |
| 423 |
#X: any |
| 424 |
_("Y", 203.6, "C9H7H[1]2NO2", "tyrosine"), |
| 425 |
#Z: E or Q |
| 426 |
#-: gap |
| 427 |
)) |
| 428 |
_set_amino_acid_average('B', 'DN') |
| 429 |
_set_amino_acid_average('J', 'LI') |
| 430 |
_set_amino_acid_average('Z', 'EQ') |
| 431 |
_set_amino_acid_average('X', 'ACDEFGHIKLMNPQRSTVWY', name='any') |
| 432 |
_set_amino_acid_average('-', '', name='gap') |
| 433 |
__doc__ += "\n\n*AMINO_ACID_CODES*::\n\n " + "\n ".join( |
| 434 |
"%s: %s"%(k, v.name) for k, v in sorted(AMINO_ACID_CODES.items())) |
| 435 |
|
| 436 |
# mypy doesn't like redefinitions |
| 437 |
def _1(formula: str, V: float, name: str) -> tuple[str, Molecule]: |
| 438 |
molecule = Molecule(name, formula, cell_volume=V) |
| 439 |
return name, molecule |
| 440 |
NUCLEIC_ACID_COMPONENTS: dict[str, Molecule] = dict(( |
| 441 |
# formula, volume, name |
| 442 |
_1("NaPO3", 60, "phosphate"), |
| 443 |
_1("C5H6H[1]O3", 125, "ribose"), |
| 444 |
_1("C5H7O2", 115, "deoxyribose"), |
| 445 |
_1("C5H2H[1]2N5", 114, "adenine"), |
| 446 |
_1("C4H2H[1]N2O2", 99, "uracil"), |
| 447 |
_1("C5H4H[1]N2O2", 126, "thymine"), |
| 448 |
_1("C5HH[1]3N5O", 119, "guanine"), |
| 449 |
_1("C4H2H[1]2N3O", 103, "cytosine"), |
| 450 |
)) |
| 451 |
__doc__ += "\n\n*NUCLEIC_ACID_COMPONENTS*::\n\n " + "\n ".join( |
| 452 |
"%s: %s"%(k, v.formula) for k, v in sorted(NUCLEIC_ACID_COMPONENTS.items())) |
| 453 |
|
| 454 |
CARBOHYDRATE_RESIDUES: dict[str, Molecule] = dict(( |
| 455 |
# formula, volume, name |
| 456 |
_1("C6H7H[1]3O5", 171.9, "Glc"), |
| 457 |
_1("C6H7H[1]3O5", 166.8, "Gal"), |
| 458 |
_1("C6H7H[1]3O5", 170.8, "Man"), |
| 459 |
_1("C6H7H[1]4O5", 170.8, "Man (terminal)"), |
| 460 |
_1("C8H10H[1]3NO5", 222.0, "GlcNAc"), |
| 461 |
_1("C8H10H[1]3NO5", 232.9, "GalNAc"), |
| 462 |
_1("C6H7H[1]3O4", 160.8, "Fuc (terminal)"), |
| 463 |
_1("C11H11H[1]5NO8", 326.3, "NeuNac (terminal)"), |
| 464 |
# Glycosaminoglycans |
| 465 |
_1("C14H15H[1]5NO11Na", 390.7, "hyaluronate"), # GlcA.GlcNAc |
| 466 |
_1("C14H17H[1]5NO13SNa", 473.5, "keratan sulphate"), # Gal.GlcNAc.SO4 |
| 467 |
_1("C14H15H[1]4NO14SNa", 443.5, "chondroitin sulphate"), # GlcA.GalNAc.SO4 |
| 468 |
)) |
| 469 |
__doc__ += "\n\n*CARBOHYDRATE_RESIDUES*::\n\n " + "\n ".join( |
| 470 |
"%s: %s"%(k, v.formula) for k, v in sorted(CARBOHYDRATE_RESIDUES.items())) |
| 471 |
|
| 472 |
LIPIDS: dict[str, Molecule] = dict(( |
| 473 |
# formula, volume, name |
| 474 |
_1("CH2", 27, "methylene"), |
| 475 |
_1("CD2", 27, "methylene-D"), |
| 476 |
_1("C10H18NO8P", 350, "phospholipid headgroup"), |
| 477 |
_1("C6H5O6", 240, "triglyceride headgroup"), |
| 478 |
_1("C36H72NO8P", 1089, "DMPC"), |
| 479 |
_1("C36H20D52NO8P", 1089, "DMPC-D52"), |
| 480 |
_1("C29H55H[1]3NO8P", 932, "DLPE"), |
| 481 |
_1("C27H45H[1]O", 636, "cholesteral"), |
| 482 |
_1("C45H78O2", 1168, "oleate"), |
| 483 |
_1("C57H104O6", 1617, "trioleate form"), |
| 484 |
_1("C39H77H[1]2N2O2P", 1166, "palmitate ester"), |
| 485 |
)) |
| 486 |
__doc__ += "\n\n*LIPIDS*::\n\n " + "\n ".join( |
| 487 |
"%s: %s"%(k, v.formula) for k, v in sorted(LIPIDS.items())) |
| 488 |
|
| 489 |
|
| 490 |
RNA_BASES: dict[str, Molecule] = {} |
| 491 |
RNA_CODES: dict[str, Molecule] = {} |
| 492 |
DNA_BASES: dict[str, Molecule] = {} |
| 493 |
DNA_CODES: dict[str, Molecule] = {} |
| 494 |
|
| 495 |
def _set_rna_dna_codes() -> None: |
| 496 |
""" |
| 497 |
Convert RNA/DNA table values into Molecule. |
| 498 |
|
| 499 |
Measured volumes from isolated mers reported in Durchschlag 1997, converted |
| 500 |
from mL/mol, with an addition of 30.39 to account for phosphorylation and |
| 501 |
dehydration in sequence. The value of 30.39 comes from comparison of the |
| 502 |
volume given in Harroun (2006) to the volumes of the RNA + T nucleosides |
| 503 |
given in Buckin (1989) after correcting for units. Harroun (2006) doesn't |
| 504 |
give volumes for AGC in the DNA nucleosides despite them being different in |
| 505 |
the Buckin source (especially guanosine). |
| 506 |
""" |
| 507 |
# code, formula, volume (mL/mol), name |
| 508 |
rna_bases = ( |
| 509 |
("A", "C10H8H[1]3N5O6P", 170.8, "adenosine"), |
| 510 |
("T", "C9H8H[1]2N2O8P", 151.7, "uridine"), # Use H[1] for U in RNA |
| 511 |
("G", "C10H7H[1]4N5O7P", 178.2, "guanosine"), |
| 512 |
("C", "C9H8H[1]3N3O7P", 153.7, "cytidine"), |
| 513 |
) |
| 514 |
dna_bases = ( |
| 515 |
("A", "C10H9H[1]2N5O5P", 169.8, "adenosine"), |
| 516 |
("T", "C10H11H[1]1N2O7P", 167.6, "thymidine"), |
| 517 |
("G", "C10H8H[1]3N5O6P", 173.7, "guanosine"), |
| 518 |
("C", "C9H9H[1]2N3O6P", 153.4, "cytidine"), |
| 519 |
) |
| 520 |
|
| 521 |
codes = ( |
| 522 |
#code, nucleotides, name |
| 523 |
("A", "A", "adenosine"), |
| 524 |
("C", "C", "cytidine"), |
| 525 |
("G", "G", "guanosine"), |
| 526 |
("T", "T", "thymidine"), |
| 527 |
("U", "T", "uridine"), # RNA_BASES["T"] is uridine |
| 528 |
("R", "AG", "purine"), |
| 529 |
("Y", "CT", "pyrimidine"), |
| 530 |
("K", "GT", "ketone"), |
| 531 |
("M", "AC", "amino"), |
| 532 |
("S", "CG", "strong"), |
| 533 |
("W", "AT", "weak"), |
| 534 |
("B", "CGT", "not A"), |
| 535 |
("D", "AGT", "not C"), |
| 536 |
("H", "ACT", "not G"), |
| 537 |
("V", "ACG", "not T"), |
| 538 |
("N", "ACGT", "any base"), |
| 539 |
("X", "", "masked"), |
| 540 |
("-", "", "gap"), |
| 541 |
) |
| 542 |
|
| 543 |
for code, formula, volume, name in rna_bases: |
| 544 |
cell_volume = volume * 1e24/avogadro_number + 30.39 |
| 545 |
molecule = Molecule(name, formula, cell_volume=cell_volume) |
| 546 |
molecule.code = code |
| 547 |
RNA_BASES[code] = molecule |
| 548 |
|
| 549 |
for code, formula, volume, name in dna_bases: |
| 550 |
cell_volume = volume * 1e24/avogadro_number + 30.39 |
| 551 |
molecule = Molecule(name, formula, cell_volume=cell_volume) |
| 552 |
molecule.code = code |
| 553 |
DNA_BASES[code] = molecule |
| 554 |
|
| 555 |
for code, bases, name in codes: |
| 556 |
D, V, _ = _code_average(bases, RNA_BASES) |
| 557 |
rna = Molecule(name, D.hill, cell_volume=V) |
| 558 |
rna.code = code |
| 559 |
D, V, _ = _code_average(bases, DNA_BASES) |
| 560 |
dna = Molecule(name, D.hill, cell_volume=V) |
| 561 |
dna.code = code |
| 562 |
RNA_CODES[code] = rna |
| 563 |
DNA_CODES[code] = dna |
| 564 |
|
| 565 |
_set_rna_dna_codes() |
| 566 |
# pylint: enable=bad-whitespace |
| 567 |
|
| 568 |
__doc__ += "\n\n*RNA_BASES*::\n\n " + "\n ".join( |
| 569 |
"%s:%s"%(k, v.name) for k, v in sorted(RNA_BASES.items())) |
| 570 |
__doc__ += "\n\n*DNA_BASES*::\n\n " + "\n ".join( |
| 571 |
"%s:%s"%(k, v.name) for k, v in sorted(DNA_BASES.items())) |
| 572 |
|
| 573 |
|
| 574 |
CODE_TABLES: dict[str, dict[str, Molecule]] = { |
| 575 |
'aa': AMINO_ACID_CODES, |
| 576 |
'dna': DNA_CODES, |
| 577 |
'rna': RNA_CODES, |
| 578 |
} |
| 579 |
|
| 580 |
def fasta_table() -> None: |
| 581 |
elements = default_table() |
| 582 |
|
| 583 |
rows = [] |
| 584 |
rows += [v for k, v in sorted(AMINO_ACID_CODES.items())] |
| 585 |
rows += [v for k, v in sorted(NUCLEIC_ACID_COMPONENTS.items())] |
| 586 |
rows += [Sequence("beta casein", beta_casein)] |
| 587 |
|
| 588 |
print("%25s %7s %7s %7s %5s %5s %5s %5s %5s %5s" |
| 589 |
% ("name", "M(H2O)", "M(D2O)", "volume", |
| 590 |
"den", "#el", "xray", "nH2O", "nD2O", "%D2O match")) |
| 591 |
for v in rows: |
| 592 |
protons = sum(num*el.number for el, num in v.natural_formula.atoms.items()) |
| 593 |
electrons = protons - v.charge |
| 594 |
Xsld = xray_sld(v.formula, wavelength=cast(float, elements.Cu.K_alpha)) |
| 595 |
print("%25s %7.1f %7.1f %7.1f %5.2f %5d %5.2f %5.2f %5.2f %5.1f"%( |
| 596 |
v.name, v.mass, v.Dmass, v.cell_volume, v.natural_formula.density or 0., |
| 597 |
electrons, Xsld[0], v.sld, v.Dsld, v.D2Omatch)) |
| 598 |
|
| 599 |
beta_casein = "RELEELNVPGEIVESLSSSEESITRINKKIEKFQSEEQQQTEDELQDKIHPFAQTQSLVYPFPGPIPNSLPQNIPPLTQTPVVVPPFLQPEVMGVSKVKEAMAPKHKEMPFPKYPVEPFTESQSLTLTDVENLHLPLPLLQSWMHQPHQPLPPTVMFPPQSVLSLSQSKVLPVPQKAVPYPQRDMPIQAFLLYQEPVLGPVRGPFPIIV" |
| 600 |
|
| 601 |
## Uncomment to show package path on CI infrastructure |
| 602 |
#def doctestpath(): |
| 603 |
# """ |
| 604 |
# Checking import path for doctests:: |
| 605 |
# |
| 606 |
# >>> import periodictable |
| 607 |
# >>> print(f"Path to imported periodictable in docstr is {periodictable.__file__}") |
| 608 |
# some path printed here |
| 609 |
# """ |
| 610 |
|
| 611 |
def test(): |
| 612 |
from periodictable.constants import avogadro_number |
| 613 |
from .formulas import formula |
| 614 |
elements = default_table() |
| 615 |
|
| 616 |
## Uncomment to show package path on CI infrastructure |
| 617 |
#import periodictable |
| 618 |
#print(f"Path to imported periodictable in package is {periodictable.__file__}") |
| 619 |
#print(fail_test) |
| 620 |
|
| 621 |
# Beta casein results checked against Duncan McGillivray's spreadsheet |
| 622 |
# name Hmass Dmass vol den #el xray Hsld Dsld |
| 623 |
# =========== ======= ======= ======= ===== ===== ===== ===== ===== |
| 624 |
# beta casein 23561.9 23880.9 30872.9 1.27 12614 11.55 1.68 2.75 |
| 625 |
# ... updated for new mass table [2023-08] |
| 626 |
# same 23562.3 23881.2 same 1.27 same 1.68 2.75 |
| 627 |
|
| 628 |
seq = Sequence("beta casein", beta_casein) |
| 629 |
density = seq.mass/avogadro_number/seq.cell_volume*1e24 |
| 630 |
# print(seq.formula) |
| 631 |
# print(seq.mass, seq.Dmass, density, seq.sld, seq.Dsld) |
| 632 |
|
| 633 |
# Sequence now includes terminators: H[1]-...-OH[1], so adjust the masses |
| 634 |
# slightly from the spreadsheet values. |
| 635 |
H2O = formula("H2O@1n") |
| 636 |
D2O = formula("D2O@1n") |
| 637 |
assert abs(seq.mass - (23562.3 + H2O.mass)) < 0.1 |
| 638 |
assert abs(seq.Dmass - (23881.2 + D2O.mass)) < 0.1 |
| 639 |
assert abs(seq.cell_volume - 30872.9) < 0.1 |
| 640 |
assert abs(density - 1.267) < 0.01 |
| 641 |
assert abs(seq.sld - 1.68) < 0.01 |
| 642 |
assert abs(seq.Dsld - 2.75) < 0.01 |
| 643 |
|
| 644 |
# Check that X-ray sld is independent of isotope |
| 645 |
H = seq.labile_formula.replace(elements.H[1], elements.H) |
| 646 |
D = seq.labile_formula.replace(elements.H[1], elements.D) |
| 647 |
Hsld, Dsld = xray_sld(H, wavelength=1.54), xray_sld(D, wavelength=1.54) |
| 648 |
#print Hsld, Dsld |
| 649 |
assert abs(Hsld[0]-Dsld[0]) < 1e-10 |
| 650 |
|
| 651 |
if __name__ == "__main__": |
| 652 |
fasta_table() |
| 653 |
#test() |