Commit 814f1db3 authored by Rayan Chikhi's avatar Rayan Chikhi
Browse files

getting snippets to compile with new Graph API

parent 92595cce
......@@ -17,7 +17,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::create (new BankStrings ("AATGC", NULL), "-kmer-size 4 -abundance-min 1 -verbose 0");
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator ();
// We check that we have only two possible nodes
assert (it.size() == 2);
......@@ -38,7 +38,7 @@ int main (int argc, char* argv[])
{
// We get the neighbors of this specific current
// Note that we get here the full edge information
Graph::Vector<Edge> neighbors = graph.successors<Edge> (current);
Graph::Vector<Edge> neighbors = graph.successorsEdge (current);
// We check that we got only one successor
assert (neighbors.size() == 1);
......
......@@ -17,7 +17,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::create (new BankStrings ("AATGC", NULL), "-kmer-size 4 -abundance-min 1 -verbose 0");
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator ();
// We check that we have only two possible nodes
assert (it.size() == 2);
......@@ -37,7 +37,7 @@ int main (int argc, char* argv[])
// It could have been retrieved by an previous call to Graph::neighbors<Edge> method
// Now, we want to get the successor of the current node, only by giving the transition nucleotide.
Node neighbor = graph.successor<Node> (current, NUCL_C);
Node neighbor = graph.successor (current, NUCL_C);
// WARNING ! This Graph::successor method doesn't check whether the neighbor actually belongs to the graph.
// It is supposed here that the client knows perfectly that its transition nucleotide is valid.
......@@ -52,17 +52,17 @@ int main (int argc, char* argv[])
bool exists;
Node potentialNeighbor;
potentialNeighbor = graph.successor<Node> (current, NUCL_A, exists);
potentialNeighbor = graph.successor (current, NUCL_A, exists);
assert (exists == false);
potentialNeighbor = graph.successor<Node> (current, NUCL_C, exists);
potentialNeighbor = graph.successor (current, NUCL_C, exists);
assert (exists == true);
assert (graph.toString (potentialNeighbor) == "ATGC");
potentialNeighbor = graph.successor<Node> (current, NUCL_G, exists);
potentialNeighbor = graph.successor (current, NUCL_G, exists);
assert (exists == false);
potentialNeighbor = graph.successor<Node> (current, NUCL_T, exists);
potentialNeighbor = graph.successor (current, NUCL_T, exists);
assert (exists == false);
}
}
......
......@@ -17,7 +17,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::create (new BankStrings ("AATGC", NULL), "-kmer-size 5 -abundance-min 1 -verbose 0");
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator ();
// We check that we have only one possible node
assert (it.size() == 1);
......
......@@ -23,7 +23,7 @@ int main (int argc, char* argv[])
// We create a Node iterator that iterates all the simple nodes from the first node
// Remember that a simple node has indegree==1 and outdegree==1
Graph::Iterator<Node> path = graph.simplePath<Node> (node, DIR_OUTCOMING);
Graph::Iterator<Node> path = graph.simplePath (node, DIR_OUTCOMING);
// We iterate the simple path.
for (path.first(); !path.isDone(); path.next())
......
......@@ -23,7 +23,7 @@ int main (int argc, char* argv[])
// We create a Edge iterator that iterates all the simple edges from the first node
// Recall that a simple node has indegree==1 and outdegree==1
Graph::Iterator<Edge> path = graph.simplePath<Edge> (node, DIR_OUTCOMING);
Graph::Iterator<Edge> path = graph.simplePathEdge (node, DIR_OUTCOMING);
// We iterate the simple path.
for (path.first(); !path.isDone(); path.next())
......
......@@ -34,7 +34,7 @@ int main (int argc, char* argv[])
Node node = graph.buildNode (sequences[0]);
// We retrieve the branching neighbors for the node.
Graph::Vector<BranchingNode> branchingNeighbors = graph.successors<BranchingNode> (node);
Graph::Vector<BranchingNode> branchingNeighbors = graph.successorsBranching (node);
std::cout << "We found " << branchingNeighbors.size() << " branching neighbors from node " << graph.toString(node) << std::endl;
......
......@@ -34,7 +34,7 @@ int main (int argc, char* argv[])
Node node = graph.buildNode (sequences[0]);
// We retrieve the branching neighbors for the node.
Graph::Vector<BranchingEdge> branchingNeighbors = graph.successors<BranchingEdge> (node);
Graph::Vector<BranchingEdge> branchingNeighbors = graph.successorsBranchingEdge (node);
std::cout << "We found " << branchingNeighbors.size() << " branching neighbors from node " << graph.toString(node) << std::endl;
......
......@@ -36,7 +36,7 @@ int main (int argc, char* argv[])
// We get an iterator for branching nodes of the graph.
// We use a progress iterator to get some progress feedback
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iterator<BranchingNode>(), "statistics");
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iteratorBranching(), "statistics");
// We define some kind of unique identifier for a couple (indegree,outdegree)
typedef pair<size_t,size_t> InOut_t;
......@@ -53,8 +53,8 @@ int main (int argc, char* argv[])
map <InOut_t,size_t>& localTopology = topology();
// We get branching nodes neighbors for the current branching node.
Graph::Vector<BranchingEdge> successors = graph.successors <BranchingEdge> (node);
Graph::Vector<BranchingEdge> predecessors = graph.predecessors<BranchingEdge> (node);
Graph::Vector<BranchingEdge> successors = graph.successorsBranchingEdge ((Node&)node); /* node casting is necessary due to incomplete Graph.hpp api for successorsBranchingEdge */
Graph::Vector<BranchingEdge> predecessors = graph.predecessorsBranchingEdge ((Node&)node);
// We increase the occurrences number for the current couple (in/out) neighbors
localTopology [make_pair(predecessors.size(), successors.size())] ++;
......
......@@ -16,29 +16,29 @@ using namespace std;
// We define a class that marks nodes in a graph and call tell whether a given node is marked or not.
// We use a map for the implementation (could be not optimal).
template<typename T> class GraphMarker
class GraphMarker
{
public:
GraphMarker (const Graph& graph) : graph(graph)
{
// We insert all the nodes into our map.
auto iter = [&] (const T& item) { this->markMap[item] = false; };
graph.iterator<T>().iterate (iter);
auto iter = [&] (const BranchingNode& item) { this->markMap[item] = false; };
graph.iteratorBranching().iterate (iter);
}
void mark (const T& item) { markMap [item] = true; }
void mark (const BranchingNode& item) { markMap [item] = true; }
void mark (const set<T>& items)
void mark (const set<BranchingNode>& items)
{
for (typename set<T>::const_iterator it=items.begin(); it != items.end(); ++it) { mark (*it); }
for (typename set<BranchingNode>::const_iterator it=items.begin(); it != items.end(); ++it) { mark (*it); }
}
bool isMarked (const T& item) const { return markMap.find(item)->second; }
bool isMarked (const BranchingNode& item) const { return markMap.find(item)->second; }
private:
const Graph& graph;
map<T,bool> markMap;
map<BranchingNode,bool> markMap;
};
/********************************************************************************/
......@@ -46,7 +46,7 @@ private:
// We define a class that performs a breadth first search in a graph from a starting node.
// We need a marker to globally mark the visited nodes from one call to another.
// The nodes visit is handled by a std::queue object.
template<typename T> class BFS
class BFS
{
public:
......@@ -54,7 +54,7 @@ public:
// Process the BFS started from the provided node. As a result, we get the nodes number in the
// found connected component.
const set<T>& run (const T& node)
const set<BranchingNode>& run (const BranchingNode& node)
{
// ALGORITHM (recursion on n)
// We define as C(n) the set of nodes of the connected component to be computed.
......@@ -70,7 +70,7 @@ public:
// card(F(n)) = 0
// We set the initial state for F(0)
set<T> frontline;
set<BranchingNode> frontline;
frontline.insert (node);
// We set the initial state for C(0)
......@@ -81,13 +81,13 @@ public:
while (!frontline.empty())
{
// We get the neighbors for the current front line, ie. we get N(F(n))
set<T> neighbors = graph.neighbors<T> (frontline.begin(), frontline.end());
set<BranchingNode> neighbors = graph.neighbors (frontline.begin(), frontline.end()); // this is a very special overload of neighbors() from Graph.hpp
// We reset the current front line => we reuse it for computing F(n+1)
frontline.clear();
// We compute the recursion for F(n+1) and C(n+1)
for (typename set<T>::iterator it = neighbors.begin(); it != neighbors.end(); it++)
for (typename set<BranchingNode>::iterator it = neighbors.begin(); it != neighbors.end(); it++)
{
if (connectedComponent.find (*it) == connectedComponent.end())
{
......@@ -105,11 +105,11 @@ public:
}
// We provide an accessor to the nodes of the found connected component
const set<T>& get() const { return connectedComponent; }
const set<BranchingNode>& get() const { return connectedComponent; }
private:
const Graph& graph;
set<T> connectedComponent;
set<BranchingNode> connectedComponent;
};
/********************************************************************************/
......@@ -134,10 +134,10 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (options->getStr(STR_URI_GRAPH));
// We create a graph marker.
GraphMarker<BranchingNode> marker (graph);
GraphMarker marker (graph);
// We create an object for Breadth First Search for the de Bruijn graph.
BFS<BranchingNode> bfs (graph);
BFS bfs (graph);
// We want to compute the distribution of connected components of the branching nodes.
// - key is a connected component class (for a given number of branching nodes for this component)
......@@ -145,7 +145,7 @@ int main (int argc, char* argv[])
map<size_t,size_t> distrib;
// We get an iterator for branching nodes of the graph. We use a progress iterator to get some progress feedback
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iterator<BranchingNode>(), "statistics");
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iteratorBranching(), "statistics");
// We want to know the number of connected components
size_t nbConnectedComponents = 0;
......
......@@ -20,26 +20,26 @@ using namespace std;
// We define a class that marks nodes in a graph and call tell whether a given node is marked or not.
// We use a map for the implementation (could be not optimal).
template<typename T> class GraphMarker
class GraphMarker
{
public:
GraphMarker (const Graph& graph) : graph(graph)
{
// We insert all the nodes into our map.
auto iter = [&] (const T& item) { this->markMap[item] = false; };
graph.iterator<T>().iterate (iter);
auto iter = [&] (const BranchingNode& item) { this->markMap[item] = false; };
graph.iteratorBranching().iterate (iter);
}
void mark (const T& item) { markMap [item] = true; }
void mark (const BranchingNode& item) { markMap [item] = true; }
void mark (const set<T>& items) { for (typename set<T>::const_iterator it=items.begin(); it != items.end(); ++it) { mark (*it); } }
void mark (const set<BranchingNode>& items) { for (typename set<BranchingNode>::const_iterator it=items.begin(); it != items.end(); ++it) { mark (*it); } }
bool isMarked (const T& item) const { return markMap.find(item)->second; }
bool isMarked (const BranchingNode& item) const { return markMap.find(item)->second; }
private:
const Graph& graph;
map<T,bool> markMap;
map<BranchingNode,bool> markMap;
};
/********************************************************************************/
......@@ -47,7 +47,7 @@ private:
// We define a class that performs a breadth first search in a graph from a starting node.
// We need a marker to globally mark the visited nodes from one call to another.
// The nodes visit is handled by a std::queue object.
template<typename T> class BFS
class BFS
{
public:
......@@ -55,7 +55,7 @@ public:
// Process the BFS started from the provided node. As a result, we get the nodes number in the
// found connected component.
const set<T>& run (const T& node)
const set<BranchingNode>& run (const BranchingNode& node)
{
// ALGORITHM (recursion on n)
// We define as C(n) the set of nodes of the connected component to be computed.
......@@ -71,7 +71,7 @@ public:
// card(F(n)) = 0
// We set the initial state for F(0)
set<T> frontline;
set<BranchingNode> frontline;
frontline.insert (node);
// We set the initial state for C(0)
......@@ -82,13 +82,13 @@ public:
while (!frontline.empty())
{
// We get the neighbors for the current front line, ie. we get N(F(n))
set<T> neighbors = graph.neighbors<T> (frontline.begin(), frontline.end());
set<BranchingNode> neighbors = graph.neighbors (frontline.begin(), frontline.end());
// We reset the current front line => we reuse it for computing F(n+1)
frontline.clear();
// We compute the recursion for F(n+1) and C(n+1)
for (typename set<T>::iterator it = neighbors.begin(); it != neighbors.end(); it++)
for (typename set<BranchingNode>::iterator it = neighbors.begin(); it != neighbors.end(); it++)
{
if (connectedComponent.find (*it) == connectedComponent.end())
{
......@@ -106,11 +106,11 @@ public:
}
// We provide an accessor to the nodes of the found connected component
const set<T>& get() const { return connectedComponent; }
const set<BranchingNode>& get() const { return connectedComponent; }
private:
const Graph& graph;
set<T> connectedComponent;
set<BranchingNode> connectedComponent;
};
/********************************************************************************/
......@@ -135,10 +135,10 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (options->getStr(STR_URI_GRAPH));
// We create a graph marker.
GraphMarker<BranchingNode> marker (graph);
GraphMarker marker (graph);
// We create an object for Breadth First Search for the de Bruijn graph.
BFS<BranchingNode> bfs (graph);
BFS bfs (graph);
// We want to compute the distribution of connected components of the branching nodes.
// - key is a connected component class (for a given number of branching nodes for this component)
......@@ -146,7 +146,7 @@ int main (int argc, char* argv[])
map<size_t,size_t> distrib;
// We get an iterator for all nodes of the graph. We use a progress iterator to get some progress feedback
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iterator<BranchingNode>(), "statistics");
ProgressGraphIterator<BranchingNode,ProgressTimer> itBranching (graph.iteratorBranching(), "statistics");
// We want time duration of the iteration
TimeInfo ti;
......
......@@ -27,10 +27,10 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (options->getStr(STR_URI_GRAPH));
// We iterate the branching nodes
Dispatcher().iterate (graph.iterator<BranchingNode> (), [&] (const BranchingNode& node)
Dispatcher().iterate (graph.iteratorBranching (), [&] (const BranchingNode& node)
{
// We iterate the successors of the current node
graph.successors<BranchingEdge>(node).iterate ([&] (const BranchingEdge& edge)
graph.successorsBranchingEdge((Node&)node).iterate ([&] (const BranchingEdge& edge)
{
if (edge.from == edge.to) { cout << "CYCLE: " << graph.toString (edge) << endl; }
});
......
......@@ -83,7 +83,8 @@ public:
model.iterate (seq.getData(), [&] (const Kmer<>::ModelCanonical::Kmer& kmer, size_t rank)
{
// We count the branching nodes.
if (graph.isBranching (Node::Value(kmer.value()))) { nbBranching++; }
Node node = Node::Value(kmer.value());
if (graph.isBranching (node)) { nbBranching++; }
});
// We increase the (local) distribution for this number of branching nodes per sequence
......
......@@ -28,32 +28,46 @@ public:
_parser->push_front (new OptionOneParam (STR_NODE_TYPE, "node type (0: all, 1:branching)", false, "1" ));
}
template<typename NodeType>
void process (const char* name)
void processNode(Graph &graph, ofstream &output)
{
string outputFile = getInput()->get(STR_URI_OUTPUT) ?
getInput()->getStr(STR_URI_OUTPUT) :
(System::file().getBaseName(getInput()->getStr(STR_URI_GRAPH)) + ".dot");
map<Node, u_int64_t> mapping;
u_int64_t count = 0;
Graph::Iterator<Node> itMap = graph.iterator ();
for (itMap.first(); !itMap.isDone(); itMap.next()) { mapping[itMap.item()] = count++; }
ofstream output (outputFile.c_str());
ProgressGraphIterator<Node,ProgressTimer> it = graph.iterator ();
for (it.first(); !it.isDone(); it.next())
{
Node current = it.item();
output << "digraph " << name << "{\n";
Graph::Vector<Node> neighbors = graph.neighbors(current.kmer);
// We load the graph
Graph graph = Graph::load (getInput()->getStr(STR_URI_GRAPH));
for (size_t i=0; i<neighbors.size(); i++)
{
output << mapping[current.kmer] << " -> " << mapping[neighbors[i].kmer] << " ;\n";
}
}
output << "}\n";
output.close();
}
void processBranchingNode(Graph &graph, ofstream & output)
{
map<Node, u_int64_t> mapping;
u_int64_t count = 0;
Graph::Iterator<NodeType> itMap = graph.iterator<NodeType> ();
Graph::Iterator<BranchingNode> itMap = graph.iteratorBranching();
for (itMap.first(); !itMap.isDone(); itMap.next()) { mapping[itMap.item()] = count++; }
ProgressGraphIterator<NodeType,ProgressTimer> it = graph.iterator<NodeType> ();
ProgressGraphIterator<BranchingNode,ProgressTimer> it = graph.iteratorBranching ();
for (it.first(); !it.isDone(); it.next())
{
NodeType current = it.item();
BranchingNode current = it.item();
Graph::Vector<NodeType> neighbors = graph.neighbors<NodeType> (current.kmer);
Graph::Vector<BranchingNode> neighbors = graph.neighborsBranching (current.kmer);
for (size_t i=0; i<neighbors.size(); i++)
{
......@@ -66,13 +80,32 @@ public:
output.close();
}
// Actual job done by the tool is here
void execute ()
{
string outputFile = getInput()->get(STR_URI_OUTPUT) ?
getInput()->getStr(STR_URI_OUTPUT) :
(System::file().getBaseName(getInput()->getStr(STR_URI_GRAPH)) + ".dot");
ofstream output (outputFile.c_str());
// We load the graph
Graph graph = Graph::load (getInput()->getStr(STR_URI_GRAPH));
switch (getInput()->getInt(STR_NODE_TYPE))
{
case 0: process<Node> ("all"); break;
case 1: process<BranchingNode> ("branching"); break;
case 0:
output << "digraph " << "all" << "{\n";
processNode(graph, output);
break;
case 1:
output << "digraph " << "branching" << "{\n";
processBranchingNode(graph, output);
break;
default: break;
}
}
......
......@@ -21,7 +21,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (argv[1]);
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator ();
// We loop each node. Note the structure of the for loop.
for (it.first(); !it.isDone(); it.next())
......
......@@ -25,7 +25,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (argv[1]);
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator ();
// We choose how many cores we want to use.
// By convention, a 0 value will use all available cores.
......
......@@ -21,7 +21,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::load (argv[1]);
// We get an iterator for branching nodes of the graph.
Graph::Iterator<BranchingNode> it = graph.iterator<BranchingNode> ();
Graph::Iterator<BranchingNode> it = graph.iteratorBranching ();
// We loop each node. Note the structure of the for loop.
for (it.first(); !it.isDone(); it.next())
......
......@@ -17,7 +17,7 @@ int main (int argc, char* argv[])
Graph graph = Graph::create (new BankStrings ("AATGC", NULL), "-kmer-size 4 -abundance-min 1 -verbose 0");
// We get an iterator for all nodes of the graph.
Graph::Iterator<Node> it = graph.iterator<Node> ();
Graph::Iterator<Node> it = graph.iterator();
// We check that we have only two possible nodes
assert (it.size() == 2);
......@@ -37,7 +37,7 @@ int main (int argc, char* argv[])
if (s=="AATG")
{
// We get the neighbors of this specific current
Graph::Vector<Node> neighbors = graph.successors<Node> (current);
Graph::Vector<Node> neighbors = graph.successors(current);
// We check that we got only one successor
assert (neighbors.size() == 1);
......
......@@ -38,7 +38,7 @@ int main (int argc, char* argv[])
Path pathLeft;
// We get one branching node in the graph
Graph::Iterator<BranchingNode> itBranching = graph.iterator<BranchingNode> ();
Graph::Iterator<BranchingNode> itBranching = graph.iteratorBranching ();
for (itBranching.first(); !itBranching.isDone(); itBranching.next())
{
BranchingNode current = itBranching.item();
......@@ -49,7 +49,8 @@ int main (int argc, char* argv[])
//if (terminator.is_marked_branching (itBranching.item())) { continue; }
int lenRight = traversal->traverse (current, DIR_OUTCOMING, pathRight);
int lenLeft = traversal->traverse (graph.reverse(current), DIR_OUTCOMING, pathLeft);
Node rev = graph.reverse(current);
int lenLeft = traversal->traverse (rev, DIR_OUTCOMING, pathLeft);
cout << "lenLeft=" << lenLeft << " lenRight=" << lenRight << endl;
}
......
......@@ -47,7 +47,7 @@ int main (int argc, char* argv[])
size_t kmerSize = atol (storage->root().getProperty("kmer_size").c_str());
// We get a minimizer model
Model model (kmerSize, 8, typename Kmer<>::ComparatorMinimizerFrequency(), repart.getMinimizerFrequencies());
Model model (kmerSize, 8, typename Kmer<>::ComparatorMinimizerFrequencyOrLex(), repart.getMinimizerFrequencies());
// We create the output file
string outputUri = options->get(STR_URI_OUTPUT) ?
......
......@@ -20,7 +20,7 @@ public:
Graph graph = Graph::create (getParser()->getProperties());
// We get an iterator for all nodes of the graph. We use a progress iterator to get some progress feedback
ProgressGraphIterator<BranchingNode,ProgressTimer> it (graph.iterator<BranchingNode>(), "MiniDiscoSNP: finding bubbles ");
ProgressGraphIterator<BranchingNode,ProgressTimer> it (graph.iteratorBranching(), "MiniDiscoSNP: finding bubbles ");
int nbsnps = 1 ;
int ksize = graph.getKmerSize();
......@@ -40,7 +40,7 @@ public:
if ((indegree ==1 && outdegree==2) || (indegree ==2 && outdegree==1) )
{
//get neighbor branching edges
Graph::Vector<BranchingEdge> branchingNeighbors = graph.successors<BranchingEdge> (current);
Graph::Vector<BranchingEdge> branchingNeighbors = graph.successorsBranchingEdge (current);
//clean bubble
if(branchingNeighbors.size()==2 && branchingNeighbors[0].distance == ksize && branchingNeighbors[1].distance == ksize)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment