Search in sources :

Example 51 with SBOLValidationException

use of org.sbolstandard.core2.SBOLValidationException in project ice by JBEI.

the class SBOL2Visitor method visit.

public void visit(Sequence sequence) throws SBOLValidationException, URISyntaxException {
    // ice data model conflates the sequence and component
    Entry entry = sequence.getEntry();
    // Set required properties
    String partId = entry.getPartNumber();
    String dcUri = sequence.getComponentUri();
    if (dcUri == null) {
        componentDefinition = doc.createComponentDefinition(uriString, partId, "1", ComponentDefinition.DNA);
    } else {
        String displayId = StringUtils.isBlank(sequence.getIdentifier()) ? displayIdFromUri(dcUri) : sequence.getIdentifier();
        String prefix = prefixFromUri(dcUri);
        componentDefinition = doc.createComponentDefinition(prefix, displayId, "1", ComponentDefinition.DNA);
    }
    componentDefinition.setName(entry.getName());
    componentDefinition.setDescription(entry.getShortDescription());
    org.sbolstandard.core2.Sequence dnaSequence;
    String dsUri = sequence.getUri();
    if (dsUri == null || dsUri.isEmpty()) {
        dsUri = "sequence_" + sequence.getFwdHash().replaceAll("[\\s\\-()]", "");
        dnaSequence = doc.createSequence(uriString, dsUri, "1", sequence.getSequence(), org.sbolstandard.core2.Sequence.IUPAC_DNA);
    } else {
        dnaSequence = doc.createSequence(prefixFromUri(dsUri), displayIdFromUri(dsUri), "1", sequence.getSequence(), org.sbolstandard.core2.Sequence.IUPAC_DNA);
    }
    dnaSequence.setElements(sequence.getSequence());
    componentDefinition.addSequence(dnaSequence);
    List<SequenceFeature> features = new ArrayList<>(sequence.getSequenceFeatures());
    Collections.sort(features, new SequenceFeatureComparator());
    for (SequenceFeature feature : features) {
        visit(feature);
    }
    componentDefinition.createAnnotation(new QName(ICE_NS, "id", ICE_PREFIX), entry.getId());
    if (entry.getRecordId() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "recordId", ICE_PREFIX), entry.getRecordId());
    if (entry.getVersionId() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "versionId", ICE_PREFIX), entry.getVersionId());
    if (entry.getRecordType() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "recordType", ICE_PREFIX), entry.getRecordType());
    if (entry.getOwner() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "owner", ICE_PREFIX), entry.getOwner());
    if (entry.getOwnerEmail() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "ownerEmail", ICE_PREFIX), entry.getOwnerEmail());
    if (entry.getCreator() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "creator", ICE_PREFIX), entry.getCreator());
    if (entry.getCreatorEmail() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "creatorEmail", ICE_PREFIX), entry.getCreatorEmail());
    if (entry.getStatus() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "status", ICE_PREFIX), entry.getStatus());
    if (entry.getAlias() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "alias", ICE_PREFIX), entry.getAlias());
    for (SelectionMarker selectionMarker : entry.getSelectionMarkers()) {
        componentDefinition.createAnnotation(new QName(ICE_NS, "selectionMarker", ICE_PREFIX), selectionMarker.getName());
    }
    if (entry.getLinks() != null) {
        for (Link link : entry.getLinks()) {
            componentDefinition.createAnnotation(new QName(ICE_NS, link.getLink(), ICE_PREFIX), link.getUrl());
        }
    }
    if (entry.getKeywords() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "keywords", ICE_PREFIX), entry.getKeywords());
    if (entry.getShortDescription() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "shortDescription", ICE_PREFIX), entry.getShortDescription());
    if (entry.getLongDescription() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "longDescription", ICE_PREFIX), entry.getLongDescription());
    if (entry.getLongDescriptionType() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "longDescriptionType", ICE_PREFIX), entry.getLongDescriptionType());
    if (entry.getReferences() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "references", ICE_PREFIX), entry.getReferences());
    if (entry.getCreationTime() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "creationTime", ICE_PREFIX), entry.getCreationTime().toString());
    if (entry.getModificationTime() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "modificationTime", ICE_PREFIX), entry.getModificationTime().toString());
    if (entry.getBioSafetyLevel() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "bioSafetyLevel", ICE_PREFIX), entry.getBioSafetyLevel());
    if (entry.getIntellectualProperty() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "intellectualProperty", ICE_PREFIX), entry.getIntellectualProperty());
    if (entry.getVisibility() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "visibility", ICE_PREFIX), entry.getVisibility());
    if (entry.getParameters() != null) {
        for (Parameter parameter : entry.getParameters()) {
            componentDefinition.createAnnotation(new QName(ICE_NS, parameter.getKey(), ICE_PREFIX), parameter.getValue());
        }
    }
    if (entry.getFundingSource() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "fundingSource", ICE_PREFIX), entry.getFundingSource());
    if (entry.getPrincipalInvestigator() != null)
        componentDefinition.createAnnotation(new QName(ICE_NS, "principalInvestigator", ICE_PREFIX), entry.getPrincipalInvestigator());
