Browse Source

tweak guessElementSymbolString

Alexander Rose 3 years ago
parent
commit
7c5ae5d7ee
1 changed files with 8 additions and 10 deletions
  1. 8 10
      src/mol-model-formats/structure/util.ts

+ 8 - 10
src/mol-model-formats/structure/util.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -46,6 +46,9 @@ export function guessElementSymbolTokens(tokens: Tokens, str: string, start: num
     TokenBuilder.add(tokens, s, s); // no reasonable guess, add empty token
 }
 
+const TwoCharElementNames = new Set(['NA', 'CL', 'FE', 'SI', 'BR', 'AS']);
+const OneCharElementNames = new Set(['C', 'H', 'N', 'O', 'P', 'S']);
+
 const reTrimSpacesAndNumbers = /^[\s\d]+|[\s\d]+$/g;
 export function guessElementSymbolString(atomId: string, compId: string) {
     // trim spaces and numbers, convert to upper case
@@ -54,14 +57,10 @@ export function guessElementSymbolString(atomId: string, compId: string) {
 
     if (l === 0) return atomId; // empty
     if (l === 1) return atomId; // one char
+    if (TwoCharElementNames.has(atomId)) return atomId; // two chars
 
-    if (l === 2) { // two chars
-        if (atomId === 'NA' || atomId === 'CL' || atomId === 'FE' || atomId === 'SI' ||
-            atomId === 'BR' || atomId === 'AS'
-        ) return atomId;
-    }
-
-    if (l === 3 && compId === atomId) { // three chars
+    // check for Charmm ion names where component and atom id are the same
+    if (l === 3 && compId === atomId) {
         if (atomId === 'SOD') return 'NA';
         if (atomId === 'POT') return 'K';
         if (atomId === 'CES') return 'CS';
@@ -69,8 +68,7 @@ export function guessElementSymbolString(atomId: string, compId: string) {
         if (atomId === 'CLA') return 'CL';
     }
 
-    const c = atomId[0];
-    if (c === 'C' || c === 'H' || c === 'N' || c === 'O' || c === 'P' || c === 'S') return c;
+    if (OneCharElementNames.has(atomId[0])) return atomId[0];
 
     return ''; // no reasonable guess, return empty string
 }