Skip to content

Commit

Permalink
Initial cluestringCoefficient has been implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
dapregi committed Sep 29, 2015
1 parent 527c89f commit be8aee7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public class QueryCommandOptions {
@Parameter(names = {"--betweenness"}, description = "", required = false, arity = 0)
public boolean betweenness;

@Parameter(names = {"--clusteringCoeff"}, description = "", required = false, arity = 0)
public boolean clusteringCoeff;

@Parameter(names = {"-o", "--output-file"}, description = "", required = false, arity = 1)
public String outputFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public void execute() {
networkDBAdaptor.betweenness(query);
}

if (queryCommandOptions.clusteringCoeff) {
Query query = new Query("id", queryCommandOptions.id);
query.put("nodeLabel", queryCommandOptions.nodeType);

networkDBAdaptor.clusteringCoefficient(query);
}

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
4 changes: 4 additions & 0 deletions bionetdb-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opencb.bionetdb.core.neo4j;

import org.apache.commons.math3.util.CombinatoricsUtils;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.IndexManager;
Expand All @@ -12,6 +13,7 @@
import org.opencb.datastore.core.QueryOptions;
import org.opencb.datastore.core.QueryResult;

import java.text.DecimalFormat;
import java.util.*;

/**
Expand Down Expand Up @@ -278,7 +280,7 @@ private void insertPhysicalEntities(List<PhysicalEntity> physicalEntityList, Que
addRelationship(n, ont, RelTypes.ONTOLOGY);
}

/* Insert the cellular locations */
/* Insert the cellular locations */
for (CellularLocation c : p.getCellularLocation()) {
Node cel = getOrCreateCellularLocationNode(parseCellularLocation(c));
addRelationship(n, cel, RelTypes.CELLULARLOCATION);
Expand Down Expand Up @@ -847,6 +849,54 @@ public QueryResult betweenness(Query query) {

@Override
public QueryResult clusteringCoefficient(Query query) {
// The clustering coefficient of a node is defined as the probability that two randomly
// selected neighbors are connected to each other. With the number of neighbors as n and
// the number of mutual connections between the neighbors r the calculation is:
// clusteringCoefficient = r/NumberOfPossibleConnectionsBetweenTwoNeighbors. Where:
// NumberOfPossibleConnectionsBetweenTwoNeighbors: n!/(2!(n-2)!).

// TODO multiple ids
String id = query.getString("id");

StringBuilder cypherQuery = new StringBuilder();
cypherQuery.append("MATCH (a { name: \"" + id + "\" })--(:Interaction)--(b)");
cypherQuery.append(" WITH a, count(DISTINCT b) AS n");
cypherQuery.append(" MATCH (a)--(:Interaction)--(:PhysicalEntity)"
+ "--(:Interaction)-[r]-(:PhysicalEntity)--(:Interaction)--(a)");
cypherQuery.append(" MATCH (a)-[:CELLULARLOCATION]-(c:CellularLocation)");
cypherQuery.append(" RETURN a.name, c.id, n, count(DISTINCT r) AS r");

Result execute = this.database.execute(cypherQuery.toString());

if (execute.hasNext()) {
String msg = "#ID\tLOCATION\tCLUSTERING_COEFFICIENT";
System.out.println(msg);
while (execute.hasNext()) {
Map<String, Object> result = execute.next();
Integer r = (int) (long) result.get("r");
Integer n = (int) (long) result.get("n");

// Computed value must fit into a double. The largest n for which n! < Double.MAX_VALUE is 170.
if (n > 170) {
String msg2 = "\"" + result.get("a.name").toString() + "\"\t"
+ "\"" + result.get("c.id").toString() + "\"\t"
+ "\"NA\"";
System.out.println(msg2);
} else if (n > 1) {
double possibleConnexions = CombinatoricsUtils.factorialDouble(n)
/ (CombinatoricsUtils.factorialDouble(2) * (CombinatoricsUtils.factorialDouble(n - 2)));
DecimalFormat df = new DecimalFormat("###.##");
String msg3 = "\"" + result.get("a.name").toString() + "\"\t"
+ "\"" + result.get("c.id").toString() + "\"\t"
+ "\"" + df.format(r / possibleConnexions) + "\"";
System.out.println(msg3);
} else {
System.out.println(0.00);
}
}
} else {
System.out.println("Physical Entity not found");
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ public void testBetweenness() throws Exception {

}

@Test
public void testClusteringCoefficient() throws Exception {
loadTestData();
networkDBAdaptor.clusteringCoefficient(new Query("id", "PEP"));

}

private void loadTestData() throws URISyntaxException, IOException, NetworkDBException {
BioPaxParser bioPaxParser = new BioPaxParser("L3");
Path inputPath = Paths.get(getClass().getResource("/Saccharomyces_cerevisiae.owl.gz").toURI());
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down

0 comments on commit be8aee7

Please sign in to comment.