RefgetStore
Example: Full RefgetStore Usage
library(gtars)
temp_dir <- tempdir()
# 1. Prepare a dummy FASTA filetemp_fasta_path <- file.path(temp_dir, "source.fa")fasta_content <- paste( ">chr1", "ATGCATGCATGCAGTCGTAGC", ">chr2", "GGGGAAAA", sep = "\n")writeLines(fasta_content, temp_fasta_path)
# 2. Digest the FASTA to get collection info and digestcollection <- digest_fasta(temp_fasta_path)collection_digest <- collection@digestcat(sprintf("Source FASTA digested. Collection digest: %s\n", collection_digest))
# 3. Create an in-memory RefgetStore in Encoded modestore <- refget_store("encoded")print(store)
# 4. Add the FASTA to the storeadded <- add_fasta(store, temp_fasta_path)cat(sprintf("FASTA added to the store as collection %s\n", added$digest))
# 5. Get a sequence by its digest (from the first sequence in the collection)seq_digest_chr1 <- collection[1]@metadata@sha512t24urecord_chr1 <- get_sequence(store, seq_digest_chr1)if (!is.null(record_chr1)) { cat(sprintf("Retrieved sequence by digest: %s, length %s\n", record_chr1@metadata@name, record_chr1@metadata@length)) cat(sprintf(" Sequence (full): %s\n", get_substring(store, seq_digest_chr1, 0, record_chr1@metadata@length)))}
# Or look it up by collection and namerecord_chr2 <- get_sequence_by_name(store, collection_digest, "chr2")
# 6. Get a substring (0-based, half-open)sub_seq <- get_substring(store, seq_digest_chr1, 5, 15)cat(sprintf("Substring from chr1[5:15]: %s\n", sub_seq))
# 7. Prepare a BED file for region retrievaltemp_bed_path <- file.path(temp_dir, "test.bed")bed_content <- paste( "chr1\t0\t10", "chr2\t2\t6", "chr_nonexistent\t0\t5", sep = "\n")writeLines(bed_content, temp_bed_path)
# 8. Retrieve sequences from the BED file into memory.# Regions on unknown sequences (chr_nonexistent) are skipped with a warning.retrieved_list <- get_seqs_bed_file_to_vec(store, collection_digest, temp_bed_path)cat("Retrieved sequences from BED file (as list):\n")for (rs in retrieved_list) { print(rs)}
# The same, as a data.frame with columns sequence, chrom_name, start, endretrieved_df <- get_seqs_bed_file_to_df(store, collection_digest, temp_bed_path)
# 9. Write BED regions to a new FASTA file.# Unlike step 8, this stops with an error on unknown sequences,# so give it a BED file with valid regions only.# Headers look like >chr1:0-10; pass label = "{seq}" for sequence digests.valid_bed_path <- file.path(temp_dir, "valid.bed")writeLines(paste("chr1\t0\t10", "chr2\t2\t6", sep = "\n"), valid_bed_path)temp_output_fa_path <- file.path(temp_dir, "output.fa")get_seqs_bed_file(store, collection_digest, valid_bed_path, temp_output_fa_path)cat(sprintf("Retrieved sequences from BED file written to: %s\n", temp_output_fa_path))
# 10. Export the whole collection (or selected sequences) to FASTA.# The default label is the sequence digest (SQ....); "{name}" writes names.export_fasta(store, file.path(temp_dir, "all.fa"), collections = collection_digest, label = "{name}")export_fasta(store, file.path(temp_dir, "chr1.fa"), collections = collection_digest, names = c("chr1"), label = "{name}", line_width = 60)
# 11. Write store to a new directory.# In the template, %s2 is the first 2 characters of the digest and %s the full digest.temp_saved_store_path <- file.path(temp_dir, "my_refget_store")write_store_to_directory(store, temp_saved_store_path, "sequences/%s2/%s.seq")cat(sprintf("Store saved to: %s\n", temp_saved_store_path))
# 12. Open the store from the directorystore_load <- refget_store_open_local(temp_saved_store_path)cat(sprintf("Store successfully loaded from: %s\n", temp_saved_store_path))print(store_load)A saved store can also be served over HTTP and opened remotely with
refget_store_open_remote(cache_path, remote_url). The server must support HTTP
range requests; see the Python reference for details.
Packed layout (alpha)
A standard on-disk store writes one .seq file per sequence, which means
hundreds of thousands of files for a large store. The packed layout writes
sequences into a few large pack files under packs/ instead, and finds each
sequence through an index file, sequences.pack.idx.
packed <- refget_store_on_disk(file.path(temp_dir, "packed_store"), packed = TRUE)add_fasta(packed, temp_fasta_path)
# Removing collections leaves dead bytes in the packs; compact() reclaims themcompact(packed) # list(packs_before, packs_after, bytes_reclaimed, packs_pruned)compact(store, prune_unreferenced = TRUE) also deletes pack files that no
index entry points to, such as packs left by a writer that crashed. Use it only
when no other process is writing to the store: a pack another writer has sealed
but not yet committed looks the same, and would be deleted too.
A store's layout is fixed when it is created. Opening an existing standard
store with packed = TRUE is an error; refget_store_open_local opens either
kind.
A standard store refuses an import that would leave it with more than 500,000
.seq files. To go past that, use the packed layout, or raise the limit with
add_fasta(store, path, standalone_file_limit = ...). force = TRUE does not
skip the limit.
Reads from a packed store can go through a block cache, which is off by
default. set_pack_cache(store, 128) turns it on with 128 MB; 0 turns it
off. It helps clustered reads (for example a sorted BED) on a cold network
file system, and hurts scattered reads on stores larger than the cache.
The packed layout is alpha: its on-disk format may change before it is declared stable.