-
Notifications
You must be signed in to change notification settings - Fork 2
/
Neuron.cpp
5963 lines (4900 loc) · 152 KB
/
Neuron.cpp
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Neuron.h"
#include "Limit.h"
#include "Func.h"
#include <fstream>
#include <cstdlib>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "ParameterList.h"
#include "Random.h"
//#include <direct.h>
#include <sstream>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "pca.h"
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define SIGN(a, b) ( (b) < 0 ? -fabs(a) : fabs(a) )
using namespace std;
Neuron::Neuron() {
setDefaults();
}
;
void Neuron::setDefaults() {
d[0][0] = 0;
ifstream* in_d;
soma = NULL;
nrSeg = 0;
lp = NULL;
setShrinkX(1);
setShrinkY(1);
setShrinkZ(1);
char def[50];
strcpy(def, "empty");
name = def; //strcpy(name,def);
oriented = false;
}
;
void Neuron::init() {
cerr<<"init \n";
findDuplicate(soma);
buildFathers(soma, soma);
//insert a new compartment for SWC conversions for single soma files only. This modification helps in converting the SWCs to .hoc files. sri 01/07/2010.
//Check if the cell has one stem or more. If next2 is null, then single stem, otherwise, two stems. SP on 02/25/11.
if (soma->getType() == 1 && soma->getNext2() == NULL
&& !(soma->getNext1()->getType() == 1)) {
cerr
<< "single soma..adding new soma compartment to a single stemmed existing soma point"
<< "\n";
addCylinderSoma(soma);
} else if (soma->getType() == 1 && !(soma->getNext1()->getType() == 1
|| soma->getNext2()->getType() == 1)) {
cerr
<< "single soma..adding new soma compartment to a two or more stemmed soma point"
<< "\n";
cerr << "soma:" << soma->getType() << " " << "next1:"
<< soma->getNext1()->getType() << " " << "next2:"
<< soma->getNext2()->getType() << "\n";
addCylinderSoma(soma);
}
renumerateId();
setOrder(soma);
setParameters(soma);
}
;
//add a new soma to the existing soma as the beginning point to convert a sphere to cylinder approximation of soma part.
// modified by sridevi on 12/08/2010
void Neuron::addCylinderSoma(Segment* soma) {
if (soma->getType() != 1) {
if (soma->getNext1() != NULL)
addCylinderSoma(soma->getNext1());
if (soma->getNext2() != NULL)
addCylinderSoma(soma->getNext2());
} else {
cerr << "first soma point" << soma->getId() << " "
<< soma->getEnd()->getX() << " " << soma->getEnd()->getY()
<< " " << soma->getEnd()->getZ() << " " << soma->getType()<<"\n";
double x = soma->getEnd()->getX();
double y1 = soma->getEnd()->getY() - soma->getRadius();
double z = soma->getEnd()->getZ() ;//approximation to covert spehere to cylinder assumption for soma point.
//New Soma Z
double y2 = soma->getEnd()->getY() + soma->getRadius();//approximation to covert spehere to cylinder assumption for soma point.
Vector* a1 = new Vector(x, y1, z);
Vector* a2 = new Vector(x, y2, z);
//we are in the soma
Segment* newsoma1 = soma->clone();
Segment* newsoma2 = soma->clone();
//Ghost segment
newsoma1->setEnd(a1);
newsoma1->setPrev(soma);
//SG code added 26/08/2011
newsoma1->setPrevFather(soma);
newsoma2->setEnd(a2);
newsoma2->setPrev(soma);
newsoma2->setPrevFather(soma);
soma->addNewSomaChild(newsoma1);
soma->addNewSomaChild(newsoma2);
cerr<<"Cycl soma added \n";
}
}
/*
void Neuron::zeroing() {
if (soma->getEnd()->getX() != 0 || soma->getEnd()->getY() != 0
|| soma->getEnd()->getZ() != 0)
translation(soma->getEnd()->getX(), soma->getEnd()->getY(),
soma->getEnd()->getZ());
}
;*/
//grow a virtual neuron based on the list of parameters
Neuron::Neuron(ParameterList* lps) {
OpenPRM(lps);
}
;
//grow a virtual neuron based on the list of parameters
void Neuron::OpenPRM(ParameterList* lps) {
double d, rho, diam, az, ele, torch;
int met, id, pid;
setDefaults();
setName(lps->getFileName());
rnd = new Random();
lp = lps;
lp->setType(1);
torch = 0;
met = lp->getParameter(METHOD);
//create soma
d = lp->getParameter(SOMA_DIAMETER);
pid = -1;
id = 1;
addPolar(id, 1, 1, 0, 0, 0, d, -1);
pid = id;
int maxType = lp->getParameter(MAX_TYPE);
for (int ii = 3; ii <= maxType; ii++) {
//3:Apical....
lp->setType(ii);
d = lp->getParameter(NUM_TREE);
for (int i = 0; i < d; i++) {
//first virtual segment long 1
if (lp->isPresent(CONSLENGTH) == 1)
rho = lp->getParameter(CONSLENGTH);
if (lp->isPresent(LENGTH) == 1)
rho = lp->getParameter(LENGTH);
az = lp->getParameter(TREE_AZY);
az *= M_PI / 180;//az=0;
ele = lp->getParameter(TREE_ELEV);
ele *= M_PI / 180;
diam = lp->getParameter(INIT_DIAM);
id++;
id = addPolar(id, 3, rho, az, ele, torch, diam, pid);
switch (met) {
case 0:
id = growBurke(diam, ii, id);
break;
case 1:
id = growTamori(diam, ii, id);
break;
case 2:
id = growHillman(diam, ii, id);
break;
default:
cerr << "No Valid method specified!\n";
}
}
}
if (soma != NULL) {
soma->polarRelativeToabsolute();
soma->polarToCartesian();
init();
}
}
;
int Neuron::growBurke(double diameter, int type, int pid) {
int id = pid;
double length = lp->getParameter(LENGTH); //Sample_Distribution (dist_bin_length [tree_type]);
double taper = lp->getParameter(TAPER); //Sample_Distribution (dist_taper [tree_type]);
double k1_overlap = lp->getParameter(K1_OVERLAP); //Sample_Distribution (dist_k1_overlap [tree_type]);
double k2_overlap = lp->getParameter(K2_OVERLAP); //Sample_Distribution (dist_k2_overlap [tree_type]);
double k1_nonoverlap = lp->getParameter(K1_NONOVERLAP); //Sample_Distribution (dist_k1_nonoverlap [tree_type]);
double k2_nonoverlap = lp->getParameter(K2_NONOVERLAP); //Sample_Distribution (dist_k2_nonoverlap [tree_type]);
double k1_terminate = lp->getParameter(K1_TERMINATE); //Sample_Distribution (dist_k1_terminate [tree_type]);
double k2_terminate = lp->getParameter(K2_TERMINATE); //Sample_Distribution (dist_k2_terminate [tree_type]);
double pbr, pnonoverlap, ptr;
double torch = 0;
diameter += taper * length;
pbr = k1_overlap * exp(k2_overlap * diameter);
ptr = k1_terminate * exp(k2_terminate * diameter);
pnonoverlap = k1_nonoverlap * exp(k2_nonoverlap * diameter);
if (pnonoverlap < pbr)
pbr = pnonoverlap;
/* decide whether to branch, terminate or grow */
if (rnd->rnd01() < pbr * length) {
/* branch */
double r1 = lp->getParameter(GAUSS_BRANCH); // Sample_Distribution (dist_gaussian_branch [tree_type]);
double r2 = lp->getParameter(GAUSS_BRANCH); //Sample_Distribution (dist_gaussian_branch [tree_type]);
double burkea = lp->getParameter(LINEAR_BRANCH);//Sample_Distribution (dist_linear_branch [tree_type]);
double diam1 = diameter * (r1 + r2 * burkea);
double diam2 = diameter * (r2 + r1 * burkea);
double totbifang = lp->getParameter(BIFAMPLI); //Sample_Distribution (dist_bifurcating_amplitude_angle [tree_type]);
double bifang1 = rnd->rnd01() * totbifang;
double bifang2 = bifang1 - totbifang;
id = pid + 1;
id = addPolar(id, type, length, bifang1, 0, torch, diam1, pid);
id = growBurke(diam1, type, id);
id++;
id = addPolar(id, type, length, bifang2, 0, torch, diam2, pid);
pid = growBurke(diam2, type, id);
return pid;
} else if (rnd->rnd01() < ptr * length) { /* terminate */
return id;
} else { /* grow a stem */
double azi = lp->getParameter(EXTEND_AZIMUTH);
double ele = lp->getParameter(EXTEND_ELE);
id = pid + 1;
torch = 0;
id = addPolar(id, type, length, azi, ele, torch, diameter, pid);
id = growBurke(diameter, type, id);
}
return pid;
}
int Neuron::growTamori(double diameter, int type, int pid) {
double length, taper;
length = lp->getParameter(CONSLENGTH);
taper = lp->getParameter(TAPER);
diameter *= (1 - taper);
int id = pid;
if (diameter < lp->getParameter(THRESHOLD)) {
length = lp->getParameter(TERMLENGTH);
id++;
id = addPolar(id, type, length, 0, 0, 0, diameter, pid);
//if less than min then terminate
return id;
}
//bifurcate
double ratio, power, rall, diam1, diam2;
ratio = lp->getParameter(BIFRATIO);
power = lp->getParameter(BIFPOWER);
rall = pow((1 + pow(ratio, power)), -1 / power);
diam2 = diameter * rall * pow(lp->getParameter(PK), 1 / power);
diam1 = ratio * diam2;
double dimension, eq5_4_n1, eq5_4_n2, eq5_4_den, bifang1, bifang2;
dimension = 1.0 + rnd->rnd01() * (power - 1);
/* Equation 5.4 from Tamori, 1993 */
eq5_4_n1 = pow(1 + pow(ratio, power), 2 * dimension / power);
eq5_4_n2 = pow(ratio, 2 * dimension);
eq5_4_den = 2 * pow(1 + pow(ratio, power), dimension / power);
bifang1 = acos(
(eq5_4_n1 + eq5_4_n2 - 1) / (eq5_4_den * pow(ratio, dimension)));
bifang2 = acos((eq5_4_n1 - eq5_4_n2 + 1) / (eq5_4_den));
//for display purpose
if (rnd->rnd01() < 0.5) {
bifang1 *= -1;
bifang2 *= -1;
}
double torch = lp->getParameter(BIFORIENT) * M_PI / 180;
double azi, ele;
id = pid + 1;
azi = bifang1;
ele = 0;
id = addPolar(id, type, length, azi, ele, torch, diam1, pid);
id = growTamori(diam1, type, id);
id++;
azi = bifang2;
ele = 0;
id = addPolar(id, type, length, azi, ele, torch, diam2, pid);
id = growTamori(diam2, type, id);
return id;
}
int Neuron::growHillman(double diameter, int type, int pid) {
// model of Hillman
double length, taper;
length = lp->getParameter(CONSLENGTH);
taper = lp->getParameter(TAPER);
double diamOld = diameter;
diameter *= (1 - taper);
int id = pid;
if (diameter < lp->getParameter(THRESHOLD)) {
length = lp->getParameter(TERMLENGTH);
id++;
id = addPolar(id, type, length, 0, 0, 0, diameter, pid);
//if less than min then terminate
return id;
}
//bifurcate
double ratio, power, rall, diam1, diam2, totbifang;
ratio = lp->getParameter(BIFRATIO);
power = lp->getParameter(BIFPOWER);
rall = pow((1 + pow(ratio, power)), -1 / power);
diam2 = diameter * rall * pow(lp->getParameter(PK), 1 / power);
diam1 = ratio * diam2;
double bifang1, bifang2;
totbifang = lp->getParameter(BIFAMPLI);
totbifang *= M_PI / 180;
bifang1 = rnd->rnd01() * totbifang;
bifang2 = bifang1 - totbifang;
double torch = lp->getParameter(BIFORIENT) * M_PI / 180;
double azi, ele;
azi = bifang1;
ele = 0;
id = pid + 1;
id = addPolar(id, type, length, azi, ele, torch, diam1, pid);
id = growHillman(diam1, type, id);
id++;
azi = bifang2;
ele = 0;
addPolar(id, type, length, azi, ele, torch, diam2, pid);
pid = growHillman(diam2, type, id);
return pid;
}
//build a neuron from an input file
Neuron::Neuron(char* c,int neuroClass, bool isDiaPresent, char * dia_n){
cerr << "creating neuron:" << c << "\n";
ferror = false;
foundsoma = false;
//reset comment field
d[0][0] = 0;
neuronalClass = neuroClass;
soma = NULL;
strtseg = NULL;
nrSeg = 0;
setShrinkX(1);
setShrinkY(1);
setShrinkZ(1);
name = c;
name_str = c;
namectr = 1;
setIsDiaSet(isDiaPresent);
if(isDiaSet)
dia = dia_n;
int startind;
#ifdef OS_WIN
//Windows specific stuff
startind = name_str.rfind("\\");
#else
//Normal stuff
startind = name_str.rfind("/");
#endif
int endind = name_str.rfind(".") - 1;
dir_str = name_str.substr(0, startind + 1);
name_str = name_str.substr(startind + 1, endind - startind);
//cerr<<name_str<<"\n";
if(name_str == "03204L2.CNG.swc")
cerr<<"stop here.."<<name_str<<"\n";
oriented = false;
rf = 0;
//flag variable to intiate rf and first segment
flag = 0;
ctr = 0;
//replace '*' in the file path with ' '
int jj = 0;
while (c[jj] != 0) {
if (c[jj] == '*')
c[jj] = ' ';
jj++;
}
in = new ifstream(c, ios::in | ios::binary);
//SG Code
in_d = new ifstream(c, ios::in | ios::binary);
char * ext;
//look for last point in the file name to extract extension
int i;
for (i = 0; i < 1024 && c[i] != 0; i++) {
if (c[i] == '.')
ext = c + i;
}
int h = ext - c;
int jk = strlen(c);
//error if file does not exist or extension is smaller than 2 chars
if (in == NULL || strlen(c) <= (ext - c)) {
cerr << "Wrong FileName:" << c
<< " !\n----------------------------------\n";
soma = new Segment(0, 1, new Vector(0, 0, 0), 1, -1);
return;
}
// This code extracts the last 4 characters of a file name which
// consist of the file extension and is used for comparison of file types.
int typ = -1;
char *substr = new char[4];
for (int i = 0; i <= 4; i++)
{
substr[i] = c[strlen(c) - (4 - i)];
}
// string string(c),substr;
// size_t pos;
// pos = string.find_last_of(".");
// substr = string.substr(pos);
//if (strstr(c, ".swc") != NULL || strstr(c, ".SWC") != NULL) // Bug : Checks for a .swc file extension anywhere in the filename.
//if (strcmp(substr,".swc") == 0 || strcmp(substr,".SWC") == 0)
//typ = 0;
if (strcmp(substr,".hoc") == 0 || strcmp(substr,".dat") == 0) // filters out .dat and .hoc files right at the
typ = 5; // beginning to prevent extra computation.
else
typ = LookForFileType(in);
cerr<<"returning.."<<typ;
switch (typ) {
case 0:
//computes SWC files
OpenSWC();
if (soma->getType() != 1 && foundsoma == true) {
//SG Code changes Sep 02 2011
//Commented to fix the problem where the conversion not taking place if the
//soma not placed on first line of swc file.
//addVirtualSoma(soma);
buildFathers(soma, soma);
//initializing this vairable for every new neuron sri 03/25/10
found = 0;
Segment * tmp = rearrangeSWC(soma);
soma = tmp;
tmp = NULL;
soma->setPrev(NULL);
renumerateId(soma);
}
break;
case 1:
//computes Amaral, Henze, Miller Eutectic files
OpenAMA();
break;
case 2:
//computes NeuroLucida files
OpenNeuroL();
break;
case 3:
//computes Claiborne files
OpenClaiborne();
//OpenSEG();
break;
case 4:
OpenAmira();
break;
default:
cout << "Given File type is not supported!!\n";
cerr << "Given File type is not supported!!\n";
break;
}
char buff[30];
//the tree_ext variable is used to create subtrees (only for swc files). commented out this line by mistake in 2.8.6 version.
tree_ext = ".swc";
if (soma == NULL) {
cout << "Unable to create Neuron:" << c
<< " !\n----------------------------------\n";
Vector * e = new Vector(0, 0, 0);
soma = new Segment(0, 1, e, 1, -1);
delete e;
return;
}
in->close();
delete in;
in = NULL;
init();
soma->setClass(neuronalClass);
cerr<<"Neuron() done \n";
}
void Neuron::doPCA() {
if (oriented == false) {
if (soma->getEnd()->getX() != 0 || soma->getEnd()->getY() != 0
|| soma->getEnd()->getZ() != 0)
translation(soma->getEnd()->getX(), soma->getEnd()->getY(),
soma->getEnd()->getZ());
pca();
if (soma->getEnd()->getX() != 0 || soma->getEnd()->getY() != 0
|| soma->getEnd()->getZ() != 0)
translation(soma->getEnd()->getX(), soma->getEnd()->getY(),
soma->getEnd()->getZ());
setOrder(soma);
setParameters(soma);
}
oriented = true;
}
//LookForFileType function looks for unique tags to find the File extension implemented by sridevi 11/29/04
int Neuron::LookForFileType(ifstream* in) {
char * AMH_TAG1 = "No.";
char * AMH_TAG2 = "points";
char * AMH_TAG3 = "Point";
char * NL_SOMA_TAG1 = "(CellBody)";
char * NL_SOMA_TAG2 = "(Closed)";
char * CL_TAG1 = "S";
char * SWC_TAG1 = "1";
char * SWC_TAG2 = "-1";
char * SWC_TAG3 = "2";
char * SWC_TAG4 = "1";
char * SWC_COMMENT = "#";
int type = -1;
if (lookFor("AmiraMesh", in) == true)
return 4;
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
if (lookFor("Point", in) == true)
if (lookFor("Type", in) == true)
if (lookFor("Tag", in) == true)
return 1;
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
if (lookFor(NL_SOMA_TAG1, in) == true) {
return 2;
}
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
if (lookFor(NL_SOMA_TAG2, in) == true) {
return 2;
}
//neurolucida files which doesn't have a CellBody keyword.
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
if (lookFor("(Axon)", in) == true) {
// cout<<"found Axon......... \n";
return 2;
}
//SP added in->peek() ==-1 for moving the 'in' pointer to the beginning of the file. The change is made consistently everywhere. 10/10/12
//neurolucida files which doesn't have a CellBody keyword.
if (in->peek()==-1 || in->fail()){
in->clear();
in->seekg(0, ios::beg);
}
//cerr<<"c in peek.."<<in->peek();
if (lookFor("(Dendrite)", in) == true){
//cout<<"found Dendrite......... \n";
return 2;
}
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
char whole_line[500];
//skip the lines that start with # or %
while (in->peek() == 35 || in->peek() == 37) {
in->getline(whole_line, 500, '\n');
//cerr<<"skipping in eutectic"<<whole_line<<"\n";
}
if (lookFor("1", in) == true)
if (lookFor("S", in) == true)
if (lookFor("C", in) == true)
if (lookFor("B", in) == true)
if (lookFor("T", in) == true)
return 3;//type=3;
/*
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
if (lookFor("1", in) == true)
if (lookFor("P", in) == true)
return 3;
*/
//checking for the first compartment id = 1 and pid = -1, also count of #tokens in the first line == 7 only.
if (in->fail() || in->peek() == -1){
in->clear();
in->seekg(0, ios::beg);
}
//skip the lines that start with # or %
while (in->peek() == 35 || in->peek() == 37) {
in->getline(whole_line, 500, '\n');
//cerr<<"skipping in SWC format"<<whole_line<<"\n";
}
//cerr<<"beginning char.."<<in->peek();
// Parses the input file to check if the file is in SWC format. it looks for 1 1 and -1 mathces.
if (lookFor(SWC_TAG1, in) == true){
if (lookFor(SWC_TAG2, in) == true)
if (lookFor(SWC_TAG3, in) == true)
if (lookFor(SWC_TAG4, in) == true)
return 0;
}
return 5;
}
//modified the return type to bool from void by sridevi on 11/29/04
bool Neuron::lookFor(char * string, ifstream* in) {
char c[500];
*in >> c;
//cerr<<"starting string:"<<c<<"\n";
//cerr<<string<<" "<<strlen(string)<<"\n";
//cerr<<"checking.."<<in->peek()<<"\n";
//cerr<<"checking again.."<<in->peek()<<"\n";
//while (strncmp(string, c, strlen(string)) != 0 && !in->eof() && in->peek()!= -1) {
//skip lines that start with '#'
while((!in->eof() || !in->fail()) && strncmp(c,string,strlen(string))!=0){
//cout<<"c............... :"<<c<<"\n";
*in >> c;
//cerr<< ".."<<c<<" "<<in->peek()<<"\n";
}
//cerr << "Input line"<< c;
//cerr<<"string:"<<string<<"c:"<<c<<"\n";
if (memcmp(string, c, strlen(string)) == 0) {
//if(strncmp(string,c,strlen(string))==0){
cerr<<"c............... :"<<c<<" String........: "<<string<<"\n";
return true;
} else
return false;
}
double * Neuron::getValues(double * ret) {
char c = 'y';
double xSum = 0, ySum = 0, zSum = 0, rSum = 0, xSq = 0, ySq = 0, zSq = 0,
x, y, z, r;
int count = 0;
{
x = -1;
y = -1;
z = -1;
while (c != -1) {
while (c != '(' && c != -1 && c != ')') {
in->peek();
c = in->get();
}
if (c == ')')
break;
//remove spaces after '('. added on 09/10/2010 by Sridevi. Otherwise the soma points are skipped
while (in->peek() == ' ' && in->peek() != -1)
in->get();
if ((in->peek() >= '0' && in->peek() <= '9') || in->peek() == '-') {
*in >> x;
*in >> y;
*in >> z;
*in >> r;
//cout<<x<<" "<<y<<" "<<z<<" "<<r<<endl;
xSum += x;
ySum += y;
zSum += z;
rSum += r;
xSq += x * x;
ySq += y * y;
zSq += z * z;
count++;
}
//go to end line
while (c != 13 && c != 10) {
in->peek();
c = in->get();
}
if (in->peek() == ')')
break;
}
ret[0] = xSum / count;
ret[1] = ySum / count;
ret[2] = zSum / count;
double tmp = 0;
double xvar = 0, yvar = 0, zvar = 0;
tmp = xSq / count - ret[0] * ret[0];
if (tmp > 0)
xvar = sqrt(tmp);
tmp = ySq / count - ret[1] * ret[1];
if (tmp > 0)
yvar = sqrt(tmp);
tmp = zSq / count - ret[2] * ret[2];
if (tmp > 0)
zvar = sqrt(tmp);
double soma = (xvar + yvar + zvar) / 3 * 4;
ret[3] = soma;
c = in->peek();
}//else
return ret;
}
int Neuron::NeurolAdd(int id, int type, double x0, double y0, double z0) {
int biforc[1000];
biforc[0] = 1;
biforc[1] = 1;
int index = 0;
int lastindex = 0;
int lookForCloseParenthesis = 0;
int pid;
int markctr = 0;
pid = 1;
int endTree = 0;
while (in->peek() != -1 && endTree == 0) {
int save = 0;
int foundBar = 0;
index = 0;
lookForCloseParenthesis = 0;
save = 0;
char c = 'y';
double x, y, z, d;
while (c != '(' && c != '|' && in->peek() != -1) {
in->peek();
c = in->get();
if (lookForCloseParenthesis == 1 && c == ')')
lookForCloseParenthesis = 2;
if (c == 10 || c == 13) {
index = 0;
lookForCloseParenthesis = 1;
}
if (c == ' ')
index++;
if (lookForCloseParenthesis == 2) {
char k[100];
*in >> k;
if (strcmp(k, "tree") == 0) {
index = 0;
lastindex = 0;
biforc[2] = 1;
pid = 1;
endTree = 1;
break;
} else if (strcmp(k, "split") == 0) {
lookForCloseParenthesis = 0;
}
}
}
if (c == '|')
foundBar = 1;
//remove spaces after '('
while (in->peek() == ' ' && in->peek() != -1)
in->get();
//if the line does not contains anything then save the previous id
if (in->peek() == 10 || in->peek() == 13)
save = 1;
// when found a '|' correct for pid
if (lastindex == index && foundBar == 1) {
int jj = 0;
foundBar = 0;
pid = biforc[index];
}
if (lastindex > index && save == 1) {
//after a termination
pid = biforc[index];
lastindex = index;
}
if (lastindex < index && save == 1) {
//new biforcation
biforc[index] = id;
lastindex = index;
}
//probable location for error!!
c = in->peek();
if (c == -1) {
int stop = 1;
}
// to skip marker blocks. when R is not present the line is skipped.
int pos;
pos = in->tellg();
char tmp[200];
in->getline(tmp, 200);
if (strstr(tmp, "Marker") != NULL || strstr(tmp, "Spines") != NULL
|| strstr(tmp, "spines") != NULL || strstr(tmp, "marker")
!= NULL) {
markctr++;
while (strstr(tmp, "End of markers") == NULL) {
pos = in->tellg();
in->getline(tmp, 200);
}
}
//adding second parameter here or eclipse is showing it as invalid argument error.
in->seekg(pos,ios::beg);
//if a number follow get the segment cooordinates
if ((in->peek() >= '0' && in->peek() <= '9') || in->peek() == '-') {
id++;
//get segment
c = in->peek();
x = -1;
y = -1;
z = -1;
*in >> x;
*in >> y;
*in >> z;
*in >> d;
//cout<<x<<' '<<y<<' '<<z<<' '<<d<<"\n";
if (soma == 0) {
//create a soma
add(1, type, x - x0 - 0.001, y - y0 - 0.001, z - z0 - 0.001, d,
-1);
id = 2;
pid = 1;
}
add(id, type, x - x0, y - y0, z - z0, d, pid);
pid = id;
}
//go to end line
while (c != 13 && c != -1 && c != 10) {
in->peek();
c = in->get();
}
}
//cout<<"markctr:"<<markctr<<"\n";
return id;
}
;
void Neuron::OpenNeuroL() {
//translation cood, to have soma always in 0,0,0
double x0 = 0, y0 = 0, z0 = 0;
strcpy(
d[0],
"# Neurolucida to SWC conversion from L-Measure. Sridevi Polavaram: [email protected]");
strcpy(d[1], "# Original fileName:");
strcat(d[1], getFileName(name));
strcpy(d[2], "#");
d[3][0] = 0;
int cellBody_cnt = 0;
char buffer [33];
//store biforc
double x = 0, y = 0, z = 0;
double ret[5];
//initialize the ret array to a big integer
ret[0] = ret[1] = ret[2] = ret[3] = ret[4] = -999999999;
//modified the code such that all cellbody tags are searched at once sri 07/22/2010
if (in->fail())
in->clear();
in->seekg(0, ios::beg);
int id = 1; int pid = -1;
while (!in->fail()) {
if (lookFor("(CellBody)", in)) {
cerr << "CellBody tag for cell body.." << endl;
getValues(ret);