Skip to content

Commit

Permalink
Fixed inference of total ordering of components during SBOL conversio…
Browse files Browse the repository at this point in the history
…n and fixed bug with minimizing graph (deleting select blank edges).
  • Loading branch information
nroehner committed Jan 13, 2020
1 parent 75d848a commit 770e60f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 47 deletions.
19 changes: 11 additions & 8 deletions src/main/java/knox/spring/data/neo4j/domain/NodeSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -830,15 +830,16 @@ public void deleteBlankEdges(Set<Edge> blankEdges) {
}

public boolean deleteBlankEdge(Edge edge, HashMap<String, Set<Edge>> nodeIDToIncomingEdges) {
if ((nodeIDToIncomingEdges.get(edge.getHeadID()).size() == 1 || edge.getTail().getNumEdges() == 1)
if (((nodeIDToIncomingEdges.containsKey(edge.getHeadID()) && nodeIDToIncomingEdges.get(edge.getHeadID()).size() == 1)
|| edge.getTail().getNumEdges() == 1)
&& !edge.getTail().hasDiffNodeType(edge.getHead())
&& (!edge.getTail().isAcceptNode() || edge.getHead().isAcceptNode()
|| nodeIDToIncomingEdges.get(edge.getHeadID()).size() == 1)
&& (!edge.getHead().isStartNode() || edge.getTail().isStartNode()
|| edge.getTail().getNumEdges() == 1)
&& (!edge.getTail().isStartNode()
|| nodeIDToIncomingEdges.get(edge.getHeadID()).size() == 1
|| !nodeIDToIncomingEdges.get(edge.getTailID()).isEmpty())) {
|| (nodeIDToIncomingEdges.containsKey(edge.getHeadID()) && nodeIDToIncomingEdges.get(edge.getHeadID()).size() == 1)
|| (nodeIDToIncomingEdges.containsKey(edge.getTailID()) && !nodeIDToIncomingEdges.get(edge.getTailID()).isEmpty()))) {
edge.delete();

Set<Edge> headEdges = edge.getHead().removeEdges();
Expand All @@ -851,11 +852,13 @@ public boolean deleteBlankEdge(Edge edge, HashMap<String, Set<Edge>> nodeIDToInc

Set<Edge> incomingHeadEdges = new HashSet<Edge>();

for (Edge incomingHeadEdge : nodeIDToIncomingEdges.get(edge.getHeadID())) {
if (incomingHeadEdge != edge) {
incomingHeadEdge.setHead(edge.getTail());

incomingHeadEdges.add(incomingHeadEdge);
if (nodeIDToIncomingEdges.containsKey(edge.getHeadID())) {
for (Edge incomingHeadEdge : nodeIDToIncomingEdges.get(edge.getHeadID())) {
if (incomingHeadEdge != edge) {
incomingHeadEdge.setHead(edge.getTail());

incomingHeadEdges.add(incomingHeadEdge);
}
}
}

Expand Down
128 changes: 89 additions & 39 deletions src/main/java/knox/spring/data/neo4j/sbol/SBOLConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,40 +85,41 @@ private List<NodeSpace> recurseVariableComponents(CombinatorialDerivation combin
// order components by sequence constraints
VariableComponent[] sortedVCs = sortVariableComponents(combinatorialDerivation);
for (VariableComponent variableComponent : sortedVCs) {
if (variableComponent != null) {
// recurse through variant derivations
Set<CombinatorialDerivation> variantDerivs = variableComponent.getVariantDerivations();

// recurse through variant derivations
Set<CombinatorialDerivation> variantDerivs = variableComponent.getVariantDerivations();
Boolean hasVariants = !variableComponent.getVariants().isEmpty() || !variableComponent.getVariantCollections().isEmpty();

Boolean hasVariants = !variableComponent.getVariants().isEmpty() || !variableComponent.getVariantCollections().isEmpty();

//handle structure for just repeats
if (variantDerivs.size() == 1 && !hasVariants){
for (CombinatorialDerivation cv : variantDerivs) {
inputSpace.add(applyOperator(variableComponent.getOperator(), recurseVariableComponents(cv)));
//handle structure for just repeats
if (variantDerivs.size() == 1 && !hasVariants){
for (CombinatorialDerivation cv : variantDerivs) {
inputSpace.add(applyOperator(variableComponent.getOperator(), recurseVariableComponents(cv)));
}
}
}

//else handle collapsed complex ORs
else if (variantDerivs.size() > 0){
List<NodeSpace> orSpace = new LinkedList<>();
NodeSpace outputSpace = new NodeSpace();
//else handle collapsed complex ORs
else if (variantDerivs.size() > 0){
List<NodeSpace> orSpace = new LinkedList<>();
NodeSpace outputSpace = new NodeSpace();

if (hasVariants){
orSpace.add(createNodeSpaceFromVariableComponent(variableComponent, template)); //add variants
}
if (hasVariants){
orSpace.add(createNodeSpaceFromVariableComponent(variableComponent, template)); //add variants
}

for (CombinatorialDerivation cv : variantDerivs) {
orSpace.add(applyOperator(OperatorType.ONE, recurseVariableComponents(cv)));
}
for (CombinatorialDerivation cv : variantDerivs) {
orSpace.add(applyOperator(OperatorType.ONE, recurseVariableComponents(cv)));
}

OROperator.apply(orSpace, outputSpace); //"or" all the elements in the list
List<NodeSpace> tempSpace = new LinkedList<>();
tempSpace.add(outputSpace);
inputSpace.add(applyOperator(variableComponent.getOperator(), tempSpace));
}
OROperator.apply(orSpace, outputSpace); //"or" all the elements in the list
List<NodeSpace> tempSpace = new LinkedList<>();
tempSpace.add(outputSpace);
inputSpace.add(applyOperator(variableComponent.getOperator(), tempSpace));
}

else if (hasVariants){
inputSpace.add(createNodeSpaceFromVariableComponent(variableComponent, template));
else if (hasVariants){
inputSpace.add(createNodeSpaceFromVariableComponent(variableComponent, template));
}
}
}

Expand All @@ -128,20 +129,69 @@ else if (hasVariants){
private VariableComponent[] sortVariableComponents(CombinatorialDerivation combinatorialDerivation){
//make ordered components from sequence constraints
List<Component> orderedComponents = new ArrayList<>();
Set<SequenceConstraint> seqConstraints = combinatorialDerivation.getTemplate().getSequenceConstraints();

ComponentDefinition template = combinatorialDerivation.getTemplate();
Set<SequenceConstraint> seqConstraints = template.getSequenceConstraints();

//check if total ordering
Set<URI> subjectURIs = new HashSet<URI>();
Set<URI> objectURIs = new HashSet<URI>();

Set<URI> firstURI = new HashSet<URI>();
Set<URI> lastURI = new HashSet<URI>();

HashMap<URI,URI> precedesMap = new HashMap<URI,URI>();

for (SequenceConstraint constraint : seqConstraints) {
//subject precedes object
Component subject = constraint.getSubject();
Component object = constraint.getObject();
int subIndex = orderedComponents.indexOf(subject);
int objIndex = orderedComponents.indexOf(object);
if (subIndex == -1 && objIndex == -1){
orderedComponents.add(subject);
orderedComponents.add(object);
}else if(subIndex > -1){
orderedComponents.add(subIndex+1, object);
}else if(objIndex > -1){
orderedComponents.add(objIndex, subject);
if (constraint.getRestriction().equals(RestrictionType.PRECEDES)) {
subjectURIs.add(constraint.getSubjectURI());
objectURIs.add(constraint.getObjectURI());

firstURI.add(constraint.getSubjectURI());
lastURI.add(constraint.getObjectURI());

precedesMap.put(constraint.getSubjectURI(), constraint.getObjectURI());
}
}

firstURI.removeAll(objectURIs);
lastURI.removeAll(subjectURIs);

boolean totalOrdering;

if (firstURI.size() == 1 && lastURI.size() == 1) {
URI currentURI = firstURI.iterator().next();

orderedComponents.add(template.getComponent(currentURI));

while (precedesMap.containsKey(currentURI)) {
currentURI = precedesMap.get(currentURI);

orderedComponents.add(template.getComponent(currentURI));
}

totalOrdering = currentURI.equals(lastURI.iterator().next());
} else {
totalOrdering = false;
}

if (!totalOrdering) {
orderedComponents.clear();

for (SequenceConstraint constraint : seqConstraints) {
//subject precedes object
Component subject = constraint.getSubject();
Component object = constraint.getObject();
int subIndex = orderedComponents.indexOf(subject);
int objIndex = orderedComponents.indexOf(object);
if (subIndex == -1 && objIndex == -1){
orderedComponents.add(subject);
orderedComponents.add(object);
}else if(subIndex > -1){
orderedComponents.add(subIndex+1, object);
}else if(objIndex > -1){
orderedComponents.add(objIndex, subject);
}
}
}

Expand Down

0 comments on commit 770e60f

Please sign in to comment.