def tree_layout(tree_file, ps_node_list): t = EvolTree(tree_file, format=0) style_other = NodeStyle() style_other['size'] = 6 style_ps = NodeStyle() style_ps['fgcolor'] = '#ff0000' style_ps['size'] = 6 for node in t.iter_descendants(): descendant = t.get_descendant_by_node_id(node.node_id) if node.node_id in ps_node_list: descendant.img_style = style_ps else: descendant.img_style = style_other ts = TreeStyle() ts.layout_fn = layout ts.show_branch_support = False ts.show_branch_length = False ts.show_leaf_name = False result_picture = os.path.join(output_dir, 'positive_selection_tree.png') t.render(result_picture, tree_style=ts)
def load_parameters(): descendant_dict = defaultdict() with open(input_file, 'r') as f: all_lines = f.readlines() aln_file = all_lines[0].strip() if not os.path.exists(aln_file): logger.error('Invalid cDNA alignment file: {0}'.format(aln_file)) sys.exit(1) logger.info('Input cDNA alignment file: {0}'.format(aln_file)) seq_id_dict = defaultdict() seq_id_list = [] for seq_record in AlignIO.read(aln_file, 'fasta'): seq_id_dict[str(seq_record.id)] = 1 seq_id_list.append(str(seq_record.id)) tree_file = all_lines[1].strip() if not os.path.exists(tree_file): logger.error('Invalid tree file: {0}'.format(tree_file)) sys.exit(1) logger.info('Input tree file: {0}'.format(tree_file)) tmp_t = Tree(tree_file, format=0) node_id_dict = defaultdict() for node in tmp_t: node_id_dict[str(node.name)] = 1 if seq_id_dict != node_id_dict: if len(seq_id_dict) < len(node_id_dict): logger.warning('Sequences is less than tree nodes.') logger.info('Trim input tree file.') tree_file = trim_tree(tree_file, seq_id_list) else: logger.error('Sequences is falsely greater than tree nodes.') sys.exit(1) t = EvolTree(tree_file, format=1) for descendant in t.iter_descendants(): descendant_dict[descendant.node_id] = str(descendant) root = t.get_tree_root() id_list = [] for leaf in t.traverse('preorder'): id_list.append(leaf.node_id) select_nodes = [] if len(all_lines) > 2: for each_line in all_lines[2:]: s = each_line.strip() if s: select_nodes.append(s) if select_nodes: nodes_line = ', '.join(select_nodes) logger.info('Input nodes: {0}'.format(nodes_line)) for node in select_nodes: if node not in t: logger.error('Error node: {0}'.format(node)) sys.exit(1) if not t.check_monophyly(values=select_nodes, target_attr='name'): logger.error('Some nodes are not monophyletic.') sys.exit(1) common_ancestor = t.get_common_ancestor(select_nodes) else: common_ancestor = root logger.info('No specific node') run_list = [] for s in common_ancestor.iter_descendants(): run_list.append(s.node_id) logger.info('These node ids will be checked: {0}'.format( str(run_list))) return run_list, aln_file, tree_file, descendant_dict