Skip to content

Commit e777d01

Browse files
committed
feat: CT exports with alias atoms
1 parent 6385125 commit e777d01

5 files changed

Lines changed: 31 additions & 14 deletions

File tree

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"nmr-processing": "^19.1.0",
9393
"numeral": "^2.0.6",
9494
"openchemlib": "^9.8.0",
95-
"openchemlib-utils": "^8.3.1",
95+
"openchemlib-utils": "^8.4.0",
9696
"papaparse": "^5.5.3",
9797
"react-d3-utils": "^3.1.2",
9898
"react-dropzone": "^14.3.8",

src/component/panels/MoleculesPanel/MoleculePanelHeader.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,20 @@ export default function MoleculePanelHeader(props: MoleculePanelHeaderProps) {
193193
[molecules, currentIndex, copyHandler, saveAsPNGHandler, saveAsSVGHandler],
194194
);
195195

196-
function handlePasteMolfileAction() {
196+
function handlePasteMoleculeAction() {
197197
onClickPastMolecule?.();
198-
void readText().then(handlePasteMolfile);
198+
void readText().then(handlePasteMolecule);
199199
}
200-
async function handlePasteMolfile(molfile: string | undefined) {
201-
if (!molfile) return;
200+
201+
/**
202+
* The moleecule pasted could be SMILES, molfile or SDF
203+
* @param text - text from clipboard
204+
* @returns
205+
*/
206+
async function handlePasteMolecule(text: string | undefined) {
207+
if (!text) return;
202208
try {
203-
const molecules = getMolecules(molfile);
209+
const molecules = getMolecules(text);
204210
dispatch({ type: 'ADD_MOLECULES', payload: { molecules } });
205211
} catch {
206212
toaster.show({
@@ -296,7 +302,7 @@ export default function MoleculePanelHeader(props: MoleculePanelHeaderProps) {
296302
<Toolbar.Item
297303
tooltip="Paste SMILES or molfile"
298304
icon={<FaPaste />}
299-
onClick={handlePasteMolfileAction}
305+
onClick={handlePasteMoleculeAction}
300306
/>
301307
{renderSource === 'moleculePanel' && (
302308
<Toolbar.Item
@@ -350,7 +356,7 @@ export default function MoleculePanelHeader(props: MoleculePanelHeaderProps) {
350356
<ClipboardFallbackModal
351357
mode={shouldFallback}
352358
onDismiss={cleanShouldFallback}
353-
onReadText={handlePasteMolfile}
359+
onReadText={handlePasteMolecule}
354360
text={text}
355361
label="Molfile"
356362
/>

src/data/SpectraManager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { BlobWriter, TextReader, ZipWriter } from '@zip.js/zip.js';
1313
import fileSaver from 'file-saver';
1414
import * as OCL from 'openchemlib';
15+
import { toMolfile } from 'openchemlib-utils';
1516

1617
import type { State } from '../component/reducer/Reducer.js';
1718

@@ -204,8 +205,12 @@ export async function exportForCT(options: ExportForCTOptions) {
204205
const { molfile } = molecules[0];
205206
const molecule = OCL.Molecule.fromMolfile(molfile);
206207
const molFileName = molecule.getMolecularFormula().formula;
208+
const ctMolfile = toMolfile(molecule, {
209+
includeCustomAtomLabelsAsALines: true,
210+
customLabelPosition: 'normal',
211+
});
207212

208-
await zip.add(`${molFileName}.mol`, new TextReader(molecule.toMolfile()));
213+
await zip.add(`${molFileName}.mol`, new TextReader(ctMolfile));
209214

210215
const blob = await zip.close();
211216
fileSaver.saveAs(blob, `${name}.zip`);

src/data/molecules/MoleculeManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ function getLabelNumber(reserveNumbers: number[]): number {
9898
return 1;
9999
}
100100

101+
/**
102+
* We will extract the molecule(s) from the text.
103+
* Because a molfile is actually a SDF we can just call 'readSDF' method
104+
* @param text - text containing one or several molecules in SMILES, molfile or SDF format
105+
* @returns
106+
*/
101107
export function getMolecules(text: string) {
102108
// parse SDF
103109
if (/v[23]000/i.test(text)) {

0 commit comments

Comments
 (0)