Skip to content

Commit

Permalink
Only reconstruct "Up edges" when necessary
Browse files Browse the repository at this point in the history
The API has a flag that loads them by default, but users can
choose. All of the calculations we have implemented only need up
edges if you are doing sample-based subset queries.
  • Loading branch information
dcdehaas committed Jul 22, 2024
1 parent e0bb4e1 commit c3f39f7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/grgl/grg.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ using ConstMutableGRGPtr = std::shared_ptr<const MutableGRG>;

class CSRGRG : public GRG {
public:
explicit CSRGRG(size_t numSamples, size_t edgeCount, size_t nodeCount, uint16_t ploidy)
explicit CSRGRG(size_t numSamples, size_t edgeCount, size_t nodeCount, uint16_t ploidy, bool loadUpEdges = true)
: GRG(numSamples, ploidy),
m_downEdges(edgeCount),
m_upEdges(edgeCount),
m_upEdges(loadUpEdges ? edgeCount : 0),
m_downPositions(nodeCount + 2),
m_upPositions(nodeCount + 2),
m_nodeData(nodeCount) {}
Expand Down
6 changes: 3 additions & 3 deletions src/grg_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ static inline MutableGRGPtr loadMutableGRG(const std::string& filename) {
return result;
}

static inline GRGPtr loadImmutableGRG(const std::string& filename) {
static inline GRGPtr loadImmutableGRG(const std::string& filename, bool loadUpEdges = true) {
GRGPtr result;
std::ifstream inStream(filename, std::ios::binary);
if (!inStream.good()) {
std::cerr << "Could not read " << filename << std::endl;
return result;
}
try {
result = readImmutableGrg(inStream);
result = readImmutableGrg(inStream, loadUpEdges);
} catch (SerializationFailure& e) {
std::cerr << "Failed to load GRG: " << e.what() << std::endl;
return result;
Expand Down Expand Up @@ -249,4 +249,4 @@ inline MutableGRGPtr grgFromTrees(const std::string& filename, bool binaryMutati

}

#endif /* GRG_HELPERS_H */
#endif /* GRG_HELPERS_H */
4 changes: 3 additions & 1 deletion src/grgp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ int main(int argc, char** argv) {
grgl::GRGPtr theGRG;
START_TIMING_OPERATION();
if (ends_with(*infile, ".grg")) {
theGRG = grgl::loadImmutableGRG(*infile);
// We only need up edges if we're calculating subsets via samples.
const bool loadUpEdges = (bool)sampleSubset;
theGRG = grgl::loadImmutableGRG(*infile, loadUpEdges);
if (!theGRG) {
std::cerr << "Failed to load " << *infile << std::endl;
return 2;
Expand Down
2 changes: 1 addition & 1 deletion src/python/_grgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ PYBIND11_MODULE(_grgl, m) {
:rtype: pygrgl.MutableGRG
)^");

m.def("load_immutable_grg", &grgl::loadImmutableGRG, py::arg("filename"), R"^(
m.def("load_immutable_grg", &grgl::loadImmutableGRG, py::arg("filename"), py::arg("load_up_edges") = true, R"^(
Load a GRG file from disk. Immutable GRGs are much faster to traverse than mutable
GRGs and take up less RAM, so this is the preferred method if you are using a GRG
for calculation or annotation, and not modifying the graph structure itself.
Expand Down
3 changes: 2 additions & 1 deletion src/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ GRGPtr readImmutableGrg(std::istream& inStream, bool loadUpEdges) {
assert_deserialization(header.ploidy != 0, "Malformed GRG file: ploidy was 0");

// Construct GRG and allocate all the nodes.
CSRGRGPtr grg = std::make_shared<CSRGRG>(header.sampleCount, header.edgeCount, header.nodeCount, header.ploidy);
CSRGRGPtr grg =
std::make_shared<CSRGRG>(header.sampleCount, header.edgeCount, header.nodeCount, header.ploidy, loadUpEdges);

// The GRG serialization only encodes the down edges, we deserialize them here.
for (size_t i = 0; i < header.edgeCount; i++) {
Expand Down

0 comments on commit c3f39f7

Please sign in to comment.