use of org.sbolstandard.core2.Collection 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;
}
use of org.sbolstandard.core2.Collection in project libSBOLj by SynBioDex.
the class SBOLDocument method updateReferences.
// TODO: need to update persistentIdentities too
private void updateReferences(URI originalIdentity, URI newIdentity) throws SBOLValidationException {
for (TopLevel topLevel : getTopLevels()) {
for (URI wasDerivedFrom : topLevel.getWasDerivedFroms()) {
if (wasDerivedFrom.equals(originalIdentity)) {
topLevel.removeWasDerivedFrom(originalIdentity);
topLevel.addWasDerivedFrom(newIdentity);
}
}
for (URI wasGeneratedBy : topLevel.getWasGeneratedBys()) {
if (wasGeneratedBy.equals(originalIdentity)) {
topLevel.removeWasGeneratedBy(originalIdentity);
topLevel.addWasGeneratedBy(newIdentity);
}
}
for (URI attachmentURI : topLevel.getAttachmentURIs()) {
if (attachmentURI.equals(originalIdentity)) {
topLevel.removeAttachment(originalIdentity);
topLevel.addAttachment(newIdentity);
}
}
}
for (Collection collection : getCollections()) {
for (URI memberURI : collection.getMemberURIs()) {
if (memberURI.equals(originalIdentity)) {
collection.removeMember(originalIdentity);
collection.addMember(newIdentity);
}
}
updateReferences(collection, originalIdentity, newIdentity);
}
for (ComponentDefinition componentDefinition : getComponentDefinitions()) {
updateReferences(componentDefinition, originalIdentity, newIdentity);
for (Component component : componentDefinition.getComponents()) {
if (component.getDefinitionURI().equals(originalIdentity)) {
component.setDefinition(newIdentity);
for (MapsTo mapsTo : component.getMapsTos()) {
ComponentDefinition cd = getComponentDefinition(newIdentity);
if (cd != null) {
String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
mapsTo.setRemote(newURI);
}
}
}
updateReferences(component, originalIdentity, newIdentity);
for (MapsTo mapsTo : component.getMapsTos()) {
updateReferences(mapsTo, originalIdentity, newIdentity);
}
}
for (SequenceAnnotation sa : componentDefinition.getSequenceAnnotations()) {
for (Location loc : sa.getLocations()) {
updateReferences(loc, originalIdentity, newIdentity);
}
updateReferences(sa, originalIdentity, newIdentity);
}
for (SequenceConstraint sc : componentDefinition.getSequenceConstraints()) {
updateReferences(sc, originalIdentity, newIdentity);
}
for (URI sequenceURI : componentDefinition.getSequenceURIs()) {
if (sequenceURI.equals(originalIdentity)) {
componentDefinition.removeSequence(originalIdentity);
componentDefinition.addSequence(newIdentity);
}
}
}
for (ModuleDefinition moduleDefinition : getModuleDefinitions()) {
updateReferences(moduleDefinition, originalIdentity, newIdentity);
for (FunctionalComponent functionalComponent : moduleDefinition.getFunctionalComponents()) {
if (functionalComponent.getDefinitionURI().equals(originalIdentity)) {
functionalComponent.setDefinition(newIdentity);
for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
ComponentDefinition cd = getComponentDefinition(newIdentity);
if (cd != null) {
String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
mapsTo.setRemote(newURI);
}
}
}
updateReferences(functionalComponent, originalIdentity, newIdentity);
for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
updateReferences(mapsTo, originalIdentity, newIdentity);
}
}
for (Module module : moduleDefinition.getModules()) {
if (module.getDefinitionURI().equals(originalIdentity)) {
module.setDefinition(newIdentity);
for (MapsTo mapsTo : module.getMapsTos()) {
ModuleDefinition md = getModuleDefinition(newIdentity);
if (md != null) {
String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
URI newURI = URIcompliance.createCompliantURI(md.getPersistentIdentity().toString(), displayId, md.getVersion());
mapsTo.setRemote(newURI);
}
}
}
updateReferences(module, originalIdentity, newIdentity);
for (MapsTo mapsTo : module.getMapsTos()) {
updateReferences(mapsTo, originalIdentity, newIdentity);
}
}
for (Interaction interaction : moduleDefinition.getInteractions()) {
updateReferences(interaction, originalIdentity, newIdentity);
for (Participation participation : interaction.getParticipations()) {
updateReferences(participation, originalIdentity, newIdentity);
}
}
for (URI modelURI : moduleDefinition.getModelURIs()) {
if (modelURI.equals(originalIdentity)) {
moduleDefinition.removeModel(originalIdentity);
moduleDefinition.addModel(newIdentity);
}
}
}
for (Model model : getModels()) {
updateReferences(model, originalIdentity, newIdentity);
}
for (Attachment attachment : getAttachments()) {
updateReferences(attachment, originalIdentity, newIdentity);
}
for (Implementation implementation : getImplementations()) {
if (implementation.isSetBuilt() && implementation.getBuiltURI().equals(originalIdentity)) {
implementation.setBuilt(newIdentity);
}
updateReferences(implementation, originalIdentity, newIdentity);
}
for (Sequence sequence : getSequences()) {
updateReferences(sequence, originalIdentity, newIdentity);
}
for (GenericTopLevel genericTopLevel : getGenericTopLevels()) {
updateReferences(genericTopLevel, originalIdentity, newIdentity);
}
for (CombinatorialDerivation combinatorialDerivation : getCombinatorialDerivations()) {
updateReferences(combinatorialDerivation, originalIdentity, newIdentity);
if (combinatorialDerivation.getTemplateURI().equals(originalIdentity)) {
combinatorialDerivation.setTemplate(newIdentity);
ComponentDefinition cd = getComponentDefinition(newIdentity);
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, originalIdentity, newIdentity);
}
}
for (Activity activity : getActivities()) {
updateReferences(activity, originalIdentity, newIdentity);
for (Association association : activity.getAssociations()) {
if (association.getAgentURI().equals(originalIdentity)) {
association.setAgent(newIdentity);
}
if (association.isSetPlan() && association.getPlanURI().equals(originalIdentity)) {
association.setPlan(newIdentity);
}
updateReferences(association, originalIdentity, newIdentity);
}
for (Usage usage : activity.getUsages()) {
if (usage.getEntityURI().equals(originalIdentity)) {
usage.setEntity(newIdentity);
}
updateReferences(usage, originalIdentity, newIdentity);
}
}
for (Agent agent : getAgents()) {
updateReferences(agent, originalIdentity, newIdentity);
}
for (Plan plan : getPlans()) {
updateReferences(plan, originalIdentity, newIdentity);
}
}
use of org.sbolstandard.core2.Collection in project libSBOLj by SynBioDex.
the class SBOLDocument method removeTopLevel.
/**
* Removes the given top-level from this SBOL document's list of top-levels.
*
* @param topLevel
* the top-level to be removed
* @param instancesMap
* map of toplevel instances
* @return {@code true} if the given top-level was successfully removed,
* {@code false} otherwise
* @throws SBOLValidationException
* if the following SBOL validation rule was violated: 12103.
*/
private final <TL extends TopLevel> boolean removeTopLevel(TopLevel topLevel, Map<URI, TL> instancesMap) throws SBOLValidationException {
if (complete) {
for (Collection c : collections.values()) {
if (c.containsMember(topLevel.getIdentity())) {
throw new SBOLValidationException("sbol-12103", c);
}
}
}
Set<TopLevel> setToRemove = new HashSet<>();
setToRemove.add(topLevel);
boolean changed = instancesMap.values().removeAll(setToRemove);
URI latestVersion = null;
for (TL tl : instancesMap.values()) {
if (topLevel.getPersistentIdentity().toString().equals(tl.getPersistentIdentity().toString())) {
if (latestVersion == null) {
latestVersion = tl.getIdentity();
} else if (isFirstVersionNewer(extractVersion(tl.getIdentity()), extractVersion(latestVersion))) {
latestVersion = tl.getIdentity();
}
}
}
if (latestVersion != null) {
instancesMap.put(topLevel.getPersistentIdentity(), instancesMap.get(latestVersion));
}
return changed;
}
use of org.sbolstandard.core2.Collection in project libSBOLj by SynBioDex.
the class Collection method copy.
/* (non-Javadoc)
* @see org.sbolstandard.core2.abstract_classes.TopLevel#copy(java.lang.String, java.lang.String, java.lang.String)
*/
/**
* @throws SBOLValidationException if an SBOL validation rule violation occurred in any of the following methods:
* <ul>
* <li>{@link #deepCopy()},</li>
* <li>{@link URIcompliance#createCompliantURI(String, String, String)},</li>
* <li>{@link #setDisplayId(String)},</li>
* <li>{@link #setVersion(String)},</li>
* <li>{@link #setWasDerivedFrom(URI)}, or</li>
* <li>{@link #setIdentity(URI)}.</li>
* </ul>
*/
@Override
Collection copy(String URIprefix, String displayId, String version) throws SBOLValidationException {
Collection cloned = this.deepCopy();
cloned.setPersistentIdentity(createCompliantURI(URIprefix, displayId, ""));
cloned.setDisplayId(displayId);
cloned.setVersion(version);
URI newIdentity = createCompliantURI(URIprefix, displayId, version);
if (!this.getIdentity().equals(newIdentity)) {
cloned.addWasDerivedFrom(this.getIdentity());
} else {
cloned.setWasDerivedFroms(this.getWasDerivedFroms());
}
cloned.setIdentity(newIdentity);
return cloned;
}
use of org.sbolstandard.core2.Collection in project libSBOLj by SynBioDex.
the class SynBioHubFrontend method submit.
/**
* Submit file to a new private collection on SynBioHub
* @param id The submission identifier
* @param version The submission version
* @param name The submission name
* @param description The submission description
* @param citations The pubMedIds for this submission
* @param overwrite_merge '0' prevent, '1' overwrite, '2' merge and prevent, '3' merge and overwrite
* @param inputStream inputStream to submit to SynBioHub
* @throws SynBioHubException if there was an error communicating with the SynBioHub
*/
private void submit(URI uri, String id, String version, String name, String description, String citations, String overwrite_merge, InputStream inputStream) throws SynBioHubException {
if (user.equals("")) {
Exception e = new Exception("Must be logged in to submit.");
throw new SynBioHubException(e);
}
String url = backendUrl + "/submit";
HttpPost request = new HttpPost(url);
request.setHeader("X-authorization", user);
request.setHeader("Accept", "text/plain");
MultipartEntityBuilder params = MultipartEntityBuilder.create();
/* example for setting a HttpMultipartMode */
params.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if (uri == null) {
params.addTextBody("id", id);
params.addTextBody("version", version);
params.addTextBody("name", name);
params.addTextBody("description", description);
params.addTextBody("citations", citations);
params.addTextBody("collectionChoices", "");
} else {
params.addTextBody("rootCollections", uri.toString());
}
params.addTextBody("overwrite_merge", overwrite_merge);
params.addTextBody("user", user);
if (inputStream != null) {
params.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, "file");
} else {
params.addTextBody("file", "");
}
try {
request.setEntity(params.build());
HttpResponse response = client.execute(request);
checkResponseCode(response);
} catch (Exception e) {
// e.printStackTrace();
throw new SynBioHubException(e);
} finally {
request.releaseConnection();
}
}
Aggregations