Skip to content

Commit db9bfb5

Browse files
authored
Refactor portals (minor) and add more tests to improve coverage (#53)
1 parent 4408a50 commit db9bfb5

File tree

8 files changed

+106
-34
lines changed

8 files changed

+106
-34
lines changed

README.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ DIMSpy
22
======
33
|Version| |Py versions| |Git| |Bioconda| |Build Status (Travis)| |Build Status (AppVeyor)| |License| |RTD doc| |codecov| |binder|
44

5-
Python package to process direct-infusion mass spectrometry-based metabolomics and lipidomics data
5+
Python package for processing direct-infusion mass spectrometry-based metabolomics and lipidomics data
66

7-
- **Documentation:** https://computational-metabolomics.github.io/dimspy
7+
- **Documentation:** https://dimspy.readthedocs.io/en/latest
88
- **Source:** https://github.com/computational-metabolomics/dimspy
99
- **Bug reports:** https://github.com/computational-metabolomics/dimspy/issues
1010

1111
Installation
1212
------------
13-
See the `Installation page <https://computational-metabolomics.github.io/dimspy/introduction.html#installation>`__ of
13+
See the `Installation page <https://dimspy.readthedocs.io/en/latest/introduction.html#installation>`__ of
1414
the `online documentation <https://computational-metabolomics.github.io/dimspy/>`__.
1515

1616

@@ -31,9 +31,10 @@ will help you to make the PR if you are new to `git`.
3131

3232
Developers & Contributors
3333
-------------------------
34-
- Ralf J. M. Weber ([email protected]) - `University of Birmingham (UK) <http://www.birmingham.ac.uk/index.aspx>`_
34+
- Ralf J. M. Weber ([email protected]) - `University of Birmingham (UK) <https://www.birmingham.ac.uk/staff/profiles/biosciences/weber-ralf.aspx>`_
3535
- Jiarui (Albert) Zhou ([email protected]) - `University of Birmingham (UK) <http://www.birmingham.ac.uk/index.aspx>`_, `HIT Shenzhen (China) <http://www.hitsz.edu.cn>`_
3636
- Thomas N. Lawson ([email protected]) - `University of Birmingham (UK) <http://www.birmingham.ac.uk/index.aspx>`_
37+
- Martin R. Jones ([email protected]) - `Eawag (Switzerland) <https://www.eawag.ch/en/aboutus/portrait/organisation/staff/profile/martin-jones/show/>`_
3738

3839

3940
License
@@ -63,7 +64,7 @@ Released under the GNU General Public License v3.0 (see `LICENSE file <https://g
6364
:target: https://www.gnu.org/licenses/gpl-3.0.html
6465

6566
.. |RTD doc| image:: https://img.shields.io/badge/documentation-RTD-71B360.svg?style=flat&maxAge=3600
66-
:target: https://computational-metabolomics.github.io/dimspy/
67+
:target: https://dimspy.readthedocs.io/en/latest/
6768

6869
.. |codecov| image:: https://codecov.io/gh/computational-metabolomics/dimspy/branch/master/graph/badge.svg
6970
:target: https://codecov.io/gh/computational-metabolomics/dimspy

dimspy/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
from dimspy import __version__
1010

1111

12-
def map_delimiter(delimiter):
12+
def map_delimiter(delimiter): # pragma: no cover
1313
seps = {"comma": ",", "tab": "\t"}
1414
if delimiter in seps:
1515
return seps[delimiter]
1616
else:
1717
return delimiter
1818

1919

20-
def main():
20+
def main(): # pragma: no cover
2121

2222
print(("Executing dimspy version %s." % __version__))
2323

dimspy/portals/mzml_portal.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def peaklist(self, scan_id, function_noise="median"):
8585
return pl
8686
return None
8787

88-
def peaklists(self, scan_ids, function_noise="median"): # generator
89-
88+
def peaklists(self, scan_ids, function_noise="median"):
9089
if function_noise not in ["mean", "median", "mad"]:
9190
raise ValueError("select a function that is available [mean, median, mad]")
9291
run = pymzml.run.Reader(self.filename)
@@ -95,38 +94,33 @@ def peaklists(self, scan_ids, function_noise="median"): # generator
9594
return pls
9695

9796
def tics(self):
97+
tic_values = collections.OrderedDict()
9898
run = pymzml.run.Reader(self.filename)
9999
for scan in run:
100-
if scan["id"] == "TIC":
101-
hit = zip(*scan.peaks("raw"))[1]
102-
run.info["file_object"].close()
103-
return hit
100+
tic_values[scan["id"]] = scan.TIC
104101
run.info["file_object"].close()
105-
return
102+
return tic_values
106103

107-
def injection_times(self):
108-
injection_times = {}
104+
def ion_injection_times(self):
105+
iits = collections.OrderedDict()
109106
run = pymzml.run.Reader(self.filename)
110107
for scan in run:
111-
injection_times[scan['id']] = None
112-
for element in scan.xmlTree:
113-
if "MS:1000927" == element.get('accession'):
114-
injection_times[scan['id']] = float(element.get("value"))
115-
break
116-
if scan['id'] not in injection_times:
117-
injection_times[scan['id']] = None
108+
if "MS:1000927" in scan:
109+
iits[scan['id']] = scan["MS:1000927"]
110+
else:
111+
iits[scan['id']] = None
118112
run.info["file_object"].close()
119-
return injection_times
113+
return iits
120114

121115
def scan_dependents(self):
122116
l = []
123117
run = pymzml.run.Reader(self.filename)
124118
for scan in run:
125119
if type(scan["id"]) == int:
126120
scan_id = scan["id"]
127-
if "precursors" in list(scan.keys()):
121+
if hasattr(scan, "precursors"):
128122
spectrum_ref = None
129-
for element in scan.xmlTree:
123+
for element in scan.element:
130124
for e in list(element.items()):
131125
if e[0] == 'spectrumRef':
132126
spectrum_ref = int(e[1].split("scan=")[1])

dimspy/portals/thermo_raw_portal.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,20 @@ def tics(self):
123123
tics = collections.OrderedDict()
124124
for scan_id in range(self.run.RunHeaderEx.FirstSpectrum, self.run.RunHeaderEx.LastSpectrum + 1):
125125
scan_stats = self.run.GetScanStatsForScanNumber(scan_id)
126-
tics[scan_id].append(scan_stats.TIC)
126+
tics[scan_id] = scan_stats.TIC
127127
return tics
128128

129-
def injection_times(self):
130-
injection_times = collections.OrderedDict()
129+
def ion_injection_times(self):
130+
iits = collections.OrderedDict()
131131
for scan_id in range(self.run.RunHeaderEx.FirstSpectrum, self.run.RunHeaderEx.LastSpectrum + 1):
132132
extra_values = list(self.run.GetTrailerExtraInformation(scan_id).Values)
133133
extra_labels = list(self.run.GetTrailerExtraInformation(scan_id).Labels)
134134
for i, label in enumerate(extra_labels):
135135
if "Ion Injection Time (ms):" == label:
136-
injection_times[scan_id] = float(extra_values[i])
137-
if scan_id not in injection_times:
138-
injection_times[scan_id] = None
139-
return injection_times
136+
iits[scan_id] = float(extra_values[i])
137+
if scan_id not in iits:
138+
iits[scan_id] = None
139+
return iits
140140

141141
def scan_dependents(self):
142142
l = []

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
numpy==1.16.2
22
scipy==1.1.0
33
fastcluster==1.1.25
4-
plotly==2.5.1
54
pymzml==2.4.2
65
h5py==2.8.0
76
pythonnet==2.4.0

tests/test_mzml_portal.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
import unittest
6+
import os
7+
from dimspy.portals.mzml_portal import Mzml
8+
9+
10+
def to_test_data(*args):
11+
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", "MTBLS79_subset", *args)
12+
13+
def to_test_result(*args):
14+
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_results", *args)
15+
16+
17+
class MzmlPortalsTestCase(unittest.TestCase):
18+
19+
def test_mzml_portal(self):
20+
21+
run = Mzml(to_test_data("mzml", "batch04_QC17_rep01_262.mzML"))
22+
self.assertEqual((run.run.get_spectrum_count(), run.run.get_spectrum_count()), (88, 88))
23+
self.assertListEqual(list(run.headers().keys()), ['FTMS + p ESI w SIM ms [70.00-170.00]',
24+
'FTMS + p ESI w SIM ms [140.00-240.00]',
25+
'FTMS + p ESI w SIM ms [210.00-310.00]',
26+
'FTMS + p ESI w SIM ms [280.00-380.00]',
27+
'FTMS + p ESI w SIM ms [350.00-450.00]',
28+
'FTMS + p ESI w SIM ms [420.00-520.00]',
29+
'FTMS + p ESI w SIM ms [490.00-590.00]'])
30+
self.assertListEqual(list(run.scan_ids().keys()), list(range(1,89)))
31+
self.assertListEqual(list(run.tics().values())[0:2], [39800032.0, 38217892.0])
32+
self.assertEqual(len(run.tics()), 88)
33+
self.assertListEqual(list(run.ion_injection_times().values())[0:2], [40.433891296387, 40.094646453857])
34+
self.assertEqual(len(run.ion_injection_times()), 88)
35+
self.assertListEqual(run.scan_dependents(), [])
36+
run.close()
37+
38+
if __name__ == '__main__':
39+
unittest.main()

tests/test_thermo_raw_portal.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
import unittest
6+
import os
7+
from dimspy.portals.thermo_raw_portal import ThermoRaw
8+
9+
10+
def to_test_data(*args):
11+
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", "MTBLS79_subset", *args)
12+
13+
def to_test_result(*args):
14+
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_results", *args)
15+
16+
17+
class ThermoRawPortalsTestCase(unittest.TestCase):
18+
19+
def test_thermo_raw_portal(self):
20+
21+
run = ThermoRaw(to_test_data("raw", "batch04_QC17_rep01_262.RAW"))
22+
self.assertListEqual(list(run.headers().keys()), ['FTMS + p ESI w SIM ms [70.00-170.00]',
23+
'FTMS + p ESI w SIM ms [140.00-240.00]',
24+
'FTMS + p ESI w SIM ms [210.00-310.00]',
25+
'FTMS + p ESI w SIM ms [280.00-380.00]',
26+
'FTMS + p ESI w SIM ms [350.00-450.00]',
27+
'FTMS + p ESI w SIM ms [420.00-520.00]',
28+
'FTMS + p ESI w SIM ms [490.00-590.00]'])
29+
self.assertListEqual(list(run.scan_ids().keys()), list(range(1,89)))
30+
self.assertListEqual(list(run.tics().values())[0:2], [39800032.0, 38217892.0])
31+
self.assertEqual(len(run.tics()), 88)
32+
self.assertListEqual(list(run.ion_injection_times().values())[0:2], [40.434, 40.095])
33+
self.assertEqual(len(run.ion_injection_times()), 88)
34+
self.assertListEqual(run.scan_dependents(), [])
35+
run.close()
36+
37+
if __name__ == '__main__':
38+
unittest.main()

tests/test_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
import unittest
14+
import os
1415
import copy
1516
import numpy as np
1617
from dimspy.tools import *

0 commit comments

Comments
 (0)