
import json
import requests

def getKeyPair(path_to_key_pair_file, server_name):
    keysf = open(path_to_key_pair_file, 'r')
    keys_json_string = keysf.read()
    keysf.close()
    keys = json.loads(keys_json_string)
    key_dict = keys[server_name]
    AUTHID = key_dict['key']
    AUTHPW = key_dict['secret']
    return (AUTHID, AUTHPW)

HEADERS = {'accept': 'application/json'}

keypair = getKeyPair('keypairs.json', 'test')

AUTHID = keypair[0]
AUTHPW = keypair[1]

''' script for boiling down experiment json objects
each field deemed as important will stay, while the unimportant fields will be removed '''

# organism object is replaced by organism scientific name
def boildown_organism(organism_object):
    return organism_object['scientific_name']

# award object is replaced by the name of the award
def boildown_award(award_object):
    return award_object['name']

# source object is replaced by the title of the source
def boildown_source(source_object):
    return source_object['title']

# lab object is replaced by the title of the lab
def boildown_lab(lab_object):
    return lab_object['title']

# check if the target object is a control or gene-target type
def is_control_target(target_object):
    for entry in target_object['investigated_as']:
        if entry == 'control':
            return True
    return False

# list of reference objects is replaced by boiled_down publications
def boildown_references(references_list):
    listToReturn = []
    for publicationObject in references_list:
        listToReturn.append(boildown_publication(publicationObject))
    return listToReturn

# publication object is replaced by publicatio identifiers (like Pubmed id)
def boildown_publication(publication_object):
    return publication_object['identifiers']

# document object is replaced by dictionary containing boiled down attachments and references in addition of a url if it exists
def boildown_document(document_object):
    documentDictionary = {}
    if 'document_type' in document_object:
        documentDictionary['document_type'] = document_object['document_type']
    if 'attachment' in document_object:
        documentDictionary['attachment']=boildown_attachment(document_object['attachment'], document_object['@id'])
    if 'urls' in document_object:
        documentDictionary['urls']=document_object['urls']
    if 'references' in document_object:
        ref_dict = {}
        for entry in document_object['references']:
            URL = "https://www.encodeproject.org/" + entry + "?frame=embedded&format=json"
            response = requests.get(URL, auth=(AUTHID, AUTHPW), headers=HEADERS)
            response_json_dict = response.json()
            ref_dict[entry] = boildown_publication(response_json_dict)
        documentDictionary['references'] = ref_dict
    return documentDictionary
# attachment is replaced by dictionary of (md5sum and href) entries
def boildown_attachment(attachment_dict, doc_id):
    dictionaryToReturn = {}
    for key in attachment_dict:
        if key in attachment_interesting_values:
            dictionaryToReturn[key]=doc_id+attachment_dict[key]
    return dictionaryToReturn

# list of documents is replaced by list containing results of boildown_document() function calls
def boildown_documents(documents_list):
    listToReturn = []
    for documentObject in documents_list:
        listToReturn.append(boildown_document(documentObject))
    return listToReturn

# donor object is replaced by limited set (donor_interesting_values list) of its properties
def boildown_donor(donor_object):
    donorDictionary = {}
    for key in donor_object.keys():
        if key in donor_interesting_values:
            donorDictionary[key]=donor_object[key]
        if key == 'target' or key == 'mutated_gene':
            if is_control_target(donor_object[key]) == False:
                donorDictionary[key] = donor_object[key]['label']
    return donorDictionary

# constructs list is replaced by a list of boiled down construct objects
def boildown_constructs(constructs_list):
    listToReturn = []
    for entry in constructs_list:
        listToReturn.append(boildown_construct(entry))
    return listToReturn

# construct object is replaced by dictionary containing values form construct_interesting_values list and target and documents objects boiled down
def boildown_construct(construct_object):
    construct_dictionary = {}
    for key in construct_object.keys():
        if key in construct_interesting_values:
            construct_dictionary[key]=construct_object[key]
        if key == 'target':
            if is_control_target(construct_object[key]) == False:
                construct_dictionary[key] = construct_object[key]['label']
        if key == 'documents':
            construct_dictionary[key] = boildown_documents(construct_object[key])

    return construct_dictionary

# protocol documents are replaced by a list that is returned by boildown_documents() function call
def boildown_protocol_documents(documents_list):