This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.c
1884 lines (1663 loc) · 66.9 KB
/
cell.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
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
/*
Cell-ID is intended to identify cells in images and to calculate a
number of statistics, including statistics derived from corresponding
fluorescence images.
Copyright (C) 2005 Andrew Gordon
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Andrew Gordon can be contacted at
Molecular Sciences Institute
2168 Shattuck Ave, 2nd Floor
Berkeley, CA 94704
**********************************************************************
Start-copyright-notice-for-libtiff
Libtiff software is used by Cell-ID for some of the reading in of
TIF image file data and also for creating new TIF files. Libtiff is
available at http://www.remotesensing.org/libtiff/. The libtiff software
was written by Sam Leffler while working for Silicon Graphics and
contains the following copyright notice:
"Copyright (c) 1988-1997 Sam Leffler
Copyright (c) 1991-1997 Silicon Graphics, Inc.
Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation, and (ii) the names
of Sam Leffler and Silicon Graphics may not be used in any advertising or
publicity relating to the software without the specific, prior written
permission of Sam Leffler and Silicon Graphics.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE."
End-copyright-notice-for-Libtiff
*********************************************
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "nums.h"
#include "tif_routines.h"
#include "segment.h"
#include "date_and_time.h"
#include "image_type.h"
#include "align_image.h"
#include "split_and_overlap.h"
#include "parameters.h"
#include "flatten.h"
//V1.2a
#include "file_type.h"
#include <glib.h>
//V1.2a TEST
#include "oif.h"
int seconds_per_day=86400;
#define max_files 1000
#define max_strlen 500
float *flat_cors=NULL;
//new_phase tells if we have a new file to search for cells. It's
//external since if there's not a new file, we don't need to try to
//match the cells again to their location in the list of known cells,
//when we calculate new fluorescence values. We declare it here since
//it's extern since segment.c uses it to decide whether to do the match
//against the known cells or just to use the previous values.
int new_phase=0;
int main(int argc, char *argv[]){
//The first argument should point to a file that contains a list of
//phase contrast files.
//The second argument should point to a file that contains a list of
//fluorescence files.
//The first list should be a series of phase contrast pictures,
//and the second list should be a series of fluorescent images of
//the _same_ pictures. Ie, a 1-1 correspondence, the first picture in
//the first list corresponds to the first pictures in the second list.
//We're going to search the phase contrast picture for the cells and
//their boundaries and then calculate how much fluorescence is in each
//one with the second picture.
//We'll loop over all of them, tracking the cells through the frames.
//V1.2a When reading oif (Olympus Image File) the first arguments is the
//name of the file ("myfile.oif") and the second argument is the output
// Wellcome message
printf("\n*** Cell_ID Version 1.4.6 *** \n\n");
FILE *fp_in;
FILE *fp;
int bf_index[max_files]; //V1.2a Index of the BF file used to find cells,
//for each fluorescent file
FILE *bf_fl_file=NULL;
char bf_fl_file_name[500];
char *flat_files[max_files]; //For flattening image
char *dark_files[max_files]; //For subtracting fixed background
char *phase_files[max_files];
char *fluor_files[max_files];
char *third_files[max_files];
int flag[max_files];
int flag_bf[max_files];
char c0,c1,c2;
int time_flag[max_files];
int time_index[max_files];
char *s;
char *s2;
char line[500];
char line2[500];
char line3[500];
int i,j,k;
int n_fluor,n_phase,n_third,n_dark,n_flat;
int iloop,nloop;
float *ftmp;
int flat_d[max_files],flat_t[max_files],flat_dmin,flat_cur=-1;
int fl_d[max_files],fl_t[max_files],fl_dmin;
int ph_d[max_files],ph_t[max_files],ph_dmin;
int third_d[max_files],third_t[max_files],third_dmin;
int dtmp,t,d_cur,t_cur;
unsigned short year;
unsigned char month,day;
int hours,minutes,seconds;
int dt,dt_min,j_min;
int j_cur=0;
int third_cur=-999;
int output_third_image=0;
int first=0;
int xmax_new;
int ymax_new;
//Images
float *bf=NULL;
float *fl=NULL;
float *third_image=NULL;
int *bf_fl_labels=NULL;
int *third_labels=NULL;
int xmax=-999;
int ymax=-999;
int *fret_label_array=NULL;
int align_fl=0;
int align_individual_cells=0;
int align_individual_cells_boundary=0;
int force_nucleus_in_center=0;
int output_individual_cells=0;
int fret_image;
float xstage,ystage;
float tmp,tmp1,tmp2;
int itmp;
double double_tmp;
char append[2];
int do_append;
int nuclear_fret_lower;
float *dark=NULL;
float exposure[max_files];
float t_exposure;
double max_d_over_s_cut_t0,max_d_over_s_cut_save;
double max_split_d_over_minor_t0,max_split_d_over_minor_save;
#define n_recomb_cuts_max 100
float recombination_cuts[n_recomb_cuts_max];
int recombination_cuts_type[n_recomb_cuts_max];
int recombination_cuts_flag[n_recomb_cuts_max];
int do_recombination=0;
int n_recomb_cuts=0;
int treat_brightfield_as_fluorescence=0;
int i_last_find_cell_call=0;
int n_found_cells;
struct point **found_boundary_points=NULL;
struct point **found_interior_points=NULL;
third_image=NULL; //Default in case don't have third image type
third_image_type=no_third_image; //default type, value defined in image_type.h
nuclear_fret_lower=-1;
recalculate_internal=1; //First time through always do
image_type=bright_field; //default
fret_image=0; //default to false (image_type for fret images also
//tells what _kind_ of fret image, so set this fret_image flag also).
n_third=-1;
overall_id_offset=-1; //flag value, will be changed to 0 later
strcpy(append,"w");
do_append=0;
for(i=0;i<max_files;i++){
flag_bf[i]=0; //for case of treat_bright_field_as_fluorescnece,
//this will mark which files were the brighr field files
}
//Check if there is a file with certain parameters to load
//First set parameters to default values
max_d_over_s_cut_t0=-999.0;
max_split_d_over_minor_t0=-999.0;
max_split_d_over_minor=0.5;
max_d_over_s_cut=6.0;
max_pixels_per_cell=1500;
min_pixels_per_cell=75;
background_reject_factor=3.0;
I_over_U_for_match=0.2;
nucleus_radii[0]=2; //V1.4.5 radii size initialization
nucleus_radii[1]=3;
nucleus_radii[2]=4;
nucleus_radii[3]=5;
nucleus_radii[4]=6;
nucleus_radii[5]=7;
file_type=list_file; //V1.2a
bf_fl_mapping=bf_fl_mapping_time;//V1.2a
int paw_output=0; //V1.3a
int n_bf_as_fl=0; //V1.4.2
//Command line parsing variables
GOptionContext *ctx;
GOptionGroup *grpCellFind, *grpMisc, *grpImageType;
char *equal_sign = NULL;
int help_flag = 0;
char *param_file = "parameters.txt";
char *dark_list_file = "dark.txt";
char *flat_list_file = "flat.txt";
char *bright_list_file = NULL;
char *fluor_list_file = NULL;
char *third_list_file = NULL;
char *output_basename = NULL;
char *fret_bf = NULL;
char *fret_nuclear = NULL;
char *str_align_fl = NULL;
//V1.4a
gchar *file_basename = NULL; //for getting correct channel identification
//from filenames with paths
char str_third_img_label[500];
char *pnt_third_img_label = NULL;
char str_image_type[500];
char *pnt_image_type = NULL;
//Checking for parameters file option in command line manually
for(i=1;i<argc;i++){
if(strstr(argv[i],"-?")!=NULL||strstr(argv[i],"--help")!=NULL||argc==1){
help_flag = 1;
break;
}
if(strcmp(argv[i],"-p")==0 || strcmp(argv[i],"--param")==0){
equal_sign=strstr(argv[i],"=");
if(equal_sign==NULL){
if(i+1<argc && argv[i+1][0]!='-'){
param_file=argv[i+1];
}else{
printf("Filename requiered after -p or --param option");
return 0;
}
} else {
param_file=++equal_sign;
}
}
}
if(help_flag==0 && (fp=fopen(param_file,"r"))!=NULL ){
printf("Reading %s\n",param_file);
//fscanf() only reads to next white space, not end of line
while((fgets(line,450,fp))!=NULL){ //next line (while not EOF)
if (line[0]!='#'){ //if not a comment
if ((strstr(line,"max_split_over_minor"))!=NULL){
if ((strstr(line,"_t0"))!=NULL){
sscanf(line,"%s %le",line2,&double_tmp);
max_split_d_over_minor_t0=double_tmp;
}else{
sscanf(line,"%s %le",line2,&double_tmp);
max_split_d_over_minor=double_tmp;
}
}else if((strstr(line,"max_dist_over_waist"))!=NULL){
if ((strstr(line,"_t0"))!=NULL){
sscanf(line,"%s %le",line2,&double_tmp);
max_d_over_s_cut_t0=double_tmp;
}else{
sscanf(line,"%s %le",line2,&double_tmp);
max_d_over_s_cut=double_tmp;
}
}else if((strstr(line,"background_reject_factor"))!=NULL){
sscanf(line,"%s %le",line2,&double_tmp);
background_reject_factor=double_tmp;
}else if((strstr(line,"tracking_comparison"))!=NULL){
sscanf(line,"%s %le",line2,&double_tmp);
I_over_U_for_match=double_tmp;
}else if((strstr(line,"paw_output"))!=NULL){
paw_output=1;
}else if((strstr(line,"max_pixels_per_cell"))!=NULL){
sscanf(line,"%s %i",line2,&itmp);
max_pixels_per_cell=itmp;
}else if((strstr(line,"min_pixels_per_cell"))!=NULL){
sscanf(line,"%s %i",line2,&itmp);
min_pixels_per_cell=itmp;
}else if((strstr(line,"force_nucleus_in_center"))!=NULL){
force_nucleus_in_center=1;
}else if((strstr(line,"output_individual_cells"))!=NULL){
output_individual_cells=1;
//system("mkdir -p cells");
//image types:
}else if(strstr(line,"image_type")!=NULL){
sscanf(line,"%s %s",line2,str_image_type);
//V1.2a file type reading from parameters
/*
}else if(strstr(line,"file_type")!=NULL){
sscanf(line,"%s %s",line2,line3);
if(strstr(line3,"oif")!=NULL){
file_type=oif_file;
printf("Olympus Image File (.oif) requiered as first argument.\n");
}else if(strstr(line3,"oem")!=NULL){
file_type=oem_file;
printf("Open Emviorment of Microscopy File (.oem) requiered as argument.\n");
//printf("as arguments.\n");
}else {
file_type=list_file;
if (strstr(line3,"bf_fl_list")==NULL){
printf("Not valid file_type in parameter.txt, using default.\n");
}
printf("Bright field and fluorescence file lists required as arguments.\n");
//printf("arguments.\n");
}
*/
//V1.2a bright field to fluorescence mapping
}else if(strstr(line,"bf_fl_mapping")!=NULL){
sscanf(line,"%s %s",line2,line3);
if(strstr(line3,"time")!=NULL){
//printf("Mapping bright field and fluorescence images by time.\n");
bf_fl_mapping=bf_fl_mapping_time;
} else if(strstr(line3,"list")!=NULL){
//printf("Mapping bright field and fluorescence images by list order.\n");
//printf("Same number of elemtes requiered in list files.\n");
bf_fl_mapping=bf_fl_mapping_list;
} else {
printf("-%s- is a invalid value for bf_fl_mapping in parameter.txt.\n",line3);
printf("Using time mapping by default.\n");
bf_fl_mapping=bf_fl_mapping_time;
}
//V1.4
//}else if(strstr(line,"nucleus_channel")!=NULL){
// sscanf(line,"%s %s",line2,nucleus_channel);
}else if(strstr(line,"fret")!=NULL){
sscanf(line,"%s %s",line2,line3);
if (fret_image!=1){ //if we haven't been here before
fret_image=1; //To indicate that we have a fret split image
//printf("Searching a split FRET image.\n");
}
//Further check the fret line:
if (strstr(line3,"bf_top_only")!=NULL){
//Now check if the split-brightfield has an image on top only or
//bottom only, or bottom and top (note that each of the argument
//strings has "fret" in them, so they'll pass the above if
//statement).
image_type=fret_bf_top_only;
//printf("Cells on top part of BF image only.\n");
}else if((strstr(line3,"bf_bottom_only"))!=NULL){
image_type=fret_bf_bottom_only;
//printf("Cells on bottom part of BF image only.\n");
}else if((strstr(line3,"bf_bottom_and_top"))!=NULL){
image_type=fret_bf_bottom_and_top;
//printf("Cells on bottom and top part of BF image.\n");
}
//Check if should use top or bottom of image that's used
//for nuclear label. (If this isn't set, then it defaults
//to bottom of image if there is a third image set and to
//the top of the image otherwise--this is done below all this).
if(strstr(line3,"nuclear_top")!=NULL){
nuclear_fret_lower=1;
}else if(strstr(line3,"nuclear_bottom")!=NULL){
nuclear_fret_lower=0;
}
}else if(strstr(line,"align_fl_to_first")!=NULL){
//Check whether we want to use the first fluorescence image to
//align all the others
//printf("Will align all FL files to first FL file.\n");
align_fl=1;
}else if(strstr(line,"align_fl_to_bf")!=NULL){
//or whether to align fl images to bf
//printf("Will align first FL files to brightfield.\n");
align_fl=2;
//Check for third list of images. The use of the
//images depends on the image_type.
}else if(strstr(line,"treat_brightfield_as_fluorescence_also")!=NULL){
treat_brightfield_as_fluorescence=1;
//printf("Adding BF image as additional fluorescence image.\n");
}else if (strstr(line,"third_image")!=NULL){
sscanf(line,"%s %s",line2,str_third_img_label);
}else if(strstr(line,"align_individual_cells")!=NULL){
align_individual_cells=1;
//see if "boundary" is part of name
if(strstr(line,"align_individual_cells_boundary")!=NULL){
align_individual_cells_boundary=1;
//printf("Will wiggle each cell around to re-align with BF");
//printf(" using boundary.\n");
}//else{
//printf("Will wiggle each cell around to re-align with BF.\n");
//}
}else if(strstr(line,"do_recombination")!=NULL){
do_recombination=1;
}else if(strstr(line,"recombination_fl_per_a_nucleus")!=NULL){
sscanf(line,"%s %i %e",line2,&itmp,&tmp);
recombination_cuts_type[n_recomb_cuts]=0;
recombination_cuts[n_recomb_cuts]=tmp;
recombination_cuts_flag[n_recomb_cuts]=itmp;
printf("Will try to recombine cells with nucleus fluorescence\n");
printf("intensity below %e for fl images with flag=%i.\n",
recombination_cuts[n_recomb_cuts],
recombination_cuts_flag[n_recomb_cuts]);
n_recomb_cuts++;
if (n_recomb_cuts>=n_recomb_cuts_max){
printf("Too many recombination cuts, dropping %s.\n",line);
n_recomb_cuts--;
}
}else if(strstr(line,"recombination_is_a_cell_fl_per_a")!=NULL){
sscanf(line,"%s %i %e",line2,&itmp,&tmp);
recombination_cuts_type[n_recomb_cuts]=1;
recombination_cuts[n_recomb_cuts]=tmp;
recombination_cuts_flag[n_recomb_cuts]=itmp;
printf("For recomb only, considering cells with total FL\n");
printf("intensity above ");
printf("%e for fluorescence images with flag=%i.\n",
recombination_cuts[n_recomb_cuts],
recombination_cuts_flag[n_recomb_cuts]);
n_recomb_cuts++;
if (n_recomb_cuts>=n_recomb_cuts_max){
printf("Too many recombination cuts, dropping %s.\n",line);
n_recomb_cuts--;
}
}else if(strstr(line,"recombination_all_new_cells")!=NULL){
recombination_cuts_type[n_recomb_cuts]=2;
recombination_cuts[n_recomb_cuts]=0.0;
recombination_cuts_flag[n_recomb_cuts]=0;
printf("All cells produced after i_t 0 will be recombined.\n");
n_recomb_cuts++;
if (n_recomb_cuts>=n_recomb_cuts_max){
printf("Too many recombination cuts, dropping %s.\n",line);
n_recomb_cuts--;
}
}else if(strstr(line,"append_output")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
overall_id_offset=itmp;
}else if(strstr(line,"nucleus_radius_1")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[0]=itmp;
}else if(strstr(line,"nucleus_radius_2")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[1]=itmp;
}else if(strstr(line,"nucleus_radius_3")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[2]=itmp;
}else if(strstr(line,"nucleus_radius_4")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[3]=itmp;
}else if(strstr(line,"nucleus_radius_5")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[4]=itmp;
}else if(strstr(line,"nucleus_radius_6")!=NULL){
sscanf(line,"%s %i",line2,&itmp);
nucleus_radii[5]=itmp;
}
} //End of check that first character wasn't a "#"
} //End of while loop over parameters.txt
fclose(fp);
}else if (help_flag==0) { //End of check that parameters.txt was open ok
printf("%s not found. Using default parameters. \n",param_file);
}
//Reading command line options with GOptions library
GOptionEntry optFileNames[]={
{"bright",'b',0,G_OPTION_ARG_STRING, &bright_list_file,
"Text file list of brightfield tif images",NULL},
{"fluor",'f',0,G_OPTION_ARG_STRING, &fluor_list_file,
"Text file of fluorescent tif images",NULL},
{"third",'3',0,G_OPTION_ARG_STRING, &third_list_file,
"List of third images to be used",NULL},
{"dark",'d',0,G_OPTION_ARG_STRING, &dark_list_file,
"List of dark images to be used","dark.txt"},
{"flat",'t',0,G_OPTION_ARG_STRING, &flat_list_file,
"List of flat images to be used","flat.txt"},
{"param",'p',0,G_OPTION_ARG_STRING, ¶m_file,
"Parameters file ", "parameters.txt"},
{"output",'o',0,G_OPTION_ARG_STRING, &output_basename,
"Basename of output files (including dirs)", NULL},
{NULL}
};
GOptionEntry optCellFind[]={
{"brf",0,0,G_OPTION_ARG_DOUBLE, &background_reject_factor,
"background_reject_factor segmentation parameter",NULL},
{"max-ppc",0,0,G_OPTION_ARG_INT, &max_pixels_per_cell,
"max_pixels_per_cell segmentation parameter",NULL},
{"min-ppc",0,0,G_OPTION_ARG_INT, &min_pixels_per_cell,
"min_pixels_per_cell segmentation parameter",NULL},
{"max-som",0,0,G_OPTION_ARG_DOUBLE, &max_split_d_over_minor,
"max_split_over_minor cell splitting parameter",NULL},
{"max-dow",0,0,G_OPTION_ARG_DOUBLE, &max_d_over_s_cut,
"max_dist_over_waist cell splitting parameter",NULL},
{"max-som-t0",0,0,G_OPTION_ARG_DOUBLE, &max_split_d_over_minor_t0,
"max_split_over_minor_t0 cell splitting parameter",NULL},
{"max-dow-t0",0,0,G_OPTION_ARG_DOUBLE, &max_d_over_s_cut_t0,
"max_dist_over_waist_t0 cell splitting parameter",NULL},
{"track",0,0,G_OPTION_ARG_DOUBLE, &I_over_U_for_match,
"tracking_comparison parameter",NULL},
{"align-fl",0,0,G_OPTION_ARG_STRING, &str_align_fl,
"align_fl_to_first/bf option (first, bf)",NULL},
{"nucl1",0,0,G_OPTION_ARG_INT, &(nucleus_radii[0]),
"nucleus_radius_1 parameter ",NULL},
{"nucl2",0,0,G_OPTION_ARG_INT, &(nucleus_radii[1]),
"nucleus_radius_2 parameter ",NULL},
{"nucl3",0,0,G_OPTION_ARG_INT, &(nucleus_radii[2]),
"nucleus_radius_3 parameter ",NULL},
{"nucl4",0,0,G_OPTION_ARG_INT, &(nucleus_radii[3]),
"nucleus_radius_4 parameter ",NULL},
{"nucl5",0,0,G_OPTION_ARG_INT, &(nucleus_radii[4]),
"nucleus_radius_5 parameter ",NULL},
{"nucl6",0,0,G_OPTION_ARG_INT, &(nucleus_radii[5]),
"nucleus_radius_6 parameter ",NULL},
//{"nuc-ch",0,0,G_OPTION_ARG_STRING, &nucleus_channel_pnt,
// "channel used to find nucleus, first three letters of image name",NULL},
{NULL}
};
GOptionEntry optMisc[]={
{"force-nuc",0,0,G_OPTION_ARG_NONE, &force_nucleus_in_center,
"force_nucleus_in_center option",NULL},
{"list-mapping",0,0,G_OPTION_ARG_NONE, &bf_fl_mapping,
"bf_fl_mapping list",NULL},
{"bf-as-fl",0,0,G_OPTION_ARG_NONE, &treat_brightfield_as_fluorescence,
"treat_brightfield_as_fluorescence option",NULL},
{"align-ind",0,0,G_OPTION_ARG_NONE, &align_individual_cells,
"align_individual_cells option",NULL},
{"align-ind-boundary",0,0,G_OPTION_ARG_NONE, &align_individual_cells_boundary,
"align_individual_cells_boundary option",NULL},
{"output-ind",0,0,G_OPTION_ARG_NONE, &output_individual_cells,
"output_individual_cells option",NULL},
{"append_output",0,0,G_OPTION_ARG_INT, &overall_id_offset,
"append_output option, argument is the id-offset (>=0)",NULL},
{"output_paw",0,0,G_OPTION_ARG_NONE, &paw_output,
"Make output for PAW.",NULL},
//{"recomb",0,0,G_OPTION_ARG_NONE, &do_recombination,
// "do_recombination option",NULL},
{NULL}
};
GOptionEntry optImageType[]={
{"fret-bf",0,0,G_OPTION_ARG_STRING, &fret_bf,
"fret_bf options for split images (top, bottom or both)",NULL},
{"fret-nuclear",0,0,G_OPTION_ARG_STRING, &fret_nuclear,
"fret_nuclear options for split images (top, bottom )",NULL},
{"image-type",0,0,G_OPTION_ARG_STRING, &pnt_image_type,
"image_type options (bright, decon, hex, confocal)","bright"},
{"3-img-label",0,0,G_OPTION_ARG_STRING, &pnt_third_img_label,
"third_image label option (nuclear, vacuole)","none"},
{NULL}
};
ctx = g_option_context_new("- Cell ID options");
grpCellFind = g_option_group_new("cell", "Cell Segmenting",
"Parameters used to find cells", NULL, NULL);
grpMisc = g_option_group_new("misc", "Miscelaneus options",
"Miscelaneus options", NULL, NULL);
grpImageType = g_option_group_new("image-type", "Image Type options",
"Specifications for the input images", NULL, NULL);
g_option_group_add_entries(grpCellFind, optCellFind);
g_option_group_add_entries(grpImageType, optImageType);
g_option_group_add_entries(grpMisc, optMisc);
g_option_context_add_main_entries(ctx, optFileNames, "Input filenames options");
g_option_context_add_group(ctx, grpCellFind);
g_option_context_add_group(ctx, grpImageType);
g_option_context_add_group(ctx, grpMisc);
g_option_context_parse(ctx, &argc, &argv, NULL);
g_option_context_free(ctx);
//V1.4.5
printf("Using nucleus radii %i %i %i %i %i %i px.\n",nucleus_radii[0],nucleus_radii[1]
,nucleus_radii[2],nucleus_radii[3],nucleus_radii[4],nucleus_radii[5]);
if(pnt_third_img_label==NULL) pnt_third_img_label=&str_third_img_label[0];
if(pnt_image_type==NULL) pnt_image_type=&str_image_type[0];
if(help_flag==1){
printf("For help type 'cell --help'\n");
printf("Required options: --bright brightfile.txt --fluor fluorfile.txt");
return 0;
}
if(output_individual_cells==1){
system("mkdir -p cells");
}
//Reading fret parameters form command line and steping on parameters.txt
//if required.
if(fret_bf != NULL){
fret_image=1;
if (strstr(fret_bf,"top")!=NULL){
//Now check if the split-brightfield has an image on top only or
//bottom only, or bottom and top
image_type=fret_bf_top_only;
//printf("Cells on top part of BF image only.\n");
}else if((strstr(fret_bf,"bottom"))!=NULL){
image_type=fret_bf_bottom_only;
//printf("Cells on bottom part of BF image only.\n");
}else if((strstr(fret_bf,"both"))!=NULL){
image_type=fret_bf_bottom_and_top;
//printf("Cells on bottom and top part of BF image.\n");
}
}
if(fret_nuclear!=NULL){
fret_image=1;
if(strstr(fret_nuclear,"top")!=NULL){
nuclear_fret_lower=1;
}else if(strstr(fret_nuclear,"bottom")!=NULL){
nuclear_fret_lower=0;
}
}
//Printing the final fret and image type parameters
if (fret_image==1){
printf("Searching a split FRET image.\n");
if(image_type==fret_bf_top_only){
printf("Cells on top part of BF image only.\n");
}else if(image_type==fret_bf_bottom_only){
printf("Cells on bottom part of BF image only.\n");
}else if(image_type==fret_bf_bottom_and_top){
printf("Cells on bottom and top part of BF image.\n");
}
//If nuclear_fret_lower wasn't set, set to defaults
if (nuclear_fret_lower==(-1)){
if (third_image_type==no_third_image){
nuclear_fret_lower=1; //Upper part if third image
}else{
nuclear_fret_lower=0; //Lower part if no third image
}
}
if (nuclear_fret_lower==1){
printf("Using top part of nuclear-image for nucleus.\n");
}else{
printf("Using bottom part of nuclear-image for nucleus.\n");
}
} else {
//image_type is either a fret type, or one of the following
if (strstr(pnt_image_type,"bright")!=NULL){
image_type=bright_field;
printf("Searching brightfield image for cells.\n");
}else if(strstr(pnt_image_type,"decon")!=NULL){
image_type=metamorph_deconvolution;
printf("Searching metamorph_deconvolution image for cells.\n");
}else if(strstr(pnt_image_type,"hex")!=NULL){
image_type=hexagonal_grid;
printf("Searching hexagonal_grid image type for cells.\n");
//}else if(strstr(str_image_type,"membrane_tagged_fl")!=NULL){
// image_type=membrane_tagged_fl;
// printf("Searching a membrane tagged fluor image for cells.\n");
}else if(strstr(pnt_image_type,"confocal")!=NULL){
image_type=confocal_transmission;
printf("Searching a confocal transmission image for cells.\n");
}
}
//Stepping parameters.txt values
if(str_align_fl!=NULL){
if(strstr(str_align_fl,"first")!=NULL){
align_fl=1;
}else if(strstr(str_align_fl,"bf")!=NULL){
align_fl=2;
}else{
printf("'%s' is a invalid value for --align-fl, no aligment will be done.\n"
,str_align_fl);
}
}
//Printing final values
if(align_fl==1){
printf("Will align all FL files to first FL file.\n");
}else if (align_fl==2){
printf("Will align first FL files to brightfield.\n");
}
if(treat_brightfield_as_fluorescence==1){
printf("Adding BF image as additional fluorescence image.\n");
}
//Cheking for third image type, note that the same string has been read in
//parametes.txt and steped on with GOptions
if((strstr(pnt_third_img_label,"nuclear")!=NULL)){
third_image_type=nuclear_label;
printf("Third image will be used to label nucleus.\n");
}else if(strstr(pnt_third_img_label,"vacuole")!=NULL){
third_image_type=vacuole_label;
printf("Third image will be used to correct for vacuole.\n");
}else{
if(third_list_file!=NULL){
printf("Unknown third image type, ignoring third images.");
}
third_image_type=no_third_image;
}
//printing values
if(align_individual_cells_boundary==1){
align_individual_cells=1;
printf("Will wiggle each cell around to re-align with BF");
printf(" using boundary.\n");
}else if(align_individual_cells==1){
printf("Will wiggle each cell around to re-align with BF.\n");
}
if (overall_id_offset>=0){
strcpy(append,"a");
do_append=1;
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!! !!!!!!\n");
printf("!!!! We're going to OVERWRITE output data. !!!!!!\n");
printf("!!!! !!!!!!\n");
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("All cells will be given ID offset %i.\n",
overall_id_offset);
}else if (overall_id_offset==-1){
overall_id_offset=0;
}else{
printf("Append_output was requested with ID offset of %i.\n",overall_id_offset);
printf("Minimum offset is 0.\n");
exit(0);
}
//Read in the files
n_third=0;
if (third_list_file!=NULL){//if there is a third image
if( (fp_in=fopen(third_list_file,"r"))==NULL ){
printf("Couldnt open file %s.\n",third_list_file);
return 0;
}
//Read in names of the third-files to be read in
i=0;
while(fscanf(fp_in,"%s",line)==1){
third_files[i]=(char *)malloc(max_strlen*sizeof(char));
strcpy(third_files[i],line);
i++;
}
fclose(fp_in);
n_third=i;
}
if ((n_recomb_cuts>0)&&(do_recombination==0)){
printf("***** Recombination cuts were set but NOT doing the");
printf("***** recombination check.");
}
printf("Using parameters:\n");
printf(" max_dist_over_minor_axis=%le\n",max_split_d_over_minor);
printf(" max_dist_over_waist=%le\n",max_d_over_s_cut);
printf(" back_reject=%le\n",background_reject_factor);
printf(" max_pixels_per_cell=%i\n",max_pixels_per_cell);
printf(" min_pixels_per_cell=%i\n",min_pixels_per_cell);
printf(" I_over_U_for_match (tracking parameter)=%le\n",I_over_U_for_match);
max_d_over_s_cut_save=max_d_over_s_cut;
max_split_d_over_minor_save=max_split_d_over_minor;
if (max_d_over_s_cut_t0>-10.0){
printf(" For first image: max_dist_over_waist=%e\n",
max_d_over_s_cut_t0);
}
if (max_split_d_over_minor_t0>-10.0){
printf(" For first image: max_dist_over_minor_axis=%e\n",
max_split_d_over_minor_t0);
}
//End checking of arguments
//Read in the names of the phase and fluorescence files.
//loading brightfield file names into array phase_files
if( (fp_in=fopen(bright_list_file,"r"))==NULL ){
printf("Couldnt open file %s.\n",bright_list_file);
return 0;
}
i=0;
while(fscanf(fp_in,"%s",line)==1){ //the 1 means it filled the "%s" part
phase_files[i]=(char *)malloc(max_strlen*sizeof(char));
strcpy(phase_files[i],line);
i++;
}
fclose(fp_in);
n_phase=i;
//loading fluorescence file names into array fluor_files
if( (fp_in=fopen(fluor_list_file,"r"))==NULL ){
printf("Couldnt open file %s.\n",fluor_list_file);
return 0;
}
i=0;
while(fscanf(fp_in,"%s",line)==1){ //the 1 means it filled the "%s" part
fluor_files[i]=(char *)malloc(max_strlen*sizeof(char));
strcpy(fluor_files[i],line);
i++;
}
fclose(fp_in);
n_fluor=i;
if (treat_brightfield_as_fluorescence==1){
//if mapping list we have to duplicate the phase file list
if(bf_fl_mapping==bf_fl_mapping_list){
//adding first bf as fl
phase_files[n_phase]=(char *)malloc(max_strlen*sizeof(char));
strcpy(phase_files[n_phase],phase_files[0]);
fluor_files[n_fluor]=(char *)malloc(max_strlen*sizeof(char));
strcpy(fluor_files[n_fluor],phase_files[0]);
n_bf_as_fl=1;
//adding new bf as fl
for(i=1;i<n_phase;i++){
for(j=0;j<i;j++){
if(strstr(phase_files[i],phase_files[j])==NULL){//new bf
phase_files[n_phase+n_bf_as_fl]=(char *)malloc(max_strlen*sizeof(char));
strcpy(phase_files[n_phase+n_bf_as_fl],phase_files[i]);
fluor_files[n_fluor+n_bf_as_fl]=(char *)malloc(max_strlen*sizeof(char));
strcpy(fluor_files[n_fluor+n_bf_as_fl],phase_files[i]);
n_bf_as_fl++;
}
}
}
n_phase+=n_bf_as_fl;
n_fluor+=n_bf_as_fl;
}else{
//Copy brightfield image names into fluor list
for(i=0;i<n_phase;i++){
fluor_files[n_fluor]=(char *)malloc(max_strlen*sizeof(char));
strcpy(fluor_files[n_fluor],phase_files[i]);
flag_bf[n_fluor]=1; //mark that is a brightfield
n_fluor++;
}
}
}
//dark files
if( (fp_in=fopen(dark_list_file,"r"))==NULL ){
printf("%s not found. No dark-field corrections.\n", dark_list_file);
n_dark=0;
}else{
i=0;
while(fscanf(fp_in,"%s",line)==1){ //the 1 means it filled the "%s" part
dark_files[i]=(char *)malloc(max_strlen*sizeof(char));
strcpy(dark_files[i],line);
//Get exposure time of each. (Note that we're going to end up
//reading these in every time, instead of saving them in memory. We
//only do the correction once and we don't want to have them sitting
//around in memory probably.)
exposure[i]=get_exposure(dark_files[i]);
i++;
}
fclose(fp_in);
n_dark=i;
}
//Files to flatten image
if( (fp_in=fopen(flat_list_file,"r"))==NULL ){
printf("%s not found. No flattening-image corrections.\n",flat_list_file);
n_flat=0;
}else{
i=0;
while(fscanf(fp_in,"%s",line)==1){ //the 1 means it filled the "%s" part
flat_files[i]=(char *)malloc(max_strlen*sizeof(char));
strcpy(flat_files[i],line);
i++;
}
fclose(fp_in);
n_flat=i;
}
//We're going to keep track of the ordinal number of
//each flag. E.g., the first time flag=1 appears we'll
//increment time_flag[1], etc.
for(i=0;i<max_files;i++){
time_flag[i]=0;
time_index[i]=0;
}
//V1.2b I
if (bf_fl_mapping==bf_fl_mapping_time){ //Mapping by time
//Loop over all the files and calculate dates and times of each
//(have to pull this out of metamorph's special tag in the tiff file)
//V1.2a TODO modify, beware of ph_t, ph_dmin, ph_d arrays for future use
ph_dmin=(int)(0x1ffffff);
for(i=0;i<n_phase;i++){
if(get_date_and_time(phase_files[i],&dtmp,&t,&xstage,&ystage)==0){
printf("Couldn't get date and time for %s.\n",phase_files[i]);
dtmp=0.0;
t=0.0;
xstage=-99999.0;
ystage=-99999.0;
}
t=t/1000; //Convert to seconds
ph_t[i]=t;
if (dtmp<ph_dmin){
ph_dmin=dtmp;
}
ph_d[i]=dtmp;
printf("Stage position: %e %e\n",xstage,ystage);fflush(stdout);
}
// exit(0);
/*
//As a test, only use first BF
for(i=1;i<n_phase;i++){
ph_d[i]=0.0;
ph_t[i]=0.0;
}
//end-test
*/
//V1.2a TODO modify, beware of the fl_d, fl_dmin, fl_t arrays
fl_dmin=(int)(0x1ffffff);
for(i=0;i<n_fluor;i++){
if(get_date_and_time(fluor_files[i],&dtmp,&t,&xstage,&ystage)==0){
printf("Couldn't get date and time for %s.\n",fluor_files[i]);