Search in sources :

Example 1 with GenericLocation

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

the class SBOLReader method parseGenericLocation.

/**
 * @param typeGenLoc
 * @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,
 * 11002; or
 *</li>
 * <li>an SBOL validation rule violation occurred in the following constructor or methods:
 * 	<ul>
 * 		<li>{@link GenericLocation#GenericLocation(URI)},</li>
 * 		<li>{@link GenericLocation#setDisplayId(String)},</li>
 * 		<li>{@link GenericLocation#setVersion(String)},</li>
 * 		<li>{@link GenericLocation#setWasDerivedFrom(URI)}, or</li>
 * 		<li>{@link Identified#setAnnotations(List)}.</li>
 * 	</ul>
 * </li>
 * </ul>
 */
private static GenericLocation parseGenericLocation(NestedDocument<QName> typeGenLoc) throws SBOLValidationException {
    // URIcompliance.extractDisplayId(typeGenLoc.getIdentity());
    String displayId = null;
    String name = null;
    String description = null;
    // URI.create(URIcompliance.extractPersistentId(typeGenLoc.getIdentity()));
    URI persistentIdentity = null;
    URI orientation = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : typeGenLoc.getProperties()) {
        if (namedProperty.getName().equals(Sbol2Terms.GenericLocation.orientation) || namedProperty.getName().equals(Sbol2Terms.GenericLocation.Orientation)) {
            if (!(namedProperty.getValue() instanceof Literal) || orientation != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-11002", typeGenLoc.getIdentity());
            }
            orientation = 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", typeGenLoc.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", typeGenLoc.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", typeGenLoc.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else 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", typeGenLoc.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", typeGenLoc.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", typeGenLoc.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", typeGenLoc.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    GenericLocation gl = new GenericLocation(typeGenLoc.getIdentity());
    if (displayId != null)
        gl.setDisplayId(displayId);
    if (name != null)
        gl.setName(name);
    if (description != null)
        gl.setDescription(description);
    if (orientation != null)
        try {
            gl.setOrientation(OrientationType.convertToOrientationType(orientation));
        } catch (SBOLValidationException e) {
            throw new SBOLValidationException("sbol-11002", gl);
        }
    if (persistentIdentity != null)
        gl.setPersistentIdentity(persistentIdentity);
    if (version != null)
        gl.setVersion(version);
    gl.setWasDerivedFroms(wasDerivedFroms);
    gl.setWasGeneratedBys(wasGeneratedBys);
    if (!annotations.isEmpty())
        gl.setAnnotations(annotations);
    return gl;
}
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 GenericLocation

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

the class SBOLReader method parseSequenceAnnotationV1.

/**
 * @param SBOLDoc
 * @param sequenceAnnotation
 * @param precedePairs
 * @param parentURI
 * @param sa_num
 * @param instantiatedComponents
 * @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)}, </li>
 * 		<li>{@link #parseDnaComponentV1(SBOLDocument, IdentifiableDocument)}, </li>
 * 		<li>{@link Range#Range(URI, int, int)}, </li>
 * 		<li>{@link Range#setDisplayId(String)}, </li>
 * 		<li>{@link Range#setVersion(String)}, </li>
 * 		<li>{@link GenericLocation#GenericLocation(URI)}, </li>
 * 		<li>{@link SequenceAnnotation#SequenceAnnotation(URI, Set)}, </li>
 * 		<li>{@link SequenceAnnotation#setDisplayId(String)}, </li>
 * 		<li>{@link SequenceAnnotation#setVersion(String)}, </li>
 * 		<li>{@link SequenceAnnotation#setWasDerivedFrom(URI)}, </li>
 * 		<li>{@link SequenceAnnotation#setComponent(URI)}, or </li>
 * 		<li>{@link SequenceAnnotation#setAnnotations(List)}; or</li>
 * 	</ul>
 * </li>
 * <li>the following SBOL validation rule was violated: 11002.</li>
 * </ul>
 * @throws SBOLConversionException
 */
private static SequenceAnnotation parseSequenceAnnotationV1(SBOLDocument SBOLDoc, NestedDocument<QName> sequenceAnnotation, List<SBOLPair> precedePairs, String parentURI, int sa_num, Set<String> instantiatedComponents) throws SBOLValidationException, SBOLConversionException {
    Integer start = null;
    Integer end = null;
    String strand = null;
    URI componentURI = null;
    URI identity = sequenceAnnotation.getIdentity();
    String persIdentity = sequenceAnnotation.getIdentity().toString();
    List<Annotation> annotations = new ArrayList<>();
    if (URIPrefix != null) {
        persIdentity = createCompliantURI(parentURI, "annotation" + sa_num, "").toString();
        identity = createCompliantURI(parentURI, "annotation" + sa_num, version);
    }
    if (!sequenceAnnotation.getType().equals(Sbol1Terms.SequenceAnnotations.SequenceAnnotation)) {
        throw new SBOLConversionException("QName has to be" + Sbol1Terms.SequenceAnnotations.SequenceAnnotation.toString());
    }
    for (NamedProperty<QName> namedProperty : sequenceAnnotation.getProperties()) {
        if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.bioStart)) {
            if (!(namedProperty.getValue() instanceof Literal) || start != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-11102", sequenceAnnotation.getIdentity());
            }
            String temp = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
            start = Integer.parseInt(temp);
        } else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.bioEnd)) {
            if (!(namedProperty.getValue() instanceof Literal) || end != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-11103", sequenceAnnotation.getIdentity());
            }
            String temp2 = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
            end = Integer.parseInt(temp2);
        } else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.strand)) {
            if (!(namedProperty.getValue() instanceof Literal) || strand != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-11002", sequenceAnnotation.getIdentity());
            }
            strand = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.subComponent)) {
            if (componentURI != null) {
                throw new SBOLValidationException("sbol-10904", sequenceAnnotation.getIdentity());
            }
            if (namedProperty.getValue() instanceof NestedDocument) {
                componentURI = parseDnaComponentV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue()).getIdentity();
            } else {
                if (!(namedProperty.getValue() instanceof Literal) || (!(((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(Sbol1Terms.SequenceAnnotations.precedes)) {
            URI left = sequenceAnnotation.getIdentity();
            URI right = null;
            if (namedProperty.getValue() instanceof NestedDocument) {
                // TODO: need to check if ++sa_num here okay
                right = parseSequenceAnnotationV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue(), precedePairs, parentURI, ++sa_num, instantiatedComponents).getIdentity();
            } else {
                if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                    throw new SBOLValidationException("sbol-11404", sequenceAnnotation.getIdentity());
                }
                right = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
            }
            SBOLPair pair = new SBOLPair(left, right);
            precedePairs.add(pair);
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    String componentDisplayId = URIcompliance.extractDisplayId(componentURI);
    String displayId = "annotation" + sa_num;
    if (compliant && componentDisplayId != null && !instantiatedComponents.contains(componentDisplayId)) {
        identity = createCompliantURI(parentURI, componentDisplayId + "_annotation", version);
        persIdentity = createCompliantURI(parentURI, componentDisplayId + "_annotation", "").toString();
        displayId = componentDisplayId + "_annotation";
    }
    // Note: Do not create a seqAnnotation if Location is empty
    Location location = null;
    if (// create SequenceAnnotation & Component
    start != null && end != null) {
        URI range_identity = createCompliantURI(persIdentity, "range", version);
        location = new Range(range_identity, start, end);
        if (!persIdentity.equals("")) {
            location.setPersistentIdentity(createCompliantURI(persIdentity, "range", ""));
            location.setDisplayId("range");
            location.setVersion(version);
        }
        if (strand != null) {
            if (strand.equals("+")) {
                location.setOrientation(OrientationType.INLINE);
            } else if (strand.equals("-")) {
                location.setOrientation(OrientationType.REVERSECOMPLEMENT);
            }
        }
    } else {
        URI dummyGenericLoc_id = createCompliantURI(persIdentity, "genericLocation", version);
        location = new GenericLocation(dummyGenericLoc_id);
        if (!persIdentity.equals("")) {
            location.setPersistentIdentity(createCompliantURI(persIdentity, "genericLocation", ""));
            location.setDisplayId("genericLocation");
            location.setVersion(version);
        }
        if (strand != null) {
            if (strand.equals("+")) {
                location.setOrientation(OrientationType.INLINE);
            } else if (strand.equals("-")) {
                location.setOrientation(OrientationType.REVERSECOMPLEMENT);
            }
        }
    }
    Set<Location> locations = new HashSet<>();
    locations.add(location);
    SequenceAnnotation s = new SequenceAnnotation(identity, locations);
    if (!persIdentity.equals("")) {
        s.setPersistentIdentity(URI.create(persIdentity));
        s.setDisplayId(displayId);
        s.setVersion(version);
    }
    if (identity != sequenceAnnotation.getIdentity())
        s.addWasDerivedFrom(sequenceAnnotation.getIdentity());
    if (componentURI != null)
        s.setComponent(componentURI);
    if (!annotations.isEmpty())
        s.setAnnotations(annotations);
    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 3 with GenericLocation

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

the class SequenceAnnotation method copy.

void copy(SequenceAnnotation sequenceAnnotation) throws SBOLValidationException {
    ((Identified) this).copy((Identified) sequenceAnnotation);
    for (Location location : sequenceAnnotation.getLocations()) {
        String displayId = URIcompliance.findDisplayId(location);
        if (location instanceof Range) {
            Range range = (Range) location;
            Range newRange;
            if (range.isSetOrientation()) {
                newRange = this.addRange(displayId, range.getStart(), range.getEnd(), range.getOrientation());
            } else {
                newRange = this.addRange(displayId, range.getStart(), range.getEnd());
            }
            newRange.copy(range);
        } else if (location instanceof Cut) {
            Cut cut = (Cut) location;
            Cut newCut;
            if (cut.isSetOrientation()) {
                newCut = this.addCut(displayId, cut.getAt(), cut.getOrientation());
            } else {
                newCut = this.addCut(displayId, cut.getAt());
            }
            newCut.copy(cut);
        } else if (location instanceof GenericLocation) {
            GenericLocation genericLocation = (GenericLocation) location;
            GenericLocation newGenericLocation;
            if (genericLocation.isSetOrientation()) {
                newGenericLocation = this.addGenericLocation(displayId, genericLocation.getOrientation());
            } else {
                newGenericLocation = this.addGenericLocation(displayId);
            }
            newGenericLocation.copy(genericLocation);
        }
    }
    Location location = this.getLocation("DUMMY__LOCATION");
    if (location != null) {
        this.removeLocation(location);
    }
    if (sequenceAnnotation.isSetComponent()) {
        String componentDisplayId = URIcompliance.findDisplayId(sequenceAnnotation.getComponent());
        this.setComponent(componentDisplayId);
    }
    this.roles = new HashSet<>();
    for (URI role : sequenceAnnotation.getRoles()) {
        this.addRole(URI.create(role.toString()));
    }
}
Also used : URI(java.net.URI) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI)

Example 4 with GenericLocation

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

the class SequenceAnnotation method addGenericLocation.

/**
 * Creates a generic location with the given arguments and then adds it to this sequence annotation's
 * list of locations.
 * <p>
 * This method first creates a compliant URI for the generic location to be created.
 * It starts with this sequence annotation's persistent identity URI,
 * followed by the given display ID, and ends an empty string for version.
 *
 * @param displayId the display ID for the generic location to be created
 * @return the created generic location instance
 * @throws SBOLValidationException if any of the following SBOL validation rules was violated:
 * 10201, 10202, 10204, 10206.
 */
public GenericLocation addGenericLocation(String displayId) throws SBOLValidationException {
    URI identity = createCompliantURI(this.getPersistentIdentity().toString(), displayId, this.getVersion());
    GenericLocation genericLocation = new GenericLocation(identity);
    genericLocation.setPersistentIdentity(createCompliantURI(this.getPersistentIdentity().toString(), displayId, ""));
    genericLocation.setDisplayId(displayId);
    genericLocation.setVersion(this.getVersion());
    addLocation(genericLocation);
    return genericLocation;
}
Also used : URI(java.net.URI) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI)

Example 5 with GenericLocation

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

the class SequenceAnnotationTest method test_locationMethods.

@Test
public void test_locationMethods() throws SBOLValidationException {
    Cut promoter_cut = promoter_SA.addCut("promoter_cut", 1);
    assertTrue(gRNA_b_gene.getSequenceAnnotation("promoter_SA").getLocation("promoter_cut").equals(promoter_cut));
    promoter_cut.unsetOrientation();
    assertNull(promoter_cut.getOrientation());
    Location test = promoter_cut;
    assertNotNull(test.toString());
    Cut terminator_cut = terminator_SA.addCut("terminator_cut", 100);
    assertTrue(gRNA_b_gene.getSequenceAnnotation("terminator_SA").getLocation("terminator_cut").equals(terminator_cut));
    Cut gene_cut = gene_SA.addCut("gene_cut", 50, OrientationType.INLINE);
    assertTrue(gRNA_b_gene.getSequenceAnnotation("gene_SA").getLocation("gene_cut").equals(gene_cut));
    gene_SA.removeLocation(gene_cut);
    assertNull(gene_SA.getLocation("gene_cut"));
    Range gene_range = gene_SA.addRange("gene_range", 50, 99);
    assertTrue(gRNA_b_gene.getSequenceAnnotation("gene_SA").getLocation("gene_range").equals(gene_range));
    gene_range.unsetOrientation();
    assertNull(gene_range.getOrientation());
    promoter_SA.removeLocation(promoter_cut);
    assertNull(promoter_SA.getLocation(promoter_cut.getIdentity()));
    GenericLocation promoter_glocation = promoter_SA.addGenericLocation("promoter_glocation");
    assertTrue(gRNA_b_gene.getSequenceAnnotation("promoter_SA").getLocation("promoter_glocation").equals(promoter_glocation));
    terminator_SA.removeLocation(terminator_cut);
    assertNull(terminator_SA.getLocation("terminator_cut"));
    GenericLocation terminator_glocation = terminator_SA.addGenericLocation("terminator_glocation", OrientationType.INLINE);
    assertTrue(gRNA_b_gene.getSequenceAnnotation("terminator_SA").getLocation("terminator_glocation").equals(terminator_glocation));
}
Also used : Cut(org.sbolstandard.core2.Cut) GenericLocation(org.sbolstandard.core2.GenericLocation) Range(org.sbolstandard.core2.Range) GenericLocation(org.sbolstandard.core2.GenericLocation) Location(org.sbolstandard.core2.Location) Test(org.junit.Test)

Aggregations

URI (java.net.URI)5 URIcompliance.createCompliantURI (org.sbolstandard.core2.URIcompliance.createCompliantURI)5 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 QName (javax.xml.namespace.QName)2 Literal (org.sbolstandard.core.datatree.Literal)2 StringifyQName (org.sbolstandard.core.io.json.StringifyQName)2 Test (org.junit.Test)1 NestedDocument (org.sbolstandard.core.datatree.NestedDocument)1 Cut (org.sbolstandard.core2.Cut)1 GenericLocation (org.sbolstandard.core2.GenericLocation)1 Location (org.sbolstandard.core2.Location)1 Range (org.sbolstandard.core2.Range)1