How to use the tsinfer.exceptions.FileFormatError function in tsinfer

To help you get started, we’ve selected a few tsinfer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tskit-dev / tsinfer / tests / test_cli.py View on Github external
def test_list_unknown_files(self):
        zero_file = os.path.join(self.tempdir.name, "zeros")
        with open(zero_file, "wb") as f:
            f.write(bytearray(100))
        for bad_file in [zero_file]:
            self.assertRaises(
                exceptions.FileFormatError, self.run_command, ["list", bad_file]
            )
        if sys.platform == "win32":
            # Windows raises a PermissionError not IsADirectoryError when opening a dir
            self.assertRaises(PermissionError, self.run_command, ["list", "/"])
        else:
            self.assertRaises(IsADirectoryError, self.run_command, ["list", "/"])
github tskit-dev / tsinfer / tests / test_formats.py View on Github external
def test_load(self):
        self.assertRaises(
            FileNotFoundError, self.tested_class.load, "/file/does/not/exist"
        )
        if sys.platform != "win32":
            self.assertRaises(IsADirectoryError, self.tested_class.load, "/")
            bad_format_files = ["LICENSE", "/dev/urandom"]
        else:
            # Windows raises a PermissionError not IsADirectoryError when opening a dir
            self.assertRaises(PermissionError, self.tested_class.load, "/")
            # No /dev/urandom on Windows
            bad_format_files = ["LICENSE"]
        for bad_format_file in bad_format_files:
            self.assertTrue(os.path.exists(bad_format_file))
            self.assertRaises(
                exceptions.FileFormatError, self.tested_class.load, bad_format_file
            )
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
def _check_format(self):
        try:
            format_name = self.format_name
            format_version = self.format_version
        except KeyError:
            raise exceptions.FileFormatError("Incorrect file format")
        if format_name != self.FORMAT_NAME:
            raise exceptions.FileFormatError(
                "Incorrect file format: expected '{}' got '{}'".format(
                    self.FORMAT_NAME, format_name
                )
            )
        if format_version[0] < self.FORMAT_VERSION[0]:
            raise exceptions.FileFormatError(
                "Format version {} too old. Current version = {}".format(
                    format_version, self.FORMAT_VERSION
                )
            )
        if format_version[0] > self.FORMAT_VERSION[0]:
            raise exceptions.FileFormatError(
                "Format version {} too new. Current version = {}".format(
                    format_version, self.FORMAT_VERSION
                )
github mcveanlab / treeseq-inference / scripts / GDF5_subsampling_ts_general.py View on Github external
subprocess.call(["python3", tsinfer_executable, "--version"])

if 'run_old_tsinfer' in tsinfer_executable:
    import dbm
    #pre-release tsinfer versions don't have sensible version numbers set
    #and have to be called with SampleData.initialise
    #monkey patch so that old versions of tsinfer (which use add_variant not add_site) work.
    #This can be deleted unless you want to use old pre-release (e.g. Feb 2018) versions of tsinfer
    tsinfer.SampleData.add_site = lambda a,b,c: tsinfer.SampleData.add_variant(a,c,b)
    tsinfer.SampleData.sites_position = tsinfer.SampleData.position
    NoSuchTsinferFileError = dbm.error
    def make_sample_data(path, num_samples):
        return tsinfer.SampleData.initialise(
            filename=path, sequence_length=34025983, num_samples = num_samples)
else:
    NoSuchTsinferFileError = tsinfer.exceptions.FileFormatError
    def make_sample_data(path, num_samples):
        return tsinfer.SampleData(path=path)



def treestring(name, tree):
    return "TREE " + name + " = [&R] " + tree.newick(precision=14)[:-1] + ":0;\n"

def header(n_tips, node_labels):
    #convert back to 0-based tip labels, or use string node labels (escaped with single quotes)
    tip_map = [
        str(i+1) + " " + ("'{}'".format(node_labels[i].replace("'","''")) if i in node_labels else str(i))
        for i in range(n_tips)]
    return "#NEXUS\nBEGIN TREES;\nTRANSLATE\n{};\n".format(",\n".join(tip_map))
        
def footer():
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
def _check_format(self):
        try:
            format_name = self.format_name
            format_version = self.format_version
        except KeyError:
            raise exceptions.FileFormatError("Incorrect file format")
        if format_name != self.FORMAT_NAME:
            raise exceptions.FileFormatError(
                "Incorrect file format: expected '{}' got '{}'".format(
                    self.FORMAT_NAME, format_name
                )
            )
        if format_version[0] < self.FORMAT_VERSION[0]:
            raise exceptions.FileFormatError(
                "Format version {} too old. Current version = {}".format(
                    format_version, self.FORMAT_VERSION
                )
            )
        if format_version[0] > self.FORMAT_VERSION[0]:
            raise exceptions.FileFormatError(
                "Format version {} too new. Current version = {}".format(
                    format_version, self.FORMAT_VERSION
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
def _open_lmbd_readonly(self):
        # We set the mapsize here because LMBD will map 1TB of virtual memory if
        # we don't, making it hard to figure out how much memory we're actually
        # using.
        map_size = None
        try:
            map_size = os.path.getsize(self.path)
        except OSError as e:
            raise exceptions.FileFormatError(str(e)) from e
        try:
            store = zarr.LMDBStore(
                self.path, map_size=map_size, readonly=True, subdir=False, lock=False
            )
        except lmdb.InvalidError as e:
            raise exceptions.FileFormatError(
                "Unknown file format:{}".format(str(e))
            ) from e
        except lmdb.Error as e:
            raise exceptions.FileFormatError(str(e)) from e
        return store
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
# using.
        map_size = None
        try:
            map_size = os.path.getsize(self.path)
        except OSError as e:
            raise exceptions.FileFormatError(str(e)) from e
        try:
            store = zarr.LMDBStore(
                self.path, map_size=map_size, readonly=True, subdir=False, lock=False
            )
        except lmdb.InvalidError as e:
            raise exceptions.FileFormatError(
                "Unknown file format:{}".format(str(e))
            ) from e
        except lmdb.Error as e:
            raise exceptions.FileFormatError(str(e)) from e
        return store
github tskit-dev / tsinfer / tsinfer / formats.py View on Github external
# for a given format_name.
    tsinfer_file = None
    try:
        logger.debug("Trying SampleData file")
        tsinfer_file = SampleData.load(path)
        logger.debug("Loaded SampleData file")
    except exceptions.FileFormatError as e:
        logger.debug("SampleData load failed: {}".format(e))
    try:
        logger.debug("Trying AncestorData file")
        tsinfer_file = AncestorData.load(path)
        logger.debug("Loaded AncestorData file")
    except exceptions.FileFormatError as e:
        logger.debug("AncestorData load failed: {}".format(e))
    if tsinfer_file is None:
        raise exceptions.FileFormatError(
            "Unrecognised file format. Try running with -vv and check the log "
            "for more details on what went wrong"
        )
    return tsinfer_file