Skip to content

Commit

Permalink
Merge pull request #39 from computational-metabolomics/dev
Browse files Browse the repository at this point in the history
Add a number of bug fixes and improvements
  • Loading branch information
RJMW committed Nov 26, 2018
2 parents 5f7d6ff + e453c90 commit fe5cf6c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- "2.7"

install:
- sudo apt-get update
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
Expand All @@ -15,9 +16,13 @@ install:
- conda update -q conda
- conda info -a

- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy=1.13.0 scipy=0.19.1 pymzml=0.7.8 pythonnet=2.3.0 h5py=2.7.0 fastcluster=1.1.23 -c conda-forge -c bioconda
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy=1.13.0 scipy=1.0 pymzml=0.7.10 pythonnet=2.3.0 h5py=2.7.0 fastcluster=1.1.23 coverage green codecov -c conda-forge -c bioconda
- source activate test-environment
- python setup.py install

script:
- dimspy --help
- python setup.py test

after_script:
- python -m codecov
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DIMSpy
======
|Version| |Py versions| |Git| |Bioconda| |Build Status (Travis)| |Build Status (AppVeyor)| |License| |RTD doc|
|Version| |Py versions| |Git| |Bioconda| |Build Status (Travis)| |Build Status (AppVeyor)| |License| |RTD doc| |codecov|

Python package to process direct-infusion mass spectrometry-based metabolomics and lipidomics data

Expand Down Expand Up @@ -64,3 +64,6 @@ Released under the GNU General Public License v3.0 (see `LICENSE file <https://g

.. |RTD doc| image:: https://img.shields.io/badge/documentation-RTD-71B360.svg?style=flat&maxAge=3600
:target: https://computational-metabolomics.github.io/dimspy/

.. |codecov| image:: https://codecov.io/gh/computational-metabolomics/dimspy/branch/master/graph/badge.svg
:target: https://codecov.io/gh/computational-metabolomics/dimspy
2 changes: 1 addition & 1 deletion dimspy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import h5py
import tools
from portals import hdf5_portal
from . import __version__
from dimspy import __version__


def map_delimiter(delimiter):
Expand Down
5 changes: 4 additions & 1 deletion dimspy/models/peaklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def __len__(self):
def __str__(self):
return self.to_str(', ')

def __repr__(self):
return self.to_str('\t')

def __getitem__(self, item):
if type(item) in (int, slice, list, np.ndarray):
return self.get_peak(item)
Expand Down Expand Up @@ -348,7 +351,7 @@ def add_attribute(self, attr_name, attr_value, attr_dtype=None, is_flag=False,
attr_name = str(attr_name) # rfn.append_fields doesn't recognise unicode

adt = bool if is_flag else \
attr_dtype if attr_dtype is not None else \
attr_dtype if attr_dtype not in (None, str, unicode) else \
attr_value.dtype.str if hasattr(attr_value, 'dtype') else \
('S%d' % max(map(len, attr_value))) if type(attr_value[0]) in (unicode, str) else \
type(attr_value[0])
Expand Down
2 changes: 1 addition & 1 deletion dimspy/portals/thermo_raw_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def peaklist(self, scan_id, function_noise="noise_packets"):
else:
mzs, ints, baseline, noise = [], [], [], []

if function_noise == "noise_packets":
if function_noise == "noise_packets" and len(ints) > 0:
snr = [p.SignalToNoise for p in scan.GetCentroids()]
elif function_noise == "median" and len(ints) > 0:
snr = ints / np.median(ints)
Expand Down
31 changes: 22 additions & 9 deletions dimspy/process/peak_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,34 @@ def _cluster_peaks(mzs, ppm, distype='euclidean', linkmode='centroid'):
if len(mzs) == 1:
return np.zeros_like(mzs, dtype=int).reshape((-1, 1))

outer_mzs = np.add.outer(mzs, mzs)
np.fill_diagonal(outer_mzs, 0)

# avg_mz_pair = np.divide(outer_mzs, 2)
outer_mzs /= 2 # inplace operation to reduce memory usage

# mdist_mz_pair = squareform(avg_mz_pair)
mdist_mz_pair = squareform(outer_mzs)
del outer_mzs # reduce memory use

m = np.column_stack([mzs])
mdist = fc.pdist(m, metric=distype)
del m

outer_mzs = np.add.outer(mzs, mzs)
np.fill_diagonal(outer_mzs, 0)
avg_mz_pair = np.divide(outer_mzs, 2)
mdist_mz_pair = squareform(avg_mz_pair)
relative_errors = np.multiply(mdist_mz_pair, 1e-6)
# relative_errors = np.multiply(mdist_mz_pair, 1e-6)
mdist_mz_pair *= 1e-6 # inplace operation to reduce memory usage

with np.errstate(divide='ignore', invalid='ignore'): # using errstate context to avoid seterr side effects
m_mass_tol = np.divide(mdist, relative_errors)
m_mass_tol[np.isnan(m_mass_tol)] = 0.0
z = fc.linkage(m_mass_tol, method=linkmode)
# m_mass_tol = np.divide(mdist, relative_errors)
mdist /= mdist_mz_pair # inplace operation to reduce memory usage
# m_mass_tol[np.isnan(m_mass_tol)] = 0.0
mdist[np.isnan(mdist)] = 0.0

# z = fc.linkage(m_mass_tol, method=linkmode)
z = fc.linkage(mdist, method=linkmode)
del mdist, mdist_mz_pair

# cut tree at ppm threshold & order matches the order of mzs
# cut tree at ppm threshold
return cluster.hierarchy.cut_tree(z, height=ppm)


Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ numpy==1.13.3
scipy==1.0.0
fastcluster==1.1.23
plotly==2.5.1
pymzml==0.7.8
pymzml==0.7.10
h5py==2.7.0
pythonnet==2.3.0
pythonnet>=2.3.0

0 comments on commit fe5cf6c

Please sign in to comment.