Skip to content

Commit

Permalink
Merge pull request #5 from theGreatHerrLebert/david@rustdf
Browse files Browse the repository at this point in the history
David@rustdf
  • Loading branch information
theGreatHerrLebert authored Sep 20, 2023
2 parents f9e9248 + d8b2d19 commit 7b8b37c
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mscore/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod mz_spectrum;
pub use {mz_spectrum::MzSpectrum, mz_spectrum::TOFMzSpectrum, mz_spectrum::ImsSpectrum, mz_spectrum::TimsSpectrum, mz_spectrum::ImsFrame, mz_spectrum::TimsFrame};
pub use {mz_spectrum::MzSpectrum, mz_spectrum::TOFMzSpectrum, mz_spectrum::ImsSpectrum, mz_spectrum::TimsSpectrum, mz_spectrum::ImsFrame, mz_spectrum::TimsFrame, mz_spectrum::MsType};
67 changes: 61 additions & 6 deletions mscore/src/mz_spectrum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;
use std::collections::BTreeMap;
use std::fmt::Formatter;
use std::fmt::{Display, Formatter};
use itertools;

/// Represents a mass spectrum with associated m/z values and intensities.
Expand All @@ -10,6 +10,58 @@ pub struct MzSpectrum {
pub intensity: Vec<f64>,
}

/// Represents the type of spectrum.
///
/// # Description
///
/// The `SpecType` enum is used to distinguish between precursor and fragment spectra.
///
#[derive(Clone)]
pub enum MsType {
Precursor,
FragmentDda,
FragmentDia,
Unknown,
}

impl MsType {
/// Returns the `MsType` enum corresponding to the given integer value.
///
/// # Arguments
///
/// * `ms_type` - An integer value corresponding to the `MsType` enum.
///
pub fn new(ms_type: i32) -> MsType {
match ms_type {
0 => MsType::Precursor,
8 => MsType::FragmentDda,
9 => MsType::FragmentDia,
_ => MsType::Unknown,
}
}

/// Returns the integer value corresponding to the `MsType` enum.
pub fn to_i32(&self) -> i32 {
match self {
MsType::Precursor => 0,
MsType::FragmentDda => 8,
MsType::FragmentDia => 9,
MsType::Unknown => -1,
}
}
}

impl Display for MsType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
MsType::Precursor => write!(f, "Precursor"),
MsType::FragmentDda => write!(f, "FragmentDda"),
MsType::FragmentDia => write!(f, "FragmentDia"),
MsType::Unknown => write!(f, "Unknown"),
}
}
}

impl MzSpectrum {
/// Constructs a new `MzSpectrum`.
///
Expand Down Expand Up @@ -331,6 +383,7 @@ impl fmt::Display for ImsFrame {
#[derive(Clone)]
pub struct TimsFrame {
pub frame_id: i32,
pub ms_type: MsType,
pub retention_time: f64,
pub scan: Vec<i32>,
pub inv_mobility: Vec<f64>,
Expand All @@ -345,6 +398,7 @@ impl TimsFrame {
/// # Arguments
///
/// * `frame_id` - index of frame in TDF raw file.
/// * `ms_type` - The type of frame.
/// * `retention_time` - The retention time in seconds.
/// * `scan` - A vector of scan IDs.
/// * `inv_mobility` - A vector of inverse ion mobilities.
Expand All @@ -355,12 +409,13 @@ impl TimsFrame {
/// # Examples
///
/// ```
/// use mscore::MsType;
/// use mscore::TimsFrame;
///
/// let frame = TimsFrame::new(1, 100.0, vec![1, 2], vec![0.1, 0.2], vec![1000, 2000], vec![100.5, 200.5], vec![50.0, 60.0]);
///
/// let frame = TimsFrame::new(1, MsType::PRECURSOR, 100.0, vec![1, 2], vec![0.1, 0.2], vec![1000, 2000], vec![100.5, 200.5], vec![50.0, 60.0]);
/// ```
pub fn new(frame_id: i32, retention_time: f64, scan: Vec<i32>, inv_mobility: Vec<f64>, tof: Vec<i32>, mz: Vec<f64>, intensity: Vec<f64>) -> Self {
TimsFrame { frame_id, retention_time, scan, inv_mobility, tof, mz, intensity }
pub fn new(frame_id: i32, ms_type: MsType, retention_time: f64, scan: Vec<i32>, inv_mobility: Vec<f64>, tof: Vec<i32>, mz: Vec<f64>, intensity: Vec<f64>) -> Self {
TimsFrame { frame_id, ms_type, retention_time, scan, inv_mobility, tof, mz, intensity }
}

///
Expand Down Expand Up @@ -447,7 +502,7 @@ impl fmt::Display for TimsFrame {
.max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
.unwrap();

write!(f, "TimsFrame(id: {}, rt: {}, data points: {}, max by intensity: (mz: {}, intensity: {}))", self.frame_id, self.retention_time, self.scan.len(), format!("{:.3}", mz), i)
write!(f, "TimsFrame(id: {}, type: {}, rt: {}, data points: {}, max by intensity: (mz: {}, intensity: {}))", self.frame_id, self.ms_type, self.retention_time, self.scan.len(), format!("{:.3}", mz), i)
}
}

Expand Down
120 changes: 120 additions & 0 deletions pyims/.github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# This file is autogenerated by maturin v1.2.3
# To update, run
#
# maturin generate-ci github
#
name: CI

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
linux:
runs-on: ubuntu-latest
strategy:
matrix:
target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: auto
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

windows:
runs-on: windows-latest
strategy:
matrix:
target: [x64, x86]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: ${{ matrix.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

macos:
runs-on: macos-latest
strategy:
matrix:
target: [x86_64, aarch64]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
- name: Upload sdist
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

release:
name: Release
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
needs: [linux, windows, macos, sdist]
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
with:
command: upload
args: --non-interactive --skip-existing *
72 changes: 72 additions & 0 deletions pyims/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
15 changes: 15 additions & 0 deletions pyims/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pyims"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "pyims"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.19.2", features = ["extension-module"] }
numpy = "0.19.0"
mscore = {path = "../mscore"}
rustdf = {path = "../rustdf"}
16 changes: 16 additions & 0 deletions pyims/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build-system]
requires = ["maturin>=1.2,<2.0"]
build-backend = "maturin"

[project]
name = "pyims"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]


[tool.maturin]
features = ["pyo3/extension-module"]
14 changes: 14 additions & 0 deletions pyims/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod pyhandle;
mod py_mz_spectrum;

use pyo3::prelude::*;
use crate::pyhandle::PyTimsDataset;
use crate::py_mz_spectrum::PyMzSpectrum;

/// A Python module implemented in Rust.
#[pymodule]
fn pyims(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PyTimsDataset>()?;
m.add_class::<PyMzSpectrum>()?;
Ok(())
}
Loading

0 comments on commit 7b8b37c

Please sign in to comment.