本文整理汇总了Python中cogent.parse.tree.DndParser类的典型用法代码示例。如果您正苦于以下问题:Python DndParser类的具体用法?Python DndParser怎么用?Python DndParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DndParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_tree_from_alignment
def build_tree_from_alignment(aln, moltype, best_tree=False, params=None):
"""Returns a tree from Alignment object aln.
aln: an cogent.core.alignment.Alignment object, or data that can be used
to build one.
moltype: cogent.core.moltype.MolType object
best_tree: if True (default:False), uses a slower but more accurate
algorithm to build the tree.
params: dict of parameters to pass in to the Clustal app controller.
The result will be an cogent.core.tree.PhyloNode object, or None if tree
fails.
"""
# Create instance of app controller, enable tree, disable alignment
app = Clustalw(InputHandler="_input_as_multiline_string", params=params, WorkingDir="/tmp")
app.Parameters["-align"].off()
# Set params to empty dict if None.
if params is None:
params = {}
if moltype == DNA or moltype == RNA:
params["-type"] = "d"
elif moltype == PROTEIN:
params["-type"] = "p"
else:
raise ValueError, "moltype must be DNA, RNA, or PROTEIN"
# best_tree -> bootstrap
if best_tree:
if "-bootstrap" not in params:
app.Parameters["-bootstrap"].on(1000)
if "-seed" not in params:
app.Parameters["-seed"].on(randint(0, 1000))
if "-bootlabels" not in params:
app.Parameters["-bootlabels"].on("nodes")
else:
app.Parameters["-tree"].on()
# Setup mapping. Clustalw clips identifiers. We will need to remap them.
seq_collection = SequenceCollection(aln)
int_map, int_keys = seq_collection.getIntMap()
int_map = SequenceCollection(int_map)
# Collect result
result = app(int_map.toFasta())
# Build tree
tree = DndParser(result["Tree"].read(), constructor=PhyloNode)
for node in tree.tips():
node.Name = int_keys[node.Name]
# Clean up
result.cleanUp()
del (seq_collection, app, result, int_map, int_keys)
return tree
示例2: get_support_file
def get_support_file(group, tree_file, support_file):
def test_group(s):
try:
return group[s]
except KeyError:
return None
color_map = {}
for ind, group_name in enumerate(list(set(group.itervalues()))):
if len(list(set(group.itervalues())))>20:
color_map[group_name] = "#000000"
else:
color_map[group_name] = COLS_BREWER[ind]
color_dict = {}
t = DndParser(open(tree_file, 'U'), constructor=PhyloNode, unescape_name=True)
nodes = t.getNodesDict()
for node, value in nodes.iteritems():
sub_nodes = value.getNodeNames()
sub_node_groups = set(map(test_group, sub_nodes))
try:
sub_node_groups.remove(None)
except KeyError:
pass
sub_node_groups = list(sub_node_groups)
if (len(sub_node_groups)) > 1:
color_dict[node] = 'grey'
else:
color_dict[node] = color_map[sub_node_groups[0]]
with open(support_file, 'w') as out:
for node, color in color_dict.iteritems():
out.write('%s\t%s\n' % (node, color))
示例3: test_DndParser
def test_DndParser(self):
"""DndParser tests"""
t_str = "(A_a,(B:1.0,C),'D_e':0.5)E;"
tree_unesc = DndParser(t_str, PhyloNode, unescape_name=True)
tree_esc = DndParser(t_str, PhyloNode, unescape_name=False)
self.assertEqual(tree_unesc.Name, 'E')
self.assertEqual(tree_unesc.Children[0].Name, 'A a')
self.assertEqual(tree_unesc.Children[1].Children[0].Name, 'B')
self.assertEqual(tree_unesc.Children[1].Children[0].Length, 1.0)
self.assertEqual(tree_unesc.Children[1].Children[1].Name, 'C')
self.assertEqual(tree_unesc.Children[2].Name, 'D_e')
self.assertEqual(tree_unesc.Children[2].Length, 0.5)
self.assertEqual(tree_esc.Name, 'E')
self.assertEqual(tree_esc.Children[0].Name, 'A_a')
self.assertEqual(tree_esc.Children[1].Children[0].Name, 'B')
self.assertEqual(tree_esc.Children[1].Children[0].Length, 1.0)
self.assertEqual(tree_esc.Children[1].Children[1].Name, 'C')
self.assertEqual(tree_esc.Children[2].Name, "'D_e'")
self.assertEqual(tree_esc.Children[2].Length, 0.5)
reload_test = tree_esc.getNewick(with_distances=True, \
escape_name=False)
obs = DndParser(reload_test, unescape_name=False)
self.assertEqual(obs.getNewick(with_distances=True), \
tree_esc.getNewick(with_distances=True))
reload_test = tree_unesc.getNewick(with_distances=True, \
escape_name=False)
obs = DndParser(reload_test, unescape_name=False)
self.assertEqual(obs.getNewick(with_distances=True), \
tree_unesc.getNewick(with_distances=True))
示例4: check_tree_subset
def check_tree_subset(fasta_labels,
tree_fp):
""" Returns a list of all fasta labels that are not a subset of the tree
fasta_labels: list of fasta labels
tree_fp: tree filepath
"""
# Need to get modified fasta labels with underscore stripped
raw_fasta_labels = set([label.split('_')[0] for label in fasta_labels])
tree_f = open(tree_fp, "U")
tree = DndParser(tree_f)
# Get a set of tree tip names
tree_tips = set(tree.getTipNames())
labels_not_in_tips = []
for curr_label in raw_fasta_labels:
if curr_label not in tree_tips:
labels_not_in_tips.append(curr_label)
# Return True if all found in tree tips
if len(labels_not_in_tips) == 0:
labels_not_in_tips = True
return labels_not_in_tips
示例5: build_tree_from_alignment
def build_tree_from_alignment(aln, moltype, best_tree=False, params=None):
"""Returns a tree from alignment
Will check MolType of aln object
"""
if params is None:
params = {}
if moltype == DNA or moltype == RNA:
params["-nt"] = True
elif moltype == PROTEIN:
params["-nt"] = False
else:
raise ValueError, "FastTree does not support moltype: %s" % moltype.label
if best_tree:
params["-slow"] = True
# Create mapping between abbreviated IDs and full IDs
int_map, int_keys = aln.getIntMap()
# Create SequenceCollection from int_map.
int_map = SequenceCollection(int_map, MolType=moltype)
app = FastTree(params=params)
result = app(int_map.toFasta())
tree = DndParser(result["Tree"].read(), constructor=PhyloNode)
# remap tip names
for tip in tree.tips():
tip.Name = int_keys[tip.Name]
return tree
示例6: test_make_distance_based_exclusion_fn
def test_make_distance_based_exclusion_fn(self):
"""make_distance_based_exclusion_fn should return a working function"""
exclude_similar_strains = make_distance_based_exclusion_fn(0.03)
# Test that new function is documented
exp_doc = "Exclude neighbors of tip within 0.030000 branch length units"
self.assertEqual(exp_doc, exclude_similar_strains.__doc__)
# Test that the function works
test_tree = self.SimpleTree.deepcopy()
# print test_tree.getNewick(with_distances=True)
tip = test_tree.getNodeMatchingName("C")
obs = exclude_similar_strains(tip, test_tree).getNewick(with_distances=True)
exp = "(A:0.02,B:0.01)root;"
self.assertEqual(obs, exp)
# Test on a tree where a single node will remain
test_tree = DndParser("((A:0.02,B:0.01)E:0.05,(C:0.06,D:0.01)F:0.05)root;")
# print test_tree.getNewick(with_distances=True)
tip = test_tree.getNodeMatchingName("D")
obs = exclude_similar_strains(tip, test_tree).getNewick(with_distances=True)
exp = "((A:0.02,B:0.01)E:0.05,C:0.11)root;"
self.assertEqual(obs, exp)
# Test that we raise if distance is too large
test_tree = self.SimpleTree.deepcopy()
test_fn = make_distance_based_exclusion_fn(300.0)
tip = test_tree.getNodeMatchingName("C")
self.assertRaises(ValueError, test_fn, tip, test_tree)
示例7: sort_order
def sort_order(records):
"""returns the sort order by id"""
tree = DndParser("(((nosp,sp)named,notnamed)inpref,\
((nosp,sp)named,notnamed)outpref);")
for n in tree.tips():
n.LengthsAndIds = []
lookup = {}
lookup[('named_isolate',True,True)] = \
tree.Children[0].Children[0].Children[0]
lookup[('named_isolate',True,False)] = \
tree.Children[0].Children[0].Children[1]
lookup[('clone',True,False)] = \
tree.Children[0].Children[1]
lookup[('named_isolate',False,True)] = \
tree.Children[1].Children[0].Children[0]
lookup[('named_isolate',False,False)] = \
tree.Children[1].Children[0].Children[1]
lookup[('clone',False,False)] = \
tree.Children[1].Children[1]
for k,v in records.items():
to_lookup = tuple(v[1:])
lookup[to_lookup].LengthsAndIds.append((v[0],k))
order = []
# tips go left->right
for n in tree.tips():
order.extend([i for l,i in sorted(n.LengthsAndIds)[::-1]])
return order
示例8: test_score_tree
def test_score_tree(self):
"""Determine's the tree's fmeasure score"""
# set RankNames and RankNameScores
# if name in RankNames, check score, look at tips, etc
t_str = "(((a,b),(c,d))e,(f,g),h)i;"
t = DndParser(t_str)
t.RankNames = ['i',None,None,None] # 1.0 * 6
t.RankNameScores = [1.0,None,None,None]
t.Children[0].RankNames = [None,'e','foo',None] # 0.5 * 3, 0.6 * 3
t.Children[0].RankNameScores = [None, 0.5, 0.6, None]
t.Children[0].Children[0].RankNames = [None] * 7
t.Children[0].Children[1].RankNames = [None] * 7
t.Children[1].RankNames = [None] * 7
t.Children[1].RankNameScores = [None] * 7
tips = t.tips()
tips[0].Consensus = [None] * 7
tips[1].Consensus = [1,3,None,None]
tips[2].Consensus = [2,4,5,None]
tips[3].Consensus = [None,1,None,None]
tips[4].Consensus = [None,1,None,None]
tips[5].Consensus = [2,None,3,None]
tips[6].Consensus = [None,4,None,None]
decorate_ntips(t)
exp = ((1.0 * 6) + (0.5 * 3) + (0.6 * 3)) / (6 + 3 + 3)
obs = score_tree(t)
self.assertEqual(obs, exp)
示例9: test_backfill_names_gap
def test_backfill_names_gap(self):
"""correctly backfill names"""
consensus_tree = DndParser("(((s1,s2)g1,(s3,s4)g2,(s5,s6)g3)f1)o1;")
rank_lookup = {'s':6,'g':5,'f':4,'o':3,'c':2,'p':1,'k':0}
for n in consensus_tree.traverse(include_self=True):
n.Rank = rank_lookup[n.Name[0]]
input = "((((1)s1,(2)s2),((3)s3,(4)s5)))o1;"
lookup = dict([(n.Name, n) for n in consensus_tree.traverse(include_self=True)])
#exp = "((((1)s1,(2)s2)g1,((3)'g2; s3',(4)'g3; s5')))'o1; f1'"
t = DndParser(input)
t.Rank = 3
t.Children[0].Rank = None
t.Children[0].Children[0].Rank = None
t.Children[0].Children[1].Rank = None
t.Children[0].Children[0].Children[0].Rank = 6
t.Children[0].Children[0].Children[1].Rank = 6
t.Children[0].Children[1].Children[0].Rank = 6
t.Children[0].Children[1].Children[1].Rank = 6
backfill_names_gap(t, lookup)
self.assertEqual(t.BackFillNames, ['o1'])
self.assertEqual(t.Children[0].BackFillNames, [])
self.assertEqual(t.Children[0].Children[0].BackFillNames, [])
self.assertEqual(t.Children[0].Children[1].BackFillNames, [])
self.assertEqual(t.Children[0].Children[0].Children[0].BackFillNames, ['f1','g1','s1'])
self.assertEqual(t.Children[0].Children[0].Children[1].BackFillNames, ['f1','g1','s2'])
self.assertEqual(t.Children[0].Children[1].Children[0].BackFillNames, ['f1','g2','s3'])
self.assertEqual(t.Children[0].Children[1].Children[1].BackFillNames, ['f1','g3','s5'])
示例10: test_bifurcating
def test_bifurcating(self):
"""Coerces nodes to have <= 2 children"""
t_str = "((a:1,b:2,c:3)d:4,(e:5,f:6,g:7)h:8,(i:9,j:10,k:11)l:12)m:14;"
t = DndParser(t_str)
# can't break up easily... sorry 80char
exp_str = "((a:1.0,(b:2.0,c:3.0):0.0)d:4.0,((e:5.0,(f:6.0,g:7.0):0.0)h:8.0,(i:9.0,(j:10.0,k:11.0):0.0)l:12.0):0.0)m:14.0;"
obs = t.bifurcating()
示例11: bootstrap_tree_from_alignment
def bootstrap_tree_from_alignment(aln, seed=None, num_trees=None, params=None):
"""Returns a tree from Alignment object aln with bootstrap support values.
aln: an cogent.core.alignment.Alignment object, or data that can be used
to build one.
seed: an interger, seed value to use
num_trees: an integer, number of trees to bootstrap against
params: dict of parameters to pass in to the Clustal app controller.
The result will be an cogent.core.tree.PhyloNode object, or None if tree
fails.
If seed is not specifed in params, a random integer between 0-1000 is used.
"""
# Create instance of controllor, enable bootstrap, disable alignment,tree
app = Clustalw(InputHandler='_input_as_multiline_string', params=params, \
WorkingDir='/tmp')
app.Parameters['-align'].off()
app.Parameters['-tree'].off()
if app.Parameters['-bootstrap'].isOff():
if num_trees is None:
num_trees = 1000
app.Parameters['-bootstrap'].on(num_trees)
if app.Parameters['-seed'].isOff():
if seed is None:
seed = randint(0,1000)
app.Parameters['-seed'].on(seed)
if app.Parameters['-bootlabels'].isOff():
app.Parameters['-bootlabels'].on("node")
# Setup mapping. Clustalw clips identifiers. We will need to remap them.
seq_collection = SequenceCollection(aln)
int_map, int_keys = seq_collection.getIntMap()
int_map = SequenceCollection(int_map)
# Collect result
result = app(int_map.toFasta())
# Build tree
tree = DndParser(result['Tree'].read(), constructor=PhyloNode)
for node in tree.tips():
node.Name = int_keys[node.Name]
# Clean up
result.cleanUp()
del(seq_collection, app, result, int_map, int_keys)
return tree
示例12: build_tree_from_alignment
def build_tree_from_alignment(aln, moltype, best_tree=False, params={}):
"""Returns a tree from Alignment object aln.
aln: an xxx.Alignment object, or data that can be used to build one.
moltype: cogent.core.moltype.MolType object
best_tree: best_tree suppport is currently not implemented
params: dict of parameters to pass in to the RAxML app controller.
The result will be an xxx.Alignment object, or None if tree fails.
"""
if best_tree:
raise NotImplementedError
if '-m' not in params:
if moltype == DNA or moltype == RNA:
#params["-m"] = 'GTRMIX'
# in version 7.2.3, GTRMIX is no longer supported but says GTRCAT
# behaves like GTRMIX (http://www.phylo.org/tools/raxmlhpc2.html)
params["-m"] = 'GTRGAMMA'
elif moltype == PROTEIN:
params["-m"] = 'PROTGAMMAmatrixName'
else:
raise ValueError("Moltype must be either DNA, RNA, or PROTEIN")
if not hasattr(aln, 'toPhylip'):
aln = Alignment(aln)
seqs, align_map = aln.toPhylip()
# generate temp filename for output
params["-w"] = "/tmp/"
params["-n"] = get_tmp_filename().split("/")[-1]
params["-k"] = True
params["-p"] = randint(1,100000)
params["-x"] = randint(1,100000)
ih = '_input_as_multiline_string'
raxml_app = Raxml(params=params,
InputHandler=ih,
WorkingDir=None,
SuppressStderr=True,
SuppressStdout=True)
raxml_result = raxml_app(seqs)
tree = DndParser(raxml_result['Bootstrap'], constructor=PhyloNode)
for node in tree.tips():
node.Name = align_map[node.Name]
raxml_result.cleanUp()
return tree
示例13: test_get_nearest_named_ancestor
def test_get_nearest_named_ancestor(self):
"""correctly get the nearest named ancestor"""
t = DndParser("(((s1,s2)g1,s3))root;")
t2 = DndParser("(((s1,s2)g1,s3));")
exp_t = t
exp_t2 = None
obs_t = get_nearest_named_ancestor(t.getNodeMatchingName('s3'))
obs_t2 = get_nearest_named_ancestor(t2.getNodeMatchingName('s3'))
self.assertEqual(obs_t, exp_t)
self.assertEqual(obs_t2, exp_t2)
示例14: test_reroot
def test_reroot(self):
"""Should correctly reroot a tree"""
t = DndParser("(((a,b)c,(d,e)f)g,(h,i)j);")
tips = ['a','b']
for n in t.traverse():
n.Length = 1.0
# note, g is lost because it has a single descendent and gets pruned off
exp = "((a:1.0,b:1.0)c:0.5,((d:1.0,e:1.0)f:1.0,(h:1.0,i:1.0)j:2.0):0.5);"
obs = reroot(t, tips)
self.assertEqual(obs.getNewick(with_distances=True), exp)
示例15: assign_tax_labels_to_tree
def assign_tax_labels_to_tree(tree,std):
"""Puts new tip labels onto tree
tree : newick string
std : output from shorten_taxonomy_strings
"""
tree_nodes = DndParser(tree, PhyloNode)
for node in tree_nodes.tips():
label = node.Name.strip('\'') #incase there are actual quotes
tax = std[label]
new_label = str(label) + '_' + tax
node.Name = new_label
return tree_nodes