Utilities¶
Core utility functions for mapping manipulation, merging, filtering, and comparison.
sssom.util
¶
Utility functions.
MappingSetDataFrame
dataclass
¶
A collection of mappings represented as a DataFrame, together with additional metadata.
Source code in src/sssom/util.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
prefix_map
property
¶
Get a simple, bijective prefix map.
with_converter(converter, df, metadata=None)
classmethod
¶
Instantiate with a converter instead of a vanilla prefix map.
Source code in src/sssom/util.py
from_mappings(mappings, *, converter=None, metadata=None)
classmethod
¶
Instantiate from a list of mappings, mapping set metadata, and an optional converter.
Source code in src/sssom/util.py
from_mapping_set(mapping_set, *, converter=None)
classmethod
¶
Instantiate from a mapping set and an optional converter.
:param mapping_set: A mapping set :param converter: A prefix map or pre-instantiated converter. If none given, uses a default prefix map derived from the Bioregistry.
:returns: A mapping set dataframe
Source code in src/sssom/util.py
from_mapping_set_document(doc)
classmethod
¶
Instantiate from a mapping set document.
Source code in src/sssom/util.py
to_mapping_set_document()
¶
to_mapping_set()
¶
to_mappings()
¶
clean_context()
¶
standardize_references()
¶
Standardize this MSDF's dataframe and metadata with respect to its converter.
merge(*msdfs, inplace=True)
¶
Merge two MappingSetDataframes.
:param msdfs: Multiple/Single MappingSetDataFrame(s) to merge with self :param inplace: If true, msdf2 is merged into the calling MappingSetDataFrame, if false, it simply return the merged data frame.
:returns: Merged MappingSetDataFrame
Source code in src/sssom/util.py
clean_prefix_map(strict=True)
¶
Remove unused prefixes from the internal prefix map based on the internal dataframe.
:param strict: Boolean if True, errors out if all prefixes in dataframe are not listed in the 'curie_map'.
:raises ValueError: If prefixes absent in 'curie_map' and strict flag = True
Source code in src/sssom/util.py
remove_mappings(msdf)
¶
Remove mappings in right msdf from left msdf.
:param msdf: MappingSetDataframe object to be removed from primary msdf object.
Source code in src/sssom/util.py
propagate(fill_empty=False)
¶
Propagate slot values from the set level down to individual records.
Propagation, as defined by the SSSOM specification, is the process by which the values of so-called "propagatable slots" in the set metadata are moved to the corresponding slots in each individual mapping records.
Propagation of a slot is only allowed iff no individual records already have a value for that slot.
:param fill_empty: If True, propagation of a slot is allowed even if some individual records already have a value for that slot. The set-level value will be propagated to all the records for which the slot is empty. Note that (1) this is not spec-compliant behaviour, and (2) this makes the operation non-reversible by a subsequent condensation.
:returns: The list of slots that were effectively propagated.
Source code in src/sssom/util.py
condense()
¶
Condense record-level slot values to the set whenever possible.
Condensation is the opposite of propagation. It is the process by which the values of so-called "propagatable" slots found in individual mapping records are moved to the corresponding slots in the set metadata.
Condensation of a slot is only allowed iff (1) all records have the same value for that slot and (2) the slot does not already have a different value in the set metadata.
:returns: The list of slots that were effectively condensed.
Source code in src/sssom/util.py
infer_cardinality(scope=None)
¶
Infer cardinality values in the set.
This method will automatically fill the mapping_cardinality slot for all records in the
set, overwriting any pre-existing values.
See
https://mapping-commons.github.io/sssom/spec-model/#mapping-cardinality-and-cardinality-scope
for more information about cardinality computation,
https://mapping-commons.github.io/sssom/spec-model/#literal-mappings for how to deal with
literal mapping records, and
https://mapping-commons.github.io/sssom/spec-model/#representing-unmapped-entities for how
to deal with mapping records involving sssom:NoTermFound.
:param scope: A list of slot names that defines the subset of the records in which
cardinality will be computed. For example, with a scope of ['predicate_id'], for any
given record the cardinality will be computed relatively to the subset of records that
have the same predicate. The default is an empty list, meaning that cardinality is
computed relatively to the entire set of records.
Source code in src/sssom/util.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
get_compatible_version()
¶
Get the minimum version of SSSOM this set is compatible with.
Source code in src/sssom/util.py
enforce_version(version, strict=False, inplace=False)
¶
Ensure the set is compliant with a given version of the SSSOM specification.
This method will forcefully remove any slot or enum value that is not defined in the specified version of the specification.
:param version: The targeted version of the specification, as a
string of the form X.Y.
:param strict: If True, unknown slots will be removed as well,
unless they are properly declared as extensions.
:param inplace: if True, the method will modify and return the
set it has been called upon. The default is to
leave that set untouched and to return a
modified copy.
:return: A set that is compliant with the requested version of
the SSSOM specification.
Source code in src/sssom/util.py
EntityPair
dataclass
¶
A tuple of entities.
Note that (e1,e2) == (e2,e1)
Source code in src/sssom/util.py
MappingSetDiff
dataclass
¶
Represents a difference between two mapping sets.
Currently this is limited to diffs at the level of entity-pairs. For example, if file1 has A owl:equivalentClass B, and file2 has A skos:closeMatch B, this is considered a mapping in common.
Source code in src/sssom/util.py
combined_dataframe
instance-attribute
¶
Dataframe that combines with left and right dataframes with information injected into the comment column
PrefixReconciliation
¶
collapse(df)
¶
Collapse rows with same S/P/O and combines confidence.
sort_sssom(df)
¶
Sort SSSOM by columns.
:param df: SSSOM DataFrame to be sorted.
:returns: Sorted SSSOM DataFrame
Source code in src/sssom/util.py
filter_redundant_rows(df, ignore_predicate=False)
¶
Remove rows if there is another row with same S/O and higher confidence.
:param df: Pandas DataFrame to filter :param ignore_predicate: If true, the predicate_id column is ignored, defaults to False
:returns: Filtered pandas DataFrame
Source code in src/sssom/util.py
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
get_row_based_on_hierarchy(df)
¶
Get row based on hierarchy of predicates.
The hierarchy is as follows:
- owl:equivalentClass
- owl:equivalentProperty
- rdfs:subClassOf
- rdfs:subPropertyOf
- owl:sameAs
- skos:exactMatch
- skos:closeMatch
- skos:broadMatch
- skos:narrowMatch
- oboInOwl:hasDbXref
- skos:relatedMatch
- rdfs:seeAlso
:param df: Dataframe containing multiple predicates for same subject and object.
:returns: Dataframe with a single row which ranks higher in the hierarchy.
:raises KeyError: if no rows are available
Source code in src/sssom/util.py
assign_default_confidence(df)
¶
Assign :data:numpy.nan to confidence that are blank.
:param df: SSSOM DataFrame
:returns: A Tuple consisting of the original DataFrame and dataframe consisting of empty confidence values.
Source code in src/sssom/util.py
remove_unmatched(df)
¶
Remove rows where no match is found.
:param df: Pandas DataFrame
:returns: Pandas DataFrame with 'PREDICATE_ID' not 'noMatch'.
.. todo:: https://github.com/OBOFoundry/SSSOM/issues/28
Source code in src/sssom/util.py
create_entity(identifier, mappings)
¶
Create an Entity object.
:param identifier: Entity Id :param mappings: Mapping dictionary
:returns: An Entity object
Source code in src/sssom/util.py
group_mappings(df)
¶
Group mappings by EntityPairs.
Source code in src/sssom/util.py
compare_dataframes(df1, df2)
¶
Perform a diff between two SSSOM dataframes.
:param df1: A mapping dataframe :param df2: A mapping dataframe
:returns: A mapping set diff
.. warning::
currently does not discriminate between mappings with different predicates
Source code in src/sssom/util.py
add_default_confidence(df, confidence=np.nan)
¶
Add confidence column to DataFrame if absent and initializes to 0.95.
If confidence column already exists, only fill in the None ones by 0.95.
:param df: DataFrame whose confidence column needs to be filled.
:returns: DataFrame with a complete confidence column.
Source code in src/sssom/util.py
dataframe_to_ptable(df, *, inverse_factor=None, default_confidence=None)
¶
Export a KBOOM table.
:param df: Pandas DataFrame :param inverse_factor: Multiplier to (1 - confidence), defaults to 0.5 :param default_confidence: Default confidence to be assigned if absent.
:returns: List of rows
:raises ValueError: Predicate value error :raises ValueError: Predicate type value error
Source code in src/sssom/util.py
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 | |
merge_msdf(*msdfs, reconcile=False)
¶
Merge multiple MappingSetDataFrames into one.
:param msdfs: A Tuple of MappingSetDataFrames to be merged :param reconcile: If reconcile=True, then dedupe(remove redundant lower confidence mappings) and reconcile (if msdf contains a higher confidence negative mapping, then remove lower confidence positive one. If confidence is the same, prefer HumanCurated. If both HumanCurated, prefer negative mapping). Defaults to False.
:returns: Merged MappingSetDataFrame.
Source code in src/sssom/util.py
deal_with_negation(df)
¶
Combine negative and positive rows with matching [SUBJECT_ID, OBJECT_ID, CONFIDENCE] combination.
Rule: negative trumps positive if modulus of confidence values are equal.
:param df: Merged Pandas DataFrame
:returns: Pandas DataFrame with negations addressed
:raises ValueError: If the dataframe is none after assigning default confidence
Source code in src/sssom/util.py
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 | |
propagate_condensed_slots(df, metadata, fill_empty=False)
¶
Propagate slot values from the set level down to individual records.
This function performs the same operation as the MappingSetDataFrame#propagate()
method. It is intended to allow propagating a mapping set before an instance of
the MappingSetDataFrame class can be obtained.
:param df: The DataFrame into which values should be propagated. :param metadata: The dictionary of set-level metadata. :param fill_empty: If True, propagation of a slot is allowed even if some individual records already have a value for that slot. The set-level value will be propagated to all the records for which the slot is empty. Note that (1) this is not spec-compliant behaviour, and (2) this makes the operation non-reversible by a subsequent condensation.
:returns: The list of slots that were effectively propagated.
Source code in src/sssom/util.py
get_file_extension(file)
¶
Get file extension.
:param file: File path
:returns: format of the file passed, default tsv
Source code in src/sssom/util.py
to_mapping_set_dataframe(doc)
¶
Convert MappingSetDocument into MappingSetDataFrame.
:param doc: MappingSetDocument object
:returns: MappingSetDataFrame object
Source code in src/sssom/util.py
get_dict_from_mapping(map_obj)
¶
Get information for linkml objects (MatchTypeEnum, PredicateModifierEnum) from the Mapping object and return the dictionary form of the object.
:param map_obj: Mapping object
:returns: Dictionary
Source code in src/sssom/util.py
get_prefix_from_curie(curie)
¶
get_prefixes_used_in_table(df)
¶
Get a list of prefixes used in CURIEs in key feature columns in a dataframe.
Source code in src/sssom/util.py
get_prefixes_used_in_metadata(meta)
¶
Get a set of prefixes used in CURIEs in the metadata.
Source code in src/sssom/util.py
filter_out_prefixes(df, filter_prefixes, features=None, require_all_prefixes=False)
¶
Filter out rows which contains a CURIE with a prefix in the filter_prefixes list.
:param df: Pandas DataFrame of SSSOM Mapping :param filter_prefixes: List of prefixes :param features: List of dataframe column names dataframe to consider :param require_all_prefixes: If True, all prefixes must be present in a row to be filtered out
:returns: Pandas Dataframe
Source code in src/sssom/util.py
filter_prefixes(df, filter_prefixes, features=None, require_all_prefixes=True)
¶
Filter out rows which do NOT contain a CURIE with a prefix in the filter_prefixes list.
:param df: Pandas DataFrame of SSSOM Mapping :param filter_prefixes: List of prefixes :param features: List of dataframe column names dataframe to consider :param require_all_prefixes: If True, all prefixes must be present in a row to be filtered out
:returns: Pandas Dataframe
Source code in src/sssom/util.py
raise_for_bad_path(file_path)
¶
Raise exception if file path is invalid.
:param file_path: File path
:raises FileNotFoundError: Invalid file path
Source code in src/sssom/util.py
is_multivalued_slot(slot)
¶
Check whether the slot is multivalued according to the SSSOM specification.
:param slot: Slot name
:returns: Slot is multivalued or no
Source code in src/sssom/util.py
reconcile_prefix_and_data(msdf, prefix_reconciliation)
¶
Reconciles prefix_map and translates CURIE switch in dataframe.
:param msdf: Mapping Set DataFrame. :param prefix_reconciliation: Prefix reconcilation dictionary from a YAML file
:returns: Mapping Set DataFrame with reconciled prefix_map and data.
This method is build on :func:curies.remap_curie_prefixes and :func:curies.rewire. Note that
if you want to overwrite a CURIE prefix in the Bioregistry extended prefix map, you need to
provide a place for the old one to go as in {"geo": "ncbi.geo", "geogeo": "geo"}. Just doing
{"geogeo": "geo"} would not work since geo already exists.
Source code in src/sssom/util.py
sort_df_rows_columns(df, by_columns=True, by_rows=True)
¶
Canonical sorting of DataFrame columns.
:param df: Pandas DataFrame with random column sequence. :param by_columns: Boolean flag to sort columns canonically. :param by_rows: Boolean flag to sort rows by column #1 (ascending order).
:returns: Pandas DataFrame columns sorted canonically.
Source code in src/sssom/util.py
get_all_prefixes(msdf)
¶
Fetch all prefixes in the MappingSetDataFrame.
:param msdf: MappingSetDataFrame
:returns: List of all prefixes.
:raises ValidationError: If slot is wrong. :raises ValidationError: If slot is wrong.
Source code in src/sssom/util.py
augment_metadata(msdf, meta, replace_multivalued=False)
¶
Augment metadata with parameters passed.
:param msdf: MappingSetDataFrame (MSDF) object. :param meta: Dictionary that needs to be added/updated to the metadata of the MSDF. :param replace_multivalued: Multivalued slots should be replaced or not, defaults to False.
:returns: MSDF with updated metadata.
:raises ValueError: If type of slot is neither str nor list.
Source code in src/sssom/util.py
are_params_slots(params)
¶
Check if parameters conform to the slots in MAPPING_SET_SLOTS.
:param params: Dictionary of parameters.
:returns: True/False
:raises ValueError: If params are not slots.
Source code in src/sssom/util.py
invert_mappings(df, subject_prefix=None, merge_inverted=True, update_justification=True, predicate_invert_dictionary=None)
¶
Switching subject and objects based on their prefixes and adjusting predicates accordingly.
:param df: Pandas dataframe. :param subject_prefix: Prefix of subjects desired. :param merge_inverted: If True (default), add inverted dataframe to input else, just return inverted data. :param update_justification: If True (default), the justification is updated to "sempav:MappingInversion", else it is left as it is. :param predicate_invert_dictionary: YAML file providing the inverse mapping for predicates.
:returns: Pandas dataframe with all subject IDs having the same prefix.
Source code in src/sssom/util.py
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 | |
safe_compress(uri, converter)
¶
Parse a CURIE from an IRI.
:param uri: The URI to parse. If this is already a CURIE, return directly. :param converter: Converter used for compression
:returns: A CURIE
Source code in src/sssom/util.py
pandas_set_no_silent_downcasting(no_silent_downcasting=True)
¶
Set pandas future.no_silent_downcasting option. Context https://github.com/pandas-dev/pandas/issues/57734.