def testUnnormalized(self): for i in range(0xFF): s = bitprocessing.int_as_bitstring(i, 8, ".", "*")[::-1] r = paup.PaupService.bipartition_groups_to_split_bitmask( s, normalized=False) self.assertEqual(r, i, "%s => %s => %s" \ % (bitprocessing.int_as_bitstring(i, 8), s, bitprocessing.int_as_bitstring(r, 8)))
def testNormalized1(self): for i in range(0xFF): s = bitprocessing.int_as_bitstring(i, 8, ".", "*")[::-1] r = paup.PaupService.bipartition_groups_to_split_bitmask(s, normalized=True) normalized = Bipartition.normalize_bitmask(i, 0xFF, 1) self.assertEqual(r, normalized, "%s => %s => %s" \ % (bitprocessing.int_as_bitstring(i, 8), s, bitprocessing.int_as_bitstring(normalized, 8)))
def testNormalized1(self): for i in range(0xFF): s = bitprocessing.int_as_bitstring(i, 8, ".", "*")[::-1] r = paup.PaupService.bipartition_groups_to_split_bitmask( s, normalized=True) normalized = Bipartition.normalize_bitmask(i, 0xFF, 1) self.assertEqual(r, normalized, "%s => %s => %s" \ % (bitprocessing.int_as_bitstring(i, 8), s, bitprocessing.int_as_bitstring(normalized, 8)))
def split_mapper(tree, taxon_set): split_dict={} for node in tree.postorder_node_iter(): if node.parent_node is None: node.value = 1.0 else: taxlist=[] tax = taxon_set.bitmask_taxa_list(node.edge.split_bitmask) for i in tax: taxlist.append(i.label) split_dict[bitprocessing.int_as_bitstring(node.edge.split_bitmask, length=len(taxon_set))] = taxlist return split_dict
def split_freq(tree,taxon_set, split_occ_dict): #for each node for node in tree.postorder_node_iter(): #if not root if node.parent_node is None: node.value = 1.0 else: #split=dendropy.treesplit.split_as_string(node.edge.split_bitmask, width=len(taxon_set)) #deprecated split=bitprocessing.int_as_bitstring(node.edge.split_bitmask, length=len(taxon_set)) print(split) if split not in split_occ_dict.keys(): split_occ_dict[split] = [1] else: split_occ_dict[split].append(1) return split_occ_dict
def split_as_string_rev(split_mask, width, symbol1='.', symbol2='*'): deprecate.dendropy_deprecation_warning( preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string_rev()'.", old_construct="from dendropy import treesplit\nd = treesplit.split_as_string(...)", new_construct="""\ # if using a bipartition d = bipartition.split_as_bitstring(...)[::-1] d = bipartition.leafset_as_bitstring(...)[::-1] # if a "raw" bitmask from dendropy.utility import bitprocessing d = bitprocessing.int_as_bitstring(..., reverse=True)""") return bitprocessing.int_as_bitstring( mask=split_mask, length=width, symbol0=symbol0, symbol1=symbol1, reverse=True)
def split_as_string(split_mask, width, symbol1=None, symbol2=None): deprecate.dendropy_deprecation_warning( preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string()'.", old_construct="from dendropy import treesplit\nd = treesplit.split_as_string(...)", new_construct="""\ # if using a bipartition d = bipartition.split_as_bitstring(...) d = bipartition.leafset_as_bitstring(...) # if a "raw" bitmask from dendropy.utility import bitprocessing d = bitprocessing.int_as_bitstring(...)""") return bitprocessing.int_as_bitstring( n=split_mask, length=width, symbol0=symbol1, symbol1=symbol2, reverse=False)
def split_as_string(split_mask, width, symbol1=None, symbol2=None): deprecate.dendropy_deprecation_warning( preamble= "Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string()'.", old_construct= "from dendropy import treesplit\nd = treesplit.split_as_string(...)", new_construct="""\ # if using a bipartition d = bipartition.split_as_bitstring(...) d = bipartition.leafset_as_bitstring(...) # if a "raw" bitmask from dendropy.utility import bitprocessing d = bitprocessing.int_as_bitstring(...)""") return bitprocessing.int_as_bitstring(n=split_mask, length=width, symbol0=symbol1, symbol1=symbol2, reverse=False)
def split_as_string_rev(split_mask, width, symbol1='.', symbol2='*'): deprecate.dendropy_deprecation_warning( preamble= "Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string_rev()'.", old_construct= "from dendropy import treesplit\nd = treesplit.split_as_string(...)", new_construct="""\ # if using a bipartition d = bipartition.split_as_bitstring(...)[::-1] d = bipartition.leafset_as_bitstring(...)[::-1] # if a "raw" bitmask from dendropy.utility import bitprocessing d = bitprocessing.int_as_bitstring(..., reverse=True)""") return bitprocessing.int_as_bitstring(mask=split_mask, length=width, symbol0=symbol0, symbol1=symbol1, reverse=True)
def getBitStringLabels(tree, l): tree2 = tree.clone(depth=1) for node in tree2.postorder_node_iter(): lab = "\"" + str(bitprocessing.int_as_bitstring(node.edge.split_bitmask, length=l)) + "\"" node.label=lab return tree2
def main(): params = parseArgs() print("\n####################anomaly_finder.py######################\n") #read trees taxon_set = dendropy.TaxonNamespace() print("Reading primary tree...", params.tree) mrc = dendropy.Tree.get_from_path(src=params.tree, schema=params.ftype, taxon_namespace = taxon_set) if params.btrees: print("Reading bootstrap trees...", params.btrees) trees = dendropy.TreeList.get_from_path(params.btrees, params.ftype, taxon_namespace = taxon_set) else: print("No bootstrap trees provided.") #initialize some dictionaries master_dict=dict() mrc_dict=dict() split_dict=dict() split_occ_dict=dict() #if bootstrap trees provided, get results for them #master_dict: #key = pair of edges #value = list of anomaly zone results (1=True; 0=False) if params.btrees: print("Finding anomalous nodes in bootsteap trees...") for sp_tree in trees: #dendropy.treesplit.encode_splits(sp_tree) #deprecated sp_tree.encode_bipartitions() #split_bitmasks = sp_tree.split_bitmask_edge_map.keys() master_dict = get_nodes(sp_tree, master_dict) split_occ_dict=split_freq(sp_tree, taxon_set, split_occ_dict) #sys.exit(0) #get results for main tree print("Finding anomalous nodes in primary tree...") mrc.encode_bipartitions() mrc_dict=get_nodes(mrc, mrc_dict) #get AZ results for primary tree split_dict=split_mapper(mrc, taxon_set) #match a list of taxa to the split pattern from the bitmask for the whole tree print("\n######################ENCODING#############################\n") print("Writing results with splits encoded as:") labelTree = getBitStringLabels(mrc, len(taxon_set)) labelTree.print_plot(show_internal_node_labels=True) print("(If tree is large, import the following into FigTree)\n") print(labelTree.as_string(schema="newick")) #if bootstrap trees provided: #for each internal edge pair: print("\n######################RESULTS##############################\n") print("Showing anomalous nodes only:\n") if params.btrees: print("ParentEdge\tDescendantEdge\tProp.Anomalous") seen=False for k,v in master_dict.items(): taxlab1=list() taxlab2=list() #print(k[0]) #if edge pair has results for main tree if k in mrc_dict.keys(): #print("k:",k, "v:",v) #bitstring for parent node parentLabel = bitprocessing.int_as_bitstring(k[0], length=len(taxon_set)) #print(parentLabel) #list of descendent taxa parentList = taxon_set.bitmask_taxa_list(k[0]) #print(parentList) #label for descendant edge descendantLabel = bitprocessing.int_as_bitstring(k[1], length=len(taxon_set)) #list of descendant taxa descendantList = taxon_set.bitmask_taxa_list(k[1]) #print(descendantList) for i in parentList: taxlab1.append(i.label) for j in descendantList: taxlab2.append(j.label) if sum(v) > 0: seen=True print(parentLabel, descendantLabel,sum(v)/len(v), sep="\t") if seen==False: print("No anomalous nodes detected.") else: print("ParentEdge\tDescendantEdge") seen=False for k,v in mrc_dict.items(): taxlab1=list() taxlab2=list() anom=False #print("k:",k, "v:",v) #bitstring for parent node parentLabel = bitprocessing.int_as_bitstring(k[0], length=len(taxon_set)) #print(parentLabel) #list of descendent taxa parentList = taxon_set.bitmask_taxa_list(k[0]) #print(parentList) #label for descendant edge descendantLabel = bitprocessing.int_as_bitstring(k[1], length=len(taxon_set)) #list of descendant taxa descendantList = taxon_set.bitmask_taxa_list(k[1]) #print(descendantList) for i in parentList: taxlab1.append(i.label) for j in descendantList: taxlab2.append(j.label) if sum(v) > 0: seen=True print(parentLabel, descendantLabel, sep="\t") if seen==False: print("No anomalous nodes detected.") print("\n########################OUTPUT############################\n") if params.btrees: print("Tree with anomalous pairs annotated as \"AZ Event : Prop. BS\"") outTreeBS = getAnomalyLabelsBS(mrc, master_dict) outTreeBS.print_plot(show_internal_node_labels=True) print("(If tree is large, import the following into FigTree)\n") print(outTreeBS.as_string(schema="newick")) with open(params.out, 'w') as fh: l=outTreeBS.as_string(schema="newick") l=l+"\n" fh.write(l) fh.close() else: print("Tree with anomalous edge pairs annotated (AZ pairs numbered)") outTree = getAnomalyLabels(mrc, mrc_dict) outTree.print_plot(show_internal_node_labels=True) print("(If tree is large, import the following into FigTree)\n") print(outTree.as_string(schema="newick")) with open(params.out, 'w') as fh: l=outTree.as_string(schema="newick") l=l+"\n" fh.write(l) fh.close() print("\n########################DONE!############################\n\n")
def testUnnormalized(self): for i in range(0xFF): s = bitprocessing.int_as_bitstring(i, 8, ".", "*")[::-1] r = paup.PaupService.bipartition_groups_to_split_bitmask(s, normalized=False) self.assertEqual(r, i, "%s => %s => %s" \ % (bitprocessing.int_as_bitstring(i, 8), s, bitprocessing.int_as_bitstring(r, 8)))