-
Notifications
You must be signed in to change notification settings - Fork 3
feat(nmr-cli): add nmr-correlation as a new command #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vcnainala
merged 6 commits into
NFDI4Chem:development
from
MuhammadAbeerAkmal:nmr-correlation-command
Sep 21, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1bf96a9
feat: expose nmr-correlation package as a new nmr-cli command
MuhammadAbeerAkmal f883685
Merge upstream/development, resolve conflicts with #138
MuhammadAbeerAkmal fc84644
Address review: support local directory input, filter for FT spectra,…
MuhammadAbeerAkmal e078af8
Address round 2 review: explicit if/else branching, clarify spectrum …
MuhammadAbeerAkmal cc293d1
Address review: filter correlation spectra by ranges/zones instead of…
MuhammadAbeerAkmal 7445ef8
Address review: extract readSpectra and filterSpectra helper functions
MuhammadAbeerAkmal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,115 @@ | ||
| import { buildCorrelationData } from 'nmr-correlation' | ||
| import type { Options as CorrelationOptions, Spectra } from 'nmr-correlation' | ||
| import { FifoLogger } from 'fifo-logger' | ||
| import type { NmriumState, Spectrum } from '@zakodium/nmrium-core' | ||
| import { | ||
| buildWebSource, | ||
| core, | ||
| loadFileCollection, | ||
| parsingOptions, | ||
| processSpectra, | ||
| } from './parse/prase-spectra' | ||
| import { isSpectrum2D } from './parse/data/data2d/isSpectrum2D' | ||
|
|
||
| // Default tolerances confirmed by vcnainala on issue #66 | ||
| const DEFAULT_TOLERANCE_H = 0.02 | ||
| const DEFAULT_TOLERANCE_C = 0.25 | ||
|
|
||
| export interface CorrelationInput { | ||
| url?: string | ||
| dir?: string | ||
| mf: string | ||
| toleranceH?: number | ||
| toleranceC?: number | ||
| } | ||
|
|
||
| interface ReadSpectraOptions { | ||
| url?: string | ||
| dir?: string | ||
| } | ||
|
|
||
| async function readSpectra( | ||
| options: ReadSpectraOptions, | ||
| logger: FifoLogger | ||
| ): Promise<Partial<NmriumState>> { | ||
| const { url, dir } = options | ||
|
|
||
| if (url) { | ||
| const { state } = await core.readFromWebSource(buildWebSource(url), { | ||
| ...parsingOptions, | ||
| logger, | ||
| }) | ||
| return state | ||
| } | ||
|
|
||
| if (dir) { | ||
| const { state } = await core.read(await loadFileCollection(dir), { | ||
| ...parsingOptions, | ||
| logger, | ||
| }) | ||
| return state | ||
| } | ||
|
|
||
| throw new Error('Either a spectra URL or a local directory path is required') | ||
| } | ||
|
|
||
| // buildCorrelationData needs detected ranges (1D) or zones (2D) to find | ||
| // correlations, so require isFt plus at least one detected range/zone. | ||
| // This also excludes spectra that failed to initiate or failed detection, | ||
| // since those never get ranges/zones populated either. | ||
| // Note: a pre-existing bug (see https://github.com/NFDI4Chem/nmrkit/issues/139) | ||
| // currently makes every spectrum fail initiation, so real cross-spectrum correlation links are untested here. | ||
| function filterSpectra(spectra: Spectrum[]): Spectrum[] { | ||
| return spectra.filter(spectrum => { | ||
| const { info } = spectrum | ||
| if (info.isFt !== true) return false | ||
|
|
||
| if (isSpectrum2D(spectrum)) { | ||
| const { zones } = spectrum | ||
| return zones.values.length > 0 | ||
| } | ||
|
|
||
| const { ranges } = spectrum | ||
| return ranges.values.length > 0 | ||
| }) | ||
| } | ||
|
|
||
| function resolveTolerance(value: number | undefined, fallback: number): number { | ||
| return value === undefined || Number.isNaN(value) ? fallback : value | ||
| } | ||
|
|
||
| export async function generateCorrelationData(input: CorrelationInput) { | ||
| const { url, dir, mf, toleranceH, toleranceC } = input | ||
| const logger = new FifoLogger() | ||
|
|
||
| const state = await readSpectra({ url, dir }, logger) | ||
|
|
||
| if (state.data) { | ||
| processSpectra( | ||
| state.data, | ||
| { autoProcessing: true, autoDetection: true }, | ||
| logger | ||
| ) | ||
| } | ||
|
|
||
| const spectra = filterSpectra(state.data?.spectra ?? []) | ||
|
|
||
| const options: CorrelationOptions = { | ||
| mf, | ||
| tolerance: { | ||
| H: resolveTolerance(toleranceH, DEFAULT_TOLERANCE_H), | ||
| C: resolveTolerance(toleranceC, DEFAULT_TOLERANCE_C), | ||
| }, | ||
| } | ||
|
|
||
| let correlationData | ||
| try { | ||
| correlationData = buildCorrelationData(spectra as Spectra, options) | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Failed to build correlation data: ${error instanceof Error ? error.message : String(error)}` | ||
| ) | ||
| } | ||
|
|
||
| return { ...correlationData, logs: logger.getLogs() } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.