This notebook uses the idr0021 dataset (idr0021-lawo-pericentriolarmaterial/experimentA) and tries to reproduce some of the analysis published in 'Subdiffraction imaging of centrosomes reveals higher-order organizational features of pericentriolar material'; in particular to create a figure similar to Figure 1 of the article.
The grey blocks with the square brackets to the left are code blocks. Click on a block to select it and click the play button in the toolbar to execute the block of code (alternatively hold SHIFT key and hit ENTER). The bracket will show a star to indicate that this code is currently running. The output is displayed below the block. Wait for it to finish. When the execution is finished the star will be replaced by a number.
As we go through the notebook you are expected to run the code blocks in order.
Here's an example:
message("Just wait for a bit...")
Sys.sleep(3)
message("Done.")
Just wait for a bit... Done.
You can change the code and run the block again.
Load the necessary libraries:
# Load the libraries
library(romero.gateway)
library(EBImage, warn.conflicts = FALSE)
Loading required package: rJava Loading required package: jpeg Loading required package: httr *** Welcome to rOMERO 0.4.8 ***
server <- OMEROServer(host = 'wss://idr.openmicroscopy.org/omero-ws', port = 443L, username='public', password='public')
server <- connect(server)
paste('Successfully logged in as', server@user$getUserName())
imageId <- 1884839
image <- loadObject(server, "ImageData", imageId)
paste("Image", imageId, "loaded.")
Load the pixel values and display the image
# There is just one plane, so z = 1 and t = 1
z <- 1
t <- 1
# Load the second channel
channelIndex <- 2
pixels <- getPixelValues(image, z, t, channelIndex)
ebimage <- EBImage::Image(data = pixels, colormode = 'Grayscale')
ebimage <- normalize(ebimage)
EBImage::display(ebimage)
We are using four steps for the segmentation:
Note: Up till now we still had a binary image (the pixel array only consists of 0's (background) and 1's (objects)), therefore we need:
# Threshold
threshImg <- thresh(ebimage, w=10, h=10, offset=0.01)
# Remove noise
threshImg <- medianFilter(threshImg, size=3)
# Fill holes
threshImg <- fillHull(threshImg)
# Distinguish objects
threshImg <- bwlabel(threshImg)
# Show the segmented image
EBImage::display(colorLabels(threshImg))
Exercise: Modify the parameters of the thresh function w, h and offset to get a better segmentation. Goal: The two centrioles should be detected as two separate objects. We don't have to get rid off all the noise, but the two centrioles should be the largest objects detected. (Hint: w=15, h=15, offset=0.1 seems to work pretty well)
Use the computeFeatures methods to calculate some properties of the objects.
shapes = computeFeatures.shape(threshImg)
moments = computeFeatures.moment(threshImg)
# merge the two dataframes together into one 'features' dataframe
features <- merge(shapes, moments, by=0, all=TRUE)
features
Row.names | s.area | s.perimeter | s.radius.mean | s.radius.sd | s.radius.min | s.radius.max | m.cx | m.cy | m.majoraxis | m.eccentricity | m.theta |
---|---|---|---|---|---|---|---|---|---|---|---|
<I<chr>> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> |
1 | 1 | 1 | 0.0000000 | 0.0000000 | 0.0000000 | 0.0000000 | 21.000000 | 1.000000 | 0.000000 | 0.0000000 | 0.000000e+00 |
10 | 8 | 6 | 1.2453560 | 0.1800605 | 1.1180340 | 1.5000000 | 137.000000 | 8.500000 | 3.464102 | 0.5773503 | 1.570796e+00 |
100 | 29 | 15 | 2.6006209 | 0.2335571 | 2.1868293 | 2.9522120 | 242.413793 | 106.724138 | 6.278955 | 0.3757493 | 5.122576e-01 |
101 | 12 | 10 | 1.5340525 | 0.4203366 | 0.9848858 | 2.4020824 | 222.083333 | 107.416667 | 5.044025 | 0.8038460 | -1.454864e+00 |
102 | 5 | 5 | 0.8478015 | 0.2850134 | 0.4472136 | 1.2649111 | 93.600000 | 109.800000 | 3.098387 | 0.8164966 | 1.249046e+00 |
103 | 9 | 7 | 1.3005843 | 0.2521557 | 0.9035079 | 1.7202278 | 2.333333 | 111.111111 | 3.885753 | 0.6945875 | -3.543131e-01 |
104 | 6 | 7 | 1.0339719 | 0.4429541 | 0.4040610 | 1.7379322 | 85.166667 | 113.666667 | 4.363338 | 0.9252742 | 2.257265e-01 |
105 | 5 | 5 | 0.8478015 | 0.2850134 | 0.4472136 | 1.2649111 | 3.200000 | 114.400000 | 3.098387 | 0.8164966 | 3.217506e-01 |
106 | 25 | 18 | 2.5288553 | 1.0393265 | 0.7243558 | 4.1462463 | 65.120000 | 118.320000 | 9.384197 | 0.9146676 | -1.162370e+00 |
107 | 1 | 1 | 0.0000000 | 0.0000000 | 0.0000000 | 0.0000000 | 138.000000 | 115.000000 | 0.000000 | 0.0000000 | 0.000000e+00 |
108 | 125 | 52 | 7.0264244 | 3.0934525 | 0.5737090 | 12.7785242 | 173.088000 | 120.440000 | 23.793945 | 0.8909881 | -1.277904e-01 |
109 | 3 | 4 | 0.5000000 | 0.5000000 | 0.0000000 | 1.0000000 | 125.000000 | 117.000000 | 3.265986 | 1.0000000 | 1.570796e+00 |
11 | 7 | 6 | 1.0853143 | 0.2105114 | 0.6666667 | 1.3333333 | 105.000000 | 8.714286 | 3.023716 | 0.3779645 | 1.392664e-12 |
110 | 20 | 13 | 2.0855466 | 0.4799761 | 1.3434038 | 2.7798940 | 52.650000 | 119.550000 | 6.331371 | 0.7829373 | 1.375404e+00 |
111 | 4 | 4 | 0.7071068 | 0.0000000 | 0.7071068 | 0.7071068 | 77.500000 | 118.500000 | 2.000000 | 0.0000000 | 0.000000e+00 |
112 | 126 | 46 | 6.6612103 | 2.7215999 | 2.2271736 | 11.5910141 | 26.404762 | 127.373016 | 23.206100 | 0.9420899 | 1.154526e+00 |
113 | 61 | 27 | 4.1374647 | 1.3212051 | 1.4229165 | 5.9835165 | 86.983607 | 127.114754 | 13.521108 | 0.8857414 | 8.341317e-01 |
114 | 4 | 4 | 0.7653882 | 0.3188744 | 0.2500000 | 1.0307764 | 64.250000 | 125.000000 | 2.828427 | 0.7905694 | 1.570796e+00 |
115 | 53 | 28 | 3.8373751 | 1.1689266 | 1.4069797 | 5.7178560 | 99.377358 | 129.509434 | 11.903533 | 0.8372610 | -1.551983e+00 |
116 | 5 | 5 | 0.8478015 | 0.2850134 | 0.4472136 | 1.2649111 | 193.600000 | 125.800000 | 3.098387 | 0.8164966 | 1.249046e+00 |
117 | 25 | 15 | 2.3563785 | 0.3443711 | 1.7499206 | 2.9881246 | 41.840000 | 128.400000 | 6.077523 | 0.4936818 | 1.214324e+00 |
118 | 154 | 41 | 6.5404900 | 0.6453818 | 5.0073711 | 7.7836984 | 122.902597 | 132.772727 | 14.438788 | 0.2647317 | -7.050784e-01 |
119 | 94 | 45 | 5.7103309 | 2.2875988 | 1.3916861 | 9.4400513 | 69.170213 | 134.670213 | 19.403243 | 0.9129808 | -1.276544e+00 |
12 | 4 | 4 | 0.7071068 | 0.0000000 | 0.7071068 | 0.7071068 | 140.500000 | 8.500000 | 2.000000 | 0.0000000 | 0.000000e+00 |
120 | 15 | 12 | 1.7752951 | 0.7127759 | 0.6508541 | 2.5671536 | 185.600000 | 131.733333 | 6.444743 | 0.8681009 | 1.557163e+00 |
121 | 7 | 7 | 1.0615668 | 0.4233184 | 0.5150788 | 1.7670453 | 172.428571 | 131.714286 | 4.155375 | 0.8886656 | 1.425068e+00 |
122 | 101 | 41 | 5.1606418 | 1.6648429 | 1.1628290 | 8.1621360 | 199.653465 | 137.138614 | 15.652704 | 0.7887905 | -9.800267e-01 |
123 | 59 | 28 | 4.1580734 | 1.4445419 | 1.6525336 | 6.6129318 | 225.525424 | 135.576271 | 14.019717 | 0.9162400 | -4.435859e-01 |
124 | 35 | 23 | 3.1608687 | 1.3726418 | 0.5849402 | 5.0211274 | 158.600000 | 137.571429 | 12.141917 | 0.9318193 | 3.777620e-01 |
125 | 35 | 20 | 2.8523443 | 0.5604747 | 1.5000000 | 3.8275318 | 17.657143 | 140.171429 | 7.621507 | 0.5835851 | -5.331956e-01 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
72 | 8 | 8 | 1.1955252 | 0.5523537 | 0.3750000 | 2.0348526 | 60.375000 | 80.000000 | 4.898979 | 0.9185587 | 1.57079633 |
73 | 7 | 7 | 1.1225971 | 0.4573440 | 0.3194383 | 1.7202278 | 64.714286 | 78.857143 | 4.375345 | 0.8785816 | -0.39269908 |
74 | 56 | 29 | 3.8606131 | 1.7929229 | 0.7779665 | 7.0013588 | 148.964286 | 83.178571 | 14.975695 | 0.9292147 | -0.21560767 |
75 | 4 | 5 | 0.7476460 | 0.4012798 | 0.2828427 | 1.2165525 | 80.250000 | 81.750000 | 3.464102 | 0.9128709 | -0.32175055 |
76 | 16 | 13 | 1.8689205 | 0.6235326 | 0.9003615 | 2.8221441 | 255.125000 | 83.750000 | 6.602898 | 0.8888697 | -1.43862511 |
77 | 10 | 10 | 1.3718347 | 0.6066873 | 0.5000000 | 2.0615528 | 1.500000 | 84.000000 | 5.656854 | 0.9354143 | 1.57079633 |
78 | 4 | 4 | 0.7071068 | 0.0000000 | 0.7071068 | 0.7071068 | 12.500000 | 82.500000 | 2.000000 | 0.0000000 | 0.00000000 |
79 | 2 | 2 | 0.5000000 | 0.0000000 | 0.5000000 | 0.5000000 | 77.000000 | 82.500000 | 2.000000 | 1.0000000 | 1.57079633 |
8 | 72 | 36 | 4.7881843 | 2.2128248 | 1.0456406 | 8.3721780 | 81.625000 | 8.763889 | 18.931991 | 0.9533509 | 0.05236656 |
80 | 45 | 21 | 3.3575135 | 0.7775952 | 1.9219463 | 4.7487915 | 201.266667 | 86.377778 | 9.791060 | 0.7937288 | 1.07079965 |
81 | 16 | 10 | 1.8594157 | 0.3906061 | 1.3000000 | 2.5079872 | 175.250000 | 88.500000 | 5.291503 | 0.6813851 | 1.57079633 |
82 | 62 | 26 | 4.1787711 | 1.1795857 | 2.3923572 | 6.1231204 | 26.709677 | 90.612903 | 12.853083 | 0.8702648 | -0.48501180 |
83 | 16 | 12 | 1.9057713 | 0.6454820 | 0.8374896 | 3.1413461 | 233.000000 | 88.812500 | 6.518245 | 0.8751080 | 0.46287927 |
84 | 5 | 5 | 0.8478015 | 0.2850134 | 0.4472136 | 1.2649111 | 70.400000 | 90.200000 | 3.098387 | 0.8164966 | 1.24904577 |
85 | 65 | 24 | 4.1049409 | 0.5789852 | 2.9050363 | 4.9520268 | 90.015385 | 95.338462 | 10.028608 | 0.5319533 | 1.55642329 |
86 | 19 | 13 | 2.1923756 | 0.5843101 | 1.2427303 | 3.1994822 | 137.473684 | 94.631579 | 6.545604 | 0.7755072 | -1.01722197 |
87 | 43 | 24 | 3.3180849 | 0.9695181 | 1.3572081 | 5.0091929 | 145.093023 | 95.302326 | 9.055612 | 0.6102557 | 0.18435997 |
88 | 9 | 8 | 1.2855876 | 0.3796570 | 0.7603453 | 2.0194368 | 166.222222 | 94.111111 | 4.312594 | 0.7869526 | 1.05560791 |
89 | 3 | 4 | 0.5000000 | 0.5000000 | 0.0000000 | 1.0000000 | 217.000000 | 94.000000 | 3.265986 | 1.0000000 | 1.57079633 |
9 | 17 | 11 | 1.9631700 | 0.3597704 | 1.3876670 | 2.5258989 | 49.176471 | 9.235294 | 5.674635 | 0.7432143 | 0.69463622 |
90 | 1 | 1 | 0.0000000 | 0.0000000 | 0.0000000 | 0.0000000 | 237.000000 | 94.000000 | 0.000000 | 0.0000000 | 0.00000000 |
91 | 24 | 19 | 2.5805675 | 1.2343001 | 0.6698906 | 4.3378767 | 203.750000 | 98.416667 | 10.456258 | 0.9294198 | -0.54041950 |
92 | 132 | 36 | 6.1224486 | 1.1098073 | 4.3212081 | 8.4856451 | 117.469697 | 101.416667 | 16.125134 | 0.7502212 | -0.18369661 |
93 | 26 | 15 | 2.4549039 | 0.5334397 | 1.4907120 | 3.3499585 | 221.307692 | 98.615385 | 7.107075 | 0.7524093 | 0.72732735 |
94 | 10 | 9 | 1.3405445 | 0.3918643 | 0.7027284 | 1.8392162 | 49.200000 | 98.300000 | 4.541385 | 0.7917414 | 1.25275858 |
95 | 104 | 37 | 5.8113223 | 1.9295219 | 2.9071334 | 9.2586011 | 176.586538 | 102.182692 | 18.508058 | 0.9160405 | 0.44578688 |
96 | 1 | 1 | 0.0000000 | 0.0000000 | 0.0000000 | 0.0000000 | 45.000000 | 98.000000 | 0.000000 | 0.0000000 | 0.00000000 |
97 | 62 | 32 | 4.3194596 | 1.4672476 | 1.4149039 | 7.1415652 | 39.290323 | 102.870968 | 12.819935 | 0.8216759 | 0.74604555 |
98 | 42 | 20 | 3.2406474 | 0.6109047 | 1.9557607 | 4.2690748 | 22.714286 | 104.904762 | 8.356337 | 0.5740744 | 1.11550492 |
99 | 11 | 9 | 1.4168372 | 0.3022216 | 0.9493337 | 1.8888889 | 2.363636 | 105.090909 | 4.346130 | 0.7084438 | 0.26303321 |
For further analysis we need the object sizes: 's.area', 's.perimeter', especially 's.radius.mean' (~ diameter).
For the visualisation we can use the location (m.cx, m.mcy), the major radius (m.majoraxis), the minor radius (can be calculated from m.eccentricity) and the rotation angle (m.theta) to draw an Ellipse ROI around each object.
In order to be able to re-use this code later, we define a function for it:
#' Create ROIs from a features table
#'
#' @param features The shape and moments generated by EBImage computeFeatures
#' @return A dataframe specifying the x, y, rx, ry, w, h, theta and text parameters of the ROIs
createROIs <- function(features) {
rois <- data.frame(x = c(0), y = c(0), rx = c(0), ry = c(0), w = c(0), h = c(0), theta = c(0), text = c('remove'), stringsAsFactors = FALSE)
for (index in 1:length(features[,1])) {
x <- features[index,8]
y <- features[index,9]
r1 <- features[index,10]
ecc <- features[index,11]
r2 <- sqrt(r1^2 * (1 - ecc^2))
theta <- features[index,12]
rois <- rbind(rois, c(x, y, r1, r2, NA, NA, -theta, as.character(index)))
}
rois <- rois[-1,]
rownames(rois) <- c()
return(rois)
}
print("Function 'createROIs' defined.")
[1] "Function 'createROIs' defined."
rois <- createROIs(features)
rois
x | y | rx | ry | w | h | theta | text |
---|---|---|---|---|---|---|---|
<chr> | <chr> | <chr> | <chr> | <chr> | <chr> | <chr> | <chr> |
21 | 1 | 0 | 0 | NA | NA | 0 | 1 |
137 | 8.5 | 3.46410161513775 | 2.82842712474619 | NA | NA | -1.5707963267949 | 2 |
242.413793103448 | 106.724137931034 | 6.27895460943305 | 5.81884133707928 | NA | NA | -0.51225761447762 | 3 |
222.083333333333 | 107.416666666667 | 5.04402536508443 | 3.00037540331586 | NA | NA | 1.45486399636712 | 4 |
93.6 | 109.8 | 3.09838667697063 | 1.78885438200471 | NA | NA | -1.24904577239644 | 5 |
2.33333333333333 | 111.111111111111 | 3.88575337505748 | 2.79544289069596 | NA | NA | 0.354313136063922 | 6 |
85.1666666666667 | 113.666666666667 | 4.36333821343559 | 1.65500979239979 | NA | NA | -0.225726545776959 | 7 |
3.2 | 114.4 | 3.09838667696579 | 1.78885438199909 | NA | NA | -0.321750554396375 | 8 |
65.12 | 118.32 | 9.38419708654179 | 3.79315766096677 | NA | NA | 1.16236988732183 | 9 |
138 | 115 | 0 | 0 | NA | NA | 0 | 10 |
173.088 | 120.44 | 23.7939454615514 | 10.8030854561425 | NA | NA | 0.12779037477232 | 11 |
125 | 117 | 3.26598632370942 | 0 | NA | NA | -1.5707963267949 | 12 |
105 | 8.71428571428571 | 3.02371578407588 | 2.79941684889511 | NA | NA | -1.39266376207678e-12 | 13 |
52.65 | 119.55 | 6.33137050565772 | 3.93874948684139 | NA | NA | -1.37540426235772 | 14 |
77.5 | 118.5 | 2 | 2 | NA | NA | 0 | 15 |
26.4047619047619 | 127.373015873016 | 23.2061002122394 | 7.782406582729 | NA | NA | -1.1545263939604 | 16 |
86.983606557377 | 127.114754098361 | 13.5211075134196 | 6.27621583041621 | NA | NA | -0.834131741479452 | 17 |
64.25 | 125 | 2.82842712474619 | 1.73205080756888 | NA | NA | -1.5707963267949 | 18 |
99.377358490566 | 129.509433962264 | 11.9035333632372 | 6.50889400981891 | NA | NA | 1.55198276136682 | 19 |
193.6 | 125.8 | 3.09838667697251 | 1.78885438198519 | NA | NA | -1.24904577239553 | 20 |
41.84 | 128.4 | 6.07752323323418 | 5.28527306290434 | NA | NA | -1.21432404314039 | 21 |
122.902597402597 | 132.772727272727 | 14.4387882464185 | 13.9236427260626 | NA | NA | 0.705078386886165 | 22 |
69.1702127659574 | 134.670212765957 | 19.4032431949018 | 7.91657397870116 | NA | NA | 1.27654376675037 | 23 |
140.5 | 8.5 | 2 | 2 | NA | NA | 0 | 24 |
185.6 | 131.733333333333 | 6.44474250760075 | 3.19909094895142 | NA | NA | -1.55716334256621 | 25 |
172.428571428571 | 131.714285714286 | 4.15537470567399 | 1.90547113310948 | NA | NA | -1.42506792955351 | 26 |
199.653465346535 | 137.138613861386 | 15.6527036589454 | 9.62112505195178 | NA | NA | 0.980026746601844 | 27 |
225.525423728814 | 135.576271186441 | 14.0197171239129 | 5.61671639288455 | NA | NA | 0.443585897677941 | 28 |
158.6 | 137.571428571429 | 12.1419173788632 | 4.4065773563478 | NA | NA | -0.37776199377028 | 29 |
17.6571428571429 | 140.171428571429 | 7.62150712713029 | 6.18906017467537 | NA | NA | 0.533195579740987 | 30 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
60.375 | 80 | 4.89897948556636 | 1.93649167310371 | NA | NA | -1.5707963267949 | 211 |
64.7142857142857 | 78.8571428571429 | 4.37534528542514 | 2.0896309997102 | NA | NA | 0.392699081698376 | 212 |
148.964285714286 | 83.1785714285714 | 14.9756950614374 | 5.53412293055224 | NA | NA | 0.215607668541409 | 213 |
80.25 | 81.75 | 3.46410161513775 | 1.4142135623731 | NA | NA | 0.321750554396642 | 214 |
255.125 | 83.75 | 6.60289771671258 | 3.02518457992762 | NA | NA | 1.43862510691498 | 215 |
1.5 | 84 | 5.65685424949238 | 2 | NA | NA | -1.5707963267949 | 216 |
12.5 | 82.5 | 2 | 2 | NA | NA | 0 | 217 |
77 | 82.5 | 2 | 0 | NA | NA | -1.5707963267949 | 218 |
81.625 | 8.76388888888889 | 18.931990912656 | 5.71488993740865 | NA | NA | -0.0523665611700603 | 219 |
201.266666666667 | 86.3777777777778 | 9.79105964533295 | 5.95562524082737 | NA | NA | -1.07079964556212 | 220 |
175.25 | 88.5 | 5.29150262212918 | 3.87298334620742 | NA | NA | -1.5707963267949 | 221 |
26.7096774193548 | 90.6129032258064 | 12.8530828759802 | 6.331225855466 | NA | NA | 0.485011795524207 | 222 |
233 | 88.8125 | 6.51824510191157 | 3.15435901434917 | NA | NA | -0.462879265387388 | 223 |
70.4 | 90.2 | 3.09838667696288 | 1.7888543819978 | NA | NA | -1.24904577240076 | 224 |
90.0153846153846 | 95.3384615384615 | 10.0286080531355 | 8.49196155485219 | NA | NA | -1.55642329028852 | 225 |
137.473684210526 | 94.6315789473684 | 6.54560422017892 | 4.13249356513248 | NA | NA | 1.01722196789785 | 226 |
145.093023255814 | 95.3023255813954 | 9.05561193325286 | 7.17389675306088 | NA | NA | -0.184359965360197 | 227 |
166.222222222222 | 94.1111111111111 | 4.31259446356955 | 2.66092866090112 | NA | NA | -1.05560791353491 | 228 |
217 | 94 | 3.26598632370942 | 0 | NA | NA | -1.5707963267949 | 229 |
49.1764705882353 | 9.23529411764706 | 5.67463535152451 | 3.79663411224214 | NA | NA | -0.694636224968552 | 230 |
237 | 94 | 0 | 0 | NA | NA | 0 | 231 |
203.75 | 98.4166666666667 | 10.4562580942408 | 3.85861230092796 | NA | NA | 0.540419500270439 | 232 |
117.469696969697 | 101.416666666667 | 16.1251337745639 | 10.6617277718554 | NA | NA | 0.183696611424955 | 233 |
221.307692307692 | 98.6153846153846 | 7.10707545178759 | 4.68140086144037 | NA | NA | -0.727327349958734 | 234 |
49.2 | 98.3 | 4.5413850547356 | 2.77413442079348 | NA | NA | -1.25275857544377 | 235 |
176.586538461538 | 102.182692307692 | 18.5080575660813 | 7.42331807782222 | NA | NA | -0.445786884774115 | 236 |
45 | 98 | 0 | 0 | NA | NA | 0 | 237 |
39.2903225806452 | 102.870967741935 | 12.8199348611956 | 7.3067861931975 | NA | NA | -0.746045546162609 | 238 |
22.7142857142857 | 104.904761904762 | 8.35633677062996 | 6.8421945825549 | NA | NA | -1.11550492490951 | 239 |
2.36363636363636 | 105.090909090909 | 4.34612998414177 | 3.06735632460415 | NA | NA | -0.263033209498027 | 240 |
Tasks:
Note: We need to be able to specify the channel. Only one channel in each image is relevant for our analysis. The relevant channel is identified by its name.
We put all the pieces together and wrap them up in a function called 'analyzeImage':
#' Performs an image segmentation to find the largest ROI of the image
#' and returns some features of interest (area, perimeter and diameter).
#' Optionally: Creates an ROI for each object detected by the segmentation.
#'
#' @param image The image to segment
#' @param channelName The channel to be taken into account
#' @param df The dataframe to add the features to (channelName, imageName, ImageID, ROIIndex, area, perimeter, diameter)
#' @return The dataframe with the features
analyzeImage <- function(image, channelName, df) {
# Find the channel index
chnames <- getChannelNames(image)
chindex <- match(channelName, chnames, nomatch = 0)
if (chindex == 0) {
message (paste("Could not resolve channel name, skipping ", image@dataobject$getId()))
return(df)
}
# Load the pixels
pixels <- getPixelValues(image, 1, 1, chindex)
ebi <- EBImage::Image(data = pixels, colormode = 'Grayscale')
ebi <- normalize(ebi)
# this is our segmentation workflow from above
threshImg <- thresh(ebi, w=15, h=15, offset=0.1)
threshImg <- medianFilter(threshImg, size=3)
threshImg <- fillHull(threshImg)
threshImg <- bwlabel(threshImg)
# Calculate the features
shapes = suppressMessages(computeFeatures.shape(threshImg))
moments = suppressMessages(computeFeatures.moment(threshImg))
features <- merge(shapes, moments, by=0, all=TRUE)
if (length(features[,1])>1) {
# Add the ROIs to the image
rois <- createROIs(features)
# Add the interesting properties (area, perimeter and diameter)
# of the largest object together with channel name, image name, image id
# and roi index to the dataframe
features <- features[order(-features[,2]),]
diameter <- features[1,4]*2
df <- rbind(df, c(channelName, image@dataobject$getName(), image@dataobject$getId(), features[1,1], features[1,2], features[1,3], diameter))
}
return(df)
}
print("Function 'analyzeImage' defined.")
[1] "Function 'analyzeImage' defined."
Iterate over the dataset and call the analyze function for each of the images:
datasetId <- 51
channelName <- 'CDK5RAP2-C'
dataset <- loadObject(server, "DatasetData", datasetId)
# Keep the channel name, image name, image id, area, perimeter, and diameter of the largest ROIs
result <- data.frame(Channel = c('remove'), ImageName = c('remove'), Image = c(0), ROIIndex = c(0), Area = c(0), Perimeter = c(0), Diameter = c(0), stringsAsFactors = FALSE)
images <- getImages(dataset)
for (image in images) {
result <- tryCatch({
analyzeImage(image, channelName, result)
}, warning = function(war) {
message(paste("WARNING: ", image@dataobject$getId(),war))
return(result)
}, error = function(err) {
message(paste("ERROR: ", image@dataobject$getId() ,err))
return(result)
}, finally = {
})
}
result <- result[-1,]
rownames(result) <- c()
# set the correct datatypes
result$Channel <- as.factor(result$Channel)
result$Area <- as.numeric(result$Area)
result$Perimeter <- as.numeric(result$Perimeter)
result$Diameter <- as.numeric(result$Diameter)
result
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : omero.gateway.exception.DSOutOfServiceException: Connection lost. Traceback: 1. loadObject(server, "DatasetData", datasetId) 2. loadObject(server, "DatasetData", datasetId) 3. browse$getHierarchy(ctx, clazz, ids, .jnull(class = "omero/sys/Parameters")) 4. .jrcall(x, name, ...) 5. .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, . .jcast(if (inherits(o, "jobjRef") || inherits(o, "jarrayRef")) o else cl, . "java/lang/Object"), .jnew("java/lang/String", method), . j_p, j_pc, use.true.class = TRUE, evalString = simplify, . evalArray = FALSE) 6. .jcheck(silent = FALSE)
The image segmentation has already been run over the whole idr0021 project and the results attached to the project as table 'Summary from R'. We are going to load this table from OMERO as dataframe "centrioles". In this manner, we will quickly obtain a sizeable segmentation dataset to perform a statistical analysis on.
projectId <- 51
project <- loadObject(server, "ProjectData", projectId)
dataframes <- availableDataframes(project)
print(dataframes)
# Load the dataframe
dfID <- dataframes$ID[1]
centrioles <- loadDataframe(project, dfID)
# Make sure the data types are correct
centrioles$Dataset <- as.factor(centrioles$Dataset)
centrioles$Diameter <- as.numeric(centrioles$Diameter)
centrioles
# Order the datasets ascending by mean diameter
ag <-aggregate(centrioles$Diameter ~ centrioles$Dataset, centrioles, median)
ordered <- factor(centrioles$Dataset, levels=ag[order(ag$`centrioles$Diameter`), 'centrioles$Dataset'])
# Draw the plot
plot(centrioles$Diameter ~ ordered, ylab='Diameter (nm)', xlab="Protein", cex.axis=0.5)
fit <- aov(centrioles$Diameter ~ centrioles$Dataset)
summary(fit)
Are all proteins significantly different from each other?
# Two-sample Wilcoxon test ('Mann-Whitney') of all pairwise combinations:
combins <- combn(levels(centrioles$Dataset), 2)
params_list <- split(as.vector(combins), rep(1:ncol(combins), each = nrow(combins)))
testResults <- data.frame()
for (param in params_list) {
testdf <- subset(centrioles, centrioles$Dataset %in% param)
pval <- wilcox.test(formula = Diameter ~ Dataset, data = testdf)$p.value
testResults <- rbind(testResults, data.frame(Protein_1=param[1], Protein_2=param[2], p_value=pval))
}
testResults
Finally close the connection to the OMERO.server again:
disconnect(server)
Copyright (c) 2018-2021, University of Dundee All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.