diff --git a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/analyze/GraphMeasure.java b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/analyze/GraphMeasure.java index b2cd754daff25e4e6da9c44b2f0892e4ad928042..e4d2de39423d36959a192e5c7f8fe096baf5b915 100644 --- a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/analyze/GraphMeasure.java +++ b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/analyze/GraphMeasure.java @@ -49,6 +49,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; +import java.util.Comparator; /** * compute several measures of the level or connectivity, size or shape of a given graph using lazy builder to avoid redundant calculus @@ -59,29 +61,29 @@ import java.util.Set; * @version $Id: $Id */ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { - + /** The graph. */ private final BioGraph<V, E> g; - + /** The number of edges. */ private double numberOfEdges; - + /** The number of vertex. */ private final double numberOfVertex; - + /** The number of connected component. */ private Integer numberOfConnectedComp; - + /** The diameter. */ private Double diameter; - + /** The length. */ private Double length; - + /** if the graph is directed */ private boolean directed = true; - - + + /** * Instantiates a new graph measure. * @@ -92,7 +94,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { this.numberOfEdges = Integer.valueOf(g.edgeSet().size()).doubleValue(); this.numberOfVertex = Integer.valueOf(g.vertexSet().size()).doubleValue(); } - + /** * Get list of set of BioMetabolite, each BioMetabolite in a set belong to the same connected component * @param <V> vertex class @@ -101,10 +103,82 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { * @param g the graph * @return the connected component */ - public static <V extends BioEntity, E extends Edge<V>> List<Set<V>> getConnectedCompenent(BioGraph<V,E> g){ + public static <V extends BioEntity, E extends Edge<V>> List<Set<V>> getConnectedComponents(BioGraph<V,E> g){ return new ConnectivityInspector<>(g).connectedSets(); } - + /** + * Get a set of all vertices that are in the maximally connected component together with the specified vertex. + * @param <V> vertex class + * @param <E> edge class + * + * @param g the graph + * @param t the target vertex + * @return the set of all vertices maximally connected to the target vertex + */ + //public static <V extends BioEntity, E extends Edge<V>, BioEntity<T>> Set<V>> getConnectedSetOf(BioGraph<V,E> g, Vertex <T>){ + public static <V extends BioEntity, E extends Edge<V>> Set<V> getConnectedSetOf(BioGraph<V,E> g, V t){ + return new ConnectivityInspector<>(g).connectedSetOf(t); + } + + /** + * Get the position of the component (ordered by size) for a given vertex + * @param <V> vertex class + * @param <E> edge class + * + * @param g the graph + * @param t the target vertex + * @return the component containing the vertex and output component ranking in the console. + */ + public static <V extends BioEntity, E extends Edge<V>> Set<V> isPartofNComponent(BioGraph<V,E> g, V t){ + //init connectivity inspector + ConnectivityInspector CI = new ConnectivityInspector<>(g); + //Sort the list of components by size + List<Set<V>> components = CI.connectedSets(); + //sort with lambdafunction + Comparator<Set<V>> reversedComparator = (s1,s2) -> Integer.valueOf(s2.size()).compareTo(s1.size()); + List<Set<V>> componentsReverseOrder = components.stream().sorted(reversedComparator).collect(Collectors.toList()); + // Collections.sort(components,Comparator.reverseOrder()); + //Get the set of all vertices maximally connected to the target vertex + Set<V> SetOfT = CI.connectedSetOf(t); + int counter = 0; + for(Set s:componentsReverseOrder){ + counter++; + if (s.containsAll(SetOfT)){ + if (counter == 1){ + System.out.println("Metabolite "+t.getName()+" is part of the "+counter+" st component"); + }else if (counter == 2){ + System.out.println("Metabolite "+t.getName()+" is part of the "+counter+" nd component"); + }else if (counter == 3){ + System.out.println("Metabolite "+t.getName()+" is part of the "+counter+" rd component"); + }else{ + System.out.println("Metabolite "+t.getName()+" is part of the "+counter+" th component"); + } + return s; + } + } + return null; + } + /** + * Get the number of edges of the provided component + * @param <V> vertex class + * @param <E> edge class + * + * @param g the graph + * @param comp the connected component + * @return the number of edges in this component + */ + public static <V extends BioEntity, E extends Edge<V>> Integer getNumberEdgesOfComponent(BioGraph<V,E> g, Set<V> comp){ + Integer n_edges = 0; + for(V v1 : comp) { + for(V v2 : comp) { + if((v1 != v2) && (g.containsEdge(v1, v2))) { + n_edges++; + } + } + } + return n_edges; + } + /** * Gets the number of connected component * @@ -112,10 +186,10 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { */ public int getNumberOfConnectedComponent(){ if(numberOfConnectedComp !=null) return numberOfConnectedComp; - numberOfConnectedComp = getConnectedCompenent(g).size(); + numberOfConnectedComp = getConnectedComponents(g).size(); return numberOfConnectedComp; } - + /** * Gets the number of cycle. * @@ -127,7 +201,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { int numberOfVertex = g.vertexSet().size(); return numberOfEdges-numberOfVertex+numberOfConnectedComp; } - + /** * Gets the diameter of the graph, i.e. the maximum length of a shortest path between two node in the graph * If the graph is disconnected, return the longest distance found in any connected component @@ -155,7 +229,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { int diameter = (int) distStats.getMax(); return diameter; } - + /** * Gets the gamma index of the graph, i.e. the ratio between the observed number of edges and the expected maximal number of possible edge, as measure of the level of connectivity * @@ -166,7 +240,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { if(!directed) maxNumberOfEdges = maxNumberOfEdges*0.5; return numberOfEdges /maxNumberOfEdges; } - + /** * Gets the alpha index of the graph, i.e. the ratio between the observed number of cycle and the expected maximal number of possible cycle, as measure of the level of connectivity * cannot be computed on directed graph @@ -179,7 +253,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { double maxNumberOfCycle = (numberOfVertex *(numberOfVertex -1))*0.5 - (numberOfVertex -1); return (numberOfEdges -(numberOfVertex -1))/maxNumberOfCycle; } - + /** * Gets the beta index of the graph, i.e. the ratio between the number of edges and the number of vertex as measure of the level of connectivity * @@ -188,7 +262,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { public double getBeta(){ return numberOfEdges / numberOfVertex; } - + /** * Gets the total length of the graph, i.e. the sum of each edge weight * @@ -202,7 +276,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { } return length; } - + /** * Gets the eta index, i.e. the mean length of edges. * @@ -211,7 +285,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { public double getEta(){ return getLength()/ numberOfEdges; } - + /** * Gets the pi index, i.e. the ratio between the diameter of the graph and the total graph length. * @@ -220,7 +294,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { public double getPi(){ return getDiameter()/ getLength(); } - + /** * get the overall closeness centralization index (OCCI), according to Freeman,L.C. (1979) Centrality in social networks: Conceptual clarification. Social Networks, 1, 215–239. * @@ -233,24 +307,24 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { PathBasedCentrality<V, E, BioGraph<V, E>> centralityComputor = new PathBasedCentrality<>(g); Set<BioPath<V, E>> paths = centralityComputor.getAllShortestPaths(); Map<V, Double> closenessIndex = centralityComputor.getInCloseness(paths); - + double max = 0.0; for(Double closeness : closenessIndex.values()){ if(closeness>max) max=closeness; } - + double sum = 0.0; for(Double closeness : closenessIndex.values()){ sum += (max - closeness); } //normalize centrality sum = sum*(g.vertexSet().size()-1); - + double occi = (2* numberOfVertex - 3) * sum; occi = occi/((numberOfVertex -1)*(numberOfVertex -2)); return occi; } - + /** * get the overall closeness centralization index (OCCI), according to Freeman,L.C. (1979) Centrality in social networks: Conceptual clarification. Social Networks, 1, 215–239. * @@ -263,23 +337,23 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { public double getOCCI(Set<BioPath<V, E>> validPaths){ PathBasedCentrality<V, E, BioGraph<V, E>> centralityComputor = new PathBasedCentrality<>(g); Map<V, Double> closenessIndex = centralityComputor.getCloseness(validPaths); - + double max = 0.0; for(Double closeness : closenessIndex.values()){ if(closeness>max) max=closeness; } - + double sum = 0.0; for(Double closeness : closenessIndex.values()){ sum += (max - closeness); } - + double occi = (2* numberOfVertex - 3) * sum; occi = (occi / ((numberOfVertex -1)*(numberOfVertex -2))); - + return occi; } - + /** * adjust the edge count for multigraph. Edges having the same source and target will be counted as one edge. */ @@ -290,7 +364,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { } this.numberOfEdges =links.size(); } - + /** * get whether or not the graph is considered as directed * @@ -299,7 +373,7 @@ public class GraphMeasure<V extends BioEntity, E extends Edge<V>> { public boolean isDirected() { return directed; } - + /** * set whether or not the graph should be considered as directed * @param directed if the network should be considered as directed diff --git a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/connect/weighting/RPAIRSlikePolicy.java b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/connect/weighting/RPAIRSlikePolicy.java new file mode 100644 index 0000000000000000000000000000000000000000..269cbcf03e6ee7f9e3378b058b353b1c1a740822 --- /dev/null +++ b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/connect/weighting/RPAIRSlikePolicy.java @@ -0,0 +1,146 @@ +package fr.inrae.toulouse.metexplore.met4j_graph.computation.connect.weighting; + +import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioEntity; +import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioMetabolite; +import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioReaction; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.analyze.GraphMeasure; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.transform.GraphFilter; +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.GraphFactory; +import fr.inrae.toulouse.metexplore.met4j_graph.core.WeightingPolicy; +import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.CompoundGraph; +import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.ReactionEdge; + +import java.util.*; + +/** + * Weighting policy that mimic RPAIRS's now discontinued tags. Define Main and Side transitions within each reaction. + * This 'main' tag also include the RPAIRS 'trans' tag. + */ +public class RPAIRSlikePolicy extends WeightingPolicy<BioMetabolite, ReactionEdge, CompoundGraph> { + + AtomMappingWeightPolicy preprocess; + Double mainValue = 1.0; + Double sideValue = 0.0; + + Double spuriousValue = -1.0; + Boolean removeSide = false; + Boolean removeSpurious = false; + + public RPAIRSlikePolicy(AtomMappingWeightPolicy aam){ + preprocess=aam.binarize(); + preprocess.removeNoCC=false; + preprocess.removeNotFound=false; + } + + /** + * remove side transitions , estimated from edge redundancy + * @return + */ + public RPAIRSlikePolicy removeSideTransitions(){ + removeSide=true; + removeSpurious=true; + return this; + } + + /** + * remove spurious transitions, which don't appear in the carbon skeleton graph (not involving at least two C) + * @return this instance + */ + public RPAIRSlikePolicy removeSpuriousTransitions(){ + removeSpurious=true; + return this; + } + + @Override + public void setWeight(CompoundGraph compoundGraph) { + preprocess.setWeight(compoundGraph); + Set<ReactionEdge> spuriousEdges = new HashSet<>(); + Set<ReactionEdge> validEdges = new HashSet<>(); + + //spurious edges are not directly tagged but stored in first step, to avoid conflict if spuriousValue match with some AtomMappingWeightPolicy weights + for(ReactionEdge e : compoundGraph.edgeSet()){ + if(Double.isNaN(compoundGraph.getEdgeWeight(e)) || compoundGraph.getEdgeWeight(e)==0.0){ + spuriousEdges.add(e); + }else { + validEdges.add(e); + } + } + + for(ReactionEdge e : validEdges){ + Set<BioMetabolite> mainCC = getMainComponent(compoundGraph,e.getReaction(), spuriousEdges); + if(mainCC.contains(e.getV1()) && mainCC.contains(e.getV2())){ + compoundGraph.setEdgeWeight(e,mainValue); + }else{ + compoundGraph.setEdgeWeight(e,sideValue); + } + } + + if(removeSide){ + GraphFilter.weightFilter(compoundGraph,sideValue,GraphFilter.EQUALITY); + } + if(removeSpurious){ + compoundGraph.removeAllEdges(spuriousEdges); + }else{ + for(ReactionEdge e : spuriousEdges){ + compoundGraph.setEdgeWeight(e,spuriousValue); + } + } + if(removeSpurious || removeSide) compoundGraph.removeIsolatedNodes(); + } + + /** + * Compute the highest scored connected component of a reaction subgraph + * + * @param g the cpd graph skeleton network + * @param reaction the reaction + * @return a set of compounds in the best connected component + */ + public Set<BioMetabolite> getMainComponent(CompoundGraph g, BioReaction reaction, Set<ReactionEdge> spuriousEdges){ + //Compute reaction subgraph + CompoundGraph rG = g.getReactionSubGraph(reaction); + + //remove spurious edges + Set<ReactionEdge> edgesToRemove = new HashSet<>(rG.edgeSet()); + edgesToRemove.retainAll(spuriousEdges); + rG.removeAllEdges(edgesToRemove); + if(rG.edgeSet().size()==0) return new HashSet<>(); + + //Compute connected components + List<Set<BioMetabolite>> cc = GraphMeasure.getConnectedComponents(rG); + Double bestScore = 0.0; + Set<BioMetabolite> mainCC = null; + + for(Set<BioMetabolite> component : cc) { + CompoundGraph ccSubGraph = CompoundGraph.getFactory().createSubGraph(rG,component); + Double score = getComponentScore(g,ccSubGraph); + if(score>bestScore){ + bestScore = score; + mainCC = component; + } + } + return mainCC; + } + + /** + * Get the number of parallel edges of the provided component + * @param <V> vertex class + * @param <E> edge class + * + * @param wholeG the complete graph + * @param subG the reaction connected component + * @return the redundancy score, i.e. estimation of how many other reactions share the same edges as the component + */ + private <V extends BioEntity, E extends Edge<V>> Double getComponentScore(CompoundGraph wholeG, CompoundGraph subG){ + Double Nedge = Double.valueOf(subG.edgeSet().size()); + Double NPedge = 0.0; + for(ReactionEdge e : subG.edgeSet()){ + BioMetabolite v1 = e.getV1(); + BioMetabolite v2 = e.getV2(); + Integer redundancy = wholeG.getAllEdges(v1,v2).size(); + NPedge = NPedge + Double.valueOf(redundancy); + } + return Nedge/NPedge; + } +} diff --git a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/transform/GraphOperation.java b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/transform/GraphOperation.java index 59e933a8ea4d862d3a7330a61a6b9c2f028c331d..be5c8da3013cadcc441147cabded865cd8b1038d 100644 --- a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/transform/GraphOperation.java +++ b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/computation/transform/GraphOperation.java @@ -53,7 +53,7 @@ public class GraphOperation { * Instantiates a new graph operation. */ public GraphOperation() {} - + /** * compute the intersection of two graph. * @@ -75,7 +75,7 @@ public class GraphOperation { } return i; } - + /** * compute the union of two graph. * @@ -103,7 +103,7 @@ public class GraphOperation { } return u; } - + /** * compute the size of the intersect of two graph. * @@ -121,7 +121,7 @@ public class GraphOperation { } return size; } - + /** * compute the size of the union of two graph. * @@ -135,7 +135,7 @@ public class GraphOperation { public static <V extends BioEntity, E extends Edge<V>, G extends BioGraph<V,E>> int unionSize(G g1, G g2){ return g1.edgeSet().size()+g2.edgeSet().size()- intersectSize(g1, g2); } - + /** * compute the order of the intersect of two graph. * @@ -151,7 +151,7 @@ public class GraphOperation { } return order; } - + /** * compute the order of the union of two graph. * @param <G> the graph class @@ -163,5 +163,5 @@ public class GraphOperation { public static <G extends BioGraph<? extends BioEntity, ? extends Edge<?>>> int unionOrder(G g1, G g2){ return g1.vertexSet().size()+g2.vertexSet().size()- intersectOrder(g1, g2); } - + } diff --git a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/core/compound/CompoundGraph.java b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/core/compound/CompoundGraph.java index 260ef34c84469fcb22810b8d2004ece3dbb051b8..5ec7f02027a9c208c724003216a58820fd042385 100644 --- a/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/core/compound/CompoundGraph.java +++ b/met4j-graph/src/main/java/fr/inrae/toulouse/metexplore/met4j_graph/core/compound/CompoundGraph.java @@ -106,6 +106,24 @@ public class CompoundGraph extends BioGraph<BioMetabolite, ReactionEdge> { } return edgeList; } + + /** + * Compute the reaction subgraph + * + * @param reaction the reaction to evaluate + * @return the reaction subgraph + */ + public CompoundGraph getReactionSubGraph(BioReaction reaction) { + //Create graph for the reaction + CompoundGraph rSubGraph = new CompoundGraph(); + //Create all possible edges between left and right reactants + for(ReactionEdge e : this.getEdgesFromReaction(reaction.getId())){ + rSubGraph.addVertex(e.getV1()); + rSubGraph.addVertex(e.getV2()); + rSubGraph.addEdge(e); + } + return rSubGraph; + } /** * Gets the biochemical reaction list. diff --git a/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestCompoundGraph.java b/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestCompoundGraph.java index 790f81c4f18a0812e1f0bf098af5df97a4265fee..d9608c79e4eddef8b2ef7a8502a81ce8816b05ee 100644 --- a/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestCompoundGraph.java +++ b/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestCompoundGraph.java @@ -186,4 +186,20 @@ public class TestCompoundGraph { assertEquals(0,cg.getEdgesFromCompartment(bn, comp2).size()); assertTrue(cg.getEdgesFromCompartment(bn, comp).contains(e1)); } + + + @Test + public void testGetReactionSubGraph() { + CompoundGraph gr1 = cg.getReactionSubGraph(r1); + assertEquals(2, gr1.vertexSet().size()); + assertEquals(1, gr1.edgeSet().size()); + assertTrue(gr1.containsVertex(v1)); + assertTrue(gr1.containsVertex(v2)); + + CompoundGraph gr2 = cg.getReactionSubGraph(r2); + assertEquals(2, gr2.vertexSet().size()); + assertEquals(1, gr2.edgeSet().size()); + assertTrue(gr2.containsVertex(v2)); + assertTrue(gr2.containsVertex(v3)); + } } diff --git a/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestRPAIRSlikePolicy.java b/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestRPAIRSlikePolicy.java new file mode 100644 index 0000000000000000000000000000000000000000..4a1be9dbba4b5873f65ea6f840fdd3dcb9c02d71 --- /dev/null +++ b/met4j-graph/src/test/java/fr/inrae/toulouse/metexplore/met4j_graph/TestRPAIRSlikePolicy.java @@ -0,0 +1,227 @@ +package fr.inrae.toulouse.metexplore.met4j_graph; + +import fr.inrae.toulouse.metexplore.met4j_chemUtils.chemicalSimilarity.FingerprintBuilder; +import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioMetabolite; +import fr.inrae.toulouse.metexplore.met4j_core.biodata.BioReaction; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.connect.weighting.AtomMappingWeightPolicy; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.connect.weighting.DefaultWeightPolicy; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.connect.weighting.RPAIRSlikePolicy; +import fr.inrae.toulouse.metexplore.met4j_graph.core.WeightingPolicy; +import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.CompoundGraph; +import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.ReactionEdge; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openscience.cdk.exception.CDKException; +import org.openscience.cdk.similarity.Tanimoto; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestRPAIRSlikePolicy { + + public static CompoundGraph g; + + public static BioMetabolite m1sm,m1ss,m1ps,m1pm; + public static BioMetabolite m2sm1,m2sm2,m2ss,m2pm,m2ps1,m2ps2; + public static Map<BioMetabolite, Map<BioMetabolite,Integer>> aam; + public static ReactionEdge t1m,t1s1,t1sp1,t1sp2; + public static ReactionEdge t2sp1,t2sp2,t2sp3,t2sp4,t2sp5,t2sp6,t2s1,t2m1,t2m2; + + public static BioReaction r1,r2; + + @BeforeClass + public static void init(){ + //create graph + g = new CompoundGraph(); + + //create reaction 1 + r1 = new BioReaction("1"); + aam = new HashMap<BioMetabolite, Map<BioMetabolite,Integer>>(); + //add substrate metabolites + m1sm = new BioMetabolite("glucose","glucose"); + g.addVertex(m1sm); + aam.put(m1sm,new HashMap<>()); + m1ss = new BioMetabolite("atp","atp"); + g.addVertex(m1ss); + aam.put(m1ss,new HashMap<>()); + //add product metabolites + m1ps = new BioMetabolite("adp","adp"); + g.addVertex(m1ps); + m1pm = new BioMetabolite("glucose6p","glucose-6-P"); + g.addVertex(m1pm); + //add edges + //main + t1m = new ReactionEdge(m1sm,m1pm,r1); + g.addEdge(m1sm,m1pm,t1m); + aam.get(m1sm).put(m1pm,6); + //side + t1s1 = new ReactionEdge(m1ss,m1ps,r1); + g.addEdge(m1ss,m1ps,t1s1); + aam.get(m1ss).put(m1ps,10); + //spurious + t1sp1 = new ReactionEdge(m1sm,m1ps,r1); + g.addEdge(m1sm,m1ps,t1sp1); + aam.get(m1sm).put(m1ps,0); + t1sp2 = new ReactionEdge(m1ss,m1pm,r1); + g.addEdge(m1ss,m1pm,t1sp2); + aam.get(m1ss).put(m1pm,0); + //create reaction 2 + r2 = new BioReaction("2"); + //add substrate metabolites + m2sm1 = new BioMetabolite("propanoate","propanoate"); + g.addVertex(m2sm1); + aam.put(m2sm1,new HashMap<>()); + m2sm2 = new BioMetabolite("coa","coa"); + g.addVertex(m2sm2); + aam.put(m2sm2,new HashMap<>()); + m2ss = m1ss; + //add product metabolites + m2pm = new BioMetabolite("propanoylcoa","propanoyl-COA"); + g.addVertex(m2pm); + m2ps1 = new BioMetabolite("pi","pi"); + g.addVertex(m2ps1); + m2ps2 = m1ps; + //add edges + //main + t2m1 = new ReactionEdge(m2sm1,m2pm,r2); + g.addEdge(m2sm1,m2pm,t2m1); + aam.get(m2sm1).put(m2pm,3); + t2m2 = new ReactionEdge(m2sm2,m2pm,r2); + g.addEdge(m2sm2,m2pm,t2m2); + aam.get(m2sm2).put(m2pm,21); + //side + t2s1 = new ReactionEdge(m2ss,m2ps2,r2); + g.addEdge(m2ss,m2ps2,t2s1); + aam.get(m2ss).put(m2ps2,10); + //spurious + t2sp1 = new ReactionEdge(m2sm1,m2ps1,r2); + g.addEdge(m2sm1,m2ps1,t2sp1); + aam.get(m2sm1).put(m2ps1,0); + t2sp2 = new ReactionEdge(m2sm1,m2ps2,r2); + g.addEdge(m2sm1,m2ps2,t2sp2); + aam.get(m2sm1).put(m2ps2,0); + t2sp3 = new ReactionEdge(m2sm2,m2ps1,r2); + g.addEdge(m2sm2,m2ps1,t2sp3); + aam.get(m2sm2).put(m2ps1,0); + t2sp4 = new ReactionEdge(m2sm2,m2ps2,r2); + g.addEdge(m2sm2,m2ps2,t2sp4); + aam.get(m2sm2).put(m2ps2,0); + t2sp5 = new ReactionEdge(m2ss,m2pm,r2); + g.addEdge(m2ss,m2pm,t2sp5); + aam.get(m2ss).put(m2pm,0); + t2sp6 = new ReactionEdge(m2ss,m2ps1,r2); + g.addEdge(m2ss,m2ps1,t2sp6); + aam.get(m2ss).put(m2ps1,0); + } + + /** + * Reset weight. + */ + @After + public void resetWeight(){ + init(); + } + + @Test + public void testRPAIRSlikeWeightPolicy() { + AtomMappingWeightPolicy preprocess = new AtomMappingWeightPolicy().fromNumberOfConservedCarbons(aam); + RPAIRSlikePolicy wp = new RPAIRSlikePolicy(preprocess).removeSpuriousTransitions(); + wp.setWeight(g); + + assertEquals(1.0,g.getEdgeWeight(t1m),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m1),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m2),Double.MIN_VALUE); + + assertEquals(0.0,g.getEdgeWeight(t1s1),Double.MIN_VALUE); + assertEquals(0.0,g.getEdgeWeight(t2s1),Double.MIN_VALUE); + + assertTrue(!g.containsEdge(t1sp1)); + assertTrue(!g.containsEdge(t1sp2)); + assertTrue(!g.containsEdge(t2sp1)); + assertTrue(!g.containsEdge(t2sp2)); + assertTrue(!g.containsEdge(t2sp3)); + assertTrue(!g.containsEdge(t2sp4)); + assertTrue(!g.containsEdge(t2sp5)); + assertTrue(!g.containsEdge(t2sp6)); + + } + + @Test + public void testRPAIRSlikeWeightPolicyII() { + AtomMappingWeightPolicy preprocess = new AtomMappingWeightPolicy().fromNumberOfConservedCarbons(aam); + RPAIRSlikePolicy wp = new RPAIRSlikePolicy(preprocess); + wp.setWeight(g); + + assertEquals(1.0,g.getEdgeWeight(t1m),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m1),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m2),Double.MIN_VALUE); + + assertEquals(0.0,g.getEdgeWeight(t1s1),Double.MIN_VALUE); + assertEquals(0.0,g.getEdgeWeight(t2s1),Double.MIN_VALUE); + + assertEquals(-1.0,g.getEdgeWeight(t1sp1),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t1sp2),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp1),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp2),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp3),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp4),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp5),Double.MIN_VALUE); + assertEquals(-1.0,g.getEdgeWeight(t2sp6),Double.MIN_VALUE); + + } + + @Test + public void testRPAIRSlikeWeightPolicyIII() { + AtomMappingWeightPolicy preprocess = new AtomMappingWeightPolicy().fromNumberOfConservedCarbons(aam); + RPAIRSlikePolicy wp = new RPAIRSlikePolicy(preprocess).removeSideTransitions(); + wp.setWeight(g); + + assertEquals(1.0,g.getEdgeWeight(t1m),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m1),Double.MIN_VALUE); + assertEquals(1.0,g.getEdgeWeight(t2m2),Double.MIN_VALUE); + + assertTrue(!g.containsEdge(t1s1)); + assertTrue(!g.containsEdge(t2s1)); + + assertTrue(!g.containsEdge(t1sp1)); + assertTrue(!g.containsEdge(t1sp2)); + assertTrue(!g.containsEdge(t2sp1)); + assertTrue(!g.containsEdge(t2sp2)); + assertTrue(!g.containsEdge(t2sp3)); + assertTrue(!g.containsEdge(t2sp4)); + assertTrue(!g.containsEdge(t2sp5)); + assertTrue(!g.containsEdge(t2sp6)); + + } + + @Test + public void testGetMainComponent() { + AtomMappingWeightPolicy preprocess = new AtomMappingWeightPolicy().fromNumberOfConservedCarbons(aam); + RPAIRSlikePolicy wp = new RPAIRSlikePolicy(preprocess); + Set<ReactionEdge> spuriousEdges = new HashSet<>(); + spuriousEdges.add(t1sp1); + spuriousEdges.add(t1sp2); + spuriousEdges.add(t2sp1); + spuriousEdges.add(t2sp2); + spuriousEdges.add(t2sp3); + spuriousEdges.add(t2sp4); + spuriousEdges.add(t2sp5); + spuriousEdges.add(t2sp6); + + Set<BioMetabolite> cc = wp.getMainComponent(g,r1, spuriousEdges); + assertTrue(cc.contains(m1sm)); + assertTrue(cc.contains(m1pm)); + + Set<BioMetabolite> cc2 = wp.getMainComponent(g,r2, spuriousEdges); + assertTrue(cc2.contains(m2sm1)); + assertTrue(cc2.contains(m2sm2)); + assertTrue(cc2.contains(m2pm)); + } + +} \ No newline at end of file diff --git a/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/CarbonSkeletonNet.java b/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/CarbonSkeletonNet.java index b74845ac9cf22d0e259eb3aa358ee89107d8aa23..043e7d799555fbc474f5581e6b927175dccde3b3 100644 --- a/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/CarbonSkeletonNet.java +++ b/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/CarbonSkeletonNet.java @@ -8,6 +8,7 @@ import fr.inrae.toulouse.metexplore.met4j_graph.computation.connect.weighting.Re import fr.inrae.toulouse.metexplore.met4j_graph.computation.transform.EdgeMerger; import fr.inrae.toulouse.metexplore.met4j_graph.computation.transform.VertexContraction; import fr.inrae.toulouse.metexplore.met4j_graph.computation.utils.ComputeAdjacencyMatrix; +import fr.inrae.toulouse.metexplore.met4j_graph.computation.transform.GraphOperation; import fr.inrae.toulouse.metexplore.met4j_graph.core.compound.CompoundGraph; import fr.inrae.toulouse.metexplore.met4j_graph.io.Bionetwork2BioGraph; import fr.inrae.toulouse.metexplore.met4j_graph.io.ExportGraph; @@ -20,6 +21,8 @@ import fr.inrae.toulouse.metexplore.met4j_io.jsbml.reader.plugin.PackageParser; import fr.inrae.toulouse.metexplore.met4j_mathUtils.matrix.ExportMatrix; import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.AbstractMet4jApplication; import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.annotations.*; + +import org.apache.commons.lang3.StringUtils; import org.kohsuke.args4j.Option; import java.io.IOException; diff --git a/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/NetworkSummary.java b/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/NetworkSummary.java index 0d03d674f03fad79316dd8d87d51322de43066f3..0c41e4bf202e7004a484453a93941ea6bfd35a88 100644 --- a/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/NetworkSummary.java +++ b/met4j-toolbox/src/main/java/fr/inrae/toulouse/metexplore/met4j_toolbox/networkAnalysis/NetworkSummary.java @@ -18,7 +18,6 @@ import fr.inrae.toulouse.metexplore.met4j_io.jsbml.reader.JsbmlReader; import fr.inrae.toulouse.metexplore.met4j_io.jsbml.reader.Met4jSbmlReaderException; import fr.inrae.toulouse.metexplore.met4j_mathUtils.matrix.BioMatrix; import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.AbstractMet4jApplication; -import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.annotations.EnumFormats; import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.annotations.Format; import fr.inrae.toulouse.metexplore.met4j_toolbox.generic.annotations.ParameterType; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; @@ -135,7 +134,7 @@ public class NetworkSummary extends AbstractMet4jApplication { //connectivity System.err.println("extract connected component..."); - List<Set<BioMetabolite>> cc = GraphMeasure.getConnectedCompenent(graph); + List<Set<BioMetabolite>> cc = GraphMeasure.getConnectedComponents(graph); fw.write("Number of connected component:\t" + cc.size() + "\n"); Map<Integer, Integer> ccSizes = cc.stream().collect(Collectors.groupingBy(Set::size)) .entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (e -> e.getValue().size())));