SynBioDex / pySBOL

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

Activity usages #97

Open eoberortner opened 5 years ago

eoberortner commented 5 years ago

Hi, how do you set the qualifiedUsages of an Activity properly?

The following piece of code does not serialize the usages of the Activity:

    document = Document()

    original_cd = ComponentDefinition('original_CD')
    document.addComponentDefinition(original_cd)

    my_activity = Activity('my_activity')
    activity_usages = [original_cd.identity]
    my_activity.usages = activity_usages
    document.addActivity(my_activity)

    updated_cd = ComponentDefinition('updated_CD')
    updated_cd.wasDerivedFrom = original_cd.identity
    updated_cd.wasGeneratedBy = my_activity.identity
    document.addComponentDefinition(updated_cd)

    print(document.writeString())

And here's the generated serialized document that I get:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:dcterms="http://purl.org/dc/terms/"
   xmlns:prov="http://www.w3.org/ns/prov#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:sbol="http://sbols.org/v2#"
   xmlns:sys-bio="http://sys-bio.org#">
  <prov:Activity rdf:about="http://examples.org/Activity/my_activity/1">
    <sbol:displayId>my_activity</sbol:displayId>
    <sbol:persistentIdentity rdf:resource="http://examples.org/Activity/my_activity"/>
    <sbol:version>1</sbol:version>
  </prov:Activity>
  <sbol:ComponentDefinition rdf:about="http://examples.org/ComponentDefinition/original_CD/1">
    <sbol:displayId>original_CD</sbol:displayId>
    <sbol:persistentIdentity rdf:resource="http://examples.org/ComponentDefinition/original_CD"/>
    <sbol:type rdf:resource="http://www.biopax.org/release/biopax-level3.owl#DnaRegion"/>
    <sbol:version>1</sbol:version>
  </sbol:ComponentDefinition>
  <sbol:ComponentDefinition rdf:about="http://examples.org/ComponentDefinition/updated_CD/1">
    <sbol:displayId>updated_CD</sbol:displayId>
    <sbol:persistentIdentity rdf:resource="http://examples.org/ComponentDefinition/updated_CD"/>
    <sbol:type rdf:resource="http://www.biopax.org/release/biopax-level3.owl#DnaRegion"/>
    <sbol:version>1</sbol:version>
    <prov:wasDerivedFrom rdf:resource="http://examples.org/ComponentDefinition/original_CD/1"/>
    <prov:wasGeneratedBy rdf:resource="http://examples.org/Activity/my_activity/1"/>
  </sbol:ComponentDefinition>
</rdf:RDF>

My other question is creating your own list and assigning it to an SBOL object's property (1) vs. using python's functions (.append(), .extend(),...) for adding objects to a list (2)? Here's an example: (1)

    activity_usages = [original_cd.identity]
    my_activity.usages = activity_usages

vs (2)

    my_activity.usages.append(original_cd.identity)

When using (2), the following error comes up "AttributeError: 'OwnedUsage' object has no attribute 'append'" How do I know when to use option (1) and when to use option (2)? Based on when an error is thrown?

Thanks for your help!

bbartley commented 5 years ago

Hi, one thing to keep in mind is that Activity.usages expects a Usage object, not a URI reference. So, something like this:

>>> product = ComponentDefinition('product')
>>> substrate = ComponentDefinition('substrate')
>>> a = Activity('a')
>>> product.wasGeneratedBy = a.identity
>>> u = a.usages.create('u')
>>> u.entity = substrate.identity 

In general when modifying properties that can contain multiple values, you have to use the following idiom:

>>> product.roles = SO + "0000001"
>>> product.roles
['http://identifiers.org/so/SO:0000001']
>>> product.roles += [SO + "1234567"]
>>> product.roles
['http://identifiers.org/so/SO:0000001', 'http://identifiers.org/so/SO:1234567']

It's doesn't behave exactly like a pythonic list, because it's actually a wrapped C++ data structure. So, avoid using append and extend methods.

eoberortner commented 5 years ago

Thanks Bryan for pointing out that there’s a Usage object needed and to avoid python list operations! It’ll still take some time for me to understand the python SBOL library.

Thanks, Ernst

On Jun 11, 2019, at 7:21 AM, bbartley notifications@github.com wrote:

Hi, one thing to keep in mind is that Activity.usages expects a Usage object, not a URI reference. So, something like this:

product = ComponentDefinition('product') substrate = ComponentDefinition('substrate') a = Activity('a') product.wasGeneratedBy = a.identity u = a.usages.create('u') u.entity = substrate.identity In general when modifying properties that can contain multiple values, you have to use the following idiom:

product.roles = SO + "0000001" product.roles ['http://identifiers.org/so/SO:0000001'] product.roles += [SO + "1234567"] product.roles ['http://identifiers.org/so/SO:0000001', 'http://identifiers.org/so/SO:1234567'] It's doesn't behave exactly like a pythonic list, because it's actually a wrapped C++ data structure. So, avoid using append and extend methods.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SynBioDex/pySBOL/issues/97?email_source=notifications&email_token=AAKQ4ZXTPEOZMAKDGT5K7L3PZ6YGHA5CNFSM4HW6LMDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXNJF7Y#issuecomment-500863743, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKQ4ZWCLMOEQ3CGCB4KKBLPZ6YGHANCNFSM4HW6LMDA.