Skip to content
Snippets Groups Projects

A BioGraph with meta-nodes

Open Clement Frainay requested to merge feature/FormulaNet into develop
12 files
+ 465
69
Compare changes
  • Side-by-side
  • Inline
Files
12
@@ -40,6 +40,8 @@ import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioMetabolite;
import fr.inrae.toulouse.metexplore.met4j_graph.core.BioGraph;
import fr.inrae.toulouse.metexplore.met4j_graph.core.Edge;
import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.CompoundGraph;
import fr.inrae.toulouse.metexplore.met4j_graph.core.meta.MetaGraph;
import fr.inrae.toulouse.metexplore.met4j_graph.core.meta.MetaNode;
import java.util.*;
import java.util.function.Function;
@@ -103,7 +105,7 @@ public class VertexContraction<V extends BioEntity,E extends Edge<V>, G extends
*/
public static <V extends BioEntity, E extends Edge<V>, G extends BioGraph<V,E>> G contractBy(G g, Function<V,String> groupingFunction, Function<List<V>,V> pickFunction){
G g2 = (G) g.clone();
Map<String, List<V>> groupedNodes = g.vertexSet().stream().collect(Collectors.groupingBy(groupingFunction));
Map<String, List<V>> groupedNodes = g.vertexSet().stream().filter(x->(groupingFunction.apply(x)!=null)).collect(Collectors.groupingBy(groupingFunction));
for(List<V> toContract : groupedNodes.values()){
V v = pickFunction.apply(toContract);
toContract.remove(v);
@@ -112,6 +114,33 @@ public class VertexContraction<V extends BioEntity,E extends Edge<V>, G extends
return g2;
}
/**
* Contract all nodes in graph from an aggregation function, which provide a common group id (key) for each member of a set to contract
* creates a MetaGraph, where each node contains the set of contracted nodes sharing the same group id
* @param g the graph
* @param groupingFunction the aggregation function
* @param <V> the node class
* @param <E> the edge class
* @param <G> the graph class
* @return a meta-graph with super-vertices referencing the contracted nodes
*/
public static <V extends BioEntity, E extends Edge<V>, G extends BioGraph<V,E>> MetaGraph<V,E> contractByAsMeta(G g, Function<V,String> groupingFunction){
MetaGraph<V,E> g2 = new MetaGraph<V,E>();
Map<String, List<V>> groupedNodes = g.vertexSet().stream().filter(x->(groupingFunction.apply(x)!=null)).collect(Collectors.groupingBy(groupingFunction));
for(Map.Entry<String, List<V>> toContract : groupedNodes.entrySet()) {
g2.addVertex(new MetaNode<>(toContract.getKey(), toContract.getValue()));
}
for(E edge : g.edgeSet()){
try{
g2.addEdgesFromSubVertices(edge);
}catch (IllegalArgumentException e){
System.err.println("Edge "+edge.getV1().getId()+"->"+edge.getV2().getId()+" contains node ignored during vertex contraction " +
" (this may happen if some node as undefined arguments)");
}
}
return g2;
}
/**
* Remove compartment in a compound graph by contracting all nodes sharing a given attribute provided by the mapper
* @param g the graph to decompartmentalize
Loading