2020# ' @param adjacency_method character. The method to use for computing the adjacency matrix. Options are "loop", "indexed", direct or beta
2121# ' @param isChild_method character. The method to use for computing the isChild matrix. Options are "classic" or "partialparent"
2222# ' @param adjBeta_method numeric The method to use for computing the building the adjacency_method matrix when using the "beta" build
23+ # ' @param compress logical. If TRUE, use compression when saving the checkpoint files. Defaults to TRUE.
2324# ' @param ... additional arguments to be passed to \code{\link{ped2com}}
2425# ' @details The algorithms and methodologies used in this function are further discussed and exemplified in the vignette titled "examplePedigreeFunctions". For more advanced scenarios and detailed explanations, consult this vignette.
2526# ' @export
@@ -42,6 +43,7 @@ ped2com <- function(ped, component,
4243update_rate = 100 ,
4344save_path = " checkpoint/" ,
4445adjBeta_method = NULL ,
46+ compress = TRUE ,
4547... ) {
4648# ------
4749# Check inputs
@@ -66,7 +68,8 @@ ped2com <- function(ped, component,
6668gc = gc ,
6769component = component ,
6870adjBeta_method = adjBeta_method ,
69- nr = nrow(ped )
71+ nr = nrow(ped ),
72+ compress = compress
7073 )
7174
7275
@@ -186,7 +189,8 @@ ped2com <- function(ped, component,
186189isChild <- .loadOrComputeIsChild(
187190ped = ped ,
188191checkpoint_files = checkpoint_files ,
189- config = config
192+ config = config ,
193+ compress = config $ compress
190194 )
191195# --- Step 2: Compute Relatedness Matrix ---
192196
@@ -228,11 +232,11 @@ ped2com <- function(ped, component,
228232 }
229233# Save progress every save_rate iterations
230234if (config $ saveable == TRUE && (count %% save_rate_gen == 0 )) {
231- saveRDS(r ,file = checkpoint_files $ r_checkpoint )
232- saveRDS(gen ,file = checkpoint_files $ gen_checkpoint )
233- saveRDS(newIsPar ,file = checkpoint_files $ newIsPar_checkpoint )
234- saveRDS(mtSum ,file = checkpoint_files $ mtSum_checkpoint )
235- saveRDS(count ,file = checkpoint_files $ count_checkpoint )
235+ saveRDS(r ,file = checkpoint_files $ r_checkpoint , compress = config $ compress )
236+ saveRDS(gen ,file = checkpoint_files $ gen_checkpoint , compress = config $ compress )
237+ saveRDS(newIsPar ,file = checkpoint_files $ newIsPar_checkpoint , compress = config $ compress )
238+ saveRDS(mtSum ,file = checkpoint_files $ mtSum_checkpoint , compress = config $ compress )
239+ saveRDS(count ,file = checkpoint_files $ count_checkpoint , compress = config $ compress )
236240 }
237241if (config $ gc == TRUE && config $ nr > 1000000 ) {
238242 gc()
@@ -245,7 +249,7 @@ ped2com <- function(ped, component,
245249 gc()
246250 }
247251if (config $ saveable == TRUE ) {
248- saveRDS(r ,file = checkpoint_files $ ram_checkpoint )
252+ saveRDS(r ,file = checkpoint_files $ ram_checkpoint , compress = config $ compress )
249253 }
250254
251255if (config $ component == " generation" ) {# no need to do the rest
@@ -261,22 +265,26 @@ ped2com <- function(ped, component,
261265r = r ,
262266isChild = isChild ,
263267checkpoint_files = checkpoint_files ,
264- config = config
268+ config = config ,
269+ compress = config $ compress
265270 )
266271
267272# --- Step 4: T crossproduct ---
268273
269274if (config $ resume == TRUE && file.exists(checkpoint_files $ tcrossprod_checkpoint )&&
270275config $ component != " generation" ) {
271- if (config $ verbose == TRUE )cat (" Resuming: Loading tcrossprod...\n " )
276+ if (config $ verbose == TRUE )message (" Resuming: Loading tcrossprod...\n " )
272277r <- readRDS(checkpoint_files $ tcrossprod_checkpoint )
273278 }else {
274279r <- .computeTranspose(
275280r2 = r2 ,transpose_method = transpose_method ,
276281verbose = config $ verbose
277282 )
278283if (config $ saveable == TRUE ) {
279- saveRDS(r ,file = checkpoint_files $ tcrossprod_checkpoint )
284+ saveRDS(r ,
285+ file = checkpoint_files $ tcrossprod_checkpoint ,
286+ compress = config $ compress
287+ )
280288 }
281289 }
282290
@@ -293,7 +301,7 @@ ped2com <- function(ped, component,
293301 diag(r )<- 1
294302 }
295303if (config $ saveable == TRUE ) {
296- saveRDS(r ,file = checkpoint_files $ final_matrix )
304+ saveRDS(r ,file = checkpoint_files $ final_matrix , compress = config $ compress )
297305 }
298306return (r )
299307}
@@ -314,6 +322,7 @@ ped2add <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
314322save_rate_gen = save_rate ,
315323save_rate_parlist = 100000 * save_rate ,
316324save_path = " checkpoint/" ,
325+ compress = TRUE ,
317326... ) {
318327 ped2com(
319328ped = ped ,
@@ -331,6 +340,7 @@ ped2add <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
331340save_rate_gen = save_rate_gen ,
332341save_rate_parlist = save_rate_parlist ,
333342save_path = save_path ,
343+ compress = compress ,
334344...
335345 )
336346}
@@ -354,6 +364,7 @@ ped2mit <- ped2mt <- function(ped, max_gen = 25,
354364save_rate_gen = save_rate ,
355365save_rate_parlist = 100000 * save_rate ,
356366save_path = " checkpoint/" ,
367+ compress = TRUE ,
357368... ) {
358369 ped2com(
359370ped = ped ,
@@ -371,6 +382,7 @@ ped2mit <- ped2mt <- function(ped, max_gen = 25,
371382save_rate_gen = save_rate_gen ,
372383save_rate_parlist = save_rate_parlist ,
373384save_path = save_path ,
385+ compress = compress ,
374386...
375387 )
376388}
@@ -391,6 +403,7 @@ ped2cn <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
391403save_rate_gen = save_rate ,
392404save_rate_parlist = 1000 * save_rate ,
393405save_path = " checkpoint/" ,
406+ compress = TRUE ,
394407... ) {
395408 ped2com(
396409ped = ped ,
@@ -408,6 +421,7 @@ ped2cn <- function(ped, max_gen = 25, sparse = TRUE, verbose = FALSE,
408421save_rate_gen = save_rate_gen ,
409422save_rate_parlist = save_rate_parlist ,
410423save_path = save_path ,
424+ compress = compress ,
411425...
412426 )
413427}
@@ -534,18 +548,20 @@ initializeCheckpoint <- function(config = list(
534548# ' @param config A list containing configuration parameters such as `resume`, `verbose`, and `saveable`.
535549# ' @param message_resume Optional message to display when resuming from a checkpoint.
536550# ' @param message_compute Optional message to display when computing the checkpoint.
551+ # ' @param compress a logical specifying whether saving to a named file is to use "gzip" compression, or one of "gzip", "bzip2", "xz" or "zstd" to indicate the type of compression to be used. Ignored if file is a connection.
537552# ' @return The loaded or computed checkpoint.
538553# ' @keywords internal
539554loadOrComputeCheckpoint <- function (file ,compute_fn ,
540555config ,message_resume = NULL ,
541- message_compute = NULL ) {
556+ message_compute = NULL ,
557+ compress = TRUE ) {
542558if (config $ resume == TRUE && file.exists(file )) {
543559if (config $ verbose == TRUE && ! is.null(message_resume )) cat(message_resume )
544560return (readRDS(file ))
545561 }else {
546562if (config $ verbose == TRUE && ! is.null(message_compute )) cat(message_compute )
547563result <- compute_fn()
548- if (config $ saveable == TRUE ) saveRDS(result ,file = file )
564+ if (config $ saveable == TRUE ) saveRDS(result ,file = file , compress = compress )
549565return (result )
550566 }
551567}
@@ -561,7 +577,8 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
561577# '
562578# ' @keywords internal
563579# ' @importFrom Matrix sparseMatrix
564- .loadOrComputeIsPar <- function (iss ,jss ,parVal ,ped ,checkpoint_files ,config ) {
580+ .loadOrComputeIsPar <- function (iss ,jss ,parVal ,ped ,checkpoint_files ,config ,
581+ compress = TRUE ) {
565582isPar <- loadOrComputeCheckpoint(
566583file = checkpoint_files $ isPar ,
567584compute_fn = function () {
@@ -573,7 +590,8 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
573590 },
574591config = config ,
575592message_resume = " Resuming: Loading adjacency matrix...\n " ,
576- message_compute = " Initializing adjacency matrix...\n "
593+ message_compute = " Initializing adjacency matrix...\n " ,
594+ compress = compress
577595 )
578596
579597return (isPar )
@@ -586,13 +604,14 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
586604# '
587605# ' @keywords internal
588606
589- .loadOrComputeIsChild <- function (ped ,checkpoint_files ,config ) {
607+ .loadOrComputeIsChild <- function (ped ,checkpoint_files ,config , compress = TRUE ) {
590608isChild <- loadOrComputeCheckpoint(
591609file = checkpoint_files $ isChild ,
592610compute_fn = function () isChild(isChild_method = config $ isChild_method ,ped = ped ),
593611config = config ,
594612message_resume = " Resuming: Loading isChild matrix...\n " ,
595- message_compute = " Computing isChild matrix...\n "
613+ message_compute = " Computing isChild matrix...\n " ,
614+ compress = compress
596615 )
597616
598617return (isChild )
@@ -608,15 +627,17 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
608627
609628
610629.loadOrComputeInverseDiagonal <- function (r ,isChild ,
611- checkpoint_files ,config ) {
630+ checkpoint_files ,config ,
631+ compress = TRUE ) {
612632r2 <- loadOrComputeCheckpoint(
613633file = checkpoint_files $ r2_checkpoint ,
614634compute_fn = function () {
615635r %*% Matrix :: Diagonal(x = sqrt(isChild ),n = config $ nr )
616636 },
617637config = config ,
618638message_resume = " Resuming: Loading I-A inverse...\n " ,
619- message_compute = " Doing I-A inverse times diagonal multiplication\n "
639+ message_compute = " Doing I-A inverse times diagonal multiplication\n " ,
640+ compress = compress
620641 )
621642if (config $ gc == TRUE ) {
622643 rm(r ,isChild )
@@ -642,7 +663,8 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
642663
643664.loadOrComputeParList <- function (checkpoint_files ,config ,
644665ped = NULL ,
645- parList = NULL ,lens = NULL ) {
666+ parList = NULL ,lens = NULL ,
667+ compress = TRUE ) {
646668if (config $ resume == TRUE &&
647669 file.exists(checkpoint_files $ parList )&&
648670 file.exists(checkpoint_files $ lens )) {
@@ -700,8 +722,8 @@ loadOrComputeCheckpoint <- function(file, compute_fn,
700722 message(" Constructed sparse matrix\n " )
701723 }
702724if (config $ saveable == TRUE ) {
703- saveRDS(list_of_adjacencies $ jss ,file = checkpoint_files $ jss )
704- saveRDS(list_of_adjacencies $ iss ,file = checkpoint_files $ iss )
725+ saveRDS(list_of_adjacencies $ jss ,file = checkpoint_files $ jss , compress = compress )
726+ saveRDS(list_of_adjacencies $ iss ,file = checkpoint_files $ iss , compress = compress )
705727 }
706728 }
707729return (list_of_adjacencies )