-
Notifications
You must be signed in to change notification settings - Fork 4
/
translate.py
169 lines (136 loc) · 4.98 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import re
import numpy as np
import astropy.units as u
from periodictable import elements
__all__ = [
'isotopologue_to_species',
'isotopologue_to_mass',
'species_name_to_fastchem_name',
'species_name_to_common_isotopologue_name',
]
def isotopologue_to_species(isotopologue):
"""
Convert isotopologue name to common species name.
Example: Take 1H2-16O and turn it to H2O, or take 48Ti-16O and turn it to TiO.
Parameters
----------
isotopologue : str
Isotopologue name, like "1H2-16O".
Returns
-------
common_name : str
Common name, like "H2O".
"""
species = ""
for element in isotopologue.split('-'):
for s in re.findall(r'\D+\d*', element):
species += ''.join(s)
return species if len(species) > 0 else isotopologue
def isotopologue_to_mass(isotopologue):
"""
Find the total atomic mass for ``isotopologue``.
Example: take 1H2-16O and turn it to 18, or take 48Ti-16O and turn it to 64.
Parameters
----------
isotopologue : str
Isotopologue name, like "1H2-16O".
Returns
-------
total_mass : astropy.units.Quantity
Total atomic mass, like 18 AMU.
"""
mass = 0
for element in isotopologue.split('-'):
multiples = list(filter(lambda x: len(x) > 0, re.split(r'\D', element)))
if len(multiples) > 1:
species_mass, multiplier = multiples
mass += float(multiplier) * float(species_mass)
elif len(multiples) == 1:
mass += float(multiples[0])
return (mass if mass != 0 else getattr(elements, isotopologue).mass) * u.u
def species_name_to_fastchem_name(species, charge=None, return_mass=False):
"""
Convert generic species name, like "H2O" or "ClAlF2", to
Hill notation for FastChem, like "H2O1" or "Al1Cl1F2".
Optionally, return the total mass of the species by summing the masses of its components.
Parameters
----------
species : str
Generic name, like "H2O" or "Ti".
charge: None or int
If ``species`` is an atom, specify the charge.
Returns
-------
hill_name : str
Name in Hill notation, like "H2O1", "Ti", "Ti1+", or "Ti1-".
"""
atoms = np.array(list(filter(
lambda x: len(x) > 0, re.split(r"(?<=[a-z])|(?=[A-Z])|\d", species)
)))
multipliers = np.array([
int(x) if len(x) > 0 else 1 for x in re.split(r'\D', species)
])
lens = [len(''.join(atom)) for atom in atoms]
multipliers_skipped = np.array([multipliers[cs] for cs in np.cumsum(lens)])
order = np.argsort(atoms)
correct_notation = ''.join([
a + str(m) for a, m in zip(atoms[order], multipliers_skipped[order])
])
# If single atom, give only the name of the atom:
if len(correct_notation) == 2 and correct_notation.endswith('1'):
# this means it is an atom with only 1 letter, like K, or P.
correct_notation = correct_notation[0]
# check charge and add it to the name if it is charged:
if charge is not None:
if charge > 0:
correct_notation += f'{charge}+'
elif charge < 0:
correct_notation += f'{abs(charge)}-'
elif len(correct_notation) == 3 and correct_notation.endswith('1'):
# this means it is an atom with 2 letters, like Na, or Ti.
# check charge and add it to the name if it is charged:
correct_notation = correct_notation[:2]
if charge is not None:
if charge > 0:
correct_notation += f'{charge}+'
elif charge < 0:
correct_notation += f'{abs(charge)}-'
if return_mass:
# Optionally return mass of species
mass = 0
for atom, mult in zip(atoms, multipliers_skipped):
mass += getattr(elements, atom).mass * mult
return correct_notation, mass
return correct_notation
def species_name_to_common_isotopologue_name(species):
"""
Convert generic species name, like "H2O", to isotopologue name like "1H2-16O".
Parameters
----------
species : str
Generic name, like "H2O".
Returns
-------
isotopologue_name : str
Isotopologue name, like "1H2-16O".
"""
atoms = np.array(list(filter(
lambda x: len(x) > 0, re.split(r"(?<=[a-z])|(?=[A-Z])|\d", species)
)))
multipliers = np.array([
int(x) if len(x) > 0 else 1 for x in re.split(r'\D', species)
])
lens = [len(''.join(atom)) for atom in atoms]
multipliers_skipped = np.array([multipliers[cs] for cs in np.cumsum(lens)])
masses = np.array([
round(getattr(elements, atom).mass) for atom, mult in zip(atoms, multipliers_skipped)
])
if len(atoms) > 1:
correct_notation = '-'.join([
str(mass) + a + (str(mult) if mult > 1 else '')
for a, mult, mass in zip(atoms, multipliers_skipped, masses)
])
# If single atom, give only the name of the atom:
else:
correct_notation = atoms[0]
return correct_notation