Skip to content

Commit

Permalink
Implement reverse operator
Browse files Browse the repository at this point in the history
Functionality: reverse the edges from the input design space.
Details:
Upon selecting reverse operator
	a. Delete input option
	b. Display some guidance

Close CIDARLAB#14
  • Loading branch information
Zackhardtoname committed Aug 2, 2019
1 parent bc4f342 commit 0e3ca78
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import java.io.InputStream;
import java.util.*;

import knox.spring.data.neo4j.domain.DesignSpace;
import knox.spring.data.neo4j.exception.*;
import knox.spring.data.neo4j.sample.DesignSampler.EnumerateType;
import knox.spring.data.neo4j.sbol.SBOLConversion;
import knox.spring.data.neo4j.services.DesignSpaceService;

import org.sbolstandard.core2.SBOLConversionException;
Expand Down Expand Up @@ -522,6 +520,38 @@ public ResponseEntity<String> mergeDesignSpaces(@RequestParam(value = "inputSpac
}
}

/**
* @api {post} /designSpace/reverse Reverse
* @apiName reverseDesignSpace
* @apiGroup DesignSpace
*
* @apiParam {String} inputSpaceID ID for the input design space to be reversed.
* @apiParam {String} outputSpaceID ID for the output design space resulting from reverse. If omitted, then the result is
* stored in the input design space.
*
* @apiDescription Reverse the edges from the input design space.
*/
@RequestMapping(value = "/designSpace/reverse", method = RequestMethod.POST)
public ResponseEntity<String> reverseDesignSpace(@RequestParam(value = "inputSpaceID", required = true) String inputSpaceID,
@RequestParam(value = "outputSpaceID", required = false) String outputSpaceID) {
try {
long startTime = System.nanoTime();

if (outputSpaceID == null) {
designSpaceService.reverseDesignSpace(inputSpaceID);
} else {
designSpaceService.reverseDesignSpace(inputSpaceID, outputSpaceID);
}

return new ResponseEntity<String>("{\"message\": \"Design space was successfully reversed after " +
(System.nanoTime() - startTime) + " ns.\"}", HttpStatus.NO_CONTENT);
} catch (ParameterEmptyException | DesignSpaceNotFoundException |
DesignSpaceConflictException | DesignSpaceBranchesConflictException ex) {
return new ResponseEntity<String>("{\"message\": \"" + ex.getMessage() + "\"}",
HttpStatus.BAD_REQUEST);
}
}

