Skip to content

Commit 738d5de

Browse files
committed
Close mzML output handle explicitly
1 parent 2135bc7 commit 738d5de

2 files changed

Lines changed: 91 additions & 23 deletions

File tree

tests/test_mzml_writer.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import vimms.MzmlWriter as mzml_writer_module
2+
from vimms.MzmlWriter import MzmlWriter
3+
4+
5+
class _ContextManager:
6+
def __enter__(self):
7+
return self
8+
9+
def __exit__(self, exc_type, exc_value, traceback):
10+
return False
11+
12+
13+
class _FakePsimsMzMLWriter:
14+
opened_handles = []
15+
16+
def __init__(self, out_handle):
17+
self.opened_handles.append(out_handle)
18+
19+
def __enter__(self):
20+
return self
21+
22+
def __exit__(self, exc_type, exc_value, traceback):
23+
return False
24+
25+
def controlled_vocabularies(self):
26+
pass
27+
28+
def file_description(self, *args, **kwargs):
29+
pass
30+
31+
def sample_list(self, *args, **kwargs):
32+
pass
33+
34+
def software_list(self, *args, **kwargs):
35+
pass
36+
37+
def scan_settings_list(self, *args, **kwargs):
38+
pass
39+
40+
def instrument_configuration_list(self, *args, **kwargs):
41+
pass
42+
43+
def data_processing_list(self, *args, **kwargs):
44+
pass
45+
46+
def run(self, *args, **kwargs):
47+
return _ContextManager()
48+
49+
def spectrum_list(self, *args, **kwargs):
50+
return _ContextManager()
51+
52+
def chromatogram_list(self, *args, **kwargs):
53+
return _ContextManager()
54+
55+
def write_chromatogram(self, *args, **kwargs):
56+
pass
57+
58+
def close(self):
59+
pass
60+
61+
62+
def test_mzml_writer_closes_output_handle(monkeypatch, tmp_path):
63+
_FakePsimsMzMLWriter.opened_handles = []
64+
monkeypatch.setattr(mzml_writer_module, "PsimsMzMLWriter", _FakePsimsMzMLWriter)
65+
66+
MzmlWriter("test_analysis", {1: []}).write_mzML(tmp_path / "test.mzML")
67+
68+
assert len(_FakePsimsMzMLWriter.opened_handles) == 1
69+
assert _FakePsimsMzMLWriter.opened_handles[0].closed

vimms/MzmlWriter.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,28 @@ def write_mzML(self, out_file):
5050
create_if_not_exist(out_dir)
5151

5252
# start writing mzML here
53-
with PsimsMzMLWriter(open(out_file, "wb")) as writer:
54-
# add default controlled vocabularies
55-
writer.controlled_vocabularies()
56-
57-
# write other fields like sample list, software list, etc.
58-
self._write_info(writer)
59-
60-
# open the run
61-
with writer.run(id=self.analysis_name):
62-
self._write_spectra(writer, self.scans)
63-
64-
# open chromatogram list sections
65-
with writer.chromatogram_list(count=1):
66-
tic_rts, tic_intensities = self._get_tic_chromatogram(self.scans)
67-
writer.write_chromatogram(
68-
tic_rts,
69-
tic_intensities,
70-
id="tic",
71-
chromatogram_type="total ion current chromatogram",
72-
time_unit="second",
73-
)
74-
75-
writer.close()
53+
with open(out_file, "wb") as out_handle:
54+
with PsimsMzMLWriter(out_handle) as writer:
55+
# add default controlled vocabularies
56+
writer.controlled_vocabularies()
57+
58+
# write other fields like sample list, software list, etc.
59+
self._write_info(writer)
60+
61+
# open the run
62+
with writer.run(id=self.analysis_name):
63+
self._write_spectra(writer, self.scans)
64+
65+
# open chromatogram list sections
66+
with writer.chromatogram_list(count=1):
67+
tic_rts, tic_intensities = self._get_tic_chromatogram(self.scans)
68+
writer.write_chromatogram(
69+
tic_rts,
70+
tic_intensities,
71+
id="tic",
72+
chromatogram_type="total ion current chromatogram",
73+
time_unit="second",
74+
)
7675

7776
def _write_info(self, out):
7877
"""

0 commit comments

Comments
 (0)