Skip to content

Markup-driven lipid CID spectrum generator (experimental) - #806

Draft
YukiMatsuzawa wants to merge 93 commits into
masterfrom
experimental/lipid_spectrum_generator_by_markup
Draft

YukiMatsuzawa wants to merge 93 commits into
masterfrom
experimental/lipid_spectrum_generator_by_markup

Conversation

@YukiMatsuzawa

Copy link
Copy Markdown
Contributor

Summary

Long-running branch implementing a markup-XML-driven lipid CID spectrum generator: a Roslyn source generator that reads MsdialLipidCidSpectrumModel.xml (precursor/class-ion/chain-ion rules per lipid class) plus Constants.xml, and emits one {Class}CidLipidSpectrumGenerator per lipid class - intended as a declarative alternative/successor to the hand-written {Class}SpectrumGenerator classes in FacadeLipidSpectrumGenerator.

Opening as draft to track this against master and keep it visible/reviewable rather than parked. #805 (claude/lipid-annotation-design-e016f8 -> this branch) adds a SpectrumViewer tool for authoring/validating these rules and will land here once ready.

Status: draft / WIP

Not yet wired into any production annotation path - see #805 for current known gaps (an unrelated pre-existing TotalChain plasmalogen-rendering issue, and lipid classes with no matching LbmClass).

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com

mikiko.takahashi and others added 30 commits July 20, 2023 16:27
Added the `FormulaStringParser` class to parse chemical formula strings into a dictionary representation, with methods for validation and conversion. Introduced corresponding tests in `FormulaStringParserTests` to ensure functionality. Updated `CompMs.Common.Lipidomics.SourceGenerator.csproj` with an `InternalsVisibleTo` directive for test project access.
Refactored `ToIon` method in `LipidSpectrumGeneratorTypeGenerator.cs` to use a new `ParseElements` helper method, improving modularity and readability. Added `ParseElements` as a private static method to encapsulate element parsing logic.

Updated `MsdialLipidCidSpectrumModel.xml` to include new `<NL>` and `<PI>` elements with detailed metadata and commented-out alternative representations for future extensibility.
Updated `_peakElements` to include `Charge` and `Formula`,
enabling dynamic `MZ` calculation when `MZ` is `0`.
Reused `ParseElements` for `Formula` processing.
Added a test case in `MsdialLipidCidSpectrumModel.xml`
to validate the new functionality. Enhances flexibility
and accuracy in lipidomics data analysis.
Introduced the `FormulaSentenceParser` class in the
`CompMs.Common.Lipidomics.SourceGenerator` namespace to parse
chemical formula sentences into structured data. Key features
include regex-based parsing, support for terms and factors, and
internal classes (`Sentence`, `Context`, `SubVar`) for data
representation. Added unit tests in `FormulaSentenceParserTest`
to validate parsing functionality with multiple test cases.
Updated the metadata key in `LipidSpectrumGeneratorTypeGenerator.cs` to use `build_metadata.AdditionalFiles.Category` for accurate retrieval.

Enhanced `CompMs.Common.Lipidomics.SourceGeneratorTests.csproj` by:
- Adding an `<Import>` for `SourceGenerator.props` to integrate build configurations.
- Including `MsdialLipidCidSpectrumModel.xml` with a `<Category>` of `Cid` for better test resource management.
The `Context` class was refactored into its own file in the
`CompMs.Common.Lipidomics.SourceGenerator` namespace, improving
modularity and readability. It now resolves terms using constants
and regex-based rules. A new `ContextTests` class was added to
validate its functionality with unit tests. The `Context` class
was removed from `FormulaSentenceParser.cs`.
Added a `_vars` dictionary and `RegisterHandle` to `Context` for
scoped `SubVar` management. Introduced `Register` and `Resolve`
methods to handle dynamic variable resolution. Updated `Sentence`
and `SubVar` to support `Sentence`-based values and caching.
Refactored `FormulaSentenceParser` to use `SubVar` objects and
removed unused methods and dependencies. General improvements
to enable dynamic and scoped variable handling.
Refactored the source generator to dynamically handle constants
defined in an external `Constants.xml` file, generating a
`ConstantFormulas` class at build time. Replaced the
`EmitStandardFormulas` method with the new `EmitConstants` method
and updated related namespaces. Adjusted project files to support
the new `IsConstants` metadata for better build integration.

Enhanced test coverage by parameterizing tests using `DynamicData`
and improving test data management. Refactored test assertions to
validate generated spectra against reference data.

