Debian Codegraph @

shelxfile/misc/elements.py

raw symbols SCIP
1 # /usr/bin/env python
2 # -*- encoding: utf-8 -*-
3 # möp
4 # ----------------------------------------------------------------------------
5 # "THE BEER-WARE LICENSE" (Revision 42):
6 # <[email protected]> wrote this file. As long as you retain
7 # this notice you can do whatever you want with this stuff. If we meet some day,
8 # and you think this stuff is worth it, you can buy me a beer in return.
9 # Daniel Kratzert
10 # ----------------------------------------------------------------------------
11 #
12
13 import re
14
15 atoms = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg',
16 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe',
17 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y',
18 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te',
19 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb',
20 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt',
21 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa',
22 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'D']
23
24 sorted_atoms = ['C', 'D', 'H', 'N', 'O', 'Cl', 'Br', 'I', 'F', 'S', 'P', 'Ac', 'Ag', 'Al',
25 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be', 'Bi', 'Bk', 'Ca', 'Cd', 'Ce',
26 'Cf', 'Cm', 'Co', 'Cr', 'Cs', 'Cu', 'Dy', 'Er', 'Eu', 'Fe', 'Fr', 'Ga', 'Gd',
27 'Ge', 'He', 'Hf', 'Hg', 'Ho', 'In', 'Ir', 'K', 'Kr', 'La', 'Li', 'Lu', 'Mg',
28 'Mn', 'Mo', 'Na', 'Nb', 'Nd', 'Ne', 'Ni', 'Np', 'Os', 'Pa', 'Pb', 'Pd', 'Pm',
29 'Po', 'Pr', 'Pt', 'Pu', 'Ra', 'Rb', 'Re', 'Rh', 'Rn', 'Ru', 'Sb', 'Sc', 'Se',
30 'Si', 'Sm', 'Sn', 'Sr', 'Ta', 'Tb', 'Tc', 'Te', 'Th', 'Ti', 'Tl', 'Tm', 'U',
31 'V', 'W', 'Xe', 'Y', 'Yb', 'Zn', 'Zr']
32
33
34 def weight_from_symbol(symbol):
35 """
36 Returns the weight of an atom from the element symbol.
37 """
38 from shelxfile.atoms.atomic_weights.atomicWeightsDecimal import atomicWeightsDecimal
39 return atomicWeightsDecimal[symbol]['standard']
40
41
42 num2element = {
43 0 : 'n',
44 1 : 'H',
45 2 : 'He',
46 3 : 'Li',
47 4 : 'Be',
48 5 : 'B',
49 6 : 'C',
50 7 : 'N',
51 8 : 'O',
52 9 : 'F',
53 10: 'Ne',
54 11: 'Na',
55 12: 'Mg',
56 13: 'Al',
57 14: 'Si',
58 15: 'P',
59 16: 'S',
60 17: 'Cl',
61 18: 'Ar',
62 19: 'K',
63 20: 'Ca',
64 21: 'Sc',
65 22: 'Ti',
66 23: 'V',
67 24: 'Cr',
68 25: 'Mn',
69 26: 'Fe',
70 27: 'Co',
71 28: 'Ni',
72 29: 'Cu',
73 30: 'Zn',
74 31: 'Ga',
75 32: 'Ge',
76 33: 'As',
77 34: 'Se',
78 35: 'Br',
79 36: 'Kr',
80 37: 'Rb',
81 38: 'Sr',
82 39: 'Y',
83 40: 'Zr',
84 41: 'Nb',
85 42: 'Mo',
86 43: 'Tc',
87 44: 'Ru',
88 45: 'Rh',
89 46: 'Pd',
90 47: 'Ag',
91 48: 'Cd',
92 49: 'In',
93 50: 'Sn',
94 51: 'Sb',
95 52: 'Te',
96 53: 'I',
97 54: 'Xe',
98 55: 'Cs',
99 56: 'Ba',
100 57: 'La',
101 58: 'Ce',
102 59: 'Pr',
103 60: 'Nd',
104 61: 'Pm',
105 62: 'Sm',
106 63: 'Eu',
107 64: 'Gd',
108 65: 'Tb',
109 66: 'Dy',
110 67: 'Ho',
111 68: 'Er',
112 69: 'Tm',
113 70: 'Yb',
114 71: 'Lu',
115 72: 'Hf',
116 73: 'Ta',
117 74: 'W',
118 75: 'Re',
119 76: 'Os',
120 77: 'Ir',
121 78: 'Pt',
122 79: 'Au',
123 80: 'Hg',
124 81: 'Tl',
125 82: 'Pb',
126 83: 'Bi',
127 84: 'Po',
128 85: 'At',
129 86: 'Rn',
130 87: 'Fr',
131 88: 'Ra',
132 89: 'Ac',
133 90: 'Th',
134 91: 'Pa',
135 92: 'U',
136 93: 'Np',
137 94: 'Pu',
138 95: 'Am',
139 96: 'Cm',
140 97: 'Bk',
141 98: 'Cf',
142 99: 'Es',
143 }
144
145 element2num = {
146 'n' : 0,
147 'H' : 1,
148 'D' : 1,
149 'He': 2,
150 'Li': 3,
151 'Be': 4,
152 'B' : 5,
153 'C' : 6,
154 'N' : 7,
155 'O' : 8,
156 'F' : 9,
157 'Ne': 10,
158 'Na': 11,
159 'Mg': 12,
160 'Al': 13,
161 'Si': 14,
162 'P' : 15,
163 'S' : 16,
164 'Cl': 17,
165 'Ar': 18,
166 'K' : 19,
167 'Ca': 20,
168 'Sc': 21,
169 'Ti': 22,
170 'V' : 23,
171 'Cr': 24,
172 'Mn': 25,
173 'Fe': 26,
174 'Co': 27,
175 'Ni': 28,
176 'Cu': 29,
177 'Zn': 30,
178 'Ga': 31,
179 'Ge': 32,
180 'As': 33,
181 'Se': 34,
182 'Br': 35,
183 'Kr': 36,
184 'Rb': 37,
185 'Sr': 38,
186 'Y' : 39,
187 'Zr': 40,
188 'Nb': 41,
189 'Mo': 42,
190 'Tc': 43,
191 'Ru': 44,
192 'Rh': 45,
193 'Pd': 46,
194 'Ag': 47,
195 'Cd': 48,
196 'In': 49,
197 'Sn': 50,
198 'Sb': 51,
199 'Te': 52,
200 'I' : 53,
201 'Xe': 54,
202 'Cs': 55,
203 'Ba': 56,
204 'La': 57,
205 'Ce': 58,
206 'Pr': 59,
207 'Nd': 60,
208 'Pm': 61,
209 'Sm': 62,
210 'Eu': 63,
211 'Gd': 64,
212 'Tb': 65,
213 'Dy': 66,
214 'Ho': 67,
215 'Er': 68,
216 'Tm': 69,
217 'Yb': 70,
218 'Lu': 71,
219 'Hf': 72,
220 'Ta': 73,
221 'W' : 74,
222 'Re': 75,
223 'Os': 76,
224 'Ir': 77,
225 'Pt': 78,
226 'Au': 79,
227 'Hg': 80,
228 'Tl': 81,
229 'Pb': 82,
230 'Bi': 83,
231 'Po': 84,
232 'At': 85,
233 'Rn': 86,
234 'Fr': 87,
235 'Ra': 88,
236 'Ac': 89,
237 'Th': 90,
238 'Pa': 91,
239 'U' : 92,
240 'Np': 93,
241 'Pu': 94,
242 'Am': 95,
243 'Cm': 96,
244 'Bk': 97,
245 'Cf': 98,
246 'Es': 99,
247 }
248
249 num2covradius = {
250 0 : 0.5,
251 1 : 0.45,
252 2 : 0.45,
253 3 : 1.23,
254 4 : 0.90,
255 5 : 0.80,
256 6 : 0.77,
257 7 : 0.74,
258 8 : 0.71,
259 9 : 0.72,
260 10: 1.00,
261 11: 1.54,
262 12: 1.49,
263 13: 1.18,
264 14: 1.11,
265 15: 1.06,
266 16: 1.02,
267 17: 0.99,
268 18: 1.00,
269 19: 2.03,
270 20: 1.74,
271 21: 1.44,
272 22: 1.32,
273 23: 1.22,
274 24: 1.18,
275 25: 1.17,
276 26: 1.17,
277 27: 1.16,
278 28: 1.15,
279 29: 1.17,
280 30: 1.25,
281 31: 1.26,
282 32: 1.22,
283 33: 1.20,
284 34: 1.16,
285 35: 1.14,
286 36: 1.12,
287 37: 2.16,
288 38: 1.91,
289 39: 1.62,
290 40: 1.45,
291 41: 1.34,
292 42: 1.30,
293 43: 1.27,
294 44: 1.25,
295 45: 1.25,
296 46: 1.28,
297 47: 1.34,
298 48: 1.48,
299 49: 1.44,
300 50: 1.41,
301 51: 1.40,
302 52: 1.36,
303 53: 1.33,
304 54: 1.31,
305 55: 2.35,
306 56: 1.98,
307 57: 1.69,
308 58: 1.65,
309 59: 1.65,
310 60: 1.64,
311 61: 1.63,
312 62: 1.62,
313 63: 1.85,
314 64: 1.61,
315 65: 1.59,
316 66: 1.59,
317 67: 1.58,
318 68: 1.57,
319 69: 1.56,
320 70: 1.74,
321 71: 1.56,
322 72: 1.44,
323 73: 1.34,
324 74: 1.30,
325 75: 1.28,
326 76: 1.26,
327 77: 1.27,
328 78: 1.30,
329 79: 1.34,
330 80: 1.49,
331 81: 1.48,
332 82: 1.47,
333 83: 1.46,
334 84: 1.46,
335 85: 1.45,
336 86: 1.00,
337 87: 1.00,
338 88: 1.00,
339 89: 1.88,
340 90: 1.65,
341 91: 1.61,
342 92: 1.42,
343 93: 1.30,
344 94: 1.51,
345 95: 1.82,
346 96: 1.20,
347 97: 1.20,
348 98: 1.20,
349 99: 1.20,
350 }
351
352 element2cov = {
353 'H' : 0.45,
354 'D' : 0.45,
355 'He': 0.45,
356 'Li': 1.23,
357 'Be': 0.90,
358 'B' : 0.82,
359 'C' : 0.77,
360 'N' : 0.75,
361 'O' : 0.73,
362 'F' : 0.72,
363 'Ne': 0.71,
364 'Na': 1.54,
365 'Mg': 1.36,
366 'Al': 1.18,
367 'Si': 1.11,
368 'P' : 1.06,
369 'S' : 1.02,
370 'Cl': 0.99,
371 'Ar': 0.98,
372 'K' : 2.03,
373 'Ca': 1.74,
374 'Sc': 1.44,
375 'Ti': 1.32,
376 'V' : 1.22,
377 'Cr': 1.18,
378 'Mn': 1.17,
379 'Fe': 1.17,
380 'Co': 1.16,
381 'Ni': 1.15,
382 'Cu': 1.17,
383 'Zn': 1.25,
384 'Ga': 1.26,
385 'Ge': 1.22,
386 'As': 1.2,
387 'Se': 1.16,
388 'Br': 1.14,
389 'Kr': 1.12,
390 'Rb': 2.16,
391 'Sr': 1.91,
392 'Y' : 1.62,
393 'Zr': 1.45,
394 'Nb': 1.34,
395 'Mo': 1.3,
396 'Tc': 1.27,
397 'Ru': 1.25,
398 'Rh': 1.25,
399 'Pd': 1.28,
400 'Ag': 1.34,
401 'Cd': 1.48,
402 'In': 1.44,
403 'Sn': 1.41,
404 'Sb': 1.4,
405 'Te': 1.36,
406 'I' : 1.33,
407 'Xe': 1.31,
408 'Cs': 2.35,
409 'Ba': 1.98,
410 'La': 1.69,
411 'Ce': 1.65,
412 'Pr': 1.65,
413 'Nd': 1.64,
414 'Pm': 1.63,
415 'Sm': 1.62,
416 'Eu': 1.85,
417 'Gd': 1.61,
418 'Tb': 1.59,
419 'Dy': 1.59,
420 'Ho': 1.58,
421 'Er': 1.57,
422 'Tm': 1.56,
423 'Yb': 1.74,
424 'Lu': 1.56,
425 'Hf': 1.44,
426 'Ta': 1.34,
427 'W' : 1.3,
428 'Re': 1.28,
429 'Os': 1.26,
430 'Ir': 1.27,
431 'Pt': 1.3,
432 'Au': 1.34,
433 'Hg': 1.49,
434 'Tl': 1.48,
435 'Pb': 1.47,
436 'Bi': 1.46,
437 'Po': 1.46,
438 'At': 1.45,
439 'Rn': 1.0,
440 'Fr': 1.0,
441 'Ra': 1.0,
442 'Ac': 1.88,
443 'Th': 1.65,
444 'Pa': 1.61,
445 'U' : 1.42,
446 'Np': 1.30,
447 'Pu': 1.51,
448 'Am': 1.82,
449 'Cm': 1.20,
450 'Bk': 1.20,
451 'Cf': 1.20,
452 'Es': 1.20,
453 }
454
455 element2color = {
456 'H' : "#FFFFFF",
457 'He': "#FFFFFF",
458 'Li': "#CC80FF",
459 'Be': "#c9d5e9",
460 'B' : "#FFB5B5",
461 'C' : "#797979",
462 'N' : "#3050F8",
463 'O' : "#FF0D0D",
464 'F' : "#90e001",
465 'Ne': "#B3E3F5",
466 'Na': "#AB5CF2",
467 'Mg': "#bbc7db",
468 'Al': "#BFA6A6",
469 'Si': "#F0C8A0",
470 'P' : "#FF8000",
471 'S' : "#eeee2c",
472 'Cl': "#419941",
473 'Ar': "#80D1E3",
474 'K' : "#8F40D4",
475 'Ca': "#bbc7db",
476 'Sc': "#E6E6E6",
477 'Ti': "#BFC2C7",
478 'V' : "#A6A6AB",
479 'Cr': "#8A99C7",
480 'Mn': "#9C7AC7",
481 'Fe': "#E06633",
482 'Co': "#F090A0",
483 'Ni': "#50D050",
484 'Cu': "#C88033",
485 'Zn': "#7D80B0",
486 'Ga': "#C28F8F",
487 'Ge': "#668F8F",
488 'As': "#BD80E3",
489 'Se': "#FFA100",
490 'Br': "#A62929",
491 'Kr': "#5CB8D1",
492 'Rb': "#702EB0",
493 'Sr': "#bbc7db",
494 'Y' : "#94FFFF",
495 'Zr': "#94E0E0",
496 'Nb': "#73C2C9",
497 'Mo': "#54B5B5",
498 'Tc': "#3B9E9E",
499 'Ru': "#248F8F",
500 'Rh': "#0A7D8C",
501 'Pd': "#006985",
502 'Ag': "#C0C0C0",
503 'Cd': "#FFD98F",
504 'In': "#A67573",
505 'Sn': "#668080",
506 'Sb': "#9E63B5",
507 'Te': "#D47A00",
508 'I' : "#940094",
509 'Xe': "#429EB0",
510 'Cs': "#57178F",
511 'Ba': "#bbc7db",
512 'La': "#d9ffff",
513 'Ce': "#d9ffff",
514 'Pr': "#d9ffff",
515 'Nd': "#d9ffff",
516 'Pm': "#d9ffff",
517 'Sm': "#d9ffff",
518 'Eu': "#d9ffff",
519 'Gd': "#d9ffff",
520 'Tb': "#d9ffff",
521 'Dy': "#d9ffff",
522 'Ho': "#d9ffff",
523 'Er': "#d9ffff",
524 'Tm': "#d9ffff",
525 'Yb': "#d9ffff",
526 'Lu': "#d9ffff",
527 'Hf': "#d9ffff",
528 'Ta': "#d9ffff",
529 'W' : "#d9ffff",
530 'Re': "#d9ffff",
531 'Os': "#d9ffff",
532 'Ir': "#d9ffff",
533 'Pt': "#d9ffff",
534 'Au': "#d9ffff",
535 'Hg': "#d9ffff",
536 'Tl': "#d9ffff",
537 'Pb': "#d9ffff",
538 'Bi': "#d9ffff",
539 'Po': "#d9ffff",
540 'At': "#d9ffff",
541 'Rn': "#d9ffff",
542 'Fr': "#d9ffff",
543 'Ra': "#d9ffff",
544 'Ac': "#d9ffff",
545 'Th': "#d9ffff",
546 'Pa': "#d9ffff",
547 'U' : "#d9ffff",
548 'Np': "#d9ffff",
549 'Pu': "#d9ffff",
550 'Am': "#d9ffff",
551 'Cm': "#d9ffff",
552 'Bk': "#d9ffff",
553 'Cf': "#d9ffff",
554 'D' : "#e2e6e6",
555 }
556
557
558 def get_element_color(element: str) -> str:
559 """
560 Retruns RGB color code in Hex for the element.
561 """
562 return element2color.get(element.capitalize())
563
564
565 def get_radius(atomic_number: int) -> float:
566 """
567 Get the covalent radius in pm for the element.
568 """
569 return num2covradius[atomic_number]
570
571
572 def get_radius_from_element(element: str) -> float:
573 """
574 Returns the covalent radius (Å) of an atom by its element name.
575 Strips trailing digits (e.g. 'O2' → 'O') before lookup and falls back
576 to carbon's radius silently for unknown/unsupported elements.
577 """
578 try:
579 cleaned = get_atomlabel(element)
580 except KeyError:
581 cleaned = element
582 return element2cov.get(cleaned.capitalize(), element2cov['C'])
583
584
585 def get_atomic_number(element: str) -> int:
586 """
587 returns the atomic number from the element symbol
588 """
589 return element2num[element]
590
591
592 def get_element(atomic_number: int) -> str:
593 """
594 returns the element symbol from the atomic number
595 """
596 return num2element[atomic_number]
597
598
599 def get_atomlabel(input_atom: str) -> str:
600 """
601 converts an atom name like C12 to the element symbol C.
602 """
603 atom = ''
604 for x in input_atom: # iterate over characters in i
605 if re.match(r'^[A-Za-z#]', x): # Alphabet and "#" as allowed characters in names
606 atom = atom + x.upper() # add characters to atoms until numbers occur
607 else: # now we have atoms like C, Ca, but also Caaa
608 break
609 try:
610 if atom[0:2].capitalize() in atoms: # fixes names like Caaa to be just Ca
611 return atom[0:2].capitalize() # atoms first, search for all two-letter atoms
612 elif atom[0].upper() in atoms:
613 return atom[0] # then for all one-letter atoms
614 else:
615 # print('*** {} is not a valid atom!! ***'.format(atom))
616 raise KeyError
617 except(IndexError):
618 # print('*** {} is not a valid atom! ***'.format(atom))
619 raise KeyError
620
621
622 if __name__ == '__main__':
623 print(weight_from_symbol('C'))