Skip to content

Commit 06ee091

Browse files
committed
convert attachment objects sbol2 to sbol3
1 parent ec693ec commit 06ee091

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

sbol_utilities/sbol3_sbol2_conversion.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,36 +150,24 @@ def visit_association(self, a: sbol3.Association):
150150
raise NotImplementedError('Conversion of Association from SBOL3 to SBOL2 not yet implemented')
151151

152152
def visit_attachment(self, a: sbol3.Attachment):
153-
# Make the Attachment object and add it to the document
154153
att2 = sbol2.Attachment(self._sbol2_identity(a), source=a.source, version=self._sbol2_version(a))
155154
self.doc2.addAttachment(att2)
156155

157-
# Handle hash and hash_algorithm properties
158156
if a.hash:
159-
# Check if hash_algorithm is specified
160-
hash_algorithm = a.hash_algorithm
161-
162157
# Check if it's SHA1 (SBOL2 only supports SHA1)
163-
# Common SHA1 identifiers
164-
sha1_identifiers = [
165-
'SHA1',
166-
'sha1',
167-
'SHA-1',
168-
'sha-1'
169-
]
158+
hash_algorithm = a.hash_algorithm
170159

171-
if any(sha1_id in str(hash_algorithm) for sha1_id in sha1_identifiers):
160+
if hash_algorithm.replace("-", "").replace(" ", "").lower() == 'sha1':
172161
# It's SHA1, so we can set it directly in SBOL2
173162
att2.hash = a.hash
174163
else:
175164
# It's not SHA1, add as backport extension properties
176165
att2.properties[BACKPORT_NAMESPACE + 'hash'] = [Literal(a.hash)]
177166
att2.properties[BACKPORT_NAMESPACE + 'hashAlgorithm'] = [Literal(hash_algorithm)]
178167

179-
att2.format = a.format
168+
att2.format = str(a.format)
180169
att2.size = a.size
181170

182-
# Map over all other TopLevel properties and extensions not covered by the constructor
183171
self._convert_toplevel(a, att2)
184172

185173

@@ -571,8 +559,21 @@ def visit_association(self, a: sbol2.Association):
571559
raise NotImplementedError('Conversion of Association from SBOL2 to SBOL3 not yet implemented')
572560

573561
def visit_attachment(self, a: sbol2.Attachment):
574-
# Priority: 2
575-
raise NotImplementedError('Conversion of Attachment from SBOL2 to SBOL3 not yet implemented')
562+
att3 = sbol3.Attachment(self._sbol3_identity(a), namespace=self._sbol3_namespace(a), source=a.source)
563+
self.doc3.add(att3)
564+
565+
# Check for backported hash properties first (higher priority)
566+
if BACKPORT_NAMESPACE + 'hash' in a.properties:
567+
att3.hash = a.properties[BACKPORT_NAMESPACE + 'hash'][0]
568+
att3.hash_algorithm = a.properties[BACKPORT_NAMESPACE + 'hashAlgorithm'][0]
569+
elif a.hash:
570+
att3.hash = a.hash
571+
att3.hash_algorithm = 'sha1'
572+
573+
att3.format = str(a.format)
574+
att3.size = a.size
575+
576+
self._convert_toplevel(a, att3)
576577

577578
def visit_collection(self, coll2: sbol2.Collection):
578579
# Make the Collection object and add it to the document

0 commit comments

Comments
 (0)