| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | clusterstopping.pl - Predict the optimal number of clusters in a data set | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | clusterstopping.pl [OPTIONS] INPUTFILE | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Predicts the optimal number of clusters for the given data. This script tries to find | ||
| 14 | the optimal number of clusters for the given INPUTFILE. | ||
| 15 | |||
| 16 | =head1 INPUT | ||
| 17 | |||
| 18 | =head2 Required Arguments: | ||
| 19 | |||
| 20 | =head3 INPUTFILE | ||
| 21 | |||
| 22 | Matrix file containing either: | ||
| 23 | |||
| 24 | * context vectors (in dense or sparse format) | ||
| 25 | * similarity values between contexts | ||
| 26 | |||
| 27 | =head2 Optional Arguments: | ||
| 28 | |||
| 29 | =head4 --prefix PRE | ||
| 30 | |||
| 31 | Specify a prefix to be used for the output filenames. | ||
| 32 | |||
| 33 | If prefix is not specified then prefix is created by concatenating | ||
| 34 | time stamp to the string "expr". | ||
| 35 | |||
| 36 | =head4 --measure MSR | ||
| 37 | |||
| 38 | Specify the cluster stopping measure to be used. | ||
| 39 | |||
| 40 | (Further details about the measures given below under the Section "DETAILS ABOUT THE CLUSTER-STOPPING MEASURES") | ||
| 41 | |||
| 42 | The possible option values: | ||
| 43 | pk1 - PK1 measure | ||
| 44 | pk2 - PK2 measure | ||
| 45 | pk3 - PK3 measure [Default] | ||
| 46 | gap - Adapted Gap Statistic | ||
| 47 | pk - All the three PK measures | ||
| 48 | all - All the four measures - PK1, PK2, PK3 and Gap | ||
| 49 | |||
| 50 | =head4 --space SP | ||
| 51 | |||
| 52 | Specify if the clustering should be performed in vector or similarity space. | ||
| 53 | |||
| 54 | The possible option values: | ||
| 55 | vector [Default] | ||
| 56 | similarity | ||
| 57 | |||
| 58 | =head4 --delta INT | ||
| 59 | |||
| 60 | NOTE: Delta value can only be a positive integer value. | ||
| 61 | |||
| 62 | Specify 0 to stop the iterating clustering process when two consecutive crfun values | ||
| 63 | are exactly equal. This is the default setting when the crfun values are integer/whole numbers. | ||
| 64 | |||
| 65 | Specify non-zero positive integer to stop the iterating clustering process when the diffference | ||
| 66 | between two consecutive crfun values is less than or equal to this value. However, note that the | ||
| 67 | integer value specified is internally shifted to capture the difference in the least significant | ||
| 68 | digit of the crfun values when these crfun values are fractional. For example: | ||
| 69 | |||
| 70 | For crfun = 1.23e-02 & delta = 1 will be transformed to 0.0001 | ||
| 71 | For crfun = 2.45e-01 & delta = 5 will be transformed to 0.005 | ||
| 72 | |||
| 73 | The default delta value when the crfun values are fractional is 1. | ||
| 74 | |||
| 75 | However if the crfun values are integer/whole numbers (exponent >= 2) then the specified delta | ||
| 76 | value is internally shifted only until the least significant digit in the scientific notation. | ||
| 77 | For example: | ||
| 78 | |||
| 79 | For crfun = 1.23e+04 & delta = 2 will be transformed to 200 | ||
| 80 | For crfun = 2.45e+02 & delta = 5 will be transformed to 5 | ||
| 81 | For crfun = 1.44e+03 & delta = 1 will be transformed to 10 | ||
| 82 | |||
| 83 | =head4 --clmethod CL | ||
| 84 | |||
| 85 | Specifies the clustering method. | ||
| 86 | |||
| 87 | The possible option values: | ||
| 88 | |||
| 89 | rb - Repeated Bisections [Default] | ||
| 90 | rbr - Repeated Bisections for by k-way refinement | ||
| 91 | direct - Direct k-way clustering | ||
| 92 | agglo - Agglomerative clustering | ||
| 93 | bagglo - Partitional biased Agglomerative clustering [Only in vector space] | ||
| 94 | |||
| 95 | For large amount of data, 'rb', 'rbr' or 'direct' are recommended. | ||
| 96 | |||
| 97 | =head4 --crfun CR | ||
| 98 | |||
| 99 | Selects the criteria function for Clustering. The meanings of these criteria | ||
| 100 | functions are explained in Cluto's manual. | ||
| 101 | |||
| 102 | The possible option values: | ||
| 103 | |||
| 104 | i1 - I1 Criterion function | ||
| 105 | i2 - I2 Criterion function [Default] | ||
| 106 | h1 - H1 Criterion function | ||
| 107 | h2 - H2 Criterion function | ||
| 108 | e1 - E1 Criterion function | ||
| 109 | |||
| 110 | =head4 --sim SIM | ||
| 111 | |||
| 112 | Specifies the similarity measure to be used | ||
| 113 | |||
| 114 | The possible option values: | ||
| 115 | |||
| 116 | cos - Cosine [Default] | ||
| 117 | corr - Correlation Coefficient | ||
| 118 | |||
| 119 | NOTE: This option can be used only in vector space. | ||
| 120 | |||
| 121 | =head4 --rowmodel RMOD | ||
| 122 | |||
| 123 | The option is used to specify the model to be used to scale every | ||
| 124 | column of each row. (For further details please refer Cluto manual) | ||
| 125 | |||
| 126 | The possible option values: | ||
| 127 | |||
| 128 | none - no scaling is performed [Default] | ||
| 129 | maxtf - post scaling the values are between 0.5 and 1.0 | ||
| 130 | sqrt - square-root of actual values | ||
| 131 | log - log of actual values | ||
| 132 | |||
| 133 | =head4 --colmodel CMOD | ||
| 134 | |||
| 135 | The option is used to specify the model to be used to (globally) scale each | ||
| 136 | column across all rows. (For further details please refer Cluto manual) | ||
| 137 | |||
| 138 | The possible option values: | ||
| 139 | |||
| 140 | none - no scaling is performed [Default] | ||
| 141 | idf - scaling according to inverse-document-frequency | ||
| 142 | |||
| 143 | =head4 --threspk1 NUM | ||
| 144 | |||
| 145 | The threshold value that should be used by the PK1 measure to predict the | ||
| 146 | k value. | ||
| 147 | |||
| 148 | Default = -0.7 | ||
| 149 | |||
| 150 | =head4 --precision NUM | ||
| 151 | |||
| 152 | Specifies the precision to be used. | ||
| 153 | |||
| 154 | Default: 4 | ||
| 155 | |||
| 156 | =head3 Adapted Gap Statistic related options: | ||
| 157 | |||
| 158 | =head4 --B NUM | ||
| 159 | |||
| 160 | The number of replicates/references to be generated. | ||
| 161 | |||
| 162 | Default: 1 | ||
| 163 | |||
| 164 | =head4 --typeref TYP | ||
| 165 | |||
| 166 | Specifies whether to generate B replicates from a reference or to generate | ||
| 167 | B references. | ||
| 168 | |||
| 169 | The possible option values: | ||
| 170 | |||
| 171 | rep - replicates [Default] | ||
| 172 | ref - references | ||
| 173 | |||
| 174 | =head4 --seed NUM | ||
| 175 | |||
| 176 | The seed to be used with the random number generator. | ||
| 177 | |||
| 178 | Default: No seed is set. | ||
| 179 | |||
| 180 | =head4 --percentage NUM | ||
| 181 | |||
| 182 | Specifies the percentage confidence to be reported in the log file. | ||
| 183 | Since Statistics::Gap uses parametric bootstrap method for reference distribution | ||
| 184 | generation, it is critical to understand the interval around the sample mean that | ||
| 185 | could contain the population ("true") mean and with what certainty. | ||
| 186 | |||
| 187 | Default: 90 | ||
| 188 | |||
| 189 | =head3 Other Options : | ||
| 190 | |||
| 191 | =head4 --help | ||
| 192 | |||
| 193 | Displays the quick summary of program options. | ||
| 194 | |||
| 195 | =head4 --version | ||
| 196 | |||
| 197 | Displays the version information. | ||
| 198 | |||
| 199 | =head4 --verbose | ||
| 200 | |||
| 201 | Displays to STDERR the current program status. | ||
| 202 | |||
| 203 | =head1 OUTPUT | ||
| 204 | |||
| 205 | =over | ||
| 206 | |||
| 207 | =item * prefix.pk(1|2|3) | ||
| 208 | |||
| 209 | Contains the crfun values, (PK1, PK2, PK3) values and the predicted k. | ||
| 210 | |||
| 211 | =item * prefix.gap | ||
| 212 | |||
| 213 | Contains the crfun values, delta values and the predicted k. | ||
| 214 | |||
| 215 | =item * prefix.gap.log | ||
| 216 | |||
| 217 | Contains a table of values for Gap(k), Obs(crfun(k)), Exp(crfun(k)), sd(k), s(k) and Confidence interval. | ||
| 218 | Also contains individual crfun values for each of the B replicates/references for every value of K. | ||
| 219 | |||
| 220 | =back | ||
| 221 | |||
| 222 | The following files (*.dat) files are created to facilitate | ||
| 223 | creation of plots if required. | ||
| 224 | |||
| 225 | =over | ||
| 226 | |||
| 227 | =item * prefix.cr.dat | ||
| 228 | |||
| 229 | Contains the crfun values. | ||
| 230 | |||
| 231 | =item * prefix.pk(1|2|3).dat | ||
| 232 | |||
| 233 | Contains the PK1, PK2, or PK3 values. | ||
| 234 | |||
| 235 | =item * prefix.exp.dat | ||
| 236 | |||
| 237 | Contains the Exp(crfun(k)) values generated for Gap Statistics. | ||
| 238 | |||
| 239 | =item * prefix.gap.dat | ||
| 240 | |||
| 241 | Contains the gap(k) values. | ||
| 242 | |||
| 243 | =back | ||
| 244 | |||
| 245 | =head1 DETAILS ABOUT THE CLUSTER-STOPPING MEASURES | ||
| 246 | |||
| 247 | Each of the four cluster-stopping measure PK1, PK2, PK3 and Gap try to predict | ||
| 248 | the optimal number of clusters for the provided data (represented in the form | ||
| 249 | of vectors or similarity values). | ||
| 250 | |||
| 251 | Following is a description about each of the measures and how they select a k value. | ||
| 252 | |||
| 253 | =head2 Formulation for PK1: | ||
| 254 | |||
| 255 | crfun[m] - mean(crfun[1...deltaM]) | ||
| 256 | PK1[m] = ----------------------------------- | ||
| 257 | std(crfun[1...deltaM]) | ||
| 258 | |||
| 259 | where m = 2,...deltaM | ||
| 260 | deltaM is a m value beyond which the crfun value does not change much. | ||
| 261 | |||
| 262 | k selection strategy: If m is the first value for which the PK1 score is | ||
| 263 | greater than the threshold value then m-1 value is selected as the k value | ||
| 264 | when using one of the maximization criterion functions like I2, H2 etc. | ||
| 265 | While using minimization criterion functions like E1, if m the first value | ||
| 266 | for which the PK1 score is less than the threshold value then m-1 is | ||
| 267 | selected as the k value. | ||
| 268 | |||
| 269 | =head2 Formulation for PK2: | ||
| 270 | |||
| 271 | crfun[m] | ||
| 272 | PK2[m] = ------------ | ||
| 273 | crfun[m-1] | ||
| 274 | |||
| 275 | where m = 2,...deltaM | ||
| 276 | |||
| 277 | k selection strategy: One standard deviation interval for the set of PK2[m] | ||
| 278 | values id calculated and the m value for which the PK2 score is outside this | ||
| 279 | interval but is still closest to the interval (from outside) is selected as | ||
| 280 | the k value. | ||
| 281 | |||
| 282 | =head2 Formulation for PK3: | ||
| 283 | |||
| 284 | 2 * crfun[m] | ||
| 285 | PK3[m] = ------------------------- | ||
| 286 | crfun[m-1] + crfun[m+1] | ||
| 287 | |||
| 288 | where m = 2,...deltaM | ||
| 289 | |||
| 290 | k selection strategy: Similar to that of PK2. One standard deviation of the | ||
| 291 | PK3[m] scores is computed and the m value for which the PK3 score is outside | ||
| 292 | this interval but is still closest to the interval (from outside) is selected | ||
| 293 | as the k value. | ||
| 294 | |||
| 295 | =head2 Adapted Gap Statistic: | ||
| 296 | |||
| 297 | Adapted Gap tries to predict the optimal number of clusters by comparing the | ||
| 298 | criterion function values that one gets for the observed/given data with the | ||
| 299 | criterion function values that one gets for a random data. It uses hypothesis | ||
| 300 | testing in this process where the null hypothesis is that the optimal number | ||
| 301 | of clusters for the given data is 1 and the aim is to refute this null | ||
| 302 | hypothesis if the given data exhibits enough pattern. | ||
| 303 | |||
| 304 | The plot of crfun values for given data is compared with the plot of crfun | ||
| 305 | values for random or reference data. The m value corresponding to the first | ||
| 306 | point of maximum difference between the two plots is chosen as the predicted | ||
| 307 | k when using the maximization crfuns. While for the minimization crfuns, the | ||
| 308 | m value corresponding to the first point of minimum difference between the | ||
| 309 | two plots is choosen as the predicted k value. | ||
| 310 | |||
| 311 | We provide two methods for the generation of reference data: | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | =item 1. One can choose to generate a random dataset over the observed distribution by | ||
| 316 | holding the row and the column marginals fixed and then generating B replicates | ||
| 317 | from this random dataset using Monte Carlo sampling. | ||
| 318 | |||
| 319 | =item 2. Or to generate B random datasets over the observed distribution by holding | ||
| 320 | the row and the column marginals fixed. | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | Please refer to L<http://search.cpan.org/dist/Algorithm-RandomMatrixGeneration/> to | ||
| 325 | learn more about generating random dataset over the observed distribution by holding | ||
| 326 | the row and the column marginals fixed. | ||
| 327 | |||
| 328 | =head1 DETAILS ABOUT THE INPUT MATRIX FORMAT | ||
| 329 | |||
| 330 | The input matrix can be in either dense or sparse format. The cell values can | ||
| 331 | be integer or real. Depending upon the value specified for the space parameter | ||
| 332 | the header in the input file (first line) changes. | ||
| 333 | |||
| 334 | Example of input matrix in dense format when space=vector: | ||
| 335 | |||
| 336 | The first line specifies the dimensions - #rows #cols | ||
| 337 | |||
| 338 | From the second line the actual matrix follows. | ||
| 339 | |||
| 340 | 6 5 | ||
| 341 | 1.3 2 0 0 3 | ||
| 342 | 2.1 0 4 2.7 0 | ||
| 343 | 1.3 2 0 0 3 | ||
| 344 | 2.1 0 4 2.7 0 | ||
| 345 | 1.3 2 0 0 3 | ||
| 346 | 2.1 0 4 2.7 0 | ||
| 347 | |||
| 348 | Example of input matrix in dense format when space=similarity: | ||
| 349 | |||
| 350 | The matrix, when in similarity space, is square and symmetric. | ||
| 351 | |||
| 352 | The first line specifies the dimensions - #rows/#cols | ||
| 353 | |||
| 354 | From the second line the actual matrix follows. | ||
| 355 | |||
| 356 | 5 | ||
| 357 | 1.0000 0.3179 0.5544 0.2541 0.4431 | ||
| 358 | 0.3179 1.0000 0.1386 0.4599 0.5413 | ||
| 359 | 0.5544 0.1386 1.0000 0.5143 0.2186 | ||
| 360 | 0.2541 0.4599 0.5143 1.0000 0.5148 | ||
| 361 | 0.4431 0.5413 0.2186 0.5148 1.0000 | ||
| 362 | |||
| 363 | Example of input matrix in sparse format when space=vector: | ||
| 364 | |||
| 365 | The first line specifies the dimensions & number of non-zero elements - #rows #cols #nonzeroElem | ||
| 366 | |||
| 367 | From the second line the matrix contents follow. Only non-zero elements are specified. Thus the | ||
| 368 | elements are specified as pairs of - #col elem. The row number is implied by the (line number-1). | ||
| 369 | |||
| 370 | 8 10 41 | ||
| 371 | 1 3 4 2 8 2 10 1 | ||
| 372 | 1 1 2 5 3 1 5 2 7 1 9 2 | ||
| 373 | 1 3 4 2 8 2 10 1 | ||
| 374 | 1 1 2 5 3 1 5 2 7 1 9 2 | ||
| 375 | 1 3 4 2 8 2 10 1 | ||
| 376 | 1 1 2 5 3 1 5 2 7 1 9 2 | ||
| 377 | 2 4 3 1 4 2 5 5 7 1 9 1 | ||
| 378 | 2 4 3 1 4 2 5 5 7 1 9 1 | ||
| 379 | |||
| 380 | Example of input matrix in sparse format when space=similarity: | ||
| 381 | |||
| 382 | The first line specifies the dimensions & number of non-zero elements - #rows/#cols #nonzeroElem | ||
| 383 | |||
| 384 | The matrix format is same as explained above. | ||
| 385 | |||
| 386 | 5 15 | ||
| 387 | 1 1.0000 3 0.5544 5 0.4431 | ||
| 388 | 2 1.0000 3 0.1386 4 0.4599 5 0.5413 | ||
| 389 | 1 0.5544 2 0.1386 3 1.0000 | ||
| 390 | 2 0.4599 4 1.0000 | ||
| 391 | 1 0.4431 2 0.5413 5 1.0000 | ||
| 392 | |||
| 393 | =head1 AUTHORS | ||
| 394 | |||
| 395 | Anagha Kulkarni, Carnegie-Mellon University | ||
| 396 | |||
| 397 | Ted Pedersen, University of Minnesota, Duluth | ||
| 398 | tpederse at d.umn.edu | ||
| 399 | |||
| 400 | =head1 COPYRIGHT | ||
| 401 | |||
| 402 | Copyright (c) 2006-2008, Anagha Kulkarni and Ted Pedersen | ||
| 403 | |||
| 404 | This program is free software; you can redistribute it and/or modify it under | ||
| 405 | the terms of the GNU General Public License as published by the Free Software | ||
| 406 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 407 | version. | ||
| 408 | |||
| 409 | This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 410 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 411 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
| 412 | |||
| 413 | You should have received a copy of the GNU General Public License along with | ||
| 414 | this program; if not, write to | ||
| 415 | |||
| 416 | The Free Software Foundation, Inc., | ||
| 417 | 59 Temple Place - Suite 330, | ||
| 418 | Boston, MA 02111-1307, USA. | ||
| 419 | |||
| 420 | =cut | ||
| 421 | |||
| 422 | ############################################################################### | ||
| 423 | # THE CODE STARTS HERE | ||
| 424 | |||
| 425 | #$0 contains the program name along with | ||
| 426 | #the complete path. Extract just the program | ||
| 427 | #name and use in error messages | ||
| 428 | $0=~s/.*\/(.+)/$1/; | ||
| 429 | |||
| 430 | ############################################################################### | ||
| 431 | |||
| 432 | # ================================ | ||
| 433 | # COMMAND LINE OPTIONS AND USAGE | ||
| 434 | # ================================ | ||
| 435 | |||
| 436 | #use strict; | ||
| 437 | use POSIX qw(floor ceil); | ||
| 438 | |||
| 439 | ### dependence on BigFloat only exists in RandomMatrix Generation | ||
| 440 | ### use Math::BigFloat; | ||
| 441 | |||
| 442 | use Algorithm::RandomMatrixGeneration; | ||
| 443 | use Getopt::Long; | ||
| 444 | |||
| 445 | # command line options | ||
| 446 | eval(GetOptions ("help","version","verbose","prefix=s","measure=s","space=s","delta=i","clmethod=s","crfun=s","sim=s","rowmodel=s","colmodel=s","threspk1=f","B=i","typeref=s","percentage=i","precision=i","seed=i")) or die("Please check the above mentioned option(s).\n"); | ||
| 447 | |||
| 448 | # show help option | ||
| 449 | if(defined $opt_help) | ||
| 450 | { | ||
| 451 | $opt_help=1; | ||
| 452 | &showhelp(); | ||
| 453 | exit; | ||
| 454 | } | ||
| 455 | |||
| 456 | # show version information | ||
| 457 | if(defined $opt_version) | ||
| 458 | { | ||
| 459 | $opt_version=1; | ||
| 460 | &showversion(); | ||
| 461 | exit; | ||
| 462 | } | ||
| 463 | |||
| 464 | # show minimal usage message if no arguments | ||
| 465 | if($#ARGV<0) | ||
| 466 | { | ||
| 467 | &showminimal(); | ||
| 468 | exit 1; | ||
| 469 | } | ||
| 470 | |||
| 471 | ############################################################################# | ||
| 472 | |||
| 473 | # ==================================== | ||
| 474 | # INITIALIZATION AND ERROR CHECKS | ||
| 475 | # ==================================== | ||
| 476 | |||
| 477 | # ---------- | ||
| 478 | # Input file | ||
| 479 | # ---------- | ||
| 480 | if(!defined $ARGV[0]) | ||
| 481 | { | ||
| 482 | print STDERR "ERROR($0): | ||
| 483 | Please specify the INPUTFILE file name...\n"; | ||
| 484 | exit 1; | ||
| 485 | } | ||
| 486 | $inpfile=$ARGV[0]; | ||
| 487 | |||
| 488 | if(!-e $inpfile) | ||
| 489 | { | ||
| 490 | print STDERR "ERROR($0): | ||
| 491 | Could not locate the INPUTFILE file $inpfile\n"; | ||
| 492 | exit 1; | ||
| 493 | } | ||
| 494 | |||
| 495 | # prefix | ||
| 496 | if(defined $opt_prefix) | ||
| 497 | { | ||
| 498 | $prefix=$opt_prefix; | ||
| 499 | } | ||
| 500 | else | ||
| 501 | { | ||
| 502 | $prefix="expr" . time(); | ||
| 503 | } | ||
| 504 | |||
| 505 | # measure | ||
| 506 | if(defined $opt_measure && $opt_measure !~ /^(all|pk|pk1|pk2|pk3|gap)$/i) | ||
| 507 | { | ||
| 508 | print STDERR "ERROR($0): | ||
| 509 | $opt_measure is not a valid option value for --measure\n"; | ||
| 510 | exit 1; | ||
| 511 | } | ||
| 512 | |||
| 513 | if(defined $opt_measure) | ||
| 514 | { | ||
| 515 | $measure = lc $opt_measure; | ||
| 516 | } | ||
| 517 | else #default | ||
| 518 | { | ||
| 519 | $measure = "pk3"; | ||
| 520 | } | ||
| 521 | |||
| 522 | # check for -ve delta value | ||
| 523 | if(defined $opt_delta && $opt_delta < 0) | ||
| 524 | { | ||
| 525 | print STDERR "ERROR($0): | ||
| 526 | $opt_delta is not a valid option value for --delta. | ||
| 527 | The delta value should be a positive integer.\n"; | ||
| 528 | exit 1; | ||
| 529 | } | ||
| 530 | |||
| 531 | # space | ||
| 532 | if(defined $opt_space && $opt_space !~ /^(vector|similarity)$/i) | ||
| 533 | { | ||
| 534 | print STDERR "ERROR($0): | ||
| 535 | $opt_space is not a valid option value for --space\n"; | ||
| 536 | exit 1; | ||
| 537 | } | ||
| 538 | if(defined $opt_space) | ||
| 539 | { | ||
| 540 | $space = lc $opt_space; | ||
| 541 | } | ||
| 542 | else #default | ||
| 543 | { | ||
| 544 | $space = "vector"; | ||
| 545 | } | ||
| 546 | |||
| 547 | # delta | ||
| 548 | if(defined $opt_delta) | ||
| 549 | { | ||
| 550 | $delta = $opt_delta; | ||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | # clmethod | ||
| 555 | if(defined $opt_clmethod && $opt_clmethod !~ /^(rb|rbr|direct|agglo|bagglo)$/i) | ||
| 556 | { | ||
| 557 | print STDERR "ERROR($0): | ||
| 558 | $opt_clmethod is not a valid option value for --clmethod\n"; | ||
| 559 | exit 1; | ||
| 560 | } | ||
| 561 | if(defined $opt_clmethod) | ||
| 562 | { | ||
| 563 | $clmethod = lc $opt_clmethod; | ||
| 564 | } | ||
| 565 | else #default | ||
| 566 | { | ||
| 567 | $clmethod = "rb"; | ||
| 568 | } | ||
| 569 | |||
| 570 | if($space eq "similarity" && $clmethod eq "bagglo") | ||
| 571 | { | ||
| 572 | print STDERR "ERROR($0): | ||
| 573 | Option value \"bagglo\" for --clmethod cannot be used in similarity space.\n"; | ||
| 574 | exit 1; | ||
| 575 | } | ||
| 576 | |||
| 577 | # crfun | ||
| 578 | if(defined $opt_crfun && $opt_crfun !~ /^(i1|i2|h1|h2|e1)$/i) | ||
| 579 | { | ||
| 580 | print STDERR "ERROR($0): | ||
| 581 | $opt_crfun is not a valid option value for --crfun\n"; | ||
| 582 | exit 1; | ||
| 583 | } | ||
| 584 | #if(defined $opt_crfun && $opt_crfun eq "e1" && defined $opt_measure && ($opt_measure ne "all" || $opt_measure ne "gap")) | ||
| 585 | #{ | ||
| 586 | # print STDERR "ERROR($0): | ||
| 587 | # --crfun e1 can be used only with --measure gap or --measure all\n"; | ||
| 588 | # exit 1; | ||
| 589 | #} | ||
| 590 | if(defined $opt_crfun) | ||
| 591 | { | ||
| 592 | $crfun = lc $opt_crfun; | ||
| 593 | $crfun_name = uc $opt_crfun; | ||
| 594 | } | ||
| 595 | else #default | ||
| 596 | { | ||
| 597 | $crfun = "i2"; | ||
| 598 | $crfun_name = "I2"; | ||
| 599 | } | ||
| 600 | |||
| 601 | # sim | ||
| 602 | if(defined $opt_sim && $opt_sim !~ /^(cos|corr)$/i) | ||
| 603 | { | ||
| 604 | print STDERR "ERROR($0): | ||
| 605 | $opt_sim is not a valid option value for --sim\n"; | ||
| 606 | exit 1; | ||
| 607 | } | ||
| 608 | if(defined $opt_space && $opt_space eq "similarity" && defined $opt_sim) | ||
| 609 | { | ||
| 610 | print STDERR "ERROR($0): | ||
| 611 | --sim option can be used only in vector space. \n"; | ||
| 612 | exit 1; | ||
| 613 | } | ||
| 614 | if(defined $opt_sim) | ||
| 615 | { | ||
| 616 | $sim = lc $opt_sim; | ||
| 617 | } | ||
| 618 | elsif($space eq "vector") #default | ||
| 619 | { | ||
| 620 | $sim = "cos"; | ||
| 621 | } | ||
| 622 | |||
| 623 | # rowmodel | ||
| 624 | if(defined $opt_rowmodel && $opt_rowmodel !~/^(none|maxtf|sqrt|log)$/i) | ||
| 625 | { | ||
| 626 | print STDERR "ERROR($0): | ||
| 627 | Specified rowmodel value: $opt_rowmodel is not supported.\n"; | ||
| 628 | exit 1; | ||
| 629 | } | ||
| 630 | if(defined $opt_space && $opt_space eq "similarity" && defined $opt_rowmodel) | ||
| 631 | { | ||
| 632 | print STDERR "ERROR($0): | ||
| 633 | --rowmodel option can be used only in vector space. \n"; | ||
| 634 | exit 1; | ||
| 635 | } | ||
| 636 | if(defined $opt_rowmodel) | ||
| 637 | { | ||
| 638 | $rowmodel = lc $opt_rowmodel; | ||
| 639 | } | ||
| 640 | elsif($space eq "vector") #default | ||
| 641 | { | ||
| 642 | $rowmodel = "none"; | ||
| 643 | } | ||
| 644 | |||
| 645 | # colmodel | ||
| 646 | if(defined $opt_colmodel && $opt_colmodel !~/^(none|idf)$/i) | ||
| 647 | { | ||
| 648 | print STDERR "ERROR($0): | ||
| 649 | Specified colmodel value: $opt_colmodel is not supported.\n"; | ||
| 650 | exit 1; | ||
| 651 | } | ||
| 652 | if(defined $opt_space && $opt_space eq "similarity" && defined $opt_colmodel) | ||
| 653 | { | ||
| 654 | print STDERR "ERROR($0): | ||
| 655 | --colmodel option can be used only in vector space. \n"; | ||
| 656 | exit 1; | ||
| 657 | } | ||
| 658 | if(defined $opt_colmodel) | ||
| 659 | { | ||
| 660 | $colmodel = lc $opt_colmodel; | ||
| 661 | } | ||
| 662 | elsif($space eq "vector") #default | ||
| 663 | { | ||
| 664 | $colmodel = "none"; | ||
| 665 | } | ||
| 666 | |||
| 667 | # threspk1 | ||
| 668 | if(($measure ne "all" && $measure ne "pk" && $measure ne "pk1") && defined $opt_threspk1) | ||
| 669 | { | ||
| 670 | print STDERR "ERROR($0): | ||
| 671 | --threspk1 option can be used only when using --measure all or pk1 \n"; | ||
| 672 | exit 1; | ||
| 673 | } | ||
| 674 | if(defined $opt_threspk1) | ||
| 675 | { | ||
| 676 | $thres_pk1 = $opt_threspk1; | ||
| 677 | } | ||
| 678 | elsif($measure eq "all" || $measure eq "pk" || $measure eq "pk1") | ||
| 679 | { | ||
| 680 | $thres_pk1 = -0.7; | ||
| 681 | } | ||
| 682 | |||
| 683 | my $B = 1; #default | ||
| 684 | if(defined $opt_B) | ||
| 685 | { | ||
| 686 | $B = $opt_B; | ||
| 687 | } | ||
| 688 | |||
| 689 | my $typeref = "rep"; #default | ||
| 690 | if(defined $opt_typeref && $opt_typeref !~ /^(rep|ref)$/i) | ||
| 691 | { | ||
| 692 | print STDERR "ERROR($0): | ||
| 693 | Specified typeref value: $opt_typeref is not supported.\n"; | ||
| 694 | exit 1; | ||
| 695 | } | ||
| 696 | elsif(defined $opt_typeref) | ||
| 697 | { | ||
| 698 | $typeref = $opt_typeref; | ||
| 699 | } | ||
| 700 | |||
| 701 | my $percentage = 90; #default | ||
| 702 | if(defined $opt_percentage) | ||
| 703 | { | ||
| 704 | $percentage = $opt_percentage; | ||
| 705 | } | ||
| 706 | |||
| 707 | my $precision = 4; #default | ||
| 708 | if(defined $opt_precision) | ||
| 709 | { | ||
| 710 | $precision = $opt_precision; | ||
| 711 | } | ||
| 712 | |||
| 713 | my $precision_str = '%.' . $precision . 'f'; | ||
| 714 | |||
| 715 | my $seed; | ||
| 716 | if(defined $opt_seed) | ||
| 717 | { | ||
| 718 | $seed = $opt_seed; | ||
| 719 | } | ||
| 720 | |||
| 721 | $cwd = `pwd`; | ||
| 722 | chomp($cwd); | ||
| 723 | |||
| 724 | #********************************************************************* | ||
| 725 | |||
| 726 | # for matrix dimensions | ||
| 727 | my $rcnt = 0; | ||
| 728 | my $ccnt = 0; | ||
| 729 | |||
| 730 | # read the input matrix file | ||
| 731 | open(INP,"<$inpfile") || die "Error opening input matrix file <$inpfile>\n"; | ||
| 732 | |||
| 733 | my $line; | ||
| 734 | $line = <INP>; | ||
| 735 | chomp($line); | ||
| 736 | |||
| 737 | #remove leading white spaces | ||
| 738 | $line=~s/^\s+//; | ||
| 739 | |||
| 740 | my $format = 0; # determine the format of the input matrix - dense (0) / sparse (1) | ||
| 741 | if($space eq "vector") | ||
| 742 | { | ||
| 743 | my @tmp = (); | ||
| 744 | @tmp = split(/\s+/,$line); | ||
| 745 | $rcnt = $tmp[0]; | ||
| 746 | $ccnt = $tmp[1]; | ||
| 747 | |||
| 748 | if($#tmp == 2) | ||
| 749 | { | ||
| 750 | $format = 1; #sparse | ||
| 751 | } | ||
| 752 | } | ||
| 753 | else | ||
| 754 | { | ||
| 755 | my @tmp = (); | ||
| 756 | @tmp = split(/\s+/,$line); | ||
| 757 | $rcnt = $tmp[0]; | ||
| 758 | $ccnt = $tmp[0]; | ||
| 759 | |||
| 760 | if($#tmp == 1) | ||
| 761 | { | ||
| 762 | $format = 1; #sparse | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | my @rmar; | ||
| 767 | my @cmar; | ||
| 768 | |||
| 769 | if($measure eq "all" || $measure eq "gap") | ||
| 770 | { | ||
| 771 | # for row and column marginals | ||
| 772 | @rmar = ((0) x $rcnt); | ||
| 773 | @cmar = ((0) x $ccnt); | ||
| 774 | |||
| 775 | # find the row and column marginals for the input matrix | ||
| 776 | if($format) # for sparse input format | ||
| 777 | { | ||
| 778 | my $row = 0; | ||
| 779 | |||
| 780 | while(<INP>) | ||
| 781 | { | ||
| 782 | # remove the newline at the end of the input line | ||
| 783 | chomp; | ||
| 784 | |||
| 785 | # for empty lines | ||
| 786 | if(m/^\s*\s*\s*$/) | ||
| 787 | { | ||
| 788 | next; | ||
| 789 | } | ||
| 790 | |||
| 791 | # remove leading white spaces | ||
| 792 | s/^\s+//; | ||
| 793 | |||
| 794 | # separate individual values in a line | ||
| 795 | my @tmp = (); | ||
| 796 | @tmp = split(/\s+/); | ||
| 797 | |||
| 798 | for(my $i=0; $i<$#tmp; $i=$i+2) | ||
| 799 | { | ||
| 800 | $rmar[$row] += $tmp[$i+1]; | ||
| 801 | my $tmp_col = $tmp[$i] - 1; | ||
| 802 | $cmar[$tmp_col] += $tmp[$i+1]; | ||
| 803 | } | ||
| 804 | |||
| 805 | $row++; | ||
| 806 | } | ||
| 807 | } | ||
| 808 | else # for dense input format | ||
| 809 | { | ||
| 810 | my $row = 0; | ||
| 811 | while(<INP>) | ||
| 812 | { | ||
| 813 | # remove the newline at the end of the input line | ||
| 814 | chomp; | ||
| 815 | |||
| 816 | # skip empty lines | ||
| 817 | if(m/^\s*\s*\s*$/) | ||
| 818 | { | ||
| 819 | next; | ||
| 820 | } | ||
| 821 | |||
| 822 | # remove leading white spaces | ||
| 823 | s/^\s+//; | ||
| 824 | |||
| 825 | # separate individual values in a line | ||
| 826 | my @tmp = (); | ||
| 827 | @tmp = split(/\s+/); | ||
| 828 | for(my $i=0; $i<=$#tmp; $i++) | ||
| 829 | { | ||
| 830 | $rmar[$row] += $tmp[$i]; | ||
| 831 | $cmar[$i] += $tmp[$i]; | ||
| 832 | } | ||
| 833 | |||
| 834 | $row++; | ||
| 835 | } | ||
| 836 | } | ||
| 837 | |||
| 838 | close INP; | ||
| 839 | } | ||
| 840 | |||
| 841 | if($measure eq "all") | ||
| 842 | { | ||
| 843 | open(OP1,">$prefix.pk1") || die "Error opening <$prefix.pk1> for writing.\n"; | ||
| 844 | open(OP2,">$prefix.pk2") || die "Error opening <$prefix.pk2> for writing.\n"; | ||
| 845 | open(OP3,">$prefix.pk3") || die "Error opening <$prefix.pk3> for writing.\n"; | ||
| 846 | open(OP4,">$prefix.gap") || die "Error opening <$prefix.gap> for writing.\n"; | ||
| 847 | } | ||
| 848 | elsif($measure eq "pk") | ||
| 849 | { | ||
| 850 | open(OP1,">$prefix.pk1") || die "Error opening <$prefix.pk1> for writing.\n"; | ||
| 851 | open(OP2,">$prefix.pk2") || die "Error opening <$prefix.pk2> for writing.\n"; | ||
| 852 | open(OP3,">$prefix.pk3") || die "Error opening <$prefix.pk3> for writing.\n"; | ||
| 853 | } | ||
| 854 | elsif($measure eq "pk1") | ||
| 855 | { | ||
| 856 | open(OP1,">$prefix.pk1") || die "Error opening <$prefix.pk1> for writing.\n"; | ||
| 857 | } | ||
| 858 | elsif($measure eq "pk2") | ||
| 859 | { | ||
| 860 | open(OP2,">$prefix.pk2") || die "Error opening <$prefix.pk2> for writing.\n"; | ||
| 861 | } | ||
| 862 | elsif($measure eq "pk3") | ||
| 863 | { | ||
| 864 | open(OP3,">$prefix.pk3") || die "Error opening <$prefix.pk3> for writing.\n"; | ||
| 865 | } | ||
| 866 | elsif($measure eq "gap") | ||
| 867 | { | ||
| 868 | open(OP4,">$prefix.gap") || die "Error opening <$prefix.gap> for writing.\n"; | ||
| 869 | } | ||
| 870 | |||
| 871 | # output file for crfun values (till deltaK) | ||
| 872 | open(FPCR,">$prefix.cr.dat") || die "Error opening <$prefix.h2.dat> for writing.\n"; | ||
| 873 | |||
| 874 | # vector space | ||
| 875 | if($space eq "vector") | ||
| 876 | { | ||
| 877 | # form the string of parameters for vcluster | ||
| 878 | $cluster_string =""; | ||
| 879 | |||
| 880 | $cluster_string .="--clmethod $clmethod "; | ||
| 881 | |||
| 882 | $cluster_string .="--crfun $crfun "; | ||
| 883 | |||
| 884 | $cluster_string .="--sim $sim "; | ||
| 885 | |||
| 886 | $cluster_string .="--rowmodel $rowmodel "; | ||
| 887 | |||
| 888 | $cluster_string .="--colmodel $colmodel "; | ||
| 889 | |||
| 890 | $cluster_string .="--nooutput "; | ||
| 891 | } | ||
| 892 | else # similarity space | ||
| 893 | { | ||
| 894 | # form the string of parameters for scluster | ||
| 895 | $cluster_string =""; | ||
| 896 | |||
| 897 | $cluster_string .="--clmethod $clmethod "; | ||
| 898 | |||
| 899 | $cluster_string .="--crfun $crfun "; | ||
| 900 | |||
| 901 | $cluster_string .="--nooutput "; | ||
| 902 | } | ||
| 903 | |||
| 904 | # initializations | ||
| 905 | $k = 0; | ||
| 906 | $last_k = 0; | ||
| 907 | $mean = 0; | ||
| 908 | $break_flag = 0; | ||
| 909 | $nan_flag = 0; | ||
| 910 | |||
| 911 | # keep clustering the input vectors into k clusters until deltaK is reached | ||
| 912 | do | ||
| 913 | { | ||
| 914 | $k++; | ||
| 915 | |||
| 916 | if($k > $rcnt) | ||
| 917 | { | ||
| 918 | print STDERR "ERROR($0): | ||
| 919 | $crfun values do not converge for the delta value of $delta | ||
| 920 | (internally: $thres). Try using larger delta value. \n"; | ||
| 921 | exit 1; | ||
| 922 | } | ||
| 923 | |||
| 924 | if($space eq "vector") | ||
| 925 | { | ||
| 926 | # cluster the vectors into k clusters | ||
| 927 | my $status = system("vcluster $cluster_string $inpfile $k > $cwd/$inpfile.$k"); | ||
| 928 | die "Error while running vcluster $cluster_string $inpfile $k\n" unless $status==0; | ||
| 929 | } | ||
| 930 | else # similarity space | ||
| 931 | { | ||
| 932 | my $status = system("scluster $cluster_string $inpfile $k > $cwd/$inpfile.$k"); | ||
| 933 | die "Error while running scluster $cluster_string $inpfile $k\n" unless $status==0; | ||
| 934 | } | ||
| 935 | |||
| 936 | # open cluto output file to be used to read the crfun values | ||
| 937 | open(FP,"$cwd/$inpfile.$k") || die "Error opening file <$inpfile.$k>\n"; | ||
| 938 | |||
| 939 | # read the complete file in one command | ||
| 940 | $temp = $/; | ||
| 941 | $/ = undef; | ||
| 942 | $str = <FP>; | ||
| 943 | $/ = $temp; | ||
| 944 | close FP; | ||
| 945 | unlink "$inpfile.$k"; | ||
| 946 | |||
| 947 | # read the crfun value | ||
| 948 | $str =~ /\-way clustering: \[.*=(.*?)\]/; | ||
| 949 | |||
| 950 | my $tmp_crfun = $1; | ||
| 951 | |||
| 952 | # check for "nan" | ||
| 953 | if($tmp_crfun =~ /nan/) | ||
| 954 | { | ||
| 955 | $nan_flag = 1; | ||
| 956 | $last_k = $k-1; | ||
| 957 | $break_flag = 1; | ||
| 958 | } | ||
| 959 | else | ||
| 960 | { | ||
| 961 | # array of crfun values for various k values | ||
| 962 | $cr[$k] = $tmp_crfun; | ||
| 963 | |||
| 964 | # print the crfun value to *.dat file | ||
| 965 | print FPCR "$k $cr[$k]\n"; | ||
| 966 | |||
| 967 | # decide the delta value | ||
| 968 | |||
| 969 | # If a delta value is specified by the user then transform this value and then use it. | ||
| 970 | # Else if crfun value is a integer/whole number then set the delta to 0 (exact match). | ||
| 971 | # Else if crfun value is a fractional number then set the delta to 1. | ||
| 972 | |||
| 973 | # if opt_delta not specified | ||
| 974 | if(!defined $opt_delta) | ||
| 975 | { | ||
| 976 | $cr[$k] =~ /e(.+)/; | ||
| 977 | $exponent = $1; | ||
| 978 | |||
| 979 | # if crfun is a integer (exponent >= 2) then use exact match (delta = 0) | ||
| 980 | if($exponent >= 2) | ||
| 981 | { | ||
| 982 | $delta = 0; | ||
| 983 | } | ||
| 984 | else # else (crfun fractional) delta = 1 | ||
| 985 | { | ||
| 986 | $delta = 1; | ||
| 987 | } | ||
| 988 | } | ||
| 989 | |||
| 990 | # The transformation shifts the given delta value to the least significant digit in | ||
| 991 | # the precision of the given crfun value for fractional crfun values. | ||
| 992 | # For example: For crfun = 1.23e-02 & delta = 1 will be transformed to 0.0001 | ||
| 993 | # For crfun = 2.45e-01 & delta = 5 will be transformed to 0.005 | ||
| 994 | # But for integer/whole crfun values (exponent >= 2) the transformation shifts | ||
| 995 | # the delta value not to the real least significant digit but to the least significant | ||
| 996 | # digit in which any difference between two consecutive crfuns can be observed. | ||
| 997 | # For example: For crfun = 1.23e+04 & delta = 2 will be transformed to 200 | ||
| 998 | # For crfun = 2.45e+02 & delta = 5 will be transformed to 5 | ||
| 999 | # For crfun = 1.44e+03 & delta = 1 will be transformed to 10 | ||
| 1000 | # Finally if the delta value is 0 then the transformed delta value is 0 too. | ||
| 1001 | |||
| 1002 | # Transformation: 1e+/-XX * 0.0D | ||
| 1003 | $cr[$k] =~ /(e.+)/; | ||
| 1004 | $trans_delta = "1.0" . $1; | ||
| 1005 | $temp = "0.0" . $delta; | ||
| 1006 | $trans_delta *= $temp; | ||
| 1007 | |||
| 1008 | # for changing the delta value when the exponent of crfun values changes | ||
| 1009 | if(!$thres || $flag) | ||
| 1010 | { | ||
| 1011 | $thres = $trans_delta; | ||
| 1012 | $flag = 0; | ||
| 1013 | # if the delta values changes print to the o/p files | ||
| 1014 | if($measure eq "all") | ||
| 1015 | { | ||
| 1016 | print OP1 "Delta: $thres\n"; | ||
| 1017 | print OP2 "Delta: $thres\n"; | ||
| 1018 | print OP3 "Delta: $thres\n"; | ||
| 1019 | print OP4 "Delta: $thres\n"; | ||
| 1020 | } | ||
| 1021 | elsif($measure eq "pk") | ||
| 1022 | { | ||
| 1023 | print OP1 "Delta: $thres\n"; | ||
| 1024 | print OP2 "Delta: $thres\n"; | ||
| 1025 | print OP3 "Delta: $thres\n"; | ||
| 1026 | } | ||
| 1027 | elsif($measure eq "pk1") | ||
| 1028 | { | ||
| 1029 | print OP1 "Delta: $thres\n"; | ||
| 1030 | } | ||
| 1031 | elsif($measure eq "pk2") | ||
| 1032 | { | ||
| 1033 | print OP2 "Delta: $thres\n"; | ||
| 1034 | } | ||
| 1035 | elsif($measure eq "pk3") | ||
| 1036 | { | ||
| 1037 | print OP3 "Delta: $thres\n"; | ||
| 1038 | } | ||
| 1039 | elsif($measure eq "gap") | ||
| 1040 | { | ||
| 1041 | print OP4 "Delta: $thres\n"; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | } | ||
| 1045 | if($trans_delta != $thres) | ||
| 1046 | { | ||
| 1047 | $flag = 1; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | if($measure eq "all") | ||
| 1051 | { | ||
| 1052 | print OP1 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1053 | print OP2 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1054 | print OP3 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1055 | print OP4 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1056 | } | ||
| 1057 | elsif($measure eq "pk") | ||
| 1058 | { | ||
| 1059 | print OP1 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1060 | print OP2 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1061 | print OP3 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1062 | } | ||
| 1063 | elsif($measure eq "pk1") | ||
| 1064 | { | ||
| 1065 | print OP1 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1066 | } | ||
| 1067 | elsif($measure eq "pk2") | ||
| 1068 | { | ||
| 1069 | print OP2 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1070 | } | ||
| 1071 | elsif($measure eq "pk3") | ||
| 1072 | { | ||
| 1073 | print OP3 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1074 | } | ||
| 1075 | elsif($measure eq "gap") | ||
| 1076 | { | ||
| 1077 | print OP4 "$crfun_name"."[$k] = " . $cr[$k] . " \n"; | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | # accumulator for calculating the mean of crfun values | ||
| 1081 | $mean += $cr[$k]; | ||
| 1082 | |||
| 1083 | # check if new crfun value has improvement over the | ||
| 1084 | # previous crfun value by at least the threshold value | ||
| 1085 | |||
| 1086 | if($crfun eq "e1") | ||
| 1087 | { | ||
| 1088 | if($k > 1 && (($cr[$k-1] - $cr[$k]) <= $thres)) | ||
| 1089 | { | ||
| 1090 | # if the new crfun value is almost comparable to the | ||
| 1091 | # previous crfun values which implies that separating | ||
| 1092 | # the instances into more clusters henceforth | ||
| 1093 | # will yield more or less the same crfun value i.e. | ||
| 1094 | # will not give a significant improvement then stop. | ||
| 1095 | |||
| 1096 | # This assumption significantly reduces the | ||
| 1097 | # clustering requirement of this method. | ||
| 1098 | |||
| 1099 | # remember the last k for further calculations | ||
| 1100 | $last_k = $k; | ||
| 1101 | $break_flag = 1; | ||
| 1102 | } | ||
| 1103 | } | ||
| 1104 | else | ||
| 1105 | { | ||
| 1106 | if($k > 1 && (($cr[$k] - $cr[$k-1]) <= $thres)) | ||
| 1107 | { | ||
| 1108 | # if the new crfun value is almost comparable to the | ||
| 1109 | # previous crfun values which implies that separating | ||
| 1110 | # the instances into more clusters henceforth | ||
| 1111 | # will yield more or less the same crfun value i.e. | ||
| 1112 | # will not give a significant improvement then stop. | ||
| 1113 | |||
| 1114 | # This assumption significantly reduces the | ||
| 1115 | # clustering requirement of this method. | ||
| 1116 | |||
| 1117 | # remember the last k for further calculations | ||
| 1118 | $last_k = $k; | ||
| 1119 | $break_flag = 1; | ||
| 1120 | } | ||
| 1121 | } | ||
| 1122 | } | ||
| 1123 | }while(!$break_flag); | ||
| 1124 | |||
| 1125 | close FPCR; | ||
| 1126 | |||
| 1127 | # quit if NaN value generated for crfun | ||
| 1128 | if($nan_flag) | ||
| 1129 | { | ||
| 1130 | print STDERR "ERROR($0): | ||
| 1131 | $crfun is generating \"NaN\" values for this data. \n"; | ||
| 1132 | exit 1; | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | # -------------Calculations for PK1------------- | ||
| 1136 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk1") | ||
| 1137 | { | ||
| 1138 | # calculate mean_crfun | ||
| 1139 | if($last_k) | ||
| 1140 | { | ||
| 1141 | $mean = $mean/$last_k; | ||
| 1142 | $mean = sprintf($precision_str,$mean); | ||
| 1143 | print OP1 "Mean = $mean\n"; | ||
| 1144 | } | ||
| 1145 | else | ||
| 1146 | { | ||
| 1147 | print STDERR "ERROR($0): | ||
| 1148 | Not enough $crfun values. $crfun values are converging very early for this data. | ||
| 1149 | Try using smaller delta value. \n"; | ||
| 1150 | exit 1; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | # calculate std_deviation_crfun | ||
| 1154 | $stdev = 0; | ||
| 1155 | for($i = 1; $i <= $last_k; $i++) | ||
| 1156 | { | ||
| 1157 | $stdev = $stdev + ($cr[$i] - $mean)**2; | ||
| 1158 | } | ||
| 1159 | $stdev = $stdev/$last_k; | ||
| 1160 | $stdev = sqrt($stdev); | ||
| 1161 | $stdev = sprintf($precision_str,$stdev); | ||
| 1162 | print OP1 "Std Dev = $stdev\n"; | ||
| 1163 | |||
| 1164 | # calculate the confidence interval for 1 standard deviation. | ||
| 1165 | $range1 = $mean-$stdev; | ||
| 1166 | $range1 = sprintf($precision_str,$range1); | ||
| 1167 | $range2 = $mean+$stdev; | ||
| 1168 | $range2 = sprintf($precision_str,$range2); | ||
| 1169 | print OP1 "Confidence Interval: $range1 to $range2\n"; | ||
| 1170 | |||
| 1171 | # print the output to a .dat file too for plots | ||
| 1172 | open(FP,">$prefix.pk1.dat") || die "Error opening $inpfile.dat for writing\n"; | ||
| 1173 | |||
| 1174 | # calculate PK1 values | ||
| 1175 | print OP1 "k : PK1 \n"; | ||
| 1176 | print OP1 "-----------\n"; | ||
| 1177 | if($stdev != 0) | ||
| 1178 | { | ||
| 1179 | $flag = 0; | ||
| 1180 | for($i = 2; $i <= $last_k; $i++) | ||
| 1181 | { | ||
| 1182 | if($stdev) | ||
| 1183 | { | ||
| 1184 | $pk1[$i] = ( $cr[$i] - $mean ) / $stdev; | ||
| 1185 | $pk1[$i] = sprintf($precision_str,$pk1[$i]); | ||
| 1186 | } | ||
| 1187 | else | ||
| 1188 | { | ||
| 1189 | print STDERR "ERROR($0): | ||
| 1190 | The one standard deviation for the crfun values is 0. Quitting to avoid dividing by zero. \n"; | ||
| 1191 | exit 1; | ||
| 1192 | } | ||
| 1193 | |||
| 1194 | print OP1 "$i : $pk1[$i]\n"; | ||
| 1195 | print FP "$i $pk1[$i]\n"; | ||
| 1196 | |||
| 1197 | if($crfun eq "e1") | ||
| 1198 | { | ||
| 1199 | if($pk1[$i] < $thres_pk1 && !$flag) | ||
| 1200 | { | ||
| 1201 | if($i > 2) | ||
| 1202 | { | ||
| 1203 | $pk1_K = $i-1; | ||
| 1204 | $pk1_K_value = $pk1[$i-1]; | ||
| 1205 | } | ||
| 1206 | else | ||
| 1207 | { | ||
| 1208 | $pk1_K = 1; | ||
| 1209 | $pk1_K_value = 0; | ||
| 1210 | } | ||
| 1211 | $flag = 1; | ||
| 1212 | } | ||
| 1213 | } | ||
| 1214 | else | ||
| 1215 | { | ||
| 1216 | if($pk1[$i] > $thres_pk1 && !$flag) | ||
| 1217 | { | ||
| 1218 | if($i > 2) | ||
| 1219 | { | ||
| 1220 | $pk1_K = $i-1; | ||
| 1221 | $pk1_K_value = $pk1[$i-1]; | ||
| 1222 | } | ||
| 1223 | else | ||
| 1224 | { | ||
| 1225 | $pk1_K = 1; | ||
| 1226 | $pk1_K_value = 0; | ||
| 1227 | } | ||
| 1228 | $flag = 1; | ||
| 1229 | } | ||
| 1230 | } | ||
| 1231 | } | ||
| 1232 | } | ||
| 1233 | else | ||
| 1234 | { | ||
| 1235 | for($i = 2; $i <= $last_k; $i++) | ||
| 1236 | { | ||
| 1237 | print OP1 "$i : Infinity \n"; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | $pk1_K = 1; | ||
| 1241 | $pk1_K_value = "Infinity"; | ||
| 1242 | } | ||
| 1243 | close FP; | ||
| 1244 | |||
| 1245 | if(!$flag && !defined $pk1_K) | ||
| 1246 | { | ||
| 1247 | $pk1_K = 1; | ||
| 1248 | $pk1_K_value = 0; | ||
| 1249 | } | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | # -------------Calculations for PK2------------- | ||
| 1253 | # calculate 1st ratio = crfun(k+1)/crfun(k) | ||
| 1254 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk2") | ||
| 1255 | { | ||
| 1256 | $mean_ratio = 0; | ||
| 1257 | for($i = 1; $i < $last_k; $i++) | ||
| 1258 | { | ||
| 1259 | if($crfun eq "e1") | ||
| 1260 | { | ||
| 1261 | if($cr[$i+1]) | ||
| 1262 | { | ||
| 1263 | $ratio_1[$i] = $cr[$i] / $cr[$i+1]; | ||
| 1264 | } | ||
| 1265 | else | ||
| 1266 | { | ||
| 1267 | my $tmp = $i+1; | ||
| 1268 | print STDERR "ERROR($0): | ||
| 1269 | $crfun" . "[$tmp] = 0. Quitting to avoid dividing by zero. \n"; | ||
| 1270 | exit 1; | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | else | ||
| 1274 | { | ||
| 1275 | if($cr[$i]) | ||
| 1276 | { | ||
| 1277 | $ratio_1[$i] = $cr[$i+1] / $cr[$i]; | ||
| 1278 | } | ||
| 1279 | else | ||
| 1280 | { | ||
| 1281 | print STDERR "ERROR($0): | ||
| 1282 | $crfun" . "[$i] = 0. Quitting to avoid dividing by zero. \n"; | ||
| 1283 | exit 1; | ||
| 1284 | } | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | $ratio_1[$i] = sprintf($precision_str,$ratio_1[$i]); | ||
| 1288 | $mean_ratio += $ratio_1[$i]; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | # calculate mean_pk2 | ||
| 1292 | if($last_k>1) | ||
| 1293 | { | ||
| 1294 | $mean_ratio = $mean_ratio/($last_k-1); | ||
| 1295 | $mean_ratio = sprintf($precision_str,$mean_ratio); | ||
| 1296 | print OP2 "Mean = $mean_ratio\n"; | ||
| 1297 | } | ||
| 1298 | else | ||
| 1299 | { | ||
| 1300 | print STDERR "ERROR($0): | ||
| 1301 | Not enough $crfun values. Quitting to avoid dividing by zero. \n"; | ||
| 1302 | exit 1; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | # calculate std_deviation_pk2 | ||
| 1306 | $stdev = 0; | ||
| 1307 | for($i = 1; $i < $last_k; $i++) | ||
| 1308 | { | ||
| 1309 | $stdev = $stdev + ($ratio_1[$i] - $mean_ratio)**2; | ||
| 1310 | } | ||
| 1311 | $stdev = $stdev/($last_k-1); | ||
| 1312 | $stdev = sqrt($stdev); | ||
| 1313 | $stdev = sprintf($precision_str,$stdev); | ||
| 1314 | print OP2 "Std Dev = $stdev\n"; | ||
| 1315 | |||
| 1316 | $range1 = $mean_ratio-$stdev; | ||
| 1317 | $range1 = sprintf($precision_str,$range1); | ||
| 1318 | $range2 = $mean_ratio+$stdev; | ||
| 1319 | $range2 = sprintf($precision_str,$range2); | ||
| 1320 | print OP2 "Confidence Interval: $range1 to $range2\n"; | ||
| 1321 | |||
| 1322 | # print the output to a .dat file too | ||
| 1323 | open(FP,">$prefix.pk2.dat") || die "Error opening <$inpfile.dat> for writing.\n"; | ||
| 1324 | |||
| 1325 | print OP2 "k : PK2 value : In/Out \n"; | ||
| 1326 | print OP2 "------------------------\n"; | ||
| 1327 | |||
| 1328 | $pk2_K = 0; | ||
| 1329 | $pk2_K_value = 999999; | ||
| 1330 | for($i = 1; $i < $last_k; $i++) | ||
| 1331 | { | ||
| 1332 | if($ratio_1[$i] >= $range1 && $ratio_1[$i] <= $range2) | ||
| 1333 | { | ||
| 1334 | print OP2 $i+1 . " : $ratio_1[$i] : 0 \n"; | ||
| 1335 | print FP $i+1 . " $ratio_1[$i] $range1 $range2\n"; | ||
| 1336 | } | ||
| 1337 | else | ||
| 1338 | { | ||
| 1339 | print OP2 $i+1 ." : $ratio_1[$i] : 1 \n"; | ||
| 1340 | print FP $i+1 . " $ratio_1[$i] $range1 $range2\n"; | ||
| 1341 | |||
| 1342 | # logic for predicting K value | ||
| 1343 | if($ratio_1[$i] >= 1) | ||
| 1344 | { | ||
| 1345 | if($ratio_1[$i] < $pk2_K_value) | ||
| 1346 | { | ||
| 1347 | $pk2_K = $i+1; | ||
| 1348 | $pk2_K_value = $ratio_1[$i]; | ||
| 1349 | } | ||
| 1350 | } | ||
| 1351 | } | ||
| 1352 | } | ||
| 1353 | close FP; | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | # -------------Calculations for PK3------------- | ||
| 1357 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk3") | ||
| 1358 | { | ||
| 1359 | if($last_k <= 2) | ||
| 1360 | { | ||
| 1361 | print STDERR "ERROR($0): | ||
| 1362 | Not enough $crfun values to compute pk3 measure.\n"; | ||
| 1363 | exit 1; | ||
| 1364 | } | ||
| 1365 | |||
| 1366 | # calculate dice = 2 * crfun(k)/(crfun(k-1) + crfun(k+1)) | ||
| 1367 | $mean_ratio = 0; | ||
| 1368 | for($i = 2; $i < $last_k; $i++) | ||
| 1369 | { | ||
| 1370 | if(($cr[$i-1]+$cr[$i+1]) != 0) | ||
| 1371 | { | ||
| 1372 | $dice[$i] = (2 * $cr[$i]) / ($cr[$i-1] + $cr[$i+1]); | ||
| 1373 | $dice[$i] = sprintf($precision_str,$dice[$i]); | ||
| 1374 | $mean_ratio += $dice[$i]; | ||
| 1375 | } | ||
| 1376 | else | ||
| 1377 | { | ||
| 1378 | my $tmp1 = $i+1; | ||
| 1379 | my $tmp2 = $i-1; | ||
| 1380 | print STDERR "ERROR($0): | ||
| 1381 | Quitting to avoid dividing by zero. crfun[$tmp1] + crfun[$tmp2] ($cr[$i-1] + $cr[$i+1]) resulting to 0 \n"; | ||
| 1382 | exit 1; | ||
| 1383 | } | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | # calculate mean_pk3 | ||
| 1387 | if($last_k>2) | ||
| 1388 | { | ||
| 1389 | $mean_ratio = $mean_ratio/($last_k-2); | ||
| 1390 | $mean_ratio = sprintf($precision_str,$mean_ratio); | ||
| 1391 | print OP3 "Mean = $mean_ratio\n"; | ||
| 1392 | } | ||
| 1393 | else | ||
| 1394 | { | ||
| 1395 | print STDERR "ERROR($0): | ||
| 1396 | Not enough $crfun values. Quitting to avoid dividing by zero. \n"; | ||
| 1397 | exit 1; | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | # calculate std_deviation_pk3 | ||
| 1401 | $stdev = 0; | ||
| 1402 | for($i = 2; $i < $last_k; $i++) | ||
| 1403 | { | ||
| 1404 | $stdev = $stdev + ($dice[$i] - $mean_ratio)**2; | ||
| 1405 | } | ||
| 1406 | $stdev = $stdev/($last_k-2); | ||
| 1407 | $stdev = sqrt($stdev); | ||
| 1408 | $stdev = sprintf($precision_str,$stdev); | ||
| 1409 | print OP3 "Std Dev = $stdev\n"; | ||
| 1410 | |||
| 1411 | $range1 = $mean_ratio-$stdev; | ||
| 1412 | $range1 = sprintf($precision_str,$range1); | ||
| 1413 | $range2 = $mean_ratio+$stdev; | ||
| 1414 | $range2 = sprintf($precision_str,$range2); | ||
| 1415 | print OP3 "Confidence Interval: $range1 to $range2\n"; | ||
| 1416 | |||
| 1417 | # print the output to a .dat file too | ||
| 1418 | open(FP,">$prefix.pk3.dat") || die "Error opening <$inpfile.dat> for writing\n"; | ||
| 1419 | |||
| 1420 | print OP3 "k : PK3 Value : In/Out \n"; | ||
| 1421 | print OP3 "------------------------\n"; | ||
| 1422 | |||
| 1423 | $pk3_K = 0; | ||
| 1424 | $pk3_K_value = 999999; | ||
| 1425 | for($i = 2; $i < $last_k; $i++) | ||
| 1426 | { | ||
| 1427 | if($dice[$i] >= $range1 && $dice[$i] <= $range2) | ||
| 1428 | { | ||
| 1429 | print OP3 "$i : $dice[$i] : 0 \n"; | ||
| 1430 | print FP "$i $dice[$i] $range1 $range2\n"; | ||
| 1431 | } | ||
| 1432 | else | ||
| 1433 | { | ||
| 1434 | print OP3 "$i : $dice[$i] : 1 \n"; | ||
| 1435 | print FP "$i $dice[$i] $range1 $range2\n"; | ||
| 1436 | |||
| 1437 | # logic for predicting K value | ||
| 1438 | if($crfun eq "e1") | ||
| 1439 | { | ||
| 1440 | if($dice[$i] < 1) | ||
| 1441 | { | ||
| 1442 | if($dice[$i] < $pk3_K_value) | ||
| 1443 | { | ||
| 1444 | $pk3_K = $i; | ||
| 1445 | $pk3_K_value = $dice[$i]; | ||
| 1446 | } | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | else | ||
| 1450 | { | ||
| 1451 | if($dice[$i] >= 1) | ||
| 1452 | { | ||
| 1453 | if($dice[$i] < $pk3_K_value) | ||
| 1454 | { | ||
| 1455 | $pk3_K = $i; | ||
| 1456 | $pk3_K_value = $dice[$i]; | ||
| 1457 | } | ||
| 1458 | } | ||
| 1459 | } | ||
| 1460 | } | ||
| 1461 | } | ||
| 1462 | close FP; | ||
| 1463 | |||
| 1464 | } | ||
| 1465 | |||
| 1466 | # -------------Calculations for Gap------------- | ||
| 1467 | |||
| 1468 | if($measure eq "all" || $measure eq "gap") | ||
| 1469 | { | ||
| 1470 | #~~~~~ Step 1: Find the crfun value per k for the observed data ~~~~~~~~ | ||
| 1471 | # This step already done. The crfun values in @cr | ||
| 1472 | |||
| 1473 | #~~~~~~~~~~~~~~~~~~~~ Step 2: Generation of Reference Model ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 1474 | |||
| 1475 | my @exp = (); # holds the crfun values for various k for the reference data. | ||
| 1476 | |||
| 1477 | if($typeref eq "rep") #replicates | ||
| 1478 | { | ||
| 1479 | # Generate the Reference Distribution | ||
| 1480 | my @refmat = (); | ||
| 1481 | if(!$seed) | ||
| 1482 | { | ||
| 1483 | @refmat = generateMatrix(\@rmar, \@cmar, $precision); | ||
| 1484 | } | ||
| 1485 | else | ||
| 1486 | { | ||
| 1487 | @refmat = generateMatrix(\@rmar, \@cmar, $precision, $seed); | ||
| 1488 | } | ||
| 1489 | |||
| 1490 | # Perform Monte Carlo Sampling B times from the generated distribution | ||
| 1491 | for(my $i = 1; $i <= $B; $i++) | ||
| 1492 | { | ||
| 1493 | my @replicate = (); | ||
| 1494 | |||
| 1495 | for(my $j = 0; $j < $rcnt; $j++) | ||
| 1496 | { | ||
| 1497 | my $rand = int(rand($rcnt)); | ||
| 1498 | push @replicate, $refmat[$rand]; | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | # compute total number of elements in @replicate | ||
| 1502 | my $totalElem = 0; | ||
| 1503 | for(my $j=0; $j<$rcnt; $j++) | ||
| 1504 | { | ||
| 1505 | for(my $k=0; $k<$ccnt; $k++) | ||
| 1506 | { | ||
| 1507 | if(defined $replicate[$j]->[$k]) | ||
| 1508 | { | ||
| 1509 | $totalElem++; | ||
| 1510 | } | ||
| 1511 | } | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | # 1. Cluster the generated replicate | ||
| 1515 | # 2. Calculate the crfun value | ||
| 1516 | |||
| 1517 | # Write the matrix to a temporary file | ||
| 1518 | my $filename = "$prefix.tmp.ref." . time() . $i; | ||
| 1519 | open(RO,">$filename") || die "Error opening temporary file <$filename> in write mode.\n"; | ||
| 1520 | |||
| 1521 | if($space eq "vector") | ||
| 1522 | { | ||
| 1523 | print RO $rcnt . " " . $ccnt . " " . $totalElem . "\n"; | ||
| 1524 | } | ||
| 1525 | else | ||
| 1526 | { | ||
| 1527 | print RO $rcnt . " " . $totalElem . "\n"; | ||
| 1528 | } | ||
| 1529 | |||
| 1530 | for(my $j=0; $j<$rcnt; $j++) | ||
| 1531 | { | ||
| 1532 | for(my $k=0; $k<$ccnt; $k++) | ||
| 1533 | { | ||
| 1534 | if(defined $replicate[$j]->[$k]) | ||
| 1535 | { | ||
| 1536 | my $tmp_col = $k+1; | ||
| 1537 | print RO "$tmp_col $replicate[$j]->[$k] "; | ||
| 1538 | } | ||
| 1539 | } | ||
| 1540 | print RO "\n"; | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | close RO; | ||
| 1544 | |||
| 1545 | for(my $k=1 ; $k<=$last_k ; $k++) | ||
| 1546 | { | ||
| 1547 | # cluster the replicate into 1..K clusters | ||
| 1548 | my $out_filename = "$prefix.tmp.ref.op." . $k . "." . time(); | ||
| 1549 | |||
| 1550 | if($space eq "vector") | ||
| 1551 | { | ||
| 1552 | my $status = system("vcluster $cluster_string $filename $k > $cwd/$out_filename"); | ||
| 1553 | die "Error running vcluster $cluster_string $filename $k \n" unless $status==0; | ||
| 1554 | } | ||
| 1555 | else | ||
| 1556 | { | ||
| 1557 | my $status = system("scluster $cluster_string $filename $k > $cwd/$out_filename"); | ||
| 1558 | die "Error running scluster $cluster_string $filename $k \n" unless $status==0; | ||
| 1559 | } | ||
| 1560 | |||
| 1561 | # read the clustering output file | ||
| 1562 | open(CO,"<$cwd/$out_filename") || die "Error opening clustering output file <$out_filename>\n"; | ||
| 1563 | |||
| 1564 | # read the complete file in one command | ||
| 1565 | my $temp = $/; | ||
| 1566 | $/ = undef; | ||
| 1567 | my $str = <CO>; | ||
| 1568 | $/ = $temp; | ||
| 1569 | close FP; | ||
| 1570 | |||
| 1571 | # read the crfun value | ||
| 1572 | $str =~ /\-way clustering: \[.*=(.*?)\]/; | ||
| 1573 | $exp[$i][$k] = $1; | ||
| 1574 | |||
| 1575 | unlink "$out_filename","$filename.clustering.$k"; | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | unlink "$filename", "$filename.tree"; | ||
| 1579 | } # for $B | ||
| 1580 | } | ||
| 1581 | else # typeref = ref | ||
| 1582 | { | ||
| 1583 | # Generate B reference distributions | ||
| 1584 | for(my $i = 1; $i <= $B; $i++) | ||
| 1585 | { | ||
| 1586 | my @refmat = (); | ||
| 1587 | |||
| 1588 | if(!$seed) | ||
| 1589 | { | ||
| 1590 | @refmat = generateMatrix(\@rmar, \@cmar, $precision); | ||
| 1591 | } | ||
| 1592 | else | ||
| 1593 | { | ||
| 1594 | @refmat = generateMatrix(\@rmar, \@cmar, $precision, $seed); | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | # compute total number of elements in @replicate | ||
| 1598 | my $totalElem = 0; | ||
| 1599 | for(my $j=0; $j<$rcnt; $j++) | ||
| 1600 | { | ||
| 1601 | for(my $k=0; $k<$ccnt; $k++) | ||
| 1602 | { | ||
| 1603 | if(defined $refmat[$j]->[$k]) | ||
| 1604 | { | ||
| 1605 | $totalElem++; | ||
| 1606 | } | ||
| 1607 | } | ||
| 1608 | } | ||
| 1609 | |||
| 1610 | # 1. Cluster the generated reference distributions | ||
| 1611 | # 2. Calculate the crfun | ||
| 1612 | |||
| 1613 | # Write the matrix to a temporary file | ||
| 1614 | my $filename = "$prefix.tmp.ref." . time() . $i; | ||
| 1615 | open(RO,">$filename") || die "Error opening temporary file <$filename> in write mode.\n"; | ||
| 1616 | |||
| 1617 | # print the dimensions of the matrix to the file. | ||
| 1618 | if($space eq "vector") | ||
| 1619 | { | ||
| 1620 | print RO $rcnt . " " . $ccnt . " " . $totalElem . "\n"; | ||
| 1621 | } | ||
| 1622 | else | ||
| 1623 | { | ||
| 1624 | print RO $rcnt . " " . $totalElem . "\n"; | ||
| 1625 | } | ||
| 1626 | |||
| 1627 | for(my $j=0; $j<$rcnt; $j++) | ||
| 1628 | { | ||
| 1629 | for(my $k=0; $k<$ccnt; $k++) | ||
| 1630 | { | ||
| 1631 | if(defined $refmat[$j]->[$k]) | ||
| 1632 | { | ||
| 1633 | my $tmp_col = $k+1; | ||
| 1634 | print RO "$tmp_col $refmat[$j]->[$k] "; | ||
| 1635 | } | ||
| 1636 | } | ||
| 1637 | print RO "\n"; | ||
| 1638 | } | ||
| 1639 | |||
| 1640 | close RO; | ||
| 1641 | |||
| 1642 | for(my $k=1 ; $k<=$last_k ; $k++) | ||
| 1643 | { | ||
| 1644 | # cluster the generated reference into 1..K clusters | ||
| 1645 | my $out_filename = "$prefix.tmp.ref.op." . $k . "." . time(); | ||
| 1646 | |||
| 1647 | if($space eq "vector") | ||
| 1648 | { | ||
| 1649 | my $status = system("vcluster $cluster_string $filename $k > $cwd/$out_filename"); | ||
| 1650 | die "Error running vcluster $cluster_string $filename $k \n" unless $status==0; | ||
| 1651 | } | ||
| 1652 | else | ||
| 1653 | { | ||
| 1654 | my $status = system("scluster $cluster_string $filename $k > $cwd/$out_filename"); | ||
| 1655 | die "Error running scluster $cluster_string $filename $k \n" unless $status==0; | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | # read the clustering output file | ||
| 1659 | open(CO,"<$cwd/$out_filename") || die "Error opening clustering output file <$out_filename>\n"; | ||
| 1660 | |||
| 1661 | # read the complete file in one command | ||
| 1662 | my $temp = $/; | ||
| 1663 | $/ = undef; | ||
| 1664 | my $str = <CO>; | ||
| 1665 | $/ = $temp; | ||
| 1666 | close FP; | ||
| 1667 | |||
| 1668 | # read the crfun value | ||
| 1669 | $str =~ /\-way clustering: \[.*=(.*?)\]/; | ||
| 1670 | my $tmp_crfun = $1; | ||
| 1671 | |||
| 1672 | # check and quit if NaN value generated | ||
| 1673 | if($tmp_crfun =~ /nan/) | ||
| 1674 | { | ||
| 1675 | print STDERR "ERROR($0): | ||
| 1676 | $crfun is generating \"NaN\" values for the expected data. \n"; | ||
| 1677 | exit 1; | ||
| 1678 | } | ||
| 1679 | else | ||
| 1680 | { | ||
| 1681 | $exp[$i][$k] = $1; | ||
| 1682 | } | ||
| 1683 | |||
| 1684 | unlink "$out_filename","$filename.clustering.$k"; | ||
| 1685 | |||
| 1686 | } # for $last_k | ||
| 1687 | |||
| 1688 | unlink "$filename", "$filename.tree"; | ||
| 1689 | } # for $B | ||
| 1690 | } #else typeref = ref | ||
| 1691 | |||
| 1692 | # For plots | ||
| 1693 | # Expected | ||
| 1694 | open(G3a,">$prefix.exp.dat") || die "Error creating file <$prefix.exp.dat>\n"; | ||
| 1695 | |||
| 1696 | my @sum = (); | ||
| 1697 | my @gap = (); | ||
| 1698 | |||
| 1699 | # Calculate average over the B crfun(exp) values | ||
| 1700 | for(my $i = 1; $i <= $last_k; $i++) | ||
| 1701 | { | ||
| 1702 | for(my $j = 1; $j <= $B; $j++) | ||
| 1703 | { | ||
| 1704 | $sum[$i] += $exp[$j][$i]; | ||
| 1705 | } | ||
| 1706 | |||
| 1707 | $sum[$i] = sprintf($precision_str,$sum[$i]/$B) + 0; | ||
| 1708 | |||
| 1709 | # Calculate Gap(k) = crfun(obs) - avg_crfun(exp) | ||
| 1710 | $gap[$i] = sprintf($precision_str, $cr[$i] - $sum[$i]) + 0; | ||
| 1711 | |||
| 1712 | print G3a "$i $sum[$i]\n"; | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | close G3a; | ||
| 1716 | |||
| 1717 | #~~~~~~~~~~~~~~~~~~~~ Step 3: Calculation of Standard Deviation ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 1718 | |||
| 1719 | my @sd = (); | ||
| 1720 | my @s = (); | ||
| 1721 | my $lower = 0; | ||
| 1722 | my $upper = 0; | ||
| 1723 | |||
| 1724 | # For plot | ||
| 1725 | open(G4,">$prefix.gap.dat") || die "Error creating file <$prefix.gap.dat>\n"; | ||
| 1726 | |||
| 1727 | # Calculate standard deviation sd for crfun(exp) | ||
| 1728 | for(my $i = 1; $i <= $last_k; $i++) | ||
| 1729 | { | ||
| 1730 | for(my $j = 1; $j <= $B; $j++) | ||
| 1731 | { | ||
| 1732 | $sd[$i] += ($exp[$j][$i] - $sum[$i])**2; | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | $sd[$i] = sprintf($precision_str,sqrt($sd[$i]/$B)) + 0; | ||
| 1736 | |||
| 1737 | # Calculate the modified standard deviation to account for | ||
| 1738 | # simulation error. | ||
| 1739 | $s[$i] = $sd[$i] * sqrt(1 + 1/$B); | ||
| 1740 | $s[$i] = sprintf($precision_str,$s[$i]) + 0; | ||
| 1741 | |||
| 1742 | $lower = $gap[$i] - $s[$i]; | ||
| 1743 | $upper = $gap[$i] + $s[$i]; | ||
| 1744 | print G4 "$i $gap[$i] $lower $upper\n"; | ||
| 1745 | } | ||
| 1746 | |||
| 1747 | close G4; | ||
| 1748 | |||
| 1749 | $gap_K = 1; | ||
| 1750 | |||
| 1751 | # Find the optimal #of k | ||
| 1752 | # for e1 | ||
| 1753 | if($crfun eq "e1") | ||
| 1754 | { | ||
| 1755 | for(my $i = 2; $i < $last_k; $i++) | ||
| 1756 | { | ||
| 1757 | if($gap[$i] <= ($gap[$i+1] + $s[$i+1])) | ||
| 1758 | { | ||
| 1759 | $gap_K = $i; | ||
| 1760 | # print "$gap_K\n"; | ||
| 1761 | last; | ||
| 1762 | } | ||
| 1763 | } | ||
| 1764 | } # e1 - end | ||
| 1765 | elsif($crfun eq "i2" || $crfun eq "i1") # for i2 | ||
| 1766 | { | ||
| 1767 | for(my $i = 1; $i < $last_k; $i++) | ||
| 1768 | { | ||
| 1769 | if($gap[$i] >= $gap[$i+1] - $s[$i+1]) | ||
| 1770 | { | ||
| 1771 | $gap_K = $i; | ||
| 1772 | # print "$gap_K\n"; | ||
| 1773 | last; | ||
| 1774 | } | ||
| 1775 | } | ||
| 1776 | } # i2 - end | ||
| 1777 | elsif($crfun eq "h2" || $crfun eq "h1") # for h2 | ||
| 1778 | { | ||
| 1779 | for(my $i = 2; $i < $last_k; $i++) | ||
| 1780 | { | ||
| 1781 | if($gap[$i] < 0) | ||
| 1782 | { | ||
| 1783 | next; | ||
| 1784 | } | ||
| 1785 | |||
| 1786 | if($gap[$i] >= $gap[$i+1] - $s[$i+1]) | ||
| 1787 | { | ||
| 1788 | $gap_K = $i; | ||
| 1789 | # print "$gap_K\n"; | ||
| 1790 | last; | ||
| 1791 | } | ||
| 1792 | } | ||
| 1793 | } # h2 - end | ||
| 1794 | |||
| 1795 | # Printing to the log file. | ||
| 1796 | open(LO,">$prefix.gap.log") || die "Error opening the Log file <$prefix.gap.log> in write mode.\n"; | ||
| 1797 | |||
| 1798 | # Print the confidence intervals | ||
| 1799 | my $alpha = 0; | ||
| 1800 | my $low_conf_int = 0; | ||
| 1801 | my $upp_conf_int = 0; | ||
| 1802 | my @tmp = (); | ||
| 1803 | |||
| 1804 | printf LO "%3s %10s %10s %10s %10s %10s %30s\n", "K", "Gap(k)", "Obs($crfun(k))", "Exp($crfun(k))", "sd(k)", "s(k)", "$percentage% Confidence Intervals"; | ||
| 1805 | printf LO "-" x 95 ."\n"; | ||
| 1806 | for(my $i = 1; $i <= $last_k; $i++) | ||
| 1807 | { | ||
| 1808 | # Calculate a from 100(1-2a) = % | ||
| 1809 | $alpha = (1 - $percentage/100)/2; | ||
| 1810 | |||
| 1811 | for(my $j = 1; $j <= $B; $j++) | ||
| 1812 | { | ||
| 1813 | $tmp[$j-1] = $exp[$j][$i]; | ||
| 1814 | } | ||
| 1815 | # sort in the numeric ascending order | ||
| 1816 | @tmp = sort {$a <=> $b} (@tmp); | ||
| 1817 | |||
| 1818 | # Calculate lower bound | ||
| 1819 | $low_conf_int = ($tmp[floor($B*$alpha)-1] + $tmp[ceil($B*$alpha)-1])/2; | ||
| 1820 | |||
| 1821 | # Calculate upper bound | ||
| 1822 | $upp_conf_int = ($tmp[floor($B*(1-$alpha))-1] + $tmp[ceil($B*(1-$alpha))-1])/2; | ||
| 1823 | |||
| 1824 | printf LO "%3d %10.4f %10.4f %10.4f %10.4f %10.4f %30s\n", $i, $gap[$i], $cr[$i], $sum[$i], $sd[$i], $s[$i], "$low_conf_int - $upp_conf_int"; | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | print LO "\nIndividual Exp($crfun(k)) values:\n"; | ||
| 1828 | for(my $i = 1; $i <= $last_k; $i++) | ||
| 1829 | { | ||
| 1830 | print LO "K=$i\n"; | ||
| 1831 | printf LO "%3s %10s\n", "B", "crfun"; | ||
| 1832 | printf LO "-" x 15 . "\n"; | ||
| 1833 | for(my $j = 1; $j <= $B; $j++) | ||
| 1834 | { | ||
| 1835 | $tmp[$j-1] = $exp[$j][$i]; | ||
| 1836 | printf LO "%3s %10s\n", "$j", "$exp[$j][$i]"; | ||
| 1837 | } | ||
| 1838 | print LO "\n"; | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | close LO; | ||
| 1842 | } | ||
| 1843 | |||
| 1844 | # print the predicted K values | ||
| 1845 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk1") | ||
| 1846 | { | ||
| 1847 | print "$pk1_K\n"; | ||
| 1848 | print OP1 "PK1 predicted k = $pk1_K (PK1[$pk1_K] = $pk1_K_value)\n"; | ||
| 1849 | close OP1; | ||
| 1850 | } | ||
| 1851 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk2") | ||
| 1852 | { | ||
| 1853 | if(!$pk2_K) | ||
| 1854 | { | ||
| 1855 | print "1\n"; | ||
| 1856 | print OP2 "PK2 predicted k = 1 (PK2[1] = 0)\n"; | ||
| 1857 | } | ||
| 1858 | else | ||
| 1859 | { | ||
| 1860 | print "$pk2_K\n"; | ||
| 1861 | print OP2 "PK2 predicted k = $pk2_K (PK2[$pk2_K] = $pk2_K_value)\n"; | ||
| 1862 | } | ||
| 1863 | close OP2; | ||
| 1864 | } | ||
| 1865 | if($measure eq "all" || $measure eq "pk" || $measure eq "pk3") | ||
| 1866 | { | ||
| 1867 | if(!$pk3_K) | ||
| 1868 | { | ||
| 1869 | print "1\n"; | ||
| 1870 | print OP3 "PK3 predicted k = 1 (PK3[1] = 0)\n"; | ||
| 1871 | } | ||
| 1872 | else | ||
| 1873 | { | ||
| 1874 | print "$pk3_K\n"; | ||
| 1875 | print OP3 "PK3 predicted k = $pk3_K (PK3[$pk3_K] = $pk3_K_value)\n"; | ||
| 1876 | } | ||
| 1877 | close OP3; | ||
| 1878 | } | ||
| 1879 | if($measure eq "all" || $measure eq "gap") | ||
| 1880 | { | ||
| 1881 | print "$gap_K\n"; | ||
| 1882 | print OP4 "Adapted Gap Statistic predicted k = $gap_K\n"; | ||
| 1883 | close OP4; | ||
| 1884 | } | ||
| 1885 | |||
| 1886 | exit 0; | ||
| 1887 | |||
| 1888 | #----------------------------------------------------------------------------- | ||
| 1889 | #show minimal usage message | ||
| 1890 | sub showminimal() | ||
| 1891 | { | ||
| 1892 | print "Usage: clusterstopping.pl [OPTIONS] INPUTFILE"; | ||
| 1893 | print "\nTYPE clusterstopping.pl --help for help\n"; | ||
| 1894 | } | ||
| 1895 | |||
| 1896 | #----------------------------------------------------------------------------- | ||
| 1897 | #show help | ||
| 1898 | sub showhelp() | ||
| 1899 | |||
| 1900 | { | ||
| 1901 | print "Usage: clusterstopping.pl [OPTIONS] INPUTFILE | ||
| 1902 | |||
| 1903 | INPUTFILE | ||
| 1904 | |||
| 1905 | Matrix file containing either: | ||
| 1906 | * context vectors (in dense or sparse format) | ||
| 1907 | * similarity values between contexts | ||
| 1908 | |||
| 1909 | Optional Arguments: | ||
| 1910 | |||
| 1911 | --prefix PRE | ||
| 1912 | |||
| 1913 | Specify a prefix to be used for the output filenames. | ||
| 1914 | |||
| 1915 | If prefix is not specified then prefix is created by concatenating | ||
| 1916 | time stamp to the string \"expr\". | ||
| 1917 | |||
| 1918 | --measure MSR | ||
| 1919 | |||
| 1920 | Specify the cluster stopping measure to be used. | ||
| 1921 | |||
| 1922 | (Further details about the measures given below under the Section | ||
| 1923 | \"DETAILS ABOUT THE CLUSTER-STOPPING MEASURES\") | ||
| 1924 | |||
| 1925 | The possible option values: | ||
| 1926 | pk1 - PK1 measure | ||
| 1927 | pk2 - PK2 measure | ||
| 1928 | pk3 - PK3 measure [Default] | ||
| 1929 | gap - Adapted Gap Statistic | ||
| 1930 | pk - All the three PK measures | ||
| 1931 | all - All the four measures - PK1, PK2, PK3 and Gap | ||
| 1932 | |||
| 1933 | --space SP | ||
| 1934 | |||
| 1935 | Specify if the clustering should be performed in vector or similarity space. | ||
| 1936 | |||
| 1937 | The possible option values: | ||
| 1938 | vector [Default] | ||
| 1939 | similarity | ||
| 1940 | |||
| 1941 | --delta INT | ||
| 1942 | |||
| 1943 | NOTE: Delta value can only be a positive integer value. | ||
| 1944 | |||
| 1945 | Specify 0 to stop the iterating clustering process when two | ||
| 1946 | consecutive crfun values are exactly equal. This is the | ||
| 1947 | default setting when the crfun values are integer/whole numbers. | ||
| 1948 | |||
| 1949 | Specify non-zero positive integer to stop the iterating | ||
| 1950 | clustering process when the diffference between two consecutive | ||
| 1951 | crfun values is less than or equal to this value. However, note | ||
| 1952 | that the integer value specified is internally shifted to capture | ||
| 1953 | the difference in the least significant digit of the crfun values | ||
| 1954 | when these crfun values are fractional. | ||
| 1955 | For example: | ||
| 1956 | For crfun = 1.23e-02 & delta = 1 will be transformed to 0.0001 | ||
| 1957 | For crfun = 2.45e-01 & delta = 5 will be transformed to 0.005 | ||
| 1958 | The default delta value when the crfun values are fractional is 1. | ||
| 1959 | |||
| 1960 | However if the crfun values are integer/whole numbers (exponent >= 2) | ||
| 1961 | then the specified delta value is internally shifted only until the | ||
| 1962 | least significant digit in the scientific notation. | ||
| 1963 | For example: | ||
| 1964 | For crfun = 1.23e+04 & delta = 2 will be transformed to 200 | ||
| 1965 | For crfun = 2.45e+02 & delta = 5 will be transformed to 5 | ||
| 1966 | For crfun = 1.44e+03 & delta = 1 will be transformed to 10 | ||
| 1967 | |||
| 1968 | --clmethod CL | ||
| 1969 | |||
| 1970 | Specifies the clustering method. | ||
| 1971 | |||
| 1972 | The possible option values: | ||
| 1973 | rb - Repeated Bisections [Default] | ||
| 1974 | rbr - Repeated Bisections for by k-way refinement | ||
| 1975 | direct - Direct k-way clustering | ||
| 1976 | agglo - Agglomerative clustering | ||
| 1977 | bagglo - Partitional biased Agglomerative clustering [Only in vector space] | ||
| 1978 | |||
| 1979 | For large amount of data, 'rb', 'rbr' or 'direct' are recommended. | ||
| 1980 | |||
| 1981 | --crfun CR | ||
| 1982 | |||
| 1983 | Selects the criteria function for Clustering. The meanings of these criteria | ||
| 1984 | functions are explained in Cluto's manual. | ||
| 1985 | |||
| 1986 | The possible option values: | ||
| 1987 | i1 - I1 Criterion function | ||
| 1988 | i2 - I2 Criterion function [Default] | ||
| 1989 | h1 - H1 Criterion function | ||
| 1990 | h2 - H2 Criterion function | ||
| 1991 | e1 - E1 Criterion function | ||
| 1992 | |||
| 1993 | --sim SIM | ||
| 1994 | |||
| 1995 | Specifies the similarity measure to be used | ||
| 1996 | |||
| 1997 | The possible option values: | ||
| 1998 | cos - Cosine [Default] | ||
| 1999 | corr - Correlation Coefficient | ||
| 2000 | |||
| 2001 | NOTE: This option can be used only in vector space. | ||
| 2002 | |||
| 2003 | --rowmodel RMOD | ||
| 2004 | |||
| 2005 | The option is used to specify the model to be used to scale every | ||
| 2006 | column of each row. (For further details please refer Cluto manual) | ||
| 2007 | |||
| 2008 | The possible option values: | ||
| 2009 | none - no scaling is performed [Default] | ||
| 2010 | maxtf - post scaling the values are between 0.5 and 1.0 | ||
| 2011 | sqrt - square-root of actual values | ||
| 2012 | log - log of actual values | ||
| 2013 | |||
| 2014 | --colmodel CMOD | ||
| 2015 | |||
| 2016 | The option is used to specify the model to be used to (globally) scale each | ||
| 2017 | column across all rows. (For further details please refer Cluto manual) | ||
| 2018 | |||
| 2019 | The possible option values: | ||
| 2020 | none - no scaling is performed [Default] | ||
| 2021 | idf - scaling according to inverse-document-frequency | ||
| 2022 | |||
| 2023 | --threspk1 NUM | ||
| 2024 | |||
| 2025 | The threshold value that should be used by the PK1 measure to predict the | ||
| 2026 | k value. | ||
| 2027 | |||
| 2028 | Default = -0.7 | ||
| 2029 | |||
| 2030 | --precision NUM | ||
| 2031 | |||
| 2032 | Specifies the precision to be used. | ||
| 2033 | |||
| 2034 | Default: 4 | ||
| 2035 | |||
| 2036 | Adapted Gap Statistic related options: | ||
| 2037 | |||
| 2038 | --B NUM | ||
| 2039 | |||
| 2040 | The number of replicates/references to be generated. | ||
| 2041 | |||
| 2042 | Default: 1 | ||
| 2043 | |||
| 2044 | --typeref TYP | ||
| 2045 | |||
| 2046 | Specifies whether to generate B replicates from a reference or to generate | ||
| 2047 | B references. | ||
| 2048 | |||
| 2049 | The possible option values: | ||
| 2050 | rep - replicates [Default] | ||
| 2051 | ref - references | ||
| 2052 | |||
| 2053 | --seed NUM | ||
| 2054 | |||
| 2055 | The seed to be used with the random number generator. | ||
| 2056 | Default: No seed is set. | ||
| 2057 | |||
| 2058 | --percentage NUM | ||
| 2059 | |||
| 2060 | Specifies the percentage confidence to be reported in the log file. | ||
| 2061 | Since Statistics::Gap uses parametric bootstrap method for reference | ||
| 2062 | distribution generation, it is critical to understand the interval | ||
| 2063 | around the sample mean that could contain the population (\"true\") | ||
| 2064 | mean and with what certainty. | ||
| 2065 | |||
| 2066 | Default: 90 | ||
| 2067 | |||
| 2068 | Other Options : | ||
| 2069 | |||
| 2070 | --help | ||
| 2071 | |||
| 2072 | Displays the quick summary of program options. | ||
| 2073 | |||
| 2074 | --version | ||
| 2075 | |||
| 2076 | Displays the version information. | ||
| 2077 | |||
| 2078 | --verbose | ||
| 2079 | |||
| 2080 | Displays to STDERR the current program status. | ||
| 2081 | |||
| 2082 | \n"; | ||
| 2083 | } | ||
| 2084 | |||
| 2085 | #------------------------------------------------------------------------------ | ||
| 2086 | #version information | ||
| 2087 | sub showversion() | ||
| 2088 | { | ||
| 2089 | print '$Id: clusterstopping.pl,v 1.33 2008/03/30 04:51:10 tpederse Exp $'; | ||
| 2090 | print "\nPredict the optimal number of clusters\n"; | ||
| 2091 | # print "clusterstopping.pl - Version 0.04\n"; | ||
| 2092 | # print "Cluster Stopping program.\n"; | ||
| 2093 | # print "Copyright (c) 2006-2008, Ted Pedersen, Anagha Kulkarni.\n"; | ||
| 2094 | # print "Date of Last Update: 07/29/2006\n"; | ||
| 2095 | } | ||
| 2096 | |||
| 2097 | ############################################################################# |