code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def heatmap(dm, partition=None, cmap=CM.Blues, fontsize=10):
assert isinstance(dm, DistanceMatrix)
datamax = float(np.abs(dm.values).max())
length = dm.shape[0]
if partition:
sorting = np.array(flatten_list(partition.get_membership()))
new_dm = dm.reorder(dm.df.columns[sorting])
... | heatmap(dm, partition=None, cmap=CM.Blues, fontsize=10)
Produce a 2D plot of the distance matrix, with values encoded by
coloured cells.
Args:
partition: treeCl.Partition object - if supplied, will reorder
rows and columns of the distance matrix to reflect
... |
def _plotly_3d_scatter(coords, partition=None):
from plotly.graph_objs import Scatter3d, Data, Figure, Layout, Line, Margin, Marker
# auto sign-in with credentials or use py.sign_in()
colourmap = {
'A':'#1f77b4',
'B':'#ff7f0e',
'C':'#2ca02c',
'D':'#d62728',
'E... | _plotly_3d_scatter(coords, partition=None)
Make a scatterplot of treeCl.CoordinateMatrix using the Plotly
plotting engine |
def _add_sphere(ax):
(u, v) = np.mgrid[0:2 * np.pi:20j, 0:np.pi:10j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)
ax.plot_wireframe(x, y, z, color='grey', linewidth=0.2)
return ax | _add_sphere(ax)
Add a wireframe unit sphere onto matplotlib 3D axes
Args:
ax - matplotlib 3D axes object
Returns:
updated matplotlib 3D axes |
def heatmap(self, partition=None, cmap=CM.Blues):
if isinstance(self.dm, DistanceMatrix):
length = self.dm.values.shape[0]
else:
length = self.dm.shape[0]
datamax = float(np.abs(self.dm).max())
fig = plt.figure()
ax = fig.add_subplot(111)
... | Plots a visual representation of a distance matrix |
def get_tree_collection_strings(self, scale=1, guide_tree=None):
records = [self.collection[i] for i in self.indices]
return TreeCollectionTaskInterface().scrape_args(records) | Function to get input strings for tree_collection
tree_collection needs distvar, genome_map and labels -
these are returned in the order above |
def from_json(buffer, auto_flatten=True, raise_for_index=True):
buffer = to_bytes(buffer)
view_out = _ffi.new('lsm_view_t **')
index_out = _ffi.new('lsm_index_t **')
buffer = to_bytes(buffer)
rv = rustcall(
_lib.lsm_view_or_index_from_json,
buffer, len(buffer), view_out, index... | Parses a JSON string into either a view or an index. If auto flatten
is enabled a sourcemap index that does not contain external references is
automatically flattened into a view. By default if an index would be
returned an `IndexedSourceMap` error is raised instead which holds the
index. |
def from_json(buffer):
buffer = to_bytes(buffer)
return View._from_ptr(rustcall(
_lib.lsm_view_from_json,
buffer, len(buffer))) | Creates a sourcemap view from a JSON string. |
def from_memdb(buffer):
buffer = to_bytes(buffer)
return View._from_ptr(rustcall(
_lib.lsm_view_from_memdb,
buffer, len(buffer))) | Creates a sourcemap view from MemDB bytes. |
def from_memdb_file(path):
path = to_bytes(path)
return View._from_ptr(rustcall(_lib.lsm_view_from_memdb_file, path)) | Creates a sourcemap view from MemDB at a given file. |
def dump_memdb(self, with_source_contents=True, with_names=True):
len_out = _ffi.new('unsigned int *')
buf = rustcall(
_lib.lsm_view_dump_memdb,
self._get_ptr(), len_out,
with_source_contents, with_names)
try:
rv = _ffi.unpack(buf, len_out... | Dumps a sourcemap in MemDB format into bytes. |
def lookup_token(self, line, col):
# Silently ignore underflows
if line < 0 or col < 0:
return None
tok_out = _ffi.new('lsm_token_t *')
if rustcall(_lib.lsm_view_lookup_token, self._get_ptr(),
line, col, tok_out):
return convert_token(... | Given a minified location, this tries to locate the closest
token that is a match. Returns `None` if no match can be found. |
def get_original_function_name(self, line, col, minified_name,
minified_source):
# Silently ignore underflows
if line < 0 or col < 0:
return None
minified_name = minified_name.encode('utf-8')
sout = _ffi.new('const char **')
... | Given a token location and a minified function name and the
minified source file this returns the original function name if it
can be found of the minified function in scope. |
def get_source_contents(self, src_id):
len_out = _ffi.new('unsigned int *')
must_free = _ffi.new('int *')
rv = rustcall(_lib.lsm_view_get_source_contents,
self._get_ptr(), src_id, len_out, must_free)
if rv:
try:
return _ffi.unpac... | Given a source ID this returns the embedded sourcecode if there
is. The sourcecode is returned as UTF-8 bytes for more efficient
processing. |
def has_source_contents(self, src_id):
return bool(rustcall(_lib.lsm_view_has_source_contents,
self._get_ptr(), src_id)) | Checks if some sources exist. |
def get_source_name(self, src_id):
len_out = _ffi.new('unsigned int *')
rv = rustcall(_lib.lsm_view_get_source_name,
self._get_ptr(), src_id, len_out)
if rv:
return decode_rust_str(rv, len_out[0]) | Returns the name of the given source. |
def iter_sources(self):
for src_id in xrange(self.get_source_count()):
yield src_id, self.get_source_name(src_id) | Iterates over all source names and IDs. |
def from_json(buffer):
buffer = to_bytes(buffer)
return Index._from_ptr(rustcall(
_lib.lsm_index_from_json,
buffer, len(buffer))) | Creates an index from a JSON string. |
def into_view(self):
try:
return View._from_ptr(rustcall(
_lib.lsm_index_into_view,
self._get_ptr()))
finally:
self._ptr = None | Converts the index into a view |
def from_bytes(buffer):
buffer = to_bytes(buffer)
return ProguardView._from_ptr(rustcall(
_lib.lsm_proguard_mapping_from_bytes,
buffer, len(buffer))) | Creates a sourcemap view from a JSON string. |
def from_path(filename):
filename = to_bytes(filename)
if NULL_BYTE in filename:
raise ValueError('null byte in path')
return ProguardView._from_ptr(rustcall(
_lib.lsm_proguard_mapping_from_path,
filename + b'\x00')) | Creates a sourcemap view from a file path. |
def lookup(self, dotted_path, lineno=None):
rv = None
try:
rv = rustcall(
_lib.lsm_proguard_mapping_convert_dotted_path,
self._get_ptr(),
dotted_path.encode('utf-8'), lineno or 0)
return _ffi.string(rv).decode('utf-8', 'rep... | Given a dotted path in the format ``class_name`` or
``class_name:method_name`` this performs an alias lookup. For
methods the line number must be supplied or the result is
unreliable. |
def apply(self, data):
if self.visitor.is_object:
obj = {}
if self.visitor.parent is None:
obj['$schema'] = self.visitor.path
obj_empty = True
for child in self.children:
empty, value = child.apply(data)
if ... | Apply the given mapping to ``data``, recursively. The return type
is a tuple of a boolean and the resulting data element. The boolean
indicates whether any values were mapped in the child nodes of the
mapping. It is used to skip optional branches of the object graph. |
def apply_iter(cls, rows, mapping, resolver, scope=None):
mapper = cls(mapping, resolver, scope=scope)
for row in rows:
_, data = mapper.apply(row)
yield data | Given an iterable ``rows`` that yield data records, and a
``mapping`` which is to be applied to them, return a tuple of
``data`` (the generated object graph) and ``err``, a validation
exception if the resulting data did not match the expected schema. |
def translate(self, text):
# Reset substitution counter
self.count = 0
# Process text
return self._make_regex().sub(self, text) | Translate text, returns the modified text. |
def cluster(self, n, embed_dim=None, algo=spectral.SPECTRAL, method=methods.KMEANS):
if n == 1:
return Partition([1] * len(self.get_dm(False)))
if embed_dim is None:
embed_dim = n
if algo == spectral.SPECTRAL:
self._coords = self.spectral_embedding(... | Cluster the embedded coordinates using spectral clustering
Parameters
----------
n: int
The number of clusters to return
embed_dim: int
The dimensionality of the underlying coordinates
... |
def spectral_embedding(self, n):
coords = spectral_embedding(self._affinity, n)
return CoordinateMatrix(normalise_rows(coords)) | Embed the points using spectral decomposition of the laplacian of
the affinity matrix
Parameters
----------
n: int
The number of dimensions |
def spectral_embedding_(self, n):
aff = self._affinity.copy()
aff.flat[::aff.shape[0]+1] = 0
laplacian = laplace(aff)
decomp = eigen(laplacian)
return CoordinateMatrix(normalise_rows(decomp.vecs[:,:n])) | Old method for generating coords, used on original analysis
of yeast data. Included to reproduce yeast result from paper.
Reason for difference - switched to using spectral embedding
method provided by scikit-learn (mainly because it spreads
points over a sphere, rather than a half sphe... |
def kpca_embedding(self, n):
return self.dm.embedding(n, 'kpca', affinity_matrix=self._affinity) | Embed the points using kernel PCA of the affinity matrix
Parameters
----------
n: int
The number of dimensions |
def cluster(self, n, embed_dim=None, algo=mds.CLASSICAL, method=methods.KMEANS):
if n == 1:
return Partition([1] * len(self.get_dm(False)))
if embed_dim is None:
embed_dim = n
if algo == mds.CLASSICAL:
self._coords = self.dm.embedding(embed_dim, 'cm... | Cluster the embedded coordinates using multidimensional scaling
Parameters
----------
n: int
The number of clusters to return
embed_dim int
The dimensionality of the underlying coordinates
... |
def cluster(self, nclusters, linkage_method=linkage.WARD, **kwargs):
if linkage_method == linkage.SINGLE:
return self._hclust(nclusters, 'single', **kwargs)
elif linkage_method == linkage.COMPLETE:
return self._hclust(nclusters, 'complete', **kwargs)
elif linkag... | Do hierarchical clustering on a distance matrix using one of the methods:
methods.SINGLE = single-linkage clustering
methods.COMPLETE = complete-linkage clustering
methods.AVERAGE = average-linkage clustering
methods.WARD = Ward's minimum variance method |
def _hclust(self, nclusters, method, noise=False):
matrix = self.get_dm(noise)
linkmat = fastcluster.linkage(squareform(matrix), method)
self.nclusters = nclusters # Store these in case we want to plot
self.linkmat = linkmat #
return _hclust(linkmat, nclusters) | :param nclusters: Number of clusters to return
:param linkage_method: single, complete, average, ward, weighted, centroid or median
(http://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html)
:param noise: Add Gaussian noise to the distance matrix prior to clusterin... |
def plot_dendrogram(self, nclusters=None, leaf_font_size=8, leaf_rotation=90, names=None,
title_font_size=16, ):
if not hasattr(self, 'nclusters') and not hasattr(self, 'linkmat'):
raise ValueError("This instance has no plottable information.")
if nclusters... | Plots the dendrogram of the most recently generated partition
:param nclusters: Override the plot default number of clusters
:return: matplotlib.pyplot.figure |
def dbscan(self, eps=0.75, min_samples=3):
est = DBSCAN(metric='precomputed', eps=eps, min_samples=min_samples)
est.fit(self.get_dm(False))
return Partition(est.labels_) | :param kwargs: key-value arguments to pass to DBSCAN
(eps: max dist between points in same neighbourhood,
min_samples: number of points in a neighbourhood)
:return: |
def _py2_and_3_joiner(sep, joinable):
if ISPY3:
sep = bytes(sep, DEFAULT_ENCODING)
joined = sep.join(joinable)
return joined.decode(DEFAULT_ENCODING) if ISPY3 else joined | Allow '\n'.join(...) statements to work in Py2 and Py3.
:param sep:
:param joinable:
:return: |
def _log_thread(self, pipe, queue):
# thread function to log subprocess output (LOG is a queue)
def enqueue_output(out, q):
for line in iter(out.readline, b''):
q.put(line.rstrip())
out.close()
# start thread
t = threading.Thread(target=... | Start a thread logging output from pipe |
def _search_for_executable(self, executable):
if os.path.isfile(executable):
return os.path.abspath(executable)
else:
envpath = os.getenv('PATH')
if envpath is None:
return
for path in envpath.split(os.pathsep):
exe... | Search for file give in "executable". If it is not found, we try the environment PATH.
Returns either the absolute path to the found executable, or None if the executable
couldn't be found. |
def get_stderr(self, tail=None):
if self.finished():
self.join_threads()
while not self.stderr_q.empty():
self.stderr_l.append(self.stderr_q.get_nowait())
if tail is None:
tail = len(self.stderr_l)
return _py2_and_3_joiner('\n', self.stderr_l... | Returns current total output written to standard error.
:param tail: Return this number of most-recent lines.
:return: copy of stderr stream |
def get_stdout(self, tail=None):
if self.finished():
self.join_threads()
while not self.stdout_q.empty():
self.stdout_l.append(self.stdout_q.get_nowait())
if tail is None:
tail = len(self.stdout_l)
return _py2_and_3_joiner('\n', self.stdout_l... | Returns current total output written to standard output.
:param tail: Return this number of most-recent lines.
:return: copy of stdout stream |
def kill(self):
if self.running():
if self.verbose:
print('Killing {} with PID {}'.format(self.exe, self.process.pid))
self.process.kill()
# Threads *should* tidy up after themselves, but we do it explicitly
self.join_threads() | Kill the running process (if there is one)
:return: void |
def _command_template(self, switches, objectInput=None):
command = ["java", "-jar", self.file_jar, "-eUTF-8"]
if self.memory_allocation:
command.append("-Xmx{}".format(self.memory_allocation))
command.extend(switches)
if not objectInput:
objectInput = su... | Template for Tika app commands
Args:
switches (list): list of switches to Tika app Jar
objectInput (object): file object/standard input to analyze
Return:
Standard output data (unicode Python 2, str Python 3) |
def detect_content_type(self, path=None, payload=None, objectInput=None):
# From Python detection content type from stdin doesn't work TO FIX
if objectInput:
message = "Detection content type with file object is not stable."
log.exception(message)
raise TikaA... | Return the content type of passed file or payload.
Args:
path (string): Path of file to analyze
payload (string): Payload base64 to analyze
objectInput (object): file object/standard input to analyze
Returns:
content type of file (string) |
def extract_only_content(self, path=None, payload=None, objectInput=None):
if objectInput:
switches = ["-t"]
result = self._command_template(switches, objectInput)
return result, True, None
else:
f = file_path(path, payload)
switches =... | Return only the text content of passed file.
These parameters are in OR. Only one of them can be analyzed.
Args:
path (string): Path of file to analyze
payload (string): Payload base64 to analyze
objectInput (object): file object/standard input to analyze
Re... |
def extract_all_content(
self,
path=None,
payload=None,
objectInput=None,
pretty_print=False,
convert_to_obj=False,
):
f = file_path(path, payload, objectInput)
switches = ["-J", "-t", "-r", f]
if not pretty_print:
switches... | This function returns a JSON of all contents and
metadata of passed file
Args:
path (string): Path of file to analyze
payload (string): Payload base64 to analyze
objectInput (object): file object/standard input to analyze
pretty_print (boolean): If True a... |
def sanitize(func):
def wrapper(*args, **kwargs):
return normalize('NFC', func(*args, **kwargs))
return wrapper | NFC is the normalization form recommended by W3C. |
def clean(func):
def wrapper(*args, **kwargs):
# tuple: output command, path given from command line,
# path of templ file when you give the payload
out, given_path, path = func(*args, **kwargs)
try:
if not given_path:
os.remove(path)
except ... | This decorator removes the temp file from disk. This is the case where
you want to analyze from a payload. |
def file_path(path=None, payload=None, objectInput=None):
f = path if path else write_payload(payload, objectInput)
if not os.path.exists(f):
msg = "File {!r} does not exist".format(f)
log.exception(msg)
raise TikaAppFilePathError(msg)
return f | Given a file path, payload or file object, it writes file on disk and
returns the temp path.
Args:
path (string): path of real file
payload(string): payload in base64 of file
objectInput (object): file object/standard input to analyze
Returns:
Path of file |
def write_payload(payload=None, objectInput=None):
temp = tempfile.mkstemp()[1]
log.debug("Write payload in temp file {!r}".format(temp))
with open(temp, 'wb') as f:
if payload:
payload = base64.b64decode(payload)
elif objectInput:
if six.PY3:
p... | This function writes a base64 payload or file object on disk.
Args:
payload (string): payload in base64
objectInput (object): file object/standard input to analyze
Returns:
Path of file |
def get_subject(self, data):
if not isinstance(data, Mapping):
return None
if data.get(self.subject):
return data.get(self.subject)
return uuid.uuid4().urn | Try to get a unique ID from the object. By default, this will be
the 'id' field of any given object, or a field specified by the
'rdfSubject' property. If no other option is available, a UUID will be
generated. |
def reverse(self):
name = self.schema.get('rdfReverse')
if name is not None:
return name
if self.parent is not None and self.parent.is_array:
return self.parent.reverse | Reverse links make sense for object to object links where we later
may want to also query the reverse of the relationship, e.g. when obj1
is a child of obj2, we want to infer that obj2 is a parent of obj1. |
def triplify(self, data, parent=None):
if data is None:
return
if self.is_object:
for res in self._triplify_object(data, parent):
yield res
elif self.is_array:
for item in data:
for res in self.items.triplify(item, par... | Recursively generate statements from the data supplied. |
def _triplify_object(self, data, parent):
subject = self.get_subject(data)
if self.path:
yield (subject, TYPE_SCHEMA, self.path, TYPE_SCHEMA)
if parent is not None:
yield (parent, self.predicate, subject, TYPE_LINK)
if self.reverse is not None:
... | Create bi-directional statements for object relationships. |
def objectify(self, load, node, depth=2, path=None):
if path is None:
path = set()
if self.is_object:
if depth < 1:
return
return self._objectify_object(load, node, depth, path)
elif self.is_array:
if depth < 1:
... | Given a node ID, return an object the information available about
this node. This accepts a loader function as it's first argument, which
is expected to return all tuples of (predicate, object, source) for
the given subject. |
def get_json(request, token):
result = []
searchtext = request.GET['q']
if len(searchtext) >= 3:
pickled = _simple_autocomplete_queryset_cache.get(token, None)
if pickled is not None:
app_label, model_name, query = pickle.loads(pickled)
model = apps.get_model(app... | Return matching results as JSON |
def _dash_f_e_to_dict(self, info_filename, tree_filename):
with open(info_filename) as fl:
models, likelihood, partition_params = self._dash_f_e_parser.parseFile(fl).asList()
with open(tree_filename) as fl:
tree = fl.read()
d = {'likelihood': likelihood, 'ml_tr... | Raxml provides an option to fit model params to a tree,
selected with -f e.
The output is different and needs a different parser. |
def to_dict(self, info_filename, tree_filename, dash_f_e=False):
logger.debug('info_filename: {} {}'
.format(info_filename, '(FOUND)' if os.path.exists(info_filename) else '(NOT FOUND)'))
logger.debug('tree_filename: {} {}'
.format(tree_filename, '(FOUN... | Parse raxml output and return a dict
Option dash_f_e=True will parse the output of a raxml -f e run,
which has different output |
def freader(filename, gz=False, bz=False):
filecheck(filename)
if filename.endswith('.gz'):
gz = True
elif filename.endswith('.bz2'):
bz = True
if gz:
return gzip.open(filename, 'rb')
elif bz:
return bz2.BZ2File(filename, 'rb')
else:
return io.open(... | Returns a filereader object that can handle gzipped input |
def fwriter(filename, gz=False, bz=False):
if filename.endswith('.gz'):
gz = True
elif filename.endswith('.bz2'):
bz = True
if gz:
if not filename.endswith('.gz'):
filename += '.gz'
return gzip.open(filename, 'wb')
elif bz:
if not filename.endsw... | Returns a filewriter object that can write plain or gzipped output.
If gzip or bzip2 compression is asked for then the usual filename extension will be added. |
def glob_by_extensions(directory, extensions):
directorycheck(directory)
files = []
xt = files.extend
for ex in extensions:
xt(glob.glob('{0}/*.{1}'.format(directory, ex)))
return files | Returns files matched by all extensions in the extensions list |
def head(filename, n=10):
with freader(filename) as fr:
for _ in range(n):
print(fr.readline().strip()) | prints the top `n` lines of a file |
def locate_file(filename, env_var='', directory=''):
f = locate_by_env(filename, env_var) or locate_by_dir(filename, directory)
return os.path.abspath(f) if can_locate(f) else None | Locates a file given an environment variable or directory
:param filename: filename to search for
:param env_var: environment variable to look under
:param directory: directory to look in
:return: (string) absolute path to filename or None if not found |
def edge_length_check(length, edge):
try:
assert 0 <= length <= edge.length
except AssertionError:
if length < 0:
raise TreeError('Negative edge-lengths are disallowed')
raise TreeError(
'This edge isn\'t long enough to prune at length {0}\n'
'(Ed... | Raises error if length is not in interval [0, edge.length] |
def logn_correlated_rate(parent_rate, branch_length, autocorrel_param, size=1):
if autocorrel_param <= 0:
raise Exception('Autocorrelation parameter must be greater than 0')
variance = branch_length * autocorrel_param
stdev = np.sqrt(variance)
ln_descendant_rate = np.random.normal(np.log(p... | The log of the descendent rate, ln(Rd), is ~ N(mu, bl*ac), where
the variance = bl*ac = branch_length * autocorrel_param, and mu is set
so that E[Rd] = Rp:
E[X] where ln(X) ~ N(mu, sigma^2) = exp(mu+(1/2)*sigma_sq)
so Rp = exp(mu+(1/2)*bl*ac),
ln(Rp) = mu + (1/2)*bl*ac,
ln(Rp) - (1/2)*bl*ac = mu... |
def _check_single_outgroup(self):
root_child_nodes = self.tree._tree.seed_node.child_nodes()
not_leaves = np.logical_not([n.is_leaf() for n in root_child_nodes])
if not_leaves[not_leaves].size <= 1:
return [root_child_nodes[np.where(not_leaves)[0]].edge]
return [] | If only one (or none) of the seed node children is not a leaf node
it is not possible to prune that edge and make a topology-changing
regraft. |
def prune(self, edge, length=None):
length = length or edge.length
edge_length_check(length, edge)
n = edge.head_node
self.tree._tree.prune_subtree(n, suppress_unifurcations=False)
n.edge_length = length
self.tree._dirty = True
return n | Prunes a subtree from the main Tree, retaining an edge length
specified by length (defaults to entire length). The length is sanity-
checked by edge_length_check, to ensure it is within the bounds
[0, edge.length].
Returns the basal node of the pruned subtree. |
def regraft(self, edge, node, length=None):
rootcheck(edge, 'SPR regraft is not allowed on the root edge')
length = length or edge.length / 2. # Length measured from head to tail
edge_length_check(length, edge)
t = edge.tail_node
h = edge.head_node
new = t.new... | Grafts a node onto an edge of the Tree, at a point specified by
length (defaults to middle of edge). |
def rspr(self, disallow_sibling_sprs=False,
keep_entire_edge=False, rescale=False):
starting_length = self.tree._tree.length()
excl = [self.tree._tree.seed_node.edge] # exclude r
if disallow_sibling_sprs:
excl.extend(self._check_single_outgroup())
pru... | Random SPR, with prune and regraft edges chosen randomly, and
lengths drawn uniformly from the available edge lengths.
N1: disallow_sibling_sprs prevents sprs that don't alter the topology
of the tree |
def get_exchangeable_nodes(self, n):
parent = n.parent_node
a, b = random.sample(n.child_nodes(), 2)
if parent.parent_node is None:
if self.tree.rooted:
c, d = random.sample(n.sister_nodes()[0].child_nodes(), 2)
else:
c, d = random... | A C | Subtrees A, B, C and D are the exchangeable nodes
\ / | around the edge headed by n
-->n | The NNI exchanges either A or B with either C or D
/ \
B D
A C C A | Subtree A is exchanged
... |
def get_children(self, inner_edge):
h = inner_edge.head_node
t = inner_edge.tail_node
if not self.tree._tree.seed_node == t:
original_seed = self.tree._tree.seed_node
self.tree._tree.reseed_at(t)
else:
original_seed = None
head_childr... | Given an edge in the tree, returns the child nodes of the head and
the tail nodes of the edge, for instance:
A C | A, B, C and D are the children of the edge --->,
\ / | C and D are the head node children, and A and B
t--->h | are the tail node children.... |
def nni(
self,
edge,
head_subtree,
tail_subtree,
):
# This implementation works on unrooted Trees. If the input Tree is
# rooted, the ReversibleDeroot decorator will temporarily unroot the
# tree while the NNI is carried out
... | *Inplace* Nearest-neighbour interchange (NNI) operation.
An edge in the tree has two or more subtrees at each end (ends are
designated 'head' and 'tail'). The NNI operation exchanges one of the
head subtrees for one of the tail subtrees, as follows:
A C ... |
def rnni(self, use_weighted_choice=False, invert_weights=False):
if use_weighted_choice:
leaves = list(self.tree._tree.leaf_edge_iter())
e, _ = self.tree.map_event_onto_tree(excluded_edges=leaves, invert_weights=invert_weights)
else:
e = random.choice(self.tr... | Apply a random NNI operation at a randomly selected edge
The edge can be chosen uniformly, or weighted by length --
invert_weights favours short edges. |
def labels(self):
return set([n.taxon.label for n in self._tree.leaf_nodes()]) | Returns the taxon set of the tree (same as the label- or
leaf-set) |
def sample_labels(self, n):
if n >= len(self):
return self.labels
sample = random.sample(self.labels, n)
return set(sample) | Returns a set of n labels sampled from the labels of the tree
:param n: Number of labels to sample
:return: set of randomly sampled labels |
def newick(self):
n = self._tree.as_string('newick',
suppress_rooting=True,
suppress_internal_node_labels=True)
if n:
return n.strip(';\n') + ';'
return n | For more control the dendropy method self.as_string('newick', **kwargs)
can be used.
KWargs include:
suppress_internal_node_labels [True/False]
- turn on/off bootstrap labels
suppress_rooting [True/False]
- turn on/off [&U] or [&R] rooting
state labe... |
def phylotree(self):
if not self._phylotree or self._dirty:
try:
if ISPY3:
self._phylotree = PhyloTree(self.newick.encode(), self.rooted)
else:
self._phylotree = PhyloTree(self.newick, self.rooted)
except Va... | Get the c++ PhyloTree object corresponding to this tree.
:return: PhyloTree instance |
def bifurcate_base(cls, newick):
t = cls(newick)
t._tree.resolve_polytomies()
return t.newick | Rewrites a newick string so that the base is a bifurcation
(rooted tree) |
def trifurcate_base(cls, newick):
t = cls(newick)
t._tree.deroot()
return t.newick | Rewrites a newick string so that the base is a trifurcation
(usually means an unrooted tree) |
def get_inner_edges(self):
inner_edges = [e for e in self._tree.preorder_edge_iter() if e.is_internal()
and e.head_node and e.tail_node]
return inner_edges | Returns a list of the internal edges of the tree. |
def intersection(self, other):
taxa1 = self.labels
taxa2 = other.labels
return taxa1 & taxa2 | Returns the intersection of the taxon sets of two Trees |
def postorder(self, skip_seed=False):
for node in self._tree.postorder_node_iter():
if skip_seed and node is self._tree.seed_node:
continue
yield node | Return a generator that yields the nodes of the tree in postorder.
If skip_seed=True then the root node is not included. |
def preorder(self, skip_seed=False):
for node in self._tree.preorder_node_iter():
if skip_seed and node is self._tree.seed_node:
continue
yield node | Return a generator that yields the nodes of the tree in preorder.
If skip_seed=True then the root node is not included. |
def prune_to_subset(self, subset, inplace=False):
if not subset.issubset(self.labels):
print('"subset" is not a subset')
return
if not inplace:
t = self.copy()
else:
t = self
t._tree.retain_taxa_with_labels(subset)
t._tree.... | Prunes the Tree to just the taxon set given in `subset` |
def randomise_branch_lengths(
self,
i=(1, 1),
l=(1, 1),
distribution_func=random.gammavariate,
inplace=False,
):
if not inplace:
t = self.copy()
else:
t = self
for n in t._tree.preorder_node_iter()... | Replaces branch lengths with values drawn from the specified
distribution_func. Parameters of the distribution are given in the
tuples i and l, for interior and leaf nodes respectively. |
def randomise_labels(
self,
inplace=False,
):
if not inplace:
t = self.copy()
else:
t = self
names = list(t.labels)
random.shuffle(names)
for l in t._tree.leaf_node_iter():
l.taxon._label = names.pop()
... | Shuffles the leaf labels, but doesn't alter the tree structure |
def reversible_deroot(self):
root_edge = self._tree.seed_node.edge
lengths = dict([(edge, edge.length) for edge
in self._tree.seed_node.incident_edges() if edge is not root_edge])
self._tree.deroot()
reroot_edge = (set(self._tree.seed_node.incident_edges(... | Stores info required to restore rootedness to derooted Tree. Returns
the edge that was originally rooted, the length of e1, and the length
of e2.
Dendropy Derooting Process:
In a rooted tree the root node is bifurcating. Derooting makes it
trifurcating.
Call the two edg... |
def autocorrelated_relaxed_clock(self, root_rate, autocorrel,
distribution='lognormal'):
optioncheck(distribution, ['exponential', 'lognormal'])
if autocorrel == 0:
for node in self._tree.preorder_node_iter():
node.rate = root_ra... | Attaches rates to each node according to autocorrelated lognormal
model from Kishino et al.(2001), or autocorrelated exponential |
def rlgt(self, time=None, times=1,
disallow_sibling_lgts=False):
lgt = LGT(self.copy())
for _ in range(times):
lgt.rlgt(time, disallow_sibling_lgts)
return lgt.tree | Uses class LGT to perform random lateral gene transfer on
ultrametric tree |
def rnni(self, times=1, **kwargs):
nni = NNI(self.copy())
for _ in range(times):
nni.rnni(**kwargs)
# nni.reroot_tree()
return nni.tree | Applies a NNI operation on a randomly chosen edge.
keyword args: use_weighted_choice (True/False) weight the random edge selection by edge length
transform (callable) transforms the edges using this function, prior to weighted selection |
def rspr(self, times=1, **kwargs):
spr = SPR(self.copy())
for _ in range(times):
spr.rspr(**kwargs)
return spr.tree | Random SPR, with prune and regraft edges chosen randomly, and
lengths drawn uniformly from the available edge lengths.
N1: disallow_sibling_sprs prevents sprs that don't alter the topology
of the tree |
def scale(self, factor, inplace=True):
if not inplace:
t = self.copy()
else:
t = self
t._tree.scale_edges(factor)
t._dirty = True
return t | Multiplies all branch lengths by factor. |
def strip(self, inplace=False):
if not inplace:
t = self.copy()
else:
t = self
for e in t._tree.preorder_edge_iter():
e.length = None
t._dirty = True
return t | Sets all edge lengths to None |
def translate(self, dct):
new_tree = self.copy()
for leaf in new_tree._tree.leaf_node_iter():
curr_name = leaf.taxon.label
leaf.taxon.label = dct.get(curr_name, curr_name)
return new_tree | Translate leaf names using a dictionary of names
:param dct: Dictionary of current names -> updated names
:return: Copy of tree with names changed |
def _name_things(self):
edges = {}
nodes = {None: 'root'}
for n in self._tree.postorder_node_iter():
nodes[n] = '.'.join([str(x.taxon) for x in n.leaf_nodes()])
for e in self._tree.preorder_edge_iter():
edges[e] = ' ---> '.join([nodes[e.tail_node], nodes[... | Easy names for debugging |
def gene_tree(
self,
scale_to=None,
population_size=1,
trim_names=True,
):
tree = self.template or self.yule()
for leaf in tree._tree.leaf_node_iter():
leaf.num_genes = 1
dfr = tree._tree.seed_node.distance_from_root()
... | Using the current tree object as a species tree, generate a gene
tree using the constrained Kingman coalescent process from dendropy. The
species tree should probably be a valid, ultrametric tree, generated by
some pure birth, birth-death or coalescent process, but no checks are
made. Op... |
def fit(self, ini_betas=None, tol=1.0e-6, max_iter=200, solve='iwls'):
self.fit_params['ini_betas'] = ini_betas
self.fit_params['tol'] = tol
self.fit_params['max_iter'] = max_iter
self.fit_params['solve'] = solve
if solve.lower() == 'iwls':
params, predy, w, ... | Method that fits a model with a particular estimation routine.
Parameters
----------
ini_betas : array
k*1, initial coefficient values, including constant.
Default is None, which calculates initial values during
estima... |
def deriv2(self, p):
from statsmodels.tools.numdiff import approx_fprime_cs
# TODO: workaround proplem with numdiff for 1d
return np.diag(approx_fprime_cs(p, self.deriv)) | Second derivative of the link function g''(p)
implemented through numerical differentiation |
def inverse(self, z):
z = np.asarray(z)
t = np.exp(-z)
return 1. / (1. + t) | Inverse of the logit transform
Parameters
----------
z : array-like
The value of the logit transform at `p`
Returns
-------
p : array
Probabilities
Notes
-----
g^(-1)(z) = exp(z)/(1+exp(z)) |
def inverse(self, z):
p = np.power(z, 1. / self.power)
return p | Inverse of the power transform link function
Parameters
----------
`z` : array-like
Value of the transformed mean parameters at `p`
Returns
-------
`p` : array
Mean parameters
Notes
-----
g^(-1)(z`) = `z`**(1/`power`) |
def deriv(self, p):
return self.power * np.power(p, self.power - 1) | Derivative of the power transform
Parameters
----------
p : array-like
Mean parameters
Returns
--------
g'(p) : array
Derivative of power transform of `p`
Notes
-----
g'(`p`) = `power` * `p`**(`power` - 1) |
def deriv2(self, p):
return self.power * (self.power - 1) * np.power(p, self.power - 2) | Second derivative of the power transform
Parameters
----------
p : array-like
Mean parameters
Returns
--------
g''(p) : array
Second derivative of the power transform of `p`
Notes
-----
g''(`p`) = `power` * (`power` - 1) ... |
def inverse_deriv(self, z):
return np.power(z, (1 - self.power)/self.power) / self.power | Derivative of the inverse of the power transform
Parameters
----------
z : array-like
`z` is usually the linear predictor for a GLM or GEE model.
Returns
-------
g^(-1)'(z) : array
The value of the derivative of the inverse of the power transform... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.