Search in sources :

Example 6 with SBOLConversionException

use of org.sbolstandard.core2.SBOLConversionException 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)

Example 7 with SBOLConversionException

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

the class SBOLReader method parseCollectionV1.

/**
 * @param SBOLDoc
 * @param topLevel
 * @return
 * @throws SBOLConversionException
 * @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 Collection#Collection(URI)},</li>
 * 		<li>{@link Collection#setVersion(String)},</li>
 * 		<li>{@link Collection#setWasDerivedFrom(URI)},</li>
 * 		<li>{@link Collection#setDisplayId(String)},</li>
 * 		<li>{@link Collection#setMembers(Set)},</li>
 * 		<li>{@link Identified#setAnnotations(List)}, or</li>
 * 		<li>{@link SBOLDocument#addCollection(Collection)}; or</li>
 * 	</ul>
 * </li>
 * <li>the following SBOL validation rule was violated: 10202.</li>
 * </ul>
 */
private static Collection parseCollectionV1(SBOLDocument SBOLDoc, IdentifiableDocument<QName> topLevel) throws SBOLValidationException, SBOLConversionException {
    URI identity = topLevel.getIdentity();
    URI persistentIdentity = null;
    String displayId = null;
    String name = null;
    String description = null;
    Set<URI> members = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    if (URIPrefix != null) {
        displayId = URIcompliance.findDisplayId(topLevel.getIdentity().toString());
        identity = createCompliantURI(URIPrefix, TopLevel.SEQUENCE, displayId, version, typesInURI);
        persistentIdentity = createCompliantURI(URIPrefix, TopLevel.SEQUENCE, displayId, "", typesInURI);
    }
    for (NamedProperty<QName> namedProperty : topLevel.getProperties()) {
        if (namedProperty.getName().equals(Sbol1Terms.Collection.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", topLevel.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
            displayId = URIcompliance.fixDisplayId(displayId);
            if (URIPrefix != null) {
                identity = createCompliantURI(URIPrefix, TopLevel.COLLECTION, displayId, version, typesInURI);
                persistentIdentity = createCompliantURI(URIPrefix, TopLevel.COLLECTION, displayId, "", typesInURI);
            }
        } else if (namedProperty.getName().equals(Sbol1Terms.Collection.name)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", topLevel.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol1Terms.Collection.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", topLevel.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol1Terms.Collection.component)) {
            if (namedProperty.getValue() instanceof Literal) {
                members.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else {
                members.add(parseDnaComponentV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue()).getIdentity());
            }
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    // Collection c = SBOLDoc.createCollection(identity);
    Collection c = new Collection(identity);
    if (persistentIdentity != null) {
        c.setPersistentIdentity(persistentIdentity);
        c.setVersion(version);
    }
    if (identity != topLevel.getIdentity())
        c.addWasDerivedFrom(topLevel.getIdentity());
    if (displayId != null)
        c.setDisplayId(displayId);
    if (name != null)
        c.setName(name);
    if (description != null)
        c.setDescription(description);
    if (!members.isEmpty())
        c.setMembers(members);
    if (!annotations.isEmpty())
        c.setAnnotations(annotations);
    Collection oldC = SBOLDoc.getCollection(topLevel.getIdentity());
    if (oldC == null) {
        SBOLDoc.addCollection(c);
    } else {
        if (!c.equals(oldC)) {
            throw new SBOLValidationException("sbol-10202", c);
        }
    }
    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) NestedDocument(org.sbolstandard.core.datatree.NestedDocument) Literal(org.sbolstandard.core.datatree.Literal) HashSet(java.util.HashSet)

Example 8 with SBOLConversionException

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

the class SBOLDocumentTest method test_createCopy.

@Test
public void test_createCopy() throws SBOLValidationException, IOException, SBOLConversionException {
    SBOLDocument repression_doc = new SBOLDocument();
    repression_doc.setDefaultURIprefix(prURI);
    repression_doc.setTypesInURIs(false);
    // repression_doc.setComplete(true);
    SBOLDocument copied_doc = new SBOLDocument();
    copied_doc.setDefaultURIprefix(prURI);
    copied_doc.setTypesInURIs(false);
    // copied_doc.setComplete(true);
    // /core2/src/test/java/org/sbolstandard/core2/Testing/RepressionModel.xml"
    InputStream docAsStream = SequenceConstraintTest.class.getResourceAsStream("/SBOLTestSuite/SBOL2/RepressionModel.xml");
    repression_doc.read(docAsStream);
    // repression_doc.read("C:/Users/meher/Documents/workspace/libSBOLj/core2/src/test/resources/SBOL2/RepressionModel.xml");
    copied_doc.createCopy(repression_doc);
    assertTrue(repression_doc.equals(copied_doc));
}
Also used : InputStream(java.io.InputStream) SBOLDocument(org.sbolstandard.core2.SBOLDocument) Test(org.junit.Test)

Example 9 with SBOLConversionException

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

the class ModuleDefModuleTree method generateModel.

public String generateModel() throws SBOLValidationException, IOException, SBOLConversionException {
    SBOLDocument doc = new SBOLDocument();
    String version = "1.0";
    doc.setDefaultURIprefix("http://sbols.org/CRISPR_Example/");
    doc.setComplete(true);
    doc.setCreateDefaults(true);
    ModuleDefinition md1 = doc.createModuleDefinition("md_top", version);
    ArrayList<String> mdNames = new ArrayList<String>();
    mdNames.add("md_top");
    int depth = maxDepth;
    ArrayList<Integer> address = new ArrayList<Integer>();
    int selectedDepth = randInt(1, maxDepth);
    for (int i = 0; i < selectedDepth; i++) {
        addrCirfRef.add(randInt(1, numOfModules));
    }
    System.out.println("depth = " + depth);
    System.out.println("mdNames = " + mdNames);
    System.out.println("address = " + address);
    System.out.println("addrSelfRef = " + addrCirfRef);
    createModuleMDGraph(doc, depth, mdNames, address, md1);
    SBOLWriter.write(doc, "TestCircularModules.rdf");
    System.out.println("Finished writing.");
    // SBOLReader.read("/Users/zhangz/libSBOLproject/libSBOLj/examples/TestCircularModules.rdf");
    // SBOLReader.read("/Users/zhangz/libSBOLproject/libSBOLj/core2/src/test/resources/test/data/Validation/sbol-11705.rdf");
    SBOLValidate.validateSBOL(doc, true, true, true);
    if (SBOLValidate.getNumErrors() > 0) {
        for (String error : SBOLValidate.getErrors()) {
            System.out.println("TEST: error = " + error);
        }
    }
    return expectedError;
}
Also used : ModuleDefinition(org.sbolstandard.core2.ModuleDefinition) SBOLDocument(org.sbolstandard.core2.SBOLDocument) ArrayList(java.util.ArrayList)

Aggregations

SBOLDocument (org.sbolstandard.core2.SBOLDocument)6 URI (java.net.URI)5 QName (javax.xml.namespace.QName)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Literal (org.sbolstandard.core.datatree.Literal)3 NestedDocument (org.sbolstandard.core.datatree.NestedDocument)3 StringifyQName (org.sbolstandard.core.io.json.StringifyQName)3 URIcompliance.createCompliantURI (org.sbolstandard.core2.URIcompliance.createCompliantURI)3 ComponentDefinition (org.sbolstandard.core2.ComponentDefinition)2 GenericTopLevel (org.sbolstandard.core2.GenericTopLevel)2 ModuleDefinition (org.sbolstandard.core2.ModuleDefinition)2 Sequence (org.sbolstandard.core2.Sequence)2 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Test (org.junit.Test)1 IdentifiableDocument (org.sbolstandard.core.datatree.IdentifiableDocument)1 Collection (org.sbolstandard.core2.Collection)1