#!/usr/bin/env python3

from subprocess import run
from sys import argv
import json

anchor_dir = argv[1]
orgs = set()
with open('orgs') as f:
    for line in f:
        ref_org = line.strip()
        orgs.add(line.strip())
orgs = list(orgs)
with open('ref_org','w') as f:
    f.write(f'{ref_org}\n')
run('chmod +x *py',shell=True)
run('./make_directories.py',shell=True)
run(f'./get_all_orgs.py {anchor_dir}',shell=True)
run(f'./make_chr_and_org_mapping_for_gff.py {anchor_dir}',shell=True)
run(f'./run_per_org_local.py {anchor_dir}',shell=True)
run(f'./get_gff_pairwise_succinct.py',shell=True)
run(f'./make_pw_table_from_aligned.py {anchor_dir}',shell=True)
run(f'./make_gff3.py {anchor_dir}',shell=True)
run('./cp_multis_to_one.py',shell=True)

with open(f'../template/job_props.json') as f:
    props = json.load(f)
name = props['job_metadata']['job_name']
run(f'mkdir ../manual_saves/{name}',shell=True)
run(f'mkdir ../manual_saves/{name}/anchors/',shell=True)
run(f'mkdir ../manual_saves/{name}/anchors/candidates',shell=True)
run(f'mkdir ../manual_saves/{name}/anchors/aligned',shell=True)
run(f'cp ../template/job_props.json ../manual_saves/{name}/',shell=True)
run(f'cp -r ../template/.snakemake/log/ ../manual_saves/{name}/',shell=True)
run(f'cp execution_times.txt ./manual_saves/{name}/',shell=True)
for org in orgs:
    run(f'cp ../anchors/candidates/{org} ../save_anchor_candidates/',shell=True)
    run(f'mv ../anchors/candidates/{org} ../manual_saves/{name}/anchors/candidates/',shell=True)
    run(f'mv ../anchors/aligned/{org} ../manual_saves/{name}/anchors/candidates/',shell=True)
