msIO 1.0.3__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.
Files changed (83) hide show
  1. msio-1.0.3/LICENSE +21 -0
  2. msio-1.0.3/PKG-INFO +151 -0
  3. msio-1.0.3/README.md +119 -0
  4. msio-1.0.3/msIO/__init__.py +15 -0
  5. msio-1.0.3/msIO/_templates/__init__.py +12 -0
  6. msio-1.0.3/msIO/_templates/_many_to_many.py +39 -0
  7. msio-1.0.3/msIO/_templates/_many_to_many_bidirectional.py +43 -0
  8. msio-1.0.3/msIO/_templates/_many_to_one.py +22 -0
  9. msio-1.0.3/msIO/_templates/_many_to_one_bidirectional.py +23 -0
  10. msio-1.0.3/msIO/_templates/_one_to_many.py +33 -0
  11. msio-1.0.3/msIO/_templates/_one_to_many_bidirectional.py +32 -0
  12. msio-1.0.3/msIO/_templates/_one_to_one.py +26 -0
  13. msio-1.0.3/msIO/_templates/relationship_patterns.py +65 -0
  14. msio-1.0.3/msIO/_version.py +2 -0
  15. msio-1.0.3/msIO/annotations/__init__.py +0 -0
  16. msio-1.0.3/msIO/annotations/main.py +228 -0
  17. msio-1.0.3/msIO/core.py +29 -0
  18. msio-1.0.3/msIO/environmental/__init__.py +0 -0
  19. msio-1.0.3/msIO/environmental/location.py +21 -0
  20. msio-1.0.3/msIO/environmental/sample.py +48 -0
  21. msio-1.0.3/msIO/export_manager/__init__.py +0 -0
  22. msio-1.0.3/msIO/export_manager/main.py +34 -0
  23. msio-1.0.3/msIO/feature_managers/__init__.py +0 -0
  24. msio-1.0.3/msIO/feature_managers/_read_mzio_graphml_as_xml.py +68 -0
  25. msio-1.0.3/msIO/feature_managers/base.py +45 -0
  26. msio-1.0.3/msIO/feature_managers/combined.py +171 -0
  27. msio-1.0.3/msIO/feature_managers/db.py +1305 -0
  28. msio-1.0.3/msIO/feature_managers/gnps.py +75 -0
  29. msio-1.0.3/msIO/feature_managers/metaboscape.py +114 -0
  30. msio-1.0.3/msIO/feature_managers/sirius.py +162 -0
  31. msio-1.0.3/msIO/feature_managers/util_library.py +42 -0
  32. msio-1.0.3/msIO/features/__init__.py +0 -0
  33. msio-1.0.3/msIO/features/base.py +80 -0
  34. msio-1.0.3/msIO/features/combined.py +143 -0
  35. msio-1.0.3/msIO/features/gnps.py +59 -0
  36. msio-1.0.3/msIO/features/metaboscape.py +128 -0
  37. msio-1.0.3/msIO/features/mgf.py +171 -0
  38. msio-1.0.3/msIO/features/sirius.py +228 -0
  39. msio-1.0.3/msIO/features/util.py +13 -0
  40. msio-1.0.3/msIO/general.py +55 -0
  41. msio-1.0.3/msIO/gui/__init__.py +0 -0
  42. msio-1.0.3/msIO/gui/_gui_log.py +32 -0
  43. msio-1.0.3/msIO/gui/about +16 -0
  44. msio-1.0.3/msIO/gui/annotation/__init__.py +3 -0
  45. msio-1.0.3/msIO/gui/annotation/annotations.py +169 -0
  46. msio-1.0.3/msIO/gui/annotation/app.py +916 -0
  47. msio-1.0.3/msIO/gui/annotation/common.py +694 -0
  48. msio-1.0.3/msIO/gui/annotation/database_panel.py +51 -0
  49. msio-1.0.3/msIO/gui/annotation/dialogs.py +688 -0
  50. msio-1.0.3/msIO/gui/annotation/export.py +105 -0
  51. msio-1.0.3/msIO/gui/annotation/filter_panel.py +398 -0
  52. msio-1.0.3/msIO/gui/annotation/matching.py +294 -0
  53. msio-1.0.3/msIO/gui/annotation/matching_panel.py +99 -0
  54. msio-1.0.3/msIO/gui/annotation/overview_panel.py +367 -0
  55. msio-1.0.3/msIO/gui/annotation/results_panel.py +400 -0
  56. msio-1.0.3/msIO/gui/annotation/scatter_panel.py +898 -0
  57. msio-1.0.3/msIO/gui/annotation/series.py +334 -0
  58. msio-1.0.3/msIO/gui/annotation/shortcuts.py +213 -0
  59. msio-1.0.3/msIO/gui/csv_to_sqlite_gui.py +266 -0
  60. msio-1.0.3/msIO/gui/feature_overview.py +467 -0
  61. msio-1.0.3/msIO/gui/find_lib_matches.py +13 -0
  62. msio-1.0.3/msIO/gui/library_search_by_name.py +222 -0
  63. msio-1.0.3/msIO/gui/main.py +85 -0
  64. msio-1.0.3/msIO/gui/msp_to_sqlite_gui.py +142 -0
  65. msio-1.0.3/msIO/list_of_ions/__init__.py +0 -0
  66. msio-1.0.3/msIO/list_of_ions/base.py +299 -0
  67. msio-1.0.3/msIO/list_of_ions/read_library.py +76 -0
  68. msio-1.0.3/msIO/list_of_ions/read_mca.py +237 -0
  69. msio-1.0.3/msIO/list_of_ions/read_mgf.py +161 -0
  70. msio-1.0.3/msIO/list_of_ions/read_msp.py +281 -0
  71. msio-1.0.3/msIO/metrics.py +72 -0
  72. msio-1.0.3/msIO/sql/__init__.py +0 -0
  73. msio-1.0.3/msIO/sql/create_library.py +151 -0
  74. msio-1.0.3/msIO/sql/session.py +32 -0
  75. msio-1.0.3/msIO/sql/visualize.py +61 -0
  76. msio-1.0.3/msIO.egg-info/PKG-INFO +151 -0
  77. msio-1.0.3/msIO.egg-info/SOURCES.txt +81 -0
  78. msio-1.0.3/msIO.egg-info/dependency_links.txt +1 -0
  79. msio-1.0.3/msIO.egg-info/requires.txt +8 -0
  80. msio-1.0.3/msIO.egg-info/top_level.txt +1 -0
  81. msio-1.0.3/pyproject.toml +3 -0
  82. msio-1.0.3/setup.cfg +4 -0
  83. msio-1.0.3/setup.py +39 -0
