sapiopycommons 2024.11.11a364__py3-none-any.whl → 2024.11.18a366__py3-none-any.whl
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.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/callbacks/callback_util.py +532 -83
- sapiopycommons/callbacks/field_builder.py +537 -0
- sapiopycommons/chem/IndigoMolecules.py +2 -0
- sapiopycommons/chem/Molecules.py +77 -18
- sapiopycommons/customreport/__init__.py +0 -0
- sapiopycommons/customreport/column_builder.py +60 -0
- sapiopycommons/customreport/custom_report_builder.py +130 -0
- sapiopycommons/customreport/term_builder.py +299 -0
- sapiopycommons/datatype/attachment_util.py +11 -10
- sapiopycommons/datatype/data_fields.py +61 -0
- sapiopycommons/datatype/pseudo_data_types.py +440 -0
- sapiopycommons/eln/experiment_handler.py +272 -70
- sapiopycommons/eln/experiment_report_util.py +653 -0
- sapiopycommons/files/complex_data_loader.py +5 -4
- sapiopycommons/files/file_bridge.py +31 -24
- sapiopycommons/files/file_bridge_handler.py +340 -0
- sapiopycommons/files/file_data_handler.py +2 -5
- sapiopycommons/files/file_util.py +59 -9
- sapiopycommons/files/file_validator.py +92 -6
- sapiopycommons/files/file_writer.py +44 -15
- sapiopycommons/flowcyto/flow_cyto.py +77 -0
- sapiopycommons/flowcyto/flowcyto_data.py +75 -0
- sapiopycommons/general/accession_service.py +375 -0
- sapiopycommons/general/aliases.py +207 -6
- sapiopycommons/general/audit_log.py +189 -0
- sapiopycommons/general/custom_report_util.py +212 -37
- sapiopycommons/general/exceptions.py +21 -8
- sapiopycommons/general/popup_util.py +21 -0
- sapiopycommons/general/sapio_links.py +50 -0
- sapiopycommons/general/time_util.py +8 -2
- sapiopycommons/multimodal/multimodal.py +146 -0
- sapiopycommons/multimodal/multimodal_data.py +490 -0
- sapiopycommons/processtracking/custom_workflow_handler.py +406 -0
- sapiopycommons/processtracking/endpoints.py +22 -22
- sapiopycommons/recordmodel/record_handler.py +481 -97
- sapiopycommons/rules/eln_rule_handler.py +34 -25
- sapiopycommons/rules/on_save_rule_handler.py +34 -31
- sapiopycommons/sftpconnect/__init__.py +0 -0
- sapiopycommons/sftpconnect/sftp_builder.py +69 -0
- sapiopycommons/webhook/webhook_context.py +39 -0
- sapiopycommons/webhook/webhook_handlers.py +201 -42
- sapiopycommons/webhook/webservice_handlers.py +67 -0
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/METADATA +5 -2
- sapiopycommons-2024.11.18a366.dist-info/RECORD +59 -0
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/WHEEL +1 -1
- sapiopycommons-2024.11.11a364.dist-info/RECORD +0 -38
- {sapiopycommons-2024.11.11a364.dist-info → sapiopycommons-2024.11.18a366.dist-info}/licenses/LICENSE +0 -0
sapiopycommons/chem/Molecules.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# Author Yechen Qiao
|
|
2
2
|
# Common Molecule Utilities for Molecule Transfers with Sapio
|
|
3
|
+
from typing import cast
|
|
3
4
|
|
|
4
5
|
from rdkit import Chem
|
|
5
6
|
from rdkit.Chem import Crippen, MolToInchi
|
|
@@ -20,6 +21,25 @@ tautomer_params.tautomerReassignStereo = False
|
|
|
20
21
|
tautomer_params.tautomerRemoveIsotopicHs = True
|
|
21
22
|
enumerator = rdMolStandardize.TautomerEnumerator(tautomer_params)
|
|
22
23
|
|
|
24
|
+
|
|
25
|
+
def get_enhanced_stereo_reg_hash(mol: Mol, enhanced_stereo: bool) -> str:
|
|
26
|
+
"""
|
|
27
|
+
Get the Registration Hash for the molecule by the current registration configuration.
|
|
28
|
+
When we are running if we are canonicalization of tautomers or cleaning up any other way, do they first before calling.
|
|
29
|
+
:param mol: The molecule to obtain hash for.
|
|
30
|
+
:param canonical_tautomer: Whether the registry system canonicalize the tautomers.
|
|
31
|
+
:param enhanced_stereo: Whether we are computing enhanced stereo at all.
|
|
32
|
+
:return: The enhanced stereo hash.
|
|
33
|
+
"""
|
|
34
|
+
if enhanced_stereo:
|
|
35
|
+
from rdkit.Chem.RegistrationHash import GetMolLayers, GetMolHash, HashScheme
|
|
36
|
+
layers = GetMolLayers(mol, enable_tautomer_hash_v2=True)
|
|
37
|
+
hash_scheme: HashScheme = HashScheme.TAUTOMER_INSENSITIVE_LAYERS
|
|
38
|
+
return GetMolHash(layers, hash_scheme=hash_scheme)
|
|
39
|
+
else:
|
|
40
|
+
return ""
|
|
41
|
+
|
|
42
|
+
|
|
23
43
|
def neutralize_atoms(mol) -> Mol:
|
|
24
44
|
"""
|
|
25
45
|
Neutralize atoms per https://baoilleach.blogspot.com/2019/12/no-charge-simple-approach-to.html
|
|
@@ -86,7 +106,6 @@ def mol_to_img(mol_str: str) -> str:
|
|
|
86
106
|
return renderer.renderToString(mol)
|
|
87
107
|
|
|
88
108
|
|
|
89
|
-
|
|
90
109
|
def mol_to_sapio_partial_pojo(mol: Mol):
|
|
91
110
|
"""
|
|
92
111
|
Get the minimum information about molecule to Sapio, just its SMILES, V3000, and image data.
|
|
@@ -96,7 +115,7 @@ def mol_to_sapio_partial_pojo(mol: Mol):
|
|
|
96
115
|
Chem.SanitizeMol(mol)
|
|
97
116
|
mol.UpdatePropertyCache()
|
|
98
117
|
smiles = Chem.MolToSmiles(mol)
|
|
99
|
-
molBlock = Chem.MolToMolBlock(mol)
|
|
118
|
+
molBlock = Chem.MolToMolBlock(mol, forceV3000=True)
|
|
100
119
|
img = mol_to_img(mol)
|
|
101
120
|
molecule = dict()
|
|
102
121
|
molecule["smiles"] = smiles
|
|
@@ -105,23 +124,52 @@ def mol_to_sapio_partial_pojo(mol: Mol):
|
|
|
105
124
|
return molecule
|
|
106
125
|
|
|
107
126
|
|
|
108
|
-
def
|
|
127
|
+
def get_cxs_smiles_hash(mol: Mol, enhanced_stereo: bool) -> str:
|
|
128
|
+
"""
|
|
129
|
+
Return the SHA1 CXS Smiles hash for the canonical, isomeric CXS SMILES of the molecule.
|
|
130
|
+
"""
|
|
131
|
+
if not enhanced_stereo:
|
|
132
|
+
return ""
|
|
133
|
+
import hashlib
|
|
134
|
+
return hashlib.sha1(Chem.MolToCXSmiles(mol, canonical=True, isomericSmiles=True).encode()).hexdigest()
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def get_has_or_group(mol: Mol, enhanced_stereo: bool) -> bool:
|
|
138
|
+
"""
|
|
139
|
+
Return true if and only if: enhanced stereochemistry is enabled and there is at least one OR group in mol.
|
|
140
|
+
"""
|
|
141
|
+
if not enhanced_stereo:
|
|
142
|
+
return False
|
|
143
|
+
from rdkit.Chem import StereoGroup_vect, STEREO_OR
|
|
144
|
+
stereo_groups: StereoGroup_vect = mol.GetStereoGroups()
|
|
145
|
+
for stereo_group in stereo_groups:
|
|
146
|
+
if stereo_group.GetGroupType() == STEREO_OR:
|
|
147
|
+
return True
|
|
148
|
+
return False
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def mol_to_sapio_substance(mol: Mol, include_stereoisomers=False,
|
|
109
152
|
normalize: bool = False, remove_salt: bool = False, make_images: bool = False,
|
|
110
|
-
salt_def: str | None = None, canonical_tautomer: bool = True
|
|
153
|
+
salt_def: str | None = None, canonical_tautomer: bool = True,
|
|
154
|
+
enhanced_stereo: bool = False, remove_atom_map: bool = True):
|
|
111
155
|
"""
|
|
112
156
|
Convert a molecule in RDKit to a molecule POJO in Sapio.
|
|
113
157
|
|
|
114
158
|
:param mol: The molecule in RDKit.
|
|
115
|
-
:param include_stereoisomers: If true, will compute all stereoisomer permutations of this molecule.
|
|
116
159
|
:param normalize If true, will normalize the functional groups and return normalized result.
|
|
117
160
|
:param remove_salt If true, we will remove salts iteratively from the molecule before returning their data.
|
|
118
161
|
We will also populate desaltedList with molecules we deleted.
|
|
162
|
+
:param make_images Whether to make images as part of the result without having another script to resolve it.
|
|
119
163
|
:param salt_def: if not none, specifies custom salt to be used during the desalt process.
|
|
120
164
|
:param canonical_tautomer: if True, we will attempt to compute canonical tautomer for the molecule. Slow!
|
|
121
165
|
This is needed for a registry. Note it stops after enumeration of 1000.
|
|
166
|
+
:param enhanced_stereo: If enabled, enhanced stereo hash will be produced.
|
|
167
|
+
:param remove_atom_map: When set, clear all atom AAM maps that were set had it been merged into some reactions earlier.
|
|
122
168
|
:return: The molecule POJO for Sapio.
|
|
123
169
|
"""
|
|
124
170
|
molecule = dict()
|
|
171
|
+
if remove_atom_map:
|
|
172
|
+
[a.SetAtomMapNum(0) for a in mol.GetAtoms()]
|
|
125
173
|
Chem.SanitizeMol(mol)
|
|
126
174
|
mol.UpdatePropertyCache()
|
|
127
175
|
Chem.GetSymmSSSR(mol)
|
|
@@ -157,7 +205,7 @@ def mol_to_sapio_substance(mol: Mol, include_stereoisomers: bool = False,
|
|
|
157
205
|
exactMass = Descriptors.ExactMolWt(mol)
|
|
158
206
|
molFormula = rdMolDescriptors.CalcMolFormula(mol)
|
|
159
207
|
charge = Chem.GetFormalCharge(mol)
|
|
160
|
-
molBlock = Chem.MolToMolBlock(mol)
|
|
208
|
+
molBlock = Chem.MolToMolBlock(mol, forceV3000=True)
|
|
161
209
|
|
|
162
210
|
molecule["cLogP"] = cLogP
|
|
163
211
|
molecule["tpsa"] = tpsa
|
|
@@ -181,27 +229,38 @@ def mol_to_sapio_substance(mol: Mol, include_stereoisomers: bool = False,
|
|
|
181
229
|
# We need to test the INCHI can be loaded back to indigo.
|
|
182
230
|
indigo_mol = indigo.loadMolecule(molBlock)
|
|
183
231
|
indigo_mol.aromatize()
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
232
|
+
if enhanced_stereo:
|
|
233
|
+
# Remove enhanced stereo layer when generating InChI as the stereo hash is generated separately for reg.
|
|
234
|
+
mol_copy: Mol = Chem.Mol(mol)
|
|
235
|
+
Chem.CanonicalizeEnhancedStereo(mol_copy)
|
|
236
|
+
molecule["inchi"] = Chem.MolToInchi(mol_copy)
|
|
237
|
+
molecule["inchiKey"] = Chem.MolToInchiKey(mol_copy)
|
|
238
|
+
else:
|
|
239
|
+
indigo_inchi.resetOptions()
|
|
240
|
+
indigo_inchi_str = indigo_inchi.getInchi(indigo_mol)
|
|
241
|
+
molecule["inchi"] = indigo_inchi_str
|
|
242
|
+
indigo_inchi_key_str = indigo_inchi.getInchiKey(indigo_inchi_str)
|
|
243
|
+
molecule["inchiKey"] = indigo_inchi_key_str
|
|
188
244
|
molecule["smiles"] = indigo_mol.smiles()
|
|
245
|
+
molecule["reg_hash"] = get_enhanced_stereo_reg_hash(mol, enhanced_stereo=enhanced_stereo)
|
|
246
|
+
molecule["cxsmiles_hash"] = get_cxs_smiles_hash(mol, enhanced_stereo=enhanced_stereo)
|
|
247
|
+
molecule["has_or_group"] = get_has_or_group(mol, enhanced_stereo=enhanced_stereo)
|
|
189
248
|
|
|
190
|
-
if include_stereoisomers and has_chiral_centers(mol):
|
|
191
|
-
stereoisomers = find_all_possible_stereoisomers(mol, only_unassigned=False, try_embedding=False, unique=True)
|
|
192
|
-
molecule["stereoisomers"] = [mol_to_sapio_partial_pojo(x) for x in stereoisomers]
|
|
193
249
|
return molecule
|
|
194
250
|
|
|
195
251
|
|
|
196
|
-
def mol_to_sapio_compound(mol: Mol, include_stereoisomers: bool = False,
|
|
252
|
+
def mol_to_sapio_compound(mol: Mol, include_stereoisomers=False, enhanced_stereo: bool = False,
|
|
197
253
|
salt_def: str | None = None, resolve_canonical: bool = True,
|
|
198
|
-
make_images: bool = False, canonical_tautomer: bool = True
|
|
254
|
+
make_images: bool = False, canonical_tautomer: bool = True,
|
|
255
|
+
remove_atom_map: bool = True):
|
|
199
256
|
ret = dict()
|
|
200
|
-
ret['originalMol'] = mol_to_sapio_substance(mol, include_stereoisomers,
|
|
257
|
+
ret['originalMol'] = mol_to_sapio_substance(mol, include_stereoisomers=False,
|
|
201
258
|
normalize=False, remove_salt=False, make_images=make_images,
|
|
202
|
-
canonical_tautomer=canonical_tautomer
|
|
259
|
+
canonical_tautomer=canonical_tautomer,
|
|
260
|
+
enhanced_stereo=enhanced_stereo, remove_atom_map=remove_atom_map)
|
|
203
261
|
if resolve_canonical:
|
|
204
262
|
ret['canonicalMol'] = mol_to_sapio_substance(mol, include_stereoisomers=False,
|
|
205
263
|
normalize=True, remove_salt=True, make_images=make_images,
|
|
206
|
-
salt_def=salt_def, canonical_tautomer=canonical_tautomer
|
|
264
|
+
salt_def=salt_def, canonical_tautomer=canonical_tautomer,
|
|
265
|
+
enhanced_stereo=enhanced_stereo, remove_atom_map=remove_atom_map)
|
|
207
266
|
return ret
|
|
File without changes
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from sapiopylib.rest.pojo.CustomReport import ReportColumn
|
|
2
|
+
from sapiopylib.rest.pojo.datatype.FieldDefinition import FieldType
|
|
3
|
+
|
|
4
|
+
from sapiopycommons.general.aliases import DataTypeIdentifier, FieldIdentifier, AliasUtil
|
|
5
|
+
from sapiopycommons.general.exceptions import SapioException
|
|
6
|
+
|
|
7
|
+
# The system fields that every record has and their field types. System fields aren't generated as record model fields
|
|
8
|
+
# for all platform version, hence the need to create a dict for them in the off chance that they're not present on
|
|
9
|
+
# the model wrapper.
|
|
10
|
+
SYSTEM_FIELDS: dict[str, FieldType] = {
|
|
11
|
+
"DataRecordName": FieldType.IDENTIFIER,
|
|
12
|
+
"RecordId": FieldType.LONG,
|
|
13
|
+
"DateCreated": FieldType.DATE,
|
|
14
|
+
"CreatedBy": FieldType.STRING,
|
|
15
|
+
"VeloxLastModifiedDate": FieldType.DATE,
|
|
16
|
+
"VeloxLastModifiedBy": FieldType.STRING
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ColumnBuilder:
|
|
21
|
+
"""
|
|
22
|
+
A class for building report columns for custom reports.
|
|
23
|
+
"""
|
|
24
|
+
@staticmethod
|
|
25
|
+
def build_column(data_type: DataTypeIdentifier, field: FieldIdentifier, field_type: FieldType | None = None) \
|
|
26
|
+
-> ReportColumn:
|
|
27
|
+
"""
|
|
28
|
+
Build a ReportColumn from a variety of possible inputs.
|
|
29
|
+
|
|
30
|
+
:param data_type: An object that can be used to identify a data type.
|
|
31
|
+
:param field: An object that can be used to identify a data field.
|
|
32
|
+
:param field_type: The field type of the provided field. This is only required if the field type cannot be
|
|
33
|
+
determined from the given data type and field, which occurs when the given field is a string and the
|
|
34
|
+
given data type is not a wrapped record model or record model wrapper.
|
|
35
|
+
:return: A ReportColumn for the inputs.
|
|
36
|
+
"""
|
|
37
|
+
# Get the data type and field names from the inputs.
|
|
38
|
+
data_type_name = AliasUtil.to_data_type_name(data_type)
|
|
39
|
+
field_name = AliasUtil.to_data_field_name(field)
|
|
40
|
+
if field_type is None:
|
|
41
|
+
field_type = ColumnBuilder.__field_type(data_type, field)
|
|
42
|
+
if field_type is None:
|
|
43
|
+
raise SapioException("The field_type parameter is required for the provided data_type and field inputs.")
|
|
44
|
+
return ReportColumn(data_type_name, field_name, field_type)
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def __field_type(data_type: DataTypeIdentifier, field: FieldIdentifier) -> FieldType | None:
|
|
48
|
+
"""
|
|
49
|
+
Given a record model wrapper and a field name, return the field type for that field. Accounts for system fields.
|
|
50
|
+
|
|
51
|
+
:param data_type: The record model wrapper that the field is on.
|
|
52
|
+
:param field: The field name to return the type of.
|
|
53
|
+
:return: The field type of the given field name.
|
|
54
|
+
"""
|
|
55
|
+
# Check if the field name is a system field. If it is, use the field type defined in this file.
|
|
56
|
+
field_name: str = AliasUtil.to_data_field_name(field)
|
|
57
|
+
if field_name in SYSTEM_FIELDS:
|
|
58
|
+
return SYSTEM_FIELDS.get(field_name)
|
|
59
|
+
# Otherwise, check if the field type can be found from the wrapper.
|
|
60
|
+
return AliasUtil.to_field_type(field, data_type)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
from sapiopylib.rest.pojo.CustomReport import ReportColumn, CustomReportCriteria, AbstractReportTerm, \
|
|
2
|
+
ExplicitJoinDefinition, RelatedRecordCriteria, QueryRestriction, FieldCompareReportTerm
|
|
3
|
+
from sapiopylib.rest.pojo.datatype.FieldDefinition import FieldType
|
|
4
|
+
|
|
5
|
+
from sapiopycommons.customreport.column_builder import ColumnBuilder
|
|
6
|
+
from sapiopycommons.general.aliases import DataTypeIdentifier, FieldIdentifier, AliasUtil, SapioRecord
|
|
7
|
+
from sapiopycommons.general.exceptions import SapioException
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CustomReportBuilder:
|
|
11
|
+
"""
|
|
12
|
+
A class used for building custom reports. Look into using the TermBuilder and ColumnBuilder classes for building
|
|
13
|
+
parts of a custom report.
|
|
14
|
+
"""
|
|
15
|
+
root_data_type: DataTypeIdentifier
|
|
16
|
+
data_type_name: str
|
|
17
|
+
root_term: AbstractReportTerm | None
|
|
18
|
+
record_criteria: RelatedRecordCriteria
|
|
19
|
+
column_list: list[ReportColumn]
|
|
20
|
+
join_list: list[ExplicitJoinDefinition]
|
|
21
|
+
|
|
22
|
+
def __init__(self, root_data_type: DataTypeIdentifier):
|
|
23
|
+
"""
|
|
24
|
+
:param root_data_type: An object that can be used to identify a data type name. Used as the root data type name
|
|
25
|
+
of this search.
|
|
26
|
+
"""
|
|
27
|
+
self.root_data_type = root_data_type
|
|
28
|
+
self.data_type_name = AliasUtil.to_data_type_name(root_data_type)
|
|
29
|
+
self.root_term = None
|
|
30
|
+
self.record_criteria = RelatedRecordCriteria(QueryRestriction.QUERY_ALL)
|
|
31
|
+
self.column_list = []
|
|
32
|
+
self.join_list = []
|
|
33
|
+
|
|
34
|
+
def has_root_term(self) -> bool:
|
|
35
|
+
"""
|
|
36
|
+
:return: Whether this report builder has had its root term set.
|
|
37
|
+
"""
|
|
38
|
+
return self.root_term is not None
|
|
39
|
+
|
|
40
|
+
def set_root_term(self, term: AbstractReportTerm) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Set the root term of the report. Use the TermBuilder class to construct the report terms.
|
|
43
|
+
|
|
44
|
+
:param term: The term to set as the root term.
|
|
45
|
+
"""
|
|
46
|
+
self.root_term = term
|
|
47
|
+
|
|
48
|
+
def has_columns(self) -> bool:
|
|
49
|
+
"""
|
|
50
|
+
:return: Whether this report builder has any report columns.
|
|
51
|
+
"""
|
|
52
|
+
return bool(self.column_list)
|
|
53
|
+
|
|
54
|
+
def add_column(self, field: FieldIdentifier, field_type: FieldType = None,
|
|
55
|
+
*, data_type: DataTypeIdentifier | None = None) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Add a column to this report builder.
|
|
58
|
+
|
|
59
|
+
:param field: An object that can be used to identify a data field.
|
|
60
|
+
:param field_type: The field type of the provided field. This is only required if the field type cannot be
|
|
61
|
+
determined from the given data type and field, which occurs when the given field is a string and the
|
|
62
|
+
given data type is not a wrapped record model or record model wrapper.
|
|
63
|
+
:param data_type: An object that can be used to identify a data type. If not provided, uses the root data type
|
|
64
|
+
provided when this builder was initialized. You'll only want to specify this value when adding a column
|
|
65
|
+
that is from a different data type than the root data type.
|
|
66
|
+
"""
|
|
67
|
+
if data_type is None:
|
|
68
|
+
data_type = self.root_data_type
|
|
69
|
+
self.column_list.append(ColumnBuilder.build_column(data_type, field, field_type))
|
|
70
|
+
|
|
71
|
+
def add_columns(self, fields: list[FieldIdentifier], *, data_type: DataTypeIdentifier | None = None) -> None:
|
|
72
|
+
"""
|
|
73
|
+
Add columns to this report builder.
|
|
74
|
+
|
|
75
|
+
:param fields: A list of objects that can be used to identify data fields.
|
|
76
|
+
:param data_type: An object that can be used to identify a data type. If not provided, uses the root data type
|
|
77
|
+
provided when this builder was initialized. You'll only want to specify this value when adding a column
|
|
78
|
+
that is from a different data type than the root data type.
|
|
79
|
+
"""
|
|
80
|
+
for field in fields:
|
|
81
|
+
self.add_column(field, data_type=data_type)
|
|
82
|
+
|
|
83
|
+
def set_query_restriction(self, base_record: SapioRecord, search_related: QueryRestriction) -> None:
|
|
84
|
+
"""
|
|
85
|
+
Set a restriction on the report for this report builder such that the returned results must be related in
|
|
86
|
+
some way to the provided base record. Without this, the report searches all records in the system that match the
|
|
87
|
+
root term.
|
|
88
|
+
|
|
89
|
+
:param base_record: The base record to run the search from.
|
|
90
|
+
:param search_related: Determine the relationship of the related records that can appear in the search, be those
|
|
91
|
+
children, parents, descendants, or ancestors.
|
|
92
|
+
"""
|
|
93
|
+
if search_related == QueryRestriction.QUERY_ALL:
|
|
94
|
+
raise SapioException("The search_related must be something other than QUERY_ALL when setting a query restriction.")
|
|
95
|
+
self.record_criteria = RelatedRecordCriteria(search_related,
|
|
96
|
+
AliasUtil.to_record_id(base_record),
|
|
97
|
+
AliasUtil.to_data_type_name(base_record))
|
|
98
|
+
|
|
99
|
+
def add_join(self, comparison_term: FieldCompareReportTerm, data_type: DataTypeIdentifier | None = None) -> None:
|
|
100
|
+
"""
|
|
101
|
+
Add a join statement to this report builder.
|
|
102
|
+
|
|
103
|
+
:param comparison_term: The field comparison term to join with.
|
|
104
|
+
:param data_type: The data type name that this join is on. If not provided, then the left side data type name
|
|
105
|
+
of the comparison term will be the data type that is joined against.
|
|
106
|
+
"""
|
|
107
|
+
if data_type is None:
|
|
108
|
+
data_type: str = comparison_term.left_data_type_name
|
|
109
|
+
else:
|
|
110
|
+
data_type: str = AliasUtil.to_data_type_name(data_type)
|
|
111
|
+
self.join_list.append(ExplicitJoinDefinition(data_type, comparison_term))
|
|
112
|
+
|
|
113
|
+
def build_report_criteria(self, page_size: int = 0, page_number: int = -1, case_sensitive: bool = False,
|
|
114
|
+
owner_restriction_set: list[str] = None) -> CustomReportCriteria:
|
|
115
|
+
"""
|
|
116
|
+
Generate a CustomReportCriteria using the column list, root term, and root data type from this report builder.
|
|
117
|
+
You can use the CustomReportManager or CustomReportUtil to run the constructed report.
|
|
118
|
+
|
|
119
|
+
:param page_size: The page size of the custom report.
|
|
120
|
+
:param page_number: The page number of the current report.
|
|
121
|
+
:param case_sensitive: When searching texts, should the search be case-sensitive?
|
|
122
|
+
:param owner_restriction_set: Specifies to only return records if the record is owned by this list of usernames.
|
|
123
|
+
:return: A CustomReportCriteria from this report builder.
|
|
124
|
+
"""
|
|
125
|
+
if not self.has_root_term():
|
|
126
|
+
raise SapioException("Cannot build a report with no root term.")
|
|
127
|
+
if not self.has_columns():
|
|
128
|
+
raise SapioException("Cannot build a report with no columns.")
|
|
129
|
+
return CustomReportCriteria(self.column_list, self.root_term, self.record_criteria, self.data_type_name,
|
|
130
|
+
case_sensitive, page_size, page_number, owner_restriction_set, self.join_list)
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
from typing import Iterable
|
|
2
|
+
|
|
3
|
+
from sapiopylib.rest.pojo.CustomReport import RawTermOperation, CompositeTermOperation, RawReportTerm, \
|
|
4
|
+
CompositeReportTerm, AbstractReportTerm, FieldCompareReportTerm
|
|
5
|
+
|
|
6
|
+
from sapiopycommons.general.aliases import DataTypeIdentifier, AliasUtil, FieldIdentifier
|
|
7
|
+
|
|
8
|
+
# Raw term operations, for comparing field values.
|
|
9
|
+
EQ = RawTermOperation.EQUAL_TO_OPERATOR
|
|
10
|
+
NEQ = RawTermOperation.NOT_EQUAL_TO_OPERATOR
|
|
11
|
+
LT = RawTermOperation.LESS_THAN_OPERATOR
|
|
12
|
+
LTE = RawTermOperation.LESS_THAN_OR_EQUAL_OPERATOR
|
|
13
|
+
GT = RawTermOperation.GREATER_THAN_OPERATOR
|
|
14
|
+
GTE = RawTermOperation.GREATER_THAN_OR_EQUAL_OPERATOR
|
|
15
|
+
|
|
16
|
+
# Composite term operations, for comparing two terms.
|
|
17
|
+
AND = CompositeTermOperation.AND_OPERATOR
|
|
18
|
+
OR = CompositeTermOperation.OR_OPERATOR
|
|
19
|
+
|
|
20
|
+
# Forms that field term values can take.
|
|
21
|
+
TermValue = str | int | float | bool | Iterable | None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TermBuilder:
|
|
25
|
+
"""
|
|
26
|
+
A class that allows for the easier constructions of custom report terms.
|
|
27
|
+
"""
|
|
28
|
+
@staticmethod
|
|
29
|
+
def all_records_term(data_type: DataTypeIdentifier) -> RawReportTerm:
|
|
30
|
+
"""
|
|
31
|
+
Create a raw report term that captures all records of the given data type.
|
|
32
|
+
|
|
33
|
+
:param data_type: The data type of this term.
|
|
34
|
+
:return: A raw report term for "data_type.RecordId >= 0".
|
|
35
|
+
"""
|
|
36
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), "RecordId", GTE, TermBuilder.to_term_val(0))
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def is_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
40
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
41
|
+
"""
|
|
42
|
+
Create a raw report term for comparing a field value with an equals operation.
|
|
43
|
+
|
|
44
|
+
:param data_type: The data type of this term.
|
|
45
|
+
:param field: The data field of this term.
|
|
46
|
+
:param value: The value to compare for this term.
|
|
47
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
48
|
+
:return: A raw report term for "data_type.field = value".
|
|
49
|
+
"""
|
|
50
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), EQ,
|
|
51
|
+
TermBuilder.to_term_val(value), trim)
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def not_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
55
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
56
|
+
"""
|
|
57
|
+
Create a raw report term for comparing a field value with a not equals operation.
|
|
58
|
+
|
|
59
|
+
:param data_type: The data type of this term.
|
|
60
|
+
:param field: The data field of this term.
|
|
61
|
+
:param value: The value to compare for this term.
|
|
62
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
63
|
+
:return: A raw report term for "data_type.field != value".
|
|
64
|
+
"""
|
|
65
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), NEQ,
|
|
66
|
+
TermBuilder.to_term_val(value), trim)
|
|
67
|
+
|
|
68
|
+
@staticmethod
|
|
69
|
+
def lt_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
70
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
71
|
+
"""
|
|
72
|
+
Create a raw report term for comparing a field value with a less than operation.
|
|
73
|
+
|
|
74
|
+
:param data_type: The data type of this term.
|
|
75
|
+
:param field: The data field of this term.
|
|
76
|
+
:param value: The value to compare for this term.
|
|
77
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
78
|
+
:return: A raw report term for "data_type.field < value".
|
|
79
|
+
"""
|
|
80
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), LT,
|
|
81
|
+
TermBuilder.to_term_val(value), trim)
|
|
82
|
+
|
|
83
|
+
@staticmethod
|
|
84
|
+
def lte_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
85
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
86
|
+
"""
|
|
87
|
+
Create a raw report term for comparing a field value with a less than or equal to operation.
|
|
88
|
+
|
|
89
|
+
:param data_type: The data type of this term.
|
|
90
|
+
:param field: The data field of this term.
|
|
91
|
+
:param value: The value to compare for this term.
|
|
92
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
93
|
+
:return: A raw report term for "data_type.field <= value".
|
|
94
|
+
"""
|
|
95
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), LTE,
|
|
96
|
+
TermBuilder.to_term_val(value), trim)
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def gt_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
100
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
101
|
+
"""
|
|
102
|
+
Create a raw report term for comparing a field value with a greater than operation.
|
|
103
|
+
|
|
104
|
+
:param data_type: The data type of this term.
|
|
105
|
+
:param field: The data field of this term.
|
|
106
|
+
:param value: The value to compare for this term.
|
|
107
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
108
|
+
:return: A raw report term for "data_type.field > value".
|
|
109
|
+
"""
|
|
110
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), GT,
|
|
111
|
+
TermBuilder.to_term_val(value), trim)
|
|
112
|
+
|
|
113
|
+
@staticmethod
|
|
114
|
+
def gte_term(data_type: DataTypeIdentifier, field: FieldIdentifier, value: TermValue,
|
|
115
|
+
*, trim: bool = False) -> RawReportTerm:
|
|
116
|
+
"""
|
|
117
|
+
Create a raw report term for comparing a field value with a greater than or equal to operation.
|
|
118
|
+
|
|
119
|
+
:param data_type: The data type of this term.
|
|
120
|
+
:param field: The data field of this term.
|
|
121
|
+
:param value: The value to compare for this term.
|
|
122
|
+
:param trim: Whether the string of the given value should be trimmed of trailing and leading whitespace.
|
|
123
|
+
:return: A raw report term for "data_type.field >= value".
|
|
124
|
+
"""
|
|
125
|
+
return RawReportTerm(AliasUtil.to_data_type_name(data_type), AliasUtil.to_data_field_name(field), GTE,
|
|
126
|
+
TermBuilder.to_term_val(value), trim)
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def compare_is_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
130
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
131
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
132
|
+
"""
|
|
133
|
+
Create a field comparison report term for comparing field values between data types with an equals operation.
|
|
134
|
+
|
|
135
|
+
:param data_type_A: The data type for the left side of this term.
|
|
136
|
+
:param field_A: The data field for the left side of this term.
|
|
137
|
+
:param data_type_B: The data type for the right side of this term.
|
|
138
|
+
:param field_B: The data field for the right side of this term.
|
|
139
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
140
|
+
:return: A field comparison report term for "data_type_A.field_A = data_type_B.field_B".
|
|
141
|
+
"""
|
|
142
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), EQ,
|
|
143
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
144
|
+
|
|
145
|
+
@staticmethod
|
|
146
|
+
def compare_not_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
147
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
148
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
149
|
+
"""
|
|
150
|
+
Create a field comparison report term for comparing field values between data types with a not equals operation.
|
|
151
|
+
|
|
152
|
+
:param data_type_A: The data type for the left side of this term.
|
|
153
|
+
:param field_A: The data field for the left side of this term.
|
|
154
|
+
:param data_type_B: The data type for the right side of this term.
|
|
155
|
+
:param field_B: The data field for the right side of this term.
|
|
156
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
157
|
+
:return: A field comparison report term for "data_type_A.field_A != data_type_B.field_B".
|
|
158
|
+
"""
|
|
159
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), NEQ,
|
|
160
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
161
|
+
|
|
162
|
+
@staticmethod
|
|
163
|
+
def compare_lt_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
164
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
165
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
166
|
+
"""
|
|
167
|
+
Create a field comparison report term for comparing field values between data types with a less than operation.
|
|
168
|
+
|
|
169
|
+
:param data_type_A: The data type for the left side of this term.
|
|
170
|
+
:param field_A: The data field for the left side of this term.
|
|
171
|
+
:param data_type_B: The data type for the right side of this term.
|
|
172
|
+
:param field_B: The data field for the right side of this term.
|
|
173
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
174
|
+
:return: A field comparison report term for "data_type_A.field_A < data_type_B.field_B".
|
|
175
|
+
"""
|
|
176
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), LT,
|
|
177
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
178
|
+
|
|
179
|
+
@staticmethod
|
|
180
|
+
def compare_lte_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
181
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
182
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
183
|
+
"""
|
|
184
|
+
Create a field comparison report term for comparing field values between data types with a less than or equal
|
|
185
|
+
to operation.
|
|
186
|
+
|
|
187
|
+
:param data_type_A: The data type for the left side of this term.
|
|
188
|
+
:param field_A: The data field for the left side of this term.
|
|
189
|
+
:param data_type_B: The data type for the right side of this term.
|
|
190
|
+
:param field_B: The data field for the right side of this term.
|
|
191
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
192
|
+
:return: A field comparison report term for "data_type_A.field_A <= data_type_B.field_B".
|
|
193
|
+
"""
|
|
194
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), LTE,
|
|
195
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
196
|
+
|
|
197
|
+
@staticmethod
|
|
198
|
+
def compare_gt_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
199
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
200
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
201
|
+
"""
|
|
202
|
+
Create a field comparison report term for comparing field values between data types with a greater than
|
|
203
|
+
operation.
|
|
204
|
+
|
|
205
|
+
:param data_type_A: The data type for the left side of this term.
|
|
206
|
+
:param field_A: The data field for the left side of this term.
|
|
207
|
+
:param data_type_B: The data type for the right side of this term.
|
|
208
|
+
:param field_B: The data field for the right side of this term.
|
|
209
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
210
|
+
:return: A field comparison report term for "data_type_A.field_A > data_type_B.field_B".
|
|
211
|
+
"""
|
|
212
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), GT,
|
|
213
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
214
|
+
|
|
215
|
+
@staticmethod
|
|
216
|
+
def compare_gte_term(data_type_A: DataTypeIdentifier, field_A: FieldIdentifier,
|
|
217
|
+
data_type_B: DataTypeIdentifier, field_B: FieldIdentifier,
|
|
218
|
+
*, trim: bool = False) -> FieldCompareReportTerm:
|
|
219
|
+
"""
|
|
220
|
+
Create a field comparison report term for comparing field values between data types with a greater than or
|
|
221
|
+
equal to operation.
|
|
222
|
+
|
|
223
|
+
:param data_type_A: The data type for the left side of this term.
|
|
224
|
+
:param field_A: The data field for the left side of this term.
|
|
225
|
+
:param data_type_B: The data type for the right side of this term.
|
|
226
|
+
:param field_B: The data field for the right side of this term.
|
|
227
|
+
:param trim: Whether the field values should be trimmed of trailing and leading whitespace for comparing.
|
|
228
|
+
:return: A field comparison report term for "data_type_A.field_A >= data_type_B.field_B".
|
|
229
|
+
"""
|
|
230
|
+
return FieldCompareReportTerm(AliasUtil.to_data_type_name(data_type_A), AliasUtil.to_data_field_name(field_A), GTE,
|
|
231
|
+
AliasUtil.to_data_type_name(data_type_B), AliasUtil.to_data_field_name(field_B), trim)
|
|
232
|
+
|
|
233
|
+
@staticmethod
|
|
234
|
+
def or_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
|
|
235
|
+
"""
|
|
236
|
+
Combine two report terms with an OR operation.
|
|
237
|
+
|
|
238
|
+
:param a: The first term in the operation.
|
|
239
|
+
:param b: The second term in the operation.
|
|
240
|
+
:param is_negated: Whether the returned term should be negated (i.e. turn this into a nor operation).
|
|
241
|
+
:return: A composite report term for "A or B".
|
|
242
|
+
"""
|
|
243
|
+
return CompositeReportTerm(a, OR, b, is_negated)
|
|
244
|
+
|
|
245
|
+
@staticmethod
|
|
246
|
+
def and_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
|
|
247
|
+
"""
|
|
248
|
+
Combine two report terms with an AND operation.
|
|
249
|
+
|
|
250
|
+
:param a: The first term in the operation.
|
|
251
|
+
:param b: The second term in the operation.
|
|
252
|
+
:param is_negated: Whether the returned term should be negated (i.e. turn this into a nand operation).
|
|
253
|
+
:return: A composite report term for "A and B".
|
|
254
|
+
"""
|
|
255
|
+
return CompositeReportTerm(a, AND, b, is_negated)
|
|
256
|
+
|
|
257
|
+
@staticmethod
|
|
258
|
+
def xor_terms(a: AbstractReportTerm, b: AbstractReportTerm, *, is_negated: bool = False) -> CompositeReportTerm:
|
|
259
|
+
"""
|
|
260
|
+
Combine two report terms with a XOR operation. Note that a XOR operation doesn't actually exist for custom
|
|
261
|
+
reports. This instead constructs a term that is "(A or B) and !(A and B)", which is equivalent to a XOR
|
|
262
|
+
operation.
|
|
263
|
+
|
|
264
|
+
:param a: The first term in the operation.
|
|
265
|
+
:param b: The second term in the operation.
|
|
266
|
+
:param is_negated: Whether the returned term should be negated (i.e. turn this into an xnor operation).
|
|
267
|
+
:return: A composite report term for "A xor B".
|
|
268
|
+
"""
|
|
269
|
+
return TermBuilder.and_terms(TermBuilder.or_terms(a, b),
|
|
270
|
+
TermBuilder.and_terms(a, b, is_negated=True),
|
|
271
|
+
is_negated=is_negated)
|
|
272
|
+
|
|
273
|
+
@staticmethod
|
|
274
|
+
def to_term_val(value: TermValue) -> str:
|
|
275
|
+
"""
|
|
276
|
+
Convert the given value to be used in a custom report term to a string. Term values may be strings, integers,
|
|
277
|
+
floats, booleans, or lists of values.
|
|
278
|
+
|
|
279
|
+
:param value: A value to be used in a custom report term.
|
|
280
|
+
:return: The provided value formatted as a string that can be used
|
|
281
|
+
"""
|
|
282
|
+
# If the given value is already a string, then nothing needs to be done with it.
|
|
283
|
+
if not isinstance(value, str):
|
|
284
|
+
# If the given value is None, then use an empty string for the search instead.
|
|
285
|
+
if value is None:
|
|
286
|
+
value = ""
|
|
287
|
+
# If the given value is an iterable object, then the return value is the contents of that iterable
|
|
288
|
+
# in a comma separated list surrounded by curly braces.
|
|
289
|
+
elif isinstance(value, Iterable):
|
|
290
|
+
# When converting a list of values to a string, values in the list which are already strings should be
|
|
291
|
+
# put in quotation marks so that strings that contain commas do not get split up. All other value
|
|
292
|
+
# types can be simply converted to a string, though.
|
|
293
|
+
def convert_list_value(val: TermValue) -> str:
|
|
294
|
+
return f"'{val}'" if isinstance(val, str) else str(val)
|
|
295
|
+
value = "{" + ",".join([convert_list_value(x) for x in value]) + "}"
|
|
296
|
+
else:
|
|
297
|
+
# Otherwise, the value is simply cast to a string.
|
|
298
|
+
value = str(value)
|
|
299
|
+
return value
|