def action(args): def newname(leaf, newname): leaf.name = newname return leaf tree = Phylo.parse(args.tree, args.tree_type).next() leafs = (leaf for leaf in tree.get_terminals()) if args.info: info = DictReader(args.info, fieldnames = ['seqname','newname']) info = {i['seqname']:i['newname'] for i in info} # for newick trees :s will be replaced by |s if args.tree_type == 'newick': info = {s.replace(':', '|'):n for s,n in info.items()} leafs = (l for l in leafs if l.name in info) leafs = (newname(l, info[l.name]) for l in leafs) if args.remove_word: leafs = (newname(l, re.sub(args.remove_word, '', l.name)) for l in leafs) leafs = (newname(l, l.name.strip()) for l in leafs) leafs = (newname(l, args.add_prefix + l.name) for l in leafs) leafs = (newname(l, l.name + args.add_suffix) for l in leafs) # do this last if args.tree_type == 'newick': leafs = (newname(l, l.name.replace(' ', '_')) for l in leafs) # execute changes and write tree list(leafs) Phylo.write(tree, args.out, args.tree_type)
def action(args): def newname(leaf, newname): leaf.name = newname return leaf tree = Phylo.parse(args.tree, args.tree_type).next() leafs = (leaf for leaf in tree.get_terminals()) if args.info: info = DictReader(args.info, fieldnames=['seqname', 'newname']) info = {i['seqname']: i['newname'] for i in info} # for newick trees :s will be replaced by |s if args.tree_type == 'newick': info = {s.replace(':', '|'): n for s, n in info.items()} leafs = (l for l in leafs if l.name in info) leafs = (newname(l, info[l.name]) for l in leafs) if args.remove_word: leafs = (newname(l, re.sub(args.remove_word, '', l.name)) for l in leafs) leafs = (newname(l, l.name.strip()) for l in leafs) leafs = (newname(l, args.add_prefix + l.name) for l in leafs) leafs = (newname(l, l.name + args.add_suffix) for l in leafs) # do this last if args.tree_type == 'newick': leafs = (newname(l, l.name.replace(' ', '_')) for l in leafs) # execute changes and write tree list(leafs) Phylo.write(tree, args.out, args.tree_type)
print d.keys() #['origin', 'dep_time', 'flight', 'hour', 'dep_delay', 'distance', 'dest', 'month', 'air_time', 'carrier', 'year', 'arr_delay', 'tailnum', 'arr_time', 'day', 'minute'] ### SELECTED KEYS selected = ["origin", "dest", "distance", "carrier"] # EXPECTED OUTPUT # {'carrier': 'UA', 'dest': 'IAH', 'distance': '1400', 'origin': 'EWR'} ### METHOD 1: DEFINE BY THE DICT COMPREHENSION {k: d[k] for k in selected} {k: d.get(k) for k in selected} {k: v for k, v in d.items() if k in selected} ### METHOD 2: DEFINE BY THE DICT CONSTRUCTOR dict((k, d[k]) for k in selected) dict(map(lambda k: (k, d[k]), selected)) dict(filter(lambda i: i[0] in selected, d.items())) ### METHOD 3: DEFINE WITH THE ZIP() FUNCTION dict(zip(selected, [d[k] for k in selected])) # ITEMGETTER() FUNCTION WITH THE UNPACK OPERATOR (*) from operator import itemgetter