Zip and Skip Tries
Loading...
Searching...
No Matches
Classes | Enumerations | Functions | Variables
genetics Namespace Reference

Contains utilities for working with genetic data and file formats. More...

Classes

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

Enumerations

enum class  FileType {
  GBK , FNA , DNA , DNA_BIN ,
  DNA_BIN_COMBINED , UNKNOWN
}
 Enumerates the supported genetic file formats. More...
 

Functions

Gene from_gbk (const std::string &filename)
 Loads a gene from a GenBank format file.
 
Gene from_fna (const std::string &filename)
 Loads a gene from a FASTA nucleotide format file.
 
Gene from_dna (const std::string &filename)
 Loads a gene from a plain text DNA format file.
 
Gene from_file (const std::string &filename)
 Loads a gene from a file with automatic format detection.
 
Gene from_file (const std::filesystem::path &path)
 Loads a gene from a file path with automatic format detection.
 
Gene from_file (const std::filesystem::directory_entry &entry)
 Loads a gene from a directory entry with automatic format detection.
 
void to_dna (const Gene &gene, const std::string &filename)
 Saves a gene to a plain text DNA format file.
 
std::vector< Genefrom_directory (const std::string &dirname)
 Loads all genes from files in a directory with automatic format detection.
 
std::vector< Genefrom_directory (const std::string &dirname, FileType type)
 Loads all genes from files of a specific format in a directory.
 
std::vector< std::string > get_dna_strings_from_directory (const std::string &dirname)
 Extracts DNA strings from all supported files in a directory.
 
FileType get_file_type (const std::string &filename)
 Determines the file type based on its filename extension.
 
FileType get_file_type (const std::filesystem::path &path)
 Determines the file type based on its path's extension.
 
FileType get_file_type (const std::filesystem::directory_entry &entry)
 Determines the file type based on a directory entry's path extension.
 
Gene from_stream (std::istream &stream)
 Loads a gene from an input stream.
 
std::vector< std::filesystem::path > get_virus_nucleotide_paths ()
 Gets paths to all virus nucleotide files in the virus directory.
 
void combine_dna_bin_files (const std::string &dirname, const std::string &combined_filename)
 Combines multiple binary DNA files into a single combined file.
 

Variables

static const std::string GENETIC_DIRECTORY = "genetic/"
 Path to the genetic data directory.
 
static const std::string VIRUS_DIRECTORY = GENETIC_DIRECTORY + "virus/"
 Path to the virus genome data directory.
 
static const std::string ABC_HUMI_DIRECTORY = GENETIC_DIRECTORY + "ABC-HuMi/"
 Path to the ABC-HuMi dataset directory.
 
static const std::string DNA_EXTENSION = ".dna"
 File extension for plain text DNA files.
 
static const std::string DNA_BIN_EXTENSION = ".dna.bin"
 File extension for binary DNA files.
 
static const std::string DNA_BIN_COMBINED_EXTENSION = ".dna.bin.combined"
 File extension for combined binary DNA files.
 
static const std::string DNA_BIN_SIZES_EXTENSION = ".dna.bin.sizes"
 File extension for binary DNA file size information.
 
static const std::string GBK_EXTENSION = ".gbk"
 File extension for GenBank format files.
 
static const std::string FNA_EXTENSION = ".fna"
 File extension for FASTA nucleotide format files.
 

Detailed Description

Contains utilities for working with genetic data and file formats.

Provides constants, enums, and functions for loading, saving, and manipulating genetic data from various file formats. The namespace encapsulates all functionality related to genetic sequence processing.

Enumeration Type Documentation

◆ FileType

Enumerates the supported genetic file formats.

This enumeration identifies the different file formats that can be processed by the genetics utility functions. Each format has specific parsing rules implemented in the corresponding from_* functions.

Enumerator
GBK 

GenBank format (.gbk)

FNA 

FASTA nucleotide format (.fna)

DNA 

Plain text DNA format (.dna)

DNA_BIN 

Binary DNA format (.dna.bin)

DNA_BIN_COMBINED 

Combined binary DNA format (.dna.bin.combined)

UNKNOWN 

Unknown or unsupported file format.

Definition at line 73 of file genetics.cuh.

74 {
75 GBK,
76 FNA,
77 DNA,
78 DNA_BIN,
80 UNKNOWN
81 };
@ DNA
Plain text DNA format (.dna)
@ UNKNOWN
Unknown or unsupported file format.
@ FNA
FASTA nucleotide format (.fna)
@ DNA_BIN_COMBINED
Combined binary DNA format (.dna.bin.combined)
@ DNA_BIN
Binary DNA format (.dna.bin)
@ GBK
GenBank format (.gbk)

Function Documentation

◆ combine_dna_bin_files()

void genetics::combine_dna_bin_files ( const std::string &  dirname,
const std::string &  combined_filename 
)

Combines multiple binary DNA files into a single combined file.

Creates a combined binary file and a separate size file that tracks the size of each original file for later extraction.

Parameters
dirnameDirectory containing binary DNA files to combine.
combined_filenameName of the output combined file.

Definition at line 335 of file genetics.cu.

336{
337 // Phase 1: Collect paths of all DNA_BIN files in the directory
339 timer.start("Adding filepaths to vector");
340 std::vector<std::filesystem::path> filepaths;
341 for (const auto& entry : std::filesystem::directory_iterator(dirname))
342 {
343 if (get_file_type(entry) == FileType::DNA_BIN)
344 {
345 filepaths.emplace_back(entry.path());
346 }
347 }
348 timer.print();
349
350 printf("Added %lu filepaths to vector\n", filepaths.size());
351
352 // Phase 2: Create the combined file header with file count, names, and placeholders for positions
353 timer.start("Writing names to binary file");
354 std::ofstream file(dirname + combined_filename + DNA_BIN_COMBINED_EXTENSION, std::ios::binary);
355 if (!file.is_open())
356 {
357 throw std::invalid_argument("File not found");
358 }
359
360 // Write the number of files first for easy parsing later
361 size_t num_names = filepaths.size();
362 file.write(reinterpret_cast<const char*>(&num_names), sizeof(num_names));
363
364 // For each file, write its name and reserve space for its position in the file
365 std::vector<std::streampos> streampos_positions;
366 for (const auto& filepath : filepaths)
367 {
368 // Extract the base filename without the .dna.bin extension
369 auto name = filepath.filename().string();
370 name = name.substr(0, name.size() - DNA_BIN_EXTENSION.size());
371
372 // Write the name with null terminator
373 file.write(name.c_str(), name.size());
374 file.write("\0", 1);
375
376 // Store the current position to update later with the actual gene data position
377 streampos_positions.push_back(file.tellp());
378
379 // Write a placeholder for the position (will update later)
380 file.write(reinterpret_cast<const char*>(&streampos_positions.back()), sizeof(streampos_positions.back()));
381 }
382
383 timer.print();
384
385 // Phase 3: Write each gene to the file and update its position in the header
386 timer.start();
387 for (unsigned i = 0; i < filepaths.size(); ++i)
388 {
389 // Remember current position where gene data will start
390 auto pos = file.tellp();
391
392 // Go back to the position placeholder in the header and update it
393 file.seekp(streampos_positions[i]);
394 file.write(reinterpret_cast<const char*>(&pos), sizeof(pos));
395
396 // Return to the end of the file to write the gene data
397 file.seekp(pos);
398
399 // Load and write the gene
400 auto gene = Gene::from_file(filepaths[i].string());
401 gene.to_file(file);
402
403 // Progress reporting for long operations
404 if (i % 1000 == 0)
405 {
406 printf("Wrote %u files, took %s\n", i, pretty_print(timer.elapsed_nanoseconds()).c_str());
407 }
408 }
409}
static BitString from_file(const std::string &filename)
Reads a BitString from a file in binary format.
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
std::string pretty_print(size_t nanoseconds)
Converts a duration in nanoseconds to a human-readable string format.
Definition data.cpp:65
static const std::string DNA_BIN_COMBINED_EXTENSION
File extension for combined binary DNA files.
Definition genetics.cuh:55
FileType get_file_type(const std::string &filename)
Determines the file type based on its filename extension.
Definition genetics.cu:256
static const std::string DNA_BIN_EXTENSION
File extension for binary DNA files.
Definition genetics.cuh:52
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

◆ from_directory() [1/2]

std::vector< Gene > genetics::from_directory ( const std::string &  dirname)

Loads all genes from files in a directory with automatic format detection.

Implementation of from_directory function that loads genes from all files in a directory.

Parameters
dirnamePath to the directory containing genetic data files.
Returns
std::vector<Gene> Vector of parsed genetic sequences.
See also
from_directory(const std::string&, FileType)

Iterates through all files in the specified directory and attempts to load each file as a genetic sequence using automatic format detection. This version handles all supported file types.

Parameters
dirnamePath to the directory containing genetic data files.
Returns
std::vector<Gene> Vector of parsed genetic sequences.

Definition at line 183 of file genetics.cu.

184{
185 std::vector<Gene> genes;
186
187 for (const auto& entry : std::filesystem::directory_iterator(dirname))
188 {
189 genes.emplace_back(from_file(entry));
190 }
191
192 return genes;
193}
Gene from_file(const std::string &filename)
Loads a gene from a file with automatic format detection.
Definition genetics.cu:284

◆ from_directory() [2/2]

std::vector< Gene > genetics::from_directory ( const std::string &  dirname,
FileType  type 
)

Loads all genes from files of a specific format in a directory.

Parameters
dirnamePath to the directory containing genetic data files.
typeThe specific file type to load.
Returns
std::vector<Gene> Vector of parsed genetic sequences.
See also
from_file

Definition at line 195 of file genetics.cu.

196{
197 std::vector<Gene> genes;
198
199 // Iterate through directory and filter by the requested file type
200 for (const auto& entry : std::filesystem::directory_iterator(dirname))
201 {
202 // Only process files matching the specified type
203 if (get_file_type(entry) == type)
204 {
205 genes.emplace_back(from_file(entry));
206 }
207 }
208
209 return genes;
210}

◆ from_dna()

Gene genetics::from_dna ( const std::string &  filename)

Loads a gene from a plain text DNA format file.

Implementation of from_dna function for loading genes from DNA format files.

Parameters
filenamePath to the DNA (.dna) file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.
See also
from_file

Opens a DNA format file and passes the file stream to from_stream to extract the nucleotide sequence. DNA format files contain only nucleotide characters.

Parameters
filenamePath to the DNA format file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.

Definition at line 152 of file genetics.cu.

153{
154 std::ifstream file(filename);
155 if (!file.is_open())
156 {
157 throw std::invalid_argument("File not found");
158 }
159
160 Gene gene;
161
162 std::string line;
163 while (std::getline(file, line))
164 {
165 for (char c : line)
166 {
168 }
169 }
170
171 return gene;
172}
void push_back(CHAR_T c) noexcept
Appends a single character to the end of the BitString.
static constexpr std::array< Nucleotide, 256 > char_to_nucleotide
Lookup table for converting characters to nucleotides.

◆ from_file() [1/3]

Gene genetics::from_file ( const std::filesystem::directory_entry &  entry)

Loads a gene from a directory entry with automatic format detection.

Parameters
entryA filesystem directory entry pointing to the genetic data file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened or has an unsupported format.
See also
from_file(const std::filesystem::path&)

Definition at line 318 of file genetics.cu.

319{
320 return from_file(entry.path().string());
321}

◆ from_file() [2/3]

Gene genetics::from_file ( const std::filesystem::path &  path)

Loads a gene from a file path with automatic format detection.

Parameters
pathA filesystem path to the genetic data file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened or has an unsupported format.
See also
from_file(const std::string&)

Definition at line 313 of file genetics.cu.

314{
315 return from_file(path.string());
316}

◆ from_file() [3/3]

Gene genetics::from_file ( const std::string &  filename)

Loads a gene from a file with automatic format detection.

Determines the file type based on its extension and calls the appropriate parsing function.

Parameters
filenamePath to the genetic data file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened or has an unsupported format.
See also
get_file_type

Definition at line 284 of file genetics.cu.

285{
287
288 switch (type)
289 {
290 case FileType::GBK:
291 return from_gbk(filename);
292 case FileType::FNA:
293 return from_fna(filename);
294 case FileType::DNA:
295 return from_dna(filename);
296 case FileType::DNA_BIN:
298 default:
299 throw std::invalid_argument("Unknown file type");
300 }
301}
Gene from_gbk(const std::string &filename)
Loads a gene from a GenBank format file.
Definition genetics.cu:71
Gene from_dna(const std::string &filename)
Loads a gene from a plain text DNA format file.
Definition genetics.cu:152
Gene from_fna(const std::string &filename)
Loads a gene from a FASTA nucleotide format file.
Definition genetics.cu:107
FileType
Enumerates the supported genetic file formats.
Definition genetics.cuh:74

◆ from_fna()

Gene genetics::from_fna ( const std::string &  filename)

Loads a gene from a FASTA nucleotide format file.

Implementation of from_fna function for parsing FASTA nucleotide format files.

Parameters
filenamePath to the FASTA nucleotide (.fna) file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.
See also
from_file

FASTA files start with a header line (beginning with '>') followed by the nucleotide sequence. This function skips the first line (header) and then passes the file stream to from_stream to extract the actual nucleotide sequence.

Parameters
filenamePath to the FASTA nucleotide file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.

Definition at line 107 of file genetics.cu.

108{
109 std::ifstream file(filename);
110 if (!file.is_open())
111 {
112 throw std::invalid_argument("File not found");
113 }
114
115 // step 1: skip header (just one line)
116
117 std::string line;
118 std::getline(file, line);
119
120 return from_stream(file);
121}
Gene from_stream(std::istream &stream)
Loads a gene from an input stream.
Definition genetics.cu:36

◆ from_gbk()

Gene genetics::from_gbk ( const std::string &  filename)

Loads a gene from a GenBank format file.

Implementation of from_gbk function for parsing GenBank format files.

Parameters
filenamePath to the GenBank (.gbk) file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.
See also
from_file

GenBank files have a specific format with a header section followed by a section that begins with "ORIGIN" followed by the nucleotide sequence. This function skips the header section and then passes the file stream to from_stream to extract the actual nucleotide sequence.

Parameters
filenamePath to the GenBank file.
Returns
Gene The parsed genetic sequence.
Exceptions
std::invalid_argumentif the file cannot be opened.

Definition at line 71 of file genetics.cu.

72{
73 std::ifstream file(filename);
74 if (!file.is_open())
75 {
76 throw std::invalid_argument("File not found");
77 }
78
79 // step 1: skip header
80 // keep reading until the line just read is "ORIGIN"
81
82 std::string line;
83 while (std::getline(file, line))
84 {
85 if (line == "ORIGIN")
86 {
87 break;
88 }
89 }
90
91 // step 2: read nucleotides
92 // read line by line, character by character, discarding any non-nucleotide characters
93
94 return from_stream(file);
95}

◆ from_stream()

Gene genetics::from_stream ( std::istream &  stream)

Loads a gene from an input stream.

Implementation of from_stream function that parses genetic sequences from a stream.

Reads valid nucleotides from the stream until EOF, skipping invalid characters.

Parameters
streamThe input stream to read from.
Returns
Gene The parsed genetic sequence.

Reads the stream line by line, extracting valid nucleotides and adding them to the gene sequence. Invalid characters are skipped. Aborts if a FASTA header marker ('>') is encountered, as this indicates incorrect parsing logic.

Parameters
streamInput stream containing genetic data.
Returns
Gene The parsed genetic sequence.

Definition at line 36 of file genetics.cu.

37{
38 Gene gene;
39
40 std::string line;
41 while (std::getline(stream, line))
42 {
43 for (char c : line)
44 {
46 {
48 }
49 if (c == '>')
50 {
51 printf("NOT GOOD\n");
52 exit(0);
53 }
54 }
55 }
56
57 return gene;
58}
bool is_valid_nucleotide(char nucleotide)
Checks if a character represents a valid nucleotide.

◆ get_dna_strings_from_directory()

std::vector< std::string > genetics::get_dna_strings_from_directory ( const std::string &  dirname)

Extracts DNA strings from all supported files in a directory.

Parameters
dirnamePath to the directory containing genetic data files.
Returns
std::vector<std::string> Vector of DNA strings.

Definition at line 212 of file genetics.cu.

213{
214 std::vector<std::string> dna_strings;
215
216 // Iterate through all files in the directory
217 for (const auto& entry : std::filesystem::directory_iterator(dirname))
218 {
219 // Process only DNA format files
220 if (get_file_type(entry) == FileType::DNA)
221 {
222 std::ifstream file(entry.path());
223 if (!file.is_open())
224 {
225 throw std::invalid_argument("File not found");
226 }
227
228 // Read just the first line from each file
229 // Note: This assumes DNA files contain a single line with the entire sequence
230 std::string dna_string;
231 std::getline(file, dna_string);
232 dna_strings.emplace_back(dna_string);
233 }
234 }
235
236 return dna_strings;
237}

◆ get_file_type() [1/3]

FileType genetics::get_file_type ( const std::filesystem::directory_entry &  entry)

Determines the file type based on a directory entry's path extension.

Parameters
entryA filesystem directory entry to analyze.
Returns
FileType The determined file type, or FileType::UNKNOWN if not recognized.
See also
get_file_type(const std::filesystem::path&)

Definition at line 308 of file genetics.cu.

309{
310 return get_file_type(entry.path().string());
311}

◆ get_file_type() [2/3]

FileType genetics::get_file_type ( const std::filesystem::path &  path)

Determines the file type based on its path's extension.

Parameters
pathA filesystem path to analyze.
Returns
FileType The determined file type, or FileType::UNKNOWN if not recognized.
See also
get_file_type(const std::string&)

Definition at line 303 of file genetics.cu.

304{
305 return get_file_type(path.string());
306}

◆ get_file_type() [3/3]

FileType genetics::get_file_type ( const std::string &  filename)

Determines the file type based on its filename extension.

Parameters
filenameThe filename to analyze.
Returns
FileType The determined file type, or FileType::UNKNOWN if not recognized.

Definition at line 256 of file genetics.cu.

257{
259 {
260 return FileType::GBK;
261 }
263 {
264 return FileType::FNA;
265 }
267 {
268 return FileType::DNA;
269 }
271 {
272 return FileType::DNA_BIN;
273 }
275 {
276 return FileType::DNA_BIN_COMBINED;
277 }
278 else
279 {
280 return FileType::UNKNOWN;
281 }
282}
static const std::string GBK_EXTENSION
File extension for GenBank format files.
Definition genetics.cuh:61
static const std::string DNA_EXTENSION
File extension for plain text DNA files.
Definition genetics.cuh:49
static const std::string FNA_EXTENSION
File extension for FASTA nucleotide format files.
Definition genetics.cuh:64

◆ get_virus_nucleotide_paths()

std::vector< std::filesystem::path > genetics::get_virus_nucleotide_paths ( )

Gets paths to all virus nucleotide files in the virus directory.

Returns
std::vector<std::filesystem::path> Vector of paths to virus nucleotide files.

Definition at line 323 of file genetics.cu.

324{
325 std::vector<std::filesystem::path> paths;
326
328 {
329 paths.emplace_back(entry.path());
330 }
331
332 return paths;
333}
static const std::string VIRUS_DIRECTORY
Path to the virus genome data directory.
Definition genetics.cuh:43

◆ to_dna()

void genetics::to_dna ( const Gene gene,
const std::string &  filename 
)

Saves a gene to a plain text DNA format file.

Implementation of to_dna function for saving genes to DNA format files.

Parameters
geneThe genetic sequence to save.
filenamePath where the DNA file will be saved.
Exceptions
std::runtime_errorif the file cannot be created or written to.

Writes the gene to a text file in DNA format, using the stream output operator of the Gene class to convert the gene to a string representation.

Parameters
geneThe genetic sequence to save.
filenamePath where the DNA file will be saved.
Exceptions
std::invalid_argumentif the file cannot be opened for writing.

Definition at line 132 of file genetics.cu.

133{
134 std::ofstream file(filename);
135 if (!file.is_open())
136 {
137 throw std::invalid_argument("File not found");
138 }
139
140 file << gene;
141}

Variable Documentation

◆ ABC_HUMI_DIRECTORY

const std::string genetics::ABC_HUMI_DIRECTORY = GENETIC_DIRECTORY + "ABC-HuMi/"
static

Path to the ABC-HuMi dataset directory.

Definition at line 46 of file genetics.cuh.

◆ DNA_BIN_COMBINED_EXTENSION

const std::string genetics::DNA_BIN_COMBINED_EXTENSION = ".dna.bin.combined"
static

File extension for combined binary DNA files.

Definition at line 55 of file genetics.cuh.

◆ DNA_BIN_EXTENSION

const std::string genetics::DNA_BIN_EXTENSION = ".dna.bin"
static

File extension for binary DNA files.

Definition at line 52 of file genetics.cuh.

◆ DNA_BIN_SIZES_EXTENSION

const std::string genetics::DNA_BIN_SIZES_EXTENSION = ".dna.bin.sizes"
static

File extension for binary DNA file size information.

Definition at line 58 of file genetics.cuh.

◆ DNA_EXTENSION

const std::string genetics::DNA_EXTENSION = ".dna"
static

File extension for plain text DNA files.

Definition at line 49 of file genetics.cuh.

◆ FNA_EXTENSION

const std::string genetics::FNA_EXTENSION = ".fna"
static

File extension for FASTA nucleotide format files.

Definition at line 64 of file genetics.cuh.

◆ GBK_EXTENSION

const std::string genetics::GBK_EXTENSION = ".gbk"
static

File extension for GenBank format files.

Definition at line 61 of file genetics.cuh.

◆ GENETIC_DIRECTORY

const std::string genetics::GENETIC_DIRECTORY = "genetic/"
static

Path to the genetic data directory.

Definition at line 40 of file genetics.cuh.

◆ VIRUS_DIRECTORY

const std::string genetics::VIRUS_DIRECTORY = GENETIC_DIRECTORY + "virus/"
static

Path to the virus genome data directory.

Definition at line 43 of file genetics.cuh.