File: mmdbFF.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (277 lines) | stat: -rw-r--r-- 7,642 bytes parent folder | download | duplicates (13)
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
/* $Id: mmdbFF.c,v 6.4 2000/06/20 20:47:03 lewisg Exp $
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  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 do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  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.
*
* ===========================================================================
*
* File Name:  mmdbFF.c - file based retrieval interface
* 
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
* $Log: mmdbFF.c,v $
* Revision 6.4  2000/06/20 20:47:03  lewisg
* use gzip
*
* Revision 6.3  1999/09/08 18:43:54  zimmerma
* Modified HashPDBCode with calls to "toupper" to ensure case-insensitivity
*
* Revision 6.2  1999/06/22 22:35:02  kimelman
* function names were changed to original names
* so we have now 3 version of access files again
*
* Revision 6.1  1999/05/11 21:31:07  kimelman
* file based retrival funciton extracted here from mmdblocl.c
* (function names slightly changed)
*
*
*/

/* LoadMMDBIdx reads the local MMDB index "mmdb.idx" into memory */
/* index format is
3\n
1 1ABC\n
3 3INS\n
234567 4GWA\n
eof
*/

#include <ncbi.h>
#include <mmdbapi1.h>
#include <mmdbdata.h>
#include <mmdblocl.h>
#include <assert.h>

static Int4Ptr pI4Mmdbidx = NULL;
static Uint4Ptr pU4Pdbidx = NULL;
static long iMMDBSize = 0;
static Char indexname[PATH_MAX];
static Char database[PATH_MAX];
static Int4Ptr pI4position = NULL;
static Char gunzip[PATH_MAX];


/* These functions are only required when working with flat files:
 * - Load/Remove the mmbd.idx file for cross referencing MMDBids with PDBids
 * - Define the hash function for PDB strings					*/

static Uint4 HashPDBCode(CharPtr pcPdb) {
  Uint4 iPdbhash;
  Uint4 c1, c2, c3, c4;

  if (!pcPdb) return 0;
  if (StringLen(pcPdb) != 4) return 0;
  c1 =  toupper(pcPdb[3]);
  c2 =  toupper(pcPdb[2]);
  c3 =  toupper(pcPdb[1]);
  c4 =  toupper(pcPdb[0]);
  iPdbhash =  c1 + (c2 << 8) + (c3 << 16) + (c4 << 24);
  
  return iPdbhash;
}

/* The index file is loaded as a list of (long) mmdbids correlated with a 
   list of unsigned long HASHED PDB codes					*/

static Boolean LoadMMDBIdx(CharPtr db,  CharPtr idx) {
  FILE *f;
  Char fullpath [PATH_MAX];
  CharPtr ptr;
  Char pcBuf[100];
  CharPtr pcTest;
  Char  pcMmdb[20];
  Char  pcPdb[20];
  Uint4Ptr     pU4Pdb;
  Int4Ptr      pI4Mmdb;
  long count = 0;
  long mmdbid = 0;
  int status;
  char *msg;

#define ERROR(err_msg) { msg = err_msg; goto errexit; }

  StringCpy(fullpath, db);
  StringCat(fullpath, idx);
  if ((f = FileOpen (fullpath, "r")) == NULL) 
    ERROR("MMDBInit: Couldn't load MMDB index.");
   
  fscanf(f, "%ld", &iMMDBSize);
  if ((iMMDBSize == 0) || (iMMDBSize > 100000))
    ERROR ("Internal - LoadMMDBIdx() Failure 2;");

  pI4Mmdbidx = (Int4Ptr) MemNew((size_t) ((iMMDBSize)*sizeof(Int4)));
  pU4Pdbidx = (Uint4Ptr) MemNew((size_t) ((iMMDBSize)*sizeof(Uint4)));

  if ((!pI4Mmdbidx) || (!pU4Pdbidx))
    ERROR ("Internal - LoadMMDBIdx() Out of Memory;");
   
  pI4Mmdb = pI4Mmdbidx;
  pU4Pdb = pU4Pdbidx;
   
  while ((status = fscanf(f, "%ld%s",  &mmdbid,  pcPdb)) != EOF) {

    StrUpper(pcPdb);
    if ((mmdbid == 0) ||  (StringLen(pcPdb) > 4)) {
      MMDBFini();
      ERROR ("Internal - LoadMMDBIdx() Bad Index Values");
    }
    
    *pI4Mmdb = mmdbid;
    *pU4Pdb =  HashPDBCode(pcPdb);
    pI4Mmdb++;
    pU4Pdb++;
    count++;
    if (count > (iMMDBSize)) {
      MMDBFini();
      ERROR ("Internal - LoadMMDBIdx() Index count is wrong at top of file;");
    }
  }
  FileClose(f);
  return TRUE;

#undef ERROR
errexit:
  ErrPostEx(SEV_FATAL,0,0, msg);
  return FALSE;
}

