Bsoft  2.1.0
Bernard's software package
tcltk_bfil.cpp
Go to the documentation of this file.
1 
9 // Tk must be included before anything else to remedy symbol conflicts
10 #include <tk.h>
11 
12 #include "tcltk_bfil.h"
13 #include "mg_extract.h"
14 #include "mg_helix.h"
15 #include "mg_select.h"
16 #include "rwmg.h"
17 #include "linked_list.h"
18 #include "timer.h"
19 #include "utilities.h"
20 
21 #include <sys/stat.h>
22 
23 // Declaration of global variables
24 extern int verbose; // Level of output to the screen
25 extern Bimage* imglist;
26 
27 // Internal function prototypes
29 Tcl_Obj* filament_count(Bfield* field);
30 Tcl_Obj* filament_count(Bmicrograph* mg);
31 Tcl_Obj* filament_count(Breconstruction* rec);
32 Tcl_Obj* filament_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
33 Tcl_Obj* filament_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
34 Tcl_Obj* filament_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
35 Tcl_Obj* filament_extract(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
36 Tcl_Obj* filament_profile(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
37 Tcl_Obj* filament_center(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[]);
38 Tcl_Obj* filament_to_particles(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
39 
40 Tcl_Obj* node_count(Bproject* project);
41 Tcl_Obj* node_count(Bfield* field);
42 Tcl_Obj* node_count(Bmicrograph* mg);
43 Tcl_Obj* node_count(Breconstruction* rec);
44 Tcl_Obj* node_ids(Bfilament* fillist);
45 Tcl_Obj* node_location(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[]);
46 Tcl_Obj* node_select(Bfilament* fillist, double radius, int objc, Tcl_Obj *CONST objv[]);
47 Tcl_Obj* node_move(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[]);
48 Tcl_Obj* node_create(Bfilament** fillist, int objc, Tcl_Obj *CONST objv[]);
49 Tcl_Obj* node_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[]);
50 Tcl_Obj* node_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[]);
51 Tcl_Obj* node_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[]);
52 
53 
54 /*
55 @brief Functions for filament picking.
56  Filament interface syntax:
57  Bmg filament [item] [action] [params]
58  Actions:
59  count counts the number of filaments
60  delete [all|mg|rec|fid] deletes filaments
61  extract [f] [w] [ax] [sf] [p] extracts filaments from a micrograph
62  profile [fid] [nid] [w] plots a filament profile
63  center [w] centers nodes based on profiles
64  toparticles [w] [b] [r] [a] generates particles along filaments
65  Parameters:
66  fid filament identifier
67  nid node identifier
68  f file name
69  w filament width
70  ax extracted filament axis
71  sf split flag
72  p filament path
73  b boxing interval
74  r helical rise
75  a helical rotation angle
76 */
77 Tcl_Obj* do_filament(Bproject* project, int objc, Tcl_Obj *CONST objv[])
78 {
79  Tcl_Obj* returnObj = NULL;
80 
81  Bstring item = Tcl_GetStringFromObj(objv[2], NULL);
82  if ( verbose & VERB_DEBUG )
83  cout << "DEBUG do_filament: Item: " << item << " (" << item.length() << ")" << endl;
84 
85  Bstring id = item.post(':');
86  id = id.remove(' ');
87  item = item.pre(':');
88 
89  if ( !item.contains("Field") && !item.contains("Micrograph") && !item.contains("Reconstruction") ) {
90  cerr << "Error in do_filament: Item " << item << " must be a micrograph or reconstruction!" << endl;
91  returnObj = Tcl_NewObj();
92  return returnObj;
93  }
94 
95  Bfield* field = NULL;
96  Bmicrograph* mg = NULL;
97  Breconstruction* rec = NULL;
98  Bimage* p = NULL;
99  Bstring filename, filename2, base, delete_what;
100 
101  if ( item.contains("Field") ) {
102  for ( field = project->field; field && field->id != id; field = field->next ) ;
103  if ( field ) {
104  mg = field->mg;
105  filename = field->mg->fmg;
106  } else
107  cerr << "Error in do_filament: Field " << id << " not found!" << endl;
108  } else if ( item.contains("Micrograph") ) {
109  for ( field = project->field; field; field = field->next ) {
110  for ( mg = field->mg; mg && mg->id != id; mg = mg->next ) ;
111  if ( mg ) break;
112  }
113  if ( mg ) {
114  if ( mg->fmg.length() )
115  filename = mg->fmg;
116  else if ( mg->fframe.length() )
117  filename = mg->fframe;
118  } else
119  cerr << "Error in do_filament: Micrograph " << id << " not found!" << endl;
120  } else if ( item.contains("Reconstruction") ) {
121  for ( rec = project->rec; rec && rec->id != id; rec = rec->next ) ;
122  if ( rec ) {
123  filename = rec->frec;
124  } else
125  cerr << "Error in do_filament: Reconstruction " << id << " not found!" << endl;
126  }
127 
128  if ( filename.length() ) {
129  base = filename.base();
130  for ( p = imglist; p; p = p->next ) {
131  filename2 = p->file_name();
132  if ( filename2.base() == base ) break;
133  }
134  }
135 
136  Bstring action = Tcl_GetStringFromObj(objv[3], NULL);
137  if ( verbose & VERB_DEBUG )
138  cout << "DEBUG do_filament: Action: " << action << " (" << action.length() << ")" << endl;
139 
140  if ( action == "count" ) {
141  if ( mg ) returnObj = filament_count(mg);
142  else if ( field ) returnObj = filament_count(field);
143  else if ( rec ) returnObj = filament_count(rec);
144  else returnObj = filament_count(project);
145  } else if ( action == "delete" ) {
146  if ( objc > 4 ) delete_what = Tcl_GetStringFromObj(objv[4], NULL);
147  if ( delete_what == "all" || delete_what == "mg" || delete_what == "rec" )
148  returnObj = filament_delete(project, objc, objv);
149  else if ( mg ) returnObj = filament_delete(mg, objc, objv);
150  else if ( rec ) returnObj = filament_delete(rec, objc, objv);
151  else returnObj = filament_delete(project, objc, objv);
152  } else if ( action == "extract" ) {
153  if ( p ) returnObj = filament_extract(mg, rec, p, objc, objv);
154  } else if ( action == "profile" ) {
155  if ( p ) returnObj = filament_profile(mg, rec, p, objc, objv);
156  } else if ( action == "center" ) {
157  if ( p ) returnObj = filament_center(mg, rec, p, objc, objv);
158  } else if ( action == "toparticles" ) {
159  returnObj = filament_to_particles(project, objc, objv);
160  } else {
161  cerr << "Error: Action " << action << " not recognized!" << endl;
162  }
163 
164  if ( !returnObj ) {
165  returnObj = Tcl_NewObj();
166  Tcl_SetIntObj(returnObj, 0);
167  }
168 
169  return returnObj;
170 }
171 
173 {
174  Tcl_Obj* returnObj = Tcl_NewObj();
175 
176  int nfil(0);
177  Bfield* field = project->field;
178  Bmicrograph* mg;
179  Breconstruction* rec;
180 
181  if ( project->select < 1 ) {
182  for ( field = project->field; field; field = field->next )
183  for ( mg = field->mg; mg; mg = mg->next )
184  if ( mg->fil )
185  nfil += count_list((char *) mg->fil);
186  } else {
187  for ( rec = project->rec; rec; rec = rec->next )
188  if ( rec->fil )
189  nfil += count_list((char *) rec->fil);
190  }
191 
192  Tcl_SetIntObj(returnObj, nfil);
193 
194  return returnObj;
195 }
196 
197 Tcl_Obj* filament_count(Bfield* field)
198 {
199  Tcl_Obj* returnObj = Tcl_NewObj();
200 
201  int nfil(0);
202  Bmicrograph* mg;
203 
204  if ( field ) {
205  for ( mg = field->mg; mg; mg = mg->next )
206  if ( mg->fil )
207  nfil += count_list((char *) mg->fil);
208  }
209 
210  Tcl_SetIntObj(returnObj, nfil);
211 
212  return returnObj;
213 }
214 
216 {
217  Tcl_Obj* returnObj = Tcl_NewObj();
218 
219  int nfil(0);
220 
221  if ( mg && mg->fil )
222  nfil = count_list((char *) mg->fil);
223 
224  Tcl_SetIntObj(returnObj, nfil);
225 
226  return returnObj;
227 }
228 
230 {
231  Tcl_Obj* returnObj = Tcl_NewObj();
232 
233  int nfil(0);
234 
235  if ( rec && rec->fil )
236  nfil = count_list((char *) rec->fil);
237 
238  Tcl_SetIntObj(returnObj, nfil);
239 
240  return returnObj;
241 }
242 
243 Tcl_Obj* filament_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[])
244 {
245  Tcl_Obj* returnObj = Tcl_NewObj();
246 
247  int fid(0), n(-1), all(0);
248 
249  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
250 
251  char string[64] = "";
252  Bfield* field = project->field;
253  Bmicrograph* mg = NULL;
254  Breconstruction* rec;
255  Bfilament* fil = NULL;
256 
257  if ( action == "all" || action == "mg" ) {
258  for ( field = project->field; field; field = field->next ) {
259  for ( mg = field->mg; mg; mg = mg->next ) {
260  filament_kill(mg->fil);
261  mg->fil = NULL;
262  }
263  }
264  all = 1;
265  }
266 
267  if ( action == "all" || action == "rec" ) {
268  for ( rec = project->rec; rec; rec = rec->next ) {
269  filament_kill(rec->fil);
270  rec->fil = NULL;
271  }
272  all = 1;
273  }
274 
275  if ( !all ) {
276  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
277 // if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &n);
278  }
279 
280  if ( fid > 0 ) {
281  if ( project->select < 1 ) {
282  if ( n < 0 ) {
283  if ( field ) for ( mg = field->mg; mg; mg = mg->next ) {
284  for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
285  if ( fil ) {
286  kill_list((char *)fil->node, sizeof(Bfilnode));
287  remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
288  }
289  }
290  } else {
291  if ( field ) for ( mg = field->mg; mg && mg->img_num != n; mg = mg->next ) ;
292  if ( mg ) {
293  for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
294  if ( fil ) {
295  kill_list((char *)fil->node, sizeof(Bfilnode));
296  remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
297  }
298  }
299  }
300  } else {
301  for ( rec = project->rec; rec; rec = rec->next ) {
302  for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
303  if ( fil ) {
304  kill_list((char *)fil->node, sizeof(Bfilnode));
305  remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
306  }
307  }
308  }
309  }
310 
311  sprintf(string, "%d", fid);
312  Tcl_SetStringObj(returnObj, string, strlen(string));
313 
314  return returnObj;
315 }
316 
317 Tcl_Obj* filament_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
318 {
319  Tcl_Obj* returnObj = Tcl_NewObj();
320 
321  int fid(0);
322  Bfilament* fil = NULL;
323 
324  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
325 
326  if ( mg && fid > 0 ) {
327  for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
328  if ( fil ) {
329  kill_list((char *)fil->node, sizeof(Bfilnode));
330  remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
331  }
332  }
333 
334  Tcl_SetIntObj(returnObj, fid);
335 
336  return returnObj;
337 }
338 
339 Tcl_Obj* filament_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
340 {
341  Tcl_Obj* returnObj = Tcl_NewObj();
342 
343  int fid(0);
344  Bfilament* fil = NULL;
345 
346  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
347 
348  if ( rec && fid > 0 ) {
349  for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
350  if ( fil ) {
351  kill_list((char *)fil->node, sizeof(Bfilnode));
352  remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
353  }
354  }
355 
356  Tcl_SetIntObj(returnObj, fid);
357 
358  return returnObj;
359 }
360 
362 {
363  int nnode(0);
364  Bfilnode* fnode = NULL;
365 
366  for ( fnode = fil->node; fnode; fnode = fnode->next ) fnode->id = ++nnode;
367 
368  return nnode;
369 }
370 
371 Tcl_Obj* filament_extract(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
372 {
373  Tcl_Obj* returnObj = Tcl_NewObj();
374 
375  if ( !p ) return returnObj;
376 
377  Bstring filename, filpath;
378  int nfil(0), split(0), axis(0);
379  double filament_width(10);
380 
381  if ( objc > 4 ) filename = Tcl_GetStringFromObj(objv[4], NULL);
382  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &filament_width);
383  if ( objc > 6 ) Tcl_GetIntFromObj(NULL, objv[6], &axis); /* Axis selection: x=1, y=2, z=3 */
384  if ( objc > 7 ) Tcl_GetIntFromObj(NULL, objv[7], &split); /* Flag to split particles into individual images */
385  if ( objc > 8 ) filpath = Tcl_GetStringFromObj(objv[8], NULL);
386 
387 // cout << filename << "\t" << filament_width << "\t" << axis << "\t" << split << endl;
388 
389  if ( filename.length() ) {
390  if ( mg ) {
391  if ( split ) {
392  filament_setup_filenames(mg->fil, filename, filpath);
393  mg->ffil = 0;
394  } else {
395  if ( filpath.length() ) {
396  if ( filename.contains("/") ) filename = filename.post_rev('/');
397  filename = filpath + "/" + filename;
398  }
399  mg->ffil = filename;
400  }
401  nfil = micrograph_extract_filaments(mg, p, filament_width, axis);
402  } else if ( rec ) {
403  if ( split ) {
404  filament_setup_filenames(rec->fil, filename, filpath);
405  rec->ffil = 0;
406  } else {
407  if ( filpath.length() ) {
408  if ( filename.contains("/") ) filename = filename.post_rev('/');
409  filename = filpath + "/" + filename;
410  }
411  rec->ffil = filename;
412  }
413  nfil = reconstruction_extract_filaments(rec, p, filament_width, axis);
414  }
415  }
416 
417  Tcl_SetIntObj(returnObj, nfil);
418 
419  return returnObj;
420 }
421 
422 Tcl_Obj* filament_profile(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
423 {
424  Tcl_Obj* returnObj = Tcl_NewObj();
425 
426  if ( !p ) return returnObj;
427 
428  int fid(0), nid(0);
429  double filament_width(10);
430 
431  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
432  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
433  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &filament_width);
434 
435  long i, n(0), img_num(0);
436  double* profile = NULL;
437  char string[MAXLINELEN] = "";
438  Bfilament* fil = NULL;
439 
440  if ( mg ) {
441  fil = mg->fil;
442  img_num = mg->img_num;
443  } else if ( rec ) fil = rec->fil;
444 
445  if ( fil ) {
446  for ( ; fil && fil->id != fid; fil = fil->next ) ;
447  if ( fil ) {
448  profile = filament_profile(fil->node, p, img_num, nid, filament_width, n);
449  sprintf(string, "%ld", n);
450  Tcl_SetStringObj(returnObj, string, strlen(string));
451  for ( i=0; i<2*n; i++ ) {
452  sprintf(string, " %lf", profile[i]);
453  Tcl_AppendToObj(returnObj, string, strlen(string));
454  }
455  delete[] profile;
456  }
457  }
458 
459  return returnObj;
460 }
461 
462 Tcl_Obj* filament_center(Bmicrograph* mg, Breconstruction* rec, Bimage* p, int objc, Tcl_Obj *CONST objv[])
463 {
464  Tcl_Obj* returnObj = Tcl_NewObj();
465 
466  if ( !p ) return returnObj;
467 
468  int filament_width(40);
469 
470  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &filament_width);
471 
472  long n(0), img_num(0);
473  double cc(0);
474  Bfilament* fil = NULL;
475 
476  if ( mg ) {
477  fil = mg->fil;
478  img_num = mg->img_num;
479  } else if ( rec ) fil = rec->fil;
480 
481  if ( fil ) {
482  cc += filaments_center(fil, p, img_num, filament_width);
483  n++;
484  }
485 
486  if ( n ) cc /= n;
487 
488  Tcl_SetDoubleObj(returnObj, cc);
489 
490  return returnObj;
491 }
492 
493 Tcl_Obj* filament_to_particles(Bproject* project, int objc, Tcl_Obj *CONST objv[])
494 {
495  Tcl_Obj* returnObj = Tcl_NewObj();
496 
497  int nfil(0);
498  double filament_width(10), boxing_interval(0), rise(1e30), angle(0);
499 
500  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &filament_width);
501  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &boxing_interval);
502  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &rise); /* Helical rise */
503  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &angle); /* Helical angle */
504 
505  Vector3<int> box_size((int)filament_width, (int)filament_width, (int)filament_width);
506  nfil = project_filaments_to_particles(project, box_size, boxing_interval, rise, angle);
507 
508  Tcl_SetIntObj(returnObj, nfil);
509 
510  return returnObj;
511 }
512 
513 /*
514  Filament node interface syntax:
515  Bmg node [item] [action] [params]
516  Actions:
517  count counts the number of filament nodes
518  ids returns the identifiers of filaments and nodes
519  location [fid] [nid] returns the location of a node
520  select [x] [y] [z] returns the filament and node identifiers at the given location
521  move [fid] [nid] [dx] [dy] [dz] updates a node location by the given vector
522  create [x] [y] [z] [fid] [nid] creates a new node at the given location and after the given node
523  delete [all|mg|rec|fid] [nid] deletes nodes
524  Parameters:
525  fid filament identifier
526  nid node identifier
527  x x location
528  y y location
529  z z location
530  dx change in x
531  dy change in y
532  dz change in z
533 */
534 Tcl_Obj* do_node(Bproject* project, int objc, Tcl_Obj *CONST objv[])
535 {
536  Tcl_Obj* returnObj = NULL;
537 
538  Bstring item = Tcl_GetStringFromObj(objv[2], NULL);
539  if ( verbose & VERB_DEBUG )
540  cout << "DEBUG do_node: Item: " << item << " (" << item.length() << ")" << endl;
541 
542  Bstring id = item.post(':');
543  id = id.remove(' ');
544  item = item.pre(':');
545 
546  if ( !item.contains("Field") && !item.contains("Micrograph") && !item.contains("Reconstruction") ) {
547  cerr << "Error in do_node: Item " << item << " must be a micrograph or reconstruction!" << endl;
548  returnObj = Tcl_NewObj();
549  return returnObj;
550  }
551 
552 
553  double radius(10);
554  Bfield* field = NULL;
555  Bmicrograph* mg = NULL;
556  Breconstruction* rec = NULL;
557  Bfilament* fil = NULL;
558  Bstring delete_what;
559 
560  if ( item.contains("Field") ) {
561  for ( field = project->field; field && field->id != id; field = field->next ) ;
562  } else if ( item.contains("Micrograph") ) {
563  for ( field = project->field; field; field = field->next ) {
564  for ( mg = field->mg; mg && mg->id != id; mg = mg->next ) ;
565  if ( mg ) break;
566  }
567  if ( mg ) {
568  fil = mg->fil;
569  radius = mg->fil_node_radius;
570  } else cerr << "Error in do_node: Micrograph " << id << " not found!" << endl;
571  } else if ( item.contains("Reconstruction") ) {
572  for ( rec = project->rec; rec && rec->id != id; rec = rec->next ) ;
573  if ( rec ) {
574  fil = rec->fil;
575  radius = rec->fil_node_radius;
576  } else cerr << "Error in do_node: Reconstruction " << id << " not found!" << endl;
577  }
578 
579  Bstring action = Tcl_GetStringFromObj(objv[3], NULL);
580  if ( verbose & VERB_DEBUG )
581  cout << "DEBUG do_node: Action: " << action << " (" << action.length() << ")" << endl;
582 
583  if ( action == "count" ) {
584  if ( mg ) returnObj = node_count(mg);
585  else if ( field ) returnObj = node_count(field);
586  else if ( rec ) returnObj = node_count(rec);
587  else returnObj = node_count(project);
588  } else if ( action == "ids" ) {
589  if ( fil ) returnObj = node_ids(fil);
590  } else if ( action == "location" ) {
591  if ( fil ) returnObj = node_location(fil, objc, objv);
592  } else if ( action == "select" ) {
593  if ( fil ) returnObj = node_select(fil, radius, objc, objv);
594  } else if ( action == "move" ) {
595  if ( fil ) returnObj = node_move(fil, objc, objv);
596  } else if ( action == "create" ) {
597  if ( mg ) returnObj = node_create(&mg->fil, objc, objv);
598  else if ( rec ) returnObj = node_create(&rec->fil, objc, objv);
599  } else if ( action == "delete" ) {
600  if ( objc > 4 ) delete_what = Tcl_GetStringFromObj(objv[4], NULL);
601  if ( delete_what == "all" || delete_what == "mg" || delete_what == "rec" )
602  returnObj = node_delete(project, objc, objv);
603  else if ( mg ) returnObj = node_delete(mg, objc, objv);
604  else if ( rec ) returnObj = node_delete(rec, objc, objv);
605  else returnObj = node_delete(project, objc, objv);
606  } else {
607  cerr << "Error: Action " << action << " not recognized!" << endl;
608  }
609 
610  if ( !returnObj ) {
611  returnObj = Tcl_NewObj(); // Return object should be empty in some cases
612 // Tcl_SetIntObj(returnObj, 0);
613  }
614 
615  return returnObj;
616 }
617 
619 {
620  Tcl_Obj* returnObj = Tcl_NewObj();
621 
622  int nnode(0);
623  Bfield* field = project->field;
624  Bmicrograph* mg = NULL;
625  Breconstruction* rec;
626  Bfilament* fil = NULL;
627 
628  if ( project->select < 1 ) {
629  for ( field = project->field; field; field = field->next )
630  for ( mg = field->mg; mg; mg = mg->next )
631  for ( fil = mg->fil; fil; fil = fil->next )
632  if ( fil->node ) nnode += count_list((char *) fil->node);
633  } else {
634  for ( rec = project->rec; rec; rec = rec->next )
635  for ( fil = rec->fil; fil; fil = fil->next )
636  if ( fil->node ) nnode += count_list((char *) fil->node);
637  }
638 
639  Tcl_SetIntObj(returnObj, nnode);
640 
641  return returnObj;
642 }
643 
644 Tcl_Obj* node_count(Bfield* field)
645 {
646  Tcl_Obj* returnObj = Tcl_NewObj();
647 
648  int nnode(0);
649  Bmicrograph* mg = NULL;
650  Bfilament* fil = NULL;
651 
652  if ( field )
653  for ( mg = field->mg; mg; mg = mg->next )
654  for ( fil = mg->fil; fil; fil = fil->next )
655  if ( fil->node ) nnode += count_list((char *) fil->node);
656 
657  Tcl_SetIntObj(returnObj, nnode);
658 
659  return returnObj;
660 }
661 
662 Tcl_Obj* node_count(Bmicrograph* mg)
663 {
664  Tcl_Obj* returnObj = Tcl_NewObj();
665 
666  int nnode(0);
667  Bfilament* fil = NULL;
668 
669  if ( mg && mg->fil )
670  for ( fil = mg->fil; fil; fil = fil->next )
671  if ( fil->node ) nnode += count_list((char *) fil->node);
672 
673  Tcl_SetIntObj(returnObj, nnode);
674 
675  return returnObj;
676 }
677 
679 {
680  Tcl_Obj* returnObj = Tcl_NewObj();
681 
682  int nnode(0);
683  Bfilament* fil = NULL;
684 
685  if ( rec && rec->fil )
686  for ( fil = rec->fil; fil; fil = fil->next )
687  if ( fil->node ) nnode += count_list((char *) fil->node);
688 
689  Tcl_SetIntObj(returnObj, nnode);
690 
691  return returnObj;
692 }
693 
694 Tcl_Obj* node_ids(Bfilament* fillist)
695 {
696  Tcl_Obj* returnObj = Tcl_NewObj();
697 
698  char string[MAXLINELEN] = "";
699  Bfilament* fil = NULL;
700  Bfilnode* fnode = NULL;
701 
702  if ( fillist ) {
703  for ( fil = fillist; fil; fil = fil->next ) {
704  for ( fnode = fil->node; fnode; fnode = fnode->next ) {
705  sprintf(string, " %d %d", fil->id, fnode->id);
706  Tcl_AppendToObj(returnObj, string, strlen(string));
707  }
708  }
709  }
710 
711  return returnObj;
712 }
713 
714 Tcl_Obj* node_location(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[])
715 {
716  Tcl_Obj* returnObj = Tcl_NewObj();
717 
718  int fid(0), nid(0);
719  char string[MAXLINELEN] = "";
720  Bfilament* fil = NULL;
721  Bfilnode* fnode = NULL;
722 
723  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
724  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
725 
726  if ( fillist ) {
727  for ( fil = fillist; fil && fil->id != fid; fil = fil->next ) ;
728  if ( fil )
729  for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
730  if ( fnode ) {
731  sprintf(string, "%f %f %f", fnode->loc[0], fnode->loc[1], fnode->loc[2]);
732  Tcl_SetStringObj(returnObj, string, strlen(string));
733  }
734  }
735 
736  return returnObj;
737 }
738 
739 Tcl_Obj* node_select(Bfilament* fillist, double radius, int objc, Tcl_Obj *CONST objv[])
740 {
741  Tcl_Obj* returnObj = Tcl_NewObj();
742 
743  int fid(0), nid(0);
744  double x(0), y(0), z(0);
745 
746  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
747  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
748  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
749 
750  double d, dmin;
751  char string[64] = "";
752  Vector3<float> loc(x, y, z);
753  Bfilament* fil = NULL;
754  Bfilnode* fnode = NULL;
755 
756 // cout << "Selection coordinates: " << loc << endl;
757  if ( fillist ) {
758  dmin = 2*radius;
759  for ( fil = fillist; fil; fil = fil->next ) {
760  for ( fnode = fil->node; fnode; fnode = fnode->next ) {
761  d = loc.distance(fnode->loc);
762  if ( dmin > d ) {
763  dmin = d;
764  fid = fil->id;
765  nid = fnode->id;
766  }
767  }
768  }
769  if ( dmin <= radius ) {
770  sprintf(string, "%d %d", fid, nid);
771  Tcl_SetStringObj(returnObj, string, strlen(string));
772  }
773  }
774 
775  return returnObj;
776 }
777 
778 Tcl_Obj* node_move(Bfilament* fillist, int objc, Tcl_Obj *CONST objv[])
779 {
780  Tcl_Obj* returnObj = Tcl_NewObj();
781 
782  int fid(0), nid(0);
783  double dx(0), dy(0), dz(0);
784 
785  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
786  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
787  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &dx);
788  if ( objc > 7 ) Tcl_GetDoubleFromObj(NULL, objv[7], &dy);
789  if ( objc > 8 ) Tcl_GetDoubleFromObj(NULL, objv[8], &dz);
790 
791  Vector3<float> d = Vector3<float>(dx, dy, dz);
792  char string[64] = "";
793  Bfilament* fil = NULL;
794  Bfilnode* fnode = NULL;
795 
796  if ( fillist ) {
797  for ( fil = fillist; fil && fil->id != fid; fil = fil->next ) ;
798  if ( fil )
799  for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
800  if ( fnode ) {
801  fnode->loc += d;
802  sprintf(string, "%d %d", fid, nid);
803  Tcl_SetStringObj(returnObj, string, strlen(string));
804  }
805  }
806 
807  return returnObj;
808 }
809 
810 Tcl_Obj* node_create(Bfilament** fillist, int objc, Tcl_Obj *CONST objv[])
811 {
812  Tcl_Obj* returnObj = Tcl_NewObj();
813 
814  int fid(0), nid(0), fid_max(0), nid_max(0);
815  double x(0), y(0), z(0);
816 
817  if ( objc > 4 ) Tcl_GetDoubleFromObj(NULL, objv[4], &x);
818  if ( objc > 5 ) Tcl_GetDoubleFromObj(NULL, objv[5], &y);
819  if ( objc > 6 ) Tcl_GetDoubleFromObj(NULL, objv[6], &z);
820  if ( objc > 7 ) Tcl_GetIntFromObj(NULL, objv[7], &fid); // Filament
821  if ( objc > 8 ) Tcl_GetIntFromObj(NULL, objv[8], &nid); // Node
822 
823  char string[64] = "";
824  Vector3<float> loc(x, y, z);
825  Bfilament* fil = NULL;
826  Bfilnode* fnode = NULL;
827  Bfilnode* fnode2 = NULL;
828 
829  if ( !(*fillist) ) {
830  fid = nid = 1;
831  fil = filament_add(fillist, fid);
832  }
833 
834  for ( fil = *fillist; fil && fil->id != fid; fil = fil->next )
835  if ( fid_max < fil->id ) fid_max = fil->id;
836 
837  if ( !fil ) {
838  fil = filament_add(fillist, ++fid_max);
839  nid = 1;
840  }
841 
842  for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next )
843  if ( nid_max < nid ) nid_max = nid;
844 
845  if ( !fnode ) {
846  nid = ++nid_max;
847  fnode = filament_node_add(&fil->node, nid);
848  } else {
849  fnode2 = fnode->next;
850  fnode->next = NULL;
851  fnode = filament_node_add(&fnode, ++nid);
852  fnode->next = fnode2;
853  for ( ; fnode2; fnode2 = fnode2->next ) fnode2->id = ++nid;
854  }
855 
856  fnode->loc = loc;
857 
858  sprintf(string, "%d %d", fil->id, fnode->id);
859  Tcl_SetStringObj(returnObj, string, strlen(string));
860 
861  return returnObj;
862 }
863 
864 Tcl_Obj* node_delete(Bproject* project, int objc, Tcl_Obj *CONST objv[])
865 {
866  Tcl_Obj* returnObj = Tcl_NewObj();
867 
868  Bstring action = Tcl_GetStringFromObj(objv[4], NULL);
869 
870  Bfield* field = project->field;
871  Bmicrograph* mg = NULL;
872  Breconstruction* rec;
873 
874  if ( action == "all" || action == "mg" ) {
875  for ( field = project->field; field; field = field->next ) {
876  for ( mg = field->mg; mg; mg = mg->next ) {
877  filament_kill(mg->fil);
878  mg->fil = NULL;
879  }
880  }
881  }
882 
883  if ( action == "all" || action == "rec" ) {
884  for ( rec = project->rec; rec; rec = rec->next ) {
885  filament_kill(rec->fil);
886  rec->fil = NULL;
887  }
888  }
889 
890 // cout << "Deleting all" << endl;
891 
892  return returnObj;
893 }
894 
895 Tcl_Obj* node_delete(Bmicrograph* mg, int objc, Tcl_Obj *CONST objv[])
896 {
897  Tcl_Obj* returnObj = Tcl_NewObj();
898 
899  int fid(0), nid(0);
900  char string[64] = "";
901  Bfilament* fil = NULL;
902  Bfilnode* fnode = NULL;
903 
904  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
905  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
906 
907  if ( mg ) {
908  for ( fil = mg->fil; fil && fil->id != fid; fil = fil->next ) ;
909  if ( fil ) {
910  for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
911  if ( fnode )
912  remove_item((char **)&fil->node, (char *)fnode, sizeof(Bfilnode));
913  filament_renumber(fil);
914  if ( !fil->node )
915  remove_item((char **)&mg->fil, (char *)fil, sizeof(Bfilament));
916  }
917  }
918 
919  sprintf(string, "%d %d", fid, nid);
920  Tcl_SetStringObj(returnObj, string, strlen(string));
921 
922  return returnObj;
923 }
924 
925 
926 Tcl_Obj* node_delete(Breconstruction* rec, int objc, Tcl_Obj *CONST objv[])
927 {
928  Tcl_Obj* returnObj = Tcl_NewObj();
929 
930  int fid(0), nid(0);
931  char string[64] = "";
932  Bfilament* fil = NULL;
933  Bfilnode* fnode = NULL;
934 
935  if ( objc > 4 ) Tcl_GetIntFromObj(NULL, objv[4], &fid);
936  if ( objc > 5 ) Tcl_GetIntFromObj(NULL, objv[5], &nid);
937 
938  if ( rec ) {
939  for ( fil = rec->fil; fil && fil->id != fid; fil = fil->next ) ;
940  if ( fil ) {
941  for ( fnode = fil->node; fnode && fnode->id != nid; fnode = fnode->next ) ;
942  if ( fnode )
943  remove_item((char **)&fil->node, (char *)fnode, sizeof(Bfilnode));
944  filament_renumber(fil);
945  if ( !fil->node )
946  remove_item((char **)&rec->fil, (char *)fil, sizeof(Bfilament));
947  }
948  }
949 
950  sprintf(string, "%d %d", fid, nid);
951  Tcl_SetStringObj(returnObj, string, strlen(string));
952 
953  return returnObj;
954 }
955 
956 
957 
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
tcltk_bfil.h
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:473
Breconstruction::fil
Bfilament * fil
Definition: mg_processing.h:308
node_select
Tcl_Obj * node_select(Bfilament *fillist, double radius, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:739
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
node_create
Tcl_Obj * node_create(Bfilament **fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:810
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bproject::select
int select
Definition: mg_processing.h:342
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bfilament
Definition: mg_processing.h:129
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
filament_delete
Tcl_Obj * filament_delete(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:243
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
node_location
Tcl_Obj * node_location(Bfilament *fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:714
Vector3< int >
filament_add
Bfilament * filament_add(Bfilament **fil, int pid)
Adds a filament parameter structure to a linked list.
Definition: mg_processing.cpp:319
Bfilament::next
Bfilament * next
Definition: mg_processing.h:130
imglist
Bimage * imglist
Definition: bshow.cpp:32
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
mg_select.h
Header file for micrograph processing.
Bfield::id
Bstring id
Definition: mg_processing.h:262
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Bfield
Definition: mg_processing.h:260
node_move
Tcl_Obj * node_move(Bfilament *fillist, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:778
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bmicrograph::fframe
Bstring fframe
Definition: mg_processing.h:207
filament_count
Tcl_Obj * filament_count(Bproject *project)
Definition: tcltk_bfil.cpp:172
Bmicrograph::fil
Bfilament * fil
Definition: mg_processing.h:243
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
project
Bproject * project
Definition: bshow.cpp:33
rwmg.h
Header file for reading and writing micrograph parameters.
Bfilament::id
int id
Definition: mg_processing.h:132
Bmicrograph::ffil
Bstring ffil
Definition: mg_processing.h:209
filament_center
Tcl_Obj * filament_center(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:462
filament_profile
Tcl_Obj * filament_profile(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:422
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
filament_extract
Tcl_Obj * filament_extract(Bmicrograph *mg, Breconstruction *rec, Bimage *p, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:371
Bfilnode::id
int id
Definition: mg_processing.h:118
do_node
Tcl_Obj * do_node(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:534
filament_to_particles
Tcl_Obj * filament_to_particles(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:493
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bfilament::node
Bfilnode * node
Definition: mg_processing.h:133
Bproject
Definition: mg_processing.h:336
node_count
Tcl_Obj * node_count(Bproject *project)
Definition: tcltk_bfil.cpp:618
remove_item
char * remove_item(char **list, char *item, unsigned long size)
Finds the given item and deletes it from the linked list.
Definition: linked_list.cpp:114
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
mg_extract.h
Header file for functions to extract particles from micrographs.
Bfilnode::next
Bfilnode * next
Definition: mg_processing.h:117
utilities.h
Header file for general utilities.
Breconstruction::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:301
do_filament
Tcl_Obj * do_filament(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:77
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
project_filaments_to_particles
int project_filaments_to_particles(Bproject *project, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1006
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
node_delete
Tcl_Obj * node_delete(Bproject *project, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bfil.cpp:864
Bfield::next
Bfield * next
Definition: mg_processing.h:261
filaments_center
double filaments_center(Bfilament *fillist, Bimage *p, long img_num, int filament_width)
Centers filament nodes in a micrograph.
Definition: mg_helix.cpp:190
filament_setup_filenames
int filament_setup_filenames(Bfilament *fil, Bstring filename, Bstring filpath)
Sets up individual filament file names for extraction.
Definition: mg_extract.cpp:86
linked_list.h
Header file for generalized linked list functions.
node_ids
Tcl_Obj * node_ids(Bfilament *fillist)
Definition: tcltk_bfil.cpp:694
Breconstruction::ffil
Bstring ffil
Definition: mg_processing.h:292
Bfilnode::loc
Vector3< double > loc
Definition: mg_processing.h:119
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bstring::base
Bstring base()
Definition: Bstring.cpp:516
filament_kill
int filament_kill(Bfilament *fil)
Frees a list of filaments.
Definition: mg_processing.cpp:552
Bmicrograph::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:235
Bfilnode
Definition: mg_processing.h:116
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
micrograph_extract_filaments
long micrograph_extract_filaments(Bmicrograph *mg, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:875
filament_node_add
Bfilnode * filament_node_add(Bfilnode **fnode, int pid)
Adds a filament node parameter structure to a linked list.
Definition: mg_processing.cpp:349
mg_helix.h
Header file for functions to process helical data.
verbose
int verbose
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
filament_renumber
int filament_renumber(Bfilament *fil)
Definition: tcltk_bfil.cpp:361
reconstruction_extract_filaments
long reconstruction_extract_filaments(Breconstruction *rec, Bimage *p, double width, int axis)
Extracts filament images from a micrograph.
Definition: mg_extract.cpp:901
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284