File size: 4,561 Bytes
ec1640d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | """
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: # file-level
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"
)
# Download data files
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":
# File-level granularity
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:
# Spectrum-level granularity
for file_path in msz_files:
msz = mscompress.read(file_path)
for spectrum in msz.spectra:
# Apply MS level filter
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
|