def mugration(params):
<EOL>if os.path.isfile(params.states):<EOL><INDENT>states = pd.read_csv(params.states, sep='<STR_LIT:\t>' if params.states[-<NUM_LIT:3>:]=='<STR_LIT>' else '<STR_LIT:U+002C>',<EOL>skipinitialspace=True)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>return <NUM_LIT:1><EOL><DEDENT>outdir = get_outdir(params, '<STR_LIT>')<EOL>taxon_name = '<STR_LIT:name>' if '<STR_LIT:name>' in states.columns else states.columns[<NUM_LIT:0>]<EOL>if params.attribute:<EOL><INDENT>if params.attribute in states.columns:<EOL><INDENT>attr = params.attribute<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>"+params.states, file=sys.stderr)<EOL>print("<STR_LIT>"+"<STR_LIT:U+002CU+0020>".join(states.columns), file=sys.stderr)<EOL>return <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>attr = states.columns[<NUM_LIT:1>]<EOL>print("<STR_LIT>"+attr, file=sys.stderr)<EOL><DEDENT>leaf_to_attr = {x[taxon_name]:x[attr] for xi, x in states.iterrows()<EOL>if x[attr]!=params.missing_data}<EOL>unique_states = sorted(set(leaf_to_attr.values()))<EOL>nc = len(unique_states)<EOL>if nc><NUM_LIT>:<EOL><INDENT>print("<STR_LIT>", file=sys.stderr)<EOL>return <NUM_LIT:1><EOL><DEDENT>elif nc<<NUM_LIT:2>:<EOL><INDENT>print("<STR_LIT>", file=sys.stderr)<EOL>return <NUM_LIT:1><EOL><DEDENT>alphabet = [chr(<NUM_LIT>+i) for i,state in enumerate(unique_states)]<EOL>missing_char = chr(<NUM_LIT>+nc)<EOL>letter_to_state = {a:unique_states[i] for i,a in enumerate(alphabet)}<EOL>letter_to_state[missing_char]=params.missing_data<EOL>reverse_alphabet = {v:k for k,v in letter_to_state.items()}<EOL>if params.weights:<EOL><INDENT>params.infer_gtr = True<EOL>tmp_weights = pd.read_csv(params.weights, sep='<STR_LIT:\t>' if params.states[-<NUM_LIT:3>:]=='<STR_LIT>' else '<STR_LIT:U+002C>',<EOL>skipinitialspace=True)<EOL>weights = {row[<NUM_LIT:0>]:row[<NUM_LIT:1>] for ri,row in tmp_weights.iterrows()}<EOL>mean_weight = np.mean(list(weights.values()))<EOL>weights = np.array([weights[c] if c in weights else mean_weight for c in unique_states], dtype=float)<EOL>weights/=weights.sum()<EOL><DEDENT>else:<EOL><INDENT>weights = np.ones(nc, dtype=float)/nc<EOL><DEDENT>W = np.ones((nc,nc), dtype=float)<EOL>mugration_GTR = GTR.custom(pi = weights, W=W, alphabet = np.array(alphabet))<EOL>mugration_GTR.profile_map[missing_char] = np.ones(nc)<EOL>mugration_GTR.ambiguous=missing_char<EOL>treeanc = TreeAnc(params.tree, gtr=mugration_GTR, verbose=params.verbose,<EOL>convert_upper=False, one_mutation=<NUM_LIT>)<EOL>pseudo_seqs = [SeqRecord(id=n.name,name=n.name,<EOL>seq=Seq(reverse_alphabet[leaf_to_attr[n.name]]<EOL>if n.name in leaf_to_attr else missing_char))<EOL>for n in treeanc.tree.get_terminals()]<EOL>treeanc.aln = MultipleSeqAlignment(pseudo_seqs)<EOL>ndiff = treeanc.infer_ancestral_sequences(method='<STR_LIT>', infer_gtr=True,<EOL>store_compressed=False, pc=params.pc, marginal=True, normalized_rate=False,<EOL>fixed_pi=weights if params.weights else None)<EOL>if ndiff==ttconf.ERROR: <EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>print("<STR_LIT>"%attr,params.tree)<EOL>basename = get_basename(params, outdir)<EOL>gtr_name = basename + '<STR_LIT>'<EOL>with open(gtr_name, '<STR_LIT:w>') as ofile:<EOL><INDENT>ofile.write('<STR_LIT>')<EOL>for state in unique_states:<EOL><INDENT>ofile.write('<STR_LIT>'%(reverse_alphabet[state], state))<EOL><DEDENT>ofile.write('<STR_LIT>'+str(treeanc.gtr)+'<STR_LIT:\n>')<EOL>print("<STR_LIT>", gtr_name)<EOL><DEDENT>terminal_count = <NUM_LIT:0><EOL>for n in treeanc.tree.find_clades():<EOL><INDENT>if n.up is None:<EOL><INDENT>continue<EOL><DEDENT>n.confidence=None<EOL>if n.is_terminal() and len(n.name)><NUM_LIT> and bioversion<"<STR_LIT>":<EOL><INDENT>n.name = n.name[:<NUM_LIT>]+'<STR_LIT>'%terminal_count<EOL>terminal_count+=<NUM_LIT:1><EOL><DEDENT>n.comment= '<STR_LIT>'%attr + letter_to_state[n.sequence[<NUM_LIT:0>]] +'<STR_LIT:">'<EOL><DEDENT>if params.confidence:<EOL><INDENT>conf_name = basename+'<STR_LIT>'<EOL>with open(conf_name, '<STR_LIT:w>') as ofile:<EOL><INDENT>ofile.write('<STR_LIT>'+'<STR_LIT:U+002CU+0020>'.join(unique_states)+'<STR_LIT:\n>')<EOL>for n in treeanc.tree.find_clades():<EOL><INDENT>ofile.write(n.name + '<STR_LIT:U+002CU+0020>'+'<STR_LIT:U+002CU+0020>'.join([str(x) for x in n.marginal_profile[<NUM_LIT:0>]])+'<STR_LIT:\n>')<EOL><DEDENT><DEDENT>print("<STR_LIT>", conf_name)<EOL><DEDENT>outtree_name = basename+'<STR_LIT>'<EOL>Phylo.write(treeanc.tree, outtree_name, '<STR_LIT>')<EOL>print("<STR_LIT>",outtree_name)<EOL>return <NUM_LIT:0><EOL>
implementing treetime mugration