openchemlib-utils
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -9,2 +9,3 @@ 'use strict'; | ||
var mlMatrix = require('ml-matrix'); | ||
var atomSorter = _interopDefault(require('atom-sorter')); | ||
@@ -552,2 +553,87 @@ let OCL; | ||
/** | ||
* Calculate the molecular formula in 'chemcalc' notation taking into account fragments, isotopes and charges | ||
* {OCL.Molecule} [molecule] an instance of OCL.Molecule | ||
* @returns {String} | ||
*/ | ||
function getMF(molecule) { | ||
let entries = molecule.getFragments(); | ||
let result = {}; | ||
let parts = []; | ||
let allAtoms = []; | ||
entries.forEach(function(entry) { | ||
let mf = getFragmentMF(entry, allAtoms); | ||
parts.push(mf); | ||
}); | ||
let counts = {}; | ||
for (let part of parts) { | ||
if (!counts[part]) counts[part] = 0; | ||
counts[part]++; | ||
} | ||
parts = []; | ||
for (let key of Object.keys(counts).sort()) { | ||
if (counts[key] > 1) { | ||
parts.push(counts[key] + key); | ||
} else { | ||
parts.push(key); | ||
} | ||
} | ||
result.parts = parts; | ||
result.mf = toMFString(allAtoms); | ||
return result; | ||
} | ||
function getFragmentMF(molecule, allAtoms) { | ||
let atoms = []; | ||
for (let i = 0; i < molecule.getAllAtoms(); i++) { | ||
let atom = {}; | ||
atom.charge = molecule.getAtomCharge(i); | ||
atom.label = molecule.getAtomLabel(i); | ||
atom.mass = molecule.getAtomMass(i); | ||
atom.implicitHydrogens = molecule.getImplicitHydrogens(i); | ||
atoms.push(atom); | ||
allAtoms.push(atom); | ||
} | ||
return toMFString(atoms); | ||
} | ||
function toMFString(atoms) { | ||
let charge = 0; | ||
let mfs = {}; | ||
for (let atom of atoms) { | ||
let label = atom.label; | ||
charge += atom.charge; | ||
if (atom.mass) { | ||
label = `[${atom.mass}${label}]`; | ||
} | ||
let mfAtom = mfs[label]; | ||
if (!mfAtom) { | ||
mfs[label] = 0; | ||
} | ||
mfs[label] += 1; | ||
if (atom.implicitHydrogens) { | ||
if (!mfs.H) mfs.H = 0; | ||
mfs.H += atom.implicitHydrogens; | ||
} | ||
} | ||
let mf = ''; | ||
let keys = Object.keys(mfs).sort(atomSorter); | ||
for (let key of keys) { | ||
mf += key; | ||
if (mfs[key] > 1) mf += mfs[key]; | ||
} | ||
if (charge > 0) { | ||
mf += `(+${charge > 1 ? charge : ''})`; | ||
} else if (charge < 0) { | ||
mf += `(${charge < -1 ? charge : '-'})`; | ||
} | ||
return mf; | ||
} | ||
let fragment; | ||
@@ -780,2 +866,3 @@ | ||
exports.getHoseCodesFromDiastereotopicID = getHoseCodesFromDiastereotopicID; | ||
exports.getMF = getMF; | ||
exports.getOCL = getOCL; | ||
@@ -782,0 +869,0 @@ exports.getPathsInfo = getPathsInfo; |
{ | ||
"name": "openchemlib-utils", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -15,2 +15,3 @@ export * from './diastereotopic/addDiastereotopicMissingChirality'; | ||
export * from './util/isCsp3'; | ||
export * from './util/getMF'; | ||
@@ -17,0 +18,0 @@ export * from './path/getPathsInfo'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59831
1641