/**
* @api {post} /designSpace/or OR
* @apiName orDesignSpaces
Expand Down
1 change: 0 additions & 1 deletion src/main/java/knox/spring/data/neo4j/domain/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ public boolean hasOrientation() {
return isInline() || isReverseComplement();
}


public boolean hasOrientation(Orientation orientation) {
return hasOrientation() && this.orientation.equals(orientation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package knox.spring.data.neo4j.operations;

import knox.spring.data.neo4j.domain.Edge;
import knox.spring.data.neo4j.domain.NodeSpace;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ReverseOperator {
public static void apply(NodeSpace inputSpace, NodeSpace outputSpace) {
//copy input space to a new output space
outputSpace.copyNodeSpace(inputSpace);

Set<Edge> allEdges = outputSpace.getEdges();

//traverse all edges of input space and flip the orientation attribute
for(Edge edge: allEdges){
edge.reverseOrientation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import knox.spring.data.neo4j.domain.Snapshot;
import knox.spring.data.neo4j.exception.*;
import knox.spring.data.neo4j.operations.ANDOperator;
import knox.spring.data.neo4j.operations.Concatenation;
import knox.spring.data.neo4j.operations.JoinOperator;
import knox.spring.data.neo4j.operations.MergeOperator;
import knox.spring.data.neo4j.operations.OROperator;
import knox.spring.data.neo4j.operations.Product;
import knox.spring.data.neo4j.operations.ReverseOperator;
import knox.spring.data.neo4j.operations.RepeatOperator;
import knox.spring.data.neo4j.operations.Star;
import knox.spring.data.neo4j.operations.Union;
import knox.spring.data.neo4j.repositories.BranchRepository;
import knox.spring.data.neo4j.repositories.CommitRepository;
import knox.spring.data.neo4j.repositories.DesignSpaceRepository;
Expand Down Expand Up @@ -325,7 +322,31 @@ public void mergeBranches(String targetSpaceID, List<String> inputBranchIDs,

saveDesignSpace(targetSpace);
}


public void reverseDesignSpace(String inputSpaceID) {
reverseDesignSpace(inputSpaceID, inputSpaceID);
}

public void reverseDesignSpace(String inputSpaceID, String outputSpaceID) {
List<String> inputSpaceIDs = new ArrayList<>();
inputSpaceIDs.add(inputSpaceID);
List<NodeSpace> inputSpaces = new ArrayList<NodeSpace>(inputSpaceIDs.size());

DesignSpace outputSpace = loadIOSpaces(inputSpaceIDs, outputSpaceID, inputSpaces);

ReverseOperator.apply(inputSpaces.get(0), outputSpace);


List<NodeSpace> inputSnaps = new ArrayList<NodeSpace>(inputSpaces.size());

NodeSpace outputSnap = mergeVersionHistories(castNodeSpacesToDesignSpaces(inputSpaces),
outputSpace, inputSnaps);

ReverseOperator.apply(inputSnaps.get(0), outputSnap);

saveDesignSpace(outputSpace);
}

private DesignSpace loadIOSpaces(List<String> inputSpaceIDs, String outputSpaceID,
List<NodeSpace> inputSpaces) {
for (String inputSpaceID : inputSpaceIDs) {
Expand Down Expand Up @@ -863,7 +884,8 @@ private DesignSpace findDesignSpace(String targetSpaceID) {
}

private DesignSpace loadDesignSpace(String targetSpaceID) {
DesignSpace targetSpace = designSpaceRepository.findOne(getDesignSpaceGraphID(targetSpaceID), 3);
Long keyID = getDesignSpaceGraphID(targetSpaceID);
DesignSpace targetSpace = designSpaceRepository.findOne(keyID, 3);

for (Commit commit : targetSpace.getCommits()) {
commit.setSnapshot(reloadSnapshot(commit.getSnapshot()));
Expand Down
18 changes: 17 additions & 1 deletion src/main/resources/static/js/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const operators = {
OR: 'or',
AND: 'and',
MERGE: 'merge',
REPEAT: 'repeat'
REPEAT: 'repeat',
REVERSE: 'reverse'
};


Expand Down Expand Up @@ -288,4 +289,19 @@ export function designSpaceMerge(inputSpaces, outputSpace, tolerance){
} else {
swalError(request.response);
}
}

export function designSpaceReverse(inputSpace, outputSpace){
let query = "?";
query += encodeQueryParameter("inputSpaceID", inputSpace, query);
query += encodeQueryParameter("outputSpaceID", outputSpace, query);

let request = new XMLHttpRequest();
request.open("POST", endpoints.DESIGN + "/" + operators.REVERSE + query, false);
request.send(null);
if (request.status >= 200 && request.status < 300) {
swalSuccess();
} else {
swalError(request.response);
}
}
14 changes: 14 additions & 0 deletions src/main/resources/static/js/knox.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,15 @@ $('#apply-operators-tooltip').click(() => {
}
div.appendChild(tolDiv);
}
if(this.value === endpoint.operators.REVERSE){
if(div.contains(inputDiv)){
div.removeChild(inputDiv);
}
const guidance = document.createElement("P");
const text = document.createTextNode("The current designSpace will be reversed.");
guidance.appendChild(text);
div.appendChild(guidance);
}
});

swal({
Expand Down Expand Up @@ -557,6 +566,11 @@ $('#apply-operators-tooltip').click(() => {
case endpoint.operators.MERGE:
endpoint.designSpaceMerge(inputSpaces, outputSpace, tolerance);
break;

case endpoint.operators.REVERSE:
// the reverse operator would only reverse one inputSpace
endpoint.designSpaceReverse(inputSpaces[0], outputSpace);
break;
}
}
});
Expand Down

0 comments on commit 0e3ca78

Please sign in to comment.