SynBioDex / pySBOL

SWIG-Python wrappers implementing SBOL 2.2
Apache License 2.0
25 stars 8 forks source link

How do I delete specific objects from a Document? #103

Closed dashaveraksa closed 5 years ago

dashaveraksa commented 5 years ago

I am trying to delete a ComponentDefinition (that had previously been added to the Document) from a Document, and have tried

doc.close(componentURI)

which is what I have been able to find in the documentation. (componentURI is a string representing the URI of the ComponentDefinition in this case). However, I am getting an AttributeError, which says that 'Document' object has no attribute 'close'. I have also tried using delete(), remove(), and clear(), but neither of those worked either. Is there a different way I can delete specific objects from Document?

Ideally, I also want to be able to delete a Collection from a Document without all the objects contained within it disappearing from the Document. Is this possible, and how could I go about this?

Thank you.

bbartley commented 5 years ago

remove(<uri>) method is the right one to use here. In the following example, an object is removed from the Document, and a reference to the object is returned to the user...

clear() will work also, but it will delete all objects in the store, and no reference to the objects will be retained

>>> from sbol import *
>>> doc = Document()
>>> cd = doc.componentDefinitions.create('cd')
>>> print(doc)
Attachment....................0
Collection....................0
CombinatorialDerivation.......0
ComponentDefinition...........1
Experiment....................0
Test..........................0
Implementation................0
Model.........................0
ModuleDefinition..............0
Sequence......................0
Analysis......................0
Build.........................0
Design........................0
SampleRoster..................0
Activity......................0
Agent.........................0
Plan..........................0
Annotation Objects............0
---
Total.........................1

>>> cd = doc.componentDefinitions.remove(cd.identity)   # User must specify the full URI
>>> print(cd) 
http://examples.org/ComponentDefinition/cd/1
>>> print(doc)
Attachment....................0
Collection....................0
CombinatorialDerivation.......0
ComponentDefinition...........0
Experiment....................0
Test..........................0
Implementation................0
Model.........................0
ModuleDefinition..............0
Sequence......................0
Analysis......................0
Build.........................0
Design........................0
SampleRoster..................0
Activity......................0
Agent.........................0
Plan..........................0
Annotation Objects............0
---
Total.........................0
dashaveraksa commented 5 years ago

Thank you for the quick reply, this was very helpful.