use of org.sbolstandard.core2.Location 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;
}
use of org.sbolstandard.core2.Location in project libSBOLj by SynBioDex.
the class ComponentDefinition method getImpliedNucleicAcidSequence.
/**
* Return the elements of a nucleic acid sequence implied by the hierarchically included components.
* <p>
* This method first tries to obtain the length of a nucleic acid sequence from the set of sequences referenced by this component definition
* that has an {@link Sequence#IUPAC_DNA} encoding. It then iterates through this component defintion's
* sequence annotations, and update the length with the ending locations that have a larger value than the current length. It then populates
* the elements with this length with unknown bases. This method iterates through this component defintion's sequence annotations to recursively
* search for bases implied by the hierarchically included components, and fills the elements with these known bases.
*
* @return the elements of a nucleic sequence implied by the hierarchically included components
*/
public String getImpliedNucleicAcidSequence() {
URI type = null;
if (this.getTypes().contains(ComponentDefinition.DNA)) {
type = ComponentDefinition.DNA;
} else if (this.getTypes().contains(ComponentDefinition.RNA)) {
type = ComponentDefinition.RNA;
} else {
return null;
}
String elements = "";
int length = 0;
if (this.getSequenceByEncoding(Sequence.IUPAC_DNA) != null) {
length = this.getSequenceByEncoding(Sequence.IUPAC_DNA).getElements().length();
}
for (SequenceAnnotation sequenceAnnotation : this.getSequenceAnnotations()) {
for (Location location : sequenceAnnotation.getLocations()) {
if (location instanceof Range) {
Range range = (Range) location;
if (range.getEnd() > length) {
length = range.getEnd();
}
}
}
}
for (int i = 0; i < length; i++) {
elements += "N";
}
char[] elementsArray = elements.toCharArray();
for (SequenceAnnotation sequenceAnnotation : this.getSequenceAnnotations()) {
String subElements = null;
if (!sequenceAnnotation.isSetComponent())
continue;
if (sequenceAnnotation.getComponent().getDefinition() != null) {
ComponentDefinition compDef = sequenceAnnotation.getComponent().getDefinition();
if (compDef.getSequenceByEncoding(Sequence.IUPAC_DNA) != null) {
subElements = compDef.getSequenceByEncoding(Sequence.IUPAC_DNA).getElements();
} else {
subElements = compDef.getImpliedNucleicAcidSequence();
}
for (Location location : sequenceAnnotation.getLocations()) {
if (location instanceof Range) {
Range range = (Range) location;
if (range.isSetOrientation() && range.getOrientation().equals(OrientationType.REVERSECOMPLEMENT)) {
subElements = Sequence.reverseComplement(subElements, type);
}
for (int i = 0; i < subElements.length(); i++) {
// TODO: need to deal with when start index out of range
elementsArray[(range.getStart() + i) - 1] = subElements.charAt(i);
}
}
}
}
}
elements = String.valueOf(elementsArray);
return elements;
}
use of org.sbolstandard.core2.Location in project libSBOLj by SynBioDex.
the class ComponentDefinition method createSequenceAnnotation.
/**
* Creates a child sequence annotation for this component definition with the given arguments,
* and then adds to this component definition's list of sequence annotations.
*
* @param displayId the display ID of the sequence annotation to be created
* @param location the location of the sequence annotation to be created
* @return the created sequence annotation
* @throws SBOLValidationException if any of the following condition is satisfied:
* <ul>
* <li>an SBOL validation rule violation occurred in {@link URIcompliance#createCompliantURI(String, String, String)};</li>
* <li>an SBOL validation rule violation occurred in {@link #createSequenceAnnotation(URI, Set)};</li>
* <li>an SBOL validation rule violation occurred in {@link SequenceAnnotation#setDisplayId(String)}; or</li>
* <li>an SBOL validation rule violation occurred in {@link SequenceAnnotation#setVersion(String)}.</li>
* </ul>
*/
private SequenceAnnotation createSequenceAnnotation(String displayId, Location location) throws SBOLValidationException {
String URIprefix = this.getPersistentIdentity().toString();
String version = this.getVersion();
URI newSequenceAnnotationURI = createCompliantURI(URIprefix, displayId, version);
Set<Location> locations = new HashSet<>();
locations.add(location);
SequenceAnnotation sa = createSequenceAnnotation(newSequenceAnnotationURI, locations);
sa.setPersistentIdentity(createCompliantURI(URIprefix, displayId, ""));
sa.setDisplayId(displayId);
sa.setVersion(version);
return sa;
}
use of org.sbolstandard.core2.Location 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);
}
}
use of org.sbolstandard.core2.Location 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()));
}
}
Aggregations