Search in sources :

Example 1 with SequenceConstraint

use of org.sbolstandard.core2.SequenceConstraint in project libSBOLj by SynBioDex.

the class SBOLReader method parseSequenceConstraint.

private static SequenceConstraint parseSequenceConstraint(NestedDocument<QName> sequenceConstraint) throws SBOLValidationException {
    // URIcompliance.extractDisplayId(sequenceConstraint.getIdentity());
    String displayId = null;
    String name = null;
    String description = null;
    // URI.create(URIcompliance.extractPersistentId(sequenceConstraint.getIdentity()));
    URI persistentIdentity = null;
    URI restriction = null;
    URI subject = null;
    URI object = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : sequenceConstraint.getProperties()) {
        if (namedProperty.getName().equals(Sbol2Terms.Identified.persistentIdentity)) {
            if (!(namedProperty.getValue() instanceof Literal) || persistentIdentity != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10203", sequenceConstraint.getIdentity());
            }
            persistentIdentity = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.SequenceConstraint.restriction)) {
            if (!(namedProperty.getValue() instanceof Literal) || restriction != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-11407", sequenceConstraint.getIdentity());
            }
            restriction = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.SequenceConstraint.hasSubject)) {
            if (!(namedProperty.getValue() instanceof Literal) || subject != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-11402", sequenceConstraint.getIdentity());
            }
            subject = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.SequenceConstraint.hasObject)) {
            if (!(namedProperty.getValue() instanceof Literal) || object != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-11404", sequenceConstraint.getIdentity());
            }
            object = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || displayId != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", sequenceConstraint.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.title)) {
            if (!(namedProperty.getValue() instanceof Literal) || name != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", sequenceConstraint.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || description != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", sequenceConstraint.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.version)) {
            if (!(namedProperty.getValue() instanceof Literal) || version != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10206", sequenceConstraint.getIdentity());
            }
            version = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasDerivedFrom)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10208", sequenceConstraint.getIdentity());
            }
            wasDerivedFroms.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasGeneratedBy)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10221", sequenceConstraint.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    SequenceConstraint s = new SequenceConstraint(sequenceConstraint.getIdentity(), restriction, subject, object);
    if (displayId != null)
        s.setDisplayId(displayId);
    if (name != null)
        s.setName(name);
    if (description != null)
        s.setDescription(description);
    if (persistentIdentity != null)
        s.setPersistentIdentity(persistentIdentity);
    if (version != null)
        s.setVersion(version);
    s.setWasDerivedFroms(wasDerivedFroms);
    s.setWasGeneratedBys(wasGeneratedBys);
    if (!annotations.isEmpty())
        s.setAnnotations(annotations);
    return s;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) Literal(org.sbolstandard.core.datatree.Literal) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) HashSet(java.util.HashSet)

Example 2 with SequenceConstraint

use of org.sbolstandard.core2.SequenceConstraint in project libSBOLj by SynBioDex.

the class ComponentDefinition method copy.

void copy(ComponentDefinition componentDefinition) throws SBOLValidationException {
    ((TopLevel) this).copy((TopLevel) componentDefinition);
    for (URI role : componentDefinition.getRoles()) {
        this.addRole(URI.create(role.toString()));
    }
    for (Component component : componentDefinition.getComponents()) {
        String displayId = URIcompliance.findDisplayId(component);
        Component newComponent = this.createComponent(displayId, component.getAccess(), component.getDefinitionURI());
        newComponent.copy(component);
    }
    for (SequenceConstraint sequenceConstraint : componentDefinition.getSequenceConstraints()) {
        String displayId = URIcompliance.findDisplayId(sequenceConstraint);
        String subjectDisplayId = URIcompliance.findDisplayId(sequenceConstraint.getSubject());
        String objectDisplayId = URIcompliance.findDisplayId(sequenceConstraint.getObject());
        SequenceConstraint newSequenceConstraint = this.createSequenceConstraint(displayId, sequenceConstraint.getRestriction(), subjectDisplayId, objectDisplayId);
        newSequenceConstraint.copy(sequenceConstraint);
    }
    for (SequenceAnnotation sequenceAnnotation : componentDefinition.getSequenceAnnotations()) {
        String displayId = URIcompliance.findDisplayId(sequenceAnnotation);
        SequenceAnnotation newSequenceAnnotation = this.createSequenceAnnotation(displayId, "DUMMY__LOCATION");
        newSequenceAnnotation.copy(sequenceAnnotation);
    }
    this.setSequences(componentDefinition.getSequenceURIs());
}
Also used : URI(java.net.URI) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI)

