Git Lab CI for docker build enabled! You can enable it using .gitlab-ci.yml in your project. Check file template at https://gitlab.bio.di.uminho.pt/snippets/5

Commit 3305d1d3 authored by Davide Lagoa's avatar Davide Lagoa
Browse files

remote service to neo4j database created

parent 62ca0537
......@@ -10,15 +10,15 @@
</description>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus BioSystems Server</name>
<!-- <url>http://192.168.1.99/nexus/content/groups/public/</url> -->
<!-- <url>http://193.137.11.210/nexus/content/groups/public/</url> -->
<url>https://www.bio.di.uminho.pt/nexus/content/groups/public/</url>
</repository>
</repository>
<repository>
<id>ebi-repo</id>
......@@ -95,6 +95,16 @@
</build>
<dependencies>
<!-- <dependency> <groupId>org.neo4j.driver</groupId> <artifactId>neo4j-java-driver</artifactId>
<version>1.7.2</version> </dependency> -->
<!-- <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-bolt</artifactId>
<version>3.4.0</version> </dependency> -->
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>pt.uminho.ceb.biosystems.transyt</groupId>
<artifactId>utilities</artifactId>
......
package pt.uminho.ceb.biosystems.transyt.service.neo4jRest;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Statement;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.graphdb.Node;
import pt.uminho.ceb.biosystems.transyt.service.transytDatabase.TransytGeneralProperties;
import pt.uminho.ceb.biosystems.transyt.service.transytDatabase.TransytNode;
import pt.uminho.ceb.biosystems.transyt.service.transytDatabase.TransytNodeLabel;
import pt.uminho.ceb.biosystems.transyt.service.transytDatabase.TransytRelationship;
import pt.uminho.ceb.biosystems.transyt.service.transytDatabase.TransytRelationshipType;
import pt.uminho.ceb.biosystems.transyt.utilities.capsules.ReactionContainer;
import pt.uminho.ceb.biosystems.transyt.utilities.enumerators.TypeOfTransporter;
// public RestNeo4jGraphDatabase( String uri, String user, String password ) throws SQLException
// {
// // Connect
// Connection con = DriverManager.getConnection(uri);
//
// // Querying
// try (Statement stmt = con.createStatement()) {
// ResultSet rs = stmt.executeQuery("MATCH (n) RETURN n");
// while (rs.next()) {
// System.out.println(rs.getInt(0));
// }
// }
// con.close();
// }
public class RestNeo4jGraphDatabase implements AutoCloseable{
private final Driver driver;
private Set<String> temporaryCompounds;
private Map<String, String> temporaryCompoundsNames;
private Map<String, String> temporaryCompoundsFormulas;
public RestNeo4jGraphDatabase(String uri, String user, String password){
temporaryCompounds = new HashSet<>();
temporaryCompoundsNames = new HashMap<>();
temporaryCompoundsFormulas = new HashMap<>();
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
}
/** Method to create a node and set one property. Return the node created.
*
* @param label
* @param properties
* @return
*/
public TransytNode createNode(TransytNodeLabel label, TransytGeneralProperties key, String value) {
Map<String, String> properties = new HashMap<>();
properties.put(key.toString(), value);
return createNode(label, properties);
}
/** Method to create a node and set properties. The properties should be given in a map format.
* Returns the node created.
*
* @param label
* @param properties
* @return
*/
public TransytNode createNode(TransytNodeLabel label, Map<String, String> properties) {
Session session = driver.session(AccessMode.WRITE);
String auxString = buildPropertiesQuery(properties);
Statement statement = new Statement( "CREATE (n:" + label + " " + auxString + ") "
+ "RETURN {id:id(n)}" );
StatementResult res = session.run(statement);
while(res.hasNext()) {
Record record = res.next();
int id = -1;
String idString = record.get(0).asMap().get("id").toString();
if(!idString.equalsIgnoreCase("NULL")) {
id = Integer.valueOf(idString);
}
return new TransytNode(id, properties);
}
session.close();
return null;
}
/** Method to create a relationship between two nodes.
*
* @param label
* @param properties
* @return
*/
public void createRelationship(int startNodeID, int endNodeID, TransytRelationshipType type) {
createRelationship(startNodeID, endNodeID, type, null);
}
/** Method to create a relationship between two nodes and set one property.
*
* @param label
* @param properties
* @return
*/
public void createRelationship(int startNodeID, int endNodeID, TransytRelationshipType type,
TransytGeneralProperties key, String value) {
Map<String, String> properties = new HashMap<>();
properties.put(key.toString(), value);
createRelationship(startNodeID, endNodeID, type, properties);
}
/** Method to create a relationship between two nodes.
*
* @param label
* @param properties
* @return
*/
public void createRelationship(int startNodeID, int endNodeID, TransytRelationshipType type, Map<String, String> properties) {
Session session = driver.session(AccessMode.WRITE);
String auxString = buildPropertiesQuery(properties);
session.run(new Statement("MATCH (a), (b) WHERE id(a)=" + startNodeID + " AND id(b)=" + endNodeID
+ " CREATE (a)-[r:" + type + " " + auxString +"]->(b)"));
session.close();
}
/**
* Auxuliar method to build the properties query when adding a node or relationship.
* A String in format {key1: 'value1', key2: 'value2'} is returned
* @return
*/
private String buildPropertiesQuery(Map<String, String> properties) {
String auxString = "";
if(properties != null && !properties.isEmpty()) {
auxString = " {";
Iterator<String> iterator = properties.keySet().iterator();
while(iterator.hasNext()) {
String key = iterator.next();
auxString = auxString + key + ": '" + properties.get(key) + "'";
if(iterator.hasNext()) {
auxString = auxString + ", ";
}
else
auxString = auxString + "} ";
}
}
return auxString;
}
/**
* Set a property to a node. If a value already exists, it will be replaced.
*
* @param label
* @param key
* @param value
*/
public void setNodeProperty(int id, TransytNodeLabel label, TransytGeneralProperties key, String value) {
Session session = driver.session(AccessMode.WRITE);
session.run(new Statement("MATCH (n:" + label + ") "
+ "WHERE id(n)="+ id +" SET n." + key + "='" + value + "'"));
session.close();
}
/**
* Set a property to a relationship. If a value already exists, it will be replaced.
*
* @param label
* @param key
* @param value
*/
public void setRelationshipProperty(int startNodeID, int endNodeID, TransytRelationshipType type,
TransytGeneralProperties key, String value) {
Session session = driver.session(AccessMode.WRITE);
session.run(new Statement("MATCH (a)-[r: "+ type +" ]->(b) "
+ "WHERE id(a)="+ startNodeID +" AND id(a)="+ endNodeID +" SET r." + key + "='" + value + "'"));
session.close();
}
/**
* @param startNodeID
* @param endNodeID
* @param type
* @return
*/
public TransytRelationship getRelationship(int startNodeID, int endNodeID, TransytRelationshipType type) {
Session session = driver.session(AccessMode.READ);
Statement statement = new Statement("MATCH (a)-[r:"+ type +" ]->(b) "
+ "WHERE id(a)="+ startNodeID +" AND id(b)="+ endNodeID +" RETURN {relid:id(r), props:properties(r)}");
StatementResult res = session.run(statement);
session.close();
while(res.hasNext()) {
Record record = res.next();
int id = -1;
String idString = record.get(0).asMap().get("id").toString();
if(!idString.equalsIgnoreCase("NULL")) {
id = Integer.valueOf(idString);
}
Map<String, String> properties = mapProperties(record.get(0).asMap().get("props").toString());
return new TransytRelationship(id, properties, type, null);
}
return null;
}
// /**
// * Get all nodes with the given label
// *
// * @return
// */
// public void getAllNodesByLabel(TransytNodeLabel label)
// {
// Session session = driver.session(AccessMode.READ);
//
// //Statement statement = new Statement( "MATCH (n:" + label + ") RETURN n, properties(n)" );
// Statement statement = new Statement( "MATCH (n:Gene) WHERE n.gene_id='BSU00870' RETURN n, properties(n)" );
// //Statement statement = new Statement( "MATCH p=(r:Regulator)-->() WHERE r.reg_id=\"BSU03960\" RETURN p" );
//
// StatementResult res = session.run(statement);
//
// while(res.hasNext()) {
//
// Record record = res.next();
//
// System.out.println(record.asMap());
// System.out.println(record.keys());
//
// System.out.println(record.values());
// System.out.println();
// }
//
// session.close();
// }
/**
* Find node with the corresponding UniProt Accession number
*
* @param accession
* @return
*/
public TransytNode findUniprotAccessionNode(String accession) {
return this.findNode(TransytNodeLabel.UniprotAccession, TransytGeneralProperties.Accession_Number, accession);
}
/**
* Find node with the corresponding TC Number
*
* @param tcNumber
* @return
*/
public TransytNode findTcNumberNode(String tcNumber) {
return this.findNode(TransytNodeLabel.TC_Number, TransytGeneralProperties.TC_Number, tcNumber);
}
/**
* Find node with the corresponding reactionID
*
* @param reactionID
* @return
*/
public TransytNode findReactionNode(String reactionID) {
return this.findNode(TransytNodeLabel.Reaction, TransytGeneralProperties.ReactionID, reactionID);
}
/**
* Find node with the corresponding metabolite ID.
*
* @param label
* @param metaboliteID
* @return
*/
public TransytNode findMetaboliteNode(TransytNodeLabel label, String id) {
return this.findNode(label, TransytGeneralProperties.MetaboliteID, id);
}
/**
* Find node with the corresponding molecular formula.
*
* @param formula
* @return
*/
public TransytNode findMolecularFormulaNode(String formula) {
return this.findNode(TransytNodeLabel.Molecular_formula, TransytGeneralProperties.Molecular_Formula, formula);
}
/**
* Find node with the corresponding TC Number
*
* @param type of transport
* @return
*/
public TransytNode findTransportTypeNode(TypeOfTransporter type) {
return this.findNode(TransytNodeLabel.Transport_Type, TransytGeneralProperties.Transport_Type, type.toString());
}
/**
* Find node with the corresponding metabolite name.
*
* @param name
* @return
*/
public TransytNode findNameNode(String name) {
return this.findNode(TransytNodeLabel.MetaboliteName, TransytGeneralProperties.Name, name);
}
/**
* Get all nodes with the given label
*
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public TransytNode findNode(TransytNodeLabel label, TransytGeneralProperties key, String value)
{
Session session = driver.session(AccessMode.READ);
Statement statement = new Statement( "MATCH (n:" + label + ") "
+ "WHERE n."+ key + "='" + value + "' "
+ "RETURN {id:id(n), props:properties(n)}");
// + "RETURN n, properties(n)" );
StatementResult res = session.run(statement);
session.close();
while(res.hasNext()) {
Record record = res.next();
int id = -1;
String idString = record.get(0).asMap().get("id").toString();
if(!idString.equalsIgnoreCase("NULL")) {
id = Integer.valueOf(idString);
}
Map<String, String> properties = mapProperties(record.get(0).asMap().get("props").toString());
return new TransytNode(id, properties);
}
return null;
}
/**
* Method to retrieve all subunits known in TCDB for the given TCNumbers.
*
* @param tcNumbers
* @return
*/
public Map<String, Set<String>> findSubunitsInDatabase(Set<String> tcNumbers) {
Map<String, Set<String>> subunits = new HashMap<>();
for(String tcNumber : tcNumbers) {
TransytNode tcNode = findTcNumberNode(tcNumber);
if(tcNode != null) {
Set<TransytRelationship> relations = getUpstreamRelationships(tcNode.getNodeID(), TransytRelationshipType.has_tc);
for(TransytRelationship rel : relations) {
Set<String> accessions = new HashSet<>();
String acc = rel.getOtherEndNode().getProperty(TransytGeneralProperties.Accession_Number);
if(subunits.containsKey(tcNumber))
accessions = subunits.get(tcNumber);
accessions.add(acc);
subunits.put(tcNumber, accessions);
}
}
}
return subunits;
}
/**
* Retrieve node's all downstream relationships of a specific type.
*
* @param id
* @param type
* @return
*/
public Set<TransytRelationship> getUpstreamRelationships(int id, TransytRelationshipType type) { //VERIFICAR SE EST A CHAM;AR BEM
Set<TransytRelationship> rels = new HashSet<>();
Session session = driver.session(AccessMode.READ);
Statement statement = new Statement( "MATCH (a)<-[t:" + type + "]-(b) WHERE id(a)="+ id +" "
+ "RETURN {relid:id(t), relProps:properties(t), endid:id(b), endNodeProps:properties(b)}");
StatementResult res = session.run(statement);
session.close();
while(res.hasNext()) {
Record record = res.next();
int idRelationship = -1;
int idStartNode = -1;
String idRelationshipString = record.get(0).asMap().get("relid").toString();
String idStartNodeString = record.get(0).asMap().get("endid").toString();
if(!idRelationshipString.equalsIgnoreCase("NULL")) {
idRelationship = Integer.valueOf(idRelationshipString);
}
if(!idStartNodeString.equalsIgnoreCase("NULL")) {
idStartNode = Integer.valueOf(idStartNodeString);
}
Map<String, String> relProperties = mapProperties(record.get(0).asMap().get("relProps").toString());
Map<String, String> nodeProperties = mapProperties(record.get(0).asMap().get("endNodeProps").toString());
rels.add(new TransytRelationship(idRelationship, relProperties, type, new TransytNode(idStartNode, nodeProperties)));
}
return rels;
}
/**
* Retrieve node's all downstream relationships of a specific type.
*
* @param id
* @param type
* @return
*/
public Set<TransytRelationship> getDownstreamRelationships(int id, TransytRelationshipType type) { //VERIFICAR SE EST A CHAM;AR BEM
Set<TransytRelationship> rels = new HashSet<>();
Session session = driver.session(AccessMode.READ);
Statement statement = new Statement( "MATCH (a)-[t:" + type + "]->(b) WHERE id(a)="+ id +" "
+ "RETURN {relid:id(t), relProps:properties(t), endid:id(b), endNodeProps:properties(b)}");
StatementResult res = session.run(statement);
session.close();
while(res.hasNext()) {
Record record = res.next();