def run_mcmc3r(indir, odir, intree, clock=2, usedata=1): if not exists(odir): os.system(f'mkdir -p {odir}') ctl = glob(f"{indir}/*.ctl") if not ctl: raise IOError('not ctl file') os.system(f'cp {ctl[0]} {odir}/04_mcmctree.ctl') os.system(f'ln -sf `realpath {indir}/in.BV` {odir}/') param = { 'treefile': intree, 'clock': clock, 'usedata': '1' if usedata == 1 else '2 ../in.BV 1', # 'seqtype': '0' } text = modify(f'{odir}/04_mcmctree.ctl', **param) with open(f'{odir}/04_mcmctree.ctl', 'w') as f1: f1.write(text) cmd = f"""/home-user/thliao/anaconda3/envs/r_env/bin/R -e "setwd('{odir}'); b = mcmc3r::make.beta(n=8, a=5, method='step-stones'); mcmc3r::make.bfctlf(b, ctlf='04_mcmctree.ctl', betaf='beta.txt')" """ check_call(cmd, shell=1, executable='/home-user/thliao/anaconda3/bin/zsh') _ctl = "04_mcmctree.ctl" cmds = [] for _ in range(1, 9): cmds.append(f"cd {odir}/{_}/ ; mcmctree {_ctl} > run.log ") return cmds
def run_mcmc3r(indir, odir, intree, clock=2, usedata=1): if not exists(odir): os.system(f'mkdir -p {odir}') os.system(f'cp {indir}/04_mcmctree.ctl {odir}/') os.system(f'ln -s `realpath {indir}/in.BV` {odir}/') param = { 'treefile': intree, 'clock': clock, 'usedata': '1' if usedata == 1 else '2 ../in.BV 1', # 'seqtype': '0' } text = modify(f'{odir}/04_mcmctree.ctl', **param) with open(f'{odir}/04_mcmctree.ctl', 'w') as f1: f1.write(text) cmd = f""" `which R` -e "setwd('{odir}'); b = mcmc3r::make.beta(n=8, a=5, method='step-stones'); mcmc3r::make.bfctlf(b, ctlf='04_mcmctree.ctl', betaf='beta.txt')" """ check_call(cmd, shell=1, executable='/home-user/thliao/anaconda3/bin/zsh')
target_ = ['set33', 'set34', 'set35', 'set36', 'set37'] target_dir = './AR_set1' for t in target_: for model in ['IR', 'AR']: if not exists(f"./{model}_{t}"): os.makedirs(f"./{model}_{t}") os.system(f'cp {target_dir}/03_mcmctree.ctl ./{model}_{t}/') param = { 'treefile': "/share/home-user/thliao/data/plancto/dating_for/cal_tree/83g_set1.newick" .replace('set1', t), 'clock': 2 if model == 'IR' else 3 } text = modify(f'./{model}_{t}/03_mcmctree.ctl', **param) with open(f'./{model}_{t}/03_mcmctree.ctl', 'w') as f1: f1.write(text) cmd = f"""R -e "setwd('{model}_{t}'); b = mcmc3r::make.beta(n=8, a=5, method='step-stones'); mcmc3r::make.bfctlf(b, ctlf='03_mcmctree.ctl', betaf='beta.txt')" """ os.system(cmd) cmd = f"""R -e "setwd('IR_{t}'); b = mcmc3r::make.beta(n=8, a=5, method='step-stones'); mcmc3r::make.bfctlf(b, ctlf='03_mcmctree.ctl', betaf='beta.txt')" """ os.system(cmd) _ctl = "03_mcmctree.ctl" cmds = [] for t in target_: for _ in range(1, 9): for model in ['IR', 'AR']: cmds.append( f"cd {model}_{t}/{_}/ ; mcmctree {_ctl} > run.log ")
def generate_batch_mcmc(cal_trees, template_dir, odir, program_name='', clock_t='2', seqtype='nucl', seqfile=None, print=1, sampfreq=20, nsample=20000, burnin=2000): """generate batch mcmc directory according to the template dir which contains the precalculated in.BV file. Args: cal_trees (dict): dict of calibrated trees with the set name as their key. template_dir (str): dir with in.BV odir (str): ouput directroy, it will automated generate descending directory following f"{odir}/{program_name}/{seqtype}/clock{clock_t}". program_name (str, optional): for generated ouput directory. Defaults to ''. clock_t (str, optional): 3 for AR. 2 for IR . Defaults to '2'. seqtype (str, optional): [description]. Defaults to 'nucl'. seqfile (str, optional): [description]. Defaults to None. print (int, optional): [description]. Defaults to 1. sampfreq (int, optional): [description]. Defaults to 20. nsample (int, optional): [description]. Defaults to 20000. burnin (int, optional): [description]. Defaults to 2000. Returns: list: list of commands suitable to run mcmc """ odir = f"{odir}/{program_name}/{seqtype}/clock{clock_t}" cmds = [] for cal_name, cal_tree in cal_trees.items(): set_name = cal_name pre_ctl = glob(f'{template_dir}/*.ctl') if not pre_ctl: continue else: pre_ctl = pre_ctl[0] inbv = abspath(glob(f'{template_dir}/in.BV')[0]) param = { # 'seqfile': seqfile_b, 'treefile': abspath(cal_tree), # 'ndata': ndata, # 'seqtype': seqtype, 'usedata': "2 in.BV 1", 'outfile': './03_mcmctree.out', 'clock': clock_t, # 'BDparas': bd_paras, 'rgene_gamma': '1 100 1' if seqtype == 'nucl' else '1 30 1', # 'sigma2_gamma': sigma2_gamma, 'burnin': burnin, 'sampfreq': sampfreq, 'nsample': nsample, # 'alpha': 0.5, 'print': print } if seqfile is not None: param['seqfile'] = seqfile # modify these ctl for repeat_n in ['run1', 'run2']: onew_name = f'{set_name}_{repeat_n}' if exists(pre_ctl): # prepare dir and soft link the in.BV final_odir = join(odir, onew_name) os.makedirs(final_odir, exist_ok=True) os.system(f"ln -sf {inbv} {final_odir}/ ") output_ctl = join(final_odir, '04_mcmctree.ctl') # generate the text text = modify(pre_ctl, **param) with open(output_ctl, 'w') as f1: f1.write(text) # generate the command cmd = f"cd {join(final_odir)} ; mcmctree ./04_mcmctree.ctl > ./run.log " # check the completeness of previous run if not exists(join(final_odir, 'FigTree.tre')): cmds.append(cmd) if exists(join(final_odir, 'mcmc.txt')): # if not figTree.tre but mcmc.txt, it means it doesn't complete at last time os.system(f"rm {join(final_odir, 'mcmc.txt')} ") return cmds