From 0d606190216646afeaaa685740f773b4f47c3847 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Fri, 13 Feb 2026 17:04:26 +0900 Subject: [PATCH 01/13] Add sdf format exporter --- src/Common/CommonStandard/Enum/CommonEnums.cs | 2 +- .../MsdialCore/Export/AlignmentSdfExporter.cs | 28 ++++ .../MsdialCore/Export/AnalysisSdfExporter.cs | 37 +++++ .../MsdialCore/Export/SpectraExport.cs | 155 +++++++++++++++++- .../Model/Dims/DimsMethodModel.cs | 1 + .../Model/Gcms/GcmsMethodModel.cs | 1 + .../ImagingImms/ImagingImmsMethodModel.cs | 10 ++ .../Model/Imms/ImmsMethodModel.cs | 10 ++ .../Model/Lcimms/LcimmsMethodModel.cs | 11 ++ .../Model/Lcms/LcmsMethodModel.cs | 11 ++ .../ViewModel/Dims/DimsAlignmentViewModel.cs | 2 +- .../ViewModel/Dims/DimsAnalysisViewModel.cs | 2 +- .../ViewModel/Dims/DimsMethodViewModel.cs | 9 + .../ViewModel/Imms/ImmsAlignmentViewModel.cs | 2 +- .../ViewModel/Lcms/LcmsAlignmentViewModel.cs | 2 +- 15 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs create mode 100644 src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs diff --git a/src/Common/CommonStandard/Enum/CommonEnums.cs b/src/Common/CommonStandard/Enum/CommonEnums.cs index 9ecfec21e..1e4720d51 100644 --- a/src/Common/CommonStandard/Enum/CommonEnums.cs +++ b/src/Common/CommonStandard/Enum/CommonEnums.cs @@ -39,7 +39,7 @@ public enum RiCompoundType { Alkanes, Fames } public enum AlignmentIndexType { RT, RI } public enum TargetOmics { Metabolomics, Lipidomics, Proteomics } public enum Ionization { ESI, EI } - public enum ExportSpectraFileFormat { mgf, msp, txt, mat, ms } + public enum ExportSpectraFileFormat { mgf, msp, txt, mat, ms, sdf } public enum ExportspectraType { profile, centroid, deconvoluted } public enum IonAbundanceUnit { Intensity, Height, Area, pmol, fmol, ng, pg, diff --git a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs new file mode 100644 index 000000000..2d01211fd --- /dev/null +++ b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs @@ -0,0 +1,28 @@ +using CompMs.MsdialCore.DataObj; +using CompMs.MsdialCore.MSDec; +using System.IO; + +namespace CompMs.MsdialCore.Export; + +public sealed class AlignmentSdfExporter : IAlignmentSpectraExporter +{ + private readonly bool _exportNoMs2Molecule; + private readonly bool _set2dCoordinates; + public AlignmentSdfExporter(bool exportNoMs2Molecule, bool set2dCoordinates) + { + _exportNoMs2Molecule = exportNoMs2Molecule; + _set2dCoordinates = set2dCoordinates; + } + public AlignmentSdfExporter() : this(exportNoMs2Molecule: true, set2dCoordinates: true) { } + + void IAlignmentSpectraExporter.Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult) + { + SpectraExport.SaveSpectraTableAsSdfFormat( + stream, + spot, + msdecResult.Spectrum, + _exportNoMs2Molecule, + _set2dCoordinates + ); + } +} diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs new file mode 100644 index 000000000..55327eb2a --- /dev/null +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -0,0 +1,37 @@ +using CompMs.MsdialCore.DataObj; +using CompMs.MsdialCore.Parser; +using System; +using System.IO; + +namespace CompMs.MsdialCore.Export +{ + public sealed class AnalysisSdfExporter : IAnalysisExporter + { + private readonly Func> _loaderFactory; + + public AnalysisSdfExporter(Func> loaderFuctory) { + _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); + } + private readonly bool _exportNoMs2Molecule; + private readonly bool _set2dCoordinates; + public AnalysisSdfExporter(bool exportNoMs2Molecule, bool set2dCoordinates) + { + _exportNoMs2Molecule = exportNoMs2Molecule; + _set2dCoordinates = set2dCoordinates; + } + public AnalysisSdfExporter() : this(exportNoMs2Molecule: true, set2dCoordinates: true) { } + + void IAnalysisExporter.Export(Stream stream, AnalysisFileBean analysisFile, ChromatogramPeakFeatureCollection peakFeatureCollection, ExportStyle exportStyle) { + var loader = _loaderFactory(analysisFile); + foreach (var peak in peakFeatureCollection.Items) { + SpectraExport.SaveSpectraTableAsSdfFormat( + stream, + peak, + loader.Load(peak).Spectrum, + _exportNoMs2Molecule, + _set2dCoordinates + ); + } + } + } +} diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 5592a2e73..dc5542c73 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -14,6 +14,10 @@ using System.IO; using System.Linq; using System.Text; +using NCDK; +using NCDK.Smiles; +using NCDK.Layout; +using NCDK.IO; namespace CompMs.MsdialCore.Export { @@ -34,6 +38,9 @@ public static void SaveSpectraTable( case ExportSpectraFileFormat.mgf: SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; + case ExportSpectraFileFormat.sdf: + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum); + break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); break; @@ -53,7 +60,8 @@ public static void SaveSpectraTable( IMSScanProperty scan, DataBaseMapper mapper, ParameterBase parameter, - AlignmentSpotProperty isotopeTrackedLastSpot = null) { + AlignmentSpotProperty isotopeTrackedLastSpot = null) + { switch (spectraFormat) { case ExportSpectraFileFormat.msp: SaveSpectraTableAsNistFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter); @@ -61,6 +69,9 @@ public static void SaveSpectraTable( case ExportSpectraFileFormat.mgf: SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; + case ExportSpectraFileFormat.sdf: + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum); + break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); break; @@ -308,6 +319,148 @@ private static void WriteChromXFieldAsMGF( } #endregion + #region sdf + public static void SaveSpectraTableAsSdfFormat( + Stream stream, + AlignmentSpotProperty spotProperty, + IEnumerable spectrum, + bool exportNoMs2Molecule = false, + bool Set2dCoordinates = false + ) + { + if (!exportNoMs2Molecule && !spotProperty.IsMsmsAssigned) + { + return; + } + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { + if(spotProperty.IsMsmsAssigned) + { + MolBlockFromSmiles(sw, spotProperty.SMILES, Set2dCoordinates); + } + else + { + EmptyMolBlock(sw); + } + WriteChromPeakFeatureInfoAsSdf(sw, spotProperty, spectrum); + sw.WriteLine("$$$$"); + sw.WriteLine(); + } + } + public static void SaveSpectraTableAsSdfFormat( + Stream stream, + ChromatogramPeakFeature chromPeakFeature, + IEnumerable spectrum, + bool exportNoMs2Molecule = false, + bool Set2dCoordinates = false + ) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { + if (chromPeakFeature.SMILES != null) + { + MolBlockFromSmiles(sw, chromPeakFeature.SMILES, Set2dCoordinates); + } + else + { + EmptyMolBlock(sw); + } + WriteChromPeakFeatureInfoAsSdf(sw, chromPeakFeature, spectrum); + sw.WriteLine("$$$$"); + sw.WriteLine(); + } + } + private static void WriteSdfDataItem(StreamWriter sw, string fieldName, string value) + { + sw.WriteLine("> <" + fieldName + ">"); + sw.WriteLine(value ?? string.Empty); + //sw.WriteLine(); + } + private static void EmptyMolBlock(StreamWriter sw) + { + sw.WriteLine(""); + sw.WriteLine(" MS-DIAL"); + sw.WriteLine(); + sw.WriteLine(" 0 0 0 0 0 0 999 V2000"); + sw.WriteLine("M END"); + + } + private static void MolBlockFromSmiles(StreamWriter sw, string smiles, bool Set2dCoordinates) + { + var sp = new SmilesParser(); + IAtomContainer mol = sp.ParseSmiles(smiles); + var sdg = new StructureDiagramGenerator + { + Molecule = mol + }; + if(Set2dCoordinates) + { + sdg.GenerateCoordinates(); + } + mol = sdg.Molecule; + using var w = new MDLV2000Writer(sw); + w.Write(mol); + } + private static void WriteChromPeakFeatureInfoAsSdf( + StreamWriter sw, + AlignmentSpotProperty spotProperty, + IEnumerable spectrum) + { + var sb = new StringBuilder(); + WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); + WriteSdfDataItem(sw, "SCANS", spotProperty.MasterAlignmentID.ToString()); + WriteSdfDataItem(sw, "PRECURSOR_MZ", Math.Round(spotProperty.MassCenter,5).ToString()); + WriteSdfDataItem(sw, "ION_MODE", spotProperty.IonMode.ToString()); + + if (spotProperty.IsMsmsAssigned) + { + if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR_TYPE", spotProperty.AdductType.AdductIonName); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); + WriteSdfDataItem(sw, "MS_LEVEL", "MS2"); + var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); + WriteSdfDataItem(sw, "NUM_PEAKS", peaks.Count.ToString()); + var peaksText = string.Join( + "\n", + spectrum.Select(p => + $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity,0)}" + ) + ); + WriteSdfDataItem(sw, "MASS_SPECTRAL_PEAKS", peaksText); + } + } + private static void WriteChromPeakFeatureInfoAsSdf( + StreamWriter sw, + ChromatogramPeakFeature spotProperty, + IEnumerable spectrum) + { + var sb = new StringBuilder(); + WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); + WriteSdfDataItem(sw, "SCANS", spotProperty.PeakID.ToString()); + WriteSdfDataItem(sw, "PRECURSOR_MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); + WriteSdfDataItem(sw, "ION_MODE", spotProperty.IonMode.ToString()); + + if (spotProperty.IsMsmsContained) + { + if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR_TYPE", spotProperty.AdductType.AdductIonName); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); + WriteSdfDataItem(sw, "MS_LEVEL", "MS2"); + var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); + WriteSdfDataItem(sw, "NUM_PEAKS", peaks.Count.ToString()); + var peaksText = string.Join( + "\n", + spectrum.Select(p => + $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity, 0)}" + ) + ); + WriteSdfDataItem(sw, "MASS_SPECTRAL_PEAKS", peaksText); + } + } + #endregion + #region mat private static void SaveSpectraTableAsMatFormat( Stream stream, diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs index fa87a97a6..95b05a1dd 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs @@ -99,6 +99,7 @@ public DimsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs index be89e2a3c..382df99ff 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs @@ -112,6 +112,7 @@ public GcmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var exportGroups = new List { peakGroup, spectraGroup, gnps, }; diff --git a/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs index f879ec605..6ddff5703 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs @@ -15,6 +15,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Threading; @@ -150,6 +151,15 @@ public AnalysisResultExportModel CreateExportAnalysisModel() { FileSuffix = "mgf", Label = "MASCOT format (*.mgf)" }, + new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_storage.Parameter.ProviderFactoryParameter.Create().Create(file.LoadRawMeasurement(true, true, 5, 5000)), _storage.Parameter.MS2DataType)), + }) + { + FilePrefix = "Sdf", + FileSuffix = "sdf", + Label = "MDL SDfile (*.sdf)" + }, new MsdialAnalysisMassBankRecordExportModel(_storage.Parameter.ProjectParam, StudyContext), }; diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs index 9056c73ae..844ae0aad 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs @@ -104,6 +104,7 @@ public ImmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); @@ -317,6 +318,15 @@ public AnalysisResultExportModel CreateExportAnalysisResult() { FileSuffix = "mgf", Label = "MASCOT format (*.mgf)" }, + new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(ProviderFactory.Create(file), _storage.Parameter.MS2DataType)), + }) + { + FilePrefix = "Sdf", + FileSuffix = "sdf", + Label = "MDL SDfile (*.sdf)" + }, new MsdialAnalysisMassBankRecordExportModel(_storage.Parameter.ProjectParam, StudyContext), }; return new AnalysisResultExportModel(AnalysisFileModelCollection, _storage.Parameter.ProjectParam.ProjectFolderPath, _broker, models); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs index e5d3cbfd3..6744f814f 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs @@ -26,6 +26,7 @@ using Reactive.Bindings.Notifiers; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reactive.Linq; @@ -100,6 +101,7 @@ public LcimmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelCo peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); @@ -319,6 +321,15 @@ static RawMeasurement map(AnalysisFileBean file) { FileSuffix = "mgf", Label = "MASCOT format (*.mgf)" }, + new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(factory.Create(file), Storage.Parameter.MS2DataType)), + }) + { + FilePrefix = "Sdf", + FileSuffix = "sdf", + Label = "MDL SDfile (*.sdf)" + }, new MsdialAnalysisMassBankRecordExportModel(Storage.Parameter.ProjectParam, _studyContext), }; return new AnalysisResultExportModel(AnalysisFileModelCollection, Storage.Parameter.ProjectParam.ProjectFolderPath, _broker, models); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs index 4f4d8d2bd..5393dcadc 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs @@ -22,6 +22,7 @@ using Reactive.Bindings.Notifiers; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reactive.Linq; @@ -110,6 +111,7 @@ public LcmsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var massBank = new AlignmentResultMassBankRecordExportModel(peakSpotSupplyer, storage.Parameter.ProjectParam, studyContext); @@ -412,6 +414,15 @@ public AnalysisResultExportModel ExportAnalysis() { FileSuffix = "mgf", Label = "MASCOT format (*.mgf)" }, + new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_providerFactory.Create(file), _storage.Parameter.MS2DataType)), + }) + { + FilePrefix = "Sdf", + FileSuffix = "sdf", + Label = "MDL SDfile (*.sdf)" + }, new MsdialAnalysisMassBankRecordExportModel(_storage.Parameter.ProjectParam, _studyContext), }; return new AnalysisResultExportModel(AnalysisFileModelCollection, _storage.Parameter.ProjectParam.ProjectFolderPath, _broker, models); diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAlignmentViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAlignmentViewModel.cs index 19b0ef76a..d55a1c0c0 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAlignmentViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAlignmentViewModel.cs @@ -156,7 +156,7 @@ private void SaveSpectra() var request = new SaveFileNameRequest(_model.SaveSpectra) { Title = "Save spectra", - Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", + Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MDF SDfile(*.sdf)|*.sdf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", RestoreDirectory = true, AddExtension = true, }; diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAnalysisViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAnalysisViewModel.cs index 5289f2154..832dea07b 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAnalysisViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsAnalysisViewModel.cs @@ -161,7 +161,7 @@ private void SaveSpectra() var request = new SaveFileNameRequest(_model.SaveSpectra) { Title = "Save spectra", - Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", + Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MDF SDfile(*.sdf)|*.sdf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", RestoreDirectory = true, AddExtension = true, }; diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs index a31460a01..dffaa7a34 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs @@ -119,6 +119,15 @@ private void ExportAnalysis() { FileSuffix = "mgf", Label = "MASCOT format (*.mgf)" }, + new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { + [ExportspectraType.deconvoluted] = new AnalysisMgfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_model.ProviderFactory.Create(file), container.Parameter.MS2DataType)) + }) + { + FilePrefix = "Sdf", + FileSuffix = "sdf", + Label = "MDL SDfile (*.sdf)" + }, new MsdialAnalysisMassBankRecordExportModel(container.Parameter.ProjectParam, _model.StudyContext), }; var model = new AnalysisResultExportModel(_model.AnalysisFileModelCollection, _model.Storage.Parameter.ProjectParam.ProjectFolderPath, _broker, models); diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Imms/ImmsAlignmentViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Imms/ImmsAlignmentViewModel.cs index 0bcc70dad..d9b0b0e47 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Imms/ImmsAlignmentViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Imms/ImmsAlignmentViewModel.cs @@ -187,7 +187,7 @@ private void SaveSpectra() { var request = new SaveFileNameRequest(_model.SaveSpectra) { Title = "Save spectra", - Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", + Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MDF SDfile(*.sdf)|*.sdf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", RestoreDirectory = true, AddExtension = true, }; diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Lcms/LcmsAlignmentViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Lcms/LcmsAlignmentViewModel.cs index 5365e1c87..02a3415b6 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Lcms/LcmsAlignmentViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Lcms/LcmsAlignmentViewModel.cs @@ -219,7 +219,7 @@ private void SaveSpectra() { var request = new SaveFileNameRequest(_model.SaveSpectra) { Title = "Save spectra", - Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", + Filter = "NIST format(*.msp)|*.msp|MassBank format(*.txt)|*.txt;|MASCOT format(*.mgf)|*.mgf|MDL SDfile(*.sdf)|*.sdf|MSFINDER format(*.mat)|*.mat;|SIRIUS format(*.ms)|*.ms", RestoreDirectory = true, AddExtension = true, }; From e043a194627057467510555b17cf4d1da1fd25f8 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Fri, 13 Feb 2026 17:09:43 +0900 Subject: [PATCH 02/13] fix tag line("_" change to " ") --- .../MsdialCore/Export/SpectraExport.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index dc5542c73..89bb5508f 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -409,25 +409,25 @@ private static void WriteChromPeakFeatureInfoAsSdf( var sb = new StringBuilder(); WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); WriteSdfDataItem(sw, "SCANS", spotProperty.MasterAlignmentID.ToString()); - WriteSdfDataItem(sw, "PRECURSOR_MZ", Math.Round(spotProperty.MassCenter,5).ToString()); - WriteSdfDataItem(sw, "ION_MODE", spotProperty.IonMode.ToString()); + WriteSdfDataItem(sw, "PRECURSOR MZ", Math.Round(spotProperty.MassCenter,5).ToString()); + WriteSdfDataItem(sw, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsAssigned) { - if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR_TYPE", spotProperty.AdductType.AdductIonName); + if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); - WriteSdfDataItem(sw, "MS_LEVEL", "MS2"); + WriteSdfDataItem(sw, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); - WriteSdfDataItem(sw, "NUM_PEAKS", peaks.Count.ToString()); + WriteSdfDataItem(sw, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( "\n", spectrum.Select(p => $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity,0)}" ) ); - WriteSdfDataItem(sw, "MASS_SPECTRAL_PEAKS", peaksText); + WriteSdfDataItem(sw, "MASS SPECTRAL PEAKS", peaksText); } } private static void WriteChromPeakFeatureInfoAsSdf( @@ -438,25 +438,25 @@ private static void WriteChromPeakFeatureInfoAsSdf( var sb = new StringBuilder(); WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); WriteSdfDataItem(sw, "SCANS", spotProperty.PeakID.ToString()); - WriteSdfDataItem(sw, "PRECURSOR_MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); - WriteSdfDataItem(sw, "ION_MODE", spotProperty.IonMode.ToString()); + WriteSdfDataItem(sw, "PRECURSOR MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); + WriteSdfDataItem(sw, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsContained) { - if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR_TYPE", spotProperty.AdductType.AdductIonName); + if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); - WriteSdfDataItem(sw, "MS_LEVEL", "MS2"); + WriteSdfDataItem(sw, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); - WriteSdfDataItem(sw, "NUM_PEAKS", peaks.Count.ToString()); + WriteSdfDataItem(sw, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( "\n", spectrum.Select(p => $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity, 0)}" ) ); - WriteSdfDataItem(sw, "MASS_SPECTRAL_PEAKS", peaksText); + WriteSdfDataItem(sw, "MASS SPECTRAL PEAKS", peaksText); } } #endregion From 8cff4a16869dedfb673dd4d696d6d44016655a7b Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Tue, 17 Feb 2026 13:36:53 +0900 Subject: [PATCH 03/13] minor --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 89bb5508f..ea5199542 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -39,7 +39,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, true, false); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -324,7 +324,7 @@ public static void SaveSpectraTableAsSdfFormat( Stream stream, AlignmentSpotProperty spotProperty, IEnumerable spectrum, - bool exportNoMs2Molecule = false, + bool exportNoMs2Molecule = true, bool Set2dCoordinates = false ) { @@ -351,13 +351,17 @@ public static void SaveSpectraTableAsSdfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, IEnumerable spectrum, - bool exportNoMs2Molecule = false, + bool exportNoMs2Molecule = true, bool Set2dCoordinates = false ) { + if (!exportNoMs2Molecule && !chromPeakFeature.IsMsmsContained) + { + return; + } using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { - if (chromPeakFeature.SMILES != null) + if (chromPeakFeature.IsMsmsContained) { MolBlockFromSmiles(sw, chromPeakFeature.SMILES, Set2dCoordinates); } From b098b31e52ebc5514e9e8b26289dcc1b27a07faf Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Tue, 17 Feb 2026 15:42:37 +0900 Subject: [PATCH 04/13] change to use stringBuilder --- .../MsdialCore/Export/AnalysisSdfExporter.cs | 10 +- .../MsdialCore/Export/SpectraExport.cs | 147 +++++++++--------- 2 files changed, 76 insertions(+), 81 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs index 55327eb2a..53d8020c9 100644 --- a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -12,14 +12,8 @@ public sealed class AnalysisSdfExporter : IAnalysisExporter> loaderFuctory) { _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); } - private readonly bool _exportNoMs2Molecule; - private readonly bool _set2dCoordinates; - public AnalysisSdfExporter(bool exportNoMs2Molecule, bool set2dCoordinates) - { - _exportNoMs2Molecule = exportNoMs2Molecule; - _set2dCoordinates = set2dCoordinates; - } - public AnalysisSdfExporter() : this(exportNoMs2Molecule: true, set2dCoordinates: true) { } + private readonly bool _exportNoMs2Molecule = true; + private readonly bool _set2dCoordinates = true; void IAnalysisExporter.Export(Stream stream, AnalysisFileBean analysisFile, ChromatogramPeakFeatureCollection peakFeatureCollection, ExportStyle exportStyle) { var loader = _loaderFactory(analysisFile); diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index ea5199542..932e4ff49 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -9,15 +9,16 @@ using CompMs.MsdialCore.DataObj; using CompMs.MsdialCore.Parameter; using CompMs.MsdialCore.Utility; +using NCDK; +using NCDK.IO; +using NCDK.Layout; +using NCDK.Smiles; using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Text; -using NCDK; -using NCDK.Smiles; -using NCDK.Layout; -using NCDK.IO; namespace CompMs.MsdialCore.Export { @@ -39,7 +40,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, true, false); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, true, true); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -70,7 +71,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum); + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, true, true); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); @@ -324,72 +325,71 @@ public static void SaveSpectraTableAsSdfFormat( Stream stream, AlignmentSpotProperty spotProperty, IEnumerable spectrum, - bool exportNoMs2Molecule = true, - bool Set2dCoordinates = false + bool exportNoMs2Molecule, + bool Set2dCoordinates ) { if (!exportNoMs2Molecule && !spotProperty.IsMsmsAssigned) { return; } - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + var sb = new StringBuilder(8 * 1024); + if(spotProperty.IsMsmsAssigned) { - if(spotProperty.IsMsmsAssigned) - { - MolBlockFromSmiles(sw, spotProperty.SMILES, Set2dCoordinates); - } - else - { - EmptyMolBlock(sw); - } - WriteChromPeakFeatureInfoAsSdf(sw, spotProperty, spectrum); - sw.WriteLine("$$$$"); - sw.WriteLine(); + MolBlockFromSmiles(sb, spotProperty.SMILES, Set2dCoordinates); } + else + { + EmptyMolBlock(sb); + } + WriteChromPeakFeatureInfoAsSdf(sb, spotProperty, spectrum); + sb.AppendLine("$$$$"); + sb.AppendLine(); + var bytes = Encoding.ASCII.GetBytes(sb.ToString()); + stream.Write(bytes, 0, bytes.Length); } public static void SaveSpectraTableAsSdfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, IEnumerable spectrum, - bool exportNoMs2Molecule = true, - bool Set2dCoordinates = false + bool exportNoMs2Molecule, + bool Set2dCoordinates ) { if (!exportNoMs2Molecule && !chromPeakFeature.IsMsmsContained) { return; } - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + var sb = new StringBuilder(8 * 1024); + if (chromPeakFeature.IsMsmsContained) { - if (chromPeakFeature.IsMsmsContained) - { - MolBlockFromSmiles(sw, chromPeakFeature.SMILES, Set2dCoordinates); - } - else - { - EmptyMolBlock(sw); - } - WriteChromPeakFeatureInfoAsSdf(sw, chromPeakFeature, spectrum); - sw.WriteLine("$$$$"); - sw.WriteLine(); + MolBlockFromSmiles(sb, chromPeakFeature.SMILES, Set2dCoordinates); + } + else + { + EmptyMolBlock(sb); } + WriteChromPeakFeatureInfoAsSdf(sb, chromPeakFeature, spectrum); + sb.AppendLine("$$$$"); + sb.AppendLine(); + var bytes = Encoding.ASCII.GetBytes(sb.ToString()); + stream.Write(bytes, 0, bytes.Length); } - private static void WriteSdfDataItem(StreamWriter sw, string fieldName, string value) + private static void WriteSdfDataItem(StringBuilder sb, string fieldName, string value) { - sw.WriteLine("> <" + fieldName + ">"); - sw.WriteLine(value ?? string.Empty); - //sw.WriteLine(); + sb.AppendLine("> <" + fieldName + ">"); + sb.AppendLine(value ?? string.Empty); } - private static void EmptyMolBlock(StreamWriter sw) + private static void EmptyMolBlock(StringBuilder sb) { - sw.WriteLine(""); - sw.WriteLine(" MS-DIAL"); - sw.WriteLine(); - sw.WriteLine(" 0 0 0 0 0 0 999 V2000"); - sw.WriteLine("M END"); + sb.AppendLine(""); + sb.AppendLine(" MS-DIAL"); + sb.AppendLine(); + sb.AppendLine(" 0 0 0 0 0 0 999 V2000"); + sb.AppendLine("M END"); } - private static void MolBlockFromSmiles(StreamWriter sw, string smiles, bool Set2dCoordinates) + private static void MolBlockFromSmiles(StringBuilder sb, string smiles, bool Set2dCoordinates) { var sp = new SmilesParser(); IAtomContainer mol = sp.ParseSmiles(smiles); @@ -402,65 +402,66 @@ private static void MolBlockFromSmiles(StreamWriter sw, string smiles, bool Set2 sdg.GenerateCoordinates(); } mol = sdg.Molecule; - using var w = new MDLV2000Writer(sw); - w.Write(mol); + using var tw = new StringWriter(sb, CultureInfo.InvariantCulture); + using (var w = new MDLV2000Writer(tw)) + { + w.Write(mol); + }; } private static void WriteChromPeakFeatureInfoAsSdf( - StreamWriter sw, + StringBuilder sb, AlignmentSpotProperty spotProperty, IEnumerable spectrum) { - var sb = new StringBuilder(); - WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); - WriteSdfDataItem(sw, "SCANS", spotProperty.MasterAlignmentID.ToString()); - WriteSdfDataItem(sw, "PRECURSOR MZ", Math.Round(spotProperty.MassCenter,5).ToString()); - WriteSdfDataItem(sw, "ION MODE", spotProperty.IonMode.ToString()); + WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); + WriteSdfDataItem(sb, "SCANS", spotProperty.MasterAlignmentID.ToString()); + WriteSdfDataItem(sb, "PRECURSOR MZ", Math.Round(spotProperty.MassCenter,5).ToString()); + WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsAssigned) { - if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); - if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); - if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); - if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); - WriteSdfDataItem(sw, "MS LEVEL", "MS2"); + if (spotProperty.AdductType != null) WriteSdfDataItem(sb, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sb, "FORMULA", spotProperty.Formula.FormulaString); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "FORMULA", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "FORMULA", spotProperty.SMILES); + WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); - WriteSdfDataItem(sw, "NUM PEAKS", peaks.Count.ToString()); + WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( "\n", spectrum.Select(p => $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity,0)}" ) ); - WriteSdfDataItem(sw, "MASS SPECTRAL PEAKS", peaksText); + WriteSdfDataItem(sb, "MASS SPECTRAL PEAKS", peaksText); } } private static void WriteChromPeakFeatureInfoAsSdf( - StreamWriter sw, + StringBuilder sb, ChromatogramPeakFeature spotProperty, IEnumerable spectrum) { - var sb = new StringBuilder(); - WriteSdfDataItem(sw, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); - WriteSdfDataItem(sw, "SCANS", spotProperty.PeakID.ToString()); - WriteSdfDataItem(sw, "PRECURSOR MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); - WriteSdfDataItem(sw, "ION MODE", spotProperty.IonMode.ToString()); + WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); + WriteSdfDataItem(sb, "SCANS", spotProperty.PeakID.ToString()); + WriteSdfDataItem(sb, "PRECURSOR MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); + WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsContained) { - if (spotProperty.AdductType != null) WriteSdfDataItem(sw, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); - if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sw, "FORMULA", spotProperty.Formula.FormulaString); - if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sw, "FORMULA", spotProperty.InChIKey); - if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sw, "FORMULA", spotProperty.SMILES); - WriteSdfDataItem(sw, "MS LEVEL", "MS2"); + if (spotProperty.AdductType != null) WriteSdfDataItem(sb, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sb, "FORMULA", spotProperty.Formula.FormulaString); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "FORMULA", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "FORMULA", spotProperty.SMILES); + WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); - WriteSdfDataItem(sw, "NUM PEAKS", peaks.Count.ToString()); + WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( "\n", spectrum.Select(p => $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity, 0)}" ) ); - WriteSdfDataItem(sw, "MASS SPECTRAL PEAKS", peaksText); + WriteSdfDataItem(sb, "MASS SPECTRAL PEAKS", peaksText); } } #endregion From 925fbd2b6b77153ba9559105fadef1d6f8e347f1 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Mon, 2 Mar 2026 11:51:14 +0900 Subject: [PATCH 05/13] Fix Sdf export item --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 932e4ff49..47e4e5f46 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -415,15 +415,18 @@ private static void WriteChromPeakFeatureInfoAsSdf( { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); WriteSdfDataItem(sb, "SCANS", spotProperty.MasterAlignmentID.ToString()); - WriteSdfDataItem(sb, "PRECURSOR MZ", Math.Round(spotProperty.MassCenter,5).ToString()); + WriteSdfDataItem(sb, "PRECURSOR M/Z", Math.Round(spotProperty.MassCenter, 5).ToString()); WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsAssigned) { if (spotProperty.AdductType != null) WriteSdfDataItem(sb, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sb, "FORMULA", spotProperty.Formula.FormulaString); - if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "FORMULA", spotProperty.InChIKey); - if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "FORMULA", spotProperty.SMILES); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); + if (!string.IsNullOrWhiteSpace(spotProperty.TimesCenter.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", spotProperty.TimesCenter.RT.Value.ToString()); + if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); @@ -443,15 +446,18 @@ private static void WriteChromPeakFeatureInfoAsSdf( { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); WriteSdfDataItem(sb, "SCANS", spotProperty.PeakID.ToString()); - WriteSdfDataItem(sb, "PRECURSOR MZ", Math.Round(spotProperty.PrecursorMz, 5).ToString()); + WriteSdfDataItem(sb, "PRECURSOR M/Z", Math.Round(spotProperty.PrecursorMz, 5).ToString()); WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); if (spotProperty.IsMsmsContained) { if (spotProperty.AdductType != null) WriteSdfDataItem(sb, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sb, "FORMULA", spotProperty.Formula.FormulaString); - if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "FORMULA", spotProperty.InChIKey); - if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "FORMULA", spotProperty.SMILES); + if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); + if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); + if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); + if (!string.IsNullOrWhiteSpace(spotProperty.ChromXs.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", spotProperty.ChromXs.RT.Value.ToString()); + if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); From 882c54031e37f936a916a88443e1840dcdedbc0a Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Mon, 2 Mar 2026 15:20:16 +0900 Subject: [PATCH 06/13] change decimal --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 47e4e5f46..950439f35 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -425,7 +425,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); - if (!string.IsNullOrWhiteSpace(spotProperty.TimesCenter.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", spotProperty.TimesCenter.RT.Value.ToString()); + if (!string.IsNullOrWhiteSpace(spotProperty.TimesCenter.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); @@ -433,7 +433,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( var peaksText = string.Join( "\n", spectrum.Select(p => - $"{Math.Round(p.Mass, 5)} {Math.Round(p.Intensity,0)}" + $"{Math.Round(p.Mass, 4)} {Math.Round(p.Intensity,0)}" ) ); WriteSdfDataItem(sb, "MASS SPECTRAL PEAKS", peaksText); From 7b28f8e18beaa7a27fa3daa445b91eb28f64ae89 Mon Sep 17 00:00:00 2001 From: mikikot113 <122430993+mikikot113@users.noreply.github.com> Date: Tue, 10 Mar 2026 10:50:42 +0900 Subject: [PATCH 07/13] Update src/MSDIAL5/MsdialCore/Export/SpectraExport.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 950439f35..c7e6655a8 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -425,7 +425,8 @@ private static void WriteChromPeakFeatureInfoAsSdf( if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); - if (!string.IsNullOrWhiteSpace(spotProperty.TimesCenter.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); + if (spotProperty.TimesCenter != null && spotProperty.TimesCenter.RT != null && spotProperty.TimesCenter.RT.Value >= 0) + WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); WriteSdfDataItem(sb, "MS LEVEL", "MS2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); From 2e466b9499360dfc3e493f069da4a95219e32182 Mon Sep 17 00:00:00 2001 From: mikikot113 <122430993+mikikot113@users.noreply.github.com> Date: Tue, 10 Mar 2026 10:52:12 +0900 Subject: [PATCH 08/13] Update src/MSDIAL5/MsdialCore/Export/SpectraExport.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index c7e6655a8..c4d2f5587 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -422,7 +422,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( { if (spotProperty.AdductType != null) WriteSdfDataItem(sb, "PRECURSOR TYPE", spotProperty.AdductType.AdductIonName); if (!string.IsNullOrWhiteSpace(spotProperty.Formula.FormulaString)) WriteSdfDataItem(sb, "FORMULA", spotProperty.Formula.FormulaString); - if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); + if (spotProperty.Formula.Mass > 0d) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); if (spotProperty.TimesCenter != null && spotProperty.TimesCenter.RT != null && spotProperty.TimesCenter.RT.Value >= 0) From 85b375c17b45c09f033b23496c99794cac9e62f6 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Mon, 16 Mar 2026 14:07:24 +0900 Subject: [PATCH 09/13] fix SDF format --- .../MsdialCore/Export/AlignmentSdfExporter.cs | 13 +- .../MsdialCore/Export/AnalysisSdfExporter.cs | 6 +- .../MsdialCore/Export/SpectraExport.cs | 345 +++++++++++------- 3 files changed, 215 insertions(+), 149 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs index 2d01211fd..d90d60130 100644 --- a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs @@ -6,23 +6,12 @@ namespace CompMs.MsdialCore.Export; public sealed class AlignmentSdfExporter : IAlignmentSpectraExporter { - private readonly bool _exportNoMs2Molecule; - private readonly bool _set2dCoordinates; - public AlignmentSdfExporter(bool exportNoMs2Molecule, bool set2dCoordinates) - { - _exportNoMs2Molecule = exportNoMs2Molecule; - _set2dCoordinates = set2dCoordinates; - } - public AlignmentSdfExporter() : this(exportNoMs2Molecule: true, set2dCoordinates: true) { } - void IAlignmentSpectraExporter.Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult) { SpectraExport.SaveSpectraTableAsSdfFormat( stream, spot, - msdecResult.Spectrum, - _exportNoMs2Molecule, - _set2dCoordinates + msdecResult.Spectrum ); } } diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs index 53d8020c9..2704748e9 100644 --- a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -12,8 +12,6 @@ public sealed class AnalysisSdfExporter : IAnalysisExporter> loaderFuctory) { _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); } - private readonly bool _exportNoMs2Molecule = true; - private readonly bool _set2dCoordinates = true; void IAnalysisExporter.Export(Stream stream, AnalysisFileBean analysisFile, ChromatogramPeakFeatureCollection peakFeatureCollection, ExportStyle exportStyle) { var loader = _loaderFactory(analysisFile); @@ -21,9 +19,7 @@ void IAnalysisExporter.Export(Stream stream, SpectraExport.SaveSpectraTableAsSdfFormat( stream, peak, - loader.Load(peak).Spectrum, - _exportNoMs2Molecule, - _set2dCoordinates + loader.Load(peak).Spectrum ); } } diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index c4d2f5587..9b1e29ce6 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -25,14 +25,16 @@ namespace CompMs.MsdialCore.Export public static class SpectraExport { public static void SaveSpectraTable( - ExportSpectraFileFormat spectraFormat, + ExportSpectraFileFormat spectraFormat, Stream exportStream, - ChromatogramPeakFeature chromPeakFeature, + ChromatogramPeakFeature chromPeakFeature, IMSScanProperty scan, IReadOnlyList spectrumList, DataBaseMapper mapper, - ParameterBase parameter) { - switch (spectraFormat) { + ParameterBase parameter) + { + switch (spectraFormat) + { case ExportSpectraFileFormat.msp: SaveSpectraTableAsNistFormat(exportStream, chromPeakFeature, scan.Spectrum, mapper, parameter); break; @@ -40,7 +42,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, true, true); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -55,15 +57,16 @@ public static void SaveSpectraTable( } public static void SaveSpectraTable( - ExportSpectraFileFormat spectraFormat, + ExportSpectraFileFormat spectraFormat, Stream exportStream, - AlignmentSpotProperty spotProperty, + AlignmentSpotProperty spotProperty, IMSScanProperty scan, DataBaseMapper mapper, ParameterBase parameter, AlignmentSpotProperty isotopeTrackedLastSpot = null) { - switch (spectraFormat) { + switch (spectraFormat) + { case ExportSpectraFileFormat.msp: SaveSpectraTableAsNistFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter); break; @@ -71,7 +74,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, true, true); + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); @@ -84,7 +87,7 @@ public static void SaveSpectraTable( break; } } - + #region msp /// /// Saves spectral data along with chromatographic peak and molecular property information in NIST MSP format to a specified stream. @@ -105,11 +108,13 @@ public static void SaveSpectraTableAsNistFormat( IEnumerable massSpectra, IMatchResultRefer refer, ParameterBase parameter) - where T: IMoleculeProperty, IChromatogramPeak, IIonProperty, IAnnotatedObject { + where T : IMoleculeProperty, IChromatogramPeak, IIonProperty, IAnnotatedObject + { var builder = new NistRecordBuilder(); builder.SetNameProperty(chromPeakFeature.Name); builder.SetChromatogramPeakProperties(chromPeakFeature); - switch (chromPeakFeature) { + switch (chromPeakFeature) + { case ChromatogramPeakFeature peak: builder.SetComment(peak); break; @@ -129,8 +134,10 @@ public static void SaveSpectraTableAsNistFormat( ChromatogramPeakFeature chromPeakFeature, IEnumerable massSpectra, IMatchResultRefer refer, - ParameterBase parameter) { - using (var file = File.Open(exportFilePath, FileMode.Create)) { + ParameterBase parameter) + { + using (var file = File.Open(exportFilePath, FileMode.Create)) + { SaveSpectraTableAsNistFormat(file, chromPeakFeature, massSpectra, refer, parameter); } } @@ -140,7 +147,8 @@ public static void SaveSpectraTableAsNistFormat( ChromatogramPeakFeature chromPeakFeature, IEnumerable massSpectra, IMatchResultRefer refer, - ParameterBase parameter) { + ParameterBase parameter) + { var builder = new NistRecordBuilder(); builder.SetNameProperty(chromPeakFeature.Name); builder.SetChromatogramPeakFeatureProperties(chromPeakFeature, chromPeakFeature.MasterPeakID); @@ -158,9 +166,11 @@ public static void SaveSpectraTableAsNistFormat( AlignmentSpotProperty spotProperty, IEnumerable massSpectra, IMatchResultRefer refer, - ParameterBase parameter) { + ParameterBase parameter) + { - using (var file = File.Open(exportFilePath, FileMode.Create)) { + using (var file = File.Open(exportFilePath, FileMode.Create)) + { SaveSpectraTableAsNistFormat(file, spotProperty, massSpectra, refer, parameter); } } @@ -170,7 +180,8 @@ public static void SaveSpectraTableAsNistFormat( AlignmentSpotProperty spotProperty, IEnumerable massSpectra, IMatchResultRefer mapper, - ParameterBase parameter) { + ParameterBase parameter) + { var builder = new NistRecordBuilder(); builder.SetNameProperty(spotProperty.Name); builder.SetChromatogramPeakProperties(spotProperty); @@ -185,7 +196,8 @@ public static void SaveSpectraTableAsNistFormat( private static void WriteChromPeakFeatureInfoAsMSP( StreamWriter sw, ChromatogramPeakFeature feature, - IMatchResultRefer refer) { + IMatchResultRefer refer) + { sw.WriteLine("NAME: " + GetNameField(feature)); sw.WriteLine("PRECURSORMZ: " + feature.PrecursorMz); sw.WriteLine("PRECURSORTYPE: " + feature.AdductType.AdductIonName); @@ -200,7 +212,8 @@ private static void WriteChromPeakFeatureInfoAsMSP( private static void WriteChromPeakFeatureInfoAsMSP( StreamWriter sw, AlignmentSpotProperty feature, - IMatchResultRefer refer) { + IMatchResultRefer refer) + { sw.WriteLine("NAME: " + GetNameField(feature)); sw.WriteLine("PRECURSORMZ: " + feature.MassCenter); sw.WriteLine("PRECURSORTYPE: " + feature.AdductType.AdductIonName); @@ -216,7 +229,8 @@ private static void WriteChromPeakFeatureInfoAsMSP( StreamWriter sw, T feature, IMatchResultRefer refer) - where T: IMoleculeProperty, IChromatogramPeak, IIonProperty, IAnnotatedObject { + where T : IMoleculeProperty, IChromatogramPeak, IIonProperty, IAnnotatedObject + { sw.WriteLine("NAME: " + GetNameField(feature)); sw.WriteLine("PRECURSORMZ: " + feature.Mass); sw.WriteLine("PRECURSORTYPE: " + feature.AdductType.AdductIonName); @@ -231,12 +245,14 @@ private static void WriteChromPeakFeatureInfoAsMSP( private static void WriteChromXFieldAsMSP( StreamWriter sw, ChromXs chromXs, - double ccs) { + double ccs) + { if (chromXs.RT.Value > 0) sw.WriteLine("RETENTIONTIME: " + chromXs.RT.Value); if (chromXs.RI.Value > 0) sw.WriteLine("RETENTIONINDEX: " + chromXs.RI.Value); - if (chromXs.Drift.Value > 0) { + if (chromXs.Drift.Value > 0) + { sw.WriteLine("MOBILITY: " + chromXs.Drift.Value); sw.WriteLine("CCS: " + ccs); } @@ -247,8 +263,10 @@ private static void WriteChromXFieldAsMSP( public static void SaveSpectraTableAsMgfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, - IEnumerable massSpectra) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + IEnumerable massSpectra) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { sw.WriteLine("BEGIN IONS"); WriteChromPeakFeatureInfoAsMgf(sw, chromPeakFeature); WriteSpectrumPeakInfo(sw, massSpectra); @@ -257,8 +275,10 @@ public static void SaveSpectraTableAsMgfFormat( } } - public static void SaveSpectraTableAsMgfFormat(Stream stream, AlignmentSpotProperty spotProperty, IEnumerable spectrum, bool exportNumOfPeaks = true) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + public static void SaveSpectraTableAsMgfFormat(Stream stream, AlignmentSpotProperty spotProperty, IEnumerable spectrum, bool exportNumOfPeaks = true) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { sw.WriteLine("BEGIN IONS"); WriteChromPeakFeatureInfoAsMgf(sw, spotProperty); WriteSpectrumPeakInfo(sw, spectrum, exportNumOfPeaks); @@ -267,7 +287,8 @@ public static void SaveSpectraTableAsMgfFormat(Stream stream, AlignmentSpotPrope } } - public static void SavePeakTableAsMgfFormat(Stream stream, AlignmentSpotProperty spotProperty) { + public static void SavePeakTableAsMgfFormat(Stream stream, AlignmentSpotProperty spotProperty) + { using StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true); sw.WriteLine("BEGIN IONS"); WriteChromPeakFeatureInfoAsMgf(sw, spotProperty); @@ -275,7 +296,8 @@ public static void SavePeakTableAsMgfFormat(Stream stream, AlignmentSpotProperty sw.WriteLine(); } - public static void WriteChromPeakFeatureInfoAsMgf(StreamWriter sw, ChromatogramPeakFeature feature) { + public static void WriteChromPeakFeatureInfoAsMgf(StreamWriter sw, ChromatogramPeakFeature feature) + { var nameField = GetNameField(feature); var commentField = GetCommentField(feature); var chargeChar = feature.AdductType.IonMode == IonMode.Positive ? "+" : "-"; @@ -290,7 +312,8 @@ public static void WriteChromPeakFeatureInfoAsMgf(StreamWriter sw, ChromatogramP public static void WriteChromPeakFeatureInfoAsMgf( StreamWriter sw, - AlignmentSpotProperty feature) { + AlignmentSpotProperty feature) + { var nameField = GetNameField(feature); var commentField = GetCommentField(feature); var chargeChar = feature.AdductType.IonMode == IonMode.Positive ? "+" : "-"; @@ -308,12 +331,14 @@ public static void WriteChromPeakFeatureInfoAsMgf( private static void WriteChromXFieldAsMGF( StreamWriter sw, ChromXs chromXs, - double ccs) { + double ccs) + { if (chromXs.RT.Value > 0) sw.WriteLine("RTINMINUTES=" + chromXs.RT.Value); if (chromXs.RI.Value > 0) sw.WriteLine("RETENTIONINDEX=" + chromXs.RI.Value); - if (chromXs.Drift.Value > 0) { + if (chromXs.Drift.Value > 0) + { sw.WriteLine("DRIFTTIME=" + chromXs.Drift.Value); sw.WriteLine("CCS=" + ccs); } @@ -322,56 +347,53 @@ private static void WriteChromXFieldAsMGF( #region sdf public static void SaveSpectraTableAsSdfFormat( - Stream stream, - AlignmentSpotProperty spotProperty, - IEnumerable spectrum, - bool exportNoMs2Molecule, - bool Set2dCoordinates + Stream stream, + AlignmentSpotProperty spotProperty, + IEnumerable spectrum ) { - if (!exportNoMs2Molecule && !spotProperty.IsMsmsAssigned) + (bool exportNoMs2Molecule, string instrumentType) = (true, ""); + if (!exportNoMs2Molecule && (!spotProperty.IsMsmsAssigned || spotProperty.SMILES.IsEmptyOrNull())) { return; } var sb = new StringBuilder(8 * 1024); - if(spotProperty.IsMsmsAssigned) + if (spotProperty.IsMsmsAssigned) { - MolBlockFromSmiles(sb, spotProperty.SMILES, Set2dCoordinates); + MolBlockFromSmiles(sb, spotProperty.SMILES); } else { EmptyMolBlock(sb); } - WriteChromPeakFeatureInfoAsSdf(sb, spotProperty, spectrum); + WriteChromPeakFeatureInfoAsSdf(sb, spotProperty, spectrum, instrumentType); sb.AppendLine("$$$$"); - sb.AppendLine(); var bytes = Encoding.ASCII.GetBytes(sb.ToString()); stream.Write(bytes, 0, bytes.Length); } public static void SaveSpectraTableAsSdfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, - IEnumerable spectrum, - bool exportNoMs2Molecule, - bool Set2dCoordinates + IEnumerable spectrum ) { - if (!exportNoMs2Molecule && !chromPeakFeature.IsMsmsContained) + (bool exportNoMs2Molecule, string instrumentType) = (true,"LCMS"); + if (!exportNoMs2Molecule && (!chromPeakFeature.IsMsmsContained || chromPeakFeature.SMILES.IsEmptyOrNull())) { return; } var sb = new StringBuilder(8 * 1024); if (chromPeakFeature.IsMsmsContained) { - MolBlockFromSmiles(sb, chromPeakFeature.SMILES, Set2dCoordinates); + MolBlockFromSmiles(sb, chromPeakFeature.SMILES); } else { EmptyMolBlock(sb); } - WriteChromPeakFeatureInfoAsSdf(sb, chromPeakFeature, spectrum); + WriteChromPeakFeatureInfoAsSdf(sb, chromPeakFeature, spectrum, instrumentType); + sb.AppendLine(""); sb.AppendLine("$$$$"); - sb.AppendLine(); var bytes = Encoding.ASCII.GetBytes(sb.ToString()); stream.Write(bytes, 0, bytes.Length); } @@ -379,17 +401,18 @@ private static void WriteSdfDataItem(StringBuilder sb, string fieldName, string { sb.AppendLine("> <" + fieldName + ">"); sb.AppendLine(value ?? string.Empty); + sb.AppendLine(""); } private static void EmptyMolBlock(StringBuilder sb) { sb.AppendLine(""); sb.AppendLine(" MS-DIAL"); sb.AppendLine(); - sb.AppendLine(" 0 0 0 0 0 0 999 V2000"); + sb.AppendLine(" 0 0 0 0 0 0 0 0 0 0999 V2000"); sb.AppendLine("M END"); } - private static void MolBlockFromSmiles(StringBuilder sb, string smiles, bool Set2dCoordinates) + private static void MolBlockFromSmiles(StringBuilder sb, string smiles) { var sp = new SmilesParser(); IAtomContainer mol = sp.ParseSmiles(smiles); @@ -397,23 +420,23 @@ private static void MolBlockFromSmiles(StringBuilder sb, string smiles, bool Set { Molecule = mol }; - if(Set2dCoordinates) - { - sdg.GenerateCoordinates(); - } + sdg.GenerateCoordinates(); mol = sdg.Molecule; using var tw = new StringWriter(sb, CultureInfo.InvariantCulture); using (var w = new MDLV2000Writer(tw)) { w.Write(mol); - }; + } + ; } private static void WriteChromPeakFeatureInfoAsSdf( StringBuilder sb, - AlignmentSpotProperty spotProperty, - IEnumerable spectrum) + AlignmentSpotProperty spotProperty, + IEnumerable spectrum, + string instrumentType + ) { - WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name)? "Unknown": spotProperty.Name); + WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); WriteSdfDataItem(sb, "SCANS", spotProperty.MasterAlignmentID.ToString()); WriteSdfDataItem(sb, "PRECURSOR M/Z", Math.Round(spotProperty.MassCenter, 5).ToString()); WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); @@ -428,13 +451,14 @@ private static void WriteChromPeakFeatureInfoAsSdf( if (spotProperty.TimesCenter != null && spotProperty.TimesCenter.RT != null && spotProperty.TimesCenter.RT.Value >= 0) WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); - WriteSdfDataItem(sb, "MS LEVEL", "MS2"); + if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); + WriteSdfDataItem(sb, "SPECTRUM TYPE", "2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( "\n", spectrum.Select(p => - $"{Math.Round(p.Mass, 4)} {Math.Round(p.Intensity,0)}" + $"{Math.Round(p.Mass, 4)} {Math.Round(p.Intensity, 0)}" ) ); WriteSdfDataItem(sb, "MASS SPECTRAL PEAKS", peaksText); @@ -443,7 +467,8 @@ private static void WriteChromPeakFeatureInfoAsSdf( private static void WriteChromPeakFeatureInfoAsSdf( StringBuilder sb, ChromatogramPeakFeature spotProperty, - IEnumerable spectrum) + IEnumerable spectrum, + string instrumentType) { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); WriteSdfDataItem(sb, "SCANS", spotProperty.PeakID.ToString()); @@ -459,7 +484,8 @@ private static void WriteChromPeakFeatureInfoAsSdf( if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); if (!string.IsNullOrWhiteSpace(spotProperty.ChromXs.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", spotProperty.ChromXs.RT.Value.ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); - WriteSdfDataItem(sb, "MS LEVEL", "MS2"); + if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); + WriteSdfDataItem(sb, "SPECTRUM TYPE", "2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); var peaksText = string.Join( @@ -475,20 +501,24 @@ private static void WriteChromPeakFeatureInfoAsSdf( #region mat private static void SaveSpectraTableAsMatFormat( - Stream stream, - ChromatogramPeakFeature feature, + Stream stream, + ChromatogramPeakFeature feature, IEnumerable spectrum, IReadOnlyList spectrumList, - IMatchResultRefer refer, - ParameterBase parameter) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + IMatchResultRefer refer, + ParameterBase parameter) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { WriteChromPeakFeatureInfoAsMSP(sw, feature, refer); sw.WriteLine("IONMODE: " + feature.IonMode); WriteParameterInfoAsNist(sw, parameter); var ms1Spectrum = spectrumList.FirstOrDefault(spec => spec.OriginalIndex == feature.MS1RawSpectrumIdTop); - if (ms1Spectrum != null) { + if (ms1Spectrum != null) + { var isotopes = DataAccess.GetIsotopicPeaks(ms1Spectrum.Spectrum, (float)feature.PrecursorMz, parameter.CentroidMs1Tolerance, parameter.PeakPickBaseParam.MaxIsotopesDetectedInMs1Spectrum); - if (!isotopes.IsEmptyOrNull()) { + if (!isotopes.IsEmptyOrNull()) + { sw.WriteLine("MSTYPE: MS1"); WriteSpectrumPeakInfo(sw, isotopes); } @@ -502,19 +532,23 @@ private static void SaveSpectraTableAsMatFormat( public static void SaveSpectraTableAsMatFormat( Stream stream, AlignmentSpotProperty feature, - IEnumerable spectrum, + IEnumerable spectrum, IMatchResultRefer refer, ParameterBase parameter, - AlignmentSpotProperty isotopeTrackedLastSpot) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + AlignmentSpotProperty isotopeTrackedLastSpot) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { WriteChromPeakFeatureInfoAsMSP(sw, feature, refer); sw.WriteLine("IONMODE: " + feature.IonMode); - if (isotopeTrackedLastSpot != null) { + if (isotopeTrackedLastSpot != null) + { WriteIsotopeTrackingFeature(sw, feature, parameter, isotopeTrackedLastSpot); } WriteParameterInfoAsNist(sw, parameter); var isotopes = feature.IsotopicPeaks; - if (!isotopes.IsEmptyOrNull()) { + if (!isotopes.IsEmptyOrNull()) + { sw.WriteLine("MSTYPE: MS1"); WriteSpectrumPeakInfo(sw, isotopes); } @@ -524,15 +558,18 @@ public static void SaveSpectraTableAsMatFormat( } } - public static void SaveSpectraTableForGcmsAsMatFormat(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, IChromatogramPeakFeature peakFeature, ProjectBaseParameter projectParameter) { + public static void SaveSpectraTableForGcmsAsMatFormat(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, IChromatogramPeakFeature peakFeature, ProjectBaseParameter projectParameter) + { SaveSpectraTableForGcmsAsMatFormatCore(stream, scan, molecule, peakFeature.Mass, peakFeature.PeakHeightTop, projectParameter); } - public static void SaveSpectraTableForGcmsAsMatFormat(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, IChromatogramPeak peak, ProjectBaseParameter projectParameter) { + public static void SaveSpectraTableForGcmsAsMatFormat(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, IChromatogramPeak peak, ProjectBaseParameter projectParameter) + { SaveSpectraTableForGcmsAsMatFormatCore(stream, scan, molecule, peak.Mass, peak.Intensity, projectParameter); } - private static void SaveSpectraTableForGcmsAsMatFormatCore(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, double quantmass, double peakHeight, ProjectBaseParameter projectParameter) { + private static void SaveSpectraTableForGcmsAsMatFormatCore(Stream stream, IMSScanProperty scan, IMoleculeProperty molecule, double quantmass, double peakHeight, ProjectBaseParameter projectParameter) + { using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, bufferSize: 4096, leaveOpen: true)) { sw.Write("NAME: "); @@ -543,7 +580,8 @@ private static void SaveSpectraTableForGcmsAsMatFormatCore(Stream stream, IMSSca sw.WriteLine("QUANTMASS: " + quantmass); var precursorMz = quantmass; - if (scan.Spectrum.Count > 0) { + if (scan.Spectrum.Count > 0) + { precursorMz = scan.Spectrum.Max(s => s.Mass); } @@ -559,31 +597,38 @@ private static void SaveSpectraTableForGcmsAsMatFormatCore(Stream stream, IMSSca sw.WriteLine("SMILES: " + molecule.SMILES); sw.WriteLine("FORMULA: " + molecule.Formula); - if (projectParameter.FinalSavedDate != default) { + if (projectParameter.FinalSavedDate != default) + { sw.WriteLine("DATE: " + projectParameter.FinalSavedDate.Date); } - if (!string.IsNullOrEmpty(projectParameter.Authors)) { + if (!string.IsNullOrEmpty(projectParameter.Authors)) + { sw.WriteLine("AUTHORS: " + projectParameter.Authors); } - if (!string.IsNullOrEmpty(projectParameter.License)) { + if (!string.IsNullOrEmpty(projectParameter.License)) + { sw.WriteLine("LICENSE: " + projectParameter.License); } - if (!string.IsNullOrEmpty(projectParameter.CollisionEnergy)) { + if (!string.IsNullOrEmpty(projectParameter.CollisionEnergy)) + { sw.WriteLine("COLLISIONENERGY: " + projectParameter.CollisionEnergy); } - if (!string.IsNullOrEmpty(projectParameter.InstrumentType)) { + if (!string.IsNullOrEmpty(projectParameter.InstrumentType)) + { sw.WriteLine("INSTRUMENTTYPE: " + projectParameter.InstrumentType); } - if (!string.IsNullOrEmpty(projectParameter.Instrument)) { + if (!string.IsNullOrEmpty(projectParameter.Instrument)) + { sw.WriteLine("INSTRUMENT: " + projectParameter.Instrument); } - if (!string.IsNullOrEmpty(projectParameter.Comment)) { + if (!string.IsNullOrEmpty(projectParameter.Comment)) + { sw.WriteLine("COMMENT: " + projectParameter.Comment); } @@ -601,15 +646,17 @@ private static void SaveSpectraTableForGcmsAsMatFormatCore(Stream stream, IMSSca } } private static void WriteIsotopeTrackingFeature( - StreamWriter sw, - AlignmentSpotProperty feature, - ParameterBase parameter, - AlignmentSpotProperty lastFeature) { + StreamWriter sw, + AlignmentSpotProperty feature, + ParameterBase parameter, + AlignmentSpotProperty lastFeature) + { var isotopeLabel = parameter.IsotopeTrackingDictionary; var labelType = isotopeLabel.IsotopeElements[isotopeLabel.SelectedID].ElementName; var isotopeTrackNum = lastFeature.PeakCharacter.IsotopeWeightNumber; sw.WriteLine("#Specific field for labeled experiment"); - switch (labelType) { + switch (labelType) + { case "13C": sw.WriteLine("CarbonCount: " + isotopeTrackNum); break; @@ -641,20 +688,24 @@ private static void WriteIsotopeTrackingFeature( #region sirius ms private static void SaveSpectraTableAsSiriusMsFormat( - Stream stream, + Stream stream, ChromatogramPeakFeature feature, - IEnumerable spectrum, - IReadOnlyList spectrumList, - DataBaseMapper mapper, - ParameterBase parameter) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + IEnumerable spectrum, + IReadOnlyList spectrumList, + DataBaseMapper mapper, + ParameterBase parameter) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { WriteChromPeakFeatureInfoAsSiriusMs(sw, feature, mapper); sw.WriteLine(); var ms1Spectrum = spectrumList.FirstOrDefault(spec => spec.OriginalIndex == feature.MS1RawSpectrumIdTop); - if (ms1Spectrum != null) { + if (ms1Spectrum != null) + { var isotopes = DataAccess.GetIsotopicPeaks(ms1Spectrum.Spectrum, (float)feature.PrecursorMz, parameter.CentroidMs1Tolerance, parameter.PeakPickBaseParam.MaxIsotopesDetectedInMs1Spectrum); - if (!isotopes.IsEmptyOrNull()) { + if (!isotopes.IsEmptyOrNull()) + { sw.WriteLine(">ms1"); WriteSpectrumPeakInfo(sw, isotopes); } @@ -671,16 +722,19 @@ private static void SaveSpectraTableAsSiriusMsFormat( AlignmentSpotProperty feature, IEnumerable spectrum, DataBaseMapper mapper, - ParameterBase parameter) { - using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) { + ParameterBase parameter) + { + using (StreamWriter sw = new StreamWriter(stream, Encoding.ASCII, 4096, true)) + { WriteChromPeakFeatureInfoAsSiriusMs(sw, feature, mapper); sw.WriteLine(); var isotopes = feature.IsotopicPeaks; - if (!isotopes.IsEmptyOrNull()) { + if (!isotopes.IsEmptyOrNull()) + { sw.WriteLine(">ms1"); WriteSpectrumPeakInfo(sw, isotopes); } - + sw.WriteLine(); sw.WriteLine(">ms2"); WriteSpectrumPeakInfo(sw, spectrum); @@ -689,9 +743,10 @@ private static void SaveSpectraTableAsSiriusMsFormat( } private static void WriteChromPeakFeatureInfoAsSiriusMs( - StreamWriter sw, - ChromatogramPeakFeature feature, - DataBaseMapper mapper) { + StreamWriter sw, + ChromatogramPeakFeature feature, + DataBaseMapper mapper) + { sw.WriteLine(">compound " + GetNameField(feature)); sw.WriteLine(">parentmass " + feature.PrecursorMz); sw.WriteLine(">ionization " + feature.AdductType.AdductIonName); @@ -701,7 +756,8 @@ private static void WriteChromPeakFeatureInfoAsSiriusMs( private static void WriteChromPeakFeatureInfoAsSiriusMs( StreamWriter sw, AlignmentSpotProperty feature, - DataBaseMapper mapper) { + DataBaseMapper mapper) + { sw.WriteLine(">compound " + GetNameField(feature)); sw.WriteLine(">parentmass " + feature.MassCenter); sw.WriteLine(">ionization " + feature.AdductType.AdductIonName); @@ -709,7 +765,8 @@ private static void WriteChromPeakFeatureInfoAsSiriusMs( } #endregion - private static string GetCommentField(ChromatogramPeakFeature feature) { + private static string GetCommentField(ChromatogramPeakFeature feature) + { var comment = feature.Comment; var id = "|PEAKID=" + feature.MasterPeakID.ToString(); var ms1 = "|MS1SCAN=" + feature.MS1RawSpectrumIdTop; @@ -720,25 +777,31 @@ private static string GetCommentField(ChromatogramPeakFeature feature) { return comment + id + ms1 + ms2 + height + area + isotope; } - private static string GetCommentField(AlignmentSpotProperty feature) { + private static string GetCommentField(AlignmentSpotProperty feature) + { var comment = feature.Comment; var id = "|PEAKID=" + feature.MasterAlignmentID.ToString(); var isotope = "|ISOTOPE=" + "M+" + feature.PeakCharacter.IsotopeWeightNumber.ToString(); return comment + id + isotope; } - private static string GetCommentField(IChromatogramPeak feature) { - if (feature is ChromatogramPeakFeature chromatogramPeakFeature) { + private static string GetCommentField(IChromatogramPeak feature) + { + if (feature is ChromatogramPeakFeature chromatogramPeakFeature) + { return GetCommentField(chromatogramPeakFeature); } - if (feature is AlignmentSpotProperty alignmentSpotProperty) { + if (feature is AlignmentSpotProperty alignmentSpotProperty) + { return GetCommentField(alignmentSpotProperty); } return $"PEAKID={feature.ID}|PEAKHEIGHT={Math.Round(feature.Intensity, 0)}"; } - private static string GetNameField(ChromatogramPeakFeature feature) { - if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") { + private static string GetNameField(ChromatogramPeakFeature feature) + { + if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") + { var id = "|ID=" + feature.MasterPeakID.ToString(); var rt = feature.PeakFeature.ChromXsTop.RT.Value > 0 ? "|RT=" + Math.Round(feature.PeakFeature.ChromXsTop.RT.Value, 3) : string.Empty; var ri = feature.PeakFeature.ChromXsTop.RI.Value > 0 ? "|RI=" + Math.Round(feature.PeakFeature.ChromXsTop.RI.Value, 3) : string.Empty; @@ -746,13 +809,16 @@ private static string GetNameField(ChromatogramPeakFeature feature) { var mz = "|MZ=" + Math.Round(feature.PrecursorMz, 4).ToString(); return "Unknown" + id + mz + rt + ri + dt; } - else { + else + { return feature.Name; } } - private static string GetNameField(AlignmentSpotProperty feature) { - if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") { + private static string GetNameField(AlignmentSpotProperty feature) + { + if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") + { var id = "|ID=" + feature.MasterAlignmentID; var rt = feature.TimesCenter.RT.Value > 0 ? "|RT=" + Math.Round(feature.TimesCenter.RT.Value, 3) : string.Empty; var ri = feature.TimesCenter.RI.Value > 0 ? "|RI=" + Math.Round(feature.TimesCenter.RI.Value, 3) : string.Empty; @@ -760,13 +826,16 @@ private static string GetNameField(AlignmentSpotProperty feature) { var mz = "|MZ=" + Math.Round(feature.MassCenter, 4).ToString(); return "Unknown" + id + mz + rt + ri + dt; } - else { + else + { return feature.Name; } } - private static string GetNameField(T feature) where T : IMoleculeProperty, IChromatogramPeak { - if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") { + private static string GetNameField(T feature) where T : IMoleculeProperty, IChromatogramPeak + { + if (feature.Name.IsEmptyOrNull() || feature.Name.ToLower() == "unknown") + { var id = "|ID=" + feature.ID; var rt = feature.ChromXs.RT.Value > 0 ? "|RT=" + Math.Round(feature.ChromXs.RT.Value, 3) : string.Empty; var ri = feature.ChromXs.RI.Value > 0 ? "|RI=" + Math.Round(feature.ChromXs.RI.Value, 3) : string.Empty; @@ -774,45 +843,54 @@ private static string GetNameField(T feature) where T : IMoleculeProperty, IC var mz = Math.Round(feature.Mass, 4).ToString(); return "Unknown" + id + mz + rt + ri + dt; } - else { + else + { return feature.Name; } } private static void WriteParameterInfoAsNist(StreamWriter sw, ParameterBase parameter) { - if (!string.IsNullOrEmpty(parameter.Authors)) { + if (!string.IsNullOrEmpty(parameter.Authors)) + { sw.WriteLine("AUTHORS: " + parameter.Authors); } - if (!string.IsNullOrEmpty(parameter.License)) { + if (!string.IsNullOrEmpty(parameter.License)) + { sw.WriteLine("LICENSE: " + parameter.License); } - if (!string.IsNullOrEmpty(parameter.CollisionEnergy)) { + if (!string.IsNullOrEmpty(parameter.CollisionEnergy)) + { sw.WriteLine("COLLISIONENERGY: " + parameter.CollisionEnergy); } - if (!string.IsNullOrEmpty(parameter.InstrumentType)) { + if (!string.IsNullOrEmpty(parameter.InstrumentType)) + { sw.WriteLine("INSTRUMENTTYPE: " + parameter.InstrumentType); } - if (!string.IsNullOrEmpty(parameter.Instrument)) { + if (!string.IsNullOrEmpty(parameter.Instrument)) + { sw.WriteLine("INSTRUMENT: " + parameter.Instrument); } - if (!string.IsNullOrEmpty(parameter.Comment)) { + if (!string.IsNullOrEmpty(parameter.Comment)) + { sw.WriteLine("PARAMETERCOMMENT: " + parameter.Comment); } } private static void WriteSpectrumPeakInfo(StreamWriter sw, IEnumerable massSpectra, bool exportNumOfPeaks = true) { - if (massSpectra is null) { + if (massSpectra is null) + { return; } var peaks = massSpectra.Where(spec => spec.Intensity > 0).ToList(); - if (exportNumOfPeaks) { + if (exportNumOfPeaks) + { sw.WriteLine("Num Peaks: " + peaks.Count); } foreach (var peak in peaks) @@ -821,11 +899,14 @@ private static void WriteSpectrumPeakInfo(StreamWriter sw, IEnumerable isotopes) { - if (!isotopes.IsEmptyOrNull()) { + private static void WriteSpectrumPeakInfo(StreamWriter sw, IEnumerable isotopes) + { + if (!isotopes.IsEmptyOrNull()) + { var peaks = isotopes.Where(spec => spec.AbsoluteAbundance > 0).ToList(); sw.WriteLine("Num Peaks: " + peaks.Count); - foreach (var peak in peaks) { + foreach (var peak in peaks) + { sw.WriteLine(Math.Round(peak.Mass, 5) + "\t" + Math.Round(peak.AbsoluteAbundance, 0)); } } From 5a9481b28459911baadc0f5966659b90a84c0770 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Tue, 17 Mar 2026 15:06:46 +0900 Subject: [PATCH 10/13] set Instrument type from ParameterBase --- .../MsdialCore/Export/AlignmentSdfExporter.cs | 21 +++++++--- .../MsdialCore/Export/AnalysisSdfExporter.cs | 11 ++++-- .../MsdialCore/Export/SpectraExport.cs | 39 ++++++++++++------- .../Model/Dims/DimsMethodModel.cs | 2 +- .../Model/Gcms/GcmsMethodModel.cs | 2 +- .../ImagingImms/ImagingImmsMethodModel.cs | 4 +- .../Model/Imms/ImmsMethodModel.cs | 6 +-- .../Model/Lcimms/LcimmsMethodModel.cs | 6 +-- .../Model/Lcms/LcmsMethodModel.cs | 6 +-- .../ViewModel/Dims/DimsMethodViewModel.cs | 4 +- 10 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs index d90d60130..4eb19278c 100644 --- a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs @@ -1,17 +1,26 @@ using CompMs.MsdialCore.DataObj; using CompMs.MsdialCore.MSDec; +using CompMs.MsdialCore.Parameter; +using System; using System.IO; namespace CompMs.MsdialCore.Export; public sealed class AlignmentSdfExporter : IAlignmentSpectraExporter { - void IAlignmentSpectraExporter.Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult) + private readonly ParameterBase _parameter; + private bool _exportNoStructurePeak; + public AlignmentSdfExporter(bool exportNoStructurePeak, ParameterBase parameter) { - SpectraExport.SaveSpectraTableAsSdfFormat( - stream, - spot, - msdecResult.Spectrum - ); + _exportNoStructurePeak = exportNoStructurePeak; + _parameter = parameter ?? throw new ArgumentNullException(nameof(parameter)); + } + public void Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult) + { + Export(stream, spot, msdecResult, _exportNoStructurePeak, _parameter); + } + public void Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult, bool exportNoStructurePeak, ParameterBase parameter) + { + SpectraExport.SaveSpectraTableAsSdfFormat(stream, spot, msdecResult.Spectrum, exportNoStructurePeak, parameter); } } diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs index 2704748e9..7d34651d0 100644 --- a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -1,4 +1,5 @@ using CompMs.MsdialCore.DataObj; +using CompMs.MsdialCore.Parameter; using CompMs.MsdialCore.Parser; using System; using System.IO; @@ -8,9 +9,11 @@ namespace CompMs.MsdialCore.Export public sealed class AnalysisSdfExporter : IAnalysisExporter { private readonly Func> _loaderFactory; - - public AnalysisSdfExporter(Func> loaderFuctory) { + private readonly ParameterBase _parameter; + private bool _exportNoStructurePeak; + public AnalysisSdfExporter(Func> loaderFuctory,ParameterBase parameter) { _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); + _parameter = parameter ?? throw new ArgumentNullException(nameof(parameter)); } void IAnalysisExporter.Export(Stream stream, AnalysisFileBean analysisFile, ChromatogramPeakFeatureCollection peakFeatureCollection, ExportStyle exportStyle) { @@ -19,7 +22,9 @@ void IAnalysisExporter.Export(Stream stream, SpectraExport.SaveSpectraTableAsSdfFormat( stream, peak, - loader.Load(peak).Spectrum + loader.Load(peak).Spectrum, + _exportNoStructurePeak, + _parameter ); } } diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 9b1e29ce6..ba7a4a97e 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -42,7 +42,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, exportNoStructurePeak: false, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -74,7 +74,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum); + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, exportNoStructurePeak: false, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); @@ -349,11 +349,13 @@ private static void WriteChromXFieldAsMGF( public static void SaveSpectraTableAsSdfFormat( Stream stream, AlignmentSpotProperty spotProperty, - IEnumerable spectrum + IEnumerable spectrum, + bool exportNoStructurePeak, + ParameterBase parameter ) { - (bool exportNoMs2Molecule, string instrumentType) = (true, ""); - if (!exportNoMs2Molecule && (!spotProperty.IsMsmsAssigned || spotProperty.SMILES.IsEmptyOrNull())) + var instrumentType = parameter.ProjectParam.InstrumentType; + if (!exportNoStructurePeak && (!spotProperty.IsMsmsAssigned || spotProperty.SMILES.IsEmptyOrNull())) { return; } @@ -374,11 +376,13 @@ IEnumerable spectrum public static void SaveSpectraTableAsSdfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, - IEnumerable spectrum + IEnumerable spectrum, + bool exportNoStructurePeak, + ParameterBase parameter ) { - (bool exportNoMs2Molecule, string instrumentType) = (true,"LCMS"); - if (!exportNoMs2Molecule && (!chromPeakFeature.IsMsmsContained || chromPeakFeature.SMILES.IsEmptyOrNull())) + var instrumentType = parameter.ProjectParam.InstrumentType; + if (!exportNoStructurePeak && (!chromPeakFeature.IsMsmsContained || chromPeakFeature.SMILES.IsEmptyOrNull())) { return; } @@ -437,9 +441,14 @@ string instrumentType ) { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); - WriteSdfDataItem(sb, "SCANS", spotProperty.MasterAlignmentID.ToString()); + WriteSdfDataItem(sb, "ALIGNMENT ID", spotProperty.MasterAlignmentID.ToString()); WriteSdfDataItem(sb, "PRECURSOR M/Z", Math.Round(spotProperty.MassCenter, 5).ToString()); WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); + if (spotProperty.TimesCenter != null && spotProperty.TimesCenter.RT != null && spotProperty.TimesCenter.RT.Value >= 0) + { + WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); + } + if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); if (spotProperty.IsMsmsAssigned) { @@ -448,10 +457,7 @@ string instrumentType if (spotProperty.Formula.Mass > 0d) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); - if (spotProperty.TimesCenter != null && spotProperty.TimesCenter.RT != null && spotProperty.TimesCenter.RT.Value >= 0) - WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); - if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); WriteSdfDataItem(sb, "SPECTRUM TYPE", "2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); @@ -471,9 +477,14 @@ private static void WriteChromPeakFeatureInfoAsSdf( string instrumentType) { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); - WriteSdfDataItem(sb, "SCANS", spotProperty.PeakID.ToString()); + WriteSdfDataItem(sb, "PEAK ID", spotProperty.PeakID.ToString()); WriteSdfDataItem(sb, "PRECURSOR M/Z", Math.Round(spotProperty.PrecursorMz, 5).ToString()); WriteSdfDataItem(sb, "ION MODE", spotProperty.IonMode.ToString()); + if (spotProperty.ChromXs != null && spotProperty.ChromXs.RT != null && spotProperty.ChromXs.RT.Value >= 0) + { + WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.ChromXs.RT.Value, 3).ToString()); + } + if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); if (spotProperty.IsMsmsContained) { @@ -482,9 +493,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( if (!string.IsNullOrWhiteSpace(spotProperty.Formula.Mass.ToString())) WriteSdfDataItem(sb, "EXACT MASS", Math.Round(spotProperty.Formula.Mass, 5).ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.InChIKey)) WriteSdfDataItem(sb, "INCHIKEY", spotProperty.InChIKey); if (!string.IsNullOrWhiteSpace(spotProperty.SMILES)) WriteSdfDataItem(sb, "SMILES", spotProperty.SMILES); - if (!string.IsNullOrWhiteSpace(spotProperty.ChromXs.RT.Value.ToString())) WriteSdfDataItem(sb, "RETENTION TIME", spotProperty.ChromXs.RT.Value.ToString()); if (!string.IsNullOrWhiteSpace(spotProperty.Ontology)) WriteSdfDataItem(sb, "ONTOLOGY", spotProperty.Ontology); - if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); WriteSdfDataItem(sb, "SPECTRUM TYPE", "2"); var peaks = spectrum.Where(spec => spec.Intensity > 0).ToList(); WriteSdfDataItem(sb, "NUM PEAKS", peaks.Count.ToString()); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs index 95b05a1dd..889f8634e 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs @@ -99,7 +99,7 @@ public DimsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs index 382df99ff..3f27b6704 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs @@ -112,7 +112,7 @@ public GcmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var exportGroups = new List { peakGroup, spectraGroup, gnps, }; diff --git a/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs index 6ddff5703..3497495ea 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/ImagingImms/ImagingImmsMethodModel.cs @@ -152,8 +152,8 @@ public AnalysisResultExportModel CreateExportAnalysisModel() { Label = "MASCOT format (*.mgf)" }, new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { - [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), - [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_storage.Parameter.ProviderFactoryParameter.Create().Create(file.LoadRawMeasurement(true, true, 5, 5000)), _storage.Parameter.MS2DataType)), + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList),_storage.Parameter), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_storage.Parameter.ProviderFactoryParameter.Create().Create(file.LoadRawMeasurement(true, true, 5, 5000)), _storage.Parameter.MS2DataType),_storage.Parameter), }) { FilePrefix = "Sdf", diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs index 844ae0aad..bef0bdee8 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs @@ -104,7 +104,7 @@ public ImmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); @@ -319,8 +319,8 @@ public AnalysisResultExportModel CreateExportAnalysisResult() { Label = "MASCOT format (*.mgf)" }, new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { - [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), - [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(ProviderFactory.Create(file), _storage.Parameter.MS2DataType)), + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList),_storage.Parameter), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(ProviderFactory.Create(file), _storage.Parameter.MS2DataType),_storage.Parameter), }) { FilePrefix = "Sdf", diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs index 6744f814f..e5016a5d9 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs @@ -101,7 +101,7 @@ public LcimmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelCo peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); @@ -322,8 +322,8 @@ static RawMeasurement map(AnalysisFileBean file) { Label = "MASCOT format (*.mgf)" }, new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { - [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), - [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(factory.Create(file), Storage.Parameter.MS2DataType)), + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList),Storage.Parameter), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(factory.Create(file), Storage.Parameter.MS2DataType),Storage.Parameter), }) { FilePrefix = "Sdf", diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs index 5393dcadc..3a176a228 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs @@ -111,7 +111,7 @@ public LcmsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter()), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var massBank = new AlignmentResultMassBankRecordExportModel(peakSpotSupplyer, storage.Parameter.ProjectParam, studyContext); @@ -415,8 +415,8 @@ public AnalysisResultExportModel ExportAnalysis() { Label = "MASCOT format (*.mgf)" }, new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { - [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), - [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_providerFactory.Create(file), _storage.Parameter.MS2DataType)), + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList),_storage.Parameter), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_providerFactory.Create(file), _storage.Parameter.MS2DataType),_storage.Parameter), }) { FilePrefix = "Sdf", diff --git a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs index dffaa7a34..b2d4fef6d 100644 --- a/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/ViewModel/Dims/DimsMethodViewModel.cs @@ -120,8 +120,8 @@ private void ExportAnalysis() { Label = "MASCOT format (*.mgf)" }, new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary> { - [ExportspectraType.deconvoluted] = new AnalysisMgfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList)), - [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_model.ProviderFactory.Create(file), container.Parameter.MS2DataType)) + [ExportspectraType.deconvoluted] = new AnalysisSdfExporter(file => new MSDecLoader(file.DeconvolutionFilePath, file.DeconvolutionFilePathList),container.Parameter), + [ExportspectraType.centroid] = new AnalysisSdfExporter(file => new CentroidMsScanPropertyLoader(_model.ProviderFactory.Create(file), container.Parameter.MS2DataType),container.Parameter) }) { FilePrefix = "Sdf", From 218506d7afbfbc6ad5c99a6b8b22f443113cbbee Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Tue, 17 Mar 2026 15:29:42 +0900 Subject: [PATCH 11/13] fix exportNoStructurePeak-> true --- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index ba7a4a97e..9997d136f 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -42,7 +42,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, exportNoStructurePeak: false, parameter); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, exportNoStructurePeak: true, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -74,7 +74,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, exportNoStructurePeak: false, parameter); + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, exportNoStructurePeak: true, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); From 6575c3172a04d8dec8f0fad91bc42acc2fadb5e0 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Tue, 17 Mar 2026 17:34:58 +0900 Subject: [PATCH 12/13] change CRLF -> LF add COLLISION ENERGY and INSTRUMENT export --- .../MsdialCore/Export/AnalysisSdfExporter.cs | 2 +- .../MsdialCore/Export/SpectraExport.cs | 28 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs index 7d34651d0..bcea364da 100644 --- a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -10,7 +10,7 @@ public sealed class AnalysisSdfExporter : IAnalysisExporter> _loaderFactory; private readonly ParameterBase _parameter; - private bool _exportNoStructurePeak; + private bool _exportNoStructurePeak = true; public AnalysisSdfExporter(Func> loaderFuctory,ParameterBase parameter) { _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); _parameter = parameter ?? throw new ArgumentNullException(nameof(parameter)); diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 9997d136f..69a30b2d9 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -354,7 +354,6 @@ public static void SaveSpectraTableAsSdfFormat( ParameterBase parameter ) { - var instrumentType = parameter.ProjectParam.InstrumentType; if (!exportNoStructurePeak && (!spotProperty.IsMsmsAssigned || spotProperty.SMILES.IsEmptyOrNull())) { return; @@ -368,9 +367,9 @@ ParameterBase parameter { EmptyMolBlock(sb); } - WriteChromPeakFeatureInfoAsSdf(sb, spotProperty, spectrum, instrumentType); + WriteChromPeakFeatureInfoAsSdf(sb, spotProperty, spectrum, parameter); sb.AppendLine("$$$$"); - var bytes = Encoding.ASCII.GetBytes(sb.ToString()); + var bytes = Encoding.ASCII.GetBytes(sb.ToString().Replace("\r\n", "\n")); stream.Write(bytes, 0, bytes.Length); } public static void SaveSpectraTableAsSdfFormat( @@ -381,7 +380,6 @@ public static void SaveSpectraTableAsSdfFormat( ParameterBase parameter ) { - var instrumentType = parameter.ProjectParam.InstrumentType; if (!exportNoStructurePeak && (!chromPeakFeature.IsMsmsContained || chromPeakFeature.SMILES.IsEmptyOrNull())) { return; @@ -395,21 +393,21 @@ ParameterBase parameter { EmptyMolBlock(sb); } - WriteChromPeakFeatureInfoAsSdf(sb, chromPeakFeature, spectrum, instrumentType); - sb.AppendLine(""); + WriteChromPeakFeatureInfoAsSdf(sb, chromPeakFeature, spectrum, parameter); + sb.AppendLine(); sb.AppendLine("$$$$"); - var bytes = Encoding.ASCII.GetBytes(sb.ToString()); + var bytes = Encoding.ASCII.GetBytes(sb.ToString().Replace("\r\n", "\n")); stream.Write(bytes, 0, bytes.Length); } private static void WriteSdfDataItem(StringBuilder sb, string fieldName, string value) { sb.AppendLine("> <" + fieldName + ">"); sb.AppendLine(value ?? string.Empty); - sb.AppendLine(""); + sb.AppendLine(); } private static void EmptyMolBlock(StringBuilder sb) { - sb.AppendLine(""); + sb.AppendLine(); sb.AppendLine(" MS-DIAL"); sb.AppendLine(); sb.AppendLine(" 0 0 0 0 0 0 0 0 0 0999 V2000"); @@ -437,7 +435,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( StringBuilder sb, AlignmentSpotProperty spotProperty, IEnumerable spectrum, - string instrumentType + ParameterBase parameter ) { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); @@ -448,7 +446,9 @@ string instrumentType { WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.TimesCenter.RT.Value, 3).ToString()); } - if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); + if (!string.IsNullOrEmpty(parameter.InstrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", parameter.InstrumentType); + if (!string.IsNullOrEmpty(parameter.Instrument)) WriteSdfDataItem(sb, "INSTRUMENT", parameter.Instrument); + if (!string.IsNullOrEmpty(parameter.CollisionEnergy)) WriteSdfDataItem(sb, "COLLISION ENERGY", parameter.CollisionEnergy); if (spotProperty.IsMsmsAssigned) { @@ -474,7 +474,7 @@ private static void WriteChromPeakFeatureInfoAsSdf( StringBuilder sb, ChromatogramPeakFeature spotProperty, IEnumerable spectrum, - string instrumentType) + ParameterBase parameter) { WriteSdfDataItem(sb, "NAME", string.IsNullOrWhiteSpace(spotProperty.Name) ? "Unknown" : spotProperty.Name); WriteSdfDataItem(sb, "PEAK ID", spotProperty.PeakID.ToString()); @@ -484,7 +484,9 @@ private static void WriteChromPeakFeatureInfoAsSdf( { WriteSdfDataItem(sb, "RETENTION TIME", Math.Round(spotProperty.ChromXs.RT.Value, 3).ToString()); } - if (!string.IsNullOrEmpty(instrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", instrumentType); + if (!string.IsNullOrEmpty(parameter.InstrumentType)) WriteSdfDataItem(sb, "INSTRUMENT TYPE", parameter.InstrumentType); + if (!string.IsNullOrEmpty(parameter.Instrument)) WriteSdfDataItem(sb, "INSTRUMENT", parameter.Instrument); + if (!string.IsNullOrEmpty(parameter.CollisionEnergy)) WriteSdfDataItem(sb, "COLLISION ENERGY", parameter.CollisionEnergy); if (spotProperty.IsMsmsContained) { From 1dbf71a5bf0c9f53d671cd1b4a36845b60392972 Mon Sep 17 00:00:00 2001 From: "mikiko.takahashi" Date: Thu, 19 Mar 2026 09:57:46 +0900 Subject: [PATCH 13/13] Modified to not output peaks for MS1 only Changed argument names --- .../MsdialCore/Export/AlignmentSdfExporter.cs | 12 ++++++------ src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs | 4 ++-- src/MSDIAL5/MsdialCore/Export/SpectraExport.cs | 12 ++++++------ .../MsdialGuiApp/Model/Dims/DimsMethodModel.cs | 2 +- .../MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs | 2 +- .../MsdialGuiApp/Model/Imms/ImmsMethodModel.cs | 2 +- .../MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs | 2 +- .../MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs | 2 +- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs index 4eb19278c..f505b6437 100644 --- a/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs @@ -9,18 +9,18 @@ namespace CompMs.MsdialCore.Export; public sealed class AlignmentSdfExporter : IAlignmentSpectraExporter { private readonly ParameterBase _parameter; - private bool _exportNoStructurePeak; - public AlignmentSdfExporter(bool exportNoStructurePeak, ParameterBase parameter) + private bool _exportNoMs2Peak; + public AlignmentSdfExporter(bool exportNoMs2Peak, ParameterBase parameter) { - _exportNoStructurePeak = exportNoStructurePeak; + _exportNoMs2Peak = exportNoMs2Peak; _parameter = parameter ?? throw new ArgumentNullException(nameof(parameter)); } public void Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult) { - Export(stream, spot, msdecResult, _exportNoStructurePeak, _parameter); + Export(stream, spot, msdecResult, _exportNoMs2Peak, _parameter); } - public void Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult, bool exportNoStructurePeak, ParameterBase parameter) + public void Export(Stream stream, AlignmentSpotProperty spot, MSDecResult msdecResult, bool exportNoMs2Peak, ParameterBase parameter) { - SpectraExport.SaveSpectraTableAsSdfFormat(stream, spot, msdecResult.Spectrum, exportNoStructurePeak, parameter); + SpectraExport.SaveSpectraTableAsSdfFormat(stream, spot, msdecResult.Spectrum, exportNoMs2Peak, parameter); } } diff --git a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs index bcea364da..69ffaf5c3 100644 --- a/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs +++ b/src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs @@ -10,7 +10,7 @@ public sealed class AnalysisSdfExporter : IAnalysisExporter> _loaderFactory; private readonly ParameterBase _parameter; - private bool _exportNoStructurePeak = true; + private bool _exportNoMs2Peak = false; public AnalysisSdfExporter(Func> loaderFuctory,ParameterBase parameter) { _loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory)); _parameter = parameter ?? throw new ArgumentNullException(nameof(parameter)); @@ -23,7 +23,7 @@ void IAnalysisExporter.Export(Stream stream, stream, peak, loader.Load(peak).Spectrum, - _exportNoStructurePeak, + _exportNoMs2Peak, _parameter ); } diff --git a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs index 69a30b2d9..fbd8307b7 100644 --- a/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs +++ b/src/MSDIAL5/MsdialCore/Export/SpectraExport.cs @@ -42,7 +42,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, exportNoStructurePeak: true, parameter); + SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, exportNoMs2Peak: false, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter); @@ -74,7 +74,7 @@ public static void SaveSpectraTable( SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum); break; case ExportSpectraFileFormat.sdf: - SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, exportNoStructurePeak: true, parameter); + SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, exportNoMs2Peak: false, parameter); break; case ExportSpectraFileFormat.mat: SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot); @@ -350,11 +350,11 @@ public static void SaveSpectraTableAsSdfFormat( Stream stream, AlignmentSpotProperty spotProperty, IEnumerable spectrum, - bool exportNoStructurePeak, + bool exportNoMs2Peak, ParameterBase parameter ) { - if (!exportNoStructurePeak && (!spotProperty.IsMsmsAssigned || spotProperty.SMILES.IsEmptyOrNull())) + if (!exportNoMs2Peak && !spotProperty.IsMsmsAssigned) { return; } @@ -376,11 +376,11 @@ public static void SaveSpectraTableAsSdfFormat( Stream stream, ChromatogramPeakFeature chromPeakFeature, IEnumerable spectrum, - bool exportNoStructurePeak, + bool exportNoMs2Peak, ParameterBase parameter ) { - if (!exportNoStructurePeak && (!chromPeakFeature.IsMsmsContained || chromPeakFeature.SMILES.IsEmptyOrNull())) + if (!exportNoMs2Peak && !chromPeakFeature.IsMsmsContained) { return; } diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs index 889f8634e..7c3e9207c 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs @@ -99,7 +99,7 @@ public DimsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(false, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs index 3f27b6704..a5910e1e2 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs @@ -112,7 +112,7 @@ public GcmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(false, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var exportGroups = new List { peakGroup, spectraGroup, gnps, }; diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs index bef0bdee8..bc89964ac 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs @@ -104,7 +104,7 @@ public ImmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelColl peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(false, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs index e5016a5d9..e65b3fb42 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs @@ -101,7 +101,7 @@ public LcimmsMethodModel(AnalysisFileBeanModelCollection analysisFileBeanModelCo peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(false, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var spectraAndReference = new AlignmentMatchedSpectraExportModel(peakSpotSupplyer, storage.DataBaseMapper, analysisFileBeanModelCollection.IncludedAnalysisFiles, CompoundSearcherCollection.BuildSearchers(storage.DataBases, storage.DataBaseMapper)); diff --git a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs index 3a176a228..a3a0126ec 100644 --- a/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs +++ b/src/MSDIAL5/MsdialGuiApp/Model/Lcms/LcmsMethodModel.cs @@ -111,7 +111,7 @@ public LcmsMethodModel( peakSpotSupplyer, new AlignmentSpectraExportFormat("Msp", "msp", new AlignmentMspExporter(storage.DataBaseMapper, storage.Parameter)), new AlignmentSpectraExportFormat("Mgf", "mgf", new AlignmentMgfExporter()), - new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(true, storage.Parameter)), + new AlignmentSpectraExportFormat("Sdf", "sdf", new AlignmentSdfExporter(false, storage.Parameter)), new AlignmentSpectraExportFormat("Mat", "mat", new AlignmentMatExporter(storage.DataBaseMapper, storage.Parameter))); var gnps = new AlignmentGnpsExportModel("GNPS", quantTypes, new GnpsMetadataAccessor(storage.DataBaseMapper, storage.Parameter), peakMeta.GetAccessor(), fileMeta.GetAccessor(), analysisFileBeanModelCollection); var massBank = new AlignmentResultMassBankRecordExportModel(peakSpotSupplyer, storage.Parameter.ProjectParam, studyContext);