Skip to content

Commit

Permalink
Merge pull request #211 from benwbooth/152-use-curies-for-relationshi…
Browse files Browse the repository at this point in the history
…p-names

[#152] Relationships contain full IRIs, try to encode it to a CURIE
  • Loading branch information
jnguyenx committed Mar 14, 2017
2 parents 13eab7e + 63a7154 commit 318f753
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 74 deletions.
Expand Up @@ -36,6 +36,7 @@
import com.google.common.base.Function;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import org.prefixcommons.CurieUtil;

/***
* Add "evidence" to a graph
Expand All @@ -55,10 +56,12 @@ public class EvidenceAspect implements GraphAspect {
RelationshipType.withName("http://purl.org/oban/association_has_predicate");

private final GraphDatabaseService graphDb;
private final CurieUtil curieUtil;

@Inject
EvidenceAspect(GraphDatabaseService graphDb) {
EvidenceAspect(GraphDatabaseService graphDb, CurieUtil curieUtil) {
this.graphDb = graphDb;
this.curieUtil = curieUtil;
}

@Override
Expand Down Expand Up @@ -99,15 +102,16 @@ public Long apply(Vertex vertex) {

if (isEdgeInGraph) { // means that the relationship exists between the subject and
// object
TinkerGraphUtil.addEdge(graph, hasSubject);
TinkerGraphUtil.addEdge(graph, hasObject);
TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
tgu.addEdge(hasSubject);
tgu.addEdge(hasObject);
for (Relationship evidence : association.getRelationships(EVIDENCE,
Direction.OUTGOING)) {
TinkerGraphUtil.addEdge(graph, evidence);
tgu.addEdge(evidence);
}
for (Relationship source : association.getRelationships(SOURCE,
Direction.OUTGOING)) {
TinkerGraphUtil.addEdge(graph, source);
tgu.addEdge(source);
}
}
} else {
Expand Down
13 changes: 9 additions & 4 deletions SciGraph-core/src/main/java/io/scigraph/internal/GraphApi.java
Expand Up @@ -45,16 +45,19 @@
import com.google.common.base.Predicate;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import org.prefixcommons.CurieUtil;

public class GraphApi {

private final GraphDatabaseService graphDb;
private final CypherUtil cypherUtil;
private final CurieUtil curieUtil;

@Inject
public GraphApi(GraphDatabaseService graphDb, CypherUtil cypherUtil) {
public GraphApi(GraphDatabaseService graphDb, CypherUtil cypherUtil, CurieUtil curieUtil) {
this.graphDb = graphDb;
this.cypherUtil = cypherUtil;
this.curieUtil = curieUtil;
}

/***
Expand Down Expand Up @@ -100,16 +103,17 @@ public Evaluation evaluate(Path path) {
});
}
Graph graph = new TinkerGraph();
TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
for (Path path: description.traverse(nodes)) {
Relationship relationship = path.lastRelationship();
if (null != relationship) {
TinkerGraphUtil.addEdge(graph, relationship);
tgu.addEdge(relationship);
}
}
if (isEmpty(graph.getEdges())) {
// If nothing was added to the graph add the root nodes
for (Node node: nodes) {
TinkerGraphUtil.addNode(graph, node);
tgu.addNode(node);
}
}
return graph;
Expand All @@ -125,13 +129,14 @@ public Graph getEdges(RelationshipType type, boolean entail, long skip, long lim
+ " SKIP " + skip
+ " LIMIT " + limit;
Graph graph = new TinkerGraph();
TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
Result result;
try {
result = cypherUtil.execute(query);
while (result.hasNext()) {
Map<String, Object> map = result.next();
Path path = (Path) map.get("path");
TinkerGraphUtil.addPath(graph, path);
tgu.addPath(path);
}
} catch (ArrayIndexOutOfBoundsException e) {
// Return and empty graph if the limit is too high...
Expand Down
102 changes: 67 additions & 35 deletions SciGraph-core/src/main/java/io/scigraph/internal/TinkerGraphUtil.java
Expand Up @@ -16,6 +16,8 @@
package io.scigraph.internal;

import static com.google.common.collect.Sets.newHashSet;

import com.google.inject.Inject;
import io.scigraph.frames.CommonProperties;
import io.scigraph.frames.NodeProperties;

Expand All @@ -35,6 +37,7 @@
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;

import org.prefixcommons.CurieUtil;
import scala.collection.convert.Wrappers.SeqWrapper;

import com.google.common.base.Optional;
Expand All @@ -49,13 +52,39 @@
/***
* Utilities for building TinkerGraphs from Neo4j objects
*/
public final class TinkerGraphUtil {
public class TinkerGraphUtil {

static final Logger logger = Logger.getLogger(TinkerGraphUtil.class.getName());

static final ImmutableSet<String> PROTECTED_PROPERTY_KEYS = ImmutableSet.of(CommonProperties.IRI, CommonProperties.CURIE);

static void copyProperties(PropertyContainer container, Element element) {
private final CurieUtil curieUtil;
private Graph graph;

@Inject
public TinkerGraphUtil(CurieUtil curieUtil) {
this.curieUtil = curieUtil;
this.graph = new TinkerGraph();
}

public TinkerGraphUtil(Graph graph, CurieUtil curieUtil) {
this.graph = graph;
this.curieUtil = curieUtil;
}

public CurieUtil getCurieUtil() {
return curieUtil;
}

public Graph getGraph() {
return graph;
}

public void setGraph(Graph graph) {
this.graph = graph;
}

void copyProperties(PropertyContainer container, Element element) {
for (String key : container.getPropertyKeys()) {
Object property = container.getProperty(key);
if (property.getClass().isArray()) {
Expand All @@ -65,11 +94,14 @@ static void copyProperties(PropertyContainer container, Element element) {
}
property = propertyList;
}
else if (key.equals(CommonProperties.IRI) && String.class.isAssignableFrom(property.getClass())) {
property = curieUtil.getCurie((String)property).or((String)property);
}
element.setProperty(key, property);
}
}

static Vertex addNode(Graph graph, Node node) {
Vertex addNode(Node node) {
Vertex vertex = graph.getVertex(node.getId());
if (null == vertex) {
vertex = graph.addVertex(node.getId());
Expand All @@ -83,21 +115,21 @@ static Vertex addNode(Graph graph, Node node) {
return vertex;
}

static Edge addEdge(Graph graph, Relationship relationship) {
Edge addEdge(Relationship relationship) {
Edge edge = graph.getEdge(relationship.getId());
if (null == edge) {
Vertex outVertex = addNode(graph, relationship.getStartNode());
Vertex inVertex = addNode(graph, relationship.getEndNode());
Vertex outVertex = addNode(relationship.getStartNode());
Vertex inVertex = addNode(relationship.getEndNode());
String label = relationship.getType().name();
// TODO #152 add CurieUtil to resolve IRI to Curie
edge = graph.addEdge(relationship.getId(), outVertex, inVertex, label);
Optional<String> curieLabel = curieUtil.getCurie(label);
edge = graph.addEdge(relationship.getId(), outVertex, inVertex, curieLabel.or(label));
copyProperties(relationship, edge);
}
return edge;
}

// TODO unit test that
static boolean removeEdge(Graph graph, Relationship relationship) {
boolean removeEdge(Relationship relationship) {
Edge edge = graph.getEdge(relationship.getId());
if (null != edge) {
graph.removeEdge(edge);
Expand All @@ -107,17 +139,17 @@ static boolean removeEdge(Graph graph, Relationship relationship) {
}
}

public static Element addElement(Graph graph, PropertyContainer container) {
public Element addElement(PropertyContainer container) {
if (container instanceof Node) {
return addNode(graph, (Node) container);
return addNode((Node) container);
} else {
return addEdge(graph, (Relationship) container);
return addEdge((Relationship) container);
}
}

public static void addPath(Graph graph, Iterable<PropertyContainer> path) {
public void addPath(Iterable<PropertyContainer> path) {
for (PropertyContainer container : path) {
addElement(graph, container);
addElement(container);
}
}

Expand All @@ -135,7 +167,7 @@ static void copyProperties(Element source, Element target) {
}
}

static Vertex addNode(Graph graph, Vertex node) {
Vertex addNode(Vertex node) {
Vertex vertex = graph.getVertex(node.getId());
if (null == vertex) {
vertex = graph.addVertex(node.getId());
Expand All @@ -144,11 +176,11 @@ static Vertex addNode(Graph graph, Vertex node) {
return vertex;
}

static Edge addEdge(Graph graph, Edge edge) {
Edge addEdge(Edge edge) {
Edge newEdge = graph.getEdge(edge.getId());
if (null == newEdge) {
Vertex outVertex = addNode(graph, edge.getVertex(Direction.OUT));
Vertex inVertex = addNode(graph, edge.getVertex(Direction.IN));
Vertex outVertex = addNode(edge.getVertex(Direction.OUT));
Vertex inVertex = addNode(edge.getVertex(Direction.IN));
String label = edge.getLabel();
newEdge = graph.addEdge(edge.getId(), outVertex, inVertex, label);
copyProperties(edge, edge);
Expand All @@ -157,32 +189,32 @@ static Edge addEdge(Graph graph, Edge edge) {
}


public static Element addElement(Graph graph, Element element) {
public Element addElement(Element element) {
if (element instanceof Vertex) {
return addNode(graph, (Vertex) element);
return addNode((Vertex) element);
} else {
return addEdge(graph, (Edge) element);
return addEdge((Edge) element);
}
}

public static void addGraph(Graph graph, Graph addition) {
public void addGraph(Graph addition) {
for (Vertex vertex : addition.getVertices()) {
addElement(graph, vertex);
addElement(vertex);
}
for (Edge edge : addition.getEdges()) {
addElement(graph, edge);
addElement(edge);
}
}

public static Graph combineGraphs(Graph graph1, Graph graph2) {
Graph graph = new TinkerGraph();
addGraph(graph, graph1);
addGraph(graph, graph2);
return graph;
public Graph combineGraphs(Graph graph2) {
TinkerGraphUtil tgu = new TinkerGraphUtil(curieUtil);
tgu.addGraph(graph);
tgu.addGraph(graph2);
return tgu.getGraph();
}

public static TinkerGraph resultToGraph(Result result) {
TinkerGraph graph = new TinkerGraph();
public Graph resultToGraph(Result result) {
graph = new TinkerGraph();
while (result.hasNext()) {
Map<String, Object> map = result.next();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand All @@ -191,15 +223,15 @@ public static TinkerGraph resultToGraph(Result result) {
if (null == value) {
continue;
} else if (value instanceof PropertyContainer) {
addElement(graph, (PropertyContainer) value);
addElement((PropertyContainer) value);
} else if (value instanceof Path) {
for (PropertyContainer container : (Path) value) {
addElement(graph, container);
addElement(container);
}
} else if (value instanceof SeqWrapper) {
for (Object thing : (SeqWrapper<?>) value) {
if (thing instanceof PropertyContainer) {
addElement(graph, (PropertyContainer) thing);
addElement((PropertyContainer) thing);
}
}
} else if (value instanceof Boolean) {
Expand Down Expand Up @@ -251,7 +283,7 @@ static <T> Set<T> getPropertiesAsSet(Object value, Class<T> type) {
return set;
}

public static void project(Graph graph, Collection<String> projection) {
public void project(Collection<String> projection) {
if (projection.contains("*")) {
return;
}
Expand Down
Expand Up @@ -62,13 +62,14 @@ public void setup() {
subject1.createRelationshipTo(object3, RelationshipType.withName(relationName));
association2.createRelationshipTo(relationNode, EvidenceAspect.HAS_PREDICATE);

TinkerGraphUtil.addNode(graph, subject1);
TinkerGraphUtil.addNode(graph, object1);
TinkerGraphUtil.addNode(graph, object2);
TinkerGraphUtil.addNode(graph, object3);
TinkerGraphUtil.addNode(graph, relationNode);
TinkerGraphUtil.addEdge(graph, rel);
aspect = new EvidenceAspect(graphDb);
TinkerGraphUtil tgu = new TinkerGraphUtil(graph, curieUtil);
tgu.addNode(subject1);
tgu.addNode(object1);
tgu.addNode(object2);
tgu.addNode(object3);
tgu.addNode(relationNode);
tgu.addEdge(rel);
aspect = new EvidenceAspect(graphDb, curieUtil);
}

@Test
Expand Down
Expand Up @@ -64,7 +64,7 @@ public void addNodes() throws Exception {
h.createRelationshipTo(j, OwlRelationships.RDFS_SUBCLASS_OF);
g.createRelationshipTo(j, OwlRelationships.RDFS_SUBCLASS_OF);
e.createRelationshipTo(b, fizz);
graphApi = new GraphApi(graphDb, cypherUtil);
graphApi = new GraphApi(graphDb, cypherUtil, curieUtil);
}

@Test
Expand Down
Expand Up @@ -49,7 +49,7 @@ public void addNodes() throws Exception {
c = graphDb.createNode();
b.createRelationshipTo(a, OwlRelationships.RDFS_SUBCLASS_OF);
c.createRelationshipTo(b, OwlRelationships.OWL_EQUIVALENT_CLASS);
graphApi = new GraphApi(graphDb, cypherUtil);
graphApi = new GraphApi(graphDb, cypherUtil, curieUtil);
}

@Test
Expand Down

0 comments on commit 318f753

Please sign in to comment.