def test_empty_encoding_writes_nothing(self): encoding = [] ostream = BytesIO() PrgEncoder.write(encoding, ostream) ostream.seek(0) self.assertEqual(ostream.read(), b"")
def write_prg(output_prefix: str, prg_string: str): """ Writes the prg to outfile. Writes it as a human readable string, and also as an integer vector """ prg_filename = Path(output_prefix + ".prg") with prg_filename.open("w") as prg: regex = re.compile( r"^(?P<sample>.+)\.max_nest(?P<max_nest>\d+)\.min_match(?P<min_match>\d+)" ) match = regex.search(prg_filename.stem) try: sample = match.group("sample") except IndexError: logging.warning( "A sample name couldn't be parsed from the prefix. " "Using 'sample' as sample name." ) sample = "sample" max_nest = int(match.group("max_nest")) min_match = int(match.group("min_match")) header = f"{sample} max_nest={max_nest} min_match={min_match}" print(f">{header}\n{prg_string}", file=prg) prg_ints_fpath = Path(output_prefix + ".bin") prg_encoder = PrgEncoder() prg_ints: PRG_Ints = prg_encoder.encode(prg_string) with prg_ints_fpath.open("wb") as ostream: prg_encoder.write(prg_ints, ostream)
def test_write_single_int(self): prg_ints = [1] write_to = BytesIO() PrgEncoder.write(prg_ints, write_to) write_to.seek(0) actual = write_to.read() expected = to_bytes(1) self.assertEqual(actual, expected)
def test_write_two_ints(self): encoding = [1, 4] write_to = BytesIO() PrgEncoder.write(encoding, write_to) write_to.seek(0) actual = write_to.read() expected = to_bytes(1) + to_bytes(4) self.assertEqual(actual, expected)
def build_from_msas(report, action, build_paths, args): """ For use by gramtools """ log.info(f"Building prg from prgs in {args.prgs_bed}") built_intervals, rescaled_prg_ints = standalone_build_from_msas( args.prgs_bed, args.reference, build_paths.coords_file, build_paths.built_prg_dirname, args.max_threads, ) built_intervals.saveas(build_paths.built_prg_bed) with open(build_paths.prg, "wb") as fhandle_out: PrgEncoder.write(rescaled_prg_ints, fhandle_out)
def test_write_multiple_ints_function_calls(self): num_elems = 100 prg_ints = [random.randint for _ in range(num_elems)] ostream = BytesIO() ostream.write = MagicMock(spec=True) # Check call to write triggers the production of bytes with patch("make_prg.prg_encoder.to_bytes", return_value=b"") as mocked_to_bytes: PrgEncoder.write(prg_ints, ostream) expected_bytes_calls = [call(i) for i in prg_ints] self.assertEqual(expected_bytes_calls, mocked_to_bytes.call_args_list) # Check ostream write operation only called once ostream.write.assert_called_once()
def write_prg(prg_fname: Path, prg_string: str, options: ArgumentParser): """ Writes th prg to `output_file`. Writes it as a human readable string, and also as an integer vector """ seqid = options.seqid or options.prg_name if options.output_type.prg: with prg_fname.open("w") as prg: header = f">{seqid} max_nest={options.max_nesting} min_match={options.min_match_length}" print(f"{header}\n{prg_string}", file=prg) if options.output_type.binary: prg_ints_fpath = prg_fname.with_suffix(".bin") prg_encoder = PrgEncoder() prg_ints: PRG_Ints = prg_encoder.encode(prg_string) with prg_ints_fpath.open("wb") as ostream: prg_encoder.write(prg_ints, ostream)
def encode_and_write_prg(self, prg_string: str): prg_encoder = PrgEncoder() prg_ints = prg_encoder.encode(prg_string) with open(self.out_fname, "wb") as fhandle_out: prg_encoder.write(prg_ints, fhandle_out)