-
Notifications
You must be signed in to change notification settings - Fork 0
/
pol.py
executable file
·165 lines (139 loc) · 7.02 KB
/
pol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itk, random, sys, re, traceback
from ExtraData import ExtraData
itk.auto_not_in_place()
random.seed()
def computeRegion(lo, img):
img = itk.output(img)
import math
transform = lo.GetBinaryPrincipalAxesToPhysicalAxesTransform()
invTransform = itk.AffineTransform.D3.New()
transform.GetInverse( invTransform )
region = lo.GetRegion()
outputSpacing = [min(itk.spacing(img))]*3
dummyImg = itk.Image.UC3.New()
dummyImg.SetSpacing( outputSpacing )
idx = region.GetIndex()
size = region.GetSize()
minPoint = [4294967295L]*3
maxPoint = [-4294967295L]*3
for x in [idx[0], idx[0]+size[0]]:
for y in [idx[1], idx[1]+size[1]]:
for z in [idx[2], idx[2]+size[2]]:
inputPoint = img.TransformIndexToPhysicalPoint( [x,y,z] )
outputPoint = invTransform.TransformPoint( inputPoint )
for i in range(0,3):
if minPoint[i] > outputPoint[i]:
minPoint[i] = outputPoint[i]
if maxPoint[i] < outputPoint[i]:
maxPoint[i] = outputPoint[i]
minIdx = dummyImg.TransformPhysicalPointToIndex(minPoint)
maxIdx = dummyImg.TransformPhysicalPointToIndex(maxPoint)
outputIdx = []
outputSize = []
for i in range(0,3):
outputIdx.append(int(minIdx[i]))
outputSize.append( int(maxIdx[i] - minIdx[i]) )
uabc3 = 1.0
for v in itk.spacing(img):
uabc3 *= v
for v in lo.GetEquivalentEllipsoidSize():
uabc3 *= v
uabc3 = math.pow(uabc3, 1/3.0)
spacing2 = [uabc3 / v for v in lo.GetEquivalentEllipsoidSize()]
return (outputIdx, outputSize, outputSpacing, spacing2)
nucleusReader = itk.ImageFileReader.IUC3.New()
centromeresReader = itk.ImageFileReader.IUC3.New()
centromeresIntReader = itk.ImageFileReader.IUC3.New()
# unflatten the nucleus and the images associated to it
li2lm = itk.LabelImageToShapeLabelMapFilter.IUC3LM3.New(nucleusReader)
nni = itk.NearestNeighborInterpolateImageFunction.IUC3D.New()
resampleNucleus = itk.ResampleImageFilter.IUC3IUC3.New(nucleusReader, Interpolator=nni)
resampleCentromeres = itk.ResampleImageFilter.IUC3IUC3.New(centromeresReader, Interpolator=nni)
resampleCentromeresInt = itk.ResampleImageFilter.IUC3IUC3.New(centromeresIntReader)
unflattenNucleus = itk.ChangeInformationImageFilter.IUC3.New(resampleNucleus, ChangeSpacing=True)
unflattenCentromeres = itk.ChangeInformationImageFilter.IUC3.New(resampleCentromeres, ChangeSpacing=True)
unflattenCentromeresInt = itk.ChangeInformationImageFilter.IUC3.New(resampleCentromeresInt, ChangeSpacing=True)
nucleusLM = itk.LabelImageToShapeLabelMapFilter.IUC3LM3.New(unflattenNucleus)
maskedCentromeres = itk.LabelMapMaskImageFilter.LM3IUC3.New(nucleusLM, unflattenCentromeres)
centromeresLM = itk.LabelImageToStatisticsLabelMapFilter.IUC3IUC3LM3.New(maskedCentromeres, unflattenCentromeresInt)
posCentromeresLM = itk.StatisticsPositionLabelMapFilter.LM3.New(centromeresLM, Attribute="CenterOfGravity")
aggrCentromeresLM = itk.AggregateLabelMapFilter.LM3.New(posCentromeresLM, InPlace=False)
shapeAggrCentromeresLM = itk.ShapeLabelMapFilter.LM3.New(aggrCentromeresLM)
simCentromeresLM = itk.LabelMap._3.New()
shapeSimCentromeresLM = itk.ShapeLabelMapFilter.LM3.New(simCentromeresLM)
simCentromeresLM2 = itk.LabelMap._3.New()
shapeSimCentromeresLM2 = itk.ShapeLabelMapFilter.LM3.New(simCentromeresLM2)
extra = ExtraData(defaults={"nucleus-ext": "-nuclei.nrrd",
"spots-ext": "-CENP.nrrd",
"spots-are-labeled": "true",
"rawspots-ext": "",
"rawspots-channel": "0",
"number-of-simulations": "500"})
nf = extra.printer(file(sys.argv[1].replace("::","-")+".txt", "w"))
nf.printHeader("image", "label", "size", "centromeres", "flatness", "cdist", "rdist", "ncdist", "nrdist", "pvalue")
for (nucleusName, centromeresName, centromeresIntName) in extra.iterate(["nucleus-ext", "spots-ext", "rawspots-ext"]):
try:
nbSims = extra.getint("number-of-simulations")
nucleusReader(FileName=nucleusName)
centromeresReader(FileName=centromeresName)
centromeresIntReader(FileName=centromeresIntName)
for flatNucleus in li2lm()[0]:
idx, size, spacing, spacing2 = computeRegion(flatNucleus, nucleusReader)
transform = flatNucleus.GetBinaryPrincipalAxesToPhysicalAxesTransform()
resampleNucleus(Transform=transform, OutputStartIndex=idx, Size=size, OutputSpacing=spacing)
resampleCentromeres(Transform=transform, OutputStartIndex=idx, Size=size, OutputSpacing=spacing)
resampleCentromeresInt(Transform=transform, OutputStartIndex=idx, Size=size, OutputSpacing=spacing)
unflattenNucleus(OutputSpacing=spacing2)
unflattenCentromeres(OutputSpacing=spacing2)
unflattenCentromeresInt(OutputSpacing=spacing2)
nucleus = nucleusLM()[0].GetLabelObject(flatNucleus.GetLabel())
maskedCentromeres.SetLabel(nucleus.GetLabel())
nucleusSize = nucleus.GetSize()
npos = nucleus.GetCentroid()
# une simulation pour une analyse globale dans R
simCentromeresLM.SetRegions(itk.region(nucleusReader))
simCentromeresLM.SetSpacing(itk.spacing(nucleusReader))
simCentromeresLM.ClearLabels()
for i in range(centromeresLM()[0].GetNumberOfLabelObjects()):
idx = nucleus.GetIndex(random.randint(0, nucleusSize-1))
simCentromeresLM.SetPixel(idx, 1)
simCentromeresLM.GetLabelObject(1).Optimize()
cpos = shapeAggrCentromeresLM()[0].GetNthLabelObject(0).GetCentroid()
cdist = (npos - cpos).GetNorm()
spos = shapeSimCentromeresLM()[0].GetNthLabelObject(0).GetCentroid()
sdist = (npos - spos).GetNorm()
# realisation dun test par noyau avec plusieurs simulations
simCentromeresLM2.SetRegions(itk.region(nucleusReader))
simCentromeresLM2.SetSpacing(itk.spacing(nucleusReader))
sdist2s = []
for s in range(nbSims):
simCentromeresLM2.ClearLabels()
for i in range(centromeresLM()[0].GetNumberOfLabelObjects()):
idx = nucleus.GetIndex(random.randint(0, nucleusSize-1))
simCentromeresLM2.SetPixel(idx, 1)
simCentromeresLM2.GetLabelObject(1).Optimize()
spos2 = shapeSimCentromeresLM2()[0].GetNthLabelObject(0).GetCentroid()
sdist2 = (npos - spos2).GetNorm()
sdist2s.append(sdist2)
sdist2s.sort()
sdist2s.reverse()
pos=0
ok=False
while pos<nbSims and not ok:
if cdist > sdist2s[pos]:
ok=True
else:
pos += 1
pvalue = float(pos)/nbSims
# pour normaliser les distances dans la première méthode
# msdist2 = sum(sdist2s)/nbSims
msdist2 = nucleus.GetEquivalentRadius()
nf.printData(nucleusName, nucleus.GetLabel(), nucleus.GetSize(), centromeresLM()[0].GetNumberOfLabelObjects(), nucleus.GetBinaryFlatness(), cdist, sdist, cdist/msdist2, sdist/msdist2, pvalue)
nf.flush()
except Exception, e:
print >> sys.stderr, "Error with", nucleusName
print >> sys.stderr, e
traceback.print_exc(file=sys.stderr)
shapeAggrCentromeresLM.ResetPipeline()