-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: export to nmredata * feat: add 1d signals does not assigned to nmredata
- Loading branch information
Showing
11 changed files
with
23,468 additions
and
17 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import jszip from 'jszip'; | ||
import { getGroupedDiastereotopicAtomIDs } from 'openchemlib-utils'; | ||
import { Molecule as OCLMolecule } from 'openchemlib/full'; | ||
|
||
import { get1DSignals } from './util/get1DSignals'; | ||
import { get2DSignals } from './util/get2DSignals'; | ||
import { getLabels } from './util/getLabels'; | ||
|
||
const tags = { | ||
solvent: 'SOLVENT', | ||
temperature: 'TEMPERATURE', | ||
assignment: 'ASSIGNMENT', | ||
j: 'J', | ||
signals: 'SIGNALS', | ||
id: 'ID', | ||
}; | ||
|
||
export function toNmredata(state, options = {}) { | ||
const { | ||
data, // it would be changed depending of the final location | ||
molecules, | ||
} = state || { | ||
data: [], // it would be changed depending of the final location | ||
molecules: [], | ||
}; | ||
|
||
const { id, prefix = '\n> <NMREDATA_', filename = 'nmredata' } = options; | ||
|
||
let sdfResult = ''; | ||
let nmrRecord = new jszip(); | ||
|
||
let molecule = OCLMolecule.fromMolfile(molecules[0].molfile); | ||
molecule.addImplicitHydrogens(); | ||
let groupedDiaIDs = getGroupedDiastereotopicAtomIDs(molecule); | ||
|
||
let groupedOptions = { | ||
prefix, | ||
molecule, | ||
groupedDiaIDs, | ||
nmrRecord, | ||
}; | ||
|
||
sdfResult += molecules[0].molfile; | ||
let labels = getLabels(data, groupedOptions); | ||
sdfResult += `${prefix}VERSION>\n1.1\\\n`; | ||
sdfResult += putTag(data, 'temperature', { prefix }); | ||
sdfResult += putTag(data, 'solvent', { prefix }); | ||
|
||
if (id) { | ||
sdfResult += `${prefix + tags.id}>\nid\\\n`; | ||
} | ||
|
||
sdfResult += formatAssignments(labels.byDiaID, groupedOptions); | ||
sdfResult += get1DSignals(data, labels, groupedOptions); | ||
sdfResult += get2DSignals(data, labels, groupedOptions); | ||
sdfResult += '\n$$$$\n'; | ||
nmrRecord.file(`${filename}.sdf`, sdfResult); | ||
return nmrRecord; | ||
} | ||
|
||
function formatAssignments(labels, options) { | ||
const { prefix } = options; | ||
let str = `${prefix + tags.assignment}>\n`; | ||
for (let l in labels) { | ||
let atoms = labels[l].atoms; | ||
str += `${labels[l].label}, ${labels[l].shift}`; // change to add label | ||
for (let atom of atoms) str += `, ${atom}`; | ||
str += '\\\n'; | ||
} | ||
return str; | ||
} | ||
|
||
function putTag(spectra, tag, options = {}) { | ||
const { prefix } = options; | ||
let str = ''; | ||
for (let spectrum of spectra) { | ||
if (spectrum.info[tag]) { | ||
str += `${prefix + tags[tag]}>\n${String(spectrum.info[tag])}\\\n`; | ||
break; | ||
} | ||
} | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function flat2DSignals(signals) { | ||
let flattedSignal = []; | ||
for (let i = 0; i < signals.length; i++) { | ||
let { x, y } = signals[i]; | ||
flattedSignal.push(x, y); | ||
} | ||
return flattedSignal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { getToFix } from './getToFix'; | ||
|
||
export function get1DSignals(data, labels, options = {}) { | ||
const { prefix, nmrRecord } = options; | ||
let str = ''; | ||
let nucleusArray = []; | ||
for (let spectrum of data) { | ||
if (spectrum.info.dimension > 1) continue; | ||
|
||
let ranges = spectrum.ranges.values || []; | ||
|
||
let nucleus = spectrum.info.nucleus; | ||
let counter = 1; | ||
let subfix = ''; | ||
nucleusArray.forEach((e) => { | ||
if (e === nucleus) counter++; | ||
}); | ||
nucleusArray.push(nucleus); | ||
|
||
if (counter > 1) subfix = `#${counter}`; | ||
|
||
str += `${prefix}1D_${nucleus.toUpperCase()}#${subfix}>`; | ||
|
||
if (spectrum.info.frequency) { | ||
str += `\nLarmor=${Number(spectrum.info.frequency).toFixed(2)}\\`; | ||
} | ||
|
||
if (spectrum.source.jcamp) { | ||
nmrRecord.file( | ||
`jcamp_folder/1d/${spectrum.display.name}`, | ||
spectrum.source.jcamp, | ||
); | ||
str += `\nSpectrum_Jcamp=file:./jcamp_folder/1d/${spectrum.display.name}\\`; | ||
} | ||
|
||
let toFix = getToFix(nucleus)[0]; | ||
|
||
for (let range of ranges) { | ||
let signals = range.signal; //.filter((s) => s.diaID && s.diaID.length); | ||
|
||
for (let signal of signals) { | ||
let { multiplicity } = signal; | ||
if ((!multiplicity || multiplicity === 'm') && nucleus === '1H') { | ||
str += `\n${Number(range.from).toFixed(toFix)}-${Number( | ||
range.to, | ||
).toFixed(toFix)}`; | ||
} else if (signal.delta) { | ||
str += `\n${Number(signal.delta).toFixed(toFix)}`; | ||
} else { | ||
continue; | ||
} | ||
|
||
let signalLabel = ''; | ||
|
||
if (signal.diaID && signal.diaID.length > 0) { | ||
signal.diaID.forEach((diaID, i, arr) => { | ||
let separator = ', '; | ||
if (i === arr.length - 1) separator = ''; | ||
let label = labels.byDiaID[diaID].label || diaID; | ||
signalLabel += `(${label})${separator}`; | ||
}); | ||
str += `, L=${signalLabel}`; | ||
} | ||
|
||
if (nucleus === '1H') { | ||
if (signal.multiplicity) str += `, S=${signal.multiplicity}`; | ||
|
||
let jCoupling = signal.j; | ||
if (Array.isArray(jCoupling) && jCoupling.length) { | ||
let separator = ', J='; | ||
for (let i = 0; i < jCoupling.length; i++) { | ||
str += `${separator}${Number(jCoupling[i].coupling).toFixed(3)}`; | ||
if (jCoupling[i].diaID) { | ||
let { diaID } = jCoupling[i]; | ||
if (!Array.isArray(diaID)) diaID = [diaID]; | ||
if (!diaID.length) continue; | ||
let jCouple = labels[diaID[0]].label || String(diaID[0]); | ||
str += `(${jCouple})`; | ||
} | ||
separator = ', '; | ||
} | ||
} | ||
if (range.integral) { | ||
str += `, E=${Number(range.integral).toFixed(toFix)}`; | ||
} else if (range.pubIntegral) { | ||
str += `, E=${range.putIntegral.toFixed(toFix)}`; | ||
} else if (range.signal[0].nbAtoms !== undefined) { | ||
str += `, E=${range.signal[0].nbAtoms}`; | ||
} | ||
} | ||
} | ||
if (signals.length) str += '\\'; | ||
} | ||
str += '\n'; | ||
} | ||
|
||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { getCouplingObserved } from './getCouplingObserved'; | ||
import { getToFix } from './getToFix'; | ||
|
||
const isArray = Array.isArray; | ||
|
||
export function get2DSignals(data, labels, options = {}) { | ||
let { prefix, nmrRecord } = options; | ||
let { byDiaID } = labels; | ||
let str = ''; | ||
let nucleusRecorded = []; | ||
for (let spectrum of data) { | ||
if (spectrum.info.dimension < 2) continue; | ||
|
||
let { nucleus, experiment, pulseSequence } = spectrum.info; | ||
|
||
if (experiment) prefix = `\n> 2D ${experiment} <NMREDATA_2D_`; | ||
|
||
nucleusRecorded.push(nucleus); | ||
|
||
let couplingObserved = getCouplingObserved(experiment); | ||
if (nucleus) { | ||
str += `${prefix}${nucleus[1]}_${couplingObserved}_${nucleus[0]}>`; | ||
} | ||
let toFix = getToFix(nucleus); | ||
|
||
str += `\nLarmor=${Number(spectrum.info.baseFrequency[0]).toFixed(2)}\\`; | ||
|
||
if (spectrum.source.jcamp) { | ||
let pathPrefix = 'jcamp_folder/1d/'; | ||
nmrRecord.file( | ||
`${pathPrefix}'${spectrum.display.name}`, | ||
spectrum.source.jcamp, | ||
); | ||
str += `\nSpectrum_Jcamp=file:./${pathPrefix}${spectrum.display.name}\\`; | ||
} | ||
|
||
if (experiment) str += `\nCorType=${experiment} \\`; | ||
if (pulseSequence) str += `\nPulseProgram=${pulseSequence} \\`; | ||
|
||
let zones = spectrum.zones.values || []; | ||
for (let zone of zones) { | ||
let signals = zone.signal; | ||
for (let signal of signals) { | ||
let { x, y, peak } = signal; | ||
let xLabel = getAssignment(x, byDiaID, toFix[0]); | ||
let yLabel = getAssignment(y, byDiaID, toFix[1]); | ||
let intensity = Math.max(...peak.map((e) => e.z)); | ||
str += `\n${xLabel}/${yLabel}, I=${intensity.toFixed(2)}\\`; | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
|
||
function getAssignment(axis, labels, toFix) { | ||
let { diaID, delta } = axis; | ||
if (diaID) { | ||
if (!isArray(diaID)) diaID = [diaID]; | ||
if (diaID.length < 1) Number(delta).toFixed(toFix); | ||
let label = diaID.map((diaID) => labels[diaID].label).join(','); | ||
return diaID.length > 1 ? `(${label})` : label; | ||
} | ||
return Number(delta).toFixed(toFix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function getCouplingObserved(experiment) { | ||
switch (experiment.toLowerCase()) { | ||
case 'hsqc': | ||
case 'cosy': | ||
return '1J'; | ||
case 'hmbc': | ||
return 'NJ'; | ||
default: | ||
return 'NJ'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { getShortestPaths } from 'openchemlib-utils'; | ||
|
||
import { flat2DSignals } from './flat2DSignals'; | ||
import { getToFix } from './getToFix'; | ||
|
||
export function getLabels(data, options = {}) { | ||
const { groupedDiaIDs, molecule } = options; | ||
|
||
let connections = getShortestPaths(molecule, { toLabel: 'H', maxLength: 1 }); | ||
|
||
let byDiaID = {}; | ||
let byAssignNumber = {}; | ||
for (let spectrum of data) { | ||
let { dimension, nucleus } = spectrum.info; | ||
let toFix = getToFix(nucleus); | ||
|
||
let [roiKey, flatSignals] = | ||
dimension > 1 ? ['zones', flat2DSignals] : ['ranges', (s) => s || []]; | ||
|
||
let rois = spectrum[roiKey].values || []; | ||
for (let roi of rois) { | ||
let signals = flatSignals(roi.signal); | ||
for (let i = 0; i < signals.length; i++) { | ||
let diaIDs = signals[i].diaID || []; | ||
for (let diaID of diaIDs) { | ||
let delta = Number(signals[i].delta).toFixed(toFix[i % dimension]); | ||
// get atomLabel | ||
let groupedOclID = groupedDiaIDs.find((dia) => { | ||
if (dia.oclID === diaID) return true; | ||
return false; | ||
}); | ||
// the addition of one in atom number it is because the atoms enumeration starts from zero | ||
|
||
let labelOptions = { | ||
atom: groupedOclID.atoms[0], | ||
molecule, | ||
connections, | ||
atomLabel: groupedOclID.atomLabel, | ||
}; | ||
|
||
byDiaID[diaID] = { | ||
atoms: groupedOclID.atoms.map((e) => e + 1), | ||
shift: delta, | ||
label: createLabel(labelOptions), | ||
}; | ||
|
||
for (let atom of groupedOclID.atoms) { | ||
labelOptions.atom = atom; | ||
byAssignNumber[atom] = { | ||
shift: delta, | ||
diaID, | ||
label: createLabel(labelOptions), | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return { byAssignNumber, byDiaID }; | ||
} | ||
|
||
function createLabel(options) { | ||
const { atom, molecule, atomLabel, connections } = options; | ||
let label = ''; | ||
if (atomLabel !== 'C') { | ||
let connectedTo = connections[atom]; | ||
let path = connectedTo.find((e) => e && e.length > 1); | ||
let pLabel = `${atomLabel}${path[0] + 1}`; | ||
let hLabel = `${molecule.getAtomLabel(path[1])}${path[1] + 1}`; | ||
label = `${pLabel}${hLabel}`; | ||
} else { | ||
label = `${atomLabel}${atom + 1}`; | ||
} | ||
return label; | ||
} |
Oops, something went wrong.