Mercurial > repos > perssond > quantification
annotate SingleCellDataExtraction.py @ 1:aba3655fdef0 draft
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
author | watsocam |
---|---|
date | Fri, 11 Mar 2022 23:35:52 +0000 |
parents | 928db0f952e3 |
children |
rev | line source |
---|---|
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
1 #Functions for reading in single cell imaging data |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
2 #Joshua Hess |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
3 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
4 #Import necessary modules |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
5 import skimage.io |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
6 import h5py |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
7 import pandas as pd |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
8 import numpy as np |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
9 import os |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
10 import skimage.measure as measure |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
11 import tifffile |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
12 |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
13 from pathlib import Path |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
14 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
15 import sys |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
16 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
17 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
18 def gini_index(mask, intensity): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
19 x = intensity[mask] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
20 sorted_x = np.sort(x) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
21 n = len(x) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
22 cumx = np.cumsum(sorted_x, dtype=float) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
23 return (n + 1 - 2 * np.sum(cumx) / cumx[-1]) / n |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
24 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
25 def intensity_median(mask, intensity): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
26 return np.median(intensity[mask]) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
27 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
28 def MaskChannel(mask_loaded, image_loaded_z, intensity_props=["intensity_mean"]): |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
29 """Function for quantifying a single channel image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
30 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
31 Returns a table with CellID according to the mask and the mean pixel intensity |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
32 for the given channel for each cell""" |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
33 # Look for regionprops in skimage |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
34 builtin_props = set(intensity_props).intersection(measure._regionprops.PROP_VALS) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
35 # Otherwise look for them in this module |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
36 extra_props = set(intensity_props).difference(measure._regionprops.PROP_VALS) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
37 dat = measure.regionprops_table( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
38 mask_loaded, image_loaded_z, |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
39 properties = tuple(builtin_props), |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
40 extra_properties = [globals()[n] for n in extra_props] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
41 ) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
42 return dat |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
43 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
44 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
45 def MaskIDs(mask, mask_props=None): |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
46 """This function will extract the CellIDs and the XY positions for each |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
47 cell based on that cells centroid |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
48 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
49 Returns a dictionary object""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
50 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
51 all_mask_props = set(["label", "centroid", "area", "major_axis_length", "minor_axis_length", "eccentricity", "solidity", "extent", "orientation"]) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
52 if mask_props is not None: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
53 all_mask_props = all_mask_props.union(mask_props) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
54 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
55 dat = measure.regionprops_table( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
56 mask, |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
57 properties=all_mask_props |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
58 ) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
59 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
60 name_map = { |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
61 "CellID": "label", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
62 "X_centroid": "centroid-1", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
63 "Y_centroid": "centroid-0", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
64 "Area": "area", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
65 "MajorAxisLength": "major_axis_length", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
66 "MinorAxisLength": "minor_axis_length", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
67 "Eccentricity": "eccentricity", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
68 "Solidity": "solidity", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
69 "Extent": "extent", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
70 "Orientation": "orientation", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
71 } |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
72 for new_name, old_name in name_map.items(): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
73 dat[new_name] = dat[old_name] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
74 for old_name in set(name_map.values()): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
75 del dat[old_name] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
76 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
77 return dat |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
78 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
79 def n_channels(image): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
80 """Returns the number of channel in the input image. Supports [OME]TIFF and HDF5.""" |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
81 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
82 image_path = Path(image) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
83 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
84 if image_path.suffix in ['.tiff', '.tif', '.btf']: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
85 s = tifffile.TiffFile(image).series[0] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
86 ndim = len(s.shape) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
87 if ndim == 2: return 1 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
88 elif ndim == 3: return min(s.shape) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
89 else: raise Exception('mcquant supports only 2D/3D images.') |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
90 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
91 elif image_path.suffix in ['.h5', '.hdf5']: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
92 f = h5py.File(image, 'r') |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
93 dat_name = list(f.keys())[0] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
94 return f[dat_name].shape[3] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
95 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
96 else: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
97 raise Exception('mcquant currently supports [OME]TIFF and HDF5 formats only') |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
98 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
99 def PrepareData(image,z): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
100 """Function for preparing input for maskzstack function. Connecting function |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
101 to use with mc micro ilastik pipeline""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
102 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
103 image_path = Path(image) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
104 print(f'{image_path} at {z}', file=sys.stderr) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
105 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
106 #Check to see if image tif(f) |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
107 if image_path.suffix in ['.tiff', '.tif', '.btf']: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
108 image_loaded_z = tifffile.imread(image, key=z) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
109 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
110 #Check to see if image is hdf5 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
111 elif image_path.suffix in ['.h5', '.hdf5']: |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
112 #Read the image |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
113 f = h5py.File(image,'r') |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
114 #Get the dataset name from the h5 file |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
115 dat_name = list(f.keys())[0] |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
116 #Retrieve the z^th channel |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
117 image_loaded_z = f[dat_name][0,:,:,z] |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
118 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
119 else: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
120 raise Exception('mcquant currently supports [OME]TIFF and HDF5 formats only') |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
121 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
122 #Return the objects |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
123 return image_loaded_z |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
124 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
125 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
126 def MaskZstack(masks_loaded,image,channel_names_loaded, mask_props=None, intensity_props=["intensity_mean"]): |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
127 """This function will extract the stats for each cell mask through each channel |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
128 in the input image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
129 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
130 mask_loaded: dictionary containing Tiff masks that represents the cells in your image. |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
131 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
132 z_stack: Multichannel z stack image""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
133 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
134 #Get the names of the keys for the masks dictionary |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
135 mask_names = list(masks_loaded.keys()) |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
136 |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
137 #Create empty dictionary to store channel results per mask |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
138 dict_of_chan = {m_name: [] for m_name in mask_names} |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
139 #Get the z channel and the associated channel name from list of channel names |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
140 print(f'channels: {channel_names_loaded}', file=sys.stderr) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
141 print(f'num channels: {len(channel_names_loaded)}', file=sys.stderr) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
142 for z in range(len(channel_names_loaded)): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
143 #Run the data Prep function |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
144 image_loaded_z = PrepareData(image,z) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
145 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
146 #Iterate through number of masks to extract single cell data |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
147 for nm in range(len(mask_names)): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
148 #Use the above information to mask z stack |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
149 dict_of_chan[mask_names[nm]].append( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
150 MaskChannel(masks_loaded[mask_names[nm]],image_loaded_z, intensity_props=intensity_props) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
151 ) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
152 #Print progress |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
153 print("Finished "+str(z)) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
154 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
155 # Column order according to histoCAT convention (Move xy position to end with spatial information) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
156 last_cols = ( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
157 "X_centroid", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
158 "Y_centroid", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
159 "column_centroid", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
160 "row_centroid", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
161 "Area", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
162 "MajorAxisLength", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
163 "MinorAxisLength", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
164 "Eccentricity", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
165 "Solidity", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
166 "Extent", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
167 "Orientation", |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
168 ) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
169 def col_sort(x): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
170 if x == "CellID": |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
171 return -2 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
172 try: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
173 return last_cols.index(x) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
174 except ValueError: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
175 return -1 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
176 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
177 #Iterate through the masks and format quantifications for each mask and property |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
178 for nm in mask_names: |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
179 mask_dict = {} |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
180 # Mean intensity is default property, stored without suffix |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
181 mask_dict.update( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
182 zip(channel_names_loaded, [x["intensity_mean"] for x in dict_of_chan[nm]]) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
183 ) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
184 # All other properties are suffixed with their names |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
185 for prop_n in set(dict_of_chan[nm][0].keys()).difference(["intensity_mean"]): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
186 mask_dict.update( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
187 zip([f"{n}_{prop_n}" for n in channel_names_loaded], [x[prop_n] for x in dict_of_chan[nm]]) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
188 ) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
189 # Get the cell IDs and mask properties |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
190 mask_properties = pd.DataFrame(MaskIDs(masks_loaded[nm], mask_props=mask_props)) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
191 mask_dict.update(mask_properties) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
192 dict_of_chan[nm] = pd.DataFrame(mask_dict).reindex(columns=sorted(mask_dict.keys(), key=col_sort)) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
193 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
194 # Return the dict of dataframes for each mask |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
195 return dict_of_chan |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
196 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
197 def ExtractSingleCells(masks,image,channel_names,output, mask_props=None, intensity_props=["intensity_mean"]): |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
198 """Function for extracting single cell information from input |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
199 path containing single-cell masks, z_stack path, and channel_names path.""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
200 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
201 #Create pathlib object for output |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
202 output = Path(output) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
203 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
204 #Read csv channel names |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
205 channel_names_loaded = pd.read_csv(channel_names) |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
206 #Check for the presence of `marker_name` column |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
207 if 'marker_name' in channel_names_loaded: |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
208 #Get the marker_name column if more than one column (CyCIF structure) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
209 channel_names_loaded_list = list(channel_names_loaded.marker_name) |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
210 #Consider the old one-marker-per-line plain text format |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
211 elif channel_names_loaded.shape[1] == 1: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
212 #re-read the csv file and add column name |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
213 channel_names_loaded = pd.read_csv(channel_names, header = None) |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
214 channel_names_loaded_list = list(channel_names_loaded.iloc[:,0]) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
215 else: |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
216 raise Exception('%s must contain the marker_name column'%channel_names) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
217 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
218 #Contrast against the number of markers in the image |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
219 if len(channel_names_loaded_list) != n_channels(image): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
220 raise Exception("The number of channels in %s doesn't match the image"%channel_names) |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
221 |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
222 #Check for unique marker names -- create new list to store new names |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
223 channel_names_loaded_checked = [] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
224 for idx,val in enumerate(channel_names_loaded_list): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
225 #Check for unique value |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
226 if channel_names_loaded_list.count(val) > 1: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
227 #If unique count greater than one, add suffix |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
228 channel_names_loaded_checked.append(val + "_"+ str(channel_names_loaded_list[:idx].count(val) + 1)) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
229 else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
230 #Otherwise, leave channel name |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
231 channel_names_loaded_checked.append(val) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
232 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
233 #Read the masks |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
234 masks_loaded = {} |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
235 #iterate through mask paths and read images to add to dictionary object |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
236 for m in masks: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
237 m_full_name = os.path.basename(m) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
238 m_name = m_full_name.split('.')[0] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
239 masks_loaded.update({str(m_name):skimage.io.imread(m,plugin='tifffile')}) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
240 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
241 scdata_z = MaskZstack(masks_loaded,image,channel_names_loaded_checked, mask_props=mask_props, intensity_props=intensity_props) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
242 #Write the singe cell data to a csv file using the image name |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
243 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
244 im_full_name = os.path.basename(image) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
245 im_name = im_full_name.split('.')[0] |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
246 |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
247 # iterate through each mask and export csv with mask name as suffix |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
248 for k,v in scdata_z.items(): |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
249 # export the csv for this mask name |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
250 scdata_z[k].to_csv( |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
251 str(Path(os.path.join(str(output), |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
252 str(im_name+"_{}"+".csv").format(k)))), |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
253 index=False |
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
254 ) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
255 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
256 |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
257 def MultiExtractSingleCells(masks,image,channel_names,output, mask_props=None, intensity_props=["intensity_mean"]): |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
258 """Function for iterating over a list of z_stacks and output locations to |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
259 export single-cell data from image masks""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
260 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
261 print("Extracting single-cell data for "+str(image)+'...') |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
262 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
263 #Run the ExtractSingleCells function for this image |
1
aba3655fdef0
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit 897a7dc7cb43e45d6f0fdfe2b2970e59f20f8853"
watsocam
parents:
0
diff
changeset
|
264 ExtractSingleCells(masks,image,channel_names,output, mask_props=mask_props, intensity_props=intensity_props) |
0
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
265 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
266 #Print update |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
267 im_full_name = os.path.basename(image) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
268 im_name = im_full_name.split('.')[0] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
269 print("Finished "+str(im_name)) |