Skip to content

Commit

Permalink
added cassiopeia dependencies to do preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
uym2 committed Jul 9, 2024
1 parent 340c5e4 commit 429f28c
Show file tree
Hide file tree
Showing 7 changed files with 29,293 additions and 0 deletions.
4 changes: 4 additions & 0 deletions laml_libs/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .errors import *
from .logging import logger
from .utilities import *
from .warnings import *
122 changes: 122 additions & 0 deletions laml_libs/mixins/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
class AutocorrelationError(Exception):
"""An Exception for the tools.autocorrelation methods."""

pass


class CassiopeiaError(Exception):
"""An general exception for the Cassiopeia software."""

pass


class CassiopeiaTreeError(Exception):
"""An Exception class for the CassiopeiaTree class."""

pass


class DataSimulatorError(Exception):
"""Generic error for the DataSimulator subclasses"""

pass


class DistanceSolverError(Exception):
"""An Exception class for all DistanceSolver subclasses."""

pass

class ecDNABirthDeathSimulatorError(Exception):
"""An ExceptionClass for ecDNABirthDeathSimulator class."""

pass

class FitchCountError(Exception):
"""An ExceptionClass for FitchCount."""

pass


class GreedySolverError(Exception):
pass


class HybridSolverError(Exception):
"""An Exception class for all HybridSolver subclasses."""

pass


class ILPSolverError(Exception):
"""An Exception class for all ILPError subclasses."""

pass


class iTOLError(Exception):
pass


class LeafSubsamplerError(Exception):
"""An Exception class for the LeafSubsampler class."""

pass


class PreprocessError(Exception):
pass


class PriorTransformationError(Exception):
"""An Exception class for generating weights from priors."""

pass


class SharedMutationJoiningSolverError(Exception):
"""An Exception class for SharedMutationJoiningSolver."""

pass


class TreeSimulatorError(Exception):
"""An Exception class for all exceptions generated by
TreeSimulator or a subclass of TreeSimulator
"""

pass


class UnknownCigarStringError(Exception):
pass


class UnspecifiedConfigParameterError(Exception):
pass


class BranchLengthEstimatorError(Exception):
"""An Exception class for the BranchLengthEstimator class."""

pass


class IIDExponentialMLEError(BranchLengthEstimatorError):
pass


class TreeMetricError(Exception):
"""An Exception class for calculating tree metrics"""

pass


class ParameterEstimateError(Exception):
"""An Exception class for the estimation and retrieval of tree parameters"""

pass


class PlottingError(Exception):
pass
6 changes: 6 additions & 0 deletions laml_libs/mixins/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import logging

import ngs_tools as ngs

logger = ngs.logging.Logger(__name__)
logger.setLevel(logging.INFO)
87 changes: 87 additions & 0 deletions laml_libs/mixins/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import functools
import importlib
from types import ModuleType
from typing import Dict, List, Optional, Tuple, Union

import numpy as np


def is_ambiguous_state(state: Union[int, Tuple[int, ...]]) -> bool:
"""Determine whether the provided state is ambiguous.
Note that this function operates on a single (indel) state.
Args:
state: Single, possibly ambiguous, character state
Returns:
True if the state is ambiguous, False otherwise.
"""
return isinstance(state, tuple)


def try_import(module: str) -> Optional[ModuleType]:
"""Helper function to import a possibly not-installed module.
Args:
module: Module to try and import
Returns:
The imported module, if the module exists, or None
"""
try:
return importlib.import_module(module)
except ModuleNotFoundError:
return None


def unravel_ambiguous_states(
state_array: List[Union[int, Tuple[int, ...]]]
) -> List[int]:
"""Helper function to unravel ambiguous states.
Args:
A list of states, potentially containing ambiguous states.
Returns:
A list of unique states contained in the list.
"""
all_states = [
list(state) if is_ambiguous_state(state) else [state]
for state in state_array
]
return functools.reduce(lambda a, b: a + b, all_states)

def find_duplicate_groups(character_matrix) -> Dict[str, Tuple[str, ...]]:
"""Maps duplicated indices in character matrix to groups.
Groups together samples in a character matrix if they have the same
character states.
Args:
character_matrix: Character matrix, potentially with ambiguous states.
Returns:
A mapping of a single sample name to the set of of samples that have
the same character states.
"""

character_matrix.index.name = "index"

# convert to sets to support ambiguous states
character_matrix_sets = character_matrix.copy()
character_matrix_sets = character_matrix_sets.apply(
lambda x: [
set(s) if is_ambiguous_state(s) else set([s])
for s in x.values
],
axis=0,
).apply(tuple, axis=1)
is_duplicated = (
character_matrix_sets.duplicated(keep=False)
)
unique_states = np.unique(character_matrix_sets[is_duplicated])
duplicate_groups = [character_matrix_sets[character_matrix_sets == val].index.values for val in unique_states]
duplicate_mappings = {g[0]: tuple(g) for g in duplicate_groups}

return duplicate_mappings
31 changes: 31 additions & 0 deletions laml_libs/mixins/warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class CassiopeiaTreeWarning(UserWarning):
"""A Warning for the CassiopeiaTree class."""

pass

class DataSimulatorWarning(UserWarning):
pass


class PreprocessWarning(UserWarning):
pass


class SharedMutationJoiningSolverWarning(UserWarning):
"""A warning class for SharedMutationJoiningSolver."""

pass


class ParameterEstimateWarning(UserWarning):
"""An warning class for the estimation and retrieval of tree parameters"""

pass


class PlottingWarning(UserWarning):
pass

class LeafSubsamplerWarning(UserWarning):

pass
Loading

0 comments on commit 429f28c

Please sign in to comment.