Skip to content

Commit

Permalink
feat(mf-parser): allow decimal isotopic composition
Browse files Browse the repository at this point in the history
closes: #252
  • Loading branch information
lpatiny committed Dec 18, 2024
1 parent 4dddbfb commit dd51cc1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
32 changes: 31 additions & 1 deletion packages/mf-parser/src/__tests__/MF.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ describe('MF', () => {
]);

expect(mf.toMF()).toBe('CC{50,50}H');
expect(mf.toText()).toBe('HC⁽⁵⁰˙⁵⁰⁾C');
expect(mf.toText()).toBe('HC⁽⁵⁰˒⁵⁰⁾C');

const info = mf.getInfo();
expect(info).toStrictEqual({
Expand All @@ -429,6 +429,36 @@ describe('MF', () => {
});
});

it('CC{0.5,0.5}H', () => {
const mf = new MF('HC{0.5,0.5}C');
const parts = mf.toParts();
expect(parts).toStrictEqual([
[
{ kind: 'atom', value: 'C', multiplier: 1 },
{
kind: 'isotopeRatio',
value: { atom: 'C', ratio: [0.5, 0.5] },
multiplier: 1,
},
{ kind: 'atom', value: 'H', multiplier: 1 },
],
]);

expect(mf.toMF()).toBe('CC{0.5,0.5}H');
expect(mf.toHtml()).toBe('HC<sup>{0.5,0.5}</sup>C');
expect(mf.toText()).toBe('HC⁽⁰˙⁵˒⁰˙⁵⁾C');

const info = mf.getInfo();
expect(info).toStrictEqual({
monoisotopicMass: 25.00782503223,
mass: 25.520354068326025,
charge: 0,
mf: 'CC{0.5,0.5}H',
unsaturation: 2.5,
atoms: { C: 2, H: 1 },
});
});

it('H(+)(H+)-1H', () => {
const mf = new MF('H(+)(H+)-1H');

Expand Down
7 changes: 7 additions & 0 deletions packages/mf-parser/src/__tests__/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ let tests = {
{ kind: 'anchor', value: 2 },
{ kind: 'atom', value: 'H' },
],
'C{50,50}': [{ kind: 'isotopeRatio', value: { atom: 'C', ratio: [50, 50] } }],
'C{0.50,0.50}': [
{
kind: 'isotopeRatio',
value: { atom: 'C', ratio: [0.5, 0.5] },
},
],
};

test.each(Object.keys(tests))('parse molecular formula %s', (key) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/mf-parser/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ class MFParser {
this.i++;
ascii = this.mf.charCodeAt(this.i);
} while (ascii !== 125 && this.i <= this.mf.length); // closing curly bracket
if (substring.match(/^[\d,]+$/)) {
if (substring.match(/^[0-9.,]+$/)) {
return substring.split(',').map(Number);
}
throw new MFError(
this.mf,
this.i,
'Curly brackets should contain only number and comma',
'Curly brackets should contain only number, dot and comma',

Check warning on line 240 in packages/mf-parser/src/parse.js

View check run for this annotation

Codecov / codecov/patch

packages/mf-parser/src/parse.js#L240

Added line #L240 was not covered by tests
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/mf-parser/src/util/subSuperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const superscript = {
'{': '⁽',
'}': '⁾',
'.': '˙',
',': '˙',
',': '˒',
};

export const subscript = {
Expand Down

0 comments on commit dd51cc1

Please sign in to comment.