| 1 |
""" |
| 2 |
HERD: HDMF External Resources Data Structure |
| 3 |
============================================== |
| 4 |
|
| 5 |
This is a user guide to interacting with the |
| 6 |
:py:class:`~hdmf.common.resources.HERD` class. The HERD type |
| 7 |
is experimental and is subject to change in future releases. If you use this type, |
| 8 |
please provide feedback to the HDMF team so that we can improve the structure and |
| 9 |
access of data stored with this type for your use cases. |
| 10 |
|
| 11 |
Introduction |
| 12 |
------------- |
| 13 |
The :py:class:`~hdmf.common.resources.HERD` class provides a way |
| 14 |
to organize and map user terms from their data (keys) to multiple entities |
| 15 |
from the external resources. A typical use case for external resources is to link data |
| 16 |
stored in datasets or attributes to ontologies. For example, you may have a |
| 17 |
dataset ``country`` storing locations. Using |
| 18 |
:py:class:`~hdmf.common.resources.HERD` allows us to link the |
| 19 |
country names stored in the dataset to an ontology of all countries, enabling |
| 20 |
more rigid standardization of the data and facilitating data query and |
| 21 |
introspection. |
| 22 |
|
| 23 |
From a user's perspective, one can think of the |
| 24 |
:py:class:`~hdmf.common.resources.HERD` as a simple table, in which each |
| 25 |
row associates a particular ``key`` stored in a particular ``object`` (i.e., Attribute |
| 26 |
or Dataset in a file) with a particular ``entity`` (i.e, a term of an online |
| 27 |
resource). That is, ``(object, key)`` refer to parts inside a |
| 28 |
file and ``entity`` refers to an external resource outside the file, and |
| 29 |
:py:class:`~hdmf.common.resources.HERD` allows us to link the two. To |
| 30 |
reduce data redundancy and improve data integrity, |
| 31 |
:py:class:`~hdmf.common.resources.HERD` stores this data internally in a |
| 32 |
collection of interlinked tables. |
| 33 |
|
| 34 |
* :py:class:`~hdmf.common.resources.KeyTable` where each row describes a |
| 35 |
:py:class:`~hdmf.common.resources.Key` |
| 36 |
* :py:class:`~hdmf.common.resources.FileTable` where each row describes a |
| 37 |
:py:class:`~hdmf.common.resources.File` |
| 38 |
* :py:class:`~hdmf.common.resources.EntityTable` where each row describes an |
| 39 |
:py:class:`~hdmf.common.resources.Entity` |
| 40 |
* :py:class:`~hdmf.common.resources.EntityKeyTable` where each row describes an |
| 41 |
:py:class:`~hdmf.common.resources.EntityKey` |
| 42 |
* :py:class:`~hdmf.common.resources.ObjectTable` where each row describes an |
| 43 |
:py:class:`~hdmf.common.resources.Object` |
| 44 |
* :py:class:`~hdmf.common.resources.ObjectKeyTable` where each row describes an |
| 45 |
:py:class:`~hdmf.common.resources.ObjectKey` pair identifying which keys |
| 46 |
are used by which objects. |
| 47 |
|
| 48 |
The :py:class:`~hdmf.common.resources.HERD` class then provides |
| 49 |
convenience functions to simplify interaction with these tables, allowing users |
| 50 |
to treat :py:class:`~hdmf.common.resources.HERD` as a single large table as |
| 51 |
much as possible. |
| 52 |
|
| 53 |
Rules to HERD |
| 54 |
--------------------------- |
| 55 |
When using the :py:class:`~hdmf.common.resources.HERD` class, there |
| 56 |
are rules to how users store information in the interlinked tables. |
| 57 |
|
| 58 |
1. Multiple :py:class:`~hdmf.common.resources.Key` objects can have the same name. |
| 59 |
They are disambiguated by the :py:class:`~hdmf.common.resources.Object` associated |
| 60 |
with each, meaning we may have keys with the same name in different objects, but for a particular object |
| 61 |
all keys must be unique. |
| 62 |
2. In order to query specific records, the :py:class:`~hdmf.common.resources.HERD` class |
| 63 |
uses '(file, object_id, relative_path, field, key)' as the unique identifier. |
| 64 |
3. :py:class:`~hdmf.common.resources.Object` can have multiple :py:class:`~hdmf.common.resources.Key` |
| 65 |
objects. |
| 66 |
4. Multiple :py:class:`~hdmf.common.resources.Object` objects can use the same :py:class:`~hdmf.common.resources.Key`. |
| 67 |
5. Do not use the private methods to add into the :py:class:`~hdmf.common.resources.KeyTable`, |
| 68 |
:py:class:`~hdmf.common.resources.FileTable`, :py:class:`~hdmf.common.resources.EntityTable`, |
| 69 |
:py:class:`~hdmf.common.resources.ObjectTable`, :py:class:`~hdmf.common.resources.ObjectKeyTable`, |
| 70 |
:py:class:`~hdmf.common.resources.EntityKeyTable` individually. |
| 71 |
6. URIs are optional, but highly recommended. If not known, an empty string may be used. |
| 72 |
7. An entity ID should be the unique string identifying the entity in the given resource. |
| 73 |
This may or may not include a string representing the resource and a colon. |
| 74 |
Use the format provided by the resource. For example, Identifiers.org uses the ID ``ncbigene:22353`` |
| 75 |
but the NCBI Gene uses the ID ``22353`` for the same term. |
| 76 |
8. In a majority of cases, :py:class:`~hdmf.common.resources.Object` objects will have an empty string |
| 77 |
for 'field'. The :py:class:`~hdmf.common.resources.HERD` class supports compound data_types. |
| 78 |
In that case, 'field' would be the field of the compound data_type that has an external reference. |
| 79 |
9. In some cases, the attribute that needs an external reference is not a object with a 'data_type'. |
| 80 |
The user must then use the nearest object that has a data type to be used as the parent object. When |
| 81 |
adding an external resource for an object with a data type, users should not provide an attribute. |
| 82 |
When adding an external resource for an attribute of an object, users need to provide |
| 83 |
the name of the attribute. |
| 84 |
10. The user must provide a :py:class:`~hdmf.common.resources.File` or an :py:class:`~hdmf.common.resources.Object` that |
| 85 |
has :py:class:`~hdmf.common.resources.File` along the parent hierarchy. |
| 86 |
""" |
| 87 |
###################################################### |
| 88 |
# Creating an instance of the HERD class |
| 89 |
# ---------------------------------------------------- |
| 90 |
|
| 91 |
# sphinx_gallery_thumbnail_path = 'figures/gallery_thumbnail_externalresources.png' |
| 92 |
from hdmf.common import HERD |
| 93 |
from hdmf.common import DynamicTable, VectorData |
| 94 |
from hdmf.term_set import TermSet |
| 95 |
from hdmf import Container, HERDManager |
| 96 |
from hdmf import Data |
| 97 |
import numpy as np |
| 98 |
import os |
| 99 |
# Ignore experimental feature warnings in the tutorial to improve rendering |
| 100 |
import warnings |
| 101 |
warnings.filterwarnings("ignore", category=UserWarning, message="HERD is experimental*") |
| 102 |
|
| 103 |
try: |
| 104 |
dir_path = os.path.dirname(os.path.abspath(__file__)) |
| 105 |
yaml_file = os.path.join(dir_path, 'example_term_set.yaml') |
| 106 |
except NameError: |
| 107 |
dir_path = os.path.dirname(os.path.abspath('.')) |
| 108 |
yaml_file = os.path.join(dir_path, 'gallery/example_term_set.yaml') |
| 109 |
|
| 110 |
|
| 111 |
# Class to represent a file |
| 112 |
class HERDManagerContainer(Container, HERDManager): |
| 113 |
def __init__(self, **kwargs): |
| 114 |
kwargs['name'] = 'HERDManagerContainer' |
| 115 |
super().__init__(**kwargs) |
| 116 |
|
| 117 |
|
| 118 |
herd = HERD() |
| 119 |
file = HERDManagerContainer(name='file') |
| 120 |
|
| 121 |
|
| 122 |
############################################################################### |
| 123 |
# Using the add_ref method |
| 124 |
# ------------------------------------------------------ |
| 125 |
# :py:func:`~hdmf.common.resources.HERD.add_ref` |
| 126 |
# is a wrapper function provided by the |
| 127 |
# :py:class:`~hdmf.common.resources.HERD` class that simplifies adding |
| 128 |
# data. Using :py:func:`~hdmf.common.resources.HERD.add_ref` allows us to |
| 129 |
# treat new entries similar to adding a new row to a flat table, with |
| 130 |
# :py:func:`~hdmf.common.resources.HERD.add_ref` taking care of populating |
| 131 |
# the underlying data structures accordingly. |
| 132 |
|
| 133 |
data = Data(name="species", data=['Homo sapiens', 'Mus musculus']) |
| 134 |
data.parent = file |
| 135 |
herd.add_ref( |
| 136 |
file=file, |
| 137 |
container=data, |
| 138 |
key='Homo sapiens', |
| 139 |
entity_id='NCBI_TAXON:9606', |
| 140 |
entity_uri='https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=9606' |
| 141 |
) |
| 142 |
|
| 143 |
herd.add_ref( |
| 144 |
file=file, |
| 145 |
container=data, |
| 146 |
key='Mus musculus', |
| 147 |
entity_id='NCBI_TAXON:10090', |
| 148 |
entity_uri='https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090' |
| 149 |
) |
| 150 |
|
| 151 |
############################################################################### |
| 152 |
# Using the add_ref method with an attribute |
| 153 |
# ------------------------------------------------------ |
| 154 |
# It is important to keep in mind that when adding and :py:class:`~hdmf.common.resources.Object` to |
| 155 |
# the :py:class:~hdmf.common.resources.ObjectTable, the parent object identified by |
| 156 |
# ``Object.object_id`` must be the closest parent to the target object |
| 157 |
# (i.e., ``Object.relative_path`` must be the shortest possible path and |
| 158 |
# as such cannot contain any objects with a ``data_type`` and associated ``object_id``). |
| 159 |
# |
| 160 |
# A common example would be with the :py:class:`~hdmf.common.table.DynamicTable` class, which holds |
| 161 |
# :py:class:`~hdmf.common.table.VectorData` objects as columns. If we wanted to add an external |
| 162 |
# reference on a column from a :py:class:`~hdmf.common.table.DynamicTable`, then we would use the |
| 163 |
# column as the object and not the :py:class:`~hdmf.common.table.DynamicTable` (Refer to rule 9). |
| 164 |
|
| 165 |
genotypes = DynamicTable(name='genotypes', description='My genotypes') |
| 166 |
genotypes.add_column(name='genotype_name', description="Name of genotypes") |
| 167 |
genotypes.add_row(id=0, genotype_name='Rorb') |
| 168 |
genotypes.parent = file |
| 169 |
herd.add_ref( |
| 170 |
file=file, |
| 171 |
container=genotypes, |
| 172 |
attribute='genotype_name', |
| 173 |
key='Rorb', |
| 174 |
entity_id='MGI:1346434', |
| 175 |
entity_uri='http://www.informatics.jax.org/marker/MGI:1343464' |
| 176 |
) |
| 177 |
|
| 178 |
# Note: :py:func:`~hdmf.common.resources.HERD.add_ref` internally resolves the object |
| 179 |
# to the closest parent, so that ``herd.add_ref(container=genotypes, attribute='genotype_name')`` and |
| 180 |
# ``herd.add_ref(container=genotypes.genotype_name, attribute=None)`` will ultimately both use the ``object_id`` |
| 181 |
# of the ``genotypes.genotype_name`` :py:class:`~hdmf.common.table.VectorData` column and |
| 182 |
# not the object_id of the genotypes table. |
| 183 |
|
| 184 |
############################################################################### |
| 185 |
# Using the add_ref method without the file parameter. |
| 186 |
# ------------------------------------------------------ |
| 187 |
# Even though :py:class:`~hdmf.common.resources.File` is required to create/add a new reference, |
| 188 |
# the user can omit the file parameter if the :py:class:`~hdmf.common.resources.Object` has a file |
| 189 |
# in its parent hierarchy. |
| 190 |
|
| 191 |
col1 = VectorData( |
| 192 |
name='Species_Data', |
| 193 |
description='species from NCBI and Ensemble', |
| 194 |
data=['Homo sapiens', 'Ursus arctos horribilis'], |
| 195 |
) |
| 196 |
|
| 197 |
# Create a DynamicTable with this column and set the table parent to the file object created earlier |
| 198 |
species = DynamicTable(name='species', description='My species', columns=[col1]) |
| 199 |
species.parent = file |
| 200 |
|
| 201 |
herd.add_ref( |
| 202 |
container=species, |
| 203 |
attribute='Species_Data', |
| 204 |
key='Ursus arctos horribilis', |
| 205 |
entity_id='NCBI_TAXON:116960', |
| 206 |
entity_uri='https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id' |
| 207 |
) |
| 208 |
|
| 209 |
############################################################################### |
| 210 |
# Visualize HERD |
| 211 |
# ------------------------------------------------------ |
| 212 |
# Users can visualize `~hdmf.common.resources.HERD` as a flattened table or |
| 213 |
# as separate tables. |
| 214 |
|
| 215 |
# `~hdmf.common.resources.HERD` as a flattened table |
| 216 |
herd.to_dataframe() |
| 217 |
|
| 218 |
# The individual interlinked tables: |
| 219 |
herd.files.to_dataframe() |
| 220 |
herd.objects.to_dataframe() |
| 221 |
herd.entities.to_dataframe() |
| 222 |
herd.keys.to_dataframe() |
| 223 |
herd.object_keys.to_dataframe() |
| 224 |
herd.entity_keys.to_dataframe() |
| 225 |
|
| 226 |
############################################################################### |
| 227 |
# Using the get_key method |
| 228 |
# ------------------------------------------------------ |
| 229 |
# The :py:func:`~hdmf.common.resources.HERD.get_key` |
| 230 |
# method will return a :py:class:`~hdmf.common.resources.Key` object. In the current version of |
| 231 |
# :py:class:`~hdmf.common.resources.HERD`, duplicate keys are allowed; however, each key needs a unique |
| 232 |
# linking Object. In other words, each combination of (file, container, relative_path, field, key) |
| 233 |
# can exist only once in :py:class:`~hdmf.common.resources.HERD`. |
| 234 |
|
| 235 |
# The :py:func:`~hdmf.common.resources.HERD.get_key` method will be able to return the |
| 236 |
# :py:class:`~hdmf.common.resources.Key` object if the :py:class:`~hdmf.common.resources.Key` object is unique. |
| 237 |
genotype_key_object = herd.get_key(key_name='Rorb') |
| 238 |
|
| 239 |
# If the :py:class:`~hdmf.common.resources.Key` object has a duplicate name, then the user will need |
| 240 |
# to provide the unique (file, container, relative_path, field, key) combination. |
| 241 |
species_key_object = herd.get_key(file=file, |
| 242 |
container=species['Species_Data'], |
| 243 |
key_name='Ursus arctos horribilis') |
| 244 |
|
| 245 |
# The :py:func:`~hdmf.common.resources.HERD.get_key` also will check the |
| 246 |
# :py:class:`~hdmf.common.resources.Object` for a :py:class:`~hdmf.common.resources.File` along the parent hierarchy |
| 247 |
# if the file is not provided as in :py:func:`~hdmf.common.resources.HERD.add_ref` |
| 248 |
|
| 249 |
############################################################################### |
| 250 |
# Using the add_ref method with a key_object |
| 251 |
# ------------------------------------------------------ |
| 252 |
# Multiple :py:class:`~hdmf.common.resources.Object` objects can use the same |
| 253 |
# :py:class:`~hdmf.common.resources.Key`. To use an existing key when adding |
| 254 |
# new entries into :py:class:`~hdmf.common.resources.HERD`, pass the |
| 255 |
# :py:class:`~hdmf.common.resources.Key` object instead of the 'key_name' to the |
| 256 |
# :py:func:`~hdmf.common.resources.HERD.add_ref` method. If a 'key_name' |
| 257 |
# is used, a new :py:class:`~hdmf.common.resources.Key` will be created. |
| 258 |
|
| 259 |
herd.add_ref( |
| 260 |
file=file, |
| 261 |
container=genotypes, |
| 262 |
attribute='genotype_name', |
| 263 |
key=genotype_key_object, |
| 264 |
entity_id='ENSEMBL:ENSG00000198963', |
| 265 |
entity_uri='https://uswest.ensembl.org/Homo_sapiens/Gene/Summary?db=core;g=ENSG00000198963' |
| 266 |
) |
| 267 |
|
| 268 |
############################################################################### |
| 269 |
# Using the get_object_entities |
| 270 |
# ------------------------------------------------------ |
| 271 |
# The :py:class:`~hdmf.common.resources.HERD.get_object_entities` method |
| 272 |
# allows the user to retrieve all entities and key information associated with an `Object` in |
| 273 |
# the form of a pandas DataFrame. |
| 274 |
|
| 275 |
herd.get_object_entities(file=file, |
| 276 |
container=genotypes['genotype_name'], |
| 277 |
relative_path='') |
| 278 |
|
| 279 |
############################################################################### |
| 280 |
# Using the get_object_type |
| 281 |
# ------------------------------------------------------ |
| 282 |
# The :py:func:`~hdmf.common.resources.HERD.get_object_entities` method |
| 283 |
# allows the user to retrieve all entities and key information associated with an `Object` in |
| 284 |
# the form of a pandas DataFrame. |
| 285 |
|
| 286 |
herd.get_object_type(object_type='Data') |
| 287 |
|
| 288 |
############################################################################### |
| 289 |
# Special Case: Using add_ref with compound data |
| 290 |
# ------------------------------------------------ |
| 291 |
# In most cases, the field is left as an empty string, but if the dataset or attribute |
| 292 |
# is a compound data_type, then we can use the 'field' value to differentiate the |
| 293 |
# different columns of the dataset. For example, if a dataset has a compound data_type with |
| 294 |
# columns/fields 'x', 'y', and 'z', and each |
| 295 |
# column/field is associated with different ontologies, then use field='x' to denote that |
| 296 |
# 'x' is using the external reference. |
| 297 |
|
| 298 |
# Let's create a new instance of :py:class:`~hdmf.common.resources.HERD`. |
| 299 |
herd = HERD() |
| 300 |
|
| 301 |
data = Data( |
| 302 |
name='data_name', |
| 303 |
data=np.array( |
| 304 |
[('Mus musculus', 9, 81.0), ('Homo sapiens', 3, 27.0)], |
| 305 |
dtype=[('species', 'U14'), ('age', 'i4'), ('weight', 'f4')] |
| 306 |
) |
| 307 |
) |
| 308 |
data.parent = file |
| 309 |
|
| 310 |
herd.add_ref( |
| 311 |
file=file, |
| 312 |
container=data, |
| 313 |
field='species', |
| 314 |
key='Mus musculus', |
| 315 |
entity_id='NCBI_TAXON:txid10090', |
| 316 |
entity_uri='https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090' |
| 317 |
) |
| 318 |
|
| 319 |
############################################################################### |
| 320 |
# Using add_ref_termset |
| 321 |
# ------------------------------------------------------ |
| 322 |
# The :py:func:`~hdmf.common.resources.HERD.add_ref_termset` |
| 323 |
# method allows users to not only validate terms, i.e., keys, but also |
| 324 |
# add references for an entire datasets, rather than single entries as we saw |
| 325 |
# prior with :py:func:`~hdmf.common.resources.HERD.add_ref`. |
| 326 |
|
| 327 |
# :py:func:`~hdmf.common.resources.HERD.add_ref_termset` has many optional fields, |
| 328 |
# giving the user a range of control when adding references. Let's see an example. |
| 329 |
herd = HERD() |
| 330 |
terms = TermSet(term_schema_path=yaml_file) |
| 331 |
|
| 332 |
herd.add_ref_termset(file=file, |
| 333 |
container=species, |
| 334 |
attribute='Species_Data', |
| 335 |
key='Ursus arctos horribilis', |
| 336 |
termset=terms) |
| 337 |
|
| 338 |
############################################################################### |
| 339 |
# Using add_ref_termset for an entire dataset |
| 340 |
# ------------------------------------------------------ |
| 341 |
# As mentioned above, :py:func:`~hdmf.common.resources.HERD.add_ref_termset` |
| 342 |
# supports iteratively validating and populating :py:class:`~hdmf.common.resources.HERD`. |
| 343 |
|
| 344 |
# When populating :py:class:`~hdmf.common.resources.HERD`, users may have some terms |
| 345 |
# that are not in the :py:class:`~hdmf.term_set.TermSet`. As a result, |
| 346 |
# :py:func:`~hdmf.common.resources.HERD.add_ref_termset` will return all of the missing |
| 347 |
# terms in a dictionary. It is up to the user to either add these terms to the |
| 348 |
# :py:class:`~hdmf.term_set.TermSet` or remove them from the dataset. |
| 349 |
|
| 350 |
herd = HERD() |
| 351 |
terms = TermSet(term_schema_path=yaml_file) |
| 352 |
|
| 353 |
herd.add_ref_termset(file=file, |
| 354 |
container=species, |
| 355 |
attribute='Species_Data', |
| 356 |
termset=terms) |
| 357 |
|
| 358 |
############################################################################### |
| 359 |
# Write HERD |
| 360 |
# ------------------------------------------------------ |
| 361 |
# :py:class:`~hdmf.common.resources.HERD` is written as a zip file of |
| 362 |
# the individual tables written to tsv. |
| 363 |
# The user provides the path, which contains the name of the file. |
| 364 |
|
| 365 |
herd.to_zip(path='./HERD.zip') |
| 366 |
|
| 367 |
############################################################################### |
| 368 |
# Read HERD |
| 369 |
# ------------------------------------------------------ |
| 370 |
# Users can read :py:class:`~hdmf.common.resources.HERD` from the zip file |
| 371 |
# by providing the path to the file itself. |
| 372 |
|
| 373 |
er_read = HERD.from_zip(path='./HERD.zip') |
| 374 |
os.remove('./HERD.zip') |