org.biopax.paxtools.query.algorithm.PathsBetweenQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of paxtools-query Show documentation
Show all versions of paxtools-query Show documentation
BioPAX graph-theoretic querying - for finding paths between molecules,
or identifying molecules that are reachable through specific paths, using the BioPAX pathway data model.
package org.biopax.paxtools.query.algorithm;
import org.biopax.paxtools.query.model.GraphObject;
import org.biopax.paxtools.query.model.Node;
import java.util.*;
/**
* Finds the paths between the specified source set of states within the boundaries of a
* specified length limit.
*
* @author Ozgun Babur
*/
public class PathsBetweenQuery
{
/**
* The set of nodes from which the paths of interests should start.
*/
private Collection> sourceSet;
/**
* Based on the limitType, given integer may be used directly as stop
* distance or may be added up with the shortest path's length and used as
* stop distance.
*/
private int limit;
/**
* Constructor with parameters
* @param sourceSet Seed to the query
* @param limit Distance limit
*/
public PathsBetweenQuery(Collection> sourceSet, int limit)
{
this.sourceSet = sourceSet;
this.limit = limit;
}
public Set run()
{
/**
* Distance labels of graph objects. Note that each source set may have a distinct label for
* the object.
*/
Map, Integer>> fwdObj = new HashMap, Integer>>();
Map, Integer>> revObj = new HashMap, Integer>>();
Set result = new HashSet();
for (Set set : sourceSet)
{
BFS bfsFwd = new BFS(set, null, Direction.DOWNSTREAM, limit);
BFS bfsRev = new BFS(set, null, Direction.UPSTREAM, limit);
recordLabels(fwdObj, set, bfsFwd.run());
recordLabels(revObj, set, bfsRev.run());
}
/**
* Only the graph objects whose sum of two search labels, coming from different sets,
* being smaller than or equal to the distance limit will be in the result.
*/
for (GraphObject go : fwdObj.keySet())
{
if (!revObj.containsKey(go)) continue;
if (onTheResultPath(fwdObj.get(go), revObj.get(go)))
{
result.add(go);
}
}
Set sources = new HashSet();
for (Set set : sourceSet)
{
sources.addAll(set);
}
CycleBreaker breaker = new CycleBreaker(result, sources, limit);
breaker.breakCycles();
Prune prune = new Prune(result, sources);
prune.run();
return result;
}
private void recordLabels(Map, Integer>> labels, Set set,
Map bfsResult)
{
for (GraphObject go : bfsResult.keySet())
{
if (!labels.containsKey(go)) labels.put(go, new HashMap, Integer>());
labels.get(go).put(set, bfsResult.get(go));
}
}
private boolean onTheResultPath(Map, Integer> fwdMap, Map, Integer> revMap)
{
for (Set set1 : fwdMap.keySet())
{
for (Set set2 : revMap.keySet())
{
if (set1 == set2) continue;
int dist = fwdMap.get(set1) + revMap.get(set2);
if (dist <= limit) return true;
}
}
return false;
}
}