msio-1.0.3/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2026] [Yannick Zander]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
msio-1.0.3/PKG-INFO ADDED
@@ -0,0 +1,151 @@
1
+ Metadata-Version: 2.4
2
+ Name: msIO
3
+ Version: 1.0.3
4
+ Summary: Reading and writing functions for different mass spectrometry related file types
5
+ Home-page: https://github.com/yaza11/msIO
6
+ Author: Yannick Zander
7
+ License: MIT
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Topic :: Scientific/Engineering :: Chemistry
11
+ Requires-Python: >=3.10
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: numpy
15
+ Requires-Dist: matplotlib
16
+ Requires-Dist: pandas
17
+ Requires-Dist: tqdm
18
+ Requires-Dist: sqlalchemy
19
+ Requires-Dist: networkx
20
+ Requires-Dist: matchms
21
+ Requires-Dist: rdkit
22
+ Dynamic: author
23
+ Dynamic: classifier
24
+ Dynamic: description
25
+ Dynamic: description-content-type
26
+ Dynamic: home-page
27
+ Dynamic: license
28
+ Dynamic: license-file
29
+ Dynamic: requires-dist
30
+ Dynamic: requires-python
31
+ Dynamic: summary
32
+
33
+ # msIO
34
+
35
+ msIO reads mass spectrometry data from several tools, combines it into one SQLite database, and provides a GUI for matching measured features against spectral libraries and annotating them.
36
+
37
+ ## Features
38
+
39
+ - **Readers** for MS data formats and tool exports:
40
+ - MGF (MS1/MS2 spectra)
41
+ - MSP spectral libraries
42
+ - MetaboScape feature tables and `.mca` annotation files
43
+ - MZmine feature tables and `.graphml` networks
44
+ - GNPS molecular networks
45
+ - SIRIUS summaries (formula, compound and CANOPUS identifications)
46
+ - **SQLite storage**: combine a feature table with MGF, GNPS and SIRIUS results into one database. MSP libraries can be turned into library databases too.
47
+ - **Annotation GUI**: match features against one or more libraries (precursor m/z, MS2 similarity), filter the results, look at MS1/MS2 spectra and structures, and annotate features, peaks, adducts, isomers and series.
48
+
49
+ ## Installation
50
+
51
+ Python 3.10 or newer is needed. msIO needs [LipidCalculator](https://github.com/yaza11/LipidCalculator), which is not on PyPI yet and has to be installed first:
52
+
53
+ ```bash
54
+ pip install git+https://github.com/yaza11/LipidCalculator.git
55
+ pip install msIO
56
+ ```
57
+
58
+ The latest development version can be installed from GitHub with `pip install git+https://github.com/yaza11/msIO.git` instead.
59
+
60
+ For Windows, there is also a standalone GUI that needs no Python installation: download `msIO_GUI-windows.zip` from the [releases](https://github.com/yaza11/msIO/releases), extract it and run `msIO_GUI.exe`.
61
+
62
+ ## Graphical user interface
63
+
64
+ Start the GUI with:
65
+
66
+ ```bash
67
+ python -m msIO.gui.main
68
+ ```
69
+
70
+ The window has these tabs:
71
+
72
+ | Tab | Purpose |
73
+ | --- | --- |
74
+ | MZmine/MetaboScape → SQLite | Combine a feature table with MGF, GNPS and SIRIUS output into a measured-feature database |
75
+ | MSP library → SQLite | Convert one or more `.msp` libraries into a library database |
76
+ | Library viewer | Search a library database by compound name and plot entries |
77
+ | Annotation | Match measured features against libraries and annotate them |
78
+ | Guide | A suggested strategy for making annotations |
79
+
80
+ You can also run each tab on its own by running its module in `msIO/gui/`.
81
+
82
+ [docs/GUI_features.md](docs/GUI_features.md) explains the annotation GUI in detail, with screenshots.
83
+
84
+ ### Standalone executable
85
+
86
+ Ready-built Windows executables are attached to the [releases](https://github.com/yaza11/msIO/releases). To build one yourself (no Python needed to run it), install PyInstaller in the environment where msIO and its dependencies are installed and run:
87
+
88
+ ```bash
89
+ pyinstaller --noconfirm --clean LipidAnnotations.spec
90
+ ```
91
+
92
+ The build goes to `dist/msIO_GUI/`. The GitHub workflow builds the releases from the same spec file.
93
+
94
+ ## Python usage
95
+
96
+ ### Build a measured-feature database
97
+
98
+ ```python
99
+ from msIO import MgfImportManager
100
+ from msIO.feature_managers.combined import ProjectImportManager
101
+ from msIO.feature_managers.gnps import GnpsImportManager
102
+ from msIO.feature_managers.metaboscape import MetaboscapeImportManager
103
+ from msIO.feature_managers.sirius import SiriusImportManager
104
+ from msIO.sql.session import initiate_db
105
+
106
+ manager = ProjectImportManager(
107
+ metaboscape_manager=MetaboscapeImportManager("height.csv"),
108
+ mgf_manager=MgfImportManager("ms1_ms2.mgf", accumulate_spectra=False),
109
+ gnps_manager=GnpsImportManager("gnps"),
110
+ sirius_manager=SiriusImportManager(path_folder_export="sirius"),
111
+ )
112
+
113
+ initiate_db("features.sqlite")
114
+ manager.to_sql("features.sqlite", feature_ids=manager.feature_ids)
115
+ ```
116
+
117
+ ### Build a library database from MSP files
118
+
119
+ ```python
120
+ from msIO.sql.create_library import write_lib_from_msp_files
121
+
122
+ write_lib_from_msp_files("library.sqlite", ["lib1.msp", "lib2.msp"])
123
+ ```
124
+
125
+ ### Query databases
126
+
127
+ ```python
128
+ from msIO.feature_managers.db import FeatureManagerDB, Library
129
+
130
+ features = FeatureManagerDB("features.sqlite")
131
+ spectrum = features.get_ms_spectrum(features.feature_ids[0], level=2)
132
+
133
+ lib = Library("library.sqlite")
134
+ entry_id = lib.find_by_name("OH-GTGT-0a")[0]
135
+ lib.plot_compound_overview(entry_id)
136
+ ```
137
+
138
+ ## Package layout
139
+
140
+ | Module | Contents |
141
+ | --- | --- |
142
+ | `msIO.list_of_ions` | Readers for peak lists: MGF, MSP, MCA and library spectra |
143
+ | `msIO.features` | Feature classes for each data source |
144
+ | `msIO.feature_managers` | Import managers for each source, plus database access (`FeatureManagerDB`, `Library`) |
145
+ | `msIO.sql` | SQLAlchemy schema, sessions and library creation |
146
+ | `msIO.annotations` | Annotation handling |
147
+ | `msIO.gui` | Tkinter GUIs |
148
+
149
+ ## License
150
+
151
+ See [LICENSE](LICENSE).
msio-1.0.3/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # msIO
2
+
3
+ msIO reads mass spectrometry data from several tools, combines it into one SQLite database, and provides a GUI for matching measured features against spectral libraries and annotating them.
4
+
5
+ ## Features
6
+
7
+ - **Readers** for MS data formats and tool exports:
8
+ - MGF (MS1/MS2 spectra)
9
+ - MSP spectral libraries
10
+ - MetaboScape feature tables and `.mca` annotation files
11
+ - MZmine feature tables and `.graphml` networks
12
+ - GNPS molecular networks
13
+ - SIRIUS summaries (formula, compound and CANOPUS identifications)
14
+ - **SQLite storage**: combine a feature table with MGF, GNPS and SIRIUS results into one database. MSP libraries can be turned into library databases too.
15
+ - **Annotation GUI**: match features against one or more libraries (precursor m/z, MS2 similarity), filter the results, look at MS1/MS2 spectra and structures, and annotate features, peaks, adducts, isomers and series.
16
+
17
+ ## Installation
18
+
19
+ Python 3.10 or newer is needed. msIO needs [LipidCalculator](https://github.com/yaza11/LipidCalculator), which is not on PyPI yet and has to be installed first:
20
+
21
+ ```bash
22
+ pip install git+https://github.com/yaza11/LipidCalculator.git
23
+ pip install msIO
24
+ ```
25
+
26
+ The latest development version can be installed from GitHub with `pip install git+https://github.com/yaza11/msIO.git` instead.
27
+
28
+ For Windows, there is also a standalone GUI that needs no Python installation: download `msIO_GUI-windows.zip` from the [releases](https://github.com/yaza11/msIO/releases), extract it and run `msIO_GUI.exe`.
29
+
30
+ ## Graphical user interface
31
+
32
+ Start the GUI with:
33
+
34
+ ```bash
35
+ python -m msIO.gui.main
36
+ ```
37
+
38
+ The window has these tabs:
39
+
40
+ | Tab | Purpose |
41
+ | --- | --- |
42
+ | MZmine/MetaboScape → SQLite | Combine a feature table with MGF, GNPS and SIRIUS output into a measured-feature database |
43
+ | MSP library → SQLite | Convert one or more `.msp` libraries into a library database |
44
+ | Library viewer | Search a library database by compound name and plot entries |
45
+ | Annotation | Match measured features against libraries and annotate them |
46
+ | Guide | A suggested strategy for making annotations |
47
+
48
+ You can also run each tab on its own by running its module in `msIO/gui/`.
49
+
50
+ [docs/GUI_features.md](docs/GUI_features.md) explains the annotation GUI in detail, with screenshots.
51
+
52
+ ### Standalone executable
53
+
54
+ Ready-built Windows executables are attached to the [releases](https://github.com/yaza11/msIO/releases). To build one yourself (no Python needed to run it), install PyInstaller in the environment where msIO and its dependencies are installed and run:
55
+
56
+ ```bash
57
+ pyinstaller --noconfirm --clean LipidAnnotations.spec
58
+ ```
59
+
60
+ The build goes to `dist/msIO_GUI/`. The GitHub workflow builds the releases from the same spec file.
61
+
62
+ ## Python usage
63
+
64
+ ### Build a measured-feature database
65
+
66
+ ```python
67
+ from msIO import MgfImportManager
68
+ from msIO.feature_managers.combined import ProjectImportManager
69
+ from msIO.feature_managers.gnps import GnpsImportManager
70
+ from msIO.feature_managers.metaboscape import MetaboscapeImportManager
71
+ from msIO.feature_managers.sirius import SiriusImportManager
72
+ from msIO.sql.session import initiate_db
73
+
74
+ manager = ProjectImportManager(
75
+ metaboscape_manager=MetaboscapeImportManager("height.csv"),
76
+ mgf_manager=MgfImportManager("ms1_ms2.mgf", accumulate_spectra=False),
77
+ gnps_manager=GnpsImportManager("gnps"),
78
+ sirius_manager=SiriusImportManager(path_folder_export="sirius"),
79
+ )
80
+
81
+ initiate_db("features.sqlite")
82
+ manager.to_sql("features.sqlite", feature_ids=manager.feature_ids)
83
+ ```
84
+
85
+ ### Build a library database from MSP files
86
+
87
+ ```python
88
+ from msIO.sql.create_library import write_lib_from_msp_files
89
+
90
+ write_lib_from_msp_files("library.sqlite", ["lib1.msp", "lib2.msp"])
91
+ ```
92
+
93
+ ### Query databases
94
+
95
+ ```python
96
+ from msIO.feature_managers.db import FeatureManagerDB, Library
97
+
98
+ features = FeatureManagerDB("features.sqlite")
99
+ spectrum = features.get_ms_spectrum(features.feature_ids[0], level=2)
100
+
101
+ lib = Library("library.sqlite")
102
+ entry_id = lib.find_by_name("OH-GTGT-0a")[0]
103
+ lib.plot_compound_overview(entry_id)
104
+ ```
105
+
106
+ ## Package layout
107
+
108
+ | Module | Contents |
109
+ | --- | --- |
110
+ | `msIO.list_of_ions` | Readers for peak lists: MGF, MSP, MCA and library spectra |
111
+ | `msIO.features` | Feature classes for each data source |
112
+ | `msIO.feature_managers` | Import managers for each source, plus database access (`FeatureManagerDB`, `Library`) |
113
+ | `msIO.sql` | SQLAlchemy schema, sessions and library creation |
114
+ | `msIO.annotations` | Annotation handling |
115
+ | `msIO.gui` | Tkinter GUIs |
116
+
117
+ ## License
118
+
119
+ See [LICENSE](LICENSE).
@@ -0,0 +1,15 @@
1
+ from importlib.util import find_spec as _find_spec
2
+
3
+ from ._version import __version__
4
+
5
+ # LipidCalculator is not on PyPI yet, so `pip install msIO` can't pull it in: fail with instructions instead of an
6
+ # ImportError deep inside msIO. find_spec only looks the package up without importing it (LipidCalculator imports msIO).
7
+ if _find_spec("LipidCalculator") is None:
8
+ raise ImportError(
9
+ "msIO needs LipidCalculator, install it with\n"
10
+ " pip install git+https://github.com/yaza11/LipidCalculator.git"
11
+ )
12
+
13
+ from .list_of_ions.base import PeakList
14
+ from .list_of_ions.read_mgf import MgfImportManager
15
+ from .list_of_ions.read_msp import MSPReader
@@ -0,0 +1,12 @@
1
+ """
2
+ Defining the right relationship patterns is crucial for the efficent functioning of the database.
3
+
4
+ We distinguish the types
5
+ - one to many (parent can have many children)
6
+ - many to one (
7
+ - many to many
8
+
9
+ We use the declarative form and we use Mapped on type annotations (so we don't need to specify all parameters in
10
+ mapped column explicitly
11
+ """
12
+
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import List
4
+
5
+ from sqlalchemy import Column
6
+ from sqlalchemy import Table
7
+ from sqlalchemy import ForeignKey
8
+ from sqlalchemy import Integer
9
+ from sqlalchemy.orm import Mapped
10
+ from sqlalchemy.orm import mapped_column
11
+ from sqlalchemy.orm import DeclarativeBase
12
+ from sqlalchemy.orm import relationship
13
+
14
+
15
+ class Base(DeclarativeBase):
16
+ pass
17
+
18
+
19
+ # note for a Core table, we use the sqlalchemy.Column construct,
20
+ # not sqlalchemy.orm.mapped_column
21
+ association_table = Table(
22
+ "association_table",
23
+ Base.metadata,
24
+ Column("left_id", ForeignKey("left_table.id")),
25
+ Column("right_id", ForeignKey("right_table.id")),
26
+ )
27
+
28
+
29
+ class Parent(Base):
30
+ __tablename__ = "left_table"
31
+
32
+ id: Mapped[int] = mapped_column(primary_key=True)
33
+ children: Mapped[List[Child]] = relationship(secondary=association_table)
34
+
35
+
36
+ class Child(Base):
37
+ __tablename__ = "right_table"
38
+
39
+ id: Mapped[int] = mapped_column(primary_key=True)
@@ -0,0 +1,43 @@
1
+ """also define back_populates"""
2
+ from __future__ import annotations
3
+
4
+ from typing import List
5
+
6
+ from sqlalchemy import Column
7
+ from sqlalchemy import Table
8
+ from sqlalchemy import ForeignKey
9
+ from sqlalchemy import Integer
10
+ from sqlalchemy.orm import Mapped
11
+ from sqlalchemy.orm import mapped_column
12
+ from sqlalchemy.orm import DeclarativeBase
13
+ from sqlalchemy.orm import relationship
14
+
15
+
16
+ class Base(DeclarativeBase):
17
+ pass
18
+
19
+
20
+ association_table = Table(
21
+ "association_table",
22
+ Base.metadata,
23
+ Column("left_id", ForeignKey("left_table.id"), primary_key=True),
24
+ Column("right_id", ForeignKey("right_table.id"), primary_key=True),
25
+ )
26
+
27
+
28
+ class Parent(Base):
29
+ __tablename__ = "left_table"
30
+
31
+ id: Mapped[int] = mapped_column(primary_key=True)
32
+ children: Mapped[List[Child]] = relationship(
33
+ secondary=association_table, back_populates="parents"
34
+ )
35
+
36
+
37
+ class Child(Base):
38
+ __tablename__ = "right_table"
39
+
40
+ id: Mapped[int] = mapped_column(primary_key=True)
41
+ parents: Mapped[List[Parent]] = relationship(
42
+ secondary=association_table, back_populates="children"
43
+ )
@@ -0,0 +1,22 @@
1
+ from typing import Optional
2
+
3
+ from sqlalchemy import ForeignKey
4
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
5
+
6
+ class Base:
7
+ pass
8
+
9
+
10
+ class Parent(Base):
11
+ __tablename__ = "parent_table"
12
+
13
+ id: Mapped[int] = mapped_column(primary_key=True)
14
+ # place foreign key in parent table
15
+ child_id: Mapped[Optional[int]] = mapped_column(ForeignKey("child_table.id"))
16
+ child: Mapped[Optional["Child"]] = relationship()
17
+
18
+
19
+ class Child(Base):
20
+ __tablename__ = "child_table"
21
+
22
+ id: Mapped[int] = mapped_column(primary_key=True)
@@ -0,0 +1,23 @@
1
+ """this is the same as one to many bidirectional with the names of Parent and child swapped"""
2
+ from typing import List
3
+
4
+ from sqlalchemy import ForeignKey
5
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
6
+
7
+ class Base:
8
+ pass
9
+
10
+
11
+ class Parent(Base):
12
+ __tablename__ = "parent_table"
13
+
14
+ id: Mapped[int] = mapped_column(primary_key=True)
15
+ child_id: Mapped[int] = mapped_column(ForeignKey("child_table.id"))
16
+ child: Mapped["Child"] = relationship(back_populates="parents")
17
+
18
+
19
+ class Child(Base):
20
+ __tablename__ = "child_table"
21
+
22
+ id: Mapped[int] = mapped_column(primary_key=True)
23
+ parents: Mapped[List["Parent"]] = relationship(back_populates="child")
@@ -0,0 +1,33 @@
1
+ """
2
+ (parent can have many children)
3
+
4
+ need to choose delete behavior for children
5
+
6
+ """
7
+ from typing import List
8
+
9
+ from sqlalchemy import ForeignKey
10
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
11
+
12
+ class Base:
13
+ pass
14
+
15
+
16
+ class Parent(Base):
17
+ __tablename__ = "parent_table"
18
+
19
+ id: Mapped[int] = mapped_column(primary_key=True)
20
+ children: Mapped[List["Child"]] = relationship()
21
+ # or if children should be deleted if parent is deleted
22
+ children: Mapped[List["Child"]] = relationship(
23
+ cascade="all, delete-orphan",
24
+ passive_deletes=True
25
+ )
26
+
27
+
28
+ class Child(Base):
29
+ __tablename__ = "child_table"
30
+
31
+ id: Mapped[int] = mapped_column(primary_key=True)
32
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
33
+
@@ -0,0 +1,32 @@
1
+ """
2
+ (parent can have many children)
3
+
4
+ need to choose delete behavior for children
5
+
6
+ for bidirectional, the child to parent relationship is many to one
7
+ """
8
+ from typing import List
9
+
10
+ from sqlalchemy import ForeignKey
11
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
12
+
13
+ class Base:
14
+ pass
15
+
16
+
17
+ class Parent(Base):
18
+ # this is "one"
19
+ __tablename__ = "parent_table"
20
+
21
+ id: Mapped[int] = mapped_column(primary_key=True)
22
+ children: Mapped[List["Child"]] = relationship(back_populates="parent") # could use Set here instead of List
23
+
24
+
25
+ class Child(Base):
26
+ # this is "many"
27
+ __tablename__ = "child_table"
28
+
29
+ id: Mapped[int] = mapped_column(primary_key=True)
30
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
31
+ parent: Mapped["Parent"] = relationship(back_populates="children")
32
+
@@ -0,0 +1,26 @@
1
+ """same as one to many bidirectional but we use Mapped[Child] instead of Mapped[List[Child]]"""
2
+ from sqlalchemy import ForeignKey, UniqueConstraint
3
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
4
+
5
+
6
+ class Base:
7
+ pass
8
+
9
+ class Parent(Base):
10
+ __tablename__ = "parent_table"
11
+
12
+ id: Mapped[int] = mapped_column(primary_key=True)
13
+ child: Mapped["Child"] = relationship(back_populates="parent")
14
+
15
+
16
+ class Child(Base):
17
+ __tablename__ = "child_table"
18
+
19
+ id: Mapped[int] = mapped_column(primary_key=True)
20
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
21
+ parent: Mapped["Parent"] = relationship(back_populates="child")
22
+ # we can enforece single parent like this (otherwise it will not be detected)
23
+ parent: Mapped["Parent"] = relationship(back_populates="child", single_parent = True)
24
+
25
+ # should always use this
26
+ __table_args__ = (UniqueConstraint("parent_id"),)
@@ -0,0 +1,65 @@
1
+ from typing import List
2
+
3
+ from sqlalchemy import ForeignKey
4
+ from sqlalchemy.orm import Mapped
5
+ from sqlalchemy.orm import mapped_column
6
+ from sqlalchemy.orm import DeclarativeBase
7
+ from sqlalchemy.orm import relationship
8
+
9
+
10
+ class Base(DeclarativeBase):
11
+ pass
12
+
13
+
14
+ # %% One to many
15
+ # (One parent, many children)
16
+
17
+ # class Parent(Base):
18
+ # __tablename__ = "parent_table"
19
+ #
20
+ # id: Mapped[int] = mapped_column(primary_key=True)
21
+ # children: Mapped[List["Child"]] = relationship()
22
+ #
23
+ #
24
+ # class Child(Base):
25
+ # __tablename__ = "child_table"
26
+ #
27
+ # id: Mapped[int] = mapped_column(primary_key=True)
28
+ # parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
29
+
30
+
31
+ # %%% bidirectional
32
+
33
+ class Parent(Base):
34
+ __tablename__ = "parent_table"
35
+
36
+ id: Mapped[int] = mapped_column(primary_key=True)
37
+ children: Mapped[List["Child"]] = relationship(back_populates="parent")
38
+
39
+
40
+ class Child(Base):
41
+ __tablename__ = "child_table"
42
+
43
+ id: Mapped[int] = mapped_column(primary_key=True)
44
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
45
+ parent: Mapped["Parent"] = relationship(back_populates="children")
46
+
47
+ # %%% one to one
48
+
49
+ class Parent(Base):
50
+ __tablename__ = "parent_table"
51
+
52
+ id: Mapped[int] = mapped_column(primary_key=True)
53
+ child: Mapped["Child"] = relationship(back_populates="parent")
54
+
55
+
56
+ class Child(Base):
57
+ __tablename__ = "child_table"
58
+
59
+ id: Mapped[int] = mapped_column(primary_key=True)
60
+ parent_id: Mapped[int] = mapped_column(ForeignKey("parent_table.id"))
61
+ parent: Mapped["Parent"] = relationship(back_populates="child")
62
+
63
+ c = Child()
64
+
65
+ p = Parent(child=c)
@@ -0,0 +1,2 @@
1
+ # single place of the version: read by setup.py and shown in the GUI title
2
+ __version__ = '1.0.3'
File without changes