#!/usr/bin/env python
# coding: utf-8

# In[1]:


#This code make research and queries between three resources:
#1. ECBD from EuOpenScreen for Chemical Compound
#2. PIDAR from EuroBioimaging for Preclinical Image Dataset Metadata: Chemical Compound, Species, Strain, Disease
#3. INFRAFRONTIER for Strain and Disease model related to strains datasets
#4. IDR Image Data Resource for Chemical compound, Species, Cell line, Organism
#It provide some example of cross researching in different case studies

# for every example you have to import this section
import numpy as np
import pandas as pd
import re
import json
import requests
import base64
import ipywidgets as widgets
import bokeh.palettes
from operator import itemgetter
from IPython.display import display, clear_output, HTML
from bokeh.io import show, output_notebook
from bokeh.models import ColorBar, ColumnDataSource, CategoricalColorMapper
from bokeh.plotting import figure
from bokeh.transform import transform
import urllib.request, json 


#**************************************************#

#1. EXAMPLE QUERIES FOR CHEMICAL COMPOUND
#Chemical compound --> between ECBD, PIDAR, IDR


#*************************************************#

#INSERT THE MOLECULE NAME THAT THE USER WANT SEARCH
research= 'PLERIXAFOR'  

#+++++++++++++++++++++++++++++++#

# Load and Query bioactive molecules from the ECBD

jsonFile= open('ebcd_eu.json','r') # you can download the file from https://ecbd.eu/static/core/compounds/bioactives.json
values = json.load(jsonFile)
jsonFile.close
array=[]
for i in range (1,len(values),1):
    dic= dict(values[i])
    array.append(dic['structure'])
    ids = []
    name = []
    inchi = []
    formula = []
    link=[]
    for j in range (1,len(array),1):
        di= dict(array[j])
        if research in di['name']:
            df_t = pd.DataFrame(columns=["id","name","formula","inchi","link"])
            ids.append(di["id"])
            name.append(di["name"])
            formula.append(di["formula"])
            inchi.append(di["inchi"])
            df_t["id"] = ids
            df_t["name"] = name
            df_t["formula"] = formula
            df_t["inchi"] = inchi
            link1="https://ecbd.eu/compound/#lib{value='2'}name{value='"
            end_link="'}"
            df_t["link"] =link1+research+end_link
print('ECBD RESULTS')
display(HTML(df_t.to_html(justify="left", index=False, render_links=True, escape=False)))
#+++++++++++++++++++++++++++++++#

#-----------------------------------#
#Load and Query the PIDAR

data=requests.get("https://pidar.hpc4ai.unito.it/download.php").json()
KEYS = ['pharmacological drug'] #add more fields if you want

def find_pidar(model):
    results = []
    for d in data:
        values = []   
        for key, value in d.items():
            k = key.lower().strip()
            if k in KEYS:
                values.append(value.lower().strip())
                if any(model in s for s in values): #and any(species in s for s in values) and any(organ in s for s in values):
                    results.append(d)
    return results