// TODO: samples
// TODO: attachments
}
Also used : QName(javax.xml.namespace.QName) org.sbolstandard.core2(org.sbolstandard.core2)

Example 52 with SBOLValidationException

use of org.sbolstandard.core2.SBOLValidationException in project ice by JBEI.

the class SBOL2Formatter method format.

@Override
public void format(Sequence sequence, OutputStream outputStream) throws FormatterException, IOException {
    SBOLDocument doc = new SBOLDocument();
    try {
        SBOL2Visitor visitor = new SBOL2Visitor(doc);
        visitor.visit(sequence);
        doc.write(outputStream);
    } catch (SBOLValidationException | SBOLConversionException | URISyntaxException e) {
        throw new FormatterException(e);
    }
}
Also used : SBOLDocument(org.sbolstandard.core2.SBOLDocument) SBOLConversionException(org.sbolstandard.core2.SBOLConversionException) URISyntaxException(java.net.URISyntaxException) SBOLValidationException(org.sbolstandard.core2.SBOLValidationException)

Example 53 with SBOLValidationException

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

the class SBOLReader method parseSequenceAnnotation.

/**
 * @param sequenceAnnotation
 * @param nested
 * @return
 * @throws SBOLValidationException if either of the following conditions is satisfied:
 * <ul>
 * <li>any of the following SBOL validation rules was violated:
 * 10203, 10204, 10206, 10208, 10212, 10213,
 * 10512,
 * 10904; or
 *</li>
 * <li>an SBOL validation rule violation occurred in the following constructor or methods:
 * 	<ul>
 * 		<li>{@link #parseLocation(NestedDocument)},</li>
 * 		<li>{@link SequenceAnnotation#SequenceAnnotation(URI, Set)},</li>
 * 		<li>{@link SequenceAnnotation#setDisplayId(String)},</li>
 * 		<li>{@link SequenceAnnotation#setVersion(String)},</li>
 * 		<li>{@link SequenceAnnotation#setComponent(URI)},</li>
 * 		<li>{@link SequenceAnnotation#setWasDerivedFrom(URI)}, or</li>
 * 		<li>{@link Identified#setAnnotations(List)}.</li>
 * 	</ul>
 * </li>
 * </ul>
 */
