| 1 |
#ifndef PBCOPPER_POA_POAGRAPHIMPL_H |
| 2 |
#define PBCOPPER_POA_POAGRAPHIMPL_H |
| 3 |
|
| 4 |
#include <pbcopper/PbcopperConfig.h> |
| 5 |
|
| 6 |
#include <pbcopper/align/AlignConfig.h> |
| 7 |
#include <pbcopper/poa/PoaGraph.h> |
| 8 |
#include "BoostGraph.h" |
| 9 |
#include "PoaAlignmentMatrix.h" |
| 10 |
|
| 11 |
#include <boost/config.hpp> |
| 12 |
#include <boost/format.hpp> |
| 13 |
#include <boost/type_traits.hpp> |
| 14 |
|
| 15 |
#include <filesystem> |
| 16 |
#include <string> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include <cfloat> |
| 20 |
#include <climits> |
| 21 |
#include <cstddef> |
| 22 |
|
| 23 |
/// |
| 24 |
/// Boost graph library typedefs, properties, and graphviz output. |
| 25 |
/// |
| 26 |
|
| 27 |
namespace PacBio { |
| 28 |
namespace Poa { |
| 29 |
namespace detail { |
| 30 |
|
| 31 |
// FWD |
| 32 |
class SdpRangeFinder; |
| 33 |
|
| 34 |
class PoaNode |
| 35 |
{ |
| 36 |
public: |
| 37 |
PoaGraph::Vertex Id; |
| 38 |
char Base; |
| 39 |
int Reads; |
| 40 |
// move the below out of here? |
| 41 |
int SpanningReads; |
| 42 |
float Score; |
| 43 |
float ReachingScore; |
| 44 |
|
| 45 |
PoaNode() : PoaNode(0, 'N', 0, 0) {} |
| 46 |
explicit PoaNode(size_t id, char base) : PoaNode(id, base, 1, 0) {} |
| 47 |
explicit PoaNode(size_t id, char base, int reads) : PoaNode(id, base, reads, 0) {} |
| 48 |
explicit PoaNode(size_t id, char base, int reads, int spanning) |
| 49 |
: Id{id}, Base{base}, Reads{reads}, SpanningReads{spanning}, Score{0}, ReachingScore{0} |
| 50 |
{} |
| 51 |
}; |
| 52 |
|
| 53 |
// External-facing vertex id type |
| 54 |
using Vertex = size_t; |
| 55 |
|
| 56 |
struct EdgeComparator |
| 57 |
{ |
| 58 |
public: |
| 59 |
explicit EdgeComparator(const BoostGraph& g) : g_(g) {} |
| 60 |
|
| 61 |
// want lex. comparison... just using pair to get it.. |
| 62 |
bool operator()(ED e1, ED e2) const noexcept |
| 63 |
{ |
| 64 |
std::pair<int, int> vt1; |
| 65 |
std::pair<int, int> vt2; |
| 66 |
vt1 = std::make_pair(get(boost::vertex_index, g_, source(e1, g_)), |
| 67 |
get(boost::vertex_index, g_, target(e1, g_))); |
| 68 |
vt2 = std::make_pair(get(boost::vertex_index, g_, source(e2, g_)), |
| 69 |
get(boost::vertex_index, g_, target(e2, g_))); |
| 70 |
return (vt1 < vt2); |
| 71 |
} |
| 72 |
|
| 73 |
private: |
| 74 |
const BoostGraph& g_; |
| 75 |
}; |
| 76 |
|
| 77 |
inline std::vector<ED> inEdges(VD v, const BoostGraph& g) |
| 78 |
{ |
| 79 |
// This is a sad workaround the nondeterministic order of iteration |
| 80 |
// from BGL's in_edges. (see: http://stackoverflow.com/questions/30968690/) |
| 81 |
|
| 82 |
// Unfortunately, we can't just use the boost::sort range adapter |
| 83 |
// because it requires an underlying random access iterator, which |
| 84 |
// we can't get from the std::set container. |
| 85 |
boost::graph_traits<BoostGraph>::in_edge_iterator begin; |
| 86 |
boost::graph_traits<BoostGraph>::in_edge_iterator end; |
| 87 |
std::tie(begin, end) = in_edges(v, g); |
| 88 |
std::vector<ED> tmp(begin, end); |
| 89 |
std::sort(tmp.begin(), tmp.end(), EdgeComparator(g)); |
| 90 |
return tmp; |
| 91 |
} |
| 92 |
|
| 93 |
class PoaGraphImpl |
| 94 |
{ |
| 95 |
friend class SdpRangeFinder; |
| 96 |
|
| 97 |
BoostGraph g_; |
| 98 |
VertexInfoMap vertexInfoMap_; // NB: this is a reference type and refers to |
| 99 |
// an "internal" property map |
| 100 |
index_map_t indexMap_; |
| 101 |
VD enterVertex_; |
| 102 |
VD exitVertex_; |
| 103 |
size_t numReads_; |
| 104 |
size_t totalVertices_; // includes "ex"-vertices which have since been removed |
| 105 |
size_t liveVertices_; // vertices that are in the graph. this is needed |
| 106 |
// for algorithms. |
| 107 |
std::map<Vertex, VD> vertexLookup_; // external ID -> internal ID |
| 108 |
|
| 109 |
void repCheck() const; |
| 110 |
|
| 111 |
VD addVertex(char base, int nReads = 1, int spanningReads = 0) |
| 112 |
{ |
| 113 |
VD vd = add_vertex(g_); |
| 114 |
Vertex vExt = totalVertices_; |
| 115 |
++totalVertices_; |
| 116 |
vertexInfoMap_[vd] = PoaNode(vExt, base, nReads, spanningReads); |
| 117 |
vertexLookup_[vExt] = vd; |
| 118 |
indexMap_[vd] = liveVertices_++; |
| 119 |
return vd; |
| 120 |
} |
| 121 |
|
| 122 |
// |
| 123 |
// utility routines |
| 124 |
// |
| 125 |
std::unique_ptr<const AlignmentColumn> makeAlignmentColumn( |
| 126 |
VD v, const AlignmentColumnMap& alignmentColumnForVertex, const std::string& sequence, |
| 127 |
const Align::AlignConfig& config, int beginRow, int endRow) const; |
| 128 |
|
| 129 |
std::unique_ptr<const AlignmentColumn> makeAlignmentColumnForExit( |
| 130 |
VD v, const AlignmentColumnMap& alignmentColumnForVertex, const std::string& sequence, |
| 131 |
const Align::AlignConfig& config) const; |
| 132 |
|
| 133 |
public: |
| 134 |
// |
| 135 |
// Vertex id translation |
| 136 |
// |
| 137 |
|
| 138 |
Vertex externalize(VD vd) const |
| 139 |
{ |
| 140 |
if (vd == null_vertex) { |
| 141 |
return PoaGraph::NullVertex; |
| 142 |
} else { |
| 143 |
return vertexInfoMap_[vd].Id; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
VD internalize(Vertex vertex) const |
| 148 |
{ |
| 149 |
if (vertex == PoaGraph::NullVertex) { |
| 150 |
return null_vertex; |
| 151 |
} |
| 152 |
return vertexLookup_.at(vertex); |
| 153 |
} |
| 154 |
|
| 155 |
std::vector<Vertex> externalizePath(const std::vector<VD>& vds) const |
| 156 |
{ |
| 157 |
std::vector<Vertex> out(vds.size(), 0); |
| 158 |
for (size_t i = 0; i < vds.size(); i++) { |
| 159 |
out[i] = externalize(vds[i]); |
| 160 |
} |
| 161 |
return out; |
| 162 |
} |
| 163 |
|
| 164 |
std::vector<VD> internalizePath(const std::vector<Vertex>& vertices) const |
| 165 |
{ |
| 166 |
std::vector<VD> out(vertices.size(), null_vertex); |
| 167 |
for (size_t i = 0; i < vertices.size(); i++) { |
| 168 |
out[i] = internalize(vertices[i]); |
| 169 |
} |
| 170 |
return out; |
| 171 |
} |
| 172 |
|
| 173 |
// |
| 174 |
// POA node lookup |
| 175 |
// |
| 176 |
const PoaNode& getPoaNode(VD v) const { return vertexInfoMap_[v]; } |
| 177 |
|
| 178 |
// |
| 179 |
// Graph traversal functions, defined in PoaGraphTraversals |
| 180 |
// |
| 181 |
|
| 182 |
std::vector<VD> sortedVertices() const; |
| 183 |
|
| 184 |
void tagSpan(VD start, VD end); |
| 185 |
|
| 186 |
std::vector<VD> consensusPath(Align::AlignMode mode, int minCoverage = -INT_MAX) const; |
| 187 |
|
| 188 |
void threadFirstRead(std::string sequence, std::vector<Vertex>* readPathOutput = NULL); |
| 189 |
|
| 190 |
void tracebackAndThread(std::string sequence, |
| 191 |
const AlignmentColumnMap& alignmentColumnForVertex, |
| 192 |
Align::AlignMode mode, std::vector<Vertex>* readPathOutput = NULL); |
| 193 |
|
| 194 |
PoaGraphImpl(); |
| 195 |
PoaGraphImpl(const PoaGraphImpl& other); |
| 196 |
|
| 197 |
void AddRead(const std::string& sequence, const Align::AlignConfig& config, |
| 198 |
SdpRangeFinder* rangeFinder = NULL, std::vector<Vertex>* readPathOutput = NULL); |
| 199 |
|
| 200 |
void AddFirstRead(const std::string& sequence, std::vector<Vertex>* readPathOutput = NULL); |
| 201 |
|
| 202 |
std::unique_ptr<PoaAlignmentMatrix> TryAddRead(const std::string& sequence, |
| 203 |
const Align::AlignConfig& config, |
| 204 |
SdpRangeFinder* const rangeFinder = NULL) const; |
| 205 |
|
| 206 |
void CommitAdd(const PoaAlignmentMatrix* const mat, std::vector<Vertex>* readPathOutput = NULL); |
| 207 |
|
| 208 |
std::unique_ptr<PoaConsensus> FindConsensus(const Align::AlignConfig& config, |
| 209 |
int minCoverage = -INT_MAX); |
| 210 |
void PruneGraph(const int minCoverage); |
| 211 |
|
| 212 |
size_t NumReads() const; |
| 213 |
std::string ToGraphViz(int flags, const PoaConsensus* pc) const; |
| 214 |
void WriteGraphVizFile(const std::filesystem::path& filename, int flags, |
| 215 |
const PoaConsensus* pc) const; |
| 216 |
void WriteGraphCsvFile(const std::filesystem::path& filename) const; |
| 217 |
}; |
| 218 |
|
| 219 |
// free functions, we should put these all in traversals |
| 220 |
std::string sequenceAlongPath(const BoostGraph& g, const VertexInfoMap& vertexInfoMap, |
| 221 |
const std::vector<VD>& path); |
| 222 |
|
| 223 |
} // namespace detail |
| 224 |
} // namespace Poa |
| 225 |
} // namespace PacBio |
| 226 |
|
| 227 |
#endif // PBCOPPER_POA_POAGRAPHIMPL_H |