import pickle


def obtain_drug_interaction_to_drugs(biana_cnx, output_pickle_file):
    """
    Obtain a dictionary drug_interaction : [drugs]
    """

    cursor = biana_cnx.cursor()

    query = ('''SELECT I.value, D.value from externalEntityRelation R, externalEntityRelationParticipant P, externalEntityDCDB_drugID D, externalEntityDCDB_druginteractionID I
                WHERE R.externalEntityRelationID = P.externalEntityRelationID AND R.type = "interaction" AND P.externalEntityID = D.externalEntityID AND R.externalEntityRelationID = I.externalEntityID
             ''')

    cursor.execute(query)

    drug_int_2_drugs = {}

    for items in cursor:
        drug_int = items[0]
        drug = items[1]
        drug_int_2_drugs.setdefault(drug_int, [])
        drug_int_2_drugs[drug_int].append(drug)

    cursor.close()

    print(drug_int_2_drugs)

    pickle.dump(drug_int_2_drugs, open(output_pickle_file, 'wb'))

    return drug_int_2_drugs