Improved code clarity, maintainability, and consistency across
the project.
Updated `Resolve` methods in the `Context` class to simplify
output by omitting multipliers when they equal `1`. Adjusted
existing tests and added new test cases in `ContextTests` to
validate the updated behavior, including complex formulas.
A new static method `IsMarkupFormula` was added to the
`FormulaStringParser` class to validate markup formula strings
using a regex pattern. The method takes a `rawMarkup` string
and an array of `constants` as input and returns a boolean
indicating whether the input matches the expected pattern.

A corresponding test method, `IsMarkupFormulaTest`, was added
to the `FormulaStringParserTests` class. It uses `[DataTestMethod]`
and `[DataRow]` attributes to test various scenarios, including
valid and invalid markup strings. The test ensures the method
works correctly with constants like `["Carbon", "Hydrogen", "Oxygen"]`.
Added a static `IsMarkupFormula(string rawMarkup)` method in
`FormulaStringParser` to validate markup formulas using regex.
Refactored the existing `IsMarkupFormula` method to use
`Regex.IsMatch` for better performance. Updated tests in
`FormulaSentenceParserTest` to cover new functionality.
Added the `ParseMarkupFormula` method to `FormulaStringParser` for
parsing markup strings into dictionaries using `XDocument` and LINQ.
Enhanced `FormulaStringParserTests` with unit tests for this method,
including cases for valid inputs, empty strings, and invalid values.
Fixed a typo in `IsMarkupFormulaTest` test data. Added `using
System.Xml.Linq;` to support XML parsing.
Refactored the `Context` class to improve formula parsing:
- Extracted formula construction logic into a new `ConstructFormula` method for modularity and reusability.
- Updated `Resolve` to use `ConstructFormula` and handle markup formulas via `FormulaStringParser`.
- Added a `GetConstants` method to expose `_constants` keys.

Added a new test case in `ContextTests` to validate markup formula resolution. Improved code readability and maintainability.
Added a check in the `Parse` method of `FormulaSentenceParser`
to identify markup formulas using `FormulaStringParser.IsMarkupFormula`.
If identified, the method now returns a `Sentence` object with
the `Raw` property set to the input string and the `Terms` property
initialized with a single `Term` containing the same `Raw` value
and a count of `1`.
Refactored `FormulaSentenceParser` to handle nullable `SubVar` and added a `LoadElements` method for nested structures. Improved regex patterns in `FormulaStringParser` for stricter XML-like validation and updated related test cases. Enhanced `LipidSpectrumGeneratorTypeGenerator` with a `Context` object for formula resolution and constant handling. Updated `Constants.xml` for shorthand constants and consistency. Modernized code with C# features and improved readability.
mikiko.takahashi and others added 28 commits September 18, 2025 16:13
# Conflicts:
#	src/Common/CommonMVVM/ChemView/ChemOntologyColor.cs
#	src/Common/CommonStandard/Algorithm/Scoring/MsScanMatching.cs
#	src/Common/CommonStandard/Database/LbmQuery.cs
#	src/Common/CommonStandard/Enum/LbmQuery.cs
#	src/Common/CommonStandard/Lipidomics/Lipidomics.cs
#	src/Common/CommonStandard/Lipidomics/LipidomicsConverter.cs
#	src/Common/CommonStandard/Lipidomics/MsmsCharacterization.cs
#	src/Common/CommonStandard/Molecular spectrum networking/MolecularSpectrumNetworkingCommon.cs
#	src/Common/CommonStandard/Ontology/ChemOntologyColorConverter.cs
#	src/Common/CommonStandard/Resources/LbmQueries.txt
#	src/Common/CommonStandard/Resources/LipidClassProperties.csv
#	src/MSFINDER/MsfinderCommonStandard/Resources/LipidQueryMaster.txt
Drop a 90MB temporary resource file and three Visual Studio
wpftmp scratch project files that were committed by mistake.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Parse_FactorIsCorrectTest compared an int[] expectation against the
double factors FormulaSentenceParser actually produces, so
CollectionAssert.AreEqual failed on boxed int vs double even when the
values matched. Switch expectations to double and correct the
"H2O/2" case, whose expected factor was 1 instead of the correct 0.5.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@YukiMatsuzawa
YukiMatsuzawa added this pull request to stack #807 September 24, 2026 09:20
…ster-8a243c

# Conflicts:
#	tests/MSDIAL5/MsdialCoreTestApp/Properties/Resources.Designer.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant