Parse an MGF file into a JSON.
$ npm i mgf-parser
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { parse } from 'mgf-parser';
const rawData = readFileSync(join(import.meta.dirname, './data.mgf'), 'utf8');
const result = parse(rawData, { uniqueX: true, maxY: 100 });
// result[0].kind -> 'IONS'
// result[0].meta -> { TITLE: 'Spectrum 1', PEPMASS: '983.6', ... }
// result[0].data -> { x: [846.6, 846.8, ...], y: [25.08, 15.12, ...] }Parses the text input rawData and returns one entry per BEGIN / END block:
| Property | Type | Description |
|---|---|---|
kind |
string |
Record type, i.e. what follows BEGIN — usually IONS. |
meta |
Record<string, string> |
Parameters of the entry, on top of the global parameters. |
data |
DataXY |
The mass spectrum, as x (m/z) and y (intensity) arrays. |
Options:
| Option | Type | Default | Description |
|---|---|---|---|
recordTypes |
string |
'' |
Regular expression the record type must match. By default all entries are returned. |
sortX |
boolean |
false |
Sort the spectrum by increasing m/z. |
uniqueX |
boolean |
false |
Merge repeating m/z values, summing their intensities. Implies sortX. |
normedY |
boolean |
false |
Normalize the spectrum so that the sum of the intensities is 1. |
maxY |
number |
undefined |
Rescale the spectrum so that the largest intensity equals maxY. Conflicts with normedY. |
The parser follows the Mascot generic format:
- Parameters declared before the first
BEGINare global: they are copied into themetaof every entry, where a parameter of the entry overrides them. - Lines starting with
#,;,!or/are comments, and blank lines are ignored. - A peak line holds an m/z and an intensity, optionally followed by the fragment
charge, which is ignored. Values may be separated by
,\t,,or;.
An unparsable line, or fragment ions found outside of a BEGIN / END block,
throws an error mentioning the line number.