result = find_pidar('amd3100')
link='https://pidar.hpc4ai.unito.it/?Dropdown=any&search='
end_link='&access=Submit'
l=link+research+end_link
list_of_keys = ['Dataset ID ','Publication title','Publication DOI','Study type','Imaging modality','Institution','Imaging facility','Species','Sample size','Disease model','Organ or Tissue','Status', 'link']
r=[]
for a in result:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('PIDAR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False)))

#-----------------------------------#

#//////////////////////////////////////////////#

#Load and Query the IDR

INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
with requests.Session() as session:
    request = requests.Request('GET', INDEX_PAGE)
    prepped = session.prepare_request(request)
    response = session.send(prepped)
    if response.status_code != 200:
        response.raise_for_status()
SEARCH_URL = "https://idr.openmicroscopy.org/searchengine/api/v1/resources/{type}/search/"
KEY_VALUE_SEARCH = SEARCH_URL + "?key={key}&value={value}"
KEY = "Compound Name"
value = 'Plerixafor'
qs1 = {'type': 'image', 'key': KEY, 'value': value}
url = KEY_VALUE_SEARCH.format(**qs1)
json = session.get(url).json()['results']['results']
list_of_keys = ['screen_id','screen_name','link']
link='https://idr.openmicroscopy.org/search/?key=Compound+Name&value='
end_link='&operator=contains'
l=link+research+end_link
r=[]
for a in json:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('IDR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))
#//////////////////////////////////////////////#

#print table for Example 1 
#end for Example 1

#*************************************************#


# In[2]:


#**************************************************#

#2. EXAMPLE QUERIES FOR Disease model
#Disease model --> between INFRAFRONTIER, PIDAR, IDR


#*************************************************#


#INSERT THE DISEASE NAME THAT THE USER WANT TO SEARCH
research= 'breast cancer'  

#-----------------------------------#

#Load and Query the PIDAR


data=requests.get("https://pidar.hpc4ai.unito.it/file.json").json()
KEYS = ['disease model'] #add more fields if you want

def find_pidar(model):
    results = []
    for d in data:
        values = []   
        for key, value in d.items():
            k = key.lower().strip()
            if k in KEYS:
                values.append(value.lower().strip())
                if any(model in s for s in values): #and any(species in s for s in values) and any(organ in s for s in values):
                    results.append(d)
    return results

result = find_pidar('breast cancer')
link='https://pidar.hpc4ai.unito.it/?Dropdown=any&search='
end_link='&access=Submit'
l=link+research+end_link
list_of_keys = ['Dataset ID ','Publication title','Publication DOI','Study type','Imaging modality','Institution','Imaging facility','Species','Sample size','Disease model','Organ or Tissue','Status', 'link']
r=[]
for a in result:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('PIDAR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False)))

#-----------------------------------#

#//////////////////////////////////////////////#

#Load and Query the IDR

INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
with requests.Session() as session:
    request = requests.Request('GET', INDEX_PAGE)
    prepped = session.prepare_request(request)
    response = session.send(prepped)
    if response.status_code != 200:
        response.raise_for_status()
SEARCH_URL = "https://idr.openmicroscopy.org/searchengine/api/v1/resources/{type}/search/"
KEY_VALUE_SEARCH = SEARCH_URL + "?key={key}&value={value}"
KEY = "Diagnosis"
value = 'Breast cancer'
qs1 = {'type': 'image', 'key': KEY, 'value': value}
url = KEY_VALUE_SEARCH.format(**qs1)
json = session.get(url).json()['results']['results']
list_of_keys = ['dataset_id','dataset_name','link']
link='https://idr.openmicroscopy.org/search/?key=Diagnosis&value='
end_link='&operator=contains'
l=link+research+end_link
r=[]
for a in json:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('IDR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))
#//////////////////////////////////////////////#

#+++++++++++++++++++++++++++++++#

# Load and Query the INFRAFRONTIER

URL='https://www.infrafrontier.eu/dib/solr/strain_disease/select?q=do_term:'
URL_END='&rows=5000'
query = requests.get(URL+research+URL_END).json()
if (query["response"]["numFound"] != 0):
    req = query["response"]["docs"]
    df = pd.DataFrame(req)
pd.set_option("display.max_rows", None)
print('INFRAFRONTIER RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))

#+++++++++++++++++++++++++++++++#

#print table for Example 2
#end for Example 2

#*************************************************#


# In[130]:


#**************************************************#

#3. EXAMPLE QUERIES FOR SPECIES
#Species--> between INFRAFRONTIER, PIDAR, IDR


#*************************************************#
#INSERT THE SPECIES NAME THAT THE USER WANT SEARCH
research= 'mice'  

#-----------------------------------#

#Load and Query the PIDAR

data=requests.get("https://pidar.hpc4ai.unito.it/file.json").json()
KEYS = ['species'] #add more fields if you want

def find_pidar(model):
    results = []
    for d in data:
        values = []   
        for key, value in d.items():
            k = key.lower().strip()
            if k in KEYS:
                values.append(value.lower().strip())
                if any(model in s for s in values): #and any(species in s for s in values) and any(organ in s for s in values):
                    results.append(d)
    return results

result = find_pidar(research)
link='https://pidar.hpc4ai.unito.it/?Dropdown=any&search='
end_link='&access=Submit'
l=link+research+end_link
list_of_keys = ['Dataset ID ','Publication title','Publication DOI','Study type','Imaging modality','Institution','Imaging facility','Species','Sample size','Disease model','Organ or Tissue','Status', 'link']
r=[]
for a in result:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('PIDAR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False)))
#-----------------------------------#

#//////////////////////////////////////////////#

#Load and Query the IDR

INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
with requests.Session() as session:
    request = requests.Request('GET', INDEX_PAGE)
    prepped = session.prepare_request(request)
    response = session.send(prepped)
    if response.status_code != 200:
        response.raise_for_status()
SEARCH_URL = "https://idr.openmicroscopy.org/searchengine/api/v1/resources/{type}/search/"
KEY_VALUE_SEARCH = SEARCH_URL + "?key={key}&value={value}"
KEY = "Phenotype"
value = 'Mouse'
qs1 = {'type': 'image', 'key': KEY, 'value': value}
url = KEY_VALUE_SEARCH.format(**qs1)
json = session.get(url).json()['results']
list_of_keys = ['screen_id','screen_name','link']
link='https://idr.openmicroscopy.org/search/?key=Diagnosis&value='
end_link='&operator=contains'
l=link+research+end_link
r=[]
for a in json:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('IDR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))

#//////////////////////////////////////////////#


#+++++++++++++++++++++++++++++++#

# Load and Query the INFRAFRONTIER


api_url='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=allele_symbol:'
api_url_two='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=mp_ids:\"'
api_url_three='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=name:'
api_url_end='&rows=200'
query = requests.get(api_url_three+research+api_url_end).json()
if (query["response"]["numFound"] != 0):
    req = query["response"]["docs"]
    df = pd.DataFrame(req)
pd.set_option("display.max_rows", None)
print('INFRAFRONTIER RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))

#+++++++++++++++++++++++++++++++#

#print table for Example 3
#end for Example 3

#*************************************************#


# In[128]:


#**************************************************#

#4. EXAMPLE QUERIES FOR STRAIN
#Strain--> between INFRAFRONTIER, PIDAR, IDR


#*************************************************#
#INSERT THE STRAIN NAME THAT THE USER WANT SEARCH
research= 'balb' 

#-----------------------------------#

#Load and Query the PIDAR

data=requests.get("https://pidar.hpc4ai.unito.it/file.json").json()
KEYS = ['strain'] #add more fields if you want

def find_pidar(model):
    results = []
    for d in data:
        values = []   
        for key, value in d.items():
            k = key.lower().strip()
            if k in KEYS:
                values.append(value.lower().strip())
                if any(model in s for s in values): #and any(species in s for s in values) and any(organ in s for s in values):
                    results.append(d)
    return results

result = find_pidar(research)
link='https://pidar.hpc4ai.unito.it/?Dropdown=any&search='
end_link='&access=Submit'
l=link+research+end_link
list_of_keys = ['Dataset ID ','Publication title','Publication DOI','Study type','Imaging modality','Institution','Imaging facility','Species','Sample size','Disease model','Organ or Tissue','Status', 'link']
r=[]
for a in result:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('PIDAR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False)))
#-----------------------------------#

#///////////////////////////////////#

#Load and Query the IDR

INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
with requests.Session() as session:
    request = requests.Request('GET', INDEX_PAGE)
    prepped = session.prepare_request(request)
    response = session.send(prepped)
    if response.status_code != 200:
        response.raise_for_status()
SEARCH_URL = "https://idr.openmicroscopy.org/searchengine/api/v1/resources/{type}/search/"
KEY_VALUE_SEARCH = SEARCH_URL + "?key={key}&value={value}"
KEY = "Strain"
value = 'Balb/c'
qs1 = {'type': 'image', 'key': KEY, 'value': value}
url = KEY_VALUE_SEARCH.format(**qs1)
json = session.get(url).json()['results']['results']
list_of_keys = ['screen_id','screen_name','link']
link='https://idr.openmicroscopy.org/search/?key=Strain&value='
end_link='&operator=contains'
l=link+research+end_link
r=[]
for a in json:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('IDR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))

#///////////////////////////////////#

#+++++++++++++++++++++++++++++++++++#

#Load and Query the INFRAFRONTIER
api_url='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=allele_symbol:'
api_url_two='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=mp_ids:\"'
api_url_three='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=name:'
api_url_end='&rows=200'
query = requests.get(api_url_three+research+api_url_end).json()
if (query["response"]["numFound"] != 0):
    req = query["response"]["docs"]
    df = pd.DataFrame(req)
pd.set_option("display.max_rows", None)
print('INFRAFRONTIER RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))
#+++++++++++++++++++++++++++++++#

#print table for Example 4
#end for Example 4

#*************************************************#


# In[137]:


#**************************************************#

#5. EXAMPLE QUERIES FOR GENE
#Gene--> between INFRAFRONTIER, PIDAR, IDR


#*************************************************#
#INSERT THE GENE NAME THAT THE USER WANT SEARCH

research= 'CXCR'  #INSERT THE MOLECULE YOU NEED


#++++++++++++++++++++++++++++++++++++++++++++#
#Load and Query the PIDAR

data=requests.get("https://pidar.hpc4ai.unito.it/file.json").json()

def find_pidar(model):
    results = []
    for d in data:
        values = []   
        for key, value in d.items():
            if model in value: #and any(species in s for s in values) and any(organ in s for s in values):
                results.append(d)
    return results

result = find_pidar(research)
link='https://pidar.hpc4ai.unito.it/?Dropdown=any&search='
end_link='&access=Submit'
l=link+research+end_link
list_of_keys = ['Dataset ID ','Publication title','Publication DOI','Study type','Imaging modality','Institution','Imaging facility','Species','Sample size','Disease model','Organ or Tissue','Status', 'link']
r=[]
for a in result:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('PIDAR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False)))
#++++++++++++++++++++++++++++++++++++++++++++#

#/////////////////////////////////////#

#Load and Query the IDR

INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
with requests.Session() as session:
    request = requests.Request('GET', INDEX_PAGE)
    prepped = session.prepare_request(request)
    response = session.send(prepped)
    if response.status_code != 200:
        response.raise_for_status()
SEARCH_URL = "https://idr.openmicroscopy.org/searchengine/api/v1/resources/{type}/search/"
KEY_VALUE_SEARCH = SEARCH_URL + "?key={key}&value={value}"
KEY = "Gene Symbol"
value = 'CXCR4'
qs1 = {'type': 'image', 'key': KEY, 'value': value}
url = KEY_VALUE_SEARCH.format(**qs1)
json = session.get(url).json()['results']['results']
list_of_keys = ['screen_id','screen_name','link']
link='https://idr.openmicroscopy.org/search/?key=Gene+Symbol&value='
end_link='&operator=contains'
l=link+research+end_link
r=[]
for a in json:
    a['link']=l
    small_dictionary = dict(map(lambda key: (key, a.get(key, None)), list_of_keys))
    r.append(small_dictionary)
df = pd.DataFrame(r)
pd.set_option("display.max_rows", None)
print('IDR RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))

#/////////////////////////////////////#

#+++++++++++++++++++++++++++++++++++#

#Load and Query the INFRAFRONTIER

api_url='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=allele_symbol:'
api_url_two='https://www.infrafrontier.eu/dib/solr/strainAPI/select?q=mp_ids:\"'
api_url_three= 'https://www.infrafrontier.eu/dib/solr/strainsearch/select?q=do_term:'
api_url_end='&rows=200'
query = requests.get(api_url_three+research+api_url_end).json()
if (query["response"]["numFound"] != 0):
    req = query["response"]["docs"]
    df = pd.DataFrame(req)
pd.set_option("display.max_rows", None)
print('INFRAFRONTIER RESULTS')
display(HTML(df.to_html(justify="left", index=False, render_links=True, escape=False) ))
#+++++++++++++++++++++++++++++++#

#print table for Example 5
#end for Example 5

#*************************************************#

