Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/CommonStandard/Enum/CommonEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions src/MSDIAL5/MsdialCore/Export/AlignmentSdfExporter.cs
Original file line number Diff line number Diff line change
@@ -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
);
}
}
31 changes: 31 additions & 0 deletions src/MSDIAL5/MsdialCore/Export/AnalysisSdfExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CompMs.MsdialCore.DataObj;
using CompMs.MsdialCore.Parser;
using System;
using System.IO;

namespace CompMs.MsdialCore.Export
{
public sealed class AnalysisSdfExporter : IAnalysisExporter<ChromatogramPeakFeatureCollection>
{
private readonly Func<AnalysisFileBean, IMsScanPropertyLoader<ChromatogramPeakFeature>> _loaderFactory;

public AnalysisSdfExporter(Func<AnalysisFileBean, IMsScanPropertyLoader<ChromatogramPeakFeature>> loaderFuctory) {
_loaderFactory = loaderFuctory ?? throw new ArgumentNullException(nameof(loaderFuctory));
}
private readonly bool _exportNoMs2Molecule = true;
private readonly bool _set2dCoordinates = true;

void IAnalysisExporter<ChromatogramPeakFeatureCollection>.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
);
}
}
}
}
160 changes: 159 additions & 1 deletion src/MSDIAL5/MsdialCore/Export/SpectraExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
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;
Expand All @@ -34,6 +39,9 @@ public static void SaveSpectraTable(
case ExportSpectraFileFormat.mgf:
SaveSpectraTableAsMgfFormat(exportStream, chromPeakFeature, scan.Spectrum);
break;
case ExportSpectraFileFormat.sdf:
SaveSpectraTableAsSdfFormat(exportStream, chromPeakFeature, scan.Spectrum, true, true);
break;
case ExportSpectraFileFormat.mat:
SaveSpectraTableAsMatFormat(exportStream, chromPeakFeature, scan.Spectrum, spectrumList, mapper, parameter);
break;
Expand All @@ -53,14 +61,18 @@ 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);
break;
case ExportSpectraFileFormat.mgf:
SaveSpectraTableAsMgfFormat(exportStream, spotProperty, scan.Spectrum);
break;
case ExportSpectraFileFormat.sdf:
SaveSpectraTableAsSdfFormat(exportStream, spotProperty, scan.Spectrum, true, true);
break;
case ExportSpectraFileFormat.mat:
SaveSpectraTableAsMatFormat(exportStream, spotProperty, scan.Spectrum, mapper, parameter, isotopeTrackedLastSpot);
break;
Expand Down Expand Up @@ -308,6 +320,152 @@ private static void WriteChromXFieldAsMGF(
}
#endregion

#region sdf
public static void SaveSpectraTableAsSdfFormat(
Stream stream,
AlignmentSpotProperty spotProperty,
IEnumerable<ISpectrumPeak> spectrum,
bool exportNoMs2Molecule,
bool Set2dCoordinates
)
{
if (!exportNoMs2Molecule && !spotProperty.IsMsmsAssigned)
{
return;
}
var sb = new StringBuilder(8 * 1024);
if(spotProperty.IsMsmsAssigned)
{
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<ISpectrumPeak> spectrum,
bool exportNoMs2Molecule,
bool Set2dCoordinates
)
{
if (!exportNoMs2Molecule && !chromPeakFeature.IsMsmsContained)
{
return;
}
var sb = new StringBuilder(8 * 1024);
if (chromPeakFeature.IsMsmsContained)
{
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(StringBuilder sb, string fieldName, string value)
{
sb.AppendLine("> <" + fieldName + ">");
sb.AppendLine(value ?? string.Empty);
}
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("M END");

}
private static void MolBlockFromSmiles(StringBuilder sb, 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 tw = new StringWriter(sb, CultureInfo.InvariantCulture);
using (var w = new MDLV2000Writer(tw))
{
w.Write(mol);
};
}
private static void WriteChromPeakFeatureInfoAsSdf(
StringBuilder sb,
AlignmentSpotProperty spotProperty,
IEnumerable<ISpectrumPeak> spectrum)
{
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(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(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(sb, "MASS SPECTRAL PEAKS", peaksText);
}
}
private static void WriteChromPeakFeatureInfoAsSdf(
StringBuilder sb,
ChromatogramPeakFeature spotProperty,
IEnumerable<ISpectrumPeak> spectrum)
{
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(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(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(sb, "MASS SPECTRAL PEAKS", peaksText);
}
}
#endregion

#region mat
private static void SaveSpectraTableAsMatFormat(
Stream stream,
Expand Down
1 change: 1 addition & 0 deletions src/MSDIAL5/MsdialGuiApp/Model/Dims/DimsMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/MSDIAL5/MsdialGuiApp/Model/Gcms/GcmsMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAlignmentResultExportModel> { peakGroup, spectraGroup, gnps, };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,6 +151,15 @@ public AnalysisResultExportModel CreateExportAnalysisModel() {
FileSuffix = "mgf",
Label = "MASCOT format (*.mgf)"
},
new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary<ExportspectraType, IAnalysisExporter<ChromatogramPeakFeatureCollection>> {
[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),
};

Expand Down
10 changes: 10 additions & 0 deletions src/MSDIAL5/MsdialGuiApp/Model/Imms/ImmsMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -317,6 +318,15 @@ public AnalysisResultExportModel CreateExportAnalysisResult() {
FileSuffix = "mgf",
Label = "MASCOT format (*.mgf)"
},
new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary<ExportspectraType, IAnalysisExporter<ChromatogramPeakFeatureCollection>> {
[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);
Expand Down
11 changes: 11 additions & 0 deletions src/MSDIAL5/MsdialGuiApp/Model/Lcimms/LcimmsMethodModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -319,6 +321,15 @@ static RawMeasurement map(AnalysisFileBean file) {
FileSuffix = "mgf",
Label = "MASCOT format (*.mgf)"
},
new SpectraTypeSelectableMsdialAnalysisExportModel(new Dictionary<ExportspectraType, IAnalysisExporter<ChromatogramPeakFeatureCollection>> {
[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);
Expand Down
Loading
Loading