-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added STDR wrap-around #94
Open
richardyz98
wants to merge
3
commits into
refactor
Choose a base branch
from
stdr
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Callable | ||
import dendropy | ||
import numpy as np | ||
import ete3 | ||
import spectraltree | ||
from typing import Callable | ||
|
||
from cassiopeia.data import CassiopeiaTree | ||
from cassiopeia.data import utilities as data_utilities | ||
from cassiopeia.solver import CassiopeiaSolver | ||
|
||
class STDRSolver(CassiopeiaSolver.CassiopeiaSolver): | ||
|
||
def __init__( | ||
self, | ||
similarity_function: Callable[[np.array], np.array] | ||
): | ||
|
||
self.similarity_function = similarity_function | ||
|
||
def solve(self, cassiopeia_tree: CassiopeiaTree) -> None: | ||
character_matrix = cassiopeia_tree.get_current_character_matrix() | ||
taxon_namespace = dendropy.TaxonNamespace(list(character_matrix.index)) | ||
metadata = spectraltree.utils.TaxaMetadata(taxon_namespace, list(character_matrix.index), alphabet=None) | ||
|
||
stdr_nj = spectraltree.STDR(spectraltree.NeighborJoining, self.similarity_function) | ||
|
||
tree_stdr_nj = stdr_nj(character_matrix.values, | ||
taxa_metadata= metadata, | ||
threshold = 64, | ||
min_split = 1, | ||
merge_method = "least_square", | ||
verbose=False) | ||
tree = data_utilities.ete3_to_networkx(ete3.Tree(tree_stdr_nj.as_string(schema="newick"))) | ||
cassiopeia_tree.populate_tree(tree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""This file stores a variety of ways to calculate the similarity used for STDR with neighborjoining | ||
""" | ||
|
||
import numpy as np | ||
import scipy | ||
import spectraltree | ||
|
||
def JC_similarity_matrix(vals): | ||
return spectraltree.JC_similarity_matrix(vals) | ||
|
||
def hamming_similarity_matrix(vals): | ||
"""Hamming Similarity""" | ||
classes = np.unique(vals) | ||
if classes.dtype == np.dtype('<U1'): | ||
# needed to use hamming distance with string arrays | ||
vord = np.vectorize(ord) | ||
vals = vord(vals) | ||
k = len(classes) | ||
hamming_matrix = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(vals, metric='hamming')) | ||
return 1 - hamming_matrix | ||
|
||
def hamming_sim_ignore_missing_values(vals): | ||
missing_val = -1 | ||
classnames, indices = np.unique(vals, return_inverse=True) | ||
num_arr = indices.reshape(vals.shape) | ||
hamming_matrix = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(num_arr, metric='hamming')) | ||
missing_array = (vals==missing_val) | ||
pdist_xor = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(missing_array, lambda u,v: np.sum(np.logical_xor(u,v)))) | ||
|
||
return 1 - (hamming_matrix*vals.shape[1] - pdist_xor) / (np.ones_like(hamming_matrix) * vals.shape[1]) | ||
|
||
def hamming_sim_normalize_missing_values(vals): | ||
missing_val = -1 | ||
classnames, indices = np.unique(vals, return_inverse=True) | ||
num_arr = indices.reshape(vals.shape) | ||
hamming_matrix = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(num_arr, metric='hamming')) | ||
missing_array = (vals==missing_val) | ||
pdist_xor = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(missing_array, lambda u,v: np.sum(np.logical_xor(u,v)))) | ||
pdist_or = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(missing_array, lambda u,v: np.sum(np.logical_or(u,v)))) | ||
|
||
return 1 - (hamming_matrix*vals.shape[1] - pdist_xor) / (np.ones_like(hamming_matrix) * vals.shape[1] - pdist_or) | ||
|
||
def shared_mutations_normalize_missing_values(vals): | ||
missing_val = -1 | ||
uncut_state = 0 | ||
classnames, indices = np.unique(vals, return_inverse=True) | ||
num_arr = indices.reshape(vals.shape) | ||
hamming_matrix = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(num_arr, metric='hamming')) | ||
missing_array = (vals==missing_val) | ||
uncut_array = (vals==uncut_state) | ||
pdist_and = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(missing_array, lambda u,v: np.sum(np.logical_and(u,v)))) | ||
pdist_or = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(missing_array, lambda u,v: np.sum(np.logical_or(u,v)))) | ||
uncut_dist_and = scipy.spatial.distance.squareform(scipy.spatial.distance.pdist(uncut_array, lambda u,v: np.sum(np.logical_and(u,v)))) | ||
return 1 - (((1 - hamming_matrix)*vals.shape[1] - pdist_and - uncut_dist_and) / (np.ones_like(hamming_matrix) * vals.shape[1] - pdist_or)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason that I'm using ete3 here instead of inputting the string directly into
populate_tree
is because the formatting is off. Right now,populate_tree
andnewick_to_network
both assume a specific newick format, specifically those with internal nodes in the newick string. Should I add an argument tonewick_to_network
,populate_tree
, and theinit
of CassiopeiaTree allowing for the user to specify the newick format?