def test_generic_readwrite(self): expected = """Begin data; Dimensions ntax=4 nchar=2; Format datatype=standard symbols="01" gap=-; Matrix Harry 00 Simon 01 Betty 10 Louise 11 ; """.split("\n") nex = NexusReader() nex.handlers['data'] = GenericHandler nex.read_file(os.path.join(EXAMPLE_DIR, 'example.nex')) for line in nex.data.write().split("\n"): e = expected.pop(0).strip() assert line.strip() == e
def test_generic_readwrite(self): expected = """Begin data; Dimensions ntax=4 nchar=2; Format datatype=standard symbols="01" gap=-; Matrix Harry 00 Simon 01 Betty 10 Louise 11 ; """.split("\n") nex = NexusReader() nex.handlers['data'] = GenericHandler nex.read_file(os.path.join(EXAMPLE_DIR, 'example.nex')) for line in nex.data.write().split("\n"): e = expected.pop(0).strip() assert line.strip() == e
from nexus import NexusReader from Bio import SeqIO import sys n = NexusReader() n.read_file("Razafimandimbison_AppS1.txt") for taxon,characters in n.data: print(">",taxon) print("".join(characters)[7865:10693]) fasta_in = "Psychotria_trn.fas" #fastaファイルを読みこみ for record in SeqIO.parse(fasta_in, 'fasta'): id_part = record.id desc_part = record.description seq = record.seq print('>', id_part) print(seq)
class TreeParser(object): START = 1 END = 2 NODE = 3 COMMA = 4 ANNOTATION = 5 BRANCH = 6 taxa_re = re.compile(r"([A-Za-z0-9_\-\.]+)") branch_re = re.compile(r"(\d+(?:\.\d+))") def __init__(self, path): self.n = NexusReader() self.n.read_file(path) def parse(self, _id): data = self.n.trees.trees[_id] tokens = self._tokenize(data) return self._parse(tokens) def _tokenize(self, data): tree_data = data[data.find( '('):-1] # skip tree-level annotation & strip the last semicolon idx = 0 tokens = [] while idx < len(tree_data): if tree_data[idx] == '(': tokens.append(self.START) idx += 1 elif tree_data[idx] == ')': tokens.append(self.END) idx += 1 elif tree_data[idx] == ',': tokens.append(self.COMMA) idx += 1 elif tree_data[idx] == '[': # annotation idx2 = tree_data.find(']', idx + 1) rawstr = tree_data[idx + 1:idx2] annotation = {} for kv in rawstr.split(','): k, v = kv.split("=", 1) annotation[k] = v obj = { 'type': self.ANNOTATION, 'annotation': annotation, } idx = idx2 + 1 tokens.append(obj) elif tree_data[idx] == ':': match = self.branch_re.search(tree_data, idx + 1) assert (match is not None) obj = { 'type': self.BRANCH, 'branch': float(tree_data[match.start():match.end()]), } idx = match.end() tokens.append(obj) else: match = self.taxa_re.search(tree_data, idx) assert (match is not None) taxa = tree_data[match.start():match.end()] obj = { 'type': self.NODE, 'taxa': taxa, } idx = match.end() tokens.append(obj) return tokens def _parse(self, tokens): count = 0 root = Node(_id=count) count += 1 node = root for token in tokens: if token == self.START: node2 = Node(_id=count) count += 1 node.left = node2 node2.parent = node node = node2 elif token == self.END: node = node.parent elif token == self.COMMA: node2 = Node(_id=count) count += 1 node.parent.right = node2 node2.parent = node.parent node = node2 elif token['type'] == self.ANNOTATION: node.annotation = token['annotation'] elif token['type'] == self.BRANCH: node.branch = token['branch'] elif token['type'] == self.NODE: node.name = token['taxa'] return root
#print(samplerAC) t = Tree("((A, B)Internal_1:0.7, (C, D)Internal_2:0.5)root:1.3;", format=1) t.add_features(size=4) print t.get_ascii(attributes=["name", "dist", "size"]) #tree = Phylo.parse("/Users/patricioburchard/Downloads/Particiones_grupos_de_especies/part1.nex.con.tre", "nexus").next() #tree = Phylo.read("/Users/patricioburchard/Downloads/Cercosaura_FinalPAUP.tre", "nexus") #tree.rooted = True #Phylo.draw(tree) #infile = open('/Users/patricioburchard/Downloads/Particiones_grupos_de_especies/part1.nex.run1.p', 'r') #t.show() n = NexusReader( '/Users/patricioburchard/Downloads/Particiones_grupos_de_especies/part1.nex.con.tre' ) n.read_file( '/Users/patricioburchard/Downloads/Particiones_grupos_de_especies/part1.nex.con.tre' ) print(n.blocks) #print(sorted(n.data.taxa)) #print(n.trees.ntrees[0]) for tree in n.trees: print(tree) #tree1 = dendropy.Tree.get_from_string(tree,"nexus") import dendropy #from dendropy import Tree #from dendropy import TreeList x = dendropy.Tree() tree_list1 = dendropy.TreeList() tree_list1.read_from_path( "/Users/patricioburchard/Downloads/Particiones_grupos_de_especies/part1.nex.con.tre",