static void CloseMMDBIdx() {
  if (pI4Mmdbidx) MemFree(pI4Mmdbidx);
  if (pU4Pdbidx) MemFree(pU4Pdbidx);
  return;
}

/* Given a Char PDBid, hash-encode it and retrieve the corresponding long MMDBid */

DocUid MMDBEvalPDB (CharPtr str) 
{
  register Uint4Ptr pU4Pdb = NULL;
  register Int4Ptr pI4Mmdb = NULL;
  register Uint4   iHash = 0;
  long i = 0;
  
  ASSERT ((pI4Mmdbidx != NULL) && (pU4Pdbidx != NULL));
  pI4Mmdb = pI4Mmdbidx;
  pU4Pdb = pU4Pdbidx;
  iHash = HashPDBCode(str);

  while (i++ < iMMDBSize && iHash != *pU4Pdb) {
    pU4Pdb++;
    pI4Mmdb++;
  } 
  if (iHash == *pU4Pdb)
    return (DocUid) *pI4Mmdb;
  return (DocUid) 0;
}

/*****************************FF_BiostrucGet******************************/
BiostrucPtr MMDBBiostrucGet (DocUid uid, Int4 mdlLvl, Int4 maxModels)
{
  Char path[PATH_MAX], compath[PATH_MAX];
  AsnIoPtr    aip = NULL;
  FILE *pipe;
  BiostrucPtr pbs = NULL;

  sprintf(path, "%s%ld.val", database, (long) uid);
  if (FileLength(path) >0)
    aip = AsnIoOpen(path, "rb");
  else
    {
      if (gunzip[0] == 0) {
        ErrPostEx(SEV_FATAL,0,0, "MMDBBiostrucGet_files failed: gunzip path missing.");
        return NULL;
      }
      sprintf(path, "%s%ld.val.gz", database, (long) uid);
      if (FileLength(path) <=0)
        return NULL;
      
      sprintf(compath,"%s -c -d %s ", gunzip, path);
      pipe=popen(compath,"rb");
      if (pipe == 0)
        {
          ErrPostEx(SEV_FATAL,0,0, "MMDBBiostrucGet failed: Can't find gunzip in path");
          return NULL;
        }
      aip = AsnIoNew(ASNIO_BIN_IN, pipe , NULL, NULL, NULL);
    }
  if (!aip)
    return NULL;

  pbs = BiostrucAsnGet(aip, NULL,  mdlLvl,  maxModels);
  AsnIoClose (aip);
  return pbs;
}

Boolean MMDBInit (void) 
{
  char *msg;
  char ndxPath[PATH_MAX];
  FILE *f;
  
#define ERROR(err_msg) { msg = err_msg; goto errexit; }

  GetAppParam("mmdb", "MMDB", "Index"   , "", indexname, sizeof(indexname));
  GetAppParam("mmdb", "MMDB", "Database", "", database, sizeof(database));
  if (!(database[0] && indexname[0]))
    return FALSE ;
  
  /* let's check if index file really there */
    
  assert(sizeof(ndxPath) > strlen(database) + strlen(indexname));
  sprintf(ndxPath,"%s%s",database,indexname);
  
  if ((f = fopen(ndxPath,"r")) == NULL)
    ERROR("MMDBInit failed: incorrect mmdb.idx path.");
  fclose(f);
  if(FALSE==LoadMMDBIdx(database,  indexname))
    return FALSE;
  GetAppParam("mmdb", "MMDBSRV", "Gunzip", "", gunzip, sizeof(gunzip));
  if(gunzip[0])
    {
      f = fopen(gunzip,"r");
      if(f)
        fclose(f);
      else
        gunzip[0]=0;
    }
  return TRUE;
  
#undef ERROR

errexit:
  ErrPostEx(SEV_ERROR,0,0, msg);
  return FALSE;
}

void LIBCALL
MMDBFini (void)
{
  CloseMMDBIdx();
}

CharPtr LIBCALL
MMDB_configuration(void)
{
  return "Version:\t$Id: mmdbFF.c,v 6.4 2000/06/20 20:47:03 lewisg Exp $\nConfiguration: Flat Files" ;
}