Example 3 with SequenceConstraint

use of org.sbolstandard.core2.SequenceConstraint in project libSBOLj by SynBioDex.

the class ComponentDefinition method copy.

/* (non-Javadoc)
	 * @see org.sbolstandard.core2.abstract_classes.TopLevel#copy(java.lang.String, java.lang.String, java.lang.String)
	 */
/**
 * @throws SBOLValidationException if an SBOL validation rule violation occurred in any of the following constructors or methods:
 * <ul>
 * <li>{@link #deepCopy()},</li>
 * <li>{@link URIcompliance#createCompliantURI(String, String, String)},</li>
 * <li>{@link #setDisplayId(String)},</li>
 * <li>{@link #setVersion(String)},</li>
 * <li>{@link #setWasDerivedFrom(URI)},</li>
 * <li>{@link #setIdentity(URI)}</li>
 * <li>{@link Component#setDisplayId(String)}</li>
 * <li>{@link Component#updateCompliantURI(String, String, String)},</li>
 * <li>{@link #addComponent(Component)},</li>
 * <li>{@link SequenceConstraint#setDisplayId(String)}</li>
 * <li>{@link SequenceConstraint#updateCompliantURI(String, String, String)},</li>
 * <li>{@link #addSequenceConstraint(SequenceConstraint)},</li>
 * <li>{@link SequenceAnnotation#setDisplayId(String)}</li>
 * <li>{@link SequenceAnnotation#updateCompliantURI(String, String, String)}, or</li>
 * <li>{@link #addSequenceAnnotation(SequenceAnnotation)},</li>
 * </ul>
 */
@Override
ComponentDefinition copy(String URIprefix, String displayId, String version) throws SBOLValidationException {
    ComponentDefinition cloned = this.deepCopy();
    cloned.setPersistentIdentity(createCompliantURI(URIprefix, displayId, ""));
    cloned.setDisplayId(displayId);
    cloned.setVersion(version);
    URI newIdentity = createCompliantURI(URIprefix, displayId, version);
    if (!this.getIdentity().equals(newIdentity)) {
        cloned.addWasDerivedFrom(this.getIdentity());
    } else {
        cloned.setWasDerivedFroms(this.getWasDerivedFroms());
    }
    cloned.setIdentity(newIdentity);
    int count = 0;
    for (Component component : cloned.getComponents()) {
        if (!component.isSetDisplayId())
            component.setDisplayId("component" + ++count);
        component.updateCompliantURI(cloned.getPersistentIdentity().toString(), component.getDisplayId(), version);
        cloned.removeChildSafely(component, cloned.components);
        cloned.addComponent(component);
    }
    count = 0;
    for (SequenceConstraint sequenceConstraint : cloned.getSequenceConstraints()) {
        if (!sequenceConstraint.isSetDisplayId())
            sequenceConstraint.setDisplayId("sequenceConstraint" + ++count);
        sequenceConstraint.updateCompliantURI(cloned.getPersistentIdentity().toString(), sequenceConstraint.getDisplayId(), version);
        cloned.removeChildSafely(sequenceConstraint, cloned.sequenceConstraints);
        cloned.addSequenceConstraint(sequenceConstraint);
    }
    count = 0;
    for (SequenceAnnotation sequenceAnnotation : cloned.getSequenceAnnotations()) {
        if (!sequenceAnnotation.isSetDisplayId())
            sequenceAnnotation.setDisplayId("sequenceAnnotation" + ++count);
        sequenceAnnotation.updateCompliantURI(cloned.getPersistentIdentity().toString(), sequenceAnnotation.getDisplayId(), version);
        cloned.removeChildSafely(sequenceAnnotation, cloned.sequenceAnnotations);
        cloned.addSequenceAnnotation(sequenceAnnotation);
    }
    return cloned;
}
Also used : URI(java.net.URI) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI)

