-
Notifications
You must be signed in to change notification settings - Fork 1
/
PruneTPBP.C
311 lines (247 loc) · 7.37 KB
/
PruneTPBP.C
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include"Prune.h"
/*****************************************************************/
void Prune :: pruneTP(int cn, int sn)
{
double tx1,ty1,tz1;
double tx2,ty2,tz2;
double ipdist;
createLine(PruCom[PruCom[cn].ppi-1],PruCom[cn]);
if(ls -> num==2){ // Current pt is last pt in comptmt.
tx1=PruCom[PruCom[cn].ppi-1].x;
ty1=PruCom[PruCom[cn].ppi-1].y;
tz1=PruCom[PruCom[cn].ppi-1].z;
removeCompartment(cn,sn);
cmr++;
}
else{
// Remove Last Pt.
tx1=ls->x[ls->num-2];
ty1=ls->y[ls->num-2];
tz1=ls->z[ls->num-2];
PruCom[cn].x = ls -> x[ls->num-2];
PruCom[cn].y = ls -> y[ls->num-2];
PruCom[cn].z = ls -> z[ls->num-2];
}
tx2=ls -> x[ls->num-1];
ty2=ls -> y[ls->num-1];
tz2=ls -> z[ls->num-1];
ipdist=sqrt((tx1-tx2)*(tx1-tx2)+ (ty1-ty2)*(ty1-ty2)+
(tz1-tz2)*(tz1-tz2));
if(PruCom[cn].tc==4) // Apical
adlprune[sn] += ipdist/RESLN;
else if (PruCom[cn].tc==3) // Basal
bdlprune[sn] += ipdist/RESLN;
dlr += ipdist/RESLN;
}
/*****************************************************************/
// If the removed compartment's parent is BP, remove the point
// from the TP list, from the BP list (if it is not a
// trifurcation), and update the BP prune numbers and
// probabilities.
/*****************************************************************/
void Prune :: rcParentBP(int cn, int sn)
{
int i,j;
if (PruCom[cn].tc==3) // Basal
bbpprune[sn]++;
else if (PruCom[cn].tc==4) // Apical
abpprune[sn]++;
// Remove pt from TP list
int SET=0;
for(i=0; i<TP.npts; i++)
if(TP.Pts[i]==cn){
SET=1;
break;
}
if(!SET){
cerr <<"\nrcParentBP :: Error, TP not found\n\n";
exit(1);
}
for(j=i;j<TP.npts-1;j++)
TP.Pts[j]=TP.Pts[j+1];
TP.npts--;
// Remove parent from bp list if that point is a bifurcation
if(Children[PruCom[cn].ppi-1].npts==2){
for(i=0; i<BP.npts; i++)
if(BP.Pts[i]==(PruCom[cn].ppi-1)) break;
for(j=i;j<BP.npts-1;j++)
BP.Pts[j]=BP.Pts[j+1];
BP.npts--;
}
}
/*****************************************************************/
// If removed compartment's parent is soma, remove the
// compartment from the TP list and from the Stems list
/*****************************************************************/
void Prune :: rcParentSoma(int cn)
{
int i,j;
// Remove pt from tp list
for(i=0; i<TP.npts; i++)
if(TP.Pts[i]==cn) break;
for(j=i;j<TP.npts-1;j++)
TP.Pts[j]=TP.Pts[j+1];
TP.npts--;
// Remove pt from stems list
for(i=0; i<Stems.npts; i++)
if(Stems.Pts[i]==cn) break;
for(j=i;j<Stems.npts-1;j++)
Stems.Pts[j]=Stems.Pts[j+1];
Stems.npts--;
}
/*****************************************************************/
// The case where the parent is neither soma nor BP
// Implies parent is also dendrite. So, update the TP to the current
// compartment's parent.
/*****************************************************************/
void Prune :: rcParentDendrite(int cn)
{
int i,j;
for(i=0; i<TP.npts; i++)
if(TP.Pts[i]==cn) break;
TP.Pts[i]=PruCom[cn].ppi-1;
// If parent (which is the current TP) has an undefined TC (-1),
// remove that from TP list.
if(PruCom[TP.Pts[i]].tc!=3 && PruCom[TP.Pts[i]].tc!=4){
for(j=i;j<TP.npts-1;j++)
TP.Pts[j]=TP.Pts[j+1];
TP.npts--;
cerr << "\nWarning: Undefined compartment removed from TP list.\n\n";
}
}
/*****************************************************************/
// Remove comptmt cn from PruCom.
// Update the indices of the SWC file to accomodate the
// removal of the compartment number cn. Also updates the parents'
// indices, the TP, BP, Soma list.
/*****************************************************************/
void Prune :: rmcmpt (int cn)
{
int i;
if((PruCom[cn].tc != 3) && (PruCom[cn].tc != 4)){
cerr << "\nrmcmpt :: GRAVE ERROR\n";
exit(1);
}
for(i=cn; i<npc-1; i++){
PruCom[i]=PruCom[i+1];
PruCom[i].pi --; // One comptmt is removed.
}
npc--;
// Update Parent Indices to accomodate above update.
for(i=0; i<npc; i++){
if((PruCom[i].ppi-1)>cn)
PruCom[i].ppi--; // One Comptmt is removed.
}
// Update lists of TP, BP, Stems, Soma to accomodate above.
for(i=0; i<TP.npts; i++)
if(TP.Pts[i]>cn) TP.Pts[i]--;
for(i=0; i<BP.npts; i++)
if(BP.Pts[i]>cn) BP.Pts[i]--;
for(i=0; i<Stems.npts; i++)
if(Stems.Pts[i]>cn) Stems.Pts[i]--;
for(i=0; i<Soma.npts; i++)
if(Soma.Pts[i]>cn) Soma.Pts[i]--;
}
/*****************************************************************/
// Remove the current compartment totally.
// If the parent to this compartment were a BP, then,
// remove that BP from the list of BP's and update BP
// probability; sn stands for the sholl's segment number
/*****************************************************************/
void Prune :: removeCompartment(int cn, int sn)
{
if(isParentBP(cn)) // If parent is BP
rcParentBP(cn,sn);
else if(isParentSoma(cn)) // If parent is Soma
rcParentSoma(cn);
else // If parent is dendrite
rcParentDendrite(cn);
rmcmpt(cn);
}
/*****************************************************************/
//**************** BPPrune starts here **************************/
/*****************************************************************/
void Prune :: findTPBP()
{
int i;
double dist;
int sn;
int trgt;
for(i=0; i<TP.npts; i++){
trgt=TP.Pts[i];
bpPruneP[i]=1.0;
while((isParentBP(trgt)==0) && (isParentSoma(trgt)==0)){
dist = 0.0;
dist += (somax-PruCom[trgt].x)*(somax-PruCom[trgt].x);
dist += (somay-PruCom[trgt].y)*(somay-PruCom[trgt].y);
dist += (somaz-PruCom[trgt].z)*(somaz-PruCom[trgt].z);
dist = sqrt(dist);
sn = (int)(dist/(double)STLENGTH);
if(PruCom[trgt].tc==3) // Basal
bpPruneP[i]*=pow(bdlP[sn], disttoParent(trgt));
else if(PruCom[trgt].tc==4) // Apical
bpPruneP[i]*=pow(adlP[sn], disttoParent(trgt));
trgt=PruCom[trgt].ppi-1;
}
if(isParentBP(trgt))
TPBP[i]=PruCom[trgt].ppi-1;
else if (isParentSoma(trgt))
TPBP[i]=-2;
else{
cerr << "\nTPBP :: There exists some error...\n";
cerr << PruCom[trgt] ;
cerr << PruCom[PruCom[trgt].ppi-1] ;
exit(1);
}
}
}
/*****************************************************************/
// Remove BP which has its TP as ntp'th TP. This is accomplished by
// continually pruning TP[ntp] until a BP is encountered.
/*****************************************************************/
void Prune :: pruneBP(int ntp)
{
int trgt;
// Actually remove the part connecting the TP to the BP by
// recursively pruning TP[ntp] until a branching point is
// encountered.
int ret=0;
int cmcnt=0;
while(!ret){
trgt=TP.Pts[ntp];
ret=removeCmprtmnt(trgt);
cmcnt++;
}
}
/*****************************************************************/
int Prune :: removeCmprtmnt(int cn)
{
double dist = 0.0;
dist += (somax-PruCom[cn].x)*(somax-PruCom[cn].x);
dist += (somay-PruCom[cn].y)*(somay-PruCom[cn].y);
dist += (somaz-PruCom[cn].z)*(somaz-PruCom[cn].z);
dist = sqrt(dist);
// Segment where current compartment belongs to
int sn = (int)(dist/(double)STLENGTH);
if (PruCom[cn].tc==3)
bdlprune[sn]+=disttoParent(cn);
else if (PruCom[cn].tc==4)
adlprune[sn]+=disttoParent(cn);
else{
cerr << "\nGravest error\n";
exit(1);
}
int ret=0;
if(isParentBP(cn)){
ret=1;
rcParentBP(cn, sn);
}
else if (isParentSoma(cn)){
cerr << "\nrC :: Grave Error!\n";
}
else
rcParentDendrite(cn);
rmcmpt(cn); // Removing comptmt cn from PruCom.
return ret;
}
/*****************************************************************/