def handle_click(xml, xpath, path=None): from lxml import etree xml_root = etree.fromstring(xml) # Track back along xpath to find the source element where we're going to set the name element = xml_root.xpath(xpath + "/string_value") tree = element[0].text if (path == None): path = os.getcwd() filter_names_and_patterns = {} filter_names_and_patterns['Phylo files'] = [ "*.tre", "*nex", "*.nwk", "*.new", "*.tnt" ] filename = dialogs.get_filename( title="Choose tree file", action=gtk.FILE_CHOOSER_ACTION_SAVE, filter_names_and_patterns=filter_names_and_patterns, folder_uri=path) if filename == None: return # guess format based on ending, assuming matrix new_output, ext = os.path.splitext(filename) output_string = "" if ext == ".tre": # Nexus tree file output_string = stk.permute_tree(tree, treefile="nexus") elif ext == ".nex": # Nexus matrix output_string = stk.permute_tree(tree, matrix="nexus") elif ext == ".tnt": # tnt matrix output_string = stk.permute_tree(tree, matrix="hennig") elif ext == ".new" or ext == ".nwk": # newick tree output_string = stk.permute_tree(tree, treefile="newick") else: dialogs.error( None, "Error creating permuting trees. Unknown format. Did you use a file extension to indicate the format required?" ) f = open(filename, 'w') f.write(output_string) f.close return
def test_permute_tree_quoted(self): """Test an awkward tree to permute""" tree = "(((Theatops_posticus, (Cryptops_spinipes, Scolopocryptops_sexspinosus)),((Rhysida_nuda, ((Alipes_crotalus, (Ethmostigmus_rubripes, (Ethmostigmus_sp1, Ethmostigmus_sp2))), (((Rhysida_sp, (Rhysida_lithobiodis, Rhysida_imarginata%1)), (Rhysida_imarginata%2, Rhysida_longipes%1)), (Rhysida_imarginata%3, (Rhysida_imarginata%4, (Rhysida_imarginata%5, Rhysida_longipes%2)))), (Digitipes_coonoorensis, Digitipes_sp1, Digitipes_barnabasi, Digitipes_sp2))), ((Cormocephalus_monthanii, (Cormocephalus_nigrificatus, (Cormocephalus_sp1, (Cormocephalus_nudipes, (Cormocephalus_sp2, Cormocephalus_westwoodi))))), (('Scolopendra cf. morsitans%1', 'Scolopendra cf. morsitans%2', ('Scolopendra cf. morsitans%3', Scolopendra_cf._amazonica)), (Asanada_agharkari, Asanada_brevicornis))))), (Craterostigmus_tasmanianus, Craterostigmus_crabilli), (Mecistocephalus_guildingii, ((Himantarium_gabrielis, Bothriogaster_signata), (Geophilus_electricus, (Strigamia_maritima, Pachymerium_ferrugineum)))));" try: trees = permute_tree(tree, treefile="nexus") except: self.assert_(False)
def test_permute_trees_3(self): XML = etree.tostring(etree.parse('data/input/permute_trees.phyml', parser), pretty_print=True) trees = obtain_trees(XML) # contains quoted taxa too output = permute_tree(trees['Hill_Davis_2011_2'], treefile="newick") self.assert_(_trees_equal(output, "(A, (B, (C, D, E_E, F, G)));"))
def test_permute_trees_2(self): XML = etree.tostring(etree.parse('data/input/permute_trees.phyml', parser), pretty_print=True) trees = obtain_trees(XML) output = permute_tree(trees['Davis_2011_1'], treefile="newick") temp_file_handle, temp_file = tempfile.mkstemp(suffix=".new") f = open(temp_file, "w") f.write(output) f.close() output_trees = import_trees(temp_file) expected_trees = import_trees("data/output/permute_trees_2.nex") os.remove(temp_file) self.assert_(len(output_trees) == len(expected_trees)) for i in range(0, len(output_trees)): self.assert_(_trees_equal(output_trees[i], expected_trees[i]))
def test_collapse_with_quotes(self): tree = "(Proteroiulus_fuscus, (Craterostigmus_tasmanianus, ((Scolopendra_viridis,(Lithobius_variegatus, (Paralamyctes_validus, Anopsobius_neozelanicus))),(Sphendononema_guildingii, ((Scutigerina_weberi%1, (Scutigerina_weberi%2,(Scutigerina_malagassa, Scutigerina_hova))), (Scutigera_coleoptrata,((Thereuopoda_longicornis, 'Thereuopodina, sp. nov.', (Thereuonema_tuberculata,Thereuonema_turkestana, Thereuopoda_clunifera)), (Allothereua_bidenticulata,Allothereua_serrulata, Parascutigera_festiva, Parascutigera_latericia))))))));" output = permute_tree(tree, treefile="newick") expected_tree = "(Proteroiulus_fuscus, (Craterostigmus_tasmanianus,((Scolopendra_viridis,(Lithobius_variegatus,(Paralamyctes_validus,Anopsobius_neozelanicus))),(Sphendononema_guildingii,((Scutigerina_weberi,(Scutigerina_malagassa, Scutigerina_hova)), (Scutigera_coleoptrata,((Thereuopoda_longicornis, 'Thereuopodina, sp. nov.', (Thereuonema_tuberculata,Thereuonema_turkestana, Thereuopoda_clunifera)), (Allothereua_bidenticulata,Allothereua_serrulata, Parascutigera_festiva, Parascutigera_latericia))))))));" self.assert_(_trees_equal(output, expected_tree))