|
| 1 | +# Copyright 2021 Google LLC. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# |
| 7 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# 3. Neither the name of the copyright holder nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from this |
| 16 | +# software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | +"""Load the sequneces of a GBZ file into shared memory, usable by multiple processes.""" |
| 30 | + |
| 31 | +from absl import app |
| 32 | +from absl import flags |
| 33 | + |
| 34 | +from deepvariant import logging_level |
| 35 | +from third_party.nucleus.io.python import gbz_reader |
| 36 | +from third_party.nucleus.io.python import hts_verbose |
| 37 | +from third_party.nucleus.util import errors |
| 38 | + |
| 39 | + |
| 40 | +# Flags related to loading GBZ into shared memory. |
| 41 | +_PANGENOME_GBZ = flags.DEFINE_string( |
| 42 | + 'pangenome_gbz', |
| 43 | + None, |
| 44 | + ( |
| 45 | + 'Required. Pangenome GBZ file to load into shared memory' |
| 46 | + '(Only the sequences are loaded into shared memory.)' |
| 47 | + ), |
| 48 | +) |
| 49 | + |
| 50 | +_NUM_SHARDS = flags.DEFINE_integer( |
| 51 | + 'num_shards', |
| 52 | + None, |
| 53 | + ( |
| 54 | + 'Required. Number of shards that will use the shared memory. It is' |
| 55 | + 'important to set this number correctly to make sure that the' |
| 56 | + 'shared memory is not deleted before all processes are done using it.' |
| 57 | + 'If this value is greater than required then the shared memory will' |
| 58 | + 'exist even after all processes are done using it,' |
| 59 | + 'which is not desired.' |
| 60 | + ), |
| 61 | +) |
| 62 | + |
| 63 | +_SHARED_MEMORY_NAME = flags.DEFINE_string( |
| 64 | + 'shared_memory_name', |
| 65 | + 'GBZ_SHARED_MEMORY', |
| 66 | + ( |
| 67 | + 'Name of the shared memory segment.' |
| 68 | + ), |
| 69 | +) |
| 70 | + |
| 71 | +_SHARED_MEMORY_SIZE_GB = flags.DEFINE_integer( |
| 72 | + 'shared_memory_size_gb', |
| 73 | + 10, |
| 74 | + ( |
| 75 | + 'Size of the shared memory in GB.' |
| 76 | + ), |
| 77 | +) |
| 78 | + |
| 79 | + |
| 80 | +def load_gbz_into_shared_memory( |
| 81 | + pangenome_gbz: str, |
| 82 | + shared_memory_name: str, |
| 83 | + shared_memory_size_gb: int, |
| 84 | + num_shards: int, |
| 85 | +): |
| 86 | + """Loads a GBZ file into a shared memory segment.""" |
| 87 | + sample_name = 'GRCh38' |
| 88 | + context = 1000 |
| 89 | + chrom_prefix = '' |
| 90 | + create_shared_memory = True |
| 91 | + num_processes = num_shards |
| 92 | + |
| 93 | + ## The only parameters that are important are: |
| 94 | + ## - shared_memory_name, |
| 95 | + ## - create_shared_memory, |
| 96 | + ## - shared_memory_size_gb, |
| 97 | + ## - num_processes |
| 98 | + ## The rest are set to default values. This is because the shared memory will |
| 99 | + ## keep only the sequences of the pangenome and not the rest of the |
| 100 | + ## information. |
| 101 | + ## Just instantating a gbz reader with create_shared_memory=True |
| 102 | + ## will load the sequences into shared memory. |
| 103 | + ## If num_processes is greater than 0 then the shared memory |
| 104 | + ## will NOT be deleted after this call (to be more precise |
| 105 | + ## after calling ~GbzReader() here). |
| 106 | + gbz_reader.GbzReader( |
| 107 | + pangenome_gbz, |
| 108 | + sample_name, |
| 109 | + context, |
| 110 | + chrom_prefix, |
| 111 | + shared_memory_name, |
| 112 | + create_shared_memory, |
| 113 | + shared_memory_size_gb, |
| 114 | + num_processes |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def main(argv=()): |
| 119 | + with errors.clean_commandline_error_exit(): |
| 120 | + if len(argv) > 1: |
| 121 | + errors.log_and_raise( |
| 122 | + 'Command line parsing failure: load_gbz_into_shared_memory does not' |
| 123 | + 'accept positional arguments but some are present on' |
| 124 | + 'the command line:' |
| 125 | + '"{}".'.format(str(argv)), |
| 126 | + errors.CommandLineError, |
| 127 | + ) |
| 128 | + del argv # Unused. |
| 129 | + |
| 130 | + logging_level.set_from_flag() |
| 131 | + hts_verbose.set(hts_verbose.htsLogLevel.HTS_LOG_WARNING) |
| 132 | + |
| 133 | + load_gbz_into_shared_memory( |
| 134 | + _PANGENOME_GBZ.value, |
| 135 | + _SHARED_MEMORY_NAME.value, |
| 136 | + _SHARED_MEMORY_SIZE_GB.value, |
| 137 | + _NUM_SHARDS.value, |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + flags.mark_flags_as_required([ |
| 143 | + 'pangenome_gbz', |
| 144 | + 'num_shards', |
| 145 | + ]) |
| 146 | + app.run(main) |
0 commit comments