Zip and Skip Tries
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
genetics::GeneManager Struct Reference

Manages efficient loading of multiple genetic sequences from a combined file. More...

Classes

struct  GeneInfo
 Structure to track information about a gene in the combined file. More...
 

Public Member Functions

 GeneManager (const std::string &path)
 Constructs a GeneManager for accessing genes in the specified path.
 
 ~GeneManager ()
 Destructor for GeneManager.
 
std::vector< Geneall_genes () const
 Retrieves all genes from the combined file.
 
std::vector< Geneget_random_genes (size_t count) const
 Retrieves a random subset of genes from the combined file.
 

Private Member Functions

void save_sizes ()
 Saves the size information of genes to a companion file.
 
void load_sizes ()
 Loads gene size information from the companion file.
 

Private Attributes

std::string m_path
 Path to the combined binary file.
 
std::ifstream m_file
 File stream for reading gene data (mutable to allow const methods to read)
 
std::vector< GeneInfom_gene_infos
 Index of all genes in the combined file.
 

Detailed Description

Manages efficient loading of multiple genetic sequences from a combined file.

This class provides utilities for loading genes from a combined binary file created by combine_dna_bin_files(). It maintains an index of genes in the file for fast random access without loading the entire dataset into memory at once.

See also
combine_dna_bin_files

Definition at line 225 of file genetics.cuh.

Constructor & Destructor Documentation

◆ GeneManager()

GeneManager::GeneManager ( const std::string &  path)

Constructs a GeneManager for accessing genes in the specified path.

Parameters
pathPath to the combined binary file containing genetic sequences.
Exceptions
std::runtime_errorIf the file cannot be opened or is improperly formatted.

Definition at line 411 of file genetics.cu.

411 : m_path(path)
412{
413 // Open the combined binary file
414 m_file.open(path + DNA_BIN_COMBINED_EXTENSION, std::ios::binary);
415 if (!m_file.is_open())
416 {
417 throw std::invalid_argument("File not found");
418 }
419
421 timer.start("Loading names and positions");
422
423 // Read the number of genes stored in the file
424 size_t num_names;
425 m_file.read(reinterpret_cast<char*>(&num_names), sizeof(num_names));
426
427 // Reserve space for all gene metadata
428 m_gene_infos.reserve(num_names);
429 // Note: Previous implementation stored names and positions in separate vectors
430 // m_names.reserve(num_names);
431 // m_positions.reserve(num_names);
432
433 // Parse the gene metadata header
434 for (size_t i = 0; i < num_names; ++i)
435 {
436 // Read null-terminated gene name
437 std::string name;
438 char c;
439 while (m_file.read(&c, 1) && c != '\0')
440 {
441 name.push_back(c);
442 }
443
444 // Read the position where this gene's data starts in the file
445 std::streampos position;
446 m_file.read(reinterpret_cast<char*>(&position), sizeof(position));
447
448 // Store the metadata (size will be loaded or determined later)
449 m_gene_infos.push_back({ name, position, 0 });
450 }
451
452 timer.print();
453
454 // Load or create the size information for each gene
455 if (std::filesystem::exists(path + DNA_BIN_SIZES_EXTENSION))
456 {
457 // If size cache already exists, load it
458 load_sizes();
459 }
460 else
461 {
462 // Otherwise, calculate and save sizes
463 save_sizes();
464 }
465
466 // Sort genes by their size for optimized access patterns
467 timer.start("Sorting by size");
468 std::sort(m_gene_infos.begin(), m_gene_infos.end(), [](const GeneInfo& a, const GeneInfo& b) { return a.size < b.size; });
469 timer.print();
470}
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
static const std::string DNA_BIN_COMBINED_EXTENSION
File extension for combined binary DNA files.
Definition genetics.cuh:55
static const std::string DNA_BIN_SIZES_EXTENSION
File extension for binary DNA file size information.
Definition genetics.cuh:58
A timer class measuring wall-clock time using std::chrono::high_resolution_clock.
Definition data.hpp:116
void start(const std::string &prompt="", bool reset=true) noexcept
Starts or resets the timer and optionally prints a starting message.
Definition data.hpp:138
std::string m_path
Path to the combined binary file.
Definition genetics.cuh:266
void load_sizes()
Loads gene size information from the companion file.
Definition genetics.cu:500
std::vector< GeneInfo > m_gene_infos
Index of all genes in the combined file.
Definition genetics.cuh:268
void save_sizes()
Saves the size information of genes to a companion file.
Definition genetics.cu:472
std::ifstream m_file
File stream for reading gene data (mutable to allow const methods to read)
Definition genetics.cuh:267

◆ ~GeneManager()

GeneManager::~GeneManager ( )

Destructor for GeneManager.

Closes any open file streams.

