Nvfaidx
NvFaidx
NvFaidx is a rest + pyo3 replacement for PyFaidx that provides a dictionary-like interface to reference genomes.
This class is a collection of SequenceAccessors, organized by sequence-id in a dictionary like manner. SequenceAcecessors are similar dict-like interfaces over actual sequence entries in the underlying index. Furthermore, utilities are provided for parsing faidx files, building faidx files, and storing faidx files to disk.
IMPORTANT by default all fasta files build an in-memory faidx object. This is due easy mistakes that may occur if a faidx file is constructed while using multi-processing (such as a default constructor that creates these files on the fly). However, methods exist to create these methods manually where a user has more control over multiprocessing.
Examples:
>>> index = NvFaidx(fasta_file, faidx_path=None, ignore_existing_fai=True)
>>> index['chr1'] # Returns a SequenceAccessor for chr1
>>> index['chr1'][0:10] # Returns the first 10 bases of chr1.
>>> faidx_filename = NvFaidx.create_faidx(fasta_file) # Creates a faidx to disk.
>>> index = NvFaidx(fasta_File, faidx_filename, ignore_existing_fai = True) # Uses a faidx from disk.
Motivation and more details:
NvFaidx is built using Noodles as a backend for Fai objects, and memory maps for backing the underlying fasta. Using a backend of Memmaps provide the following benefits: - The kernel implements this mechanism by using page faults - Each read in a mmap'd file results in a page fault: there's nothing in memory to read! - The kernel handles this page fault by going to the disk, reading the file in the specified offset + index, then returning to the user process with what it just read, preventing penalties from context switching.
Context: PyFaidx or any buffered read based index is not process safe, and therefore does not play nice with pytorch dataloaders.
Due to the order of operations, the underlying file handle is shared between processes, when seek()
is called to perform random lookups,
this can cause unexpected behavior in the forked processes.
Ref: https://github.com/mdshw5/pyfaidx/issues/211
For a good solution we need three things
1) Safe index creation, in multi-process or multi-node scenarios, this should be restricted to a single node where all workers block until it is complete (not implemented above) 2) Index object instantion must be fast. 3) Read-only use of the index object must be both thread safe and process safe with python.
See Also: bionemo.noodles.nvfaidx.SequenceAccessor
Source code in bionemo/noodles/nvfaidx.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
__init__(fasta_path, faidx_path=None, ignore_existing_fai=True, allow_duplicate_seqids=False)
Construct a dict-like object representing a memmapped, indexed FASTA file.
This is an indexed fasta reader. Consequences of this are that the FASTA file must be well formed, meaning sequence-ids and line-lengths must conform to FASTA standards. Additionally, the order of returned seqid, sequence pairs when iterating over the index is not guaranteed to be the same order as the underlying fasta file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fasta_path
|
str
|
Path to the FASTA file. |
required |
faidx_path
|
str
|
Path to the FAI index file. If None, one will be created. |
None
|
ignore_existing_fai
|
bool
|
If True, ignore any existing FAI file and create an in-memory index. Note that
this will also ignore |
True
|
allow_duplicate_seqids
|
bool
|
If true, will produce index for invalid fastas which contain duplicate seqids. In this scenario, indexing is performed by integer rather than strings. Example with invalid seqids. >chr1 dupes|not|allowd ATGATGATGATG >chr1 whoops|there|is|dupe ATGATGATGATG NvFaidx: { 0 : SequenceAccessor(chr1 dupes|not|allowed), 1 : SequenceAccessor(chr1 whoops|there|is|dupe) } |
False
|
Source code in bionemo/noodles/nvfaidx.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
create_faidx(fasta_filename, force=False)
staticmethod
Create a FAI index for a FASTA file, the result is saved in the same location as fasta_filename
, with a .fai extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fasta_filename
|
str
|
Path to the FASTA file to be indexed. |
required |
force
|
bool
|
Delete existing faidx file and create a new index file. |
False
|
Source code in bionemo/noodles/nvfaidx.py
243 244 245 246 247 248 249 250 251 252 253 |
|
SequenceAccessor
SequenceAccessor provides a dictionary-like interface to a single sequence in an indexed FASTA file.
This allows for random access to the sequence, either by index, or by slice.
Source code in bionemo/noodles/nvfaidx.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
__init__(reader, seqid, length)
Construct a SequenceAccessor object. Ultimately this is used as a convenience object with NvFaidx.
Additionally there are convenience methods that you may find useful in the class definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reader
|
PyIndexedMmapFastaReader
|
The indexed reader object that provides access to the underlying FASTA file. |
required |
seqid
|
str
|
The sequence identifier. |
required |
length
|
int
|
The length of the sequence. |
required |
Source code in bionemo/noodles/nvfaidx.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
sequence()
Returns the sequence associated with this SequenceAccessor as a string.
Source code in bionemo/noodles/nvfaidx.py
103 104 105 |
|
sequence_id()
Returns the sequenceid of this SequenceAccessor.
Source code in bionemo/noodles/nvfaidx.py
99 100 101 |
|