chrisagrams commited on
Commit
ec1640d
·
verified ·
1 Parent(s): 08761d5

Upload mscompress-test.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. mscompress-test.py +142 -0
mscompress-test.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ MSZ Mass Spectrometry Dataset Loader
3
+
4
+ This dataset contains compressed mass spectrometry data in MSZ format.
5
+ Requires the 'mscompress' library for loading.
6
+
7
+ Install: pip install mscompress
8
+ """
9
+
10
+ import datasets
11
+ from pathlib import Path
12
+ from typing import List, Dict, Any
13
+
14
+
15
+ _DESCRIPTION = """
16
+ Mass spectrometry dataset in compressed MSZ format.
17
+ """
18
+
19
+ _HOMEPAGE = ""
20
+
21
+ _LICENSE = ""
22
+
23
+ _DATA_FILES = ['test.msz']
24
+
25
+ _MS_LEVEL_FILTER = None
26
+
27
+ _GRANULARITY = "spectrum"
28
+
29
+
30
+ class MSZDatasetConfig(datasets.BuilderConfig):
31
+ """BuilderConfig for MSZ dataset."""
32
+
33
+ def __init__(self, **kwargs):
34
+ super(MSZDatasetConfig, self).__init__(**kwargs)
35
+
36
+
37
+ class MSZDataset(datasets.GeneratorBasedBuilder):
38
+ """MSZ mass spectrometry dataset."""
39
+
40
+ VERSION = datasets.Version("1.0.0")
41
+
42
+ BUILDER_CONFIGS = [
43
+ MSZDatasetConfig(
44
+ name="default",
45
+ version=VERSION,
46
+ description="Default configuration for MSZ dataset",
47
+ ),
48
+ ]
49
+
50
+ DEFAULT_CONFIG_NAME = "default"
51
+
52
+ def _info(self):
53
+ if _GRANULARITY == "spectrum":
54
+ features = datasets.Features({
55
+ "source_file": datasets.Value("string"),
56
+ "spectrum_index": datasets.Value("int64"),
57
+ "scan_number": datasets.Value("int64"),
58
+ "ms_level": datasets.Value("int32"),
59
+ "retention_time": datasets.Value("float64"),
60
+ "num_peaks": datasets.Value("int64"),
61
+ "mz": datasets.Sequence(datasets.Value("float64")),
62
+ "intensity": datasets.Sequence(datasets.Value("float64")),
63
+ })
64
+ else: # file-level
65
+ features = datasets.Features({
66
+ "file_path": datasets.Value("string"),
67
+ "file_name": datasets.Value("string"),
68
+ "num_spectra": datasets.Value("int64"),
69
+ "file_size_bytes": datasets.Value("int64"),
70
+ "compression_format": datasets.Value("string"),
71
+ })
72
+
73
+ return datasets.DatasetInfo(
74
+ description=_DESCRIPTION,
75
+ features=features,
76
+ homepage=_HOMEPAGE,
77
+ license=_LICENSE,
78
+ )
79
+
80
+ def _split_generators(self, dl_manager):
81
+ """Download and extract MSZ files"""
82
+ try:
83
+ import mscompress
84
+ except ImportError:
85
+ raise ImportError(
86
+ "The mscompress library is required to load this dataset. "
87
+ "Install it with: pip install mscompress"
88
+ )
89
+
90
+ # Download data files
91
+ data_dir = dl_manager.download_and_extract({
92
+ "data": ["data/" + f for f in _DATA_FILES]
93
+ })
94
+
95
+ return [
96
+ datasets.SplitGenerator(
97
+ name=datasets.Split.TRAIN,
98
+ gen_kwargs={
99
+ "msz_files": data_dir["data"],
100
+ },
101
+ ),
102
+ ]
103
+
104
+ def _generate_examples(self, msz_files: List[str]):
105
+ """Generate examples from MSZ files"""
106
+ import mscompress
107
+
108
+ idx = 0
109
+
110
+ if _GRANULARITY == "file":
111
+ # File-level granularity
112
+ for file_path in msz_files:
113
+ msz = mscompress.read(file_path)
114
+ yield idx, {
115
+ "file_path": file_path,
116
+ "file_name": Path(file_path).name,
117
+ "num_spectra": len(msz.spectra),
118
+ "file_size_bytes": msz.size,
119
+ "compression_format": msz.data_format.mz_original_compression,
120
+ }
121
+ idx += 1
122
+ else:
123
+ # Spectrum-level granularity
124
+ for file_path in msz_files:
125
+ msz = mscompress.read(file_path)
126
+
127
+ for spectrum in msz.spectra:
128
+ # Apply MS level filter
129
+ if _MS_LEVEL_FILTER and spectrum.ms_level not in _MS_LEVEL_FILTER:
130
+ continue
131
+
132
+ yield idx, {
133
+ "source_file": Path(file_path).name,
134
+ "spectrum_index": spectrum.index,
135
+ "scan_number": spectrum.scan_number,
136
+ "ms_level": spectrum.ms_level,
137
+ "retention_time": spectrum.retention_time,
138
+ "num_peaks": spectrum.num_peaks,
139
+ "mz": spectrum.mz.tolist(),
140
+ "intensity": spectrum.intensity.tolist(),
141
+ }
142
+ idx += 1