Definition at line 522 of file genetics.cu.

523{
524 m_file.close();
525}

Member Function Documentation

◆ all_genes()

std::vector< Gene > GeneManager::all_genes ( ) const

Retrieves all genes from the combined file.

Returns
std::vector<Gene> A vector containing all genetic sequences from the file.

Definition at line 527 of file genetics.cu.

528{
529 std::vector<Gene> genes;
530 // Iterate through all gene infos and load each gene from the combined file
531 for (const auto& info : m_gene_infos)
532 {
533 // Position file pointer at the start of this gene's data
534 m_file.seekg(info.position);
535
536 // Load the gene from the current file position and add to result vector
537 genes.emplace_back(Gene::from_file(m_file));
538 }
539
540 return genes;
541}
static BitString from_file(const std::string &filename)
Reads a BitString from a file in binary format.

◆ get_random_genes()

std::vector< Gene > GeneManager::get_random_genes ( size_t  count) const

Retrieves a random subset of genes from the combined file.

Parameters
countThe number of random genes to retrieve.
Returns
std::vector<Gene> A vector containing the randomly selected genetic sequences.

Definition at line 543 of file genetics.cu.

544{
545 // Initialize static random number generator for efficient repeated calls
546 static std::random_device rd;
547 static std::mt19937 gen(rd());
548 static std::uniform_int_distribution<size_t> dist(0, m_gene_infos.size() - 1);
549
550 // Use a set to ensure we don't select the same gene twice
551 // Store positions rather than indices to directly access the file
552 std::unordered_set<size_t> positions;
553
554 // Select random genes until we have the requested count
555 while (positions.size() < count)
556 {
557 positions.insert(m_gene_infos[dist(gen)].position);
558 }
559
560 std::vector<Gene> genes;
561
562 // Load each selected gene from its file position
563 for (const auto& position : positions)
564 {
565 // Position file pointer at the gene's data
566 m_file.seekg(position);
567
568 // Load the gene and add to result vector
569 genes.emplace_back(Gene::from_file(m_file));
570 }
571
572 return genes;
573}

◆ load_sizes()

void GeneManager::load_sizes ( )
private

Loads gene size information from the companion file.

Reads the .sizes file to populate the m_gene_infos vector with metadata about each gene in the combined binary file.

Exceptions
std::runtime_errorIf the sizes file cannot be opened or is corrupted.

Definition at line 500 of file genetics.cu.

501{
503 timer.start("Loading sizes");
504
505 // Open the pre-computed sizes file which contains size information for each gene
506 std::ifstream sizes_file(m_path + DNA_BIN_SIZES_EXTENSION, std::ios::binary);
507 if (!sizes_file.is_open())
508 {
509 throw std::invalid_argument("File not found");
510 }
511
512 // Read size for each gene in the same order as the gene infos
513 for (auto& info : m_gene_infos)
514 {
515 // Load the pre-computed size directly into the gene info structure
516 sizes_file.read(reinterpret_cast<char*>(&info.size), sizeof(info.size));
517 }
518
519 timer.print();
520}

◆ save_sizes()

void GeneManager::save_sizes ( )
private

Saves the size information of genes to a companion file.

Creates a .sizes file alongside the combined binary file that stores metadata about each gene for efficient future loading.

Definition at line 472 of file genetics.cu.

473{
475 timer.start("Loading & saving sizes");
476
477 // Create a companion file to store size information for quick loading in future
478 std::ofstream sizes_file(m_path + DNA_BIN_SIZES_EXTENSION, std::ios::binary);
479 if (!sizes_file.is_open())
480 {
481 throw std::invalid_argument("File not found");
482 }
483
484 // For each gene, read its size from the combined file and save to the sizes file
485 for (auto& info : m_gene_infos)
486 {
487 // Navigate to where this gene's data begins
488 m_file.seekg(info.position);
489
490 // Read the size value from the gene data header
491 m_file.read(reinterpret_cast<char*>(&info.size), sizeof(info.size));
492
493 // Write the size to the companion file for future quick loading
494 sizes_file.write(reinterpret_cast<const char*>(&info.size), sizeof(info.size));
495 }
496
497 timer.print();
498}

Member Data Documentation

◆ m_file

std::ifstream genetics::GeneManager::m_file
mutableprivate

File stream for reading gene data (mutable to allow const methods to read)

Definition at line 267 of file genetics.cuh.

◆ m_gene_infos

std::vector<GeneInfo> genetics::GeneManager::m_gene_infos
private

Index of all genes in the combined file.

Definition at line 268 of file genetics.cuh.

◆ m_path

std::string genetics::GeneManager::m_path
private

Path to the combined binary file.

Definition at line 266 of file genetics.cuh.


The documentation for this struct was generated from the following files: