def read_codeml_output(<EOL>filename,<EOL>df,<EOL>altall_cutoff=<NUM_LIT>,<EOL>):
<EOL>with open(filename, '<STR_LIT:r>') as f:<EOL><INDENT>data = f.read()<EOL><DEDENT>regex = re.compile('<STR_LIT>')<EOL>trees = regex.findall(data)<EOL>anc_tree = trees[<NUM_LIT:2>]<EOL>tip_tree = dendropy.Tree.get(data=trees[<NUM_LIT:0>], schema='<STR_LIT>')<EOL>anc_tree = dendropy.Tree.get(data=trees[<NUM_LIT:2>], schema='<STR_LIT>')<EOL>tree = tip_tree<EOL>ancestors = anc_tree.internal_nodes()<EOL>for i, node in enumerate(tree.internal_nodes()):<EOL><INDENT>node.label = ancestors[i].label<EOL><DEDENT>df['<STR_LIT>'] = None<EOL>for node in tree.postorder_node_iter():<EOL><INDENT>if node.parent_node is None:<EOL><INDENT>pass<EOL><DEDENT>elif node.is_leaf():<EOL><INDENT>node_label = node.taxon.label<EOL>parent_label = node.parent_node.label<EOL>df.loc[df.uid == node_label, '<STR_LIT>'] = node_label<EOL>parent_id = df.loc[df.uid == node_label, '<STR_LIT>'].values[<NUM_LIT:0>]<EOL>df.loc[df.id == parent_id, '<STR_LIT>'] = node.parent_node.label<EOL><DEDENT>elif node.is_internal():<EOL><INDENT>label = node.label<EOL>parent_id = df.loc[df.reconstruct_label == label, '<STR_LIT>'].values[<NUM_LIT:0>]<EOL>df.loc[df.id == parent_id, '<STR_LIT>'] = node.parent_node.label<EOL><DEDENT><DEDENT>node_regex = re.compile("""<STR_LIT>""")<EOL>node_num_regex = re.compile("<STR_LIT>")<EOL>df['<STR_LIT>'] = None<EOL>df['<STR_LIT>'] = None<EOL>df['<STR_LIT>'] = None<EOL>df['<STR_LIT>'] = None<EOL>for node in node_regex.findall(data):<EOL><INDENT>node_label = node_num_regex.search(node).group(<NUM_LIT:0>)<EOL>site_regex = re.compile("<STR_LIT>")<EOL>ml_sequence, ml_posterior, alt_sequence, alt_posterior = [], [], [], []<EOL>for site in site_regex.findall(node):<EOL><INDENT>scores = [float(site[i+<NUM_LIT:2>:i+<NUM_LIT:7>]) for i in range(<NUM_LIT:0>,len(site), <NUM_LIT:9>)]<EOL>residues = [site[i] for i in range(<NUM_LIT:0>, len(site), <NUM_LIT:9>)]<EOL>sorted_score_index = [i[<NUM_LIT:0>] for i in sorted(<EOL>enumerate(scores),<EOL>key=lambda x:x[<NUM_LIT:1>],<EOL>reverse=True)]<EOL>ml_idx = sorted_score_index[<NUM_LIT:0>]<EOL>alt_idx = sorted_score_index[<NUM_LIT:1>]<EOL>ml_sequence.append(residues[ml_idx])<EOL>ml_posterior.append(scores[ml_idx])<EOL>if scores[alt_idx] < altall_cutoff:<EOL><INDENT>alt_idx = ml_idx<EOL><DEDENT>alt_sequence.append(residues[alt_idx])<EOL>alt_posterior.append(scores[alt_idx])<EOL><DEDENT>keys = [<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"<EOL>]<EOL>vals = [<EOL>"<STR_LIT>".join(ml_sequence),<EOL>sum(ml_posterior) / len(ml_posterior),<EOL>"<STR_LIT>".join(alt_sequence),<EOL>sum(alt_posterior) / len(alt_posterior),<EOL>]<EOL>df.loc[df.reconstruct_label == node_label, keys] = vals<EOL><DEDENT>return df<EOL>
Read codeml file.