Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only reconstruct "Up edges" when necessary #5

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading