msformulator 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ # 源分发(sdist)包含规则
2
+ # 关键:排除体积巨大且不随包分发的模型权重,以及辅助脚本目录。
3
+
4
+ include README.md
5
+ include requirements.txt
6
+ include pyproject.toml
7
+
8
+ # 包内小型数据文件
9
+ include msformulator/data/*.pth
10
+ include msformulator/data/*.npy
11
+
12
+ # 排除大型权重与辅助脚本(它们不进入分发包)
13
+ recursive-exclude weights *
14
+ recursive-exclude scripts *
15
+ recursive-exclude .workbuddy *
16
+
17
+ # 通用清理
18
+ global-exclude *.pyc
19
+ recursive-exclude * __pycache__
20
+ prune __pycache__
@@ -0,0 +1,161 @@
1
+ Metadata-Version: 2.4
2
+ Name: msformulator
3
+ Version: 0.1.0
4
+ Summary: MSFormulator:基于扩散模型的质谱(MS2)分子式预测方法,支持 pip 安装与命令行调用。
5
+ Author-email: Your Name <you@example.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/yourusername/msformulator
8
+ Project-URL: Repository, https://github.com/yourusername/msformulator
9
+ Keywords: msformulator,mass-spectrometry,ms2,molecular-formula,diffusion-model,cheminformatics
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Scientific/Engineering :: Chemistry
14
+ Requires-Python: >=3.9
15
+ Description-Content-Type: text/markdown
16
+ Requires-Dist: numpy
17
+ Requires-Dist: torch
18
+ Requires-Dist: molmass
19
+ Requires-Dist: pyteomics
20
+ Provides-Extra: scripts
21
+ Requires-Dist: pandas; extra == "scripts"
22
+ Requires-Dist: rdkit; extra == "scripts"
23
+ Requires-Dist: tqdm; extra == "scripts"
24
+ Provides-Extra: dev
25
+ Requires-Dist: build; extra == "dev"
26
+ Requires-Dist: twine; extra == "dev"
27
+
28
+ # MSFormulator
29
+
30
+ **MSFormulator** 是基于扩散模型(diffusion)的质谱二级碎片(MS2)分子式预测方法。
31
+ 本仓库是对原始 `dxzx2/ms2_formula_model` 的**整理版本**:将原本堆在单个 `main.py`(约 1480 行)中的
32
+ 化学计算、推理流水线、模型定义拆分成了清晰的 Python 包结构,并把数据/权重与代码分离,
33
+ 现已可经 `pip install` 安装与发布(PyPI 包名 `msformulator`)。
34
+
35
+ ## 目录结构
36
+
37
+ ```
38
+ ms2_formula_model_organized/ # 工作目录(即本仓库根)
39
+ ├── run.py # 命令行入口:python run.py <pr_mz> <mz2> <type> <peak_type>
40
+ ├── pyproject.toml # 打包元数据(包名 msformulator)
41
+ ├── MANIFEST.in # 排除大体积权重,避免打进分发包
42
+ ├── requirements.txt # 依赖
43
+ ├── README.md
44
+ ├── msformulator/ # 核心包(import 名,即方法名)
45
+ │ ├── __init__.py # 暴露 main / MSFormulator / GaussianDiffusion
46
+ │ ├── config.py # 数据/权重的路径配置(不再硬编码相对路径)
47
+ │ ├── model.py # 神经网络:MSFormulator / GaussianDiffusion / UNet1D
48
+ │ ├── chemistry.py # 纯计算:质量微调、不饱和度校验、ppm、分子式解析等
49
+ │ ├── inference.py # 推理流水线:注释查找→峰编码→模型预测→分子式解码
50
+ │ ├── cli.py # 命令行入口(注册为 `msformulator` 命令)
51
+ │ ├── download.py # 权重解析与按需下载
52
+ │ ├── main.py # 入口函数 main()
53
+ │ └── data/ # 小型数据:embedding_parameters.pth、mass_zhushi.npy
54
+ ├── weights/ # 模型权重(两个 .pt,体积较大,不随包分发)
55
+ └── scripts/ # 辅助/遗留脚本
56
+ ├── find_cl.py
57
+ ├── find_pubchem_formula.py
58
+ ├── read_pubchem.py
59
+ ├── add_zhushi.py
60
+ └── legacy/ # 旧版本 main_quick.py / TEST_BIAOZHUN.py(见下“已知问题”)
61
+ ```
62
+
63
+ ## 入口函数
64
+
65
+ 入口为 `main(pr_mz, mz2, type_str, peak_type_str)`:
66
+
67
+ | 参数 | 含义 | 示例 |
68
+ |------|------|------|
69
+ | `pr_mz` | 母离子精确质量 (float) | `439.326` |
70
+ | `mz2` | 二级碎片峰列表 `[[mz, intensity], ...]` | `[]` |
71
+ | `type_str` | 母离子加合类型 | `"[M+H]+"` |
72
+ | `peak_type_str` | 碎片峰加合类型 | `"[M+H]+"` |
73
+
74
+ 返回按 ppm 误差升序排列的候选分子式列表。
75
+
76
+ ## 运行方式
77
+
78
+ 1. 安装依赖:`pip install -r requirements.txt`
79
+ 2. 命令行(源码方式):
80
+ ```bash
81
+ python run.py 439.326 "[]" "[M+H]+" "[M+H]+"
82
+ ```
83
+ 或作为模块:
84
+ ```bash
85
+ python -m msformulator.main 439.326 "[]" "[M+H]+" "[M+H]+"
86
+ ```
87
+ 3. 在 Python 中调用:
88
+ ```python
89
+ from msformulator.main import main
90
+ print(main(439.326, [], "[M+H]+", "[M+H]+"))
91
+ ```
92
+
93
+ > 运行推理时需要两个 `.pt` 权重文件(约 1.5GB)与 `msformulator/data/` 下的小型数据文件。
94
+ > 小型数据文件随包分发;权重文件**不随包分发**,首次运行会自动下载(见下文“安装 / 权重”)。
95
+
96
+ ## 安装(pip)
97
+
98
+ 本包已配置为标准可安装项目(`pyproject.toml`,分发名 `msformulator`)。
99
+
100
+ ### 1. 本地 / 源码安装
101
+
102
+ ```bash
103
+ cd ms2_formula_model_organized
104
+ pip install .
105
+ ```
106
+
107
+ ### 2. 从 PyPI 安装(发布后)
108
+
109
+ ```bash
110
+ pip install msformulator
111
+ ```
112
+
113
+ ### 3. 权重获取(重要)
114
+
115
+ 权重较大,不打包进 wheel。运行时按以下优先级解析:
116
+
117
+ - 环境变量 `MS2_WEIGHTS_DIR` 指向的目录(直接放好两个 `.pt` 即可,**不触发下载**);或
118
+ - 本地缓存 `~/.cache/msformulator/weights`,首次运行时从 `MS2_WEIGHTS_URL` 自动下载。
119
+
120
+ ```bash
121
+ # 方式 A:用自己的托管地址(GitHub Release / HuggingFace / 对象存储)
122
+ export MS2_WEIGHTS_URL="https://github.com/<user>/<repo>/releases/download/v1.0.0"
123
+ msformulator 439.326 "[]" "[M+H]+" "[M+H]+"
124
+
125
+ # 方式 B:本地已存在权重目录,跳过下载
126
+ export MS2_WEIGHTS_DIR="/path/to/your/weights"
127
+ msformulator 439.326 "[]" "[M+H]+" "[M+H]+"
128
+ ```
129
+
130
+ > 发布前请把 `msformulator/download.py` 中的 `DEFAULT_WEIGHTS_URL` 改成你的真实地址,
131
+ > 并(建议)在 `WEIGHT_SIZES` 中填入两个权重文件的字节数用于完整性校验。
132
+
133
+ ## 发布到 PyPI
134
+
135
+ ```bash
136
+ cd ms2_formula_model_organized
137
+ pip install build twine
138
+ python -m build # 生成 dist/ 下的 sdist 与 wheel
139
+ twine check dist/* # 检查分发包合法性
140
+ twine upload dist/* # 上传到 PyPI(需提前注册 PyPI 账号)
141
+ ```
142
+
143
+ > `MANIFEST.in` 已配置为排除 `weights/` 与 `scripts/`,确保 1.5GB 权重不会被打进 sdist/wheel。
144
+
145
+ ## 模块说明
146
+
147
+ - **chemistry.py**:原 `main.py` 中的纯计算函数(无 torch 依赖)。包括
148
+ `tiaozheng_f` / `tiaozheng_f2`(质量微调)、`calculate_ppm`、`calculate_molecular_mass`、
149
+ `check_molecular_formula`、`extract_*_numbers`(分子式各元素数量解析)、`parse_ion_pattern` / `get_mass` 等。
150
+ - **inference.py**:推理流水线。`get_zhushi`(按质量查找注释)、`get_peak_mass`(碎片峰编码)、
151
+ `model_test`(加载权重并做扩散模型预测)、`get_one_formula_tiaozheng` / `get_formula`(解码分子式)。
152
+ - **model.py**:原 `model.py` 完整保留,仅将 `embedding_parameters.pth` 的加载路径改为相对于包目录解析。
153
+ 核心网络类即 **`MSFormulator`**(配合 `GaussianDiffusion` 做扩散采样)。
154
+ - **download.py**:权重解析与下载(支持 `MS2_WEIGHTS_DIR` / `MS2_WEIGHTS_URL` / `MS2_CACHE_DIR` 环境变量)。
155
+
156
+ ## 已知问题
157
+
158
+ - `scripts/legacy/main_quick.py` 与 `scripts/legacy/TEST_BIAOZHUN.py` 引用了 `from msformulator.model import PCTE`,
159
+ 但 `model.py` 中**并不存在 `PCTE` 类**(这是原始代码就存在的问题),因此这两个文件当前无法运行。
160
+ 它们作为历史版本保留在 `legacy/` 中,待补齐 `PCTE` 定义后可恢复使用。
161
+ - 主入口 `main()` 使用的是 `MSFormulator`,可正常运行(依赖 `weights/` 中的权重文件)。
@@ -0,0 +1,134 @@
1
+ # MSFormulator
2
+
3
+ **MSFormulator** 是基于扩散模型(diffusion)的质谱二级碎片(MS2)分子式预测方法。
4
+ 本仓库是对原始 `dxzx2/ms2_formula_model` 的**整理版本**:将原本堆在单个 `main.py`(约 1480 行)中的
5
+ 化学计算、推理流水线、模型定义拆分成了清晰的 Python 包结构,并把数据/权重与代码分离,
6
+ 现已可经 `pip install` 安装与发布(PyPI 包名 `msformulator`)。
7
+
8
+ ## 目录结构
9
+
10
+ ```
11
+ ms2_formula_model_organized/ # 工作目录(即本仓库根)
12
+ ├── run.py # 命令行入口:python run.py <pr_mz> <mz2> <type> <peak_type>
13
+ ├── pyproject.toml # 打包元数据(包名 msformulator)
14
+ ├── MANIFEST.in # 排除大体积权重,避免打进分发包
15
+ ├── requirements.txt # 依赖
16
+ ├── README.md
17
+ ├── msformulator/ # 核心包(import 名,即方法名)
18
+ │ ├── __init__.py # 暴露 main / MSFormulator / GaussianDiffusion
19
+ │ ├── config.py # 数据/权重的路径配置(不再硬编码相对路径)
20
+ │ ├── model.py # 神经网络:MSFormulator / GaussianDiffusion / UNet1D
21
+ │ ├── chemistry.py # 纯计算:质量微调、不饱和度校验、ppm、分子式解析等
22
+ │ ├── inference.py # 推理流水线:注释查找→峰编码→模型预测→分子式解码
23
+ │ ├── cli.py # 命令行入口(注册为 `msformulator` 命令)
24
+ │ ├── download.py # 权重解析与按需下载
25
+ │ ├── main.py # 入口函数 main()
26
+ │ └── data/ # 小型数据:embedding_parameters.pth、mass_zhushi.npy
27
+ ├── weights/ # 模型权重(两个 .pt,体积较大,不随包分发)
28
+ └── scripts/ # 辅助/遗留脚本
29
+ ├── find_cl.py
30
+ ├── find_pubchem_formula.py
31
+ ├── read_pubchem.py
32
+ ├── add_zhushi.py
33
+ └── legacy/ # 旧版本 main_quick.py / TEST_BIAOZHUN.py(见下“已知问题”)
34
+ ```
35
+
36
+ ## 入口函数
37
+
38
+ 入口为 `main(pr_mz, mz2, type_str, peak_type_str)`:
39
+
40
+ | 参数 | 含义 | 示例 |
41
+ |------|------|------|
42
+ | `pr_mz` | 母离子精确质量 (float) | `439.326` |
43
+ | `mz2` | 二级碎片峰列表 `[[mz, intensity], ...]` | `[]` |
44
+ | `type_str` | 母离子加合类型 | `"[M+H]+"` |
45
+ | `peak_type_str` | 碎片峰加合类型 | `"[M+H]+"` |
46
+
47
+ 返回按 ppm 误差升序排列的候选分子式列表。
48
+
49
+ ## 运行方式
50
+
51
+ 1. 安装依赖:`pip install -r requirements.txt`
52
+ 2. 命令行(源码方式):
53
+ ```bash
54
+ python run.py 439.326 "[]" "[M+H]+" "[M+H]+"
55
+ ```
56
+ 或作为模块:
57
+ ```bash
58
+ python -m msformulator.main 439.326 "[]" "[M+H]+" "[M+H]+"
59
+ ```
60
+ 3. 在 Python 中调用:
61
+ ```python
62
+ from msformulator.main import main
63
+ print(main(439.326, [], "[M+H]+", "[M+H]+"))
64
+ ```
65
+
66
+ > 运行推理时需要两个 `.pt` 权重文件(约 1.5GB)与 `msformulator/data/` 下的小型数据文件。
67
+ > 小型数据文件随包分发;权重文件**不随包分发**,首次运行会自动下载(见下文“安装 / 权重”)。
68
+
69
+ ## 安装(pip)
70
+
71
+ 本包已配置为标准可安装项目(`pyproject.toml`,分发名 `msformulator`)。
72
+
73
+ ### 1. 本地 / 源码安装
74
+
75
+ ```bash
76
+ cd ms2_formula_model_organized
77
+ pip install .
78
+ ```
79
+
80
+ ### 2. 从 PyPI 安装(发布后)
81
+
82
+ ```bash
83
+ pip install msformulator
84
+ ```
85
+
86
+ ### 3. 权重获取(重要)
87
+
88
+ 权重较大,不打包进 wheel。运行时按以下优先级解析:
89
+
90
+ - 环境变量 `MS2_WEIGHTS_DIR` 指向的目录(直接放好两个 `.pt` 即可,**不触发下载**);或
91
+ - 本地缓存 `~/.cache/msformulator/weights`,首次运行时从 `MS2_WEIGHTS_URL` 自动下载。
92
+
93
+ ```bash
94
+ # 方式 A:用自己的托管地址(GitHub Release / HuggingFace / 对象存储)
95
+ export MS2_WEIGHTS_URL="https://github.com/<user>/<repo>/releases/download/v1.0.0"
96
+ msformulator 439.326 "[]" "[M+H]+" "[M+H]+"
97
+
98
+ # 方式 B:本地已存在权重目录,跳过下载
99
+ export MS2_WEIGHTS_DIR="/path/to/your/weights"
100
+ msformulator 439.326 "[]" "[M+H]+" "[M+H]+"
101
+ ```
102
+
103
+ > 发布前请把 `msformulator/download.py` 中的 `DEFAULT_WEIGHTS_URL` 改成你的真实地址,
104
+ > 并(建议)在 `WEIGHT_SIZES` 中填入两个权重文件的字节数用于完整性校验。
105
+
106
+ ## 发布到 PyPI
107
+
108
+ ```bash
109
+ cd ms2_formula_model_organized
110
+ pip install build twine
111
+ python -m build # 生成 dist/ 下的 sdist 与 wheel
112
+ twine check dist/* # 检查分发包合法性
113
+ twine upload dist/* # 上传到 PyPI(需提前注册 PyPI 账号)
114
+ ```
115
+
116
+ > `MANIFEST.in` 已配置为排除 `weights/` 与 `scripts/`,确保 1.5GB 权重不会被打进 sdist/wheel。
117
+
118
+ ## 模块说明
119
+
120
+ - **chemistry.py**:原 `main.py` 中的纯计算函数(无 torch 依赖)。包括
121
+ `tiaozheng_f` / `tiaozheng_f2`(质量微调)、`calculate_ppm`、`calculate_molecular_mass`、
122
+ `check_molecular_formula`、`extract_*_numbers`(分子式各元素数量解析)、`parse_ion_pattern` / `get_mass` 等。
123
+ - **inference.py**:推理流水线。`get_zhushi`(按质量查找注释)、`get_peak_mass`(碎片峰编码)、
124
+ `model_test`(加载权重并做扩散模型预测)、`get_one_formula_tiaozheng` / `get_formula`(解码分子式)。
125
+ - **model.py**:原 `model.py` 完整保留,仅将 `embedding_parameters.pth` 的加载路径改为相对于包目录解析。
126
+ 核心网络类即 **`MSFormulator`**(配合 `GaussianDiffusion` 做扩散采样)。
127
+ - **download.py**:权重解析与下载(支持 `MS2_WEIGHTS_DIR` / `MS2_WEIGHTS_URL` / `MS2_CACHE_DIR` 环境变量)。
128
+
129
+ ## 已知问题
130
+
131
+ - `scripts/legacy/main_quick.py` 与 `scripts/legacy/TEST_BIAOZHUN.py` 引用了 `from msformulator.model import PCTE`,
132
+ 但 `model.py` 中**并不存在 `PCTE` 类**(这是原始代码就存在的问题),因此这两个文件当前无法运行。
133
+ 它们作为历史版本保留在 `legacy/` 中,待补齐 `PCTE` 定义后可恢复使用。
134
+ - 主入口 `main()` 使用的是 `MSFormulator`,可正常运行(依赖 `weights/` 中的权重文件)。
@@ -0,0 +1,15 @@
1
+ """msformulator - 基于扩散模型的质谱分子式预测包。
2
+
3
+ 入口函数:main(pr_mz, mz2, type_str, peak_type_str)
4
+ pr_mz : 母离子精确质量 (float)
5
+ mz2 : 二级碎片峰列表,形如 [[mz, intensity], ...]
6
+ type_str : 母离子加合类型,如 "[M+H]+"
7
+ peak_type_str: 碎片峰加合类型,如 "[M+H]+"
8
+
9
+ 返回候选分子式列表(按 ppm 误差升序)。
10
+ """
11
+
12
+ from .main import main
13
+ from .model import MSFormulator, GaussianDiffusion
14
+
15
+ __all__ = ["main", "MSFormulator", "GaussianDiffusion"]