#!/usr/bin/python

import os
import subprocess
import json

# Dictionary of GlyGen names to OMA code
species_dict = {
    "human":"HUMAN",
    "mouse":"MOUSE",
    "rat":"RATNO",
    "fruitfly":"DROME",
    "yeast":"YEAST",
    "dicty":"DICDI",
    "pig":"PIGXX",
    "chicken":"CHICK"
    }

# Makes sure api call per page entries number is long enough for all entries in file, add 10000 entries if not
def check_records(in_file, entries):
    json_data = json.loads(open(in_file, "r").read())
    if len(json_data) == entries:
        subprocess.call(f'rm {in_file}', shell=True)
        entries += 10000
        cmd = f"wget -O {in_file} https://omabrowser.org/api/pairs/{value_a}/{value_b}/?per_page={entries}&format=json"
        subprocess.call(cmd, shell=True)
        subprocess.call("sleep 60", shell=True)
        check_records(in_file, entries)
    else:
        return

# Download all permutations of species_dict
for key_a,value_a in species_dict.items():
    for key_b,value_b in species_dict.items():
        if key_a != key_b:
            entries = 30000
            in_file = f"{key_a}_{key_b}_oma_orthologs.json"
            cmd = f"wget -O {in_file} https://omabrowser.org/api/pairs/{value_a}/{value_b}/?per_page={entries}&format=json"
            subprocess.call(cmd, shell=True)
            subprocess.call("sleep 60", shell=True)
            check_records(in_file, entries)

