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
|
/* (C) Copyright 1993, Fred Hutchinson Cancer Research Center */
/* Use, modification or distribution of these programs is subject to */
/* the terms of the non-commercial licensing agreement in license.h. */
/* pattern.c: */
/* Written by: Bill Alford */
/* Change log information is at the end of the file. */
/* system headers not in global.h */
#include <assert.h>
/* blimps library headers */
#include <global.h>
#include <files.h>
#include <blocks.h> /* includes sequences.h, output.h */
#include <matrix.h> /* includes pattern.h */
#include <residues.h>
#include <skiplist.h>
/*
* Exported variables and data structures
*/
Boolean UsePatterns = FALSE;
/*
* Local variables and data structures
*/
SkipList Patterns;
Boolean ListInitialized = FALSE;
#define PATTERN_ARRAY_START 30
#define PATTERN_ARRAY_INC 20
/*
* Function definitions
*/
static Boolean headers_match();
static Boolean search_for_pattern();
static Pattern *scan_pattern();
static void find_residues();
static PatternResidue *parse_residue();
/* Never Used */
/* static void print_patterns(); */
/* static void print_pattern(); */
/* static void print_residue(); */
/* Never used */
/* static int print_residue_for_list(); */
/*
* scan_patterns
*
* scans in the pattern entries from the pattern file for the given
* matrix and puts them in the matrix.
*
*/
void
scan_patterns(matrix)
Matrix *matrix;
{
int max_patterns_for_array = 0;
int num_patterns_seen = 0;
Boolean cont;
Pattern **patterns;
Pattern **tmp_ptr;
SkipList residue_list;
char *pat;
FILE *pat_file;
if (! UsePatterns) {
matrix->patterns = NULL;
return;
}
/* is the pattern already here? */
if (matrix->patterns != NULL) {
/* if so, then do nothing */
return;
}
else {
/* otherwise, we need to read in the patterns */
/* allocate space for the array of pointers to patterns */
CheckMem(
patterns = (Pattern **) malloc(sizeof(Pattern *) * PATTERN_ARRAY_START)
);
max_patterns_for_array = PATTERN_ARRAY_START;
num_patterns_seen = 0;
/* prepare the residue list for storing residues. */
/* this is done outside of the call to scan_pattern() so as to cut */
/* down on initialization/cleanup time */
/* Note that Patterns is global to save some startup/cleanup time */
if (! ListInitialized) {
Patterns = NewSL(residue_compare_function, NULL, ALLOW_DUPLICATES);
ListInitialized = TRUE;
}
residue_list = Patterns;
/*printf("Searching for: %s\n", matrix->number);*/
/* find the pattern entry that matches the PSSM */
if (! headers_match(matrix)) {
/*printf("Pattern not found\n");*/
rewind_file(PATTERN_FILES);
if (! search_for_pattern(matrix)) {
sprintf(ErrorBuffer,
"Unable to find pattern for PSSM %s\n", matrix->number);
ErrorReport(WARNING_ERR_LVL);
matrix->patterns = NULL;
return;
}
}
/* file should be positioned somewhere in the pattern header */
cont = FALSE;
pat_file = get_file(PATTERN_FILES);
/* there should be something there regardless */
/* probably should mention bad file format */
assert(pat_file != NULL);
/* scan until after the PA line */
while (fgets(Buffer, LARGE_BUFF_LENGTH, pat_file) &&
!((Buffer[0] == 'P') && (Buffer[1] == 'A')));
/* read in each pattern line until "//" is seen */
cont = TRUE; do {
/* read in a line */
fgets(Buffer, LARGE_BUFF_LENGTH, pat_file);
/* if it is "//" then we are done */
if ((Buffer[0] == '/') && (Buffer[1] == '/')) {
cont = FALSE;
}
else {
/* shift to the beginning of the pattern */
pat = get_token(Buffer);
/* put a null at the end of the pattern */
remove_trailing_whitespace(pat);
/* increase the size if need be */
/* (adding one early to allow room for the NULL ptr */
if (num_patterns_seen >= (max_patterns_for_array-1)) {
max_patterns_for_array += PATTERN_ARRAY_INC;
CheckMem(
tmp_ptr = (Pattern **) realloc(patterns,
max_patterns_for_array *
sizeof(Pattern *))
);
patterns = tmp_ptr;
}
/* make null for safety */
patterns[num_patterns_seen] = NULL;
patterns[num_patterns_seen+1] = NULL;
/* scan in this pattern and place it in the array */
patterns[num_patterns_seen] = scan_pattern(pat, residue_list);
/* only move ahead if something was scanned */
if (patterns[num_patterns_seen] != NULL) {
num_patterns_seen++;
}
}
} while (cont);
/* make the last position NULL */
patterns[num_patterns_seen] = NULL;
/* the patterns have been read in and we can update the matrix */
matrix->patterns = patterns;
/*print_patterns(matrix->patterns);*/
assert(NumInSL(residue_list) == 0);
}
}
Boolean
pattern_matches(seq, compare_start, pattern)
Sequence *seq;
int compare_start;
Pattern *pattern;
{
Residue *sequence;
int seq_length;
PatternResidue *p;
Boolean ok=FALSE;
int x, num, offset;
sequence = seq->sequence;
seq_length = seq->length;
/* optimize for the common case of one residue */
if (pattern->num_residues == 1) {
/* get first residue */
p = pattern->pat;
/* check the residue(s) */
offset = p->offset;
/* Is this an error? ok is never set */
/* only check if it is ok if it is within the sequence */
if (((compare_start+offset) < seq_length) &&
((compare_start+offset) >= 0)) {
num = p->num_residues;
/* optimize for the common case by only checking the one */
if (num == 1) {
/* this saves an assignment and an additional compare and a
loop back*/
if (aa_btoa[sequence[compare_start+offset]] ==
toupper(p->residues[0])) {
/* can return true here since there is only one residue */
return TRUE;
}
}
else {
for (x=0; !ok && x<num; x++) {
if (aa_btoa[sequence[compare_start+offset]] ==
toupper(p->residues[x])) {
/* can return true here since there is only one residue */
return TRUE;
}
}
}
/* if we have reached this point nothing matched */
return FALSE;
}
}
/* otherwise do the general case */
else {
/* get first residue */
p = pattern->pat;
/* while the residue is there (the pointer != NULL) */
while (p != NULL) {
/* check the residue(s) */
offset = p->offset;
/* only check if it is ok if it is within the sequence */
if (((compare_start+offset) < seq_length) &&
((compare_start+offset) >= 0)) {
ok = FALSE;
num = p->num_residues;
/* optimize for the common case by only checking the one */
if (num == 1) {
/* this saves an assignment and an additional compare and a
loop back*/
if (aa_btoa[sequence[compare_start+offset]] ==
toupper(p->residues[0])) {
ok = TRUE;
}
}
else {
for (x=0; !ok && x<num; x++) {
if (aa_btoa[sequence[compare_start+offset]] ==
toupper(p->residues[x])) {
ok = TRUE;
}
}
}
/* if it/they didn't match, return FALSE */
if (! ok) {
return FALSE;
}
}
/* get the next residue */
p = p->next;
}
}
return TRUE;
}
/*************************************************************************
*************************************************************************
*
* Local Functions
*
*************************************************************************
*************************************************************************
*/
/*
* residue_compare_function
*
* compare two residue structures. This is for the skiplist ordering.
* The shorter residues are placed first, then they are sorted by
* alphabetical order.
*
*/
int
residue_compare_function(res1, res2)
PatternResidue *res1;
PatternResidue *res2;
{
if (res1->num_residues != res2->num_residues) {
return res1->num_residues - res2->num_residues;
}
else {
return strcmp(res1->residues, res2->residues);
}
assert(0);
}
/*
* headers_match
*
* scan the pattern file from the current point and if the matrix and
* header match (have the same base name) return true.
*
*/
static Boolean
headers_match(matrix)
Matrix *matrix;
{
char *tmp;
FILE *pat_file;
/* foreach of the pattern files */
while ((pat_file = get_file(PATTERN_FILES)) != NULL) {
while (fgets(Buffer, LARGE_BUFF_LENGTH, pat_file) && !feof(pat_file)) {
/* if we are past the header, stop and return FALSE */
if ((Buffer[0] == '/') && (Buffer[1] == '/')) {
return FALSE;
}
/* otherwise, if it is the AC line, check */
else if ((Buffer[0] == 'A') && (Buffer[1] == 'C')) {
/* get the number of the pattern. Be sure to truncate the ';' */
tmp = get_token(Buffer);
tmp = get_token(NULL);
if (tmp[strlen(tmp)-1] == ';') {
tmp[strlen(tmp)-1] = '\0';
}
/* must skip over the BL/MA stuff (so add two) */
return (strcmp(tmp+2, (matrix->number)+2) == 0);
}
/* otherwise, skip it and go to the next line */
}
}
/* Should never see this */
return FALSE;
}
/*
* search_for_pattern
*
* scan from the current file position in the pattern file searching
* for the pattern. If none found, return FALSE, otherwise return
* TRUE.
*
*/
static Boolean
search_for_pattern(matrix)
Matrix *matrix;
{
FILE *pat_file;
char *tmp;
/* foreach of the pattern files */
while ((pat_file = get_file(PATTERN_FILES)) != NULL) {
while (fgets(Buffer, LARGE_BUFF_LENGTH, pat_file) && !feof(pat_file)) {
if ((Buffer[0] == 'A') && (Buffer[1] == 'C')) {
/* get the number of the pattern. Be sure to truncate the ';' */
tmp = get_token(Buffer);
tmp = get_token(NULL);
if (tmp[strlen(tmp)-1] == ';') {
tmp[strlen(tmp)-1] = '\0';
}
if ((strcmp(tmp+2, (matrix->number)+2) == 0)) {
return TRUE;
}
}
}
}
/* reached the end of the file, so return FALSE */
return FALSE;
}
/*
* scan_pattern
*
* scan the current pattern (the string is only stuff in the pattern)
* and return a pointer to the pattern data structure
*
* NULL is returned if there are no residues for the pattern
*
*/
static Pattern *
scan_pattern(pat, residue_list)
char *pat;
SkipList residue_list; /* declared externally for reuse to save time */
/* it should start and end empty */
{
Pattern *pattern;
PatternResidue *p, *s;
int start;
assert(NumInSL(residue_list) == 0);
CheckMem(
pattern = (Pattern *) malloc(sizeof(Pattern))
);
/* get all the residues in the pattern and place them in the ordered list */
find_residues(pat, residue_list);
/* the list is now full of residues */
/* prints the contents of the ordered residues list */
/* DoForSL(residue_list, print_residue_for_list, NULL); */
if (NumInSL(residue_list) == 0) {
free(pattern);
return NULL;
}
pattern->num_residues = NumInSL(residue_list);
p = Nth(residue_list, 0);
DeleteSL(residue_list, p);
start = p->offset;
p->offset = 0;
pattern->pat = p;
pattern->beg_offset = start;
while (NumInSL(residue_list) != 0) {
s = Nth(residue_list, 0);
DeleteSL(residue_list, s);
s->offset = s->offset - start;
p->next = s;
p = s;
}
p->next = NULL; /* just being safe */
assert(NumInSL(residue_list) == 0);
return pattern;
}
static void
find_residues(pat, residue_list)
char *pat;
SkipList residue_list; /* to fill with the residues */
{
int offset = 0;
PatternResidue *patres;
while (pat[0] != '\0') {
if ((pat[0] != 'x') && (pat[0] != 'X')) {
patres = parse_residue(offset, &pat);
InsertSL(residue_list, patres);
}
offset++;
pat++;
}
}
/*
*
* parse_residue
*
* creates a pattern residue to be added to the pattern. The residue
* at the current possition is the one used. THIS DOES NOT CHECK THAT
* THE RESIDUE IS A VALID RESIDUE (eg 'X').
*
*/
static PatternResidue *
parse_residue(offset, pat)
int offset;
char **pat; /* this is so that the possition in the */
/* pattern string is updated */
{
PatternResidue *pr;
int num = 0;
char *tmp;
if (**pat != '[') { /* it is only one residue */
/* declare space for the pattern residue and the character string */
CheckMem(
pr = (PatternResidue *) malloc(sizeof(PatternResidue))
);
CheckMem(
pr->residues = (char *) malloc(sizeof(char)*2)
);
/* initialize the structure */
pr->offset = offset;
pr->num_residues = 1;
pr->next = NULL;
/* set the residues string */
pr->residues[0] = **pat;
pr->residues[1] = '\0';
/* don't move the move the pattern pointer/marker ahead since it
is done once in the calling function. */
return pr;
}
else { /* it is more than one residue */
num = 0;
tmp = *pat;
tmp++; /* move over the '[' */
while (*tmp != ']') {
num++;
tmp++;
}
*tmp = '\0'; /* make place a null to end the residues */
/* declare space for the pattern residue and the character string */
CheckMem(
pr = (PatternResidue *) malloc(sizeof(PatternResidue))
);
CheckMem(
pr->residues = (char *) malloc(sizeof(char)*(num+1))
);
/* initialize the structure */
pr->offset = offset;
pr->num_residues = num;
pr->next = NULL;
/* set the residues string */
strcpy(pr->residues, (*pat)+1);
pr->residues[num] = '\0';
/* move the pattern pointer/marker ahead, all but one. It is
moved ahead one position automatically */
*pat = *pat + 1 + num;
return pr;
}
assert(0); /* shouldn't get here */
}
/* never used */
/*
static void
print_patterns(Pattern **patterns)
{
int x;
for (x = 0; patterns[x] != NULL; x++) {
printf("%d: ", x);
print_pattern(patterns[x]);
}
}
static void
print_pattern(pattern)
Pattern *pattern;
{
if (pattern == NULL) {
printf("(null)\n");
}
printf("%d: ", pattern->beg_offset);
print_residue(pattern->pat);
}
*/
/*
void
static print_residue(pat)
PatternResidue *pat;
{
if (pat == NULL) {
printf(";\n");
return;
}
printf("(%d, [%s]) ", pat->offset, pat->residues);
print_residue(pat->next);
}
*/
/*
int
static print_residue_for_list(pat, arg)
PatternResidue *pat;
void *arg;
{
print_residue(pat);
return SL_CONTINUE;
}
*/
/* Change log information follows.
* $Log: pattern.c,v $
* Revision 1.2 2011-09-28 22:10:55 gsims
* *** empty log message ***
*
* Revision 1.1 2011-05-24 16:32:28 gsims
* *** empty log message ***
*
* Revision 1.1.1.1 2011-02-22 22:16:00 gsims
* Initial Import of sift 4.0.4
*
* Revision 2.2010 1995/07/28 23:47:14 billa
* Added new convert method and pattern matching and minor updates merged.
*
*/
|