Example 4 with SequenceConstraint

use of org.sbolstandard.core2.SequenceConstraint in project libSBOLj by SynBioDex.

the class SBOLDocument method updateReferences.

// TODO: need to update persistentIdentities too
private void updateReferences(HashMap<URI, URI> uriMap) throws SBOLValidationException {
    for (TopLevel topLevel : getTopLevels()) {
        for (URI wasDerivedFrom : topLevel.getWasDerivedFroms()) {
            if (uriMap.get(wasDerivedFrom) != null) {
                topLevel.removeWasDerivedFrom(wasDerivedFrom);
                topLevel.addWasDerivedFrom(uriMap.get(wasDerivedFrom));
            }
        }
        for (URI wasGeneratedBy : topLevel.getWasGeneratedBys()) {
            if (uriMap.get(wasGeneratedBy) != null) {
                topLevel.removeWasGeneratedBy(wasGeneratedBy);
                topLevel.addWasGeneratedBy(uriMap.get(wasGeneratedBy));
            }
        }
        for (URI attachmentURI : topLevel.getAttachmentURIs()) {
            if (uriMap.get(attachmentURI) != null) {
                topLevel.removeAttachment(attachmentURI);
                topLevel.addAttachment(uriMap.get(attachmentURI));
            }
        }
    }
    for (Collection collection : getCollections()) {
        for (URI memberURI : collection.getMemberURIs()) {
            if (uriMap.get(memberURI) != null) {
                collection.removeMember(memberURI);
                collection.addMember(uriMap.get(memberURI));
            }
        }
        updateReferences(collection, uriMap);
    }
    for (ComponentDefinition componentDefinition : getComponentDefinitions()) {
        updateReferences(componentDefinition, uriMap);
        for (Component component : componentDefinition.getComponents()) {
            if (uriMap.get(component.getDefinitionURI()) != null) {
                component.setDefinition(uriMap.get(component.getDefinitionURI()));
                for (MapsTo mapsTo : component.getMapsTos()) {
                    ComponentDefinition cd = getComponentDefinition(component.getDefinitionURI());
                    if (cd != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(component, uriMap);
            for (MapsTo mapsTo : component.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
        }
        for (SequenceAnnotation sa : componentDefinition.getSequenceAnnotations()) {
            for (Location loc : sa.getLocations()) {
                updateReferences(loc, uriMap);
            }
            updateReferences(sa, uriMap);
        }
        for (SequenceConstraint sc : componentDefinition.getSequenceConstraints()) {
            updateReferences(sc, uriMap);
        }
        for (URI sequenceURI : componentDefinition.getSequenceURIs()) {
            if (uriMap.get(sequenceURI) != null) {
                componentDefinition.removeSequence(sequenceURI);
                componentDefinition.addSequence(uriMap.get(sequenceURI));
            }
        }
    }
    for (ModuleDefinition moduleDefinition : getModuleDefinitions()) {
        updateReferences(moduleDefinition, uriMap);
        for (FunctionalComponent functionalComponent : moduleDefinition.getFunctionalComponents()) {
            if (uriMap.get(functionalComponent.getDefinitionURI()) != null) {
                functionalComponent.setDefinition(uriMap.get(functionalComponent.getDefinitionURI()));
                for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
                    ComponentDefinition cd = getComponentDefinition(functionalComponent.getDefinitionURI());
                    if (cd != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(functionalComponent, uriMap);
            for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
        }
        for (Module module : moduleDefinition.getModules()) {
            if (uriMap.get(module.getDefinitionURI()) != null) {
                module.setDefinition(uriMap.get(module.getDefinitionURI()));
                for (MapsTo mapsTo : module.getMapsTos()) {
                    ModuleDefinition md = getModuleDefinition(module.getDefinitionURI());
                    if (md != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(md.getPersistentIdentity().toString(), displayId, md.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(module, uriMap);
            for (MapsTo mapsTo : module.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
        }
        for (Interaction interaction : moduleDefinition.getInteractions()) {
            updateReferences(interaction, uriMap);
            for (Participation participation : interaction.getParticipations()) {
                updateReferences(participation, uriMap);
            }
        }
        for (URI modelURI : moduleDefinition.getModelURIs()) {
            if (uriMap.get(modelURI) != null) {
                moduleDefinition.removeModel(modelURI);
                moduleDefinition.addModel(uriMap.get(modelURI));
            }
        }
    }
    for (Model model : getModels()) {
        updateReferences(model, uriMap);
    }
    for (Sequence sequence : getSequences()) {
        updateReferences(sequence, uriMap);
    }
    for (Attachment attachment : getAttachments()) {
        updateReferences(attachment, uriMap);
    }
    for (Implementation implementation : getImplementations()) {
        if (implementation.isSetBuilt()) {
            URI built = implementation.getBuiltURI();
            if (uriMap.get(built) != null) {
                implementation.setBuilt(uriMap.get(built));
            }
        }
        updateReferences(implementation, uriMap);
    }
    for (GenericTopLevel genericTopLevel : getGenericTopLevels()) {
        updateReferences(genericTopLevel, uriMap);
    }
    for (CombinatorialDerivation combinatorialDerivation : getCombinatorialDerivations()) {
        updateReferences(combinatorialDerivation, uriMap);
        if (uriMap.get(combinatorialDerivation.getTemplateURI()) != null) {
            combinatorialDerivation.setTemplate(uriMap.get(combinatorialDerivation.getTemplateURI()));
            ComponentDefinition cd = getComponentDefinition(combinatorialDerivation.getTemplateURI());
            if (cd != null) {
                for (VariableComponent variableComponent : combinatorialDerivation.getVariableComponents()) {
                    String displayId = URIcompliance.extractDisplayId(variableComponent.getVariableURI());
                    URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                    variableComponent.setVariable(newURI);
                }
            }
        }
        for (VariableComponent variableComponent : combinatorialDerivation.getVariableComponents()) {
            updateReferences(variableComponent, uriMap);
        }
    }
    for (Activity activity : getActivities()) {
        updateReferences(activity, uriMap);
        for (Association association : activity.getAssociations()) {
            if (uriMap.get(association.getAgentURI()) != null) {
                association.setAgent(uriMap.get(association.getAgentURI()));
            }
            if (uriMap.get(association.getPlanURI()) != null) {
                association.setPlan(uriMap.get(association.getPlanURI()));
            }
            updateReferences(association, uriMap);
        }
        for (Usage usage : activity.getUsages()) {
            if (uriMap.get(usage.getEntityURI()) != null) {
                usage.setEntity(uriMap.get(usage.getEntityURI()));
            }
            updateReferences(usage, uriMap);
        }
    }
    for (Agent agent : getAgents()) {
        updateReferences(agent, uriMap);
    }
    for (Plan plan : getPlans()) {
        updateReferences(plan, uriMap);
    }
}
Also used : URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI)

Example 5 with SequenceConstraint

use of org.sbolstandard.core2.SequenceConstraint in project libSBOLj by SynBioDex.

the class SBOLReader method parseDnaComponentV1.

/**
 * @param SBOLDoc
 * @param componentDef
 * @return
 * @throws SBOLValidationException if either of the following conditions is satisfied:
 * <ul>
 * <li>if an SBOL validation rule violation occurred in any of the following constructors or methods:
 * 	<ul>
 * 		<li>{@link URIcompliance#createCompliantURI(String, String, String, String, boolean)},</li>
 * 		<li>{@link #parseSequenceAnnotationV1(SBOLDocument, NestedDocument, List, String, int, Set)},</li>
 * 		<li>{@link URIcompliance#createCompliantURI(String, String, String)},</li>
 * 		<li>{@link Component#Component(URI, AccessType, URI)},</li>
 * 		<li>{@link Component#setDisplayId(String)}, </li>
 * 		<li>{@link Component#setVersion(String)}</li>
 * 		<li>{@link SequenceAnnotation#setComponent(URI)}, </li>
 * 		<li>{@link #parseDnaSequenceV1(SBOLDocument, IdentifiableDocument)}</li>
 * 		<li>{@link RestrictionType#convertToURI(RestrictionType)},</li>
 * 		<li>{@link SequenceConstraint#SequenceConstraint(URI, URI, URI, URI)},</li>
 * 		<li>{@link SequenceConstraint#setDisplayId(String)},</li>
 * 		<li>{@link SequenceConstraint#setVersion(String)},</li>
 * 		<li>{@link ComponentDefinition#ComponentDefinition(URI, Set)},</li>
 * 		<li>{@link ComponentDefinition#setVersion(String)},</li>
 * 		<li>{@link ComponentDefinition#setWasDerivedFrom(URI)}, </li>
 * 		<li>{@link Identified#setAnnotations(List)},</li>
 * 		<li>{@link ComponentDefinition#setComponents(Set)}</li>
 * 		<li>{@link ComponentDefinition#setSequenceConstraints(Set)}</li>
 * 		<li>{@link ComponentDefinition#addSequence(URI)}</li>
 * 		<li>{@link ComponentDefinition#addSequenceAnnotation(SequenceAnnotation)},</li>
 * 		<li>{@link SBOLDocument#addComponentDefinition(ComponentDefinition)}, or</li>
 * 		<li>{@link ComponentDefinition#copy(String, String, String)}; or</li>
 * 	</ul>
 * </li>
 * <li>the following SBOL validation rule was violated: 10202.</li>
 * </ul>
 * @throws SBOLConversionException
 */
private static ComponentDefinition parseDnaComponentV1(SBOLDocument SBOLDoc, IdentifiableDocument<QName> componentDef) throws SBOLValidationException, SBOLConversionException {
    String displayId = null;
    String name = null;
    String description = null;
    URI seq_identity = null;
    Set<URI> roles = new HashSet<>();
    URI identity = componentDef.getIdentity();
    String persIdentity = componentDef.getIdentity().toString();
    List<Annotation> annotations = new ArrayList<>();
    List<SequenceAnnotation> sequenceAnnotations = new ArrayList<>();
    Set<String> instantiatedComponents = new HashSet<>();
    Set<Component> components = new HashSet<>();
    Set<SequenceConstraint> sequenceConstraints = new HashSet<>();
    List<SBOLPair> precedePairs = new ArrayList<>();
    Map<URI, URI> componentDefMap = new HashMap<>();
    Set<URI> type = new HashSet<>();
    type.add(ComponentDefinition.DNA);
    type.add(SequenceOntology.LINEAR);
    int component_num = 0;
    int sa_num = 0;
    if (URIPrefix != null) {
        displayId = URIcompliance.findDisplayId(componentDef.getIdentity().toString());
        identity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, version, typesInURI);
        persIdentity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, "", typesInURI).toString();
    }
    for (NamedProperty<QName> namedProperty : componentDef.getProperties()) {
        if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", componentDef.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
            displayId = URIcompliance.fixDisplayId(displayId);
            if (URIPrefix != null) {
                persIdentity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, "", typesInURI).toString();
                identity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, version, typesInURI);
            }
        } else if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.name)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", componentDef.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", componentDef.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.type)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10507", componentDef.getIdentity());
            }
            URI convertedSO = SequenceOntology.convertSeqOntologyV1(((Literal<QName>) namedProperty.getValue()).getValue().toString());
            roles.add(convertedSO);
        } else if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.annotations)) {
            if (namedProperty.getValue() instanceof IdentifiableDocument) {
                SequenceAnnotation sa = parseSequenceAnnotationV1(SBOLDoc, ((NestedDocument<QName>) namedProperty.getValue()), precedePairs, persIdentity, ++sa_num, instantiatedComponents);
                sequenceAnnotations.add(sa);
                URI component_identity = createCompliantURI(persIdentity, "component" + component_num, version);
                URI component_persIdentity = createCompliantURI(persIdentity, "component" + component_num, "");
                String component_displayId = "component" + component_num;
                AccessType access = AccessType.PUBLIC;
                URI instantiatedComponent = sa.getComponentURI();
                ComponentDefinition instantiatedDef = SBOLDoc.getComponentDefinition(instantiatedComponent);
                if (compliant && instantiatedDef != null && instantiatedDef.isSetDisplayId() && !instantiatedComponents.contains(instantiatedDef.getDisplayId())) {
                    component_identity = createCompliantURI(persIdentity, instantiatedDef.getDisplayId(), version);
                    component_persIdentity = createCompliantURI(persIdentity, instantiatedDef.getDisplayId(), "");
                    component_displayId = instantiatedDef.getDisplayId();
                    instantiatedComponents.add(instantiatedDef.getDisplayId());
                } else {
                    component_num++;
                }
                Component component = new Component(component_identity, access, instantiatedComponent);
                if (!persIdentity.equals("")) {
                    component.setPersistentIdentity(component_persIdentity);
                    component.setDisplayId(component_displayId);
                    component.setVersion(version);
                }
                components.add(component);
                URI originalURI = ((NestedDocument<QName>) namedProperty.getValue()).getIdentity();
                componentDefMap.put(originalURI, component_identity);
                sa.setComponent(component_identity);
            } else {
                throw new SBOLConversionException("SequenceAnnotation must be nested in SBOL1.");
            }
        } else if (namedProperty.getName().equals(Sbol1Terms.DNAComponent.dnaSequence)) {
            if (seq_identity != null) {
                throw new SBOLValidationException("sbol-10512", componentDef.getIdentity());
            }
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-10512", componentDef.getIdentity());
                }
                seq_identity = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
            } else {
                seq_identity = parseDnaSequenceV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue()).getIdentity();
            }
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    if (roles.isEmpty())
        roles.add(SequenceOntology.ENGINEERED_REGION);
    int sc_number = 0;
    for (SBOLPair pair : precedePairs) {
        URI sc_identity = createCompliantURI(persIdentity, "sequenceConstraint" + ++sc_number, version);
        URI restrictionURI = RestrictionType.convertToURI(RestrictionType.PRECEDES);
        // RestrictionType restriction = RestrictionType.convertToRestrictionType(restrictionURI);
        URI subject = null;
        URI object = null;
        for (URI key : componentDefMap.keySet()) {
            if (pair.getLeft().equals(key)) {
                subject = componentDefMap.get(key);
            } else if (pair.getRight().equals(key)) {
                object = componentDefMap.get(key);
            }
        }
        SequenceConstraint sc = null;
        if (compliant && !persIdentity.equals("")) {
            String subjectId = URIcompliance.extractDisplayId(subject);
            String objectId = URIcompliance.extractDisplayId(object);
            sc_identity = createCompliantURI(persIdentity, subjectId + "_cons_" + objectId, version);
            sc = new SequenceConstraint(sc_identity, restrictionURI, subject, object);
            sc.setPersistentIdentity(createCompliantURI(persIdentity, subjectId + "_cons_" + objectId, ""));
            sc.setDisplayId(subjectId + "_cons_" + objectId);
            sc.setVersion(version);
        } else {
            sc = new SequenceConstraint(sc_identity, restrictionURI, subject, object);
        }
        sequenceConstraints.add(sc);
    }
    ComponentDefinition c = new ComponentDefinition(identity, type);
    if (!persIdentity.equals("")) {
        c.setPersistentIdentity(URI.create(persIdentity));
        c.setVersion(version);
    }
    if (roles != null)
        c.setRoles(roles);
    if (identity != componentDef.getIdentity())
        c.addWasDerivedFrom(componentDef.getIdentity());
    if (displayId != null)
        c.setDisplayId(displayId);
    if (name != null && !name.isEmpty())
        c.setName(name);
    if (description != null && !description.isEmpty())
        c.setDescription(description);
    if (seq_identity != null)
        c.addSequence(seq_identity);
    if (!annotations.isEmpty())
        c.setAnnotations(annotations);
    if (!components.isEmpty())
        c.setComponents(components);
    if (!sequenceAnnotations.isEmpty()) {
        for (SequenceAnnotation sa : sequenceAnnotations) {
            if (!dropObjectsWithDuplicateURIs || c.getSequenceAnnotation(sa.getIdentity()) == null) {
                c.addSequenceAnnotation(sa);
            }
        }
    }
    if (!sequenceConstraints.isEmpty())
        c.setSequenceConstraints(sequenceConstraints);
    ComponentDefinition oldC = SBOLDoc.getComponentDefinition(identity);
    if (oldC == null) {
        SBOLDoc.addComponentDefinition(c);
    } else if (c.getWasDerivedFroms().size() > 0 && oldC.getWasDerivedFroms().size() > 0 && !c.getWasDerivedFroms().equals(oldC.getWasDerivedFroms())) {
        URI wasDerivedFrom = (URI) c.getWasDerivedFroms().toArray()[0];
        Set<TopLevel> topLevels = SBOLDoc.getByWasDerivedFrom(wasDerivedFrom);
        for (TopLevel topLevel : topLevels) {
            if (topLevel instanceof ComponentDefinition) {
                return (ComponentDefinition) topLevel;
            }
        }
        do {
            displayId = displayId + "_";
            identity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, version, typesInURI);
            persIdentity = createCompliantURI(URIPrefix, TopLevel.COMPONENT_DEFINITION, displayId, "", typesInURI).toString();
        } while (SBOLDoc.getComponentDefinition(identity) != null);
        c = c.copy(URIPrefix, displayId, version);
        if (identity != componentDef.getIdentity()) {
            c.clearWasDerivedFroms();
            c.addWasDerivedFrom(componentDef.getIdentity());
        }
        SBOLDoc.addComponentDefinition(c);
    } else if (dropObjectsWithDuplicateURIs) {
        return oldC;
    } else {
        if (!c.equals(oldC)) {
            throw new SBOLValidationException("sbol-10202", c);
        }
    }
    return c;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) NestedDocument(org.sbolstandard.core.datatree.NestedDocument) Literal(org.sbolstandard.core.datatree.Literal) HashSet(java.util.HashSet) StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) IdentifiableDocument(org.sbolstandard.core.datatree.IdentifiableDocument)

Aggregations

URI (java.net.URI)9 URIcompliance.createCompliantURI (org.sbolstandard.core2.URIcompliance.createCompliantURI)8 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 QName (javax.xml.namespace.QName)3 Literal (org.sbolstandard.core.datatree.Literal)3 StringifyQName (org.sbolstandard.core.io.json.StringifyQName)3 IdentifiableDocument (org.sbolstandard.core.datatree.IdentifiableDocument)2 NestedDocument (org.sbolstandard.core.datatree.NestedDocument)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Test (org.junit.Test)1 ComponentDefinition (org.sbolstandard.core2.ComponentDefinition)1 SequenceConstraint (org.sbolstandard.core2.SequenceConstraint)1