Skip to content

Commit 2d84e7e

Browse files
feat: expose options for automatic processing and range/zone detection
1 parent 13754e6 commit 2d84e7e

File tree

2 files changed

+122
-28
lines changed

2 files changed

+122
-28
lines changed

app/scripts/nmr-cli/src/index.ts

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import yargs, { type Argv, type CommandModule, type Options } from 'yargs'
3-
import { loadSpectrumFromURL, loadSpectrumFromFilePath } from './prase-spectra'
3+
import { loadSpectrumFromURL, loadSpectrumFromFilePath } from './parse/prase-spectra'
44
import { generateSpectrumFromPublicationString } from './publication-string'
55
import { parsePredictionCommand } from './prediction/parsePredictionCommand'
66
import { hideBin } from 'yargs/helpers'
@@ -14,18 +14,14 @@ Commands:
1414
predict Predict spectrum from Mol
1515
1616
Options for 'parse-spectra' command:
17-
-u, --url File URL
18-
-p, --path Directory path
19-
-s, --capture-snapshot Capture snapshot
17+
-u, --url File URL
18+
-dir, --dir-path Directory path
19+
-s, --capture-snapshot Capture snapshot
20+
-p, --auto-processing Automatic processing of spectrum (FID → FT spectra).
21+
-d, --auto-detection Enable ranges and zones automatic detection.
2022
2123
Arguments for 'parse-publication-string' command:
2224
publicationString Publication string
23-
24-
Options for 'parse-spectra' command:
25-
-u, --url File URL
26-
-p, --path Directory path
27-
-s, --capture-snapshot Capture snapshot
28-
2925
3026
Options for 'predict' command:
3127
-ps,--peakShape Peak shape algorithm (default: "lorentzian") choices: ["gaussian", "lorentzian"]
@@ -46,16 +42,43 @@ Options for 'predict' command:
4642
4743
Examples:
4844
nmr-cli parse-spectra -u file-url -s // Process spectra files from a URL and capture an image for the spectra
49-
nmr-cli parse-spectra -p directory-path -s // process a spectra files from a directory and capture an image for the spectra
45+
nmr-cli parse-spectra -dir directory-path -s // process a spectra files from a directory and capture an image for the spectra
5046
nmr-cli parse-spectra -u file-url // Process spectra files from a URL
51-
nmr-cli parse-spectra -p directory-path // Process spectra files from a directory
47+
nmr-cli parse-spectra -dir directory-path // Process spectra files from a directory
5248
nmr-cli parse-publication-string "your publication string"
5349
`
5450

55-
interface FileOptionsArgs {
56-
u?: string
57-
p?: string
58-
s?: boolean
51+
export interface FileOptionsArgs {
52+
/**
53+
* -u, --url
54+
* File URL to load remote spectra or data.
55+
*/
56+
u?: string;
57+
58+
/**
59+
* -dir, --dir-path
60+
* Local directory path for file input or output.
61+
*/
62+
dir?: string;
63+
64+
/**
65+
* -s, --capture-snapshot
66+
* Capture a visual snapshot of the current state or spectrum.
67+
*/
68+
s?: boolean;
69+
70+
/**
71+
* -p, --auto-processing
72+
* Automatically process spectrum from FID to FT spectra.
73+
* Mandatory when automatic detection (`--auto-detection`) is enabled.
74+
*/
75+
p?: boolean;
76+
77+
/**
78+
* -d, --auto-detection
79+
* Perform automatic ranges and zones detection.
80+
*/
81+
d?: boolean;
5982
}
6083

6184
// Define options for parsing a spectra file
@@ -66,8 +89,8 @@ const fileOptions: { [key in keyof FileOptionsArgs]: Options } = {
6689
type: 'string',
6790
nargs: 1,
6891
},
69-
p: {
70-
alias: 'path',
92+
dir: {
93+
alias: 'dir-path',
7194
describe: 'Directory path',
7295
type: 'string',
7396
nargs: 1,
@@ -77,6 +100,16 @@ const fileOptions: { [key in keyof FileOptionsArgs]: Options } = {
77100
describe: 'Capture snapshot',
78101
type: 'boolean',
79102
},
103+
p: {
104+
alias: 'auto-processing',
105+
describe: 'Auto processing',
106+
type: 'boolean',
107+
},
108+
d: {
109+
alias: 'auto-detection',
110+
describe: 'Ranges and zones auto detection',
111+
type: 'boolean',
112+
},
80113
} as const
81114

82115
const parseFileCommand: CommandModule<{}, FileOptionsArgs> = {
@@ -85,21 +118,25 @@ const parseFileCommand: CommandModule<{}, FileOptionsArgs> = {
85118
builder: yargs => {
86119
return yargs
87120
.options(fileOptions)
88-
.conflicts('u', 'p') as Argv<FileOptionsArgs>
121+
.conflicts('u', 'dir') as Argv<FileOptionsArgs>
89122
},
90123
handler: argv => {
124+
125+
const { u, dir } = argv;
91126
// Handle parsing the spectra file logic based on argv options
92-
if (argv?.u) {
93-
loadSpectrumFromURL(argv.u, argv.s).then(result => {
127+
if (u) {
128+
loadSpectrumFromURL({ u, ...argv }).then(result => {
94129
console.log(JSON.stringify(result))
95130
})
96131
}
97132

98-
if (argv?.p) {
99-
loadSpectrumFromFilePath(argv.p, argv.s).then(result => {
133+
134+
if (dir) {
135+
loadSpectrumFromFilePath({ dir, ...argv }).then(result => {
100136
console.log(JSON.stringify(result))
101137
})
102138
}
139+
103140
},
104141
}
105142

app/scripts/nmr-cli/src/parse/prase-spectra.ts

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import { join, isAbsolute } from 'path'
2-
import { type NmriumState } from '@zakodium/nmrium-core'
2+
import { NmriumData, ParsingOptions, type NmriumState } from '@zakodium/nmrium-core'
33
import init from '@zakodium/nmrium-core-plugins'
44
import playwright from 'playwright'
55
import { FileCollection } from 'file-collection'
6+
import { FileOptionsArgs } from '..'
7+
import { isSpectrum2D } from './data/data2d/isSpectrum2D'
8+
import { initiateDatum2D } from './data/data2d/initiateDatum2D'
9+
import { initiateDatum1D } from './data/data1D/initiateDatum1D'
10+
import { detectZones } from './data/data2d/detectZones'
11+
import { detectRanges } from './data/data1D/detectRanges'
12+
import { Filters1DManager, Filters2DManager } from 'nmr-processing'
13+
14+
type RequiredKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
15+
16+
const parsingOptions: ParsingOptions = {
17+
onLoadProcessing: { autoProcessing: true },
18+
sourceSelector: { general: { dataSelection: 'preferFT' } },
19+
experimentalFeatures: true
20+
};
621

722
interface Snapshot {
823
image: string
@@ -79,9 +94,39 @@ async function captureSpectraViewAsBase64(nmriumState: Partial<NmriumState>) {
7994
await browser.close()
8095

8196
return snapshots
97+
98+
}
99+
100+
interface ProcessSpectraOptions {
101+
autoDetection: boolean; autoProcessing: boolean;
82102
}
83103

84-
async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
104+
function processSpectra(data: NmriumData, options: ProcessSpectraOptions) {
105+
106+
const { autoDetection = false, autoProcessing = false } = options
107+
108+
for (let index = 0; index < data.spectra.length; index++) {
109+
const inputSpectrum = data.spectra[index]
110+
const is2D = isSpectrum2D(inputSpectrum);
111+
const spectrum = is2D ? initiateDatum2D(inputSpectrum) : initiateDatum1D(inputSpectrum);
112+
113+
if (autoProcessing) {
114+
isSpectrum2D(spectrum) ? Filters2DManager.reapplyFilters(spectrum) : Filters1DManager.reapplyFilters(spectrum)
115+
}
116+
117+
if (autoDetection && spectrum.info.isFt) {
118+
isSpectrum2D(spectrum) ? detectZones(spectrum) : detectRanges(spectrum);
119+
}
120+
121+
data.spectra[index] = spectrum;
122+
}
123+
124+
125+
}
126+
127+
async function loadSpectrumFromURL(options: RequiredKey<FileOptionsArgs, 'u'>) {
128+
const { u: url, s: enableSnapshot = false, p: autoProcessing = false, d: autoDetection = false } = options;
129+
85130
const { pathname: relativePath, origin: baseURL } = new URL(url)
86131
const source = {
87132
entries: [
@@ -92,11 +137,16 @@ async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
92137
baseURL,
93138
}
94139

95-
const [nmriumState] = await core.readFromWebSource(source);
140+
const [nmriumState] = await core.readFromWebSource(source, parsingOptions);
96141
const {
97142
data, version
98143
} = nmriumState;
99144

145+
146+
if (data) {
147+
processSpectra(data, { autoDetection, autoProcessing });
148+
}
149+
100150
let images: Snapshot[] = []
101151

102152
if (enableSnapshot) {
@@ -106,7 +156,9 @@ async function loadSpectrumFromURL(url: string, enableSnapshot = false) {
106156
return { data, version, images }
107157
}
108158

109-
async function loadSpectrumFromFilePath(path: string, enableSnapshot = false) {
159+
async function loadSpectrumFromFilePath(options: RequiredKey<FileOptionsArgs, 'dir'>) {
160+
const { dir: path, s: enableSnapshot = false, p: autoProcessing = false, d: autoDetection = false } = options;
161+
110162
const dirPath = isAbsolute(path) ? path : join(process.cwd(), path)
111163

112164
const fileCollection = await FileCollection.fromPath(dirPath, {
@@ -115,7 +167,12 @@ async function loadSpectrumFromFilePath(path: string, enableSnapshot = false) {
115167

116168
const {
117169
nmriumState: { data, version },
118-
} = await core.read(fileCollection)
170+
} = await core.read(fileCollection, parsingOptions)
171+
172+
173+
if (data) {
174+
processSpectra(data, { autoDetection, autoProcessing })
175+
}
119176

120177
let images: Snapshot[] = []
121178

0 commit comments

Comments
 (0)