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
|
static char const rcsid[] = "$Id: makemat.c,v 6.17 2005/07/28 14:52:22 coulouri Exp $";
/*
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*/
/*****************************************************************************
File name: makemat.c
Author: Alejandro Schaffer
Contents: main routines for makematrices program to convert
PSI-BLAST checkpoints into score matrices.
*****************************************************************************/
#include <ncbi.h>
#include <objseq.h>
#include <objsset.h>
#include <sequtil.h>
#include <seqport.h>
#include <tofasta.h>
#include <blast.h>
#include <blastpri.h>
#include <txalign.h>
#include <simutil.h>
#include <posit.h>
#include <profiles.h>
#include <sqnutils.h>
/*counts the number of items in sequencesFile and matricesFile, assumed to
be one per line, and checks that the numbers are equal.
returns the number if equal, 0 if unequal, rewinds the file descriptors
before returning*/
static Int4 countProfiles(FILE *sequencesFile, FILE *profilesFile)
{
Int4 sequencesCount = 0; /*count for sequencesFile*/
Int4 matricesCount = 0; /*count for profilesFile*/
Char oneFileName[MAXLINELEN]; /*for reading one line per file*/
while (fgets(oneFileName,MAXLINELEN,sequencesFile))
sequencesCount++;
while (fgets(oneFileName,MAXLINELEN,profilesFile))
matricesCount++;
rewind(profilesFile);
rewind(sequencesFile);
if (sequencesCount == matricesCount) {
return(sequencesCount);
} else {
ErrPostEx(SEV_FATAL, 1, 0, "profiles: Sequences file has %ld entries; Matrices file has %d entries; these should be equal\n", (long) sequencesCount,matricesCount);
return(0);
}
}
/*converts name of profile file to matrix file by changing
suffix to mtx or appending suffix mtx*/
static Char *makeMatrixName(Char *profileName)
{
int length; /*length of a name*/
Char *returnName; /*string to treturn*/
int c, lastc; /*loop indices*/
length = strlen(profileName);
returnName = (Char *) MemNew((length + 5) * sizeof(Char));
for(c = 0; c < length; c++) {
returnName[c] = profileName[c];
if(('.' == profileName[c]) && ('c' == profileName[c+1])
&& ('h' == profileName[c+2]))
lastc = c;
}
returnName[lastc] = '.';
returnName[lastc+1] = 'm';
returnName[lastc+2] = 't';
returnName[lastc+3] = 'x';
returnName[lastc+4] = '\0';
return(returnName);
}
/*print out some parameters associated with a Karlin-Alschul
scoring system
checkFile is the file descriptor to write to
kbp is the pointer to a structure with the parameters
scaling determines whether scores are being scaled or not
scalingDown is 1/scalingFactor because if scores are
scaled up, then Lambda is to be scaled down*/
static void putMatrixKbp(FILE * checkFile, BLAST_KarlinBlkPtr kbp, Boolean scaling, Nlm_FloatHi scalingDown)
{
if (scaling)
fprintf(checkFile,"%le\n",kbp->Lambda * scalingDown);
else
fprintf(checkFile,"%le\n",kbp->Lambda);
fprintf(checkFile,"%le\n",kbp->K);
fprintf(checkFile,"%le\n",kbp->logK);
fprintf(checkFile,"%le\n",kbp->H);
}
/*print out a score matrix into the file descriptor checkfile
compactSerarch and psoSearch stroe information about the
matrix and the associated sequence
scaleScores determines whether scores are scaled or not*/
static void putMatrixMatrix(FILE *checkFile, compactSearchItems * compactSearch, posSearchItems *posSearch, Boolean scaleScores)
{
Int4 i, j; /*loop indices*/
if (scaleScores) {
for(i = 0; i < compactSearch->qlength; i++) {
for(j = 0; j < compactSearch->alphabetSize; j++)
fprintf(checkFile,"%ld ", (long) posSearch->posPrivateMatrix[i][j]);
fprintf(checkFile,"\n");
}
}
else {
for(i = 0; i < compactSearch->qlength; i++) {
for(j = 0; j < compactSearch->alphabetSize; j++)
fprintf(checkFile,"%ld ", (long) posSearch->posMatrix[i][j]);
fprintf(checkFile,"\n");
}
}
}
/*Write out the matrix
compactSearch and PosSearch include fields that store the matrix
and the sequence
sbp includes information about the underlying
matrix
fileName is where the matrix is to be written
error_return holds error messages
scaleScores indicates whether scores in the matrix are to be scaled
scalingFactor is the multiplicative factor to use if scaleScores
is true */
static Boolean takeMatrixCheckpoint(compactSearchItems * compactSearch,
posSearchItems *posSearch, BLAST_ScoreBlkPtr sbp,
Char *fileName,ValNodePtr *error_return, Boolean scaleScores, Nlm_FloatHi
scalingFactor)
{
FILE * checkFile; /*file in which to take the checkpoint*/
Int4 length; /*length of query sequence, and an index for it*/
Int4 i; /*indices to position and alphabet */
Char localChar; /*temporary character*/
checkFile = FileOpen(fileName, "w");
if (NULL == checkFile) {
ErrPostEx(SEV_ERROR, 0,0, "Could not open checkpoint file");
return(FALSE);
}
length = compactSearch->qlength;
fprintf(checkFile,"%ld\n",(long) length);
for(i = 0; i < length; i++) {
localChar = getRes(compactSearch->query[i]);
fprintf(checkFile,"%c",localChar);
/* The following 2 lines are needed to preserve compatibility with the
* checkpoint file libraries distributed with IMPALA (from personal
* communication with IMPALA's author) */
posSearch->posMatrix[i][Xchar] = Xscore;
posSearch->posPrivateMatrix[i][Xchar] = Xscore * scalingFactor;
}
fprintf(checkFile,"\n");
putMatrixKbp(checkFile, compactSearch->kbp_gap_std[0], scaleScores, 1/scalingFactor);
putMatrixKbp(checkFile, compactSearch->kbp_gap_psi[0], scaleScores, 1/scalingFactor);
putMatrixKbp(checkFile, sbp->kbp_ideal, scaleScores, 1/scalingFactor);
putMatrixMatrix(checkFile, compactSearch, posSearch, scaleScores);
FileClose(checkFile);
return(TRUE);
}
/*convert to matrices is the high-level procedure to convert a set of
PSI-BLAST checkpoints into a corresponding set of score matrices
profilesFile is a descriptor to a file listing the file names of files
containing checkpoints, one file name per line
sequencesFile is a descriptor to a file listing the file names of sequences
containing the master sequences for checkpoints, one file name per line
matricesFile is an output file to print out the names of the newly
produced files containing score matrices, one file name per line
auxiliaryFile is an outputput file to contain some general information
about the library of matrices and some parameters for each matrix
count is the number of checkpoints/sequences
gap_open is the cost of opening a gap
gap_extend is the cost of extending a gap
effectiveSize is the the size of the original sequence database used to
make the PSI-BLAST matrices
underlyignMatrixName is the original score matrix used to make the
PSI-BLAST matrices
scaleScores indicates whether scores should be scaled
scalingFactor indicates by how much scores are scaled, if at all*/
static Int4 convertToMatrices(FILE *profilesFile, FILE *sequencesFile, FILE *matricesFile, FILE *auxiliaryFile, Int4 count, Int4 gap_open, Int4 gap_extend,
Int4 effectiveSize, Char *underlyingMatrixName, Boolean scaleScores,
Nlm_FloatHi scalingFactor, Char *directoryPrefix)
{
int i; /*loop index over profiles*/
FILE *thisProfileFile, *thisSequenceFile; /*file
descriptors for a single profile*/
Char profileFileName[MAX_NAME_LENGTH], sequenceFileName[MAX_NAME_LENGTH];
/*file names for profiles*/
Char * matrixFileName, *relativeMatrixFileName; /*file name for corresponding matrix file*/
Char relativeProfileFileName[MAX_NAME_LENGTH], relativeSequenceFileName[MAX_NAME_LENGTH];
Int4 prefixLength; /*length of directoryPrefix*/
Int4 c1,c2; /*indices over characters in names*/
posSearchItems *posSearch; /*used to store matrix*/
Uint1Ptr query =NULL; /*query sequence read in*/
Int4 queryLength; /*length of query sequence*/
Int4 c; /*index over query*/
compactSearchItems *compactSearch; /*stores query related items*/
ValNodePtr error_return; /*stores error messages*/
Boolean success; /*did one checkpoint recovery succeed*/
BLAST_ResFreqPtr stdrfp; /* gets standard frequencies in prob field */
Int4 a; /*index over characters*/
SeqCodeTablePtr sctp;
BLAST_ScoreBlkPtr sbp;
BioseqPtr query_bsp; /*structure to hold query information*/
SeqEntryPtr sep; /*structure to hold query retrieval result*/
Int4 *lengthArray; /*array of sequence lengths*/
Nlm_FloatHi *KArray; /*array of K values, one per sequence*/
Int4 maxLength; /*maximum length of a sequence*/
Int4 KarlinReturn; /*return value from calls to set up matrix parameters*/
error_return = NULL;
posSearch = (posSearchItems *) MemNew (1 * sizeof(posSearchItems));
compactSearch = (compactSearchItems *) MemNew (1 * sizeof(compactSearchItems));
sctp = SeqCodeTableFindObj(Seq_code_ncbistdaa);
compactSearch->alphabetSize = sctp->num;
fprintf(auxiliaryFile,"%s\n",underlyingMatrixName);
fprintf(auxiliaryFile,"%ld\n",(long) gap_open);
fprintf(auxiliaryFile,"%ld\n",(long) gap_extend);
lengthArray = (Int4 *) MemNew(count * sizeof(Int4));
KArray = (Nlm_FloatHi *) MemNew(count * sizeof(Nlm_FloatHi));
maxLength = 0;
if ('\0' != directoryPrefix[0]) {
strcpy(profileFileName, directoryPrefix);
strcpy(sequenceFileName, directoryPrefix);
prefixLength = strlen(directoryPrefix);
}
posSearch->stdFreqRatios =
PSIMatrixFrequencyRatiosNew(underlyingMatrixName);
for(i = 0; i < count; i++) {
if ('\0' == directoryPrefix[0])
fscanf(profilesFile,"%s", profileFileName);
else {
fscanf(profilesFile,"%s", relativeProfileFileName);
for(c1 = prefixLength, c2 = 0; relativeProfileFileName[c2] != '\0';
c1++, c2++)
profileFileName[c1] = relativeProfileFileName[c2];
profileFileName[c1] = '\0';
}
if ((thisProfileFile = FileOpen(profileFileName, "rb")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open file %s\n", profileFileName);
return (1);
}
if ('\0' == directoryPrefix[0])
fscanf(sequencesFile,"%s", sequenceFileName);
else {
fscanf(sequencesFile,"%s", relativeSequenceFileName);
for(c1 = prefixLength, c2 = 0; relativeSequenceFileName[c2] != '\0';
c1++, c2++)
sequenceFileName[c1] = relativeSequenceFileName[c2];
sequenceFileName[c1] = '\0';
}
if ((thisSequenceFile = FileOpen(sequenceFileName, "r")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open file %s\n", sequenceFileName);
return (1);
}
sep = FastaToSeqEntryEx(thisSequenceFile, FALSE, NULL, FALSE);
if (sep != NULL) {
query_bsp = NULL;
SeqEntryExplore(sep, &query_bsp, FindProt);
if (query_bsp == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to obtain bioseq\n");
return 2;
}
query = BlastGetSequenceFromBioseq(query_bsp, &queryLength);
}
compactSearch->query = query;
for (c= 0; c < queryLength; c++)
query[c] = ResToInt(query[c]);
compactSearch->qlength = queryLength;
sbp = BLAST_ScoreBlkNew(Seq_code_ncbistdaa, 1);
sbp->read_in_matrix = TRUE;
sbp->protein_alphabet = TRUE;
sbp->posMatrix = NULL;
sbp->number_of_contexts = 1;
BlastScoreBlkMatFill(sbp, underlyingMatrixName);
compactSearch->matrix = sbp->matrix;
compactSearch->gapped_calculation = TRUE;
/* Note that these two assignments are not really needed for
* makemat's operation and thus their values are irrelevant */
compactSearch->pseudoCountConst = 10;
compactSearch->ethresh = 0.001;
BlastScoreBlkFill(sbp, (CharPtr) query, queryLength, 0);
if (0 == i) {
fprintf(auxiliaryFile, "%le\n", sbp->kbp_std[0]->K);
fprintf(auxiliaryFile, "%le\n", sbp->kbp_std[0]->H);
}
sbp->kbp_gap_std[0] = BlastKarlinBlkCreate();
KarlinReturn = BlastKarlinBlkGappedCalc(sbp->kbp_gap_std[0], gap_open, gap_extend,
sbp->name, &error_return);
if (1 == KarlinReturn) {
BlastErrorPrint(error_return);
return(-1);
}
sbp->kbp_gap_psi[0] = BlastKarlinBlkCreate();
KarlinReturn = BlastKarlinBlkGappedCalc(sbp->kbp_gap_psi[0], gap_open, gap_extend,
sbp->name, &error_return);
if (1 == KarlinReturn) {
BlastErrorPrint(error_return);
return(-1);
}
if (sbp->kbp_ideal == NULL)
sbp->kbp_ideal = BlastKarlinBlkStandardCalcEx(sbp);
compactSearch->lambda = sbp->kbp_gap_std[0]->Lambda;
compactSearch->kbp_std = sbp->kbp_std;
compactSearch->kbp_psi = sbp->kbp_psi;
compactSearch->kbp_gap_psi = sbp->kbp_gap_psi;
compactSearch->kbp_gap_std = sbp->kbp_gap_std;
compactSearch->lambda_ideal = sbp->kbp_ideal->Lambda;
compactSearch->K_ideal = sbp->kbp_ideal->K;
stdrfp = BlastResFreqNew(sbp);
BlastResFreqStdComp(sbp,stdrfp);
compactSearch->standardProb = MemNew(compactSearch->alphabetSize * sizeof(Nlm_FloatHi));
if (NULL == compactSearch->standardProb)
exit(EXIT_FAILURE);
for(a = 0; a < compactSearch->alphabetSize; a++)
compactSearch->standardProb[a] = stdrfp->prob[a];
stdrfp = BlastResFreqDestruct(stdrfp);
posSearch->posInformation = NULL;
success = impalaReadCheckpoint(posSearch, compactSearch, profileFileName, &error_return, scalingFactor);
if (!success) {
ErrPostEx(SEV_FATAL, 1,0, "Unable to recover checkpoint from %s\n",profileFileName);
return(1);
}
/*conversion to matrix and scaling is done in impalaReadCheckpopint*/
if ('\0' == directoryPrefix[0]) {
matrixFileName = makeMatrixName(profileFileName);
fprintf(matricesFile,"%s\n",matrixFileName);
} else {
matrixFileName = makeMatrixName(profileFileName);
relativeMatrixFileName = makeMatrixName(relativeProfileFileName);
fprintf(matricesFile,"%s\n",relativeMatrixFileName);
}
success = takeMatrixCheckpoint(compactSearch, posSearch, sbp, matrixFileName, &error_return, scaleScores, scalingFactor);
if (!success) {
ErrPostEx(SEV_FATAL, 1,0, "Unable to take matrix checkpoint from %s\n",profileFileName);
return(1);
}
lengthArray[i] = queryLength;
KArray[i] = sbp->kbp_gap_psi[0]->K;
if (lengthArray[i] > maxLength)
maxLength = lengthArray[i];
posCheckpointFreeMemory(posSearch, queryLength);
FileClose(thisProfileFile);
thisProfileFile = NULL;
FileClose(thisSequenceFile);
thisSequenceFile = NULL;
MemFree(query);
SeqEntryFree(sep);
sbp = BLAST_ScoreBlkDestruct(sbp);
compactSearch->standardProb = MemFree(compactSearch->standardProb);
if (success) {
MemFree(matrixFileName);
if ('\0' != directoryPrefix[0])
MemFree(relativeMatrixFileName);
}
}
fprintf(auxiliaryFile, "%ld\n", (long) maxLength);
fprintf(auxiliaryFile, "%ld\n", (long) effectiveSize);
fprintf(auxiliaryFile, "%lf\n", scalingFactor);
for(i = 0; i < count; i++) {
fprintf(auxiliaryFile, "%ld\n", (long) lengthArray[i]);
fprintf(auxiliaryFile, "%le\n", KArray[i]);
}
MemFree(KArray);
MemFree(lengthArray);
FileClose(profilesFile);
FileClose(sequencesFile);
FileClose(matricesFile);
FileClose(auxiliaryFile);
compactSearchDestruct(compactSearch);
PSIMatrixFrequencyRatiosFree(posSearch->stdFreqRatios);
MemFree(posSearch);
BLAST_ScoreBlkDestruct(sbp);
return(0);
}
#define NUMARG 8
static Args myargs [NUMARG] = {
{ "Database name for profile database",
"stdin", NULL, NULL, FALSE, 'P', ARG_FILE_IN, 0.0, 0, NULL},
{ "Cost to open a gap",
"11", NULL, NULL, FALSE, 'G', ARG_INT, 0.0, 0, NULL},
{ "Cost to extend a gap",
"1", NULL, NULL, FALSE, 'E', ARG_INT, 0.0, 0, NULL},
{ "Underlying Matrix",
"BLOSUM62", NULL, NULL, FALSE, 'U', ARG_STRING, 0.0, 0, NULL},
{ "Underlying sequence database used to make profiles",
"nr", NULL, NULL, FALSE, 'd', ARG_STRING, 0.0, 0, NULL},
{ "Effective length of the profile database (0 for length of -d option)",
"0", NULL, NULL, FALSE, 'z', ARG_INT, 0.0, 0, NULL},
{ "Scaling factor for matrix outputs to avoid round-off problems",
"100.0", NULL, NULL, FALSE, 'S', ARG_FLOAT, 0.0, 0, NULL},
{ "Print help; overrides all other arguments",
"F", NULL, NULL, FALSE, 'H', ARG_BOOLEAN, 0.0, 0, NULL}
};
Int2 Main(void)
{
Char *profilesDatabase;
Char profilesFileName[MAX_NAME_LENGTH];
Char sequencesFileName[MAX_NAME_LENGTH];
Char matricesFileName[MAX_NAME_LENGTH];
Char auxiliaryFileName[MAX_NAME_LENGTH];
Char mmapFileName[MAX_NAME_LENGTH];
FILE *profilesFile, *sequencesFile, *matricesFile, *auxiliaryFile;
Int4 count; /*how many profiles*/
Int4 effSize; /*effective database size to use*/
Int4 retcode;
ReadDBFILEPtr rdpt=NULL; /*holds result of attempt to read database*/
Boolean scaling; /*are score matrix values going to be scaled*/
Char *directoryPrefix; /*directory where profile library is kept, used
to reach other directories indirectly*/
if (! GetArgs ("makematrices", NUMARG, myargs)) {
return (1);
}
if (! SeqEntryLoad())
return (1);
UseLocalAsnloadDataAndErrMsg();
ErrSetLogLevel(SEV_WARNING);
if ((Boolean) myargs[7].intvalue) {
IMPALAPrintHelp(FALSE, 80, "makemat", stdout);
return(1);
}
profilesDatabase = myargs[0].strvalue;
directoryPrefix = (Char *) MemNew(MAX_NAME_LENGTH *sizeof(char));
strcpy(directoryPrefix,profilesDatabase);
impalaMakeFileNames(profilesDatabase,auxiliaryFileName,
mmapFileName,sequencesFileName,matricesFileName,
profilesFileName, directoryPrefix);
if ((profilesFile = FileOpen(profilesFileName, "r")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open profiles file %s\n", profilesFileName);
return (1);
}
if ((sequencesFile = FileOpen(sequencesFileName, "r")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open sequences file %s\n", sequencesFileName);
return (1);
}
if ((matricesFile = FileOpen(matricesFileName, "w")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open matrices file %s\n", matricesFileName);
return (1);
}
if ((auxiliaryFile = FileOpen(auxiliaryFileName, "w")) == NULL) {
ErrPostEx(SEV_FATAL, 1, 0, "Unable to open auxiliary file %s\n", auxiliaryFileName);
return (1);
}
effSize = myargs[5].intvalue;
if (0 == effSize) {
rdpt = readdb_new(myargs[4].strvalue, TRUE);
effSize = readdb_get_dblen(rdpt);
rdpt = readdb_destruct(rdpt);
}
count = countProfiles(sequencesFile, profilesFile);
scaling = ((myargs[6].floatvalue < 0.99) ||
(myargs[6].floatvalue > 1.01));
retcode = convertToMatrices(profilesFile, sequencesFile, matricesFile,
auxiliaryFile, count, myargs[1].intvalue,
myargs[2].intvalue, effSize, myargs[3].strvalue,
scaling, myargs[6].floatvalue, directoryPrefix);
MemFree(directoryPrefix);
return retcode;
}
|