Blame view
tools/sctk-2.4.10/src/sclite/net_dp.c
33.8 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 |
/*******************************************************************/ /* File: net_dp.c */ /* Desc: Dynamic programming alignment routines for networks. */ /* */ /* Revisions: */ /* 960319 - JGF - Changed Network_dp_align to optionally */ /* output NULLS. */ /* 961009 - JGF - added an option to output a Network of the */ /* aligned networks. */ /*******************************************************************/ #include "sctk.h" static NET_ALIGN *net_ali = (NET_ALIGN *)0; PATH *extract_best_NET_ALIGN_result(NET_ALIGN *, NETWORK *, NETWORK *,float wwd(void *, void *, int (*cmp)(void *, void *))); void find_minimal_paths(NET_ALIGN *, int, int, int *,float wwd(void *, void *, int (*cmp)(void *, void *)),NETWORK *, PATH *, int *); void create_alinged_NETWORK(NET_ALIGN *, NETWORK *, NETWORK *, void *, int); void calc_minimum_insert(NET_ALIGN *, int, int, float *, int *); void calc_minimum_delete(NET_ALIGN *, int, int, float *, int *); void calc_minimum_substi(NET_ALIGN *, int, int, float *, int *, int *); static int setup_NET_ALIGN(NET_ALIGN **,NETWORK *,NETWORK *); static void destroy_NET_ALIGN(NET_ALIGN **net_ali); PATH *extract_NET_ALIGN_result(NET_ALIGN *,NETWORK *,NETWORK *, int); static int add_null_network_heads(NETWORK *, char *); static void calculate_margin_cells(NET_ALIGN *, NETWORK *net_a,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *))); static void calculate_internal_cells(NET_ALIGN *, NETWORK *,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *))); int find_arcset_id(ARCSET *arcset, ARC *arc, int from){ int a; ARC **arclist; if (from == arcset->num) from --; for (a=from,arclist = (arcset->arcs) + a; a>=0; a--, arclist--){ if (*arclist == arc) return(a); } for (a=from+1; a<arcset->num; a++){ if (arcset->arcs[a] == arc){ fprintf(scfp,"Error: find_arcset_id found the arc after from "); return(-1); } } return(-1); } void add_to_arcset(ARC *arc, void *ptr){ ARCSET *arcset = (ARCSET *)ptr; arcset->arcs[arcset->num++] = arc; } void calc_minimum_delete(NET_ALIGN *net_ali,int a, int b, float *min_arc_d, int *min_ind_a){ int arc_a_id; ARC *arc_b, *arc_a; ARC_LIST_ATOM *arcpa; *min_arc_d = 999999999.9; *min_ind_a = -1; arc_a = net_ali->arcset_a.arcs[a]; arc_b = net_ali->arcset_b.arcs[b]; if (db >= 15) { printf(" Computing TREE DELETE COST [%d][%d]: ",a,b); printf(" "); print_arc(arc_a,0); printf(" "); print_arc(arc_b,0); } for (arcpa = arc_a->from_node->in_arcs; arcpa != NULL;arcpa = arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc,a)) <0){ fprintf(stderr,"Error: find_arcset_id failed in "); exit(1); } if (db >= 15) {printf(" look back to cell: %d,%d ",arc_a_id, b); printf(" "); print_arc(arcpa->arc,0); printf(" "); print_arc(arc_b,0);} if (net_ali->cell[arc_a_id][b].min_d < *min_arc_d){ *min_arc_d = net_ali->cell[arc_a_id][b].min_d; *min_ind_a = arc_a_id; } } if (db >= 15) printf(" Min_d=%4.2f cell [%d][%d] ", *min_arc_d, *min_ind_a, b); } void calc_minimum_insert(NET_ALIGN *net_ali,int a, int b, float *min_arc_d, int *min_ind_b){ int arc_b_id; ARC *arc_b, *arc_a; ARC_LIST_ATOM *arcpb; *min_arc_d = 999999999.9; *min_ind_b = -1; arc_a = net_ali->arcset_a.arcs[a]; arc_b = net_ali->arcset_b.arcs[b]; if (db >= 15) { printf(" Computing TREE INSERT COST [%d][%d]: ",a,b); printf(" "); print_arc(arc_a,0); printf(" "); print_arc(arc_b,0); } for (arcpb = arc_b->from_node->in_arcs; arcpb != NULL;arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),arcpb->arc,b)) <0){ fprintf(stderr,"Error: find_arcset_id failed "); exit(1); } if (db >= 15) {printf(" look back to cell: %d,%d ",a, arc_b_id); printf(" "); print_arc(arc_a,0); printf(" "); print_arc(arcpb->arc,0);} if (net_ali->cell[a][arc_b_id].min_d < *min_arc_d){ *min_arc_d = net_ali->cell[a][arc_b_id].min_d; *min_ind_b = arc_b_id; } } if (db >= 15) printf(" Min_d=%4.2f cell [%d][%d] ", *min_arc_d, a, *min_ind_b); } void calc_minimum_substi(NET_ALIGN *net_ali,int a, int b, float *min_arc_d, int *min_ind_a, int *min_ind_b){ int arc_a_id, arc_b_id; ARC *arc_a, *arc_b; ARC_LIST_ATOM *arcpa, *arcpb; *min_arc_d = 999999999.9; *min_ind_b = *min_ind_a = -1; arc_a = net_ali->arcset_a.arcs[a]; arc_b = net_ali->arcset_b.arcs[b]; if (db >= 15) { printf(" Computing TREE SUBSITUTION Cost [%d][%d]: ",a,b); printf(" ");print_arc(arc_a,0);printf(" "); print_arc(arc_b,0); } for (arcpa = arc_a->from_node->in_arcs; arcpa != NULL;arcpa = arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc,a)) <0){ fprintf(stderr,"Error: netA find_arcset_id failed "); exit(1); } for (arcpb = arc_b->from_node->in_arcs; arcpb != NULL; arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b), arcpb->arc,b)) <0){ fprintf(stderr,"Error: netB find_arcset_id failed "); exit(1); } if (db >= 15) {printf(" look back to cell: %d,%d ", arc_a_id, arc_b_id); printf(" ");print_arc(arcpa->arc,0); printf(" ");print_arc(arcpb->arc,0);} if (net_ali->cell[arc_a_id][arc_b_id].min_d < *min_arc_d){ *min_arc_d = net_ali->cell[arc_a_id][arc_b_id].min_d; *min_ind_b = arc_b_id; *min_ind_a = arc_a_id; } } } if (db >= 15) printf(" Min_d=%4.2f cell [%d][%d] ", *min_arc_d, *min_ind_a, *min_ind_b); } static int add_null_network_heads(NETWORK *net, char *proc){ if (Network_add_arc_to_head(net,(void *)0) > 0){ fprintf(stderr,"%s: Unable to add (void *)0 to Net A ",proc); return(1); } return(0); } static void calculate_margin_cells(NET_ALIGN *net_ali, NETWORK *net_a,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *))){ int a,b; float min_arc_d; int min_ind_a, min_ind_b; /* Pre-Calculate the margin Cells */ for (a=0,b=1; b<net_ali->arcset_b.num; b++){ calc_minimum_insert(net_ali,0,b,&min_arc_d,&min_ind_b); net_ali->cell[0][b].min_d = min_arc_d + wwd(NULL_WORD,net_ali->arcset_b.arcs[b]->data, net_a->arc_func.equal); net_ali->cell[0][b].back_a = 0; net_ali->cell[0][b].back_b = min_ind_b; if (db >= 15) printf(" Min_d=%4.2f back_a=%d back_b=%d ", net_ali->cell[a][b].min_d, net_ali->cell[a][b].back_a, net_ali->cell[a][b].back_b); } for (b=0,a=1; a<net_ali->arcset_a.num; a++){ calc_minimum_delete(net_ali,a,0,&min_arc_d,&min_ind_a); net_ali->cell[a][0].min_d = min_arc_d + wwd(net_ali->arcset_a.arcs[a]->data,NULL_WORD, net_a->arc_func.equal); net_ali->cell[a][0].back_a = min_ind_a; net_ali->cell[a][0].back_b = 0; if (db >= 15) printf(" Min_d=%4.2f back_a=%d back_b=%d ", net_ali->cell[a][b].min_d, net_ali->cell[a][b].back_a, net_ali->cell[a][b].back_b); } } static void calculate_internal_cells(NET_ALIGN *net_ali, NETWORK *net_a,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *))){ int a,b; for (a=1; a<net_ali->arcset_a.num; a++){ for (b=1; b<net_ali->arcset_b.num; b++){ /* for (b=1; b<2; b++){ */ int ins_min_ind_b, del_min_ind_a, sub_min_ind_a, sub_min_ind_b; float ins_min_d, del_min_d, sub_min_d; if (db >= 15){ printf("Computing minimized cost of [%d][%d]: ",a,b); print_arc(net_ali->arcset_a.arcs[a],0); print_arc(net_ali->arcset_b.arcs[b],0); } calc_minimum_insert(net_ali,a,b,&ins_min_d,&ins_min_ind_b); calc_minimum_delete(net_ali,a,b,&del_min_d,&del_min_ind_a); calc_minimum_substi(net_ali,a,b,&sub_min_d,&sub_min_ind_a, &sub_min_ind_b); if (db >= 15) printf(" Tree: Ins = %f Del = %f Sub = %f ", ins_min_d, del_min_d, sub_min_d); sub_min_d += wwd(net_ali->arcset_a.arcs[a]->data, net_ali->arcset_b.arcs[b]->data, net_a->arc_func.equal); del_min_d += wwd(net_ali->arcset_a.arcs[a]->data,0, net_a->arc_func.equal); ins_min_d += wwd(0,net_ali->arcset_b.arcs[b]->data, net_a->arc_func.equal); if (db >= 15) printf(" Tree+wwd: Ins = %f Del = %f Sub = %f ", ins_min_d, del_min_d, sub_min_d); if (sub_min_d <= del_min_d && sub_min_d <= ins_min_d){ net_ali->cell[a][b].min_d = sub_min_d; net_ali->cell[a][b].back_a = sub_min_ind_a; net_ali->cell[a][b].back_b = sub_min_ind_b; } else if (del_min_d < ins_min_d) { net_ali->cell[a][b].min_d = del_min_d; net_ali->cell[a][b].back_a = del_min_ind_a; net_ali->cell[a][b].back_b = b; } else { net_ali->cell[a][b].min_d = ins_min_d; net_ali->cell[a][b].back_a = a; net_ali->cell[a][b].back_b = ins_min_ind_b; } if (db >= 15) printf(" Min_d=%4.2f back_a=%d back_b=%d ", net_ali->cell[a][b].min_d, net_ali->cell[a][b].back_a, net_ali->cell[a][b].back_b); if (db >= 15) printf(" "); } } } void cleanup_NET_ALIGN(void){ char *proc="cleanup_NET_ALIGN"; if (db >= 1) printf("Entering %s: ",proc); Network_dpalign((NETWORK *)0,(NETWORK *)0, 0, (PATH **)0, 0); } static int setup_NET_ALIGN(NET_ALIGN **net_ali,NETWORK *net_a,NETWORK *net_b){ char *proc="setup_NET_ALIGN"; /* make sure there is enough room in the data structure */ if (((*net_ali) = alloc_NET_ALIGN((*net_ali),net_a->arc_count, net_b->arc_count)) == NET_ALIGN_NULL){ fprintf(stderr,"%s: alloc_NET_ALIGN failed ",proc); return(1); } /* link the arcs into the structure */ Network_traverse(net_a,0,0,add_to_arcset,(void *)&((*net_ali)->arcset_a), NT_CA_For + NT_Inorder); Network_traverse(net_b,0,0,add_to_arcset,(void *)&((*net_ali)->arcset_b), NT_CA_For + NT_Inorder); /* initialize the [0][0] Cell */ (*net_ali)->cell[0][0].back_a = -1; (*net_ali)->cell[0][0].back_b = -1; (*net_ali)->cell[0][0].min_d = 0.0; if (db >= 15) dump_NET_ALIGN((*net_ali),stdout); return(0); } PATH *extract_NET_ALIGN_result(NET_ALIGN *net_ali,NETWORK *net_a,NETWORK *net_b, int include_nulls){ PATH *t_path; ARC_LIST_ATOM *arcpa, *arcpb; int back_path, min_a, min_b, hops; int a, b; int arc_a_id, arc_b_id; float min_arc_d; /*********************************/ /* Beginning Back Trace */ /* Find the minimal network termination node, */ /* !!!! It's not just max x,y any more !!!!*/ min_arc_d = 999999.9; a = b = -1; for (arcpa = net_a->stop_node->in_arcs; arcpa != NULL;arcpa =arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc, net_ali->arcset_a.num)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } for (arcpb = net_b->stop_node->in_arcs; arcpb != NULL; arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),arcpb->arc, net_ali->arcset_b.num)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (min_arc_d > net_ali->cell[arc_a_id][arc_b_id].min_d){ min_arc_d = net_ali->cell[arc_a_id][arc_b_id].min_d; a = arc_a_id; b = arc_b_id; } } } min_a = a; min_b = b; {if (getenv("DBL") != NULL) { db=(int)atof(getenv("DBL")); }} if (db >= 25) dump_NET_ALIGN(net_ali,stdout); /* first do a back trace, counting the hops */ a = min_a ; b = min_b; hops = 0; while (a > 0 || b > 0){ int la, lb; if (db >= 15) printf("Counting hops a=%d b=%d %d,%d ",a,b, net_ali->cell[a][b].back_b,net_ali->cell[a][b].back_a); hops ++; /* check for an insertion of "@", and ignore it */ if (b == net_ali->cell[a][b].back_b) { if (!include_nulls && net_a->arc_func.is_null_alt(net_ali->arcset_a.arcs[a]->data)){ if (db >= 15) printf("Insertion of nulls "); hops --; } } else if (a == net_ali->cell[a][b].back_a) { /* check for a deletion of "@", and ignore it */ if (!include_nulls && net_b->arc_func.is_null_alt(net_ali->arcset_b.arcs[b]->data)){ if (db >= 15) printf("deletion of null "); hops --; } } else { /* check for a substitution of "@", and ignore it */ if (!include_nulls && (net_a->arc_func.is_null_alt(net_ali->arcset_a.arcs[a]->data))&& (net_b->arc_func.is_null_alt(net_ali->arcset_b.arcs[b]->data))){ printf("substition of to nulls "); hops --; } } la = a; lb = b; a = net_ali->cell[la][lb].back_a; b = net_ali->cell[la][lb].back_b; } a = min_a ; b = min_b; if (db >= 10) printf("Minimal Start point: D(%d,%d) = %f hops(%d) ", a,b,min_arc_d,hops); t_path = PATH_alloc(hops); back_path = hops - 1; a = min_a ; b = min_b; while (a > 0 || b > 0){ int la, lb; enum edits back; back = (a == net_ali->cell[a][b].back_a) ? INS : ((b == net_ali->cell[a][b].back_b) ? DEL : SUB); switch (back){ case DEL: if (include_nulls || !net_a->arc_func.is_null_alt(net_ali->arcset_a.arcs[a]->data)){ if (! net_a->arc_func.is_opt_del(net_ali->arcset_a.arcs[a]->data)){ t_path->pset[back_path].a_ptr = net_ali->arcset_a.arcs[a]->data; net_a->arc_func.use_count(net_ali->arcset_a.arcs[a]->data,1); t_path->pset[back_path].b_ptr = (void *)0; if (db >= 15){ printf("Del %d %d ",a,b); print_arc(net_ali->arcset_a.arcs[a],0); printf(" * "); } t_path->pset[back_path].eval = P_DEL; } else { /**** OPTIONALLY deletable, make it correct */ t_path->pset[back_path].a_ptr = net_ali->arcset_a.arcs[a]->data; net_a->arc_func.use_count(net_ali->arcset_a.arcs[a]->data,1); t_path->pset[back_path].b_ptr = net_a->arc_func.make_empty((void*)0); if (db >= 15){ printf("Corr Del %d %d ",a,b); print_arc(net_ali->arcset_a.arcs[a],0); } t_path->pset[back_path].eval = P_CORR; } --back_path; t_path->num ++; } break; case SUB: if(include_nulls || !(net_b->arc_func.is_null_alt(net_ali->arcset_a.arcs[a]->data)&& net_b->arc_func.is_null_alt(net_ali->arcset_b.arcs[b]->data))){ t_path->pset[back_path].a_ptr = net_ali->arcset_a.arcs[a]->data; net_a->arc_func.use_count(net_ali->arcset_a.arcs[a]->data,1); t_path->pset[back_path].b_ptr = net_ali->arcset_b.arcs[b]->data; net_a->arc_func.use_count(net_ali->arcset_b.arcs[b]->data,1); if (db >= 15){ printf("Sub %d %d ",a,b); print_arc(net_ali->arcset_a.arcs[a],0); print_arc(net_ali->arcset_b.arcs[b],0); } if (net_a->arc_func.equal(t_path->pset[back_path].a_ptr, t_path->pset[back_path].b_ptr) == 0) t_path->pset[back_path].eval = P_CORR; else t_path->pset[back_path].eval = P_SUB; --back_path; t_path->num ++; } break; case INS: if (include_nulls || !net_b->arc_func.is_null_alt(net_ali->arcset_b.arcs[b]->data)){ if (! net_b->arc_func.is_opt_del(net_ali->arcset_b.arcs[b]->data)){ t_path->pset[back_path].a_ptr = (void *)0; t_path->pset[back_path].b_ptr = net_ali->arcset_b.arcs[b]->data; net_a->arc_func.use_count(net_ali->arcset_b.arcs[b]->data,1); if (db >= 15){ printf("INS %d %d ",a,b); printf(" * "); print_arc(net_ali->arcset_b.arcs[b],0); } t_path->pset[back_path].eval = P_INS; } else { t_path->pset[back_path].a_ptr = net_b->arc_func.make_empty((void*)0); t_path->pset[back_path].b_ptr = net_ali->arcset_b.arcs[b]->data; net_a->arc_func.use_count(net_ali->arcset_b.arcs[b]->data,1); if (db >= 15){ printf("CORR INS %d %d ",a,b); printf(" * "); print_arc(net_ali->arcset_b.arcs[b],0); } t_path->pset[back_path].eval = P_CORR; } --back_path; t_path->num ++; } } la = a; lb = b; a = net_ali->cell[la][lb].back_a; b = net_ali->cell[la][lb].back_b; } return(t_path); } int Network_dpalign(NETWORK *net_a,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *)), PATH **out_path, int include_nulls){ char *proc = "Network_dpalign"; int prev_db; prev_db = db; if (getenv("NET_DP_DBL") != NULL) { db=(int)atof(getenv("NET_DP_DBL")); } if (db >= 1) printf("Entering %s: %s",proc,proc); if (net_a == (NETWORK *)0 && net_b == (NETWORK *)0){ if (db >= 1) printf("destroying %s's data ",proc); destroy_NET_ALIGN(&net_ali); return (1); } /* Add Null words to the beginning of Net A and B */ if (add_null_network_heads(net_a,proc) != 0) return(1); if (add_null_network_heads(net_b,proc) != 0) return(1); if (setup_NET_ALIGN(&net_ali,net_a,net_b) != 0) return(1); calculate_margin_cells(net_ali,net_a,net_b,wwd); calculate_internal_cells(net_ali,net_a,net_b,wwd); if (db >= 15) dump_NET_ALIGN(net_ali,stdout); *out_path = extract_NET_ALIGN_result(net_ali, net_a, net_b, include_nulls); db=prev_db; return(0); } int Network_dpalign_n_networks(NETWORK **in_nets, int n_nets, float wwd(void *, void *, int (*cmp)(void *, void *)), NETWORK **out_net, void *null_alt){ char *proc = "Network_dpalign_n_networks"; NETWORK **copy_in_nets; int new_db = db, n, rtn=0; if (getenv("NET_DP_DBL") != NULL) new_db =(int)atof(getenv("NET_DP_DBL")); alloc_singZ(copy_in_nets,n_nets,NETWORK *,(NETWORK *)0); if (new_db >= 1) printf("Entering %s: ",proc); /* make a copy of the networks and align to those. This allows */ /* the return network function to work from the copy to make the */ /* output network */ for (n=0; n<n_nets; n++){ if ((copy_in_nets[n] = Network_copy(in_nets[n])) == (NETWORK *)0){ fprintf(scfp,"Error: %s unable to copy networks ",proc); goto ERROR; } if (add_null_network_heads(copy_in_nets[n],proc) != 0) { goto ERROR; } if (new_db >= 10) { printf("%s: ---------------------------------------- ",proc); printf("%s: Input network %d ",proc,n); if (n == 0) printf("%s: STARTING OUTPUT NETOWRK ",proc); Network_traverse(copy_in_nets[n],0,0,print_arc,0, NT_CA_For+NT_Verbose); } } if (new_db >= 10) printf("%s: ---------------------------------------- ",proc); /* now loop through the networks, aligning them, then merging them */ for (n=1; n<n_nets; n++){ if (setup_NET_ALIGN(&net_ali,copy_in_nets[0],copy_in_nets[n]) != 0){ goto ERROR; } calculate_margin_cells(net_ali,copy_in_nets[0],copy_in_nets[n],wwd); calculate_internal_cells(net_ali,copy_in_nets[0],copy_in_nets[n],wwd); if (new_db >= 15) dump_NET_ALIGN(net_ali,stdout); create_alinged_NETWORK(net_ali,copy_in_nets[0],copy_in_nets[n], null_alt,n); if (new_db >= 10) { printf("%s: ---------------------------------------- ",proc); printf("%s: Output Network %d ",proc,n); Network_traverse(copy_in_nets[0],0,0,print_arc,0, NT_CA_For+NT_Verbose); } } Network_delete_null_arcs(copy_in_nets[0]); *out_net = copy_in_nets[0]; rtn = 0; goto CLEANUP; ERROR: rtn = 1; CLEANUP: for (n=1; n<n_nets; n++) Network_destroy(copy_in_nets[n]); free_singarr(copy_in_nets,NETWORK *); return(rtn); } /*** THIS CODE MAKES THE ASSUMPTION THE net_b network is a linear network */ /*** without branching */ void create_alinged_NETWORK(NET_ALIGN *net_ali, NETWORK *net_a, NETWORK *net_b, void *null_alt, int iteration){ char *proc = "create_alinged_NETWORK"; ARC_LIST_ATOM *arcpa, *arcpb; void *copy_b_arc_data; int min_a, min_b; int a, b, i; int arc_a_id, arc_b_id; float min_arc_d; int new_db = 0; if (getenv("ALIGN_NET_DBL") != NULL) new_db=(int)atof(getenv("ALIGN_NET_DBL")); if (new_db >= 10) printf("Entering %s: ",proc); /* Find the minimal network termination node, */ /* !!!! It's not just max x,y any more !!!!*/ min_arc_d = 999999.9; a = b = -1; for (arcpa = net_a->stop_node->in_arcs; arcpa != NULL;arcpa =arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc, net_ali->arcset_a.num)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } for (arcpb = net_b->stop_node->in_arcs; arcpb != NULL; arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),arcpb->arc, net_ali->arcset_b.num)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (min_arc_d > net_ali->cell[arc_a_id][arc_b_id].min_d){ min_arc_d = net_ali->cell[arc_a_id][arc_b_id].min_d; a = arc_a_id; b = arc_b_id; } } } min_a = a; min_b = b; /*******************************************************************/ /* Beginning the Back Trace copying the B-arcs onto A_net */ if (new_db >= 10) printf("Minimal Start point: D(%d,%d) = %f ",min_a,min_b,min_arc_d); a = min_a ; b = min_b; while (a > 0 || b > 0){ int la = a, lb = b; ARC *a_arc, *b_arc; enum edits back; back = (a == net_ali->cell[a][b].back_a) ? INS : ((b == net_ali->cell[a][b].back_b) ? DEL : SUB); b_arc = net_ali->arcset_b.arcs[b]; a_arc = net_ali->arcset_a.arcs[a]; switch (back){ case SUB: /* insert a copy the B_arc onto the from_node and to_node of the A_arc */ if (new_db >= 10){ printf("%s: SUB ",proc); printf("%s: Insert copy of B_arc",proc); print_arc(b_arc,stdout); printf("%s: between A_arcs(from)",proc); print_node(a_arc->from_node,stdout); printf("%s: A_arcs(to) ",proc); print_node(a_arc->to_node,stdout); } if (NETWORK_insert_arc_between_node(net_a, a_arc->from_node, a_arc->to_node, b_arc->net->arc_func.copy(b_arc->data)) != 0) goto ERROR; break; case DEL: /* insert a @ arc onto the from_node and to_node of the A_arc */ if (new_db >= 10){ printf("%s: DEL ",proc); printf("%s: Insert copy of null_alt ",proc); printf("%s: between A_arcs(from)",proc); print_node(a_arc->from_node,stdout); printf("%s: A_arcs(to) ",proc); print_node(a_arc->to_node,stdout); } if (NETWORK_insert_arc_between_node(net_a, a_arc->from_node, a_arc->to_node, a_arc->net->arc_func.copy(null_alt)) != 0) goto ERROR; break; case INS: /* insert a subnetwork of { @ / B_arc } onto the from_node */ /* and to_node of the A_arc, There will be 'iteration' @'s */ /* inside this network */ if (new_db >= 10){ printf("%s: INS ",proc); printf("%s: Insert a subnet with %d null arcs and a " "copy of b_arc ",proc,iteration); printf("%s: Insert copy of B_arc",proc); print_arc(b_arc,stdout); printf("%s: At A_arcs(to_node) ",proc); print_node(a_arc->to_node,stdout); } /* first, insert the copy of b_arc's data into the A_net */ copy_b_arc_data = a_arc->net->arc_func.copy(b_arc->data); if (NETWORK_insert_arc_before_node(a_arc->net, a_arc->to_node, copy_b_arc_data) != 0) goto ERROR; /* Find, in A_net, the new arc */ for (arcpa = a_arc->to_node->out_arcs; arcpa != NULL; arcpa = arcpa->next) if (arcpa->arc->data == copy_b_arc_data){ break; } if (arcpa == NULL){ fprintf(scfp,"Internal Error "); goto ERROR; } for (i=0; i<iteration; i++) if (NETWORK_insert_arc_between_node(arcpa->arc->net, arcpa->arc->from_node, arcpa->arc->to_node, a_arc->net->arc_func.copy(null_alt)) != 0) goto ERROR; break; } la = a; lb = b; a = net_ali->cell[la][lb].back_a; b = net_ali->cell[la][lb].back_b; } goto CLEANUP; ERROR: printf("ERROR OCCURED "); CLEANUP: return; } void dump_NET_ALIGN(NET_ALIGN *net_ali,FILE *fp){ int aa, bb; printf("Dump of net_ali: "); printf(" Arc List from Network A "); for (aa=0; aa<net_ali->arcset_a.num; aa++){ printf(" %2d: ",aa); print_arc(net_ali->arcset_a.arcs[aa],0); } printf(" Arc List from Network B "); for (bb=0; bb<net_ali->arcset_b.num; bb++){ printf(" %2d: ",bb); print_arc(net_ali->arcset_b.arcs[bb],0); } printf(" "); for (bb=0; bb<net_ali->arcset_b.num; bb++) printf(" %3d ",bb); printf(" "); for (aa=0; aa<net_ali->arcset_a.num; aa++) { printf(" %3d ",aa); for (bb=0; bb<net_ali->arcset_b.num; bb++) printf("D%6.3f ",net_ali->cell[aa][bb].min_d); printf(" "); printf(" "); for (bb=0; bb<net_ali->arcset_b.num; bb++){ if (aa == net_ali->cell[aa][bb].back_a) printf("I="); else if (bb == net_ali->cell[aa][bb].back_b) printf("D="); else printf("S="); printf("%2d,%2d ",net_ali->cell[aa][bb].back_a, net_ali->cell[aa][bb].back_b); } printf(" "); } } void alloc_ARCSET(ARCSET *arcset, int n){ arcset->max = n; arcset->num = 0; alloc_singarr(arcset->arcs,arcset->max,ARC *); } void free_ARCSET(ARCSET *arcset){ arcset->max = 0; arcset->num = 0; free_singarr(arcset->arcs,ARC *); } static void destroy_NET_ALIGN(NET_ALIGN **net_ali){ char *proc = "destroy_NET_ALIGN"; if (db >= 1) printf("Entering %s: ",proc); /* free the cell array */ free_2dimarr((*net_ali)->cell,(*net_ali)->arcset_a.max,CELL); /* the arcsets */ free_ARCSET(&((*net_ali)->arcset_a)); free_ARCSET(&((*net_ali)->arcset_b)); /* the structure */ free_singarr((*net_ali),NET_ALIGN); *net_ali = (NET_ALIGN *)0; } NET_ALIGN *alloc_NET_ALIGN(NET_ALIGN *net_ali, int na, int nb){ char *proc = "alloc_NET_ALIGN"; int alloc_data, init_size=100; if (db >= 1) printf("Entering %s: ",proc); if (na < init_size) na = init_size; if (nb < init_size) nb = init_size; if (db >= 2) printf("NET_ALIGN: %p na=%d nb=%d ",net_ali,na,nb); if (net_ali == NET_ALIGN_NULL) { /* do the first initialization */ alloc_singarr(net_ali,1,NET_ALIGN); alloc_data = T; } else if (net_ali->arcset_a.max < na || net_ali->arcset_b.max < nb) { /* Then ... Re-allocate the structure */ if (db >= 5) printf("%s: Free-ing previous data structure ",proc); /* free the old memory */ free_2dimarr(net_ali->cell,net_ali->arcset_a.max,CELL); free_ARCSET(&(net_ali->arcset_a)); free_ARCSET(&(net_ali->arcset_b)); alloc_data = T; } else alloc_data = F; if (alloc_data){ alloc_ARCSET(&(net_ali->arcset_a),(int)(na * 1.05)); alloc_ARCSET(&(net_ali->arcset_b),(int)(nb * 1.05)); alloc_2dimarr(net_ali->cell, net_ali->arcset_a.max,net_ali->arcset_b.max,CELL); } /* erase the data cells and arcs */ #ifdef full clear_sing(net_ali->arcset_a.arcs,net_ali->arcset_a.max,(ARC *)0); clear_sing(net_ali->arcset_b.arcs,net_ali->arcset_b.max,(ARC *)0); for (a=0; a<net_ali->arcset_a.max; a++) for (b=0; b<net_ali->arcset_b.max; b++){ c = &(net_ali->cell[a][b]); c->min_d = 0.0; c->back_a = c->back_b = 0; } #endif net_ali->arcset_a.num = 0; net_ali->arcset_b.num = 0; return(net_ali); } /*************************************************************************/ /*************************************************************************/ /*************************************************************************/ /*************************************************************************/ /*************************************************************************/ PATH *extract_best_NET_ALIGN_result(NET_ALIGN *net_ali,NETWORK *net_a,NETWORK *net_b,float wwd(void *, void *, int (*cmp)(void *, void *))){ ARC_LIST_ATOM *arcpa, *arcpb; int min_a, min_b, hops=0; int a, b; int arc_a_id, arc_b_id; float min_arc_d; int r=0, count=0; PATH *path; path = PATH_alloc(100); /*********************************/ /* Beginning Back Trace */ /* Find the minimal network termination node, */ /* !!!! It's not just max x,y any more !!!!*/ min_arc_d = 999999.9; a = b = -1; for (arcpa = net_a->stop_node->in_arcs; arcpa != NULL;arcpa =arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc, net_ali->arcset_a.num)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } for (arcpb = net_b->stop_node->in_arcs; arcpb != NULL; arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),arcpb->arc, net_ali->arcset_b.num)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (min_arc_d > net_ali->cell[arc_a_id][arc_b_id].min_d){ min_arc_d = net_ali->cell[arc_a_id][arc_b_id].min_d; a = arc_a_id; b = arc_b_id; } } } min_a = a; min_b = b; /* dump_NET_ALIGN(net_ali,stdout); */ for (arcpa = net_a->stop_node->in_arcs; arcpa != NULL;arcpa =arcpa->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),arcpa->arc, net_ali->arcset_a.num)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } for (arcpb = net_b->stop_node->in_arcs; arcpb != NULL; arcpb = arcpb->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),arcpb->arc, net_ali->arcset_b.num)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (min_arc_d == net_ali->cell[arc_a_id][arc_b_id].min_d){ printf("Minimal Start point: D(%d,%d) = %f hops(%d) ", arc_a_id,arc_b_id,min_arc_d,hops); find_minimal_paths(net_ali,arc_a_id,arc_b_id, &r,wwd, net_a, path,&count); printf("Count = %d ",count); (count) = 0; } } } db = 0; return((PATH *)0); } void find_minimal_paths(NET_ALIGN *net_ali, int a, int b, int *rlev,float wwd(void *, void *, int (*cmp)(void *, void *)),NETWORK *net_a, PATH *path,int *count) { ARC_LIST_ATOM *oarc_a, *oarc_b; ARC *arc_a, *arc_b; int la, lb, cost; int arc_a_id, arc_b_id,d; int db = 0; char buf[100]; sprintf(buf,rsprintf("%%0%ds",*rlev),""); if (a == 0 && b == 0){ if (db > 5) printf("END recursion "); if (db > 2) { printf("Count = %d ",*count); PATH_print(path,stdout,100); } (*count)++; return; } arc_a = net_ali->arcset_a.arcs[a]; arc_b = net_ali->arcset_b.arcs[b]; cost = net_ali->cell[a][b].min_d; if (db > 5){ printf("%sCurrent position: D(%d,%d) = %d ",buf,a,b,cost); printf("%s ",buf); print_arc(arc_a,0); printf("%s ",buf); print_arc(arc_b,0); } /* loop through the net A in_arcs, seaching for possible paths */ for (oarc_a = arc_a->from_node->in_arcs; oarc_a != NULL; oarc_a=oarc_a->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),oarc_a->arc, a)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } if (db > 5) { printf("%s Checking deletion arcs: ",buf); printf("%s A:",buf); print_arc(oarc_a->arc,0); } if (net_ali->cell[arc_a_id][b].min_d <= cost && (wwd(arc_a->data,NULL_WORD,net_a->arc_func.equal) + net_ali->cell[arc_a_id][b].min_d) <= cost){ if (db > 5) printf("%s Possible PATH ",buf); if (db > 5) printf("%s ***** Delete %s ",buf, ((WORD*)(arc_b->data))->value); *rlev += 2; PATH_append(path,arc_a->data,0,P_DEL); find_minimal_paths(net_ali,arc_a_id,b,rlev,wwd,net_a,path,count); PATH_remove(path); *rlev -= 2; } } for (oarc_b = arc_b->from_node->in_arcs; oarc_b != NULL; oarc_b = oarc_b->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),oarc_b->arc, b)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (db > 5) { printf("%s Checking insertion arcs: ",buf); printf("%s B:",buf); print_arc(oarc_b->arc,0); } if (net_ali->cell[a][arc_b_id].min_d <= cost && (wwd(NULL_WORD,arc_b->data,net_a->arc_func.equal) + net_ali->cell[a][arc_b_id].min_d) <= cost){ if (db > 5) printf("%s Possible PATH ",buf); if (db > 5) printf("%s ***** Insert %s ",buf, ((WORD*)(arc_b->data))->value); *rlev += 2; PATH_append(path,0,arc_b->data,P_INS); find_minimal_paths(net_ali,a,arc_b_id,rlev,wwd,net_a,path,count); PATH_remove(path); *rlev -= 2; } } for (oarc_a = arc_a->from_node->in_arcs; oarc_a != NULL; oarc_a=oarc_a->next){ if ((arc_a_id = find_arcset_id(&(net_ali->arcset_a),oarc_a->arc, a)) <0){ fprintf(stderr,"Error: find_arcset_id for Net A failed "); exit(1); } for (oarc_b = arc_b->from_node->in_arcs; oarc_b != NULL; oarc_b = oarc_b->next){ if ((arc_b_id = find_arcset_id(&(net_ali->arcset_b),oarc_b->arc, b)) < 0){ fprintf(stderr,"Error: find_arcset_id for Net B failed "); exit(1); } if (db > 5) { printf("%s Checking substitution arcs: (%d,%d) ",buf, arc_a_id,arc_b_id); printf("%s A:",buf); print_arc(oarc_a->arc,0); printf("%s B:",buf); print_arc(oarc_b->arc,0); } if (net_ali->cell[arc_a_id][arc_b_id].min_d <= cost && ((d=wwd(arc_a->data,arc_b->data,net_a->arc_func.equal)) + net_ali->cell[arc_a_id][arc_b_id].min_d) <= cost){ if (db > 5){ printf("%s Possible PATH ",buf); printf("%s ***** Substitute %s and %s ",buf, ((WORD*)(arc_a->data))->value, ((WORD*)(arc_b->data))->value); } *rlev += 2; PATH_append(path,arc_a->data,arc_b->data,(d==0)?P_CORR:P_SUB); find_minimal_paths(net_ali,arc_a_id,arc_b_id,rlev,wwd,net_a,path,count); PATH_remove(path); *rlev -= 2; } } } la = a; lb = b; a = net_ali->cell[la][lb].back_a; b = net_ali->cell[la][lb].back_b; } |