@@ -387,6 +387,21 @@ def __call__(self, o: BlitzObjectWrapper) -> URIRef:
387
387
data = encoder .encode (o )
388
388
return self .handle (data )
389
389
390
+ def annotations (self , obj , objid ):
391
+ """
392
+ Loop through all annotations and handle them individually.
393
+ """
394
+ if isinstance (obj , IObject ):
395
+ # Not a wrapper object
396
+ for annotation in obj .linkedAnnotationList ():
397
+ annid = self (annotation )
398
+ self .contains (objid , annid )
399
+ else :
400
+ for annotation in obj .listAnnotations (None ):
401
+ obj ._loadAnnotationLinks ()
402
+ annid = self (annotation )
403
+ self .contains (objid , annid )
404
+
390
405
def handle (self , data : Data ) -> URIRef :
391
406
"""
392
407
Parses the data object into RDF triples.
@@ -412,6 +427,15 @@ def handle(self, data: Data) -> URIRef:
412
427
413
428
return _id
414
429
430
+ def contains (self , parent , child ):
431
+ """
432
+ Use emit to generate isPartOf and hasPart triples
433
+
434
+ TODO: add an option to only choose one of the two directions.
435
+ """
436
+ self .emit ((child , DCTERMS .isPartOf , parent ))
437
+ self .emit ((parent , DCTERMS .hasPart , child ))
438
+
415
439
def emit (self , triple : Triple ):
416
440
if self .formatter .streaming :
417
441
print (self .formatter .serialize_triple (triple ), file = self .filehandle )
@@ -623,68 +647,56 @@ def descend(
623
647
scrid = handler (scr )
624
648
for plate in scr .listChildren ():
625
649
pltid = self .descend (gateway , plate ._obj , handler )
626
- handler .emit ((pltid , DCTERMS .isPartOf , scrid ))
627
- handler .emit ((scrid , DCTERMS .hasPart , pltid ))
628
- for annotation in scr .listAnnotations (None ):
629
- annid = handler (annotation )
630
- handler .emit ((annid , DCTERMS .isPartOf , scrid ))
650
+ handler .contains (scrid , pltid )
651
+ handler .annotations (scr , scrid )
631
652
return scrid
632
653
633
654
elif isinstance (target , Plate ):
634
655
plt = self ._lookup (gateway , "Plate" , target .id )
635
656
pltid = handler (plt )
636
- for annotation in plt .listAnnotations (None ):
637
- annid = handler (annotation )
638
- handler .emit ((annid , DCTERMS .isPartOf , pltid ))
657
+ handler .annotations (plt , pltid )
639
658
for well in plt .listChildren ():
640
659
wid = handler (well ) # No descend
641
- handler .emit (( wid , DCTERMS . isPartOf , pltid ) )
660
+ handler .contains ( pltid , wid )
642
661
for idx in range (0 , well .countWellSample ()):
643
662
img = well .getImage (idx )
644
663
imgid = self .descend (gateway , img ._obj , handler )
645
- handler .emit ((imgid , DCTERMS .isPartOf , wid ))
646
- handler .emit ((wid , DCTERMS .hasPart , imgid ))
664
+ handler .contains (wid , imgid )
647
665
return pltid
648
666
649
667
elif isinstance (target , Project ):
650
668
prj = self ._lookup (gateway , "Project" , target .id )
651
669
prjid = handler (prj )
652
- for annotation in prj .listAnnotations (None ):
653
- annid = handler (annotation )
654
- handler .emit ((annid , DCTERMS .isPartOf , prjid ))
670
+ handler .annotations (prj , prjid )
655
671
for ds in prj .listChildren ():
656
672
dsid = self .descend (gateway , ds ._obj , handler )
657
- handler .emit ((dsid , DCTERMS .isPartOf , prjid ))
658
- handler .emit ((prjid , DCTERMS .hasPart , dsid ))
673
+ handler .contains (prjid , dsid )
659
674
return prjid
660
675
661
676
elif isinstance (target , Dataset ):
662
677
ds = self ._lookup (gateway , "Dataset" , target .id )
663
678
dsid = handler (ds )
664
- for annotation in ds .listAnnotations (None ):
665
- annid = handler (annotation )
666
- handler .emit ((annid , DCTERMS .isPartOf , dsid ))
679
+ handler .annotations (ds , dsid )
667
680
for img in ds .listChildren ():
668
681
imgid = self .descend (gateway , img ._obj , handler )
669
- handler .emit ((imgid , DCTERMS .isPartOf , dsid ))
670
- handler .emit ((dsid , DCTERMS .hasPart , imgid ))
682
+ handler .contains (dsid , imgid )
671
683
return dsid
672
684
673
685
elif isinstance (target , Image ):
674
686
img = self ._lookup (gateway , "Image" , target .id )
675
687
imgid = handler (img )
676
688
if img .getPrimaryPixels () is not None :
677
689
pixid = handler (img .getPrimaryPixels ())
678
- handler .emit ((pixid , DCTERMS .isPartOf , imgid ))
679
- handler .emit ((imgid , DCTERMS .hasPart , pixid ))
680
- for annotation in img .listAnnotations (None ):
681
- img ._loadAnnotationLinks ()
682
- annid = handler (annotation )
683
- handler .emit ((annid , DCTERMS .isPartOf , imgid ))
690
+ handler .contains (imgid , pixid )
691
+ handler .annotations (img , imgid )
684
692
for roi in self ._get_rois (gateway , img ):
685
693
roiid = handler (roi )
686
- handler .emit ((roiid , DCTERMS .isPartOf , pixid ))
687
- handler .emit ((pixid , DCTERMS .hasPart , roiid ))
694
+ handler .annotations (roi , roiid )
695
+ handler .contains (pixid , roiid )
696
+ for shape in roi .iterateShapes ():
697
+ shapeid = handler (shape )
698
+ handler .annotations (shape , shapeid )
699
+ handler .contains (roiid , shapeid )
688
700
return imgid
689
701
690
702
else :
0 commit comments