|
|
""" |
|
|
MSZ Mass Spectrometry Dataset Loader |
|
|
|
|
|
This dataset contains compressed mass spectrometry data in MSZ format. |
|
|
Requires the 'mscompress' library for loading. |
|
|
|
|
|
Install: pip install mscompress |
|
|
""" |
|
|
|
|
|
import datasets |
|
|
from pathlib import Path |
|
|
from typing import List, Dict, Any |
|
|
|
|
|
|
|
|
_DESCRIPTION = """ |
|
|
Mass spectrometry dataset in compressed MSZ format. |
|
|
""" |
|
|
|
|
|
_HOMEPAGE = "" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
_DATA_FILES = ['test.msz'] |
|
|
|
|
|
_MS_LEVEL_FILTER = None |
|
|
|
|
|
_GRANULARITY = "spectrum" |
|
|
|
|
|
|
|
|
class MSZDatasetConfig(datasets.BuilderConfig): |
|
|
"""BuilderConfig for MSZ dataset.""" |
|
|
|
|
|
def __init__(self, **kwargs): |
|
|
super(MSZDatasetConfig, self).__init__(**kwargs) |
|
|
|
|
|
|
|
|
class MSZDataset(datasets.GeneratorBasedBuilder): |
|
|
"""MSZ mass spectrometry dataset.""" |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
|
MSZDatasetConfig( |
|
|
name="default", |
|
|
version=VERSION, |
|
|
description="Default configuration for MSZ dataset", |
|
|
), |
|
|
] |
|
|
|
|
|
DEFAULT_CONFIG_NAME = "default" |
|
|
|
|
|
def _info(self): |
|
|
if _GRANULARITY == "spectrum": |
|
|
features = datasets.Features({ |
|
|
"source_file": datasets.Value("string"), |
|
|
"spectrum_index": datasets.Value("int64"), |
|
|
"scan_number": datasets.Value("int64"), |
|
|
"ms_level": datasets.Value("int32"), |
|
|
"retention_time": datasets.Value("float64"), |
|
|
"num_peaks": datasets.Value("int64"), |
|
|
"mz": datasets.Sequence(datasets.Value("float64")), |
|
|
"intensity": datasets.Sequence(datasets.Value("float64")), |
|
|
}) |
|
|
else: |
|
|
features = datasets.Features({ |
|
|
"file_path": datasets.Value("string"), |
|
|
"file_name": datasets.Value("string"), |
|
|
"num_spectra": datasets.Value("int64"), |
|
|
"file_size_bytes": datasets.Value("int64"), |
|
|
"compression_format": datasets.Value("string"), |
|
|
}) |
|
|
|
|
|
return datasets.DatasetInfo( |
|
|
description=_DESCRIPTION, |
|
|
features=features, |
|
|
homepage=_HOMEPAGE, |
|
|
license=_LICENSE, |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
"""Download and extract MSZ files""" |
|
|
try: |
|
|
import mscompress |
|
|
except ImportError: |
|
|
raise ImportError( |
|
|
"The mscompress library is required to load this dataset. " |
|
|
"Install it with: pip install mscompress" |
|
|
) |
|
|
|
|
|
|
|
|
data_dir = dl_manager.download_and_extract({ |
|
|
"data": ["data/" + f for f in _DATA_FILES] |
|
|
}) |
|
|
|
|
|
return [ |
|
|
datasets.SplitGenerator( |
|
|
name=datasets.Split.TRAIN, |
|
|
gen_kwargs={ |
|
|
"msz_files": data_dir["data"], |
|
|
}, |
|
|
), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, msz_files: List[str]): |
|
|
"""Generate examples from MSZ files""" |
|
|
import mscompress |
|
|
|
|
|
idx = 0 |
|
|
|
|
|
if _GRANULARITY == "file": |
|
|
|
|
|
for file_path in msz_files: |
|
|
msz = mscompress.read(file_path) |
|
|
yield idx, { |
|
|
"file_path": file_path, |
|
|
"file_name": Path(file_path).name, |
|
|
"num_spectra": len(msz.spectra), |
|
|
"file_size_bytes": msz.size, |
|
|
"compression_format": msz.data_format.mz_original_compression, |
|
|
} |
|
|
idx += 1 |
|
|
else: |
|
|
|
|
|
for file_path in msz_files: |
|
|
msz = mscompress.read(file_path) |
|
|
|
|
|
for spectrum in msz.spectra: |
|
|
|
|
|
if _MS_LEVEL_FILTER and spectrum.ms_level not in _MS_LEVEL_FILTER: |
|
|
continue |
|
|
|
|
|
yield idx, { |
|
|
"source_file": Path(file_path).name, |
|
|
"spectrum_index": spectrum.index, |
|
|
"scan_number": spectrum.scan_number, |
|
|
"ms_level": spectrum.ms_level, |
|
|
"retention_time": spectrum.retention_time, |
|
|
"num_peaks": spectrum.num_peaks, |
|
|
"mz": spectrum.mz.tolist(), |
|
|
"intensity": spectrum.intensity.tolist(), |
|
|
} |
|
|
idx += 1 |
|
|
|