def main_regex (opts): print "in main regex" rdr = NewickReader ({'node_annotations': True}) in_tree = rdr.read (opts.in_tree_path) match_regex = re.compile (opts.match_pat) for t in in_tree.iter_tip_nodes(): t.title = match_regex.sub (opts.replace_pat, t.title) wrtr = NewickWriter () wrtr._dist_format = opts.distance_format[0] wrtr.write (in_tree, opts.out_tree_path)
def main_regex(opts): print "in main regex" rdr = NewickReader({'node_annotations': True}) in_tree = rdr.read(opts.in_tree_path) match_regex = re.compile(opts.match_pat) for t in in_tree.iter_tip_nodes(): t.title = match_regex.sub(opts.replace_pat, t.title) wrtr = NewickWriter() wrtr._dist_format = opts.distance_format[0] wrtr.write(in_tree, opts.out_tree_path)
def main_map (opts): from csv import DictReader csv_reader = DictReader (opts.in_csv_path) name_map = {} for r in csv_reader: name_map[r[opts.in_col]] = r[opts.out_col] # now rewrite the tree rdr = NewickReader ({'node_annotations': True}) in_tree = rdr.read (opts.in_tree_path) for t in in_tree.iter_tip_nodes(): t.title = name_map.get(t.title, t.title) wrtr = NewickWriter () wrtr._dist_format = opts.distance_format[0] wrtr.write (in_tree, opts.out_tree_path)
def main_map(opts): from csv import DictReader csv_reader = DictReader(opts.in_csv_path) name_map = {} for r in csv_reader: name_map[r[opts.in_col]] = r[opts.out_col] # now rewrite the tree rdr = NewickReader({'node_annotations': True}) in_tree = rdr.read(opts.in_tree_path) for t in in_tree.iter_tip_nodes(): t.title = name_map.get(t.title, t.title) wrtr = NewickWriter() wrtr._dist_format = opts.distance_format[0] wrtr.write(in_tree, opts.out_tree_path)
class NexusReader (io.BaseReader): def __init__ (self, dialect={}, tree_kls=Tree): io.BaseReader.__init__ (self, dialect=dialect) self._newick_rdr = NewickReader (dialect=self.dialect, tree_kls=Tree) def _read (self, src): """ Read the passed tree and return a Phylotree structure. :Params: src An open file or file-like object. :Returns: A phylotree. """ ## Preconditions & preparation: ## Main: file_str = src.read() m = _TREE_BLK_REGEX.search (file_str) assert (m), "can't find tree block" blk_str = m.group(1) m = _TRANS_TABLE_REGEX.search (blk_str) if m: trans_str = m.group(1) lines = trans_str.split(',') prs = [x.strip().split(' ', 1) for x in lines] table = {} for p in prs: k = p[0] v = p[1] if v.startswith('\''): v = v[1:-1] table[k] = v else: table = {} m = FIRST_TREE_REGEX.search (blk_str) assert (m), "can't find tree entry in block" tree_line = m.group(1) m = TREE_STR_REGEX.search (tree_line) assert (m), "can't find newick tree in line" tree_src = cStringIO.StringIO (m.group(1)) t = self._newick_rdr.read (tree_src) if table: for n in t.iter_nodes(): new_name = table.get(n.title, None) if new_name: n.title = new_name return t
def __init__ (self, dialect={}, tree_kls=Tree): io.BaseReader.__init__ (self, dialect=dialect) self._newick_rdr = NewickReader (dialect=self.dialect, tree_kls=Tree)