This notebook takes a list of gene symbols and queries the IDR for phenotypes associated with the genes in high content screens.
The cell below will install dependencies if you choose to run the notebook in Google Colab.
%pip install idr-py
import csv
import os
import pandas as pd
from tempfile import NamedTemporaryFile
import scipy
import numpy
from skimage import filters
import matplotlib.pyplot as plt
from idr import connection
import requests
import tifffile
INDEX_PAGE = "https://idr.openmicroscopy.org/webclient/?experimenter=-1"
# create http session
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()
or enter directly in list in notebook.
# uncomment the next two lines if you would rather read gene list
# in from a file
# with open('./includes/FiveExampleGenes.txt') as f:
# genes = f.read().splitlines()
# comment out the next line if you have read in the gene list from a file
#genes = ['ASH2L', 'ash2', '85441']
genes = ['CDK5RAP2','CETN2']
# check the gene list has been read in
genes[:5]
['CDK5RAP2', 'CETN2']
URL = "https://idr.openmicroscopy.org/mapr/api/{key}/?value={value}&case_sensitive=false&orphaned=true"
SCREENS_PROJECTS_URL = "https://idr.openmicroscopy.org/mapr/api/{key}/?value={value}"
PLATES_URL = "https://idr.openmicroscopy.org/mapr/api/{key}/plates/?value={value}&id={screen_id}"
DATASETS_URL = "https://idr.openmicroscopy.org/mapr/api/{key}/datasets/?value={value}&id={project_id}"
IMAGES_URL = "https://idr.openmicroscopy.org/mapr/api/{key}/images/?value={value}&node={parent_type}&id={parent_id}"
ATTRIBUTES_URL = "https://idr.openmicroscopy.org/webclient/api/annotations/?type=map&image={image_id}"
For each gene, search of images either in plates or datasets then search for phenotypes associated with the images. The results are saved in a CSV file.
TYPE = "gene"
KEYS = {"phenotype":
("Phenotype",
"Phenotype Term Name",
"Phenotype Term Accession",
"Phenotype Term Accession URL")
}
Parse the output of the json and save it into the CVS file.
def parse_annotation(writer, json_data, name, data_type):
screen_name = "-"
plate_name = "-"
project_name = "-"
dataset_name = "-"
if data_type == 'datasets':
project_name = name
else:
screen_name = name
for p in json_data[data_type]:
parent_id = p['id']
if data_type == 'datasets':
dataset_name = p['name']
else:
plate_name = p['name']
qs3 = {'key': TYPE, 'value': gene,
'parent_type': data_type[:-1], 'parent_id': parent_id}
url3 = IMAGES_URL.format(**qs3)
for i in session.get(url3).json()['images']:
image_id = i['id']
url4 = ATTRIBUTES_URL.format(**{'image_id': image_id})
for a in session.get(url4).json()['annotations']:
ontologies = [] # for ontology terms for a phenotype
row = {}
for v in a['values']:
if str(v[0]) in KEYS['phenotype']:
if str(v[0]) in ['Phenotype']: # has phenotype
row[str(v[0])] = v[1] # so create row
# if there are ontology mappings for the
# phenotype, add them to the ontologies list
ontList = ['Phenotype Term Name',
'Phenotype Term Accession',
'Phenotype Term Accession URL']
if str(v[0]) in ontList:
ontologies.extend([str(v[0]), str(v[1])])
if row:
if (len(ontologies) > 0): # 1+ ontology mapping
row.update({'Gene': gene,
'Screen': screen_name,
'Plate': plate_name,
'Image': image_id,
'Project' : project_name,
'Dataset': dataset_name})
# we have the start of a row now
# but we want to print out as many rows
# as there are ontology mappings
# so if there is mapping to 1 ontology term
# print 1 row, if there are 2 ontology terms
# print 2 rows etc
numberOfRows = len(ontologies)/6
# this is 3 pairs of ontology values per
# mapping, add the ontology mappings and print
n = 1
while (n <= numberOfRows):
row.update({ontologies[0]: ontologies[1],
ontologies[2]: ontologies[3],
ontologies[4]: ontologies[5]})
# remove that set of ontology mappings
ontologies = ontologies[6:]
writer.writerow(row)
n = n + 1
A CSV file is first created in the home
directory. The CSV file can then be downloaded to your local machine. To download it, click File > Open
, select the CSV file and open it, then click File > Download
.
home = os.path.expanduser("~")
csvfile = NamedTemporaryFile("w", delete=False, newline='', dir=home, suffix=".csv")
try:
fieldnames = [
'Gene', 'Screen', 'Plate', 'Project', 'Dataset', 'Image',
'Phenotype', 'Phenotype Term Name', 'Phenotype Term Accession',
'Phenotype Term Accession URL']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
new_genes = []
for g in genes:
qs1 = {'key': TYPE, 'value': g}
url1 = URL.format(**qs1)
json = session.get(url1).json()
for m in json['maps']:
new_genes.append(m['id'])
for gene in new_genes:
qs1 = {'key': TYPE, 'value': gene}
url1 = URL.format(**qs1)
json = session.get(url1).json()
for m in json['maps']:
qs2 = {'key': TYPE, 'value': gene, 'compound_id': m['id']}
url2 = SCREENS_PROJECTS_URL.format(**qs2)
json = session.get(url2).json()
for s in json['screens']:
gene = s['extra']['value']
qs3 = {'key': TYPE, 'value': gene, 'screen_id': s['id']}
url3 = PLATES_URL.format(**qs3)
parse_annotation(writer, session.get(url3).json(), s['name'], 'plates')
for p in json['projects']:
gene = s['extra']['value']
qs3 = {'key': TYPE, 'value': gene, 'project_id': p['id']}
url3 = DATASETS_URL.format(**qs3)
parse_annotation(writer, session.get(url3).json(), p['name'], 'datasets')
finally:
csvfile.close()
View a subset of the data.
df = pd.read_csv(csvfile.name)
df.head(60)
Gene | Screen | Plate | Project | Dataset | Image | Phenotype | Phenotype Term Name | Phenotype Term Accession | Phenotype Term Accession URL | |
---|---|---|---|---|---|---|---|---|---|---|
0 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_04 | - | - | 1554140 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
1 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_04 | - | - | 1554140 | dynamic changes (automatic) | increased variability of nuclear shape in popu... | CMPO_0000345 | http://www.ebi.ac.uk/cmpo/CMPO_0000345 |
2 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_05 | - | - | 1554429 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
3 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_05 | - | - | 1554429 | dynamic changes (automatic) | increased variability of nuclear shape in popu... | CMPO_0000345 | http://www.ebi.ac.uk/cmpo/CMPO_0000345 |
4 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_06 | - | - | 1554686 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
5 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_06 | - | - | 1554686 | dynamic changes (automatic) | increased variability of nuclear shape in popu... | CMPO_0000345 | http://www.ebi.ac.uk/cmpo/CMPO_0000345 |
6 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884816 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
7 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884821 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
8 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884837 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
9 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884813 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
10 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884831 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
11 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884811 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
12 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884815 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
13 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884829 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
14 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884822 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
15 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884814 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
16 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884819 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
17 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884830 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
18 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884826 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
19 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884823 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
20 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884812 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
21 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884827 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
22 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884820 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
23 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884828 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
24 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884833 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
25 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884807 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
26 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884834 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
27 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884825 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
28 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884836 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
29 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884808 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
30 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884835 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
31 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884810 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
32 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884824 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
33 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884818 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
34 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884839 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
35 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884832 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
36 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884809 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
37 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884838 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
38 | CDK5RAP2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CDK5RAP2-C | 1884817 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
39 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884861 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
40 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884859 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
41 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884844 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
42 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884842 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
43 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884858 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
44 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884857 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
45 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884848 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
46 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884852 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
47 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884849 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
48 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884856 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
49 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884845 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
50 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884851 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
51 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884843 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
52 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884850 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
53 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884840 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
54 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884846 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
55 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884841 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
56 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884854 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
57 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884860 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
58 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884853 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
59 | CETN2 | - | - | idr0021-lawo-pericentriolarmaterial/experiment... | CENT2 | 1884855 | protein localized to centrosome | protein localized in centrosome phenotype | CMPO_0000425 | http://www.ebi.ac.uk/cmpo/CMPO_0000425 |
value = 'CMPO_0000357'
df_filtered = df[df['Phenotype Term Accession'] == value]
df_filtered.head()
Gene | Screen | Plate | Project | Dataset | Image | Phenotype | Phenotype Term Name | Phenotype Term Accession | Phenotype Term Accession URL | |
---|---|---|---|---|---|---|---|---|---|---|
0 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_04 | - | - | 1554140 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
2 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_05 | - | - | 1554429 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
4 | CDK5RAP2 | idr0013-neumann-mitocheck/screenA (6) | LT0065_06 | - | - | 1554686 | polylobed (automatic) | polylobed nuclear phenotype | CMPO_0000357 | http://www.ebi.ac.uk/cmpo/CMPO_0000357 |
Select the first image if any
image_id = -1
if ~df_filtered.empty:
image_id = df_filtered.head(1)['Image'].values[0]
else:
image_id = df.head(1)['Image'].values[0]
print(image_id)
1554140
conn = connection('idr.openmicroscopy.org')
Connected to IDR ...
image = conn.getObject("Image", image_id)
print(image.getName(), image.getDescription())
LT0065_04 [Well F24, Field 1]
Load the first plane, pre-process it and segment it.
pixels = image.getPrimaryPixels()
image_plane = pixels.getPlane(0, 0, 0)
filtered = scipy.ndimage.median_filter(image_plane, size=3)
plt.imshow(filtered, cmap='gray')
plt.axis('off')
plt.title('median filtered image')
Text(0.5, 1.0, 'median filtered image')
threshold = filters.threshold_otsu(filtered)
print('Threshold value is {}'.format(threshold))
predicted = numpy.uint8(filtered > threshold) * 255
plt.imshow(predicted, cmap='gray')
plt.axis('off')
plt.title('otsu predicted binary image')
Threshold value is 53
Text(0.5, 1.0, 'otsu predicted binary image')
Save the predicted image as TIFF in the home
directory, the ID of the image as the name of the TIFF file.
To download it, click File > Open
, select the file and open it (ignore the errror in the text editor), then click File > Download
.
name="%s/%s.tif" % (home, image_id)
tifffile.imsave(name, predicted)
conn.close()
To run the exercises below, you will need to be connected to IDR. If you have run the cell in the notebook closing the connection to IDR i.e. conn.close()
, you will first need to
Insert > Cell below
, paste in conn = connection('idr.openmicroscopy.org')
and run the cell.For each exercise, create a new cell by clicking on Insert > Cell below
, write the code then run the cell.
Exercise 1:
Iterate through the df_filtered
data frame to threshold each image and save it as a TIFF file.
See Solution
Copyright (C) 2017-2021 University of Dundee. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.