org.biopax.paxtools.query.QueryExecuter 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;
import org.biopax.paxtools.model.BioPAXElement;
import org.biopax.paxtools.model.BioPAXLevel;
import org.biopax.paxtools.model.Model;
import org.biopax.paxtools.model.level3.*;
import org.biopax.paxtools.query.algorithm.*;
import org.biopax.paxtools.query.model.Graph;
import org.biopax.paxtools.query.model.GraphObject;
import org.biopax.paxtools.query.model.Node;
import org.biopax.paxtools.query.wrapperL3.Filter;
import org.biopax.paxtools.query.wrapperL3.GraphL3;
import org.biopax.paxtools.query.wrapperL3undirected.GraphL3Undirected;
import java.util.*;
/**
* This class provides static methods to execute graph queries. These cover only the most frequent
* use cases. Users can use these methods as example for executing the query they need.
*
* @author Ozgun Babur
*/
public class QueryExecuter
{
/**
* Gets neighborhood of the source set.
*
* @param sourceSet seed to the query
* @param model BioPAX model
* @param limit neigborhood distance to get
* @param direction UPSTREAM, DOWNSTREAM or BOTHSTREAM
* @param filters for filtering graph elements
* @return BioPAX elements in the result set
*/
public static Set runNeighborhood(
Set sourceSet,
Model model,
int limit,
Direction direction,
Filter... filters)
{
Graph graph;
if (model.getLevel() == BioPAXLevel.L3)
{
if (direction == Direction.UNDIRECTED)
{
graph = new GraphL3Undirected(model, filters);
direction = Direction.BOTHSTREAM;
}
else
{
graph = new GraphL3(model, filters);
}
}
else return Collections.emptySet();
Set source = prepareSingleNodeSet(sourceSet, graph);
if (sourceSet.isEmpty()) return Collections.emptySet();
NeighborhoodQuery query = new NeighborhoodQuery(source, direction, limit);
Set resultWrappers = query.run();
return convertQueryResult(resultWrappers, graph, true);
}
/**
* Gets the graph constructed by the paths between the given seed nodes. Does not get paths
* between physical entities that belong the same entity reference.
* @param sourceSet Seed to the query
* @param model BioPAX model
* @param limit Length limit for the paths to be found
* @param filters optional filters - for filtering graph elements
* @return BioPAX elements in the result
*/
public static Set runPathsBetween(Set sourceSet, Model model,
int limit, Filter... filters)
{
Graph graph;
if (model.getLevel() == BioPAXLevel.L3)
{
graph = new GraphL3(model, filters);
}
else return Collections.emptySet();
Collection> sourceWrappers = prepareNodeSets(sourceSet, graph);
if (sourceSet.size() < 2) return Collections.emptySet();
PathsBetweenQuery query = new PathsBetweenQuery(sourceWrappers, limit);
Set resultWrappers = query.run();
return convertQueryResult(resultWrappers, graph, true);
}
/**
* Gets paths between the seed nodes.
* @param sourceSet Seed to the query
* @param model BioPAX model
* @param limit Length limit for the paths to be found
* @param filters for filtering graph elements
* @return BioPAX elements in the result
* @deprecated Use runPathsBetween instead
*/
public static Set runGOI(
Set sourceSet,
Model model,
int limit,
Filter... filters)
{
return runPathsFromTo(sourceSet, sourceSet, model, LimitType.NORMAL, limit, filters);
}
/**
* Gets paths the graph composed of the paths from a source node, and ends at a target node.
* @param sourceSet Seeds for start points of paths
* @param targetSet Seeds for end points of paths
* @param model BioPAX model
* @param limitType either NORMAL or SHORTEST_PLUS_K
* @param limit Length limit fothe paths to be found
* @param filters for filtering graph elements
* @return BioPAX elements in the result
*/
public static Set runPathsFromTo(
Set sourceSet,
Set targetSet,
Model model,
LimitType limitType,
int limit,
Filter... filters)
{
Graph graph;
if (model.getLevel() == BioPAXLevel.L3)
{
graph = new GraphL3(model, filters);
}
else return Collections.emptySet();
Set source = prepareSingleNodeSet(sourceSet, graph);
Set target = prepareSingleNodeSet(targetSet, graph);
PathsFromToQuery query = new PathsFromToQuery(source, target, limitType, limit, true);
Set resultWrappers = query.run();
return convertQueryResult(resultWrappers, graph, true);
}
/**
* Gets the elements in the common upstream or downstream of the seed
* @param sourceSet Seed to the query
* @param model BioPAX model
* @param direction UPSTREAM or DOWNSTREAM
* @param limit Length limit for the search
* @param filters for filtering graph elements
* @return BioPAX elements in the result
*/
public static Set runCommonStream(
Set sourceSet,
Model model,
Direction direction,
int limit,
Filter... filters)
{
Graph graph;
if (model.getLevel() == BioPAXLevel.L3)
{
graph = new GraphL3(model, filters);
}
else return Collections.emptySet();
Collection> source = prepareNodeSets(sourceSet, graph);
if (sourceSet.size() < 2) return Collections.emptySet();
CommonStreamQuery query = new CommonStreamQuery(source, direction, limit);
Set resultWrappers = query.run();
return convertQueryResult(resultWrappers, graph, false);
}
/**
* First finds the common stream, then completes it with the paths between seed and common
* stream.
* @param sourceSet Seed to the query
* @param model BioPAX model
* @param direction UPSTREAM or DOWNSTREAM
* @param limit Length limit for the search
* @param filters for filtering graph elements
* @return BioPAX elements in the result
*/
public static Set runCommonStreamWithPOI(
Set sourceSet,
Model model,
Direction direction,
int limit,
Filter... filters)
{
Graph graph;
if (model.getLevel() == BioPAXLevel.L3)
{
graph = new GraphL3(model, filters);
}
else return Collections.emptySet();
Collection> sourceSets = prepareNodeSets(sourceSet, graph);
if (sourceSet.size() < 2) return Collections.emptySet();
// Run a common stream query
CommonStreamQuery commStream = new CommonStreamQuery(sourceSets, direction, limit);
Set resultWrappers = commStream.run();
// Stop if they have no common stream.
if (resultWrappers.isEmpty()) return Collections.emptySet();
// Extract nodes from the result
Set target = new HashSet();
for (GraphObject go : resultWrappers)
{
if (go instanceof Node) target.add((Node) go);
}
// Take union of the sources
Set source = new HashSet();
for (Set set : sourceSets)
{
source.addAll(set);
}
// Run a paths-of-interest query between source set and result set
PathsFromToQuery poi;
if (direction == Direction.DOWNSTREAM)
{
poi = new PathsFromToQuery(source, target, LimitType.NORMAL, limit, true);
}
else
{
poi = new PathsFromToQuery(target, source, LimitType.NORMAL, limit, true);
}
resultWrappers = poi.run();
return convertQueryResult(resultWrappers, graph, true);
}
/**
* Converts the query result from wrappers to wrapped BioPAX elements.
* @param resultWrappers Wrappers of the result set
* @param graph Queried graph
* @param removeDisconnected whether to remove disconnected non-complex type physical entities
* @return Set of elements in the result
*/
private static Set convertQueryResult(
Set resultWrappers, Graph graph, boolean removeDisconnected)
{
Set