Skip to content

Commit

Permalink
Merge pull request #1001 from ppillot/#999_cif_parsing
Browse files Browse the repository at this point in the history
#999 cif parsing
  • Loading branch information
fredludlow authored Nov 20, 2023
2 parents 2011e08 + bbd7a7e commit 7d271db
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/parser/cif-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const reWhitespace = /\s+/
const reQuotedWhitespace = /'((?:(?!'\s).)*)'|"((?:(?!"\s).)*)"|(\S+)/g
const reDoubleQuote = /"/g
const reTrimQuotes = /^['"]+|['"]+$/g
const reAtomSymbol = /^\D{1,2}/ // atom symbol in atom_site_label

interface Cif {[k: string]: any}

Expand Down Expand Up @@ -186,11 +187,20 @@ function parseCore (cif: Cif, structure: Structure, structureBuilder: StructureB
const c = new Vector3()
const n = cif.atom_site_type_symbol.length

const typeSymbolMap: Record<string, string> = {}

for (let i = 0; i < n; ++i) {
atomStore.growIfFull()

const atomname = cif.atom_site_label[ i ]
const element = cif.atom_site_type_symbol[ i ]
const typeSymbol = cif.atom_site_type_symbol[ i ]

// typeSymbol can be like `Al2.5+`. Retain element symbol only.
let element = typeSymbolMap[typeSymbol]
if (!element) {
const match = typeSymbol.match(reAtomSymbol)
typeSymbolMap[typeSymbol] = element = match?.[0] ?? typeSymbol
}

atomStore.atomTypeId[ i ] = atomMap.add(atomname, element)

Expand Down Expand Up @@ -1072,7 +1082,7 @@ class CifParser extends StructureParser {
_parseChunkOfLines(0, lines.length, lines)
})

if (cif.chem_comp && cif.chem_comp_atom) {
if (cif.chem_comp && cif.chem_comp_atom && !cif.struct) {
parseChemComp(cif, s, sb)
sb.finalize()
s.finalizeAtoms()
Expand Down

0 comments on commit 7d271db

Please sign in to comment.