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 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
|
/*
* sequence_match.c
*
* match 2 mmCIF files on sequence and export the matching portions
* of their atoms lists
*
*
* Created by Herbert J. Bernstein on 2/24/10.
* Copyright 2010 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* SYNOPSIS *
* *
* seqmatch [-l leftinput] [-r rightinput] \ *
* [-m leftoutput] [-s rightoutput] \ *
* [-a|-c] *
* [leftinput] [rightinput] \ *
* [leftoutput] [rightoutput] *
* *
* *
**********************************************************************/
#include "cbf.h"
#include "cbf_simple.h"
#include "cbf_string.h"
#include "cbf_copy.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "cbf_getopt.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define C2CBUFSIZ 8192
#ifdef __MINGW32__
#define NOMKSTEMP
#define NOTMPDIR
#endif
int local_exit (int status);
int outerror(int err);
int outerror(int err)
{
if ((err&CBF_FORMAT)==CBF_FORMAT)
fprintf(stderr, " cif2cbf: The file format is invalid.\n");
if ((err&CBF_ALLOC)==CBF_ALLOC)
fprintf(stderr, " cif2cbf Memory allocation failed.\n");
if ((err&CBF_ARGUMENT)==CBF_ARGUMENT)
fprintf(stderr, " cif2cbf: Invalid function argument.\n");
if ((err&CBF_ASCII)==CBF_ASCII)
fprintf(stderr, " cif2cbf: The value is ASCII (not binary).\n");
if ((err&CBF_BINARY)==CBF_BINARY)
fprintf(stderr, " cif2cbf: The value is binary (not ASCII).\n");
if ((err&CBF_BITCOUNT)==CBF_BITCOUNT)
fprintf(stderr, " cif2cbf: The expected number of bits does"
" not match the actual number written.\n");
if ((err&CBF_ENDOFDATA)==CBF_ENDOFDATA)
fprintf(stderr, " cif2cbf: The end of the data was reached"
" before the end of the array.\n");
if ((err&CBF_FILECLOSE)==CBF_FILECLOSE)
fprintf(stderr, " cif2cbf: File close error.\n");
if ((err&CBF_FILEOPEN)==CBF_FILEOPEN)
fprintf(stderr, " cif2cbf: File open error.\n");
if ((err&CBF_FILEREAD)==CBF_FILEREAD)
fprintf(stderr, " cif2cbf: File read error.\n");
if ((err&CBF_FILESEEK)==CBF_FILESEEK)
fprintf(stderr, " cif2cbf: File seek error.\n");
if ((err&CBF_FILETELL)==CBF_FILETELL)
fprintf(stderr, " cif2cbf: File tell error.\n");
if ((err&CBF_FILEWRITE)==CBF_FILEWRITE)
fprintf(stderr, " cif2cbf: File write error.\n");
if ((err&CBF_IDENTICAL)==CBF_IDENTICAL)
fprintf(stderr, " cif2cbf: A data block with the new name already exists.\n");
if ((err&CBF_NOTFOUND)==CBF_NOTFOUND)
fprintf(stderr, " cif2cbf: The data block, category, column or"
" row does not exist.\n");
if ((err&CBF_OVERFLOW)==CBF_OVERFLOW)
fprintf(stderr, " cif2cbf: The number read cannot fit into the "
"destination argument.\n The destination has been set to the nearest value.\n");
if ((err& CBF_UNDEFINED)==CBF_UNDEFINED)
fprintf(stderr, " cif2cbf: The requested number is not defined (e.g. 0/0).\n");
if ((err&CBF_NOTIMPLEMENTED)==CBF_NOTIMPLEMENTED)
fprintf(stderr, " cif2cbf: The requested functionality is not yet implemented.\n");
return 0;
}
#undef cbf_failnez
#define cbf_failnez(x) \
{int err; \
err = (x); \
if (err) { \
fprintf(stderr,"CBFlib fatal error %d\n",err); \
outerror(err); \
local_exit (-1); \
} \
}
typedef struct {
int hashchain; /* next index by residue name */
int resnumhashchain; /* next index by residue number */
const char * resname; /* residue name */
int resnum; /* residue number */
int seqmatch; /* index in another chain of a match */
} residue_entry;
typedef struct {
int hash_table[256]; /* indices by residue name hash code */
int hash_end[256]; /* indices of the ends of the name hash chains */
int hash_resnum[256]; /* indices by residue number hash code */
int hash_resnum_end[256]; /* indices of the ends of the number hash chains */
size_t size;
size_t capacity;
int minresnum;
int maxresnum;
residue_entry * residues;
const char* entity;
} residue_chain;
typedef residue_chain * chainhandle;
int residue_hash(const char * resname) {
int hashcode;
int c;
int j;
size_t len;
hashcode = 0;
if (!resname) return -1;
len = strlen(resname);
for (j=0; j<len;j++) {
c = toupper(resname[j]);
hashcode = (hashcode << 1) +(c&0xFF);
}
return (hashcode &0xff);
}
int create_chainhandle(chainhandle * handle, const char * entity, size_t capacity) {
int i;
*handle = (chainhandle)malloc(sizeof(residue_chain));
if (!*handle) return -1;
for (i=0;i<256;i++) {
(*handle)->hash_table[i] = -1;
(*handle)->hash_end[i] = -1;
(*handle)->hash_resnum[i] = -1;
(*handle)->hash_resnum_end[i] = -1;
}
(*handle)->size = 0;
(*handle)->capacity = (!capacity)?10:capacity;
(*handle)->entity = entity;
(*handle)->residues = (residue_entry*)malloc(((*handle)->capacity)*sizeof(residue_entry));
(*handle)->maxresnum = -999999;
(*handle)->minresnum = 999999;
if (!(*handle)->residues) {
free (*handle);
*handle = NULL;
return -1;
}
return 0;
}
int add_residue(chainhandle handle, const char * residue, int resnum) {
residue_entry * re;
int hashcode;
int he;
if (!handle) return -1;
if (handle->size >= handle->capacity) {
if((re=(residue_entry *)malloc(handle->capacity*2*sizeof(residue_entry)))){
memmove(re,handle->residues,(handle->size)*sizeof(residue_entry));
free(handle->residues);
handle->capacity *=2;
handle->residues = re;
} else {
return -1;
}
}
re=handle->residues;
hashcode = residue_hash(residue);
re[handle->size].hashchain=-1;
re[handle->size].resname = residue;
re[handle->size].resnum = resnum;
if (resnum < handle->minresnum) handle->minresnum = resnum;
if (resnum > handle->maxresnum) handle->maxresnum = resnum;
re[handle->size].seqmatch = -1;
if ((he=(handle->hash_end)[hashcode]) != -1) {
re[he].hashchain = handle->size;
}
(handle->hash_end)[hashcode] = handle->size;
if ((handle->hash_table)[hashcode] == -1) {
(handle->hash_table)[hashcode] = handle->size;
}
hashcode=resnum&0xFF;
re[handle->size].resnumhashchain=-1;
(handle->hash_resnum_end)[hashcode] = handle->size;
if ((handle->hash_resnum)[hashcode] == -1) {
(handle->hash_resnum)[hashcode] = handle->size;
}
(handle->size)++;
return 0;
}
int get_by_residue(chainhandle chain, int oldindex, const char* residue) {
int hashcode;
hashcode = residue_hash(residue);
if (oldindex < 0 ) {
oldindex = chain->hash_table[hashcode];
} else {
oldindex = (chain->residues)[oldindex].hashchain;
}
if (oldindex < 0) return -1;
while(cbf_cistrcmp((chain->residues)[oldindex].resname,residue) ) {
oldindex = (chain->residues)[oldindex].hashchain;
if (oldindex < 0) return -1;
}
return oldindex;
}
int get_by_resnum(chainhandle chain, int oldindex, int resnum) {
if (oldindex < 0) {
oldindex = chain->hash_resnum[resnum&0xFF];
} else {
oldindex = (chain->residues)[oldindex].resnumhashchain;
}
if (oldindex < 0) return -1;
while((chain->residues)[oldindex].resnum != resnum ) {
oldindex = (chain->residues)[oldindex].resnumhashchain;
if (oldindex < 0) return -1;
}
return oldindex;
}
/* find the next match to a given residue, returning the index,
not the residue number*/
int find_residue(chainhandle chain, const char * residue, int startnum, int minnum) {
int hashcode;
int index;
if (startnum < chain->minresnum) {
hashcode = residue_hash(residue);
index = chain->hash_table[hashcode];
} else {
index = get_by_resnum(chain,-1,startnum);
index = (chain->residues)[index].hashchain;
}
while(index >= 0 ){
if ( !cbf_cistrcmp((chain->residues)[index].resname,residue)
&& (chain->residues)[index].resnum >= minnum) {
return index;
} else {
index = (chain->residues)[index].hashchain;
}
}
return index;
}
/* compare part of two sequences by residue number
returns 0 for a match or the relative index of the
first difference, starting at 1*/
int seq_comp_partial(chainhandle leftseq, chainhandle rightseq,
int leftstart, int rightstart, int lentomatch) {
int ii;
int left, right;
int imatch;
left = -1;
right = -1;
for (ii=0; ii<lentomatch; ii++) {
left = get_by_resnum(leftseq, left, leftstart+ii);
right = get_by_resnum(rightseq, right, rightstart+ii);
if (left < 0 ) {
if (right < 0) return 0;
return ii+1;
} else {
(leftseq->residues)[left].seqmatch = -1;
if (right < 0) return ii+1;
if (!cbf_cistrcmp((leftseq->residues)[left].resname,(rightseq->residues)[right].resname)) {
(leftseq->residues)[left].seqmatch = right;
left = right = -1;
continue;
}
while ( (left = get_by_resnum(leftseq, left, leftstart+ii)) >= 0) {
right = -1;
imatch = 0;
while ( (right = get_by_resnum(rightseq, right, rightstart+ii)) >= 0) {
if (!cbf_cistrcmp((leftseq->residues)[left].resname,(rightseq->residues)[right].resname)){
imatch = 1;
break;
}
if (imatch == 1) {
left = right = -1;
continue;
}
}
}
return ii+1;
}
left = -1;
right = -1;
}
return 0;
}
int seq_comp(chainhandle leftseq, chainhandle rightseq ) {
int index;
int seqlim;
int isl;
int isr;
int iii;
int leftlow;
int lefthigh;
int rightlow;
int righthigh;
int imatch;
int lentomatch;
int kmatched;
leftlow = leftseq->minresnum;
lefthigh = leftseq->maxresnum;
rightlow = rightseq->minresnum;
righthigh = rightseq->maxresnum;
seqlim = (lefthigh-leftlow+1)/2;
if (seqlim > 6) seqlim = 6;
for (iii=0;iii <=leftseq->size; iii++) {
(leftseq->residues)[iii].seqmatch=-1;
}
kmatched = 0;
/* we will try to match a sequence of length lm from the
left sequence to some portion of the right sequence,
accepting any match of at least 4 residues */
isl = leftlow;
while (isl <= lefthigh) {
index = get_by_resnum(leftseq,-1,isl);
if (index < 0) {
isl++;
continue;
}
iii = find_residue(rightseq,(leftseq->residues)[index].resname,-1,rightlow);
if (iii < 0) {
while((index=get_by_resnum(leftseq,index,isl)>=0)){
iii = find_residue(rightseq,(leftseq->residues)[index].resname,-1,rightlow);
if (iii < 0) {
continue;
}
isr = (rightseq->residues)[iii].resnum;
lentomatch = lefthigh-isl+1;
if (lentomatch > righthigh-isr+1) {
lentomatch = righthigh-isr+1;
}
imatch = seq_comp_partial(leftseq, rightseq, isl, isr, lentomatch);
if (imatch > 0 && imatch < 4) {
(leftseq->residues)[index].seqmatch=-1;
while((index=get_by_resnum(leftseq,index,isl)>=0)){
(leftseq->residues)[index].seqmatch=-1;
}
continue;
}
if (imatch == 0 ) imatch = lentomatch;
isl += imatch-1;
rightlow = isr+imatch;
kmatched +=imatch;
break;
}
} else {
isr = (rightseq->residues)[iii].resnum;
lentomatch = lefthigh-isl+1;
if (lentomatch > righthigh-isr+1) {
lentomatch = righthigh-isr+1;
}
imatch = seq_comp_partial(leftseq, rightseq, isl, isr, lentomatch);
if (imatch > 0 && imatch < 4) {
(leftseq->residues)[index].seqmatch=-1;
while((index=get_by_resnum(leftseq,index,isl)>=0)){
(leftseq->residues)[index].seqmatch=-1;
}
isl++; continue;
}
if (imatch == 0 ) imatch = lentomatch;
isl += imatch;
rightlow = isr+imatch;
kmatched +=imatch;
break;
}
isl++;
}
return kmatched;
}
int main (int argc, char *argv [])
{
FILE *leftin, *rightin, *leftout=NULL, *rightout=NULL;
const char * leftinstr, * rightinstr, * leftoutstr, * rightoutstr;
char * leftintmpstr, *rightintmpstr;
cbf_handle leftincbf, rightincbf, leftoutcbf, rightoutcbf;
cbf_getopt_handle opts;
int doall,doca;
int nbytes;
int c;
int leftdevnull, rightdevnull;
char buf[C2CBUFSIZ];
chainhandle leftch[256];
chainhandle rightch[256];
const char * leftchname[256];
const char * rightchname[256];
int left_to_right[256];
int right_to_left[256];
int numleftch, numrightch;
int iii, jjj;
int errflg = 0;
#ifndef NOMKSTEMP
int leftintmpfd, rightintmpfd;
#endif
int leftintmpused, rightintmpused;
const char * optarg;
CBF_UNUSED( leftdevnull );
/* Ex options */
leftinstr = NULL;
rightinstr = NULL;
leftoutstr = NULL;
rightoutstr = NULL;
leftintmpstr = NULL;
rightintmpstr = NULL;
leftdevnull = rightdevnull = 0;
leftintmpused = rightintmpused = 0;
errflg = 0;
numleftch = numrightch = 0;
doall = doca = 0;
cbf_failnez(cbf_make_getopt_handle(&opts))
cbf_failnez(cbf_getopt_parse(opts, argc, argv, "-l(leftinput):" \
"-r(rightinput):" \
"-m(leftoutput):" \
"-s(rightoutput):" \
"-a(allatoms)" \
"-c(calpha)" \
))
if (!cbf_rewind_getopt_option(opts))
for(;!cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg);cbf_next_getopt_option(opts)) {
if (!c) break;
switch(c) {
case 'l': /* left input file */
if (leftinstr) errflg++;
else leftinstr = optarg;
break;
case 'r': /* right input file */
if (rightinstr) errflg++;
else rightinstr = optarg;
break;
case 'm': /* left output file */
if (leftoutstr) errflg++;
else leftoutstr = optarg;
break;
case 's': /* right output file */
if (rightoutstr) errflg++;
else rightoutstr = optarg;
break;
case 'a': /* do all atoms */
if (doall|doca) errflg++;
else doall=1;
break;
case 'c': /* do only carbon alpha */
if (doall|doca) errflg++;
else doca=1;
break;
default:
errflg++;
break;
}
}
for(;!cbf_get_getopt_data(opts,&c,NULL,NULL,&optarg);cbf_next_getopt_option(opts)) {
if (!leftinstr) {
leftinstr = optarg;
} else {
if (!rightinstr) {
rightinstr = optarg;
} else {
if (!leftoutstr) {
leftoutstr = optarg;
} else {
if (!rightoutstr) {
rightoutstr = optarg;
} else {
errflg++;
}
}
}
}
}
if (errflg) {
fprintf(stderr,"seqmatch: Usage: \n");
fprintf(stderr,
" seqmatch [-l leftin] [-r rightin] \\\n");
fprintf(stderr,
" [-m leftout] [-s rightout]\\\n");
fprintf(stderr,
" [-a|-c] \\\n");
fprintf(stderr,
" [leftin] [rightin] [leftout] [rightout]\n");
exit(2);
}
if ( cbf_make_handle (&leftincbf) ) {
fprintf(stderr,"Failed to create handle for left input cif\n");
exit(1);
}
if ( cbf_make_handle (&rightincbf) ) {
fprintf(stderr,"Failed to create handle for right input cif\n");
exit(1);
}
if ( cbf_make_handle (&leftoutcbf) ) {
fprintf(stderr,"Failed to create handle for left output cif\n");
exit(1);
}
if ( cbf_make_handle (&rightoutcbf) ) {
fprintf(stderr,"Failed to create handle for right output cif\n");
exit(1);
}
/* Read the leftin cif */
if (!leftinstr || strcmp(leftinstr?leftinstr:"","-") == 0) {
leftintmpstr = (char *)malloc(strlen("/tmp/seqmatchlXXXXXX")+1);
#ifdef NOTMPDIR
strcpy(leftintmpstr, "seqmatchlXXXXXX");
#else
strcpy(leftintmpstr, "/tmp/seqmatchlXXXXXX");
#endif
#ifdef NOMKSTEMP
if ((leftintmpstr = mktemp(leftintmpstr)) == NULL ) {
fprintf(stderr,"\n seqmatch: Can't create temporary file name %s.\n", leftintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
if ( (leftin = fopen(leftintmpstr,"wb+")) == NULL) {
fprintf(stderr,"Can't open temporary file %s.\n", leftintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
#else
if ((leftintmpfd = mkstemp(leftintmpstr)) == -1 ) {
fprintf(stderr,"\n seqmatch: Can't create temporary file %s.\n", leftintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
if ( (leftin = fdopen(leftintmpfd, "w+")) == NULL) {
fprintf(stderr,"Can't open temporary file %s.\n", leftintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
#endif
while ((nbytes = fread(buf, 1, C2CBUFSIZ, stdin))) {
if(nbytes != fwrite(buf, 1, nbytes, leftin)) {
fprintf(stderr,"Failed to write %s.\n", leftintmpstr);
exit(1);
}
}
fclose(leftin);
leftinstr = leftintmpstr;
leftintmpused = 1;
}
/* Read the left input file */
if (!(leftin = fopen (leftinstr, "rb"))) {
fprintf (stderr,"Couldn't open the left input CIF file %s\n", leftinstr);
exit (1);
}
if (leftintmpused) {
if (unlink(leftintmpstr) != 0 ) {
fprintf(stderr,"seqmatch: Can't unlink temporary file %s.\n", leftintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
}
cbf_failnez (cbf_read_widefile (leftincbf, leftin, MSG_DIGEST))
/* Read the rightin cif */
if (!rightinstr || strcmp(rightinstr?rightinstr:"","-") == 0) {
rightintmpstr = (char *)malloc(strlen("/tmp/seqmatchrXXXXXX")+1);
#ifdef NOTMPDIR
strcpy(rightintmpstr, "seqmatchrXXXXXX");
#else
strcpy(rightintmpstr, "/tmp/seqmatchrXXXXXX");
#endif
#ifdef NOMKSTEMP
if ((rightintmpstr = mktemp(rightintmpstr)) == NULL ) {
fprintf(stderr,"\n seqmatch: Can't create temporary file name %s.\n", rightintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
if ( (rightin = fopen(rightintmpstr,"wb+")) == NULL) {
fprintf(stderr,"Can't open temporary file %s.\n", rightintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
#else
if ((rightintmpfd = mkstemp(rightintmpstr)) == -1 ) {
fprintf(stderr,"\n seqmatch: Can't create temporary file %s.\n", rightintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
if ( (rightin = fdopen(rightintmpfd, "w+")) == NULL) {
fprintf(stderr,"Can't open temporary file %s.\n", rightintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
#endif
while ((nbytes = fread(buf, 1, C2CBUFSIZ, stdin))) {
if(nbytes != fwrite(buf, 1, nbytes, rightin)) {
fprintf(stderr,"Failed to write %s.\n", rightintmpstr);
exit(1);
}
}
fclose(rightin);
rightinstr = rightintmpstr;
rightintmpused = 1;
}
/* Read the right input file */
if (!(rightin = fopen (rightinstr, "rb"))) {
fprintf (stderr,"Couldn't open the right input CIF file %s\n", rightinstr);
exit (1);
}
if (rightintmpused) {
if (unlink(rightintmpstr) != 0 ) {
fprintf(stderr,"seqmatch: Can't unlink temporary file %s.\n", rightintmpstr);
fprintf(stderr,"%s\n",strerror(errno));
exit(1);
}
}
cbf_failnez (cbf_read_widefile (rightincbf, rightin, MSG_DIGEST))
leftout = rightout = NULL;
if (!cbf_find_tag(leftincbf,"_entity_poly_seq.entity_id")) {
if (!cbf_find_tag(rightincbf,"_entity_poly_seq.entity_id")) {
} else {
fprintf(stderr,"seqmatch: Unable to find _entity_poly_seq.entity_id in right input %s\n",rightinstr);
exit(1);
}
} else {
fprintf(stderr,"seqmatch: Unable to find _entity_poly_seq.entity_id in left input %s\n",leftinstr);
if (cbf_find_tag(rightincbf,"_entity_poly_seq.entity_id")) {
fprintf(stderr,"seqmatch: Unable to find _entity_poly_seq.entity_id in right input %s\n",rightinstr);
exit(1);
}
}
cbf_failnez(cbf_rewind_row(leftincbf));
cbf_failnez(cbf_rewind_row(rightincbf));
{
const char * lastleftent = NULL;
const char * lastrightent = NULL;
const char * entity, * residue;
unsigned int leftrows, rightrows;
int row;
int seqnum;
cbf_failnez (cbf_count_rows(leftincbf,&leftrows))
for (row=0;row<leftrows;row++) {
cbf_failnez(cbf_select_row(leftincbf,row))
cbf_failnez(cbf_find_column(leftincbf,"entity_id"))
cbf_failnez(cbf_get_value(leftincbf,&entity))
if (!entity) entity="";
if (!lastleftent || cbf_cistrcmp(lastleftent,entity)) {
if (numleftch <256) {
lastleftent = entity;
leftchname[numleftch] = entity;
cbf_failnez(create_chainhandle(leftch+(numleftch++), entity, leftrows))
} else {
fprintf(stderr,"sequence_match: more than 256 chains in leftincbf\n ");
exit(-1);
}
}
cbf_failnez(cbf_find_column(leftincbf,"mon_id"))
cbf_failnez(cbf_get_value(leftincbf,&residue))
cbf_failnez(cbf_find_column(leftincbf,"num"))
cbf_failnez(cbf_get_integervalue(leftincbf,&seqnum))
cbf_failnez(add_residue(leftch[numleftch-1],residue,seqnum))
}
cbf_failnez (cbf_count_rows(rightincbf,&rightrows))
for (row=0;row<rightrows;row++) {
cbf_failnez(cbf_select_row(rightincbf,row))
cbf_failnez(cbf_find_column(rightincbf,"entity_id"))
cbf_failnez(cbf_get_value(rightincbf,&entity))
if (!entity) entity="";
if (!lastrightent || cbf_cistrcmp(lastrightent,entity)) {
if (numrightch <256) {
lastrightent = entity;
rightchname[numrightch] = entity;
cbf_failnez(create_chainhandle(rightch+(numrightch++), entity, rightrows))
} else {
fprintf(stderr,"sequence_match: more than 256 chains in rightincbf\n ");
exit(-1);
}
}
cbf_failnez(cbf_find_column(rightincbf,"mon_id"))
cbf_failnez(cbf_get_value(rightincbf,&residue))
cbf_failnez(cbf_find_column(rightincbf,"num"))
cbf_failnez(cbf_get_integervalue(rightincbf,&seqnum))
cbf_failnez(add_residue(rightch[numrightch-1],residue,seqnum))
}
}
for (iii=0; iii<numleftch; iii++) {
chainhandle chain;
int minresnum, maxresnum;
int resnum;
int index;
chain = leftch[iii];
minresnum = chain->minresnum;
maxresnum = chain->maxresnum;
for (resnum=minresnum; resnum<=maxresnum; resnum++) {
index = -1;
while((index = get_by_resnum(chain, index, resnum))!=-1) {
fprintf(stderr," left chain %d residue %d %s\n",iii,resnum,(chain->residues)[index].resname);
}
}
}
for (iii=0; iii<numrightch; iii++) {
chainhandle chain;
int minresnum, maxresnum;
int resnum;
int index;
chain = rightch[iii];
minresnum = chain->minresnum;
maxresnum = chain->maxresnum;
for (resnum=minresnum; resnum<=maxresnum; resnum++) {
index = -1;
while((index = get_by_resnum(chain, index, resnum))!=-1) {
fprintf(stderr," right chain %d residue %d %s\n",iii,resnum,(chain->residues)[index].resname);
}
}
}
fprintf(stderr,"%d chains in %s, %d chains in %s\n", numleftch, leftinstr, numrightch, rightinstr);
for (iii=0; iii<256; iii++) left_to_right[iii]=-1;
for (iii=0; iii<256; iii++) right_to_left[iii]=-1;
for (iii=0; iii<numleftch; iii++) {
for (jjj=0; jjj<numrightch; jjj++) {
if (right_to_left[jjj]<0) {
int lr, rl;
int countl, countr;
countl = leftch[iii]->maxresnum-leftch[iii]->minresnum+1;
countr = rightch[iii]->maxresnum-rightch[iii]->minresnum+1;
if ((lr=seq_comp(leftch[iii],rightch[jjj])) > 1+countl/3
&& (rl=seq_comp(rightch[jjj],leftch[iii]))> 1+countr/3 ) {left_to_right[iii]=jjj; right_to_left[jjj]=iii;
fprintf(stderr,"matched %d to %d, matching %d on the left and %d on the right \n",iii,jjj, lr, rl);
}
}
}
}
cbf_failnez(cbf_copy_cbf(leftoutcbf,leftincbf,CBF_BYTE_OFFSET,CBF_HDR_FINDDIMS))
cbf_failnez(cbf_copy_cbf(rightoutcbf,rightincbf,CBF_BYTE_OFFSET,CBF_HDR_FINDDIMS))
if (!cbf_find_tag(leftoutcbf,"_atom_site.label_seq_id")) {
if (cbf_find_tag(rightoutcbf,"_atom_site.label_seq_id")) {
fprintf(stderr,"seqmatch: Unable to find _atom_site.label_seq_id in right input %s\n",rightinstr);
exit(1);
}
} else {
fprintf(stderr,"seqmatch: Unable to find _atom_site.label_seq_id in left input %s\n",leftinstr);
if (cbf_find_tag(rightoutcbf,"_atom_site.label_seq_id")) {
fprintf(stderr,"seqmatch: Unable to find _atom_site.label_seq_id in right input %s\n",rightinstr);
exit(1);
}
}
{
int row;
unsigned int leftrows;
int seqnum;
const char * resname;
const char * entity;
const char * atomtype;
const char * atomname;
int iii;
int foundent;
cbf_failnez (cbf_count_rows(leftoutcbf,&leftrows))
for (row=0;row<leftrows;row++) {
cbf_failnez(cbf_select_row(leftoutcbf,row))
/* try to get the residue sequence number
and the residue name */
cbf_failnez(cbf_find_column(leftoutcbf,"label_seq_id"))
if (!cbf_get_integervalue(leftoutcbf,&seqnum)
&&!cbf_find_column(leftoutcbf,"label_comp_id")
&&!cbf_get_value(leftoutcbf,&resname)){
/* try to get the chain identifier */
foundent = 0;
cbf_failnez(cbf_find_column(leftoutcbf,"label_entity_id"))
if (!cbf_get_value(leftoutcbf,&entity)){
for (iii=0; iii<numleftch; iii++) {
if (!cbf_cistrcmp(entity,leftchname[iii])) {
foundent = 1;
if (left_to_right[iii] < 0) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
break;
} else {
int left;
left = -1;
left = get_by_resnum(leftch[iii], left, seqnum );
if (left >=0 && !cbf_cistrcmp((leftch[iii]->residues[left]).resname,resname)) {
if (doca) {
cbf_failnez(cbf_find_column(leftoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomtype))
cbf_failnez(cbf_find_column(leftoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
break;
}
}
break;
}
while (left >=0 && (left=(leftch[iii]->residues[left]).resnumhashchain)>=0) {
if (!cbf_cistrcmp((leftch[iii]->residues[left]).resname,resname)) {
if (doca) {
cbf_failnez(cbf_find_column(leftoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomtype))
cbf_failnez(cbf_find_column(leftoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
break;
}
}
break;
}
}
/* if there is no matching residue on the right, we will just drop
this atom, but even if there is a matching residue, we may be
required to drop to atom if we are only accepting carbon alphas
*/
if (left < 0) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
break;
} else if (doca) {
cbf_failnez(cbf_find_column(leftoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomtype))
cbf_failnez(cbf_find_column(leftoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(leftoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
break;
}
}
}
}
}
}
if (!foundent) {
cbf_failnez(cbf_remove_row(leftoutcbf))
row--;
leftrows--;
}
}
}
}
{
int row;
unsigned int rightrows;
int seqnum;
const char * resname;
const char * entity;
const char * atomtype;
const char * atomname;
int iii;
int foundent;
cbf_failnez (cbf_count_rows(rightoutcbf,&rightrows))
for (row=0;row<rightrows;row++) {
cbf_failnez(cbf_select_row(rightoutcbf,row))
/* try to get the residue sequence number
and the residue name */
cbf_failnez(cbf_find_column(rightoutcbf,"label_seq_id"))
if (!cbf_get_integervalue(rightoutcbf,&seqnum)
&&!cbf_find_column(rightoutcbf,"label_comp_id")
&&!cbf_get_value(rightoutcbf,&resname)){
/* try to get the chain identifier */
foundent = 0;
cbf_failnez(cbf_find_column(rightoutcbf,"label_entity_id"))
if (!cbf_get_value(rightoutcbf,&entity)){
for (iii=0; iii<numrightch; iii++) {
if (!cbf_cistrcmp(entity,rightchname[iii])) {
foundent = 1;
if (right_to_left[iii] < 0) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
break;
} else {
int right;
right = -1;
right = get_by_resnum(rightch[iii], right, seqnum );
if (right >=0 && !cbf_cistrcmp((rightch[iii]->residues[right]).resname,resname)) {
if (doca) {
cbf_failnez(cbf_find_column(rightoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomtype))
cbf_failnez(cbf_find_column(rightoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
break;
}
}
break;
}
while (right >=0 && (right=(rightch[iii]->residues[right]).resnumhashchain)>=0) {
if (!cbf_cistrcmp((rightch[iii]->residues[right]).resname,resname)) {
if (doca) {
cbf_failnez(cbf_find_column(rightoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomtype))
cbf_failnez(cbf_find_column(rightoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
break;
}
}
break;
}
}
/* if there is no matching residue on the right, we will just drop
this atom, but even if there is a matching residue, we may be
required to drop to atom if we are only accepting carbon alphas
*/
if (right < 0) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
break;
} else if (doca) {
cbf_failnez(cbf_find_column(rightoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomtype))
cbf_failnez(cbf_find_column(rightoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(rightoutcbf,&atomname))
if (!atomtype
|| !atomname
|| cbf_cistrcmp(atomtype,"C")
|| cbf_cistrcmp(atomname,"CA")) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
break;
}
}
}
}
}
}
if (!foundent) {
cbf_failnez(cbf_remove_row(rightoutcbf))
row--;
rightrows--;
}
}
}
}
/* Having pruned just to matching residues and possibly just matching
CA, now prune to match all atoms by residue name, atom name and
atom type */
{
int leftrow, rightrow;
unsigned int leftrows, rightrows;
int leftresses, rightresses;
int leftdels, rightdels;
int leftainr, rightainr;
int lastleft, lastright;
const char * leftresname, * rightresname;
const char * leftatomtype, * rightatomtype;
const char * leftatomname, * rightatomname;
int rightresnum, leftresnum;
cbf_failnez (cbf_count_rows(leftoutcbf,&leftrows))
cbf_failnez (cbf_count_rows(rightoutcbf,&rightrows))
leftrow=rightrow=0;
leftdels=rightdels=0;
lastleft = lastright = -999999;
leftainr = rightainr = 0;
leftresses = rightresses = 0;
while (leftrow < leftrows && rightrow < rightrows) {
cbf_failnez(cbf_select_row(leftoutcbf, leftrow))
cbf_failnez(cbf_select_row(rightoutcbf, rightrow))
/* try to get the residue sequence numbers
and the residue names */
cbf_failnez(cbf_find_column(leftoutcbf,"label_seq_id"))
cbf_failnez(cbf_find_column(rightoutcbf,"label_seq_id"))
if (!cbf_get_integervalue(leftoutcbf,&leftresnum)
&&!cbf_find_column(leftoutcbf,"label_comp_id")
&&!cbf_get_value(leftoutcbf,&leftresname)
&&!cbf_get_integervalue(rightoutcbf,&rightresnum)
&&!cbf_find_column(rightoutcbf,"label_comp_id")
&&!cbf_get_value(rightoutcbf,&rightresname)) {
if (leftresnum != lastleft) {
if (leftainr!=0) leftresses++;
lastleft = leftresnum;
leftainr = 0;
}
if (rightresnum != lastright) {
if (rightainr!=0) rightresses++;
lastright = rightresnum;
rightainr = 0;
}
cbf_failnez(cbf_find_column(leftoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(leftoutcbf,&leftatomtype))
cbf_failnez(cbf_find_column(leftoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(leftoutcbf,&leftatomname))
cbf_failnez(cbf_find_column(rightoutcbf,"type_symbol"))
cbf_failnez(cbf_get_value(rightoutcbf,&rightatomtype))
cbf_failnez(cbf_find_column(rightoutcbf,"label_atom_id"))
cbf_failnez(cbf_get_value(rightoutcbf,&rightatomname))
leftainr++;
rightainr++;
if (!cbf_cistrcmp(leftatomtype,rightatomtype)
&&!cbf_cistrcmp(leftatomname,rightatomname)
&&!cbf_cistrcmp(leftresname,rightresname)
&&leftainr==rightainr) {
leftrow++;
rightrow++;
continue;
}
if (!cbf_cistrcmp(leftresname,rightresname)) {
if (leftainr > rightainr) {
cbf_failnez(cbf_remove_row(leftoutcbf))
leftrows--;
leftainr--;
rightainr--;
leftdels++;
continue;
}
if (leftainr < rightainr) {
cbf_failnez(cbf_remove_row(rightoutcbf))
rightrows--;
rightainr--;
leftainr--;
rightdels++;
continue;
}
cbf_failnez(cbf_remove_row(leftoutcbf))
cbf_failnez(cbf_remove_row(rightoutcbf))
leftrows--;
leftainr--;
leftdels++;
rightrows--;
rightainr--;
rightdels++;
continue;
}
if (rightresnum > leftresnum ) {
cbf_failnez(cbf_remove_row(leftoutcbf))
leftrows--;
leftainr--;
rightainr--;
leftdels++;
continue;
}
if (leftresnum > rightresnum ) {
cbf_failnez(cbf_remove_row(rightoutcbf))
rightrows--;
rightainr--;
leftainr--;
rightdels++;
continue;
}
cbf_failnez(cbf_remove_row(leftoutcbf))
cbf_failnez(cbf_remove_row(rightoutcbf))
leftrows--;
leftainr--;
leftdels++;
rightrows--;
rightainr--;
rightdels++;
}
}
while (leftrow < leftrows ) {
cbf_failnez(cbf_select_row(leftoutcbf, leftrow))
cbf_failnez(cbf_remove_row(leftoutcbf))
leftrows--;
}
while (rightrow < rightrows ) {
cbf_failnez(cbf_select_row(rightoutcbf, rightrow))
cbf_failnez(cbf_remove_row(rightoutcbf))
rightrows--;
}
}
if ( ! leftoutstr || strcmp(leftoutstr?leftoutstr:"","-") == 0 ) {
leftout = stdout;
} else if ( strcmp(leftoutstr?leftoutstr:"","/dev/null") ==0 ){
leftdevnull=1;
} else {
leftout = fopen (leftoutstr, "w+b");
}
if ( ! rightoutstr || strcmp(rightoutstr?rightoutstr:"","-") == 0 ) {
rightout = stdout;
} else if ( strcmp(rightoutstr?rightoutstr:"","/dev/null") ==0 ){
rightdevnull=1;
} else {
rightout = fopen (rightoutstr, "w+b");
}
cbf_failnez (cbf_write_widefile (leftoutcbf, leftout, 1, CIF,
MIME_HEADERS|MSG_DIGEST,
0))
cbf_failnez (cbf_write_widefile (rightoutcbf, rightout, 1, CIF,
MIME_HEADERS|MSG_DIGEST,
0))
exit(0);
}
int local_exit (int status)
{
exit(status);
return 1; /* avoid warnings */
}
|