Skip to content

Commit

Permalink
Core: getAnnotations method added to NetworkDBAdaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaathik committed Aug 12, 2016
1 parent 625ccdd commit fd6916c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions bionetdb-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<groupId>org.opencb.commons</groupId>
<artifactId>commons-datastore-core</artifactId>
</dependency>
<dependency>
<groupId>org.opencb.cellbase</groupId>
<artifactId>cellbase-client</artifactId>
<version>${cellbase.version}</version>
</dependency>

<dependency>
<groupId>org.neo4j.driver</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ enum NetworkQueryParams implements QueryParam {

QueryResult clusteringCoefficient(Query query);

QueryResult getAnnotations(Query query, String annotateField);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import org.apache.commons.math3.util.CombinatoricsUtils;
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.types.Node;
import org.opencb.biodata.models.core.Gene;
import org.opencb.biodata.models.variant.avro.VariantAnnotation;
import org.opencb.bionetdb.core.api.NetworkDBAdaptor;
import org.opencb.bionetdb.core.config.BioNetDBConfiguration;
import org.opencb.bionetdb.core.config.DatabaseConfiguration;
import org.opencb.bionetdb.core.exceptions.BioNetDBException;
import org.opencb.bionetdb.core.models.*;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.QueryResult;
import org.opencb.cellbase.client.rest.CellBaseClient;
import org.opencb.commons.datastore.core.*;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
Expand All @@ -28,6 +29,7 @@ public class Neo4JNetworkDBAdaptor implements NetworkDBAdaptor {
private Session session;

private BioNetDBConfiguration configuration;
private CellBaseClient cellBaseClient;

private enum NodeTypes {
PHYSICAL_ENTITY,
Expand Down Expand Up @@ -813,6 +815,56 @@ public QueryResult getNodes(Query queryN, Query queryM, QueryOptions queryOption
return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network()));
}

@Override
public QueryResult getAnnotations(Query query, String annotateField) {
QueryOptions queryOptions = new QueryOptions("include", annotateField);
long startTime = System.currentTimeMillis();
StringBuilder myQuery = new StringBuilder();
List<String> idList = new ArrayList<>();
try {
// MATCH (x:PHYSICAL_ENTITY) WHERE x.name IN ["x"] MATCH (y:PHYSICAL_ENTITY) WHERE y.name IN ["y"] WITH x,y
// MERGE (c:PHYSICAL_ENTITY {name:"c"}) MERGE (x)-[r:XREF]->(c) MERGE (b:PHYSICAL_ENTITY {name:"b"}) MERGE (y)-[r1:XREF]->(b) RETURN x,y
idList = Arrays.asList(query.getString("id").split(","));
for (String anIdList : idList) {
myQuery.append("MATCH ").append(Neo4JQueryParser.parse(anIdList, query, queryOptions)).append(" ");
}
myQuery.append("WITH ").append(StringUtils.join(idList, ","));
} catch (BioNetDBException e) {
e.printStackTrace();
}

StringBuilder mergeQuery = new StringBuilder(" ");
try {
QueryResponse<Gene> queryResponse = cellBaseClient.getGeneClient().get(Collections.singletonList(query.getString("id")), queryOptions);
// MERGE (c:PHYSICAL_ENTITY {name:"c"}) MERGE (x)-[r:XREF]->(c) MERGE (b:PHYSICAL_ENTITY {name:"b"}) MERGE (y)-[r1:XREF]->(b)
for (int i = 0; i < queryResponse.getResponse().size(); i++) {
// Iterate over the results and add node per result
for (int j = 0; j < queryResponse.getResponse().get(i).getResult().get(0).getTranscripts().size(); j++) {
for (int k = 0; k < queryResponse.getResponse().get(i).getResult().get(0).getTranscripts().get(j).getXrefs().size(); k++) {
// Take care of the label of node
mergeQuery.append("MERGE (n").append(i).append(j).append(k).append(":").append("XREF").append("{");
// properties to add on the node goes here
mergeQuery.append("}) ").append("MERGE (").append(idList.get(i)).append(")-[r").append(i).append(":")
.append(annotateField.toUpperCase()).append("]->(n").append(i).append(j).append(k).append(") ");

}
}
}
myQuery.append(mergeQuery).append("RETURN ").append(StringUtils.join(idList, ","));
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("Query: " + myQuery);
long stopTime = System.currentTimeMillis();
StatementResult run = session.run(myQuery.toString());
while (run.hasNext()) {
System.out.println(run.next().asMap());
}
int time = (int) (stopTime - startTime) / 1000;
return new QueryResult("get", time, 0, 0, null, null, Arrays.asList(new Network()));
}

private int getTotalNodes() {
return this.session.run("MATCH (n) RETURN count(n) AS count").peek().get("count").asInt();
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<java.version>1.8</java.version>
<java-common-libs.version>3.3.0-SNAPSHOT</java-common-libs.version>
<biodata.version>0.8.0-SNAPSHOT</biodata.version>
<cellbase.version>4.1.0-beta</cellbase.version>
<jackson.version>2.6.4</jackson.version>
<jersey.version>2.23</jersey.version>
<build.dir>build</build.dir>
Expand Down

0 comments on commit fd6916c

Please sign in to comment.