Mercurial > repos > perssond > quantification
annotate SingleCellDataExtraction.py @ 0:928db0f952e3 draft
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
author | perssond |
---|---|
date | Fri, 12 Mar 2021 00:19:24 +0000 |
parents | |
children | aba3655fdef0 |
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 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
11 from pathlib import Path |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
12 import csv |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
13 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
14 import sys |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
15 |
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 def MaskChannel(mask_loaded,image_loaded_z): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
18 """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
|
19 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
20 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
|
21 for the given channel for each cell""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
22 print(f'Mask loaded: {mask_loaded.shape}', file=sys.stderr) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
23 print(f'Image loaded: {image_loaded_z.shape}', file=sys.stderr) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
24 dat = measure.regionprops(mask_loaded, image_loaded_z) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
25 n = len(dat) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
26 intensity_z = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
27 for i in range(n): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
28 intensity_z[i] = dat[i].mean_intensity |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
29 # Clear reference to avoid memory leak -- see MaskIDs for explanation. |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
30 dat[i] = None |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
31 return intensity_z |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
32 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
33 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
34 def MaskIDs(mask): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
35 """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
|
36 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
|
37 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
38 Returns a dictionary object""" |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
39 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
40 dat = measure.regionprops(mask) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
41 n = len(dat) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
42 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
43 # Pre-allocate numpy arrays for all properties we'll calculate. |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
44 labels = np.empty(n, int) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
45 xcoords = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
46 ycoords = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
47 area = np.empty(n, int) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
48 minor_axis_length = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
49 major_axis_length = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
50 eccentricity = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
51 solidity = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
52 extent = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
53 orientation = np.empty(n) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
54 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
55 for i in range(n): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
56 labels[i] = dat[i].label |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
57 xcoords[i] = dat[i].centroid[1] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
58 ycoords[i] = dat[i].centroid[0] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
59 area[i] = dat[i].area |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
60 major_axis_length[i] = dat[i].major_axis_length |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
61 minor_axis_length[i] = dat[i].minor_axis_length |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
62 eccentricity[i] = dat[i].eccentricity |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
63 solidity[i] = dat[i].solidity |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
64 extent[i] = dat[i].extent |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
65 orientation[i] = dat[i].orientation |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
66 # By clearing the reference to each RegionProperties object, we allow it |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
67 # and its cache to be garbage collected immediately. Otherwise memory |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
68 # usage creeps up needlessly while this function is executing. |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
69 dat[i] = None |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
70 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
71 IDs = { |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
72 "CellID": labels, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
73 "X_centroid": xcoords, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
74 "Y_centroid": ycoords, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
75 "column_centroid": xcoords, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
76 "row_centroid": ycoords, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
77 "Area": area, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
78 "MajorAxisLength": major_axis_length, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
79 "MinorAxisLength": minor_axis_length, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
80 "Eccentricity": eccentricity, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
81 "Solidity": solidity, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
82 "Extent": extent, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
83 "Orientation": orientation, |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
84 } |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
85 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
86 return IDs |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
87 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
88 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
89 def PrepareData(image,z): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
90 """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
|
91 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
|
92 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
93 image_path = Path(image) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
94 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
|
95 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
96 #Check to see if image tif(f) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
97 if image_path.suffix == '.tiff' or image_path.suffix == '.tif' or image_path.suffix == '.btf': |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
98 #Check to see if the image is ome.tif(f) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
99 if image.endswith(('.ome.tif','.ome.tiff')): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
100 #Read the image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
101 image_loaded_z = skimage.io.imread(image,img_num=z,plugin='tifffile') |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
102 #print('OME TIF(F) found') |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
103 else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
104 #Read the image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
105 image_loaded_z = skimage.io.imread(image,img_num=z,plugin='tifffile') |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
106 #print('TIF(F) found') |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
107 # Remove extra axis |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
108 #image_loaded = image_loaded.reshape((image_loaded.shape[1],image_loaded.shape[3],image_loaded.shape[4])) |
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 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
111 elif image_path.suffix == '.h5' or image_path.suffix == '.hdf5': |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
112 #Read the image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
113 f = h5py.File(image,'r+') |
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] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
116 ###If the hdf5 is exported from ilastik fiji plugin, the dat_name will be 'data' |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
117 #Get the image data |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
118 image_loaded = np.array(f[dat_name]) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
119 #Remove the first axis (ilastik convention) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
120 image_loaded = image_loaded.reshape((image_loaded.shape[1],image_loaded.shape[2],image_loaded.shape[3])) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
121 ###If the hdf5 is exported from ilastik fiji plugin, the order will need to be |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
122 ###switched as above --> z_stack = np.swapaxes(z_stack,0,2) --> z_stack = np.swapaxes(z_stack,0,1) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
123 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
124 #Return the objects |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
125 return image_loaded_z |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
126 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
127 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
128 def MaskZstack(masks_loaded,image,channel_names_loaded): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
129 """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
|
130 in the input 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 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
|
133 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
134 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
|
135 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
136 #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
|
137 mask_names = list(masks_loaded.keys()) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
138 #Get the CellIDs for this dataset by using only a single mask (first mask) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
139 IDs = pd.DataFrame(MaskIDs(masks_loaded[mask_names[0]])) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
140 #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
|
141 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
|
142 #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
|
143 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
|
144 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
|
145 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
|
146 #Run the data Prep function |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
147 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
|
148 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
149 #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
|
150 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
|
151 #Use the above information to mask z stack |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
152 dict_of_chan[mask_names[nm]].append(MaskChannel(masks_loaded[mask_names[nm]],image_loaded_z)) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
153 #Print progress |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
154 print("Finished "+str(z)) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
155 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
156 #Iterate through the rest of the masks to modify names of channels and convert to data table |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
157 for nm in mask_names: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
158 #Check if this is the first mask |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
159 if nm == mask_names[0]: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
160 #Create channel names for this mask |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
161 new_names = [channel_names_loaded[i]+"_"+str(nm) for i 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
|
162 #Convert the channel names list and the list of intensity values to a dictionary and combine with CellIDs and XY |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
163 dict_of_chan[nm] = pd.concat([IDs,pd.DataFrame(dict(zip(new_names,dict_of_chan[nm])))],axis=1) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
164 #Get the name of the columns in the dataframe so we can reorder to histoCAT convention |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
165 cols = list(dict_of_chan[nm].columns.values) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
166 #Reorder the list (Move xy position to end with spatial information) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
167 cols.append(cols.pop(cols.index("X_centroid"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
168 cols.append(cols.pop(cols.index("Y_centroid"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
169 cols.append(cols.pop(cols.index("column_centroid"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
170 cols.append(cols.pop(cols.index("row_centroid"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
171 cols.append(cols.pop(cols.index("Area"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
172 cols.append(cols.pop(cols.index("MajorAxisLength"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
173 cols.append(cols.pop(cols.index("MinorAxisLength"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
174 cols.append(cols.pop(cols.index("Eccentricity"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
175 cols.append(cols.pop(cols.index("Solidity"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
176 cols.append(cols.pop(cols.index("Extent"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
177 cols.append(cols.pop(cols.index("Orientation"))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
178 #Reindex the dataframe with new order |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
179 dict_of_chan[nm] = dict_of_chan[nm].reindex(columns=cols) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
180 #Otherwise, add no spatial information |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
181 else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
182 #Create channel names for this mask |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
183 new_names = [channel_names_loaded[i]+"_"+str(nm) for i 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
|
184 #Use the above information to mask z stack |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
185 dict_of_chan[nm] = pd.DataFrame(dict(zip(new_names,dict_of_chan[nm]))) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
186 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
187 #Concatenate all data from all masks to return |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
188 dat = pd.concat([dict_of_chan[nm] for nm in mask_names],axis=1) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
189 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
190 #Return the dataframe |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
191 return dat |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
192 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
193 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
194 def ExtractSingleCells(masks,image,channel_names,output): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
195 """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
|
196 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
|
197 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
198 #Create pathlib object for output |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
199 output = Path(output) |
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 #Check if header available |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
202 #sniffer = csv.Sniffer() |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
203 #sniffer.has_header(open(channel_names).readline()) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
204 #If header not available |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
205 #if not sniffer: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
206 #If header available |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
207 #channel_names_loaded = pd.read_csv(channel_names) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
208 #channel_names_loaded_list = list(channel_names_loaded.marker_name) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
209 #else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
210 #print("negative") |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
211 #old one column version |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
212 #channel_names_loaded = pd.read_csv(channel_names,header=None) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
213 #Add a column index for ease |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
214 #channel_names_loaded.columns = ["marker"] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
215 #channel_names_loaded = list(channel_names_loaded.marker.values) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
216 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
217 #Read csv channel names |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
218 channel_names_loaded = pd.read_csv(channel_names) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
219 #Check for size of columns |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
220 if channel_names_loaded.shape[1] > 1: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
221 #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
|
222 channel_names_loaded_list = list(channel_names_loaded.marker_name) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
223 else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
224 #old one column version -- re-read the csv file and add column name |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
225 channel_names_loaded = pd.read_csv(channel_names, header = None) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
226 #Add a column index for ease and for standardization |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
227 channel_names_loaded.columns = ["marker"] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
228 channel_names_loaded_list = list(channel_names_loaded.marker) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
229 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
230 #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
|
231 channel_names_loaded_checked = [] |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
232 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
|
233 #Check for unique value |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
234 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
|
235 #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
|
236 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
|
237 else: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
238 #Otherwise, leave channel name |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
239 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
|
240 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
241 #Clear small memory amount by clearing old channel names |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
242 channel_names_loaded, channel_names_loaded_list = None, None |
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 #Read the masks |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
245 masks_loaded = {} |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
246 #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
|
247 for m in masks: |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
248 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
|
249 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
|
250 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
|
251 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
252 scdata_z = MaskZstack(masks_loaded,image,channel_names_loaded_checked) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
253 #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
|
254 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
255 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
|
256 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
|
257 scdata_z.to_csv(str(Path(os.path.join(str(output),str(im_name+".csv")))),index=False) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
258 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
259 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
260 def MultiExtractSingleCells(masks,image,channel_names,output): |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
261 """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
|
262 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
|
263 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
264 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
|
265 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
266 #Run the ExtractSingleCells function for this image |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
267 ExtractSingleCells(masks,image,channel_names,output) |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
268 |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
269 #Print update |
928db0f952e3
"planemo upload for repository https://github.com/ohsu-comp-bio/quantification commit a4349062e9177b5e60fb7c49115c57299e0d648d-dirty"
perssond
parents:
diff
changeset
|
270 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
|
271 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
|
272 print("Finished "+str(im_name)) |