1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "mrilib.h"
6 #include "cluster_doubleNOMASK.h"
7 
8 
9 
10 // from command.c file
11 
display_help(void)12 static void display_help(void)
13 { printf ("Clustering 4segmentation, command-line version.\n");
14   printf ("USAGE: cluster [options]\n");
15   printf ("options:\n");
16   printf ("  -v, --version Version information\n");
17   printf ("  -f filename   File loading\n");
18   printf ("  -cg a|m       Specifies whether to center each row\n"
19           "                in the data\n"
20           "                a: Subtract the mean of each row\n"
21           "                m: Subtract the median of each row\n"
22           "                (default is no centering)\n");
23   printf ("  -ng           Specifies to normalize each row in the data\n"
24           "                (default is no normalization)\n");
25   printf ("  -ca a|m       Specifies whether to center each column \n"
26           "                in the data\n"
27           "                a: Subtract the mean of each column\n"
28           "                m: Subtract the median of each column\n"
29           "                (default is no centering)\n");
30   printf ("  -na           Specifies to normalize each column in the data\n"
31           "                (default is no normalization)\n");
32   printf ("  -u jobname    Allows you to specify a different name for the \n"
33           "                output files.\n"
34           "                (default is derived from the input file name)\n");
35   printf ("  -g [0..8]     Specifies the distance measure for gene clustering \n"
36          );
37   printf ("                0: No gene clustering\n"
38           "                1: Uncentered correlation\n"
39           "                2: Pearson correlation\n"
40           "                3: Uncentered correlation, absolute value\n"
41           "                4: Pearson correlation, absolute value\n"
42           "                5: Spearman's rank correlation\n"
43           "                6: Kendall's tau\n"
44           "                7: Euclidean distance\n"
45           "                8: City-block distance\n"
46           "                (default: 1)\n");
47   printf ("  -k number     Specifies whether to run k-means clustering\n"
48           "                instead of hierarchical clustering, and the number\n"
49           "                of clusters k to use\n");
50   printf ("  -c number     Specifies the number of clusters for tree cutting\n"
51           "                after hierarchical clustering\n");
52   printf ("  -r number     For k-means clustering, the number of times the\n"
53           "                k-means clustering algorithm is run\n"
54           "                (default: 1)\n");
55   printf ("  -m [msca]     Specifies which hierarchical clustering method to use\n"
56           "                m: Pairwise complete-linkage\n"
57           "                s: Pairwise single-linkage\n"
58           "                c: Pairwise centroid-linkage\n"
59           "                a: Pairwise average-linkage\n"
60           "                (default: m)\n");
61   return;
62 }
63 
64 
display_version(void)65 static void display_version(void)
66 { printf ("\n"
67 "Clustering for segmentation, command line version ,\n"
68 "using the C Clustering Library version 1.38.\n"
69 "\n"
70 "Cluster was originally written by Michael Eisen (eisen 'AT' rana.lbl.gov)\n"
71 "Copyright 1998-99 Stanford University.\n");
72   printf ("\n"
73 "The command line version of Clustering version was created by Andrej Vovk\n"
74 "and will be surely corrected by Ziad Saad ;),\n"
75 "\n");
76   return;
77 }
78 
setjobname(const char * basename,int strip)79 static char* setjobname(const char* basename, int strip)
80 { char* jobname;
81   int n = strlen(basename);
82   if (strip)
83   { char* extension = strrchr(basename, '.');
84     if (extension) n -= strlen(extension);
85   }
86   jobname = malloc((n+1)*sizeof(char));
87   strncpy (jobname, basename, n);
88   jobname[n] = '\0';
89   return jobname;
90 }
91 
readnumber(const char word[])92 static int readnumber(const char word[])
93 { char* error = 0;
94   long value = strtol(word,&error,0);
95   if (*error=='\0') return (int)value;
96   else return -1;
97 }
98 
getmetric(int i)99 static char getmetric(int i)
100 { switch (i)
101   { case 1: return 'u';
102     case 2: return 'c';
103     case 3: return 'x';
104     case 4: return 'a';
105     case 5: return 's';
106     case 6: return 'k';
107     case 7: return 'e';
108     case 8: return 'b';
109     default: return '\0';
110   }
111   /* Never get here */
112   return '\0';
113 }
114 
115 
116 
117 
118 /* ========================================================================= */
119 
120 
121 
example_distance_gene(int nrows,int ncols,double ** data)122 double** example_distance_gene(int nrows, int ncols, double** data)
123 /* Calculate the distance matrix between genes using the Euclidean distance. */
124 { int i, j, ii, nl, nc;
125   double** distMatrix;
126   double* weight = malloc(ncols*sizeof(double));
127 
128 
129 
130 
131   printf("============ Euclidean distance matrix between genes ============\n");
132   for (i = 0; i < ncols; i++) weight[i] = 1.0;
133   distMatrix = distancematrix(nrows, ncols, data, weight, 'e', 0); // ZIAD: SIGKILL
134   if (!distMatrix)
135   { printf ("Insufficient memory to store the distance matrix\n");
136     free(weight);
137     return NULL;
138   }
139   /*printf("   Gene:");
140   for(i=0; i<nrows-1; i++) printf("%6d", i);
141   printf("\n");
142   for(i=0; i<nrows; i++)
143      { printf("Gene %2d:",i);
144     for(j=0; j<i; j++) printf(" %5.2f",distMatrix[i][j]);
145     printf("\n");
146   }
147   printf("\n");
148     */
149 
150 
151   free(weight);
152   return distMatrix;
153 }
154 
155 
156 /* ========================================================================= */
157 
example_hierarchical(int nrows,int ncols,double ** data,char * jobname,int k,double ** distmatrix)158 void example_hierarchical(int nrows, int ncols, double** data, char* jobname, int k, double** distmatrix)
159 /* Perform hierarchical clustering ... , double** distmatrix */
160 { int i, ii, nl, nc;
161   const int nnodes = nrows-1;
162   double* weight = malloc(ncols*sizeof(double));
163   int* clusterid;
164   Node* tree;
165 
166   char* filename;
167   //char* filename2;
168 
169 
170 
171   for (i = 0; i < ncols; i++) weight[i] = 1.0;
172   printf("\n");
173 
174 
175   FILE *out1;
176 
177   int n = 1 + strlen(jobname) + strlen("_C") + strlen(".ext");
178 
179   if (k)
180     { int dummy = k;
181       do n++; while (dummy/=10);
182     }
183 
184 
185   filename = malloc(n*sizeof(char));
186 
187   sprintf (filename, "%s_C%d.hie", jobname, k);
188   out1 = fopen( filename, "w" );
189 
190   /*FILE *out2;
191   filename2 = malloc(n*sizeof(char));
192 
193   sprintf (filename2, "%s_C%d.hi1", jobname, k);
194   out2 = fopen( filename2, "w" );*/
195 
196   //HERE SHOULD USE method instead of 'xxx' (s,m,a,c)
197 
198 
199   printf("================ Pairwise single linkage clustering ============\n");
200   /* Since we have the distance matrix here, we may as well use it. */
201   tree = treecluster(nrows, ncols, 0, 0, 0, 'e', 's', distmatrix);
202   /* The distance matrix was modified by treecluster, so we cannot use it any
203    * more. But we still need to deallocate it here.
204    * The first row of distmatrix is a single null pointer; no need to free it.
205    */
206   for (i = 1; i < nrows; i++) free(distmatrix[i]);
207   free(distmatrix);
208   if (!tree)
209   { /* Indication that the treecluster routine failed */
210     printf ("treecluster routine failed due to insufficient memory\n");
211     free(weight);
212     return;
213   }
214 
215   #if 0
216   /* Andrej: This block looked like it was commented out
217     I took out some of the * / because they
218     were generating warning and blocked out the
219     entire section with #if 0 .  */
220 
221   /*fprintf(out2,"Node     Item 1   Item 2    Distance\n");
222   for(i=0; i<nnodes; i++)
223     fprintf(out2,"%3d:%9d%9d      %g\n",
224            -i-1, tree[i].left, tree[i].right, tree[i].distance);
225 	   printf("\n");
226 	   fclose(out2);*/
227   //free(tree);
228 
229   /*
230   printf("================ Pairwise maximum linkage clustering ============\n");
231   tree = treecluster(nrows, ncols, data,  weight, 0, 'e', 'm', 0); */
232   /* Here, we let treecluster calculate the distance matrix for us. In that
233    * case, the treecluster routine may fail due to insufficient memory to store
234    * the distance matrix. For the small data sets in this example, that is
235    * unlikely to occur though. Let's check for it anyway:
236    */
237   if (!tree)
238   { /* Indication that the treecluster routine failed */
239     printf ("treecluster routine failed due to insufficient memory\n");
240     free(weight);
241     return;
242   }
243   printf("Node     Item 1   Item 2    Distance\n");
244   for(i=0; i<nnodes; i++)
245     printf("%3d:%9d%9d      %g\n",
246            -i-1, tree[i].left, tree[i].right, tree[i].distance);
247   printf("\n");
248   free(tree);
249 
250 
251 
252   printf("================ Pairwise average linkage clustering ============\n");
253   tree = treecluster(nrows, ncols, data, weight, 0, 'e', 'a', 0);
254   if (!tree)
255   { /* Indication that the treecluster routine failed */
256     printf ("treecluster routine failed due to insufficient memory\n");
257     free(weight);
258     return;
259   }
260   printf("Node     Item 1   Item 2    Distance\n");
261   for(i=0; i<nnodes; i++)
262     printf("%3d:%9d%9d      %g\n",
263            -i-1, tree[i].left, tree[i].right, tree[i].distance);
264   printf("\n");
265   free(tree);
266 
267 
268 
269   printf("================ Pairwise centroid linkage clustering ===========\n");
270   tree = treecluster(nrows, ncols, data, weight, 0, 'e', 'c', 0);
271   if (!tree)
272   { /* Indication that the treecluster routine failed */
273     printf ("treecluster routine failed due to insufficient memory\n");
274     free(weight);
275     return;
276   }
277   printf("Node     Item 1   Item 2    Distance\n");
278   for(i=0; i<nnodes; i++)
279     printf("%3d:%9d%9d      %g\n",
280            -i-1, tree[i].left, tree[i].right, tree[i].distance);
281   printf("\n");
282 
283 
284   #endif
285 
286 
287   printf("=============== Cutting a hierarchical clustering tree ==========\n");
288   clusterid = malloc(nrows*sizeof(int));
289   printf(" number of clusters %d \n",k);
290   cuttree (nrows, tree, k, clusterid);
291   for(i=0; i<nrows; i++)
292   fprintf(out1, "%09d\t%2d\n", i, clusterid[i]);
293   fprintf(out1, "\n");
294   fclose(out1);
295 
296 
297   free(tree);
298   free(clusterid);
299   free(weight);
300   return;
301 }
302 
getvoxlclusterdist(int * count,double ** cdata,int * clusterid,double ** data,char * jobname,int nclusters,int nrows,int ncols)303 void getvoxlclusterdist(int* count, double** cdata,
304 			int* clusterid, double** data, char* jobname,
305 			int nclusters, int nrows, int ncols)
306 {
307   int i, j, n;
308   char* filename4;
309   FILE *out4=NULL;
310   double* vcdata = malloc(nrows*sizeof(double*));
311   double difference, difference1;
312 
313   n = 1 + strlen(jobname) + strlen("_K_G") + strlen(".ext");
314 
315   int dummy = nclusters;
316   do n++; while (dummy/=10);
317 
318 
319   filename4 = malloc(n*sizeof(char));
320   sprintf (filename4, "%s_K_G%d.vcd", jobname, nclusters);
321   out4 = fopen( filename4, "w" );
322 
323   for (i = 0; i < nrows; i++){
324     difference = 0;
325     difference1 = 0;
326     for (j = 0; j < ncols; j++) {
327       difference1 = cdata[clusterid[i]][j]-data[i][j];
328       difference = difference + difference1*difference1;
329     }
330     vcdata[i] = sqrt(difference);
331   }
332 
333   printf ("------- writing voxels-centroids distances to file:\t\t"
334           " %s_K_G%d.vcd\n",jobname, nclusters);
335   for (i = 0; i < nrows; i++)
336     fprintf (out4, "%09d\t%7.3f\n", i, vcdata[i]);
337   fclose(out4); out4=NULL;
338 
339   /*for (i = 0; i < nrows; i++){
340     free(vcdata[i]);
341     }*/
342   free(vcdata);
343   return;
344 
345 }
346 
347 
348 
349 /* ========================================================================= */
350 
351 
example_kmeans(int nrows,int ncols,double ** data,int nclusters,int npass,char dist,char * jobname)352 void example_kmeans( int nrows, int ncols,
353                      double** data,
354                      int nclusters, int npass,
355                      char dist, char* jobname)
356 
357 /* Perform k-means clustering on genes */
358 {
359    int i, j, ii, nl, nc;
360    //const int nclusters = 3;
361    const int transpose = 0;
362    //const char dist = 'e';
363    const char method = 'a';
364    /* For method=='a', the centroid is defined as the mean over all elements
365      belonging to a cluster for each dimension.
366      For method=='m', the centroid is defined as the median over all elements
367      belonging to a cluster for each dimension.
368    */
369 
370    //int npass = 1;
371    int ifound = 0;
372    int test=0;
373    double error;
374    double distance;
375    int** index;
376    int* count;
377    double* weight = malloc(ncols*sizeof(double));
378    int* clusterid = malloc(nrows*sizeof(int));
379    double** cdata = malloc(nclusters*sizeof(double*));
380 
381    int n=0;
382    char* filename;
383    char* filename2;
384    char* filename3;
385    FILE *out1=NULL;
386    FILE *out2=NULL;
387    FILE *out3=NULL;
388 
389    for (i = 0; i < nclusters; i++)
390    { cdata[i] = malloc(ncols*sizeof(double));
391 
392    }
393    for (i = 0; i < ncols; i++) weight[i] = 1.0;
394 
395 
396 
397    n = 1 + strlen(jobname) + strlen("_K_G") + strlen(".ext");
398 
399    if (dist)
400    { int dummy = nclusters;
401     do n++; while (dummy/=10);
402    }
403 
404    //avovk
405    printf("a je u omari :) \n");
406    filename = malloc(n*sizeof(char));
407    filename2 = malloc(n*sizeof(char));
408    filename3 = malloc(n*sizeof(char));
409    sprintf (filename, "%s_K_G%d.kgg", jobname, nclusters);
410    out1 = fopen( filename, "w" );
411    sprintf (filename2, "%s_K_G%d.dis", jobname, nclusters);
412    out2 = fopen( filename2, "w" );
413    sprintf (filename3, "%s_K_G%d.cen", jobname, nclusters);
414    out3 = fopen( filename3, "w" );
415 
416    printf("======================== k-means clustering"
417          " ========================\n");
418 
419    printf ("\n");
420    printf ("----- doing %d passes... go stretch your legs...\n",npass);
421    //npass = 3;
422    kcluster(nclusters,nrows,ncols,data,weight,transpose,npass,method,dist,
423     clusterid, &error, &ifound);
424    printf ("Solution found %d times; ", ifound);
425    printf ("within-cluster sum of distances is %f\n", error);
426    printf ("------- writing Cluster assignments to file:\t\t"
427           " %s_K_G%d.kgg\n",jobname, nclusters);
428    for (i = 0; i < nrows; i++)
429      fprintf (out1, "%09d\t %d\n", i, clusterid[i]);
430    fclose(out1); out1=NULL;
431 
432   printf ("------- writing Distance between clusters to file:\t %s_K_G%d.dis \n", jobname, nclusters);
433   fprintf (out2,"------- Distance between clusters:\n");
434   index = malloc(nclusters*sizeof(int*));
435   count = malloc(nclusters*sizeof(int));
436   for (i = 0; i < nclusters; i++) count[i] = 0;
437   for (i = 0; i < nrows; i++) count[clusterid[i]]++;
438   for (i = 0; i < nclusters; i++) index[i] = malloc(count[i]*sizeof(int));
439   for (i = 0; i < nclusters; i++) count[i] = 0;
440   for (i = 0; i < nrows; i++)
441   { int id = clusterid[i];
442     index[id][count[id]] = i;
443     count[id]++;
444   }
445 
446   for (i = 0; i < nclusters-1; i++)
447     {
448       for (j = 1+i; j < nclusters; j++)
449 	{
450 	  distance = clusterdistance(nrows, ncols, data, weight, count[i], count[j], index[i], index[j], 'e', 'a', 0);
451 	  fprintf(out2,"Distance between %d and %d: %7.3f\n", i, j, distance);
452 	  // fprintf(stderr,"Distance between %d and %d: %7.3f\n", i, j, distance);
453 	}
454     }
455 
456   fclose(out2); out2=NULL;
457 
458 
459 
460    printf ("------- writing Cluster centroids to file:\t\t%s_K_G%d.cen\n",jobname, nclusters);
461    fprintf (out3,"------- Cluster centroids:\n");
462    getclustercentroids(nclusters, nrows, ncols, data, clusterid, cdata, 0, 'a');
463 	   fprintf(out3,"   coefficients:");
464 		       for(i=0; i<ncols; i++) fprintf(out3,"\t%7d", i);
465 		       fprintf(out3,"\n");
466    for (i = 0; i < nclusters; i++){
467       fprintf(out3,"Cluster %2d:", i);
468       for (j = 0; j < ncols; j++) fprintf(out3,"\t%7.3f", cdata[i][j]);
469       fprintf(out3,"\n");
470    }
471    fclose(out3); out3=NULL;
472    printf("Done...\n");
473 
474    /* call function to calculate distance between each voxel and centroid */
475    /* we will need:
476       count - number of elements in cluster as we allready have it
477       cdata - cluster centroids
478       clusterid
479       data */
480 
481 
482       getvoxlclusterdist(count, cdata, clusterid, data, jobname,
483 nclusters, nrows, ncols);
484 
485 
486    for (i = 0; i < nclusters; i++) free(index[i]);
487    free(index);
488    free(count);
489 
490    for (i = 0; i < nclusters; i++){
491       free(cdata[i]);
492    }
493 
494 
495    free(cdata);
496 
497    free(clusterid);
498    free(weight);
499 
500    return;
501 }
502 
503 
504 /* ========================================================================= */
505 
main(int argc,char ** argv)506 int main(int argc, char **argv)
507 {
508   int ii=0, ncol=0, nrow=0, nl=0, nc=0, posi=0, posj=0, posk=0;
509   //int nclust=atoi(argv[2]);
510   MRI_IMAGE *im = NULL;
511   double *dar = NULL;
512   double **D = NULL;
513   //int **mask = NULL;
514   double** distmatrix = NULL;
515   //from command.c
516 
517   int i = 1;
518   char* filename = 0;
519   char* jobname = 0;
520   int l = 0;
521   int k = 0;
522   int kh = 3;
523   int r = 1;
524   int s = 0;
525   int x = 2;
526   int y = 1;
527   int Rows, Columns;
528   char distmetric = 'u';
529   char arraymetric = '\0';
530   char method = 'm';
531   char cg = '\0';
532   char ca = '\0';
533   int ng = 0;
534   int na = 0;
535   while (i < argc)
536   { const char* const argument = argv[i];
537 
538 
539     i++;
540     if (strlen(argument)<2)
541     { printf("ERROR: missing argument\n");
542       return 0;
543     }
544     if (argument[0]!='-')
545     { printf("ERROR: unknown argument\n");
546       return 0;
547     }
548     if(!strcmp(argument,"--version") || !strcmp(argument,"-v"))
549     { display_version();
550       return 0;
551     }
552     if(!strcmp(argument,"--help") || !strcmp(argument,"-h"))
553     { display_help();
554       return 0;
555     }
556     if(!strcmp(argument,"-cg"))
557     { if (i==argc || strlen(argv[i])>1 || !strchr("am",argv[i][0]))
558       { printf ("Error reading command line argument cg\n");
559         return 0;
560       }
561       cg = argv[i][0];
562       i++;
563       continue;
564     }
565     if(!strcmp(argument,"-ca"))
566     { if (i==argc || strlen(argv[i])>1 || !strchr("am",argv[i][0]))
567       { printf ("Error reading command line argument ca\n");
568         return 0;
569       }
570       ca = argv[i][0];
571       i++;
572       continue;
573     }
574     if(!strcmp(argument,"-ng"))
575     { ng = 1;
576       continue;
577     }
578     if(!strcmp(argument,"-na"))
579     { na = 1;
580       continue;
581     }
582     switch (argument[1])
583     { case 'l': l=1; break;
584       case 'u':
585       { if (i==argc)
586         { printf ("Error reading command line argument u: "
587                   "no job name specified\n");
588           return 0;
589         }
590         jobname = setjobname(argv[i],0);
591         i++;
592         break;
593       }
594       case 'f':
595       { if (i==argc)
596         { printf ("Error reading command line argument f: "
597                   "no file name specified\n");
598           return 0;
599         }
600         filename = argv[i];
601         i++;
602         break;
603       }
604       case 'g':
605       { int g;
606         if (i==argc)
607         { printf ("Error reading command line argument g: parameter missing\n");
608           return 0;
609         }
610         g = readnumber(argv[i]);
611         if (g < 0 || g > 9)
612         { printf ("Error reading command line argument g: "
613                   "should be between 0 and 9 inclusive\n");
614           return 0;
615         }
616         i++;
617         distmetric = getmetric(g);
618         break;
619       }
620 
621       case 'k':
622       { if (i==argc)
623         { printf ("Error reading command line argument k: "
624                   "parameter missing\n");
625           return 0;
626         }
627         k = readnumber(argv[i]);
628         if (k < 1)
629         { printf ("Error reading command line argument k: "
630                   "a positive integer is required\n");
631           return 0;
632         }
633         i++;
634         break;
635       }
636 
637     case 'c':
638       { if (i==argc)
639         { printf ("Error reading command line argument h: parameter missing\n");
640           return 0;
641         }
642         kh = readnumber(argv[i]);
643         if (kh < 1)
644         { printf ("Error reading command line argument h: a positive integer is required\n");
645           return 0;
646         }
647         i++;
648         break;
649       }
650       case 'r':
651       { if (i==argc)
652         { printf ("Error reading command line argument r: parameter missing\n");
653           return 0;
654         }
655         r = readnumber(argv[i]);
656         if (r < 1)
657         { printf ("Error reading command line argument r: "
658                   "a positive integer is required\n");
659           return 0;
660         }
661         i++;
662         break;
663       }
664     case 'm':
665       { if (i==argc || strlen(argv[i])>1 || !strchr("msca",argv[i][0]))
666 	  { printf ("Error reading command line argument m: should be 'm', 's', 'c', or 'a'\n");
667 	    return 0;
668 	  }
669         method = argv[i][0];
670         i++;
671         break;
672       }
673 
674 
675       default: printf ("Unknown option\n");
676     }
677    }
678 
679    if(jobname==0) jobname = setjobname(filename,1);
680 
681    /*  else
682    { display_help();
683     return 0;
684    }
685    */
686 
687     if (argc < 2) {
688       display_help();
689       return 0;
690     }
691 
692    //printf("num of clusters %d \n",nclust);
693    fprintf(stderr,"Patience, reading %s...\n ", filename);
694    im = mri_read_double_1D (filename);
695    /* ZIAD I get this warning
696    Aclustering.c:408: warning: passing argument 1 of mri_read_double_1D
697                            discards qualifiers from pointer target type
698    Andrej: filename was declared as (const char *),
699           but the function expects (char *)              */
700    if (!im) {
701     fprintf(stderr,"Error: Failed to read matrix data from %s\n",
702 	    argv[2]);
703     return(-1);
704    }
705    ncol = im->ny;
706    nrow = im->nx;
707    fprintf (stderr,"Have %d cols, %d rows\nNow...\n", ncol, nrow);
708 
709    /* now just get the array and kill the rest */
710    dar = MRI_DOUBLE_PTR(im);
711 
712    /* make sure that pointer is set to NULL in im, or risk hell */
713    mri_clear_data_pointer(im);
714    if (im) mri_free(im); im = NULL; /* now kill im */
715 
716 
717    /* for double loop*/
718    D = (double **)calloc(sizeof(double*), nrow-1);
719 
720    for (ii=0;ii<(nrow-1);++ii) {
721     D[ii] = (double *)calloc(sizeof(double), ncol-1);
722    }
723 
724    for (nl=1; nl<nrow; ++nl) {
725     for (nc=1; nc<ncol; ++nc) {
726       D[nl-1][nc-1] = dar[nl+nc*nrow];
727     }
728     //fprintf(stdout,"\n");
729   }
730 
731   //show_data(nrows, ncols, data, mask);
732   //example_mean_median(nrows, ncols, data, mask);
733   //distmatrix = example_distance_gene(nrows, ncols, data, mask);
734   //if (distmatrix) example_hierarchical(nrows, ncols, data, mask, distmatrix);
735   //example_distance_array(nrows, ncols, data, mask);
736   if(k>0) example_kmeans(nrow-1, ncol-1, D, k, r, distmetric, jobname);
737 
738   else
739     {
740       distmatrix = example_distance_gene(nrow-1, ncol-1, D); // ZIAD: goes2 SIGKILL error
741         if (distmatrix)
742 	  example_hierarchical(nrow-1, ncol-1, D, jobname, kh, distmatrix);
743         }
744 
745 
746 
747    free(dar); dar = NULL; /* done with input array */
748    // To free D
749    for (ii=0;ii<(nrow-1);++ii) {
750 
751     if (D[ii]) free(D[ii]);
752    }
753    free(D);
754    free(jobname);
755    //free();
756 
757    fprintf (stderr,"\n");
758    return 0;
759    }
760 
761    /* How to compile:
762    I copied Aclustering.c and cluster.c to /usr/local/bin/afni/src
763    then compile with:
764    gcc -Wall -Wno-unused Aclustering2.c cluster.c -o Aclustering -Inifti/niftilib -Inifti/nifticdf -Inifti/znzlib -L. -bind_at_load -l3DEdge -lmri -lf2c -lmri /usr/lib64/libXm.a -lXm -lXmu -lXp -lXpm -lXext -lXt -lX11 -lz -lexpat -lm -lc
765 
766    The output are 3 files
767    - cluster_out.kgg -- as before, index number and cluster number
768    - cluster_out.dis --
769    - cluster_out.cen --
770 
771 
772    actually
773    cp /home/drejc/c_temp/Aclustering.c ./
774 
775 gcc -Wall -g -Wno-unused Aclustering_NOMASK.c cluster_doubleNOMASK.c -o AclusteringNOMASK -Inifti/niftilib -Inifti/nifticdf -Inifti/znzlib -L. -lmri -lmri /usr/lib64/libXm.a -lXm -lXmu -lXp -lXpm -lXext -lXt -lX11 -lz -lexpat -lm -lc
776 
777 
778    cp AclusteringNOMASK /home/drejc/segm4ziad/toandrej/out_mprage_10xxx/
779 
780    */
781