/
bnet.mod
670 lines (621 loc) · 21.2 KB
/
bnet.mod
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
: $Id: bnet.mod,v 1.49 2012/08/09 16:26:55 samn Exp $
:* main COMMENT
COMMENT
boolean network
ENDCOMMENT
:* main VERBATIM block
VERBATIM
#include "misc.h"
// BP gets a pointer to the boonet* that belongs to each BNET object
#define BP (*((boonet**) &(_p_sop)))
// node (can represent a molecule)
typedef struct BNODE {
char* name; // name of the node
int id; // id
int state; // state of the node
int count; // temporary counter
int knockout; // is this node knocked-out?
int start; // starting state
int sthresh; // special threshold for switching the node's state -- 0 means it's a normal node
int scount; // special count (# of times its rule is activated)
double x,y,z; // location of the node
} bnode;
// represents a rule (contains edges or links between nodes)
typedef struct BRULE {
struct BNODE** psrc; // source (usually 1 node but can be ANDed)
struct BNODE* ptarg; // pointer to target
int weight; // weight of the rule ( < 0 == inhib, > 0 == activate )
int* psrcstate; // state of source nodes required for this rule
int nsrc; // # of sources in the rule
} brule;
// C-level structure/representation of the boolean network (stores nodes)
typedef struct BOONET {
struct BNODE* pnodes;
int numnodes;
int id;
struct BRULE* prules; // list of outgoing rules (pointers to targets and weights)
int nrules; // number of outgoing edges
int rulebufsz; // prules buffer size (in units of brules)
} boonet;
// set the state of each node to its starting value
void startboonet (boonet* pnet) {
int i;
if(verbose) printf("startboonet\n");
for(i=0;i<pnet->numnodes;i++) {
pnet->pnodes[i].count = pnet->pnodes[i].scount = 0;
pnet->pnodes[i].state = pnet->pnodes[i].start; // set to starting
}
}
// create and return the C-level structure/representation of the boolean network
boonet* makeboonet (int numnodes) {
int i;
boonet* pnet;
pnet = (boonet*) calloc(1, sizeof(boonet)); // allocate memory for the boonet
pnet->pnodes = (bnode*) calloc(numnodes, sizeof(bnode)); // allocate memory for the nodes
pnet->numnodes = numnodes; // store # of nodes
startboonet(pnet); // set states to starting values
for(i=0;i<numnodes;i++) pnet->pnodes[i].id = i; // assign the IDs
return pnet;
}
// free a single bnode
void freebnode(bnode* p) {
if(p->name) {
free(p->name);
p->name = 0x0;
}
}
// free a single brule's memory
void freebrule(brule* p) {
if(p->psrc) free(p->psrc);
if(p->psrcstate) free(p->psrcstate);
}
// free the entire boolean network
void freeboonet (boonet* pnet) {
int i;
if(pnet->pnodes) {
for(i=0;i<pnet->numnodes;i++) freebnode(&pnet->pnodes[i]);
free(pnet->pnodes);
}
pnet->pnodes = 0x0;
if(pnet->prules) {
for(i=0;i<pnet->nrules;i++) freebrule(&pnet->prules[i]);
free(pnet->prules);
pnet->prules = 0x0;
}
free(pnet);
}
// add a rule from psrc to targid with specified weight and source state
// return pointer to the rule
brule* addrule (boonet* pnet, double* psrc, int targid, double weight, double* psrcstate, int nsrc) {
int idx,i;
if(pnet->prules == 0x0) { // take care of memory allocation
pnet->rulebufsz = 16;
pnet->prules = (struct BRULE*) calloc(pnet->rulebufsz, sizeof(brule) );
pnet->nrules = 0;
} else if(pnet->nrules >= pnet->rulebufsz) {
pnet->rulebufsz *= 2;
pnet->prules = (struct BRULE*) realloc(pnet->prules, pnet->rulebufsz * sizeof(brule) );
}
idx = pnet->nrules;
pnet->prules[idx].nsrc = nsrc; // set # of sources
pnet->prules[idx].ptarg = &pnet->pnodes[targid]; // set target
pnet->prules[idx].weight = weight; // set weight
// set the sources
pnet->prules[idx].psrc = (struct BNODE **) calloc(nsrc, sizeof(bnode*));
for(i=0;i<nsrc;i++) pnet->prules[idx].psrc[i] = &pnet->pnodes[(int)psrc[i]];
// set source state for rule to be 'on'
pnet->prules[idx].psrcstate = (int *) calloc(nsrc, sizeof(int));
for(i=0;i<nsrc;i++) pnet->prules[idx].psrcstate[i] = (int) psrcstate[i];
return &pnet->prules[ pnet->nrules++ ]; // return the new rule and inc # rules
}
// advance the full boolean network by a single iteration
void advanceboonet (boonet* pnet) {
int i , j, nn = pnet->numnodes, nsrcact;
bnode *pnodes = pnet->pnodes;
brule *prule;
for(i=0;i<nn;i++) pnodes[i].count = 0; // initialize temporary counts to 0
for(i=0;i<pnet->nrules;i++) {
prule = &pnet->prules[i];
nsrcact = 0; // number of sources that are activated
for(j=0;j<prule->nsrc;j++) { // go thru the sources of the rule
if(prule->psrc[j]->knockout) continue; // skip knockouts
if(prule->psrc[j]->state == prule->psrcstate[j]){//check if source node in correct state
nsrcact += 1; // count number of source nodes in correct state (for activation of the rule)
}
}
if(prule->nsrc == nsrcact) prule->ptarg->count += prule->weight;//integrate if ALL sources in proper state
}
for(i=0;i<nn;i++) { // update the state of each node based on its inputs
if(pnodes[i].knockout) {pnodes[i].scount=pnodes[i].count=pnodes[i].state=0; continue;}
if(pnodes[i].count > 0) { // turn on (more activating vs inhibiting input)
if(pnodes[i].sthresh > 0) { // special node?
pnodes[i].scount += 1; // increase the special counter
} else {//normal node so gets turned on when activation count > inhibition count
pnodes[i].state = 1;
}
} else if(pnodes[i].count<0||(pnodes[i].sthresh>0 && pnodes[i].count==0)){//turn off (inhibitory > activating input) or special node
pnodes[i].state = 0;
pnodes[i].scount = 0; // special node count reset to 0
}
if(pnodes[i].sthresh > 0){//set the state of any special nodes here
if(pnodes[i].scount >= pnodes[i].sthresh) { // passed the threshold? turn on
pnodes[i].state = 1;
} else pnodes[i].state = 0;
}
}
}
ENDVERBATIM
:* NEURON, PARAMETER, ASSIGNED blocks
NEURON {
ARTIFICIAL_CELL BNET
: POINT_PROCESS BNET
RANGE tstep
RANGE xloc, yloc, zloc
GLOBAL verbose, installed
POINTER sop :::: Structure pointer for the boolean network
}
PARAMETER {
tstep = 0
verbose = 0
sop = 0
}
ASSIGNED {
installed
}
:* CONSTRUCTOR, DESTRUCTOR, INITIAL
:** CONSTRUCT: create a structure to save the identity of this unit and char integer flags
CONSTRUCTOR {
VERBATIM
boonet* pnet;
int sz;
if((sz = (int)*getarg(1)) < 1) {
printf("BNET err0: must have a network with positive # of nodes!\n");
hxe();
}
_p_sop = (double*) makeboonet(sz);
pnet = BP;
pnet->id = ifarg(2) ? (int) *getarg(2) : 0;
ENDVERBATIM
}
: setrule(vsource, targid, weight, vsourcestate) - sets the rule from a set of 3 vectors
: vsource has node IDs of sources, targid is ID of target, weight is weight
: vsourcestate has states the source must be in for the given rule to be turned on.
FUNCTION setrule () {
VERBATIM
double *psrc, weight, *psrcstate;
int targid, i, nsrc;
if (! ifarg(4) ) {
printf("BNET.setrule(vsource, targid, weight, vsourcestates) - sets a rule.\n");
printf("vsource has node IDs of sources, targid is ID of target, weight is (-1,1) for inhib/activating\n");
printf("vsourcestate has states the source must be in for the given rule to be turned on.\n");
return 0.0;
}
if ( (nsrc = vector_arg_px(1,&psrc)) < 1) {
printf("BNET.setrule WARN0: empty source Vector!\n");
return 0.0;
}
targid = (int) *getarg(2);
if(targid < 0 || targid >= BP->numnodes) {
printf("BNET.setrule ERR0: invalid target id : %d\n",targid);
return 0.0;
}
weight = (int) *getarg(3);
if( nsrc != vector_arg_px(4,&psrcstate) ) {
printf("BNET.setrule ERR1: vsource, vsourcestate must have same size!\n");
return 0.0;
}
for(i=0;i<nsrc;i++) {
if( psrc[i] < 0 || psrc[i] >= BP->numnodes) {
printf("BNET.setrule ERR2: invalid source node id %d. netsize=%d\n",(int)psrc[i],BP->numnodes);
return 0.0;
}
if(verbose>1) printf("adding rule from %d -> %d : w = %g\n",(int)psrc[i],targid,weight);
}
addrule(BP, psrc, targid, weight, psrcstate, nsrc);
return 1.0;
ENDVERBATIM
}
: get rid of all the edges (but keep the nodes)
PROCEDURE clearrules () {
VERBATIM
BP->nrules = 0;
ENDVERBATIM
}
: print the network
PROCEDURE pr () {
VERBATIM
int i, j, k;
bnode* pnodes = BP->pnodes;
brule* prule;
char srcstr[4096], stmp[4096];
printf("net: numnodes=%d, numrules=%d\n",BP->numnodes,BP->nrules);
for(i=0;i<BP->numnodes;i++) {
if(pnodes[i].name) {
printf("%s: state=%d, count=%d\n",pnodes[i].name,pnodes[i].state,pnodes[i].count);
} else {
printf("%d: state=%d, count=%d\n",i,pnodes[i].state,pnodes[i].count);
}
}
for(i=0;i<BP->nrules;i++) {
prule = &BP->prules[i];
srcstr[0]=0;
for(j=0;j<prule->nsrc;j++) {
if(prule->psrc[j]->name) {
sprintf(stmp,"%s%s%s ", j>0?"AND ":"", prule->psrcstate[j]?"":"!", prule->psrc[j]->name);
} else {
sprintf(stmp,"%s%s%d ", j>0?"AND ":"", prule->psrcstate[j]?"":"!", prule->psrc[j]->id);
}
strcat(srcstr,stmp);
}
if(prule->ptarg->name) {
printf("%s-> %s , w = %d\n",srcstr,prule->ptarg->name,prule->weight);
} else {
printf("%s-> %d , w = %d]\n",srcstr,prule->ptarg->id,prule->weight);
}
}
ENDVERBATIM
}
: BNET.graphviz([dotname,imageename,format,L->R direction,width,height,fontsize]) - print the network as a graphviz string,
: and optionally save to a dot file (dotname) and to png/pdf. format should be png or pdf or other output formats
: supported by graphviz.
FUNCTION graphviz () {
VERBATIM
int i, j, k, LR, fsz, w, h;
bnode* pnodes = BP->pnodes;
brule* prule;
char buf[4096], *dotname, *fname, fontsize[128];
double penw; // penwidth
FILE* fp = 0x0;
dotname = ifarg(1) ? gargstr(1) : 0x0;
fname = ifarg(2) ? gargstr(2) : 0x0;
const char* ext = ifarg(3) ? gargstr(3) : "gif";
LR = ifarg(4) ? (int) *getarg(4) : 1;
w = ifarg(5) ? (int) *getarg(5) : -1;
h = ifarg(6) ? (int) *getarg(6) : -1;
fsz = ifarg(7) ? (int) *getarg(7) : -1;
if(fsz==-1) sprintf(fontsize,"%s"," "); else sprintf(fontsize,"fontsize=%d,",fsz);
if(fname) if( !(fp = fopen(dotname,"w"))) {
printf("BNET.graphviz ERR0: could not open %s\n",fname);
return 0.0;
}
sprintf(buf, "%s", "digraph G {\n"); if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf);
if(LR){sprintf(buf, "%s", "\trankdir=LR;\n"); if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf); }
if(w>0 && h>0) {sprintf(buf, "size=\"%d,%d\"\n",w,h); if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf);}
for(i=0;i<BP->numnodes;i++) {
const char* ncolor = BP->pnodes[i].knockout ? "white" : BP->pnodes[i].state > 0 ? "black" : "gray";
const char* fcolor = BP->pnodes[i].knockout ? "black" : "white";
const char* shape = pnodes[i].sthresh > 0 ? "invtriangle" : "doublecircle";
if(BP->pnodes[i].name) {
sprintf(buf,"\t%s [fontcolor=%s,%sstyle=filled,shape=%s,fillcolor=%s,color=%s]\n",
BP->pnodes[i].name,fcolor,fontsize,shape,ncolor,ncolor);
} else {
sprintf(buf,"\t%d [fontcolor=%s,%sstyle=filled,shape=%s,fillcolor=%s,color=%s]\n",
i,fcolor,fontsize,shape,ncolor,ncolor);
}
if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf);
}
for(i=0;i<BP->nrules;i++) {
prule = &BP->prules[i];
for(j=0;j<prule->nsrc;j++) {
penw = prule->psrcstate[j] == prule->psrc[j]->state ? 6.0 : 1.0;
const char* arrowtype = prule->weight < 0 ? "tee" : "open";
const char* lstyle = prule->psrcstate[j] == 0 ? ",style=dashed" : " ";
if(prule->psrc[j]->name) {
if(prule->ptarg->name) {
sprintf(buf,"\t%s -> %s [arrowhead=%s,penwidth=%g,color=%s%s]\n",
prule->psrc[j]->name,prule->ptarg->name,arrowtype,penw,prule->weight>0?"red":"blue",lstyle);
} else {
sprintf(buf,"\t%s -> %d [arrowhead=%s,penwidth=%g,color=%s%s]\n",
prule->psrc[j]->name,prule->ptarg->id,arrowtype,penw,prule->weight>0?"red":"blue",lstyle);
}
} else if(prule->ptarg->name) {
sprintf(buf,"\t%d -> %s [arrowhead=%s,penwidth=%g,color=%s%s]\n",
prule->psrc[j]->id,prule->ptarg->name,arrowtype,penw,prule->weight>0?"red":"blue",lstyle);
} else {
sprintf(buf,"\t%d -> %d [arrowhead=%s,penwidth=%g,color=%s%s]\n",
prule->psrc[j]->id,prule->ptarg->id,arrowtype,penw,prule->weight>0?"red":"blue",lstyle);
}
if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf);
}
}
sprintf(buf,"%s","}\n"); if(fp) fprintf(fp,"%s",buf); else fprintf(stdout,"%s",buf);
if(fp) fclose(fp);
if(fname) {
sprintf(buf,"dot %s -T%s > %s",dotname,ext,fname);
if(0!=system(buf)) {printf("BNET.graphviz ERR1 : couldn't run %s\n",buf); return 0.0;}
}
return 1.0;
ENDVERBATIM
}
DESTRUCTOR {
VERBATIM
freeboonet(BP);
ENDVERBATIM
}
: all nodes in network set to starting states
PROCEDURE start () {
tstep = 0
VERBATIM
startboonet(BP);
ENDVERBATIM
}
:** INITIAL
INITIAL {
start()
}
: strvalid - return the index to be used for get/setnodevals
FUNCTION strvalid () {
VERBATIM
char *pname;
static char *pnames[6] = {"state", "count", "knockout", "start", "sthresh", "scount"};
int i;
pname = gargstr(1);
for(i=0;i<6;i++) {
if(!strcmp(pname,pnames[i])) return i;
}
return -1;
ENDVERBATIM
}
:** getscount(vec) - retrieves BNET scount into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getscount () {
VERBATIM
double *ps; int i; void *vs;
if(!ifarg(1)) {
printf("BNET.getscount(vec) - returns scount of each node in vec\n");
return 0;
}
vs = vector_arg(1);
ps = vector_newsize(vs,BP->numnodes);
for(i=0;i<BP->numnodes;i++) ps[i] = (double) BP->pnodes[i].scount;
ENDVERBATIM
}
:** setscount(vec) - sets vector vec into BNET node scount. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setscount () {
VERBATIM
double *ps; int i, sz;
if(!ifarg(1)) {
printf("BNET.setscount(vec) - sets scount of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&ps)) != BP->numnodes ) {
printf("BNET.setscount ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].scount = (int) ps[i];
return 1.0;
ENDVERBATIM
}
:** getsthresh(vec) - retrieves BNET sthresh into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getsthresh () {
VERBATIM
double *ps; int i; void *vs;
if(!ifarg(1)) {
printf("BNET.getsthresh(vec) - returns sthresh of each node in vec\n");
return 0;
}
vs = vector_arg(1);
ps = vector_newsize(vs,BP->numnodes);
for(i=0;i<BP->numnodes;i++) ps[i] = (double) BP->pnodes[i].sthresh;
ENDVERBATIM
}
:** setsthresh(vec) - sets vector vec into BNET node sthresh. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setsthresh () {
VERBATIM
double *ps; int i, sz;
if(!ifarg(1)) {
printf("BNET.setsthresh(vec) - sets sthresh of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&ps)) != BP->numnodes ) {
printf("BNET.setsthresh ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].sthresh = (int) ps[i];
return 1.0;
ENDVERBATIM
}
:** getcount(vec) - retrieves BNET count into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getcount () {
VERBATIM
double *ps; int i; void *vs;
if(!ifarg(1)) {
printf("BNET.getcount(vec) - returns count of each node in vec\n");
return 0;
}
vs = vector_arg(1);
ps = vector_newsize(vs,BP->numnodes);
for(i=0;i<BP->numnodes;i++) ps[i] = (double) BP->pnodes[i].count;
ENDVERBATIM
}
:** setcount(vec) - sets vector vec into BNET node counts. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setcount () {
VERBATIM
double *ps; int i, sz;
if(!ifarg(1)) {
printf("BNET.setcount(vec) - sets count of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&ps)) != BP->numnodes ) {
printf("BNET.setcount ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].count = (int) ps[i];
return 1.0;
ENDVERBATIM
}
:** getstate(vec) - retrieves BNET state into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getstate () {
VERBATIM
double *ps; int i; void *vs;
if(!ifarg(1)) {
printf("BNET.getstate(vec) - returns state of each node in vec\n");
return 0;
}
vs = vector_arg(1);
ps = vector_newsize(vs,BP->numnodes);
for(i=0;i<BP->numnodes;i++) ps[i] = (double) BP->pnodes[i].state;
ENDVERBATIM
}
:** setstate(vec) - sets vector vec into BNET node states. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setstate () {
VERBATIM
double *ps; int i, sz;
if(!ifarg(1)) {
printf("BNET.setstate(vec) - sets state of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&ps)) != BP->numnodes ) {
printf("BNET.setstate ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].state = (int) ps[i];
return 1.0;
ENDVERBATIM
}
:** getstart(vec) - retrieves BNET start states into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getstart () {
VERBATIM
double *ps; int i; void *vs;
if(!ifarg(1)) {
printf("BNET.getstart(vec) - returns start state of each node in vec\n");
return 0;
}
vs = vector_arg(1);
ps = vector_newsize(vs,BP->numnodes);
for(i=0;i<BP->numnodes;i++) ps[i] = (double) BP->pnodes[i].start;
ENDVERBATIM
}
:** setstart(vec) - sets vector vec into BNET node start states. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setstart () {
VERBATIM
double *ps; int i, sz;
if(!ifarg(1)) {
printf("BNET.setstart(vec) - sets start state of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&ps)) != BP->numnodes ) {
printf("BNET.setstart ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].start = (int) ps[i];
return 1.0;
ENDVERBATIM
}
:** setknockout(vec) - sets vector vec into BNET node knockout flag variables. each element indexed into the vector
: corresponds to the node with the given id.
FUNCTION setknockout () {
VERBATIM
double *pk; int i, sz;
if(!ifarg(1)) {
printf("BNET.knockout(vec) - sets knockout flag of each node in vec\n");
return 0.0;
}
if( (sz = vector_arg_px(1,&pk)) != BP->numnodes ) {
printf("BNET.knockout ERR0: vec.size(%d) != BNET.numnodes(%d)\n",sz,BP->numnodes);
return 0.0;
}
for(i=0;i<BP->numnodes;i++) BP->pnodes[i].knockout = (int) pk[i];
return 1.0;
ENDVERBATIM
}
:** getknockout(vec) - retrieves BNET knockout flags into a vector. each element indexed into the vector
: corresponds to the node with the given id.
PROCEDURE getknockout () {
VERBATIM
double *pk; int i; void *vk;
if(!ifarg(1)) {
printf("BNET.getknockout(vec) - returns knockout flag of each node in vec\n");
return 0;
}
vk = vector_arg(1);
pk = vector_newsize(vk,BP->numnodes);
for(i=0;i<BP->numnodes;i++) pk[i] = (double) BP->pnodes[i].knockout;
ENDVERBATIM
}
: BNET.setnname(node id, string name) - set node name
FUNCTION setnname () {
VERBATIM
int id, sz; char *name;
id = (int) *getarg(1);
if(id < 0 || id >= BP->numnodes) {
printf("BNET.setnname ERR0: invalid node index %d\n",id);
return 0.0;
}
name = gargstr(2);
if(!(sz=strlen(name))) {
printf("BNET.setnname ERR1: empty string\n");
return 0.0;
}
if(BP->pnodes[id].name) free(BP->pnodes[id].name);
if(!(BP->pnodes[id].name = (char*) malloc(sizeof(char) * (sz + 1)))) {
printf("BNET.setnname ERR2: couldn't alloc mem for %s\n",name);
return 0.0;
}
strcpy(BP->pnodes[id].name,name);
return 1.0;
ENDVERBATIM
}
: getnname(node id, string) - get node name
FUNCTION getnname () {
VERBATIM
int i, id, sz; char **pname, string[BUFSIZ];
id = (int) *getarg(1);
if(id < 0 || id >= BP->numnodes) {
printf("BNET.getnname ERR0: invalid node index %d\n",id);
return 0.0;
}
if(!BP->pnodes[id].name || !(sz=strlen(BP->pnodes[id].name))) {
printf("BNET.getnname ERR1: node %d has no name\n",id);
return 0.0;
}
for(i=0;i<sz && i<BUFSIZ;i++) string[i] = BP->pnodes[id].name[i];
if(i < BUFSIZ) string[i]=0;
printf("Aname is %s, %s\n",BP->pnodes[id].name,string);
pname = hoc_pgargstr(2);
printf("Bname is %s, %s\n",BP->pnodes[id].name,string);
hoc_assign_str(pname,string);
return 1.0;
ENDVERBATIM
}
: advance the BNET by a single iteration - called from a simulation loop
FUNCTION advancebn () {
VERBATIM
advanceboonet(BP);
tstep = tstep + 1;
return tstep;
ENDVERBATIM
}
FUNCTION advancebnfor () {
VERBATIM
int i, n;
n = (int) *getarg(1);
for(i=0;i<n;i++) advancebn();
return tstep;
ENDVERBATIM
}
FUNCTION numnodes () {
VERBATIM
return BP->numnodes;
ENDVERBATIM
}
FUNCTION numrules () {
VERBATIM
return BP->nrules;
ENDVERBATIM
}
: identifier for BNET
FUNCTION id () {
VERBATIM
return (double) BP->id;
ENDVERBATIM
}