SynBioDex / libSBOLj

Java Library for Synthetic Biology Open Language (SBOL)
Apache License 2.0
37 stars 24 forks source link

Inverted recursive copy method #553

Open cjmyers opened 6 years ago

cjmyers commented 6 years ago

It would be useful to have an inverted recursive copy method. What this is used for is when you change an object that is referenced by other objects, it would work backwards on all the references making a copy of the referring objects and updating the references to the new copy. It could copy into a new namespace or a new version. It could also add wasDerivedFrom links to the original. Here is a rough outline of the code (note the map starts with the URI of the old object and the new object that the copy should start from):

UpdateSBOL(SBOLDocument doc, HashMap<URI,URI> map) { for (ComponentDefinition cd : doc.getRootComponentDefinitions) { UpdateSBOLRecurse(doc, cd, map); } }

UpdateSBOLRecurse(SBOLDocument doc, cd, HashMap<URI,URI> map) { ComponentDefinition copyCD = null; for (Component comp : cd.getComponents()) { if (map.get(comp.getDefinitionURI())!=null) { if (copyCD == null) { copyCD = doc.createCopy(cd,boostNamespace,null,null); // Add wasDerivedFrom / wasGeneratedBy links from copyCD to cd } Component copyComp = copyCD.getComponent(comp.getDisplayId()); copyComp.setDefinition(map.get(comp.getDefinitionURI()); map.add(cd.getIdentity(),copyCD.getIdentity()); } else { UpdateSBOL(doc, comp.getDefinition(), map); if (map.get(comp.getDefinitionURI())!=null) { if (copyCD == null) { copyCD = doc.createCopy(cd,boostNamespace,null,null); // Add wasDerivedFrom / wasGeneratedBy links from copyCD to cd } Component copyComp = copyCD.getComponent(comp.getDisplayId()); copyComp.setDefinition(map.get(comp.getDefinitionURI()); map.add(cd.getIdentity(),copyCD.getIdentity()); } } } If (copyCD != null) { String newSeq = copyCD.getImpliedNucleicAcidSequence); Sequence seq = doc.createSequence(…, newSeq) copyCD.clearSequences(); copyCD.addSequence(seq); } }