private static SequenceAnnotation parseSequenceAnnotation(NestedDocument<QName> sequenceAnnotation, Map<URI, NestedDocument<QName>> nested) throws SBOLValidationException {
    // URIcompliance.extractDisplayId(sequenceAnnotation.getIdentity());
    String displayId = null;
    String name = null;
    String description = null;
    // URI.create(URIcompliance.extractPersistentId(sequenceAnnotation.getIdentity()));
    URI persistentIdentity = null;
    Location location = null;
    URI componentURI = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    Set<URI> roles = new HashSet<>();
    Set<Location> locations = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : sequenceAnnotation.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", sequenceAnnotation.getIdentity());
            }
            persistentIdentity = 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", sequenceAnnotation.getIdentity());
            }
            displayId = ((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", sequenceAnnotation.getIdentity());
            }
            version = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.SequenceAnnotation.roles)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10906", sequenceAnnotation.getIdentity());
            }
            roles.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Location.Location)) {
            if (namedProperty.getValue() instanceof NestedDocument) {
                NestedDocument<QName> nestedDocument = ((NestedDocument<QName>) namedProperty.getValue());
                if (nestedDocument.getType() == null || !(nestedDocument.getType().equals(Sbol2Terms.Range.Range) || nestedDocument.getType().equals(Sbol2Terms.Cut.Cut) || nestedDocument.getType().equals(Sbol2Terms.GenericLocation.GenericLocation))) {
                    throw new SBOLValidationException("sbol-10902", sequenceAnnotation.getIdentity());
                }
                location = parseLocation((NestedDocument<QName>) namedProperty.getValue());
            } else {
                URI uri = (URI) ((Literal<QName>) namedProperty.getValue()).getValue();
                NestedDocument<QName> nestedDocument = nested.get(uri);
                if (nestedDocument == null || nestedDocument.getType() == null || !(nestedDocument.getType().equals(Sbol2Terms.Range.Range) || nestedDocument.getType().equals(Sbol2Terms.Cut.Cut) || nestedDocument.getType().equals(Sbol2Terms.GenericLocation.GenericLocation))) {
                    throw new SBOLValidationException("sbol-10902", sequenceAnnotation.getIdentity());
                }
                location = parseLocation(nested.get(uri));
            }
            locations.add(location);
        } else if (namedProperty.getName().equals(Sbol2Terms.SequenceAnnotation.hasComponent)) {
            if (!(namedProperty.getValue() instanceof Literal) || componentURI != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10904", sequenceAnnotation.getIdentity());
            }
            componentURI = URI.create(((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", sequenceAnnotation.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", sequenceAnnotation.getIdentity());
            }
            description = ((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", sequenceAnnotation.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", sequenceAnnotation.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    SequenceAnnotation s = new SequenceAnnotation(sequenceAnnotation.getIdentity(), locations);
    if (persistentIdentity != null)
        s.setPersistentIdentity(persistentIdentity);
    if (version != null)
        s.setVersion(version);
    if (displayId != null)
        s.setDisplayId(displayId);
    if (componentURI != null)
        s.setComponent(componentURI);
    if (name != null)
        s.setName(name);
    if (description != null)
        s.setDescription(description);
    s.setWasDerivedFroms(wasDerivedFroms);
    s.setWasGeneratedBys(wasGeneratedBys);
    if (!annotations.isEmpty())
        s.setAnnotations(annotations);
    if (!roles.isEmpty())
        s.setRoles(roles);
    // }
    return s;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) 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)

Example 54 with SBOLValidationException

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

the class SBOLReader method parseVariableComponent.

// TODO: FIX COMMENTED SECTION
@SuppressWarnings("unchecked")
private static VariableComponent parseVariableComponent(SBOLDocument SBOLDoc, NestedDocument<QName> variableComponent, Map<URI, NestedDocument<QName>> nested) throws SBOLValidationException {
    String displayId = null;
    String name = null;
    String description = null;
    URI persistentIdentity = null;
    String version = null;
    List<Annotation> annotations = new ArrayList<>();
    URI variable = null;
    OperatorType operator = null;
    HashSet<URI> variants = new HashSet<>();
    HashSet<URI> variantCollections = new HashSet<>();
    HashSet<URI> variantDerivations = new HashSet<>();
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    for (NamedProperty<QName> namedProperty : variableComponent.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", variableComponent.getIdentity());
            }
            persistentIdentity = URI.create(((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", variableComponent.getIdentity());
            }
            version = ((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", variableComponent.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", variableComponent.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", variableComponent.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.VariableComponent.hasVariable)) {
            if (!(namedProperty.getValue() instanceof Literal) || variable != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-13004", variableComponent.getIdentity());
            }
            variable = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.VariableComponent.hasVariants)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-13007", variableComponent.getIdentity());
                }
                variants.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.ComponentDefinition.ComponentDefinition)) {
                    ComponentDefinition cd = parseComponentDefinition(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue(), nested);
                    variants.add(cd.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-13007", variableComponent.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-13007", variableComponent.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.VariableComponent.hasVariantCollections)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-13009", variableComponent.getIdentity());
                }
                variantCollections.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Collection.Collection)) {
                    Collection cd = parseCollection(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue(), nested);
                    variantCollections.add(cd.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-13009", variableComponent.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-13009", variableComponent.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.VariableComponent.hasVariantDerivations)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-13013", variableComponent.getIdentity());
                }
                variantDerivations.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Collection.Collection)) {
                    CombinatorialDerivation cd = parseCombinatorialDerivation(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue(), nested);
                    variantDerivations.add(cd.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-13013", variableComponent.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-13013", variableComponent.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.VariableComponent.hasOperator)) {
            if (!(namedProperty.getValue() instanceof Literal) || operator != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-13002", variableComponent.getIdentity());
            }
            String operatorTypeStr = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
            try {
                operator = OperatorType.convertToOperatorType(URI.create(operatorTypeStr));
            } catch (SBOLValidationException e) {
                throw new SBOLValidationException("sbol-13003", variableComponent.getIdentity());
            }
        } 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", variableComponent.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", variableComponent.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    VariableComponent c = new VariableComponent(variableComponent.getIdentity(), operator, variable);
    if (persistentIdentity != null)
        c.setPersistentIdentity(persistentIdentity);
    if (version != null)
        c.setVersion(version);
    if (displayId != null)
        c.setDisplayId(displayId);
    if (!variants.isEmpty())
        c.setVariants(variants);
    if (!variantCollections.isEmpty())
        c.setVariantCollections(variantCollections);
    if (!variantDerivations.isEmpty())
        c.setVariantDerivations(variantDerivations);
    if (name != null)
        c.setName(name);
    if (description != null)
        c.setDescription(description);
    if (!annotations.isEmpty())
        c.setAnnotations(annotations);
    return c;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) IdentifiableDocument(org.sbolstandard.core.datatree.IdentifiableDocument) Literal(org.sbolstandard.core.datatree.Literal) HashSet(java.util.HashSet)

Example 55 with SBOLValidationException

use of org.sbolstandard.core2.SBOLValidationException 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)81 URIcompliance.createCompliantURI (org.sbolstandard.core2.URIcompliance.createCompliantURI)75 QName (javax.xml.namespace.QName)36 HashSet (java.util.HashSet)35 ArrayList (java.util.ArrayList)31 Literal (org.sbolstandard.core.datatree.Literal)30 StringifyQName (org.sbolstandard.core.io.json.StringifyQName)30 IdentifiableDocument (org.sbolstandard.core.datatree.IdentifiableDocument)17 Test (org.junit.Test)16 NestedDocument (org.sbolstandard.core.datatree.NestedDocument)13 ComponentDefinition (org.sbolstandard.core2.ComponentDefinition)9 SBOLDocument (org.sbolstandard.core2.SBOLDocument)9 ModuleDefinition (org.sbolstandard.core2.ModuleDefinition)6 Sequence (org.sbolstandard.core2.Sequence)5 Interaction (org.sbolstandard.core2.Interaction)4 GenericTopLevel (org.sbolstandard.core2.GenericTopLevel)3 MapsTo (org.sbolstandard.core2.MapsTo)3 Set (java.util.Set)2 Annotation (org.sbolstandard.core2.Annotation)2 GenericLocation (org.sbolstandard.core2.GenericLocation)2