~ubuntu-branches/ubuntu/warty/ncbi-tools6/warty

« back to all changes in this revision

Viewing changes to biostruc/mmdbsrv.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: mmdbsrv.c,v 6.28 2001/12/12 20:43:38 beloslyu Exp $
 
2
* ===========================================================================
 
3
*
 
4
*                            PUBLIC DOMAIN NOTICE
 
5
*               National Center for Biotechnology Information
 
6
*
 
7
*  This software/database is a "United States Government Work" under the
 
8
*  terms of the United States Copyright Act.  It was written as part of
 
9
*  the author's official duties as a United States Government employee and
 
10
*  thus cannot be copyrighted.  This software/database is freely available
 
11
*  to the public for use. The National Library of Medicine and the U.S.
 
12
*  Government have not placed any restriction on its use or reproduction.
 
13
*
 
14
*  Although all reasonable efforts have been taken to ensure the accuracy
 
15
*  and reliability of the software and data, the NLM and the U.S.
 
16
*  Government do not and cannot warrant the performance or results that
 
17
*  may be obtained by using this software or data. The NLM and the U.S.
 
18
*  Government disclaim all warranties, express or implied, including
 
19
*  warranties of performance, merchantability or fitness for any particular
 
20
*  purpose.
 
21
*
 
22
*  Please cite the author in any work or product based on this material.
 
23
*
 
24
* ===========================================================================
 
25
*
 
26
* File Name:  $RCSfile: mmdbsrv.c,v $
 
27
*
 
28
* Author:  Christopher Hogue
 
29
*
 
30
* Version Creation Date: 6 January 1997
 
31
*
 
32
* $Revision: 6.28 $
 
33
*
 
34
* File Description:
 
35
*        MMDB WWW-server 
 
36
*
 
37
* $Log: mmdbsrv.c,v $
 
38
* Revision 6.28  2001/12/12 20:43:38  beloslyu
 
39
* change the name of ftp site to ftp.ncbi.nih.gov
 
40
*
 
41
* Revision 6.27  2001/01/25 23:02:56  lewisg
 
42
* get rid of content-disp, add mdlLvl and maxModels args
 
43
*
 
44
* Revision 6.26  2000/08/21 16:12:42  lewisg
 
45
* add save=asntext option
 
46
*
 
47
* Revision 6.25  2000/06/26 18:05:26  lewisg
 
48
* fix another hardcoded url
 
49
*
 
50
* Revision 6.24  2000/06/23 22:35:58  lewisg
 
51
* get rid of hardcoded urls
 
52
*
 
53
* Revision 6.23  2000/01/31 19:59:12  lewisg
 
54
* fix output of date in pdb header
 
55
*
 
56
* Revision 6.22  1999/10/13 20:13:18  zimmerma
 
57
* DZ: Removed use of temporary files - html ouput redirected to stdout
 
58
*
 
59
* Revision 6.21  1999/06/17 14:44:09  zimmerma
 
60
* Added static char variable for cvsId in order to use unix "what"
 
61
*
 
62
* Revision 6.20  1999/06/15 18:15:34  addess
 
63
* Initialized this_uid to 0 on line 1286
 
64
*
 
65
* Revision 6.19  1999/05/13 15:51:33  kimelman
 
66
* bugfix: memory deallocation fixed
 
67
*
 
68
* Revision 6.19  1999/05/13 15:51:33  kimelman
 
69
* bugfix: memory deallocation fixed
 
70
*
 
71
* Revision 6.18  1999/05/11 23:36:34  kimelman
 
72
* make StrToInt4 static
 
73
*
 
74
* Revision 6.17  1999/05/11 17:50:23  kimelman
 
75
* style
 
76
*
 
77
* Revision 6.16  1999/04/28 21:08:30  lewisg
 
78
* fixed double freeing of Biostruc
 
79
*
 
80
* Revision 6.15  1999/04/28 18:26:15  kimelman
 
81
* exit codes changed to 0
 
82
*
 
83
* Revision 6.14  1999/04/22 01:56:44  kimelman
 
84
* 1. pdb reference read completely from configurstion files
 
85
* 2. indentaiton
 
86
* 3. NcbiMimeAsn1Free(mime) commented out <-- lead to core
 
87
*    dump AFTER sending MIME (quick & dirty fix)
 
88
*
 
89
* Revision 6.13  1999/04/19 16:52:57  kimelman
 
90
* spelling fix
 
91
*
 
92
* Revision 6.12  1999/03/05 19:18:47  addess
 
93
* changed URL to link to RCSB structure explorer
 
94
*
 
95
* Revision 6.11  1998/11/06 16:25:31  addess
 
96
* added features for layer-1 and PDB TITLE records
 
97
*
 
98
 * Revision 6.10  1998/07/17  18:45:53  madej
 
99
 * Got rid of some unused URLs.
 
100
 *
 
101
 * Revision 6.9  1998/06/11  19:12:53  madej
 
102
 * Changes in a message type for Cn3D by Yanli.
 
103
 *
 
104
 * Revision 6.8  1998/04/16  17:51:38  madej
 
105
 * Tweak to print out author names in new/old format.
 
106
 *
 
107
 * Revision 6.7  1998/04/15  14:54:05  kimelman
 
108
 * 1. Make retrieval unifirm from open server and sql server.
 
109
 * 2. mmdbsrv retrival performance tuning:
 
110
 *      - size of read-in buffers
 
111
 *      - direct pdb2mmdb DB lookup instead of full loading pdb to mmdb translataion
 
112
 *        table
 
113
 * 3. cleaning mmdblocl.* and make.mmdbsrv in order to remove obsolete code
 
114
 *
 
115
* Revision 6.6  1998/04/03 20:25:24  kimelman
 
116
* PubStruct access code added to mmdbsrv
 
117
*
 
118
* Revision 6.5  1998/02/20 18:25:26  madej
 
119
* Fix using unlink to stop file dumps into /var/tmp.
 
120
*
 
121
 * Revision 6.4  1997/11/25  18:27:45  madej
 
122
 * Fix to save asn.1 files in binary.
 
123
 *
 
124
 * Revision 6.3  1997/11/05  19:05:16  madej
 
125
 * Updated comments for config file.
 
126
 *
 
127
 * Revision 6.2  1997/11/05  15:58:57  madej
 
128
 * Bug fix for tax names in multiple listings
 
129
 *
 
130
 * Revision 6.1  1997/10/31  15:29:57  madej
 
131
 * Major reformat of structure summary page, including taxonomy links.
 
132
 *
 
133
 * Revision 6.0  1997/08/25  18:11:28  madden
 
134
 * Revision changed to 6.0
 
135
 *
 
136
* Revision 1.2  1997/05/16 21:25:41  hogue
 
137
* Added checks with vast database entries before showing vast links.
 
138
*
 
139
 * Revision 1.1  1997/02/18  18:03:30  hogue
 
140
 * Initial revision
 
141
 *
 
142
*
 
143
* ==========================================================================
 
144
*/
 
145
 
 
146
/************** 
 
147
*mmdbsrv GET Useage:
 
148
*
 
149
*db=t&form=6&uid=1OMD&dopt=s  - PDB or MMDB-ids allowed
 
150
*db=t&form=6&uid=1OMD&Dopt=i  - launch Cn3D Mime-type - first UID only.
 
151
*db=t&uid=1234,2345,1254,1232&dopt=s - multiple structure summaries 
 
152
*db=t&uid=3INS,1234,1OMD&dopt=s  - mixed id types OK
 
153
*
 
154
*Optional,  but if wrong,  error: 
 
155
*        db=t    any other is an error message
 
156
*        form=6  4 is an error message - any other ignored
 
157
*       
 
158
*Required       
 
159
*       uid=(list of MMDB ids or PDB ids)
 
160
*
 
161
*Optional dopt=
 
162
*     's' Structure summary
 
163
*     'r' PDB file 
 
164
*     'k' Kinemage file
 
165
*     'i' ASN.1 file
 
166
*     Others - error; None assumes 's'
 
167
*
 
168
*Optional - Interpreted in Context:
 
169
*        complexity="Cn3D+Subset"  ONECOORDATOM,  1
 
170
*                   "Virtual+Bond+Model" ONECOORDRES,  1
 
171
*                   "Up+to+5+Models" ALLMDL,  5
 
172
*                   "Up+to+10+Models" ALLMDL,  10
 
173
*                   "All+Models" ALLMDL,  100
 
174
 
175
*        KinemageColor="Molecule+Number" KIN_COLOR_NUMBER
 
176
*                      "Secondary+Structure"  KIN_COLOR_TYPE
 
177
*                      "Thermal+Factor" KIN_COLOR_TEMP
 
178
*                      "Element" KIN_COLOR_ATOM
 
179
*        KinemageRender=(integer)  - not seen on Struc Summary 
 
180
*        save="See"
 
181
*             "s" - saves
 
182
*             "m" MacSave format
 
183
*        header="no" same as save="s"
 
184
*        title="no" - omit title bar and bottom options
 
185
*        html="no" 
 
186
*/
 
187
 
 
188
 
 
189
/*******************************************
 
190
MMDBSRV MIRROR installation:
 
191
 
 
192
This describes what you need to run MMDBSRV:
 
193
 
 
194
1] YOUR MMDB DATA DIRECTORIES, e.g.:
 
195
...mmdb/mmdbdata/
 
196
...mmdb/vastdata/
 
197
 
 
198
These contain the MMDB data on your local filesystem,
 
199
as obtained from ftp://ftp.ncbi.nih.gov/mmdb/mmdbdata  
 
200
and updated regularly by MIRROR.  You can 
 
201
put them in the CGI-BIN or HTTP (HTML) directories
 
202
if you prefer. If you wish to implement a local
 
203
decompression/compression scheme see the source code mmdblocl.c.
 
204
 
 
205
2] YOUR CGI-BIN directory & URL, e.g.:
 
206
...cgi-bin/mmdb/
 
207
...cgi-bin/mmdb/logs/
 
208
...cgi-bin/mmdb/data/
 
209
http://123.456.789.10/cgi-bin/mmdb/
 
210
 
 
211
The first contains the compiled mmdbsrv program and the
 
212
config file, obtained from 
 
213
ftp://ftp.ncbi.nih.gov/mmdb/www/mmdbsrv
 
214
The second will contain the log files written out.
 
215
The third needs sshead.txt, sscode.txt, sstail.txt
 
216
NOTE - you must edit "sstail.txt"
 
217
in order to get the proper links to your local
 
218
copy of help documentation & server.
 
219
 
 
220
3] YOUR HTTP (HTML) directory & URL, e.g.:
 
221
...html/mmdb
 
222
...html/mmdb/mmdbgifs
 
223
http://123.456.789.10/mmdb/
 
224
 
 
225
The first contains the html files to launch mmdbsrv,
 
226
arrow.gif, strucsum.gif and any other gif files, and the
 
227
associated MMDB help files struchelp.html, cn3d.html,
 
228
strucinstall.html, vast.html, etc.  Master copies are kept
 
229
at ftp://ftp.ncbi.nih.gov/mmdb/www/http
 
230
You must hand edit or use scripts or SED to change links from 
 
231
NCBI's to your own in all these HTML files.
 
232
 
 
233
The second contains the GIF library as obtained from
 
234
the NCBI ftp site ftp://ftp.ncbi.nih.gov/mmdb/mmdbgifs
 
235
 
 
236
6] Your WWW-Server - make it aware of the following 
 
237
new MIME-types:
 
238
chemical/ncbi-asn1-binary  *.val
 
239
chemical/x-pdb  *.ent; *.pdb
 
240
chemical/x-kinemage *.kin
 
241
 
 
242
 
 
243
-----------------------------
 
244
7]
 
245
MMDBSRV gets its directory structure; link URLS and CGI call names 
 
246
from a config file.
 
247
Change these as required and make a config file.
 
248
Unix ".mmdbrc" or Win "mmdb.ini" from this example:
 
249
 
 
250
Don't change:
 
251
URLHelp, ENTREZurl, MMDBurl, PDBurl, 3DBurl 
 
252
unless there are broken external links to these.
 
253
 
 
254
-------------------- cut here --------------------
 
255
[MMDB]
 
256
;Database and Index required when local MMDB database is used.
 
257
; Database = ./Junk/
 
258
Database = /net/dorothy/cbb3/mmdb/data/
 
259
Index    = mmdb.idx
 
260
 
 
261
[MMDBSRV]
 
262
; File for configuring the MMDB server.
 
263
; Base Directory & URLs for HTML documents.
 
264
HTMLPath  = /net/vaster/usr/attic/httpd.local/htdocs/Structure/
 
265
URLBase   = http://inhouse.ncbi.nlm.nih.gov:6224/Structure/
 
266
URLHelp   = http://www.ncbi.nlm.nih.gov/Entrez/ 
 
267
;
 
268
 
 
269
; Base Directory for CGI-BIN code, data (gifs, html text) and logs.
 
270
CGIPath   = /net/vaster/usr/attic/httpd.local/cgi-bin/Structure/
 
271
URLcgi    = http://inhouse.ncbi.nlm.nih.gov:6224/cgi-bin/Structure/
 
272
CGIname   = mmdbsrv
 
273
DATApath  = ./data/
 
274
LOGpath   = ./logs/
 
275
;
 
276
 
 
277
; Base Directory for MMDB-gif set
 
278
GIFpath  = /net/vaster/usr/attic/httpd.public/htdocs/Structure/mmdbgifs/
 
279
GIFurl   = http://www.ncbi.nlm.nih.gov/Structure/mmdbgifs/
 
280
;
 
281
 
 
282
; URL's for links to Entrez, VAST.
 
283
ENTREZurl = http://www.ncbi.nlm.nih.gov/htbin-post/Entrez/query
 
284
VASTurl = http://inhouse.ncbi.nlm.nih.gov:6224/cgi-bin/Structure/vastsrv
 
285
MMDBurl = http://www.ncbi.nlm.nih.gov/Structure/
 
286
PDBurl = http://www.pdb.bnl.gov/index.html
 
287
TDBurl = http://www.pdb.bnl.gov/cgi-bin/opdbshort
 
288
 
 
289
; Location of gunzip function.
 
290
Gunzip = /usr/sbin/gunzip
 
291
 
 
292
;
 
293
MAILto   = info@ncbi.nlm.nih.gov
 
294
;
 
295
[VAST]
 
296
;Database required for local VAST fetches.
 
297
;Database = /net/vaster/usr/people6/madej/vastdata.binary/
 
298
Database = /net/dorothy/cbb3/vast/data/
 
299
 
 
300
; URL for link to taxonomy database (wgetorg for retrieval)
 
301
[TAXLINK]
 
302
TAXurl = http://www.ncbi.nlm.nih.gov/htbin-post/Taxonomy/wgetorg
 
303
 
 
304
-------------------- cut here --------------------
 
305
********************************************/
 
306
 
 
307
#include <ncbi.h>
 
308
#include <ncbiwww.h>
 
309
#include <accentr.h>
 
310
#include <ncbistr.h>
 
311
#include <miscutils.h>
 
312
#include <sys/resource.h>
 
313
#include <mmdbapi.h>
 
314
#include <mmdbdata.h>
 
315
#include <taxinc.h>
 
316
#include <sys/resource.h>
 
317
 
 
318
#include <lsqfetch.h>
 
319
#include <netentr.h>
 
320
#include <asnmime.h>
 
321
#include <objmime.h>
 
322
 
 
323
#ifdef OS_UNIX
 
324
#include <signal.h>
 
325
#include <unistd.h>
 
326
#define CPUTIME_MAX 120
 
327
#endif
 
328
  
 
329
 
 
330
 
 
331
#define NUM_COMPLEXITY_TYPES 6
 
332
static char *ComplexityDescriptions[] = 
 
333
{"Virtual Bond Model","All Atom Model","Up to 5 Models",
 
334
   "Up to 10 Models","All Models","Cn3D Subset"};
 
335
 
 
336
#define NUM_COLOR_TYPES 5
 
337
static char *ColorDescriptions[] =
 
338
{"","Molecule Number","Secondary Structure","Thermal Factor","Element"};
 
339
 
 
340
/* the config file DATApath is used for the location of these files */
 
341
 
 
342
#define HEADFILE "sshead.txt"
 
343
/* Structure Summary Header File */
 
344
 
 
345
#define TAILFILE "sstail.txt"
 
346
/* Structure Summary Tail File  */
 
347
 
 
348
#define CODEFILE "sscode.txt"
 
349
/* Structure Summary View Code */
 
350
 
 
351
#define CHAIN_CHOICE_NUM        1
 
352
#define ORG_CHOICE_NUM          6
 
353
 
 
354
/* some defines needed for the taxonomy code */
 
355
 
 
356
#define TAX_ID_CHOICE_NUM       0
 
357
#define MAX_CHAINS              128
 
358
#define MAX_TBUFF               8192
 
359
 
 
360
WWWInfoPtr              info = NULL;
 
361
 
 
362
static Boolean GlobalTitles = TRUE;
 
363
static Boolean GlobalNonHtmlOutput = FALSE;
 
364
 
 
365
static Char HTMLPath[PATH_MAX];
 
366
static Char URLBase[PATH_MAX];
 
367
static Char URLHelp[PATH_MAX];
 
368
 
 
369
static Char CGIPath[PATH_MAX];
 
370
static Char URLcgi[PATH_MAX];
 
371
static Char CGIName[256];
 
372
static Char DATApath[PATH_MAX];
 
373
static Char LOGpath[PATH_MAX];
 
374
 
 
375
static Char ENTREZurl[PATH_MAX];
 
376
static Char VASTurl[PATH_MAX];
 
377
static Char MMDBurl[PATH_MAX];
 
378
static Char PDBurl[PATH_MAX];
 
379
static Char TDBurl[PATH_MAX];
 
380
static Char TAXurl[PATH_MAX];
 
381
static Char MAILto[256];
 
382
static Char MAILTO[PATH_MAX];
 
383
static Char ARROW[PATH_MAX];
 
384
 
 
385
static char* cvsId_ = "@(#)$Id: mmdbsrv.c,v 6.28 2001/12/12 20:43:38 beloslyu Exp $";
 
386
 
 
387
/*****************************************************
 
388
 * WWWPrintFileData looks in the current CGI-BIN directory 
 
389
 *  or the "data" subdirectory for the data file.
 
390
 *  and prints it out to pFile
 
391
 */
 
392
 
 
393
static void WWWPrintFileData(CharPtr FName,  FILE *pFile)
 
394
{
 
395
 
 
396
   FILE *f = NULL;
 
397
   Char fullpath [PATH_MAX];
 
398
   CharPtr ptr;  
 
399
   Char pcBuf[1024];
 
400
   
 
401
   fullpath[0] = '\0';
 
402
   StringCpy(fullpath,  DATApath); /* look in DATApath */
 
403
   StringCat(fullpath,  FName);
 
404
   f = FileOpen (fullpath, "r");
 
405
   if (f == NULL) {
 
406
       f = FileOpen (FName, "r");  /* look in curent */
 
407
       if (f == NULL)  {  /* look in ./data/ */
 
408
         ProgramPath (fullpath, sizeof (fullpath) - 1);
 
409
         ptr = StringRChr (fullpath, DIRDELIMCHR);
 
410
         if (ptr != NULL) {
 
411
          *ptr = '\0';
 
412
         }
 
413
         FileBuildPath (fullpath, "data", FName);  
 
414
         f = FileOpen (fullpath, "r");
 
415
         if (f == NULL)  {
 
416
           return;  
 
417
         } 
 
418
       }
 
419
   }
 
420
      
 
421
   do {
 
422
     pcBuf[0] = '\0';
 
423
     ptr = fgets(pcBuf, (size_t)1024, f);
 
424
     if (ptr) fprintf(pFile, ptr);
 
425
   } while (ptr);
 
426
  
 
427
   FileClose(f);
 
428
   return;
 
429
}
 
430
 
 
431
 
 
432
static Boolean
 
433
StrToInt4 (CharPtr str, Int4Ptr longval)
 
434
{
 
435
  Nlm_Int2     i;
 
436
  Nlm_Int2     len;
 
437
  Nlm_Char     local [64];
 
438
  Nlm_Boolean  nodigits;
 
439
  long int     val;
 
440
 
 
441
  if (longval != NULL) {
 
442
    *longval = (Nlm_Int4) 0;
 
443
  }
 
444
  len = (Nlm_Int2) Nlm_StringLen (str);
 
445
  if (len == 0)
 
446
    return FALSE;
 
447
 
 
448
  nodigits = TRUE;
 
449
  for (i = 0; i < len; i++)
 
450
    {
 
451
      Nlm_Char     ch;
 
452
      
 
453
      ch = str [i];
 
454
      if ('0' <= ch && ch <= '9')
 
455
        nodigits = FALSE;
 
456
      else if ( !/*!!*/  (ch == ' ' || ch == '+' || ch == '-') || nodigits==FALSE)
 
457
        return FALSE;
 
458
    }
 
459
  if (nodigits)
 
460
    return FALSE;
 
461
  if (longval != NULL)
 
462
    {
 
463
      Nlm_StringNCpy (local, str, sizeof (local));
 
464
      if (sscanf (local, "%ld", &val) == 1)
 
465
        *longval = val;
 
466
    }
 
467
  return TRUE;
 
468
}
 
469
 
 
470
    
 
471
static Int4 CountUIDs(char *String)
 
472
{
 
473
  Int4 count = 0;
 
474
 
 
475
  for (;;)
 
476
    {
 
477
      String = SkipSet(String,", ");
 
478
      if (*String == NULLB)
 
479
        break;
 
480
      count++;
 
481
      String = SkipToSet(String,", ");
 
482
    }
 
483
  
 
484
  return(count);
 
485
}    
 
486
 
 
487
static Int4 ConvertMMDBUID(CharPtr pcString)  
 
488
{
 
489
    Int4 iUID;
 
490
    CharPtr pcTemp = NULL;
 
491
        
 
492
    if (pcString == NULL)
 
493
    return 0;
 
494
    
 
495
    iUID = 0;
 
496
    pcTemp = StringSave(pcString);
 
497
    CleanSpaces(pcTemp);
 
498
    if (!StrToInt4(pcTemp, &iUID))
 
499
      {
 
500
           iUID = MMDBEvalPDB(pcTemp);
 
501
      }
 
502
    MemFree(pcTemp);
 
503
    return iUID; 
 
504
}
 
505
 
 
506
 
 
507
 
 
508
 
 
509
static void MakeUIDList(CharPtr pcString,  ValNodePtr PNTR ppvnUIDS)
 
510
{
 
511
  CharPtr Ptr = NULL;
 
512
  CharPtr Ptr2 = NULL;
 
513
  Char SavedChar = NULL;
 
514
  Int4 Uid = 0,  i;
 
515
  Int4Ptr Uids = NULL;
 
516
 
 
517
  
 
518
  Ptr = SkipSpaces(pcString);
 
519
  if (*Ptr == NULLB)
 
520
    return;
 
521
 
 
522
  while (*Ptr)
 
523
    {
 
524
      Ptr2 = SkipToSet(Ptr," ,");
 
525
      SavedChar = *Ptr2;
 
526
      *Ptr2 = NULLB;
 
527
      Uid = 0;
 
528
      Uid = ConvertMMDBUID(Ptr);
 
529
/* printf("{%s}[%ld]\n", Ptr,  Uid); */
 
530
      if (Uid != 0)
 
531
        {
 
532
          ValNodeAddInt(ppvnUIDS, 0, Uid);
 
533
        }
 
534
      *Ptr2 = SavedChar;
 
535
      Ptr = SkipSet(Ptr2," ,");
 
536
    }
 
537
 
 
538
  return;  
 
539
}
 
540
 
 
541
 
 
542
static Int4 GetOptionValue(CharPtr Value, Int4 NumElements, CharPtr *ElementText)
 
543
{
 
544
  Int4 count, Len;
 
545
  Int4 valnum;
 
546
 
 
547
  if (StrToInt4(Value,  &valnum))
 
548
    return(valnum);
 
549
 
 
550
  Len = StringLen(Value);
 
551
  for (count = 0; count < NumElements; count++)
 
552
    if (StrNICmp(ElementText[count],Value, Len) == 0)
 
553
      break;
 
554
 
 
555
  if (count == NumElements)
 
556
    return -1;
 
557
 
 
558
  return(count);
 
559
}
 
560
 
 
561
 
 
562
 
 
563
/* Output links from the protein and nucleotide chains to GenPept reports. */
 
564
 
 
565
#define EntrezLink      "<A HREF=\"%s?uid=%ld&form=6&db=%c&dopt=%c\">%s</A>"
 
566
 
 
567
/************ Added by Ken for Layer One PDB releases ************/
 
568
 
 
569
static void My_StringNCpy(CharPtr str1, CharPtr str2, Int4 len)
 
570
{
 
571
  StringNCpy(str1,str2,len);
 
572
  str1[len] = '\0';
 
573
 
574
 
 
575
static Boolean IsLayerOne(ValNodePtr pvn)
 
576
{
 
577
  Boolean Layer = FALSE;
 
578
  CharPtr comment_txt;
 
579
  CharPtr LayerOne = {"This is Layer 1 Release"};
 
580
  CharPtr temp_string;
 
581
  Int4 len;
 
582
  
 
583
  while (pvn)
 
584
  {
 
585
     if (pvn->choice == BiostrucDescr_pdb_comment)
 
586
     {
 
587
        comment_txt = (CharPtr)pvn->data.ptrvalue;
 
588
        temp_string = StringSave(LayerOne);
 
589
        len = StringLen(LayerOne);
 
590
        My_StringNCpy(temp_string, &comment_txt[18], len);
 
591
        if (!StringICmp(temp_string, LayerOne))
 
592
        {
 
593
          Layer = TRUE;
 
594
          break;
 
595
        }
 
596
     }
 
597
     pvn = pvn->next;
 
598
   }
 
599
   
 
600
   return Layer;
 
601
}
 
602
/***** End of functions added by Ken ********************/
 
603
static void
 
604
PrintMolecularContents(PDNMS ModelStruc, FILE *gdf)
 
605
{
 
606
        PMSD pmsd;
 
607
        PDNMM pdnmm;
 
608
        PMMD pmmd;
 
609
        Int2 cnt;
 
610
 
 
611
 
 
612
 
 
613
        if (ModelStruc == NULL)
 
614
                return;
 
615
 
 
616
        /* protein chains */
 
617
        pmsd = (PMSD) ModelStruc->data.ptrvalue;
 
618
 
 
619
        for (pdnmm = pmsd->pdnmmHead, cnt = 0; pdnmm != NULL; pdnmm = pdnmm->next) {
 
620
                pmmd = (PMMD) pdnmm->data.ptrvalue;
 
621
 
 
622
                if ((pmmd->bWhat) & AM_PROT) {
 
623
                        if (cnt++ == 0) {
 
624
                                fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
625
                                fprintf(gdf, "<strong>Protein Chains:</strong></TD>\n<TD>");
 
626
 
 
627
                                if (pmmd->pcMolName[0] == ' ')
 
628
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'g',
 
629
                                                "(single chain)");
 
630
                                else
 
631
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'g',
 
632
                                                pmmd->pcMolName);
 
633
                        }
 
634
                        else {
 
635
                                fprintf(gdf, ", ");
 
636
                                fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'g',
 
637
                                        pmmd->pcMolName);
 
638
                        }
 
639
                }
 
640
        }
 
641
 
 
642
        if (cnt > 0)
 
643
                fprintf(gdf, "</TD>\n</TR>\n");
 
644
 
 
645
        /* nucleotide chains */
 
646
        for (pdnmm = pmsd->pdnmmHead, cnt = 0; pdnmm != NULL; pdnmm = pdnmm->next) {
 
647
                pmmd = (PMMD) pdnmm->data.ptrvalue;
 
648
 
 
649
                if ((pmmd->bWhat) & (AM_DNA | AM_RNA)) {
 
650
                        if (cnt++ == 0) {
 
651
                                fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
652
                                fprintf(gdf, "<strong>Nucleotide chains:</strong></TD>\n<TD>");
 
653
 
 
654
                                if (pmmd->pcMolName[0] == ' ')
 
655
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'g',
 
656
                                                "(single chain)");
 
657
                                else
 
658
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'g',
 
659
                                                pmmd->pcMolName);
 
660
                        }
 
661
                        else {
 
662
                                fprintf(gdf, ", ");
 
663
                                fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'g',
 
664
                                        pmmd->pcMolName);
 
665
                        }
 
666
                }
 
667
        }
 
668
 
 
669
        if (cnt > 0)
 
670
                fprintf(gdf, "</TD>\n</TR>\n");
 
671
 
 
672
} /* end PrintMolecularContents */
 
673
 
 
674
 
 
675
 
 
676
static void
 
677
PrintStructureInfo(PDNMS ModelStruc,  FILE *File, CharPtr tax_save)
 
678
{
 
679
   char Name[200];
 
680
   Int4 depyear, depday;
 
681
   PMSD pmsdThis = NULL;
 
682
   BiostrucSourcePtr pbssThis = NULL;
 
683
   ValNodePtr pvnThis = NULL;
 
684
   BiostrucHistoryPtr pbshThis = NULL;
 
685
   CharPtr pcAuthors = NULL;
 
686
   char Paragraph[10];
 
687
   
 
688
   if (!ModelStruc) 
 
689
     return;
 
690
 
 
691
   pmsdThis = (PMSD) ModelStruc->data.ptrvalue;
 
692
   pvnThis = ValNodeFindNext(pmsdThis->pbsBS->descr,NULL,BiostrucDescr_history);
 
693
 
 
694
   if (pvnThis) {
 
695
      pbshThis = (BiostrucHistoryPtr) pvnThis->data.ptrvalue;
 
696
      pbssThis = pbshThis->data_source;
 
697
   }
 
698
 
 
699
  /*****
 
700
  fprintf(File, "<font size=+2>MMDB Id: %ld&nbsp ", (long) pmsdThis->iMMDBid);
 
701
  *****/
 
702
  fprintf(File, "<font size=+2>");
 
703
  fprintf(File, "MMDB Id: <A HREF=\"%s%s?uid=%ld&form=6&db=t&Dopt=s\">%ld</A>&nbsp ",
 
704
        URLcgi, CGIName, (long) pmsdThis->iMMDBid, (long) pmsdThis->iMMDBid);
 
705
  fprintf(File, "PDB Id: <A HREF=\"%s=%s\">%s</a>\n", TDBurl,pmsdThis->pcPDBName, pmsdThis->pcPDBName);
 
706
  if (IsLayerOne(pmsdThis->pbsBS->descr))
 
707
    fprintf(File, " - Warning: <A HREF=\"http://www.pdb.bnl.gov/pdb-docs/what_is_LR.html\">Not validated!</A>\n");
 
708
  fprintf(File, "</font>");  
 
709
  fprintf(File, "<BR>\n<BR>\n");
 
710
  fprintf(File, "<TABLE>\n");
 
711
  PrintMolecularContents(ModelStruc, File);
 
712
  fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
713
  fprintf(File, "<strong>MEDLINE</strong>:</TD>\n<TD><A HREF=\"%s?db=t&form=6&uid=", ENTREZurl);
 
714
  fprintf(File, "%ld&Dopt=m\">PubMed</A></TD>\n</TR>\n", (long) pmsdThis->iMMDBid );
 
715
 
 
716
  if (tax_save != NULL)
 
717
        fprintf(File, "%s\n", tax_save);
 
718
  else if (pmsdThis->pcPdbSource) {
 
719
        fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
720
        fprintf(File, "<strong>Source</strong>:</TD>\n<TD>%s</TD>\n</TR>\n", pmsdThis->pcPdbSource);
 
721
  }
 
722
 
 
723
  /* insert a blank row in the table, for spacing */
 
724
  fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP> </TD>\n<TD><BR></TD>\n</TR>\n");
 
725
  pcAuthors =  AuthorListPDB(pmsdThis->pbsBS);
 
726
 
 
727
  if (pcAuthors) {
 
728
        fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
729
        fprintf(File, "<strong>PDB Authors</strong>:</TD>\n<TD>%s</TD>\n</TR>\n", pcAuthors);
 
730
        fflush(File);
 
731
        MemFree(pcAuthors);
 
732
  }
 
733
 
 
734
  if (pbssThis) {
 
735
        depyear = pbssThis->database_entry_date->data[1];
 
736
        depday = pbssThis->database_entry_date->data[3];
 
737
        fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
738
        fprintf(File, "<strong>PDB Deposition</strong>:</TD>\n<TD>%2d-%3s-%02d</TD>\n</TR>\n",
 
739
                (int) depday, NCBI_months[pbssThis->database_entry_date->data[2] - 1], (int) depyear%100);
 
740
  }
 
741
 
 
742
  if (pmsdThis->pcPdbClass) {
 
743
        fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
744
        fprintf(File, "<strong>PDB Class</strong>:</TD>\n<TD>%s</TD>\n</TR>\n", pmsdThis->pcPdbClass);
 
745
  }
 
746
  
 
747
  fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
748
  pvnThis = ValNodeFindNext(pmsdThis->pGraphDescr,NULL,BiomolDescr_pdb_comment);
 
749
  if (pvnThis)
 
750
    fprintf(File, "<strong>PDB Title</strong>:</TD>\n<TD>%s</TD>\n</TR>\n", pmsdThis->pcChemName);
 
751
  else
 
752
    fprintf(File, "<strong>PDB Compound</strong>:</TD>\n<TD>%s</TD>\n</TR>\n", pmsdThis->pcChemName);
 
753
  /* insert a blank row in the table, for spacing */
 
754
  fprintf(File, "<TR>\n<TD VALIGN=TOP NOWRAP> </TD>\n<TD><BR></TD>\n</TR>\n");
 
755
 
 
756
  /* Note: The table continues with "Sequence Neighbors" and "Structure Neighbors". */
 
757
  return;
 
758
 
 
759
}
 
760
 
 
761
 
 
762
 
 
763
static void
 
764
PrintStructureView(PDNMS ModelStruc, FILE *File)
 
765
{
 
766
        PMSD pmsd;
 
767
      
 
768
        if (ModelStruc == NULL) return;         
 
769
 
 
770
        pmsd = (PMSD) ModelStruc->data.ptrvalue;
 
771
        fprintf(File, "<FORM METHOD=POST ACTION=\"%s%s\">\n", URLcgi, CGIName);
 
772
        fprintf(File, "<INPUT TYPE=HIDDEN NAME=uid VALUE=%ld>\n", (long) pmsd->iMMDBid);
 
773
        WWWPrintFileData(CODEFILE, File);
 
774
}
 
775
 
 
776
 
 
777
 
 
778
/* This routine creates the last two rows of the table started by PrintStructureInfo.
 
779
 * These last two rows contain the links for the "Sequence Neighbors" of the chains of
 
780
 * the structure, and for "Structure Neighbors" of the chains and domains of the
 
781
 * structure.  The first part of the code is very similar to PrintMolecularContents.
 
782
 * The 2nd part creates the links for the VAST structure neighbors.
 
783
 *
 
784
 * Note there are 3 different formats for Vast neighbor links, purely for cosmetics.
 
785
 */
 
786
 
 
787
#define VastLink1       "<A HREF=\"%s?uid=%ld&chaindom=%ld\">%s</A>"
 
788
#define VastLink2       "<A HREF=\"%s?uid=%ld&chaindom=%ld\">%s.%d</A>"
 
789
#define VastLink3       "<A HREF=\"%s?uid=%ld&chaindom=%ld\">%d</A>"
 
790
 
 
791
static void
 
792
PrintStructureLinks(PDNMS ModelStruc, FILE *gdf)
 
793
{
 
794
        PMSD pmsd;
 
795
        PDNMM pdnmm;
 
796
        PMMD pmmd;
 
797
        PDNMG pdnmg;
 
798
        PMGD pmgd;
 
799
        Int2 cnt, pflg;
 
800
        Int4 uid, ichn, idom, chndom, chndom0;
 
801
 
 
802
 
 
803
 
 
804
        if (ModelStruc == NULL)
 
805
                return;
 
806
 
 
807
        /* insert a blank row in the table, for spacing */
 
808
        fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP> </TD>\n<TD> </TD>\n</TR>\n");
 
809
 
 
810
        /* protein chains */
 
811
        pmsd = (PMSD) ModelStruc->data.ptrvalue;
 
812
 
 
813
        for (pdnmm = pmsd->pdnmmHead, cnt = pflg = 0; pdnmm != NULL; pdnmm = pdnmm->next) {
 
814
                pmmd = (PMMD) pdnmm->data.ptrvalue;
 
815
 
 
816
                if ((pmmd->bWhat) & AM_PROT) {
 
817
                        if ((pmmd->iResCount <= 1) || (pmmd->iGi <= 0))
 
818
                                continue;
 
819
 
 
820
                        if (cnt++ == 0) {
 
821
                                pflg = 1;
 
822
                                fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
823
                                fprintf(gdf, "<strong>Sequence Neighbors:</strong></TD>\n<TD>");
 
824
 
 
825
                                if (pmmd->pcMolName[0] == ' ')
 
826
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'p',
 
827
                                                "(single chain)");
 
828
                                else
 
829
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'p',
 
830
                                                pmmd->pcMolName);
 
831
                        }
 
832
                        else {
 
833
                                fprintf(gdf, ", ");
 
834
                                fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'p', 'p',
 
835
                                        pmmd->pcMolName);
 
836
                        }
 
837
                }
 
838
        }
 
839
 
 
840
        /* nucleotide chains */
 
841
        for (pdnmm = pmsd->pdnmmHead; pdnmm != NULL; pdnmm = pdnmm->next) {
 
842
                pmmd = (PMMD) pdnmm->data.ptrvalue;
 
843
 
 
844
                if ((pmmd->bWhat) & (AM_DNA | AM_RNA)) {
 
845
                        if ((pmmd->iResCount <= 1) || (pmmd->iGi <= 0))
 
846
                                continue;
 
847
 
 
848
                        if (cnt++ == 0) {
 
849
                                fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
850
                                fprintf(gdf, "<strong>Sequence Neighbors:</strong></TD>\n<TD>");
 
851
 
 
852
                                if (pmmd->pcMolName[0] == ' ')
 
853
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'n',
 
854
                                                "(single chain)");
 
855
                                else
 
856
                                        fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'n',
 
857
                                                pmmd->pcMolName);
 
858
                        }
 
859
                        else {
 
860
                                fprintf(gdf, ", ");
 
861
                                fprintf(gdf, EntrezLink, ENTREZurl, (long) pmmd->iGi, 'n', 'n',
 
862
                                        pmmd->pcMolName);
 
863
                        }
 
864
                }
 
865
        }
 
866
 
 
867
        if (pflg)
 
868
                fprintf(gdf, "</TD>\n</TR>\n");
 
869
 
 
870
        if (pmsd->pdnmlModels == NULL) {
 
871
                /* end the table */
 
872
                fprintf(gdf, "</TABLE>\n<BR>\n");
 
873
                return;
 
874
        }
 
875
 
 
876
        /* VAST neighbors: loop over protein chains */
 
877
        uid = (Int4) pmsd->iMMDBid;
 
878
 
 
879
        for (pdnmm = pmsd->pdnmmHead, cnt = ichn = 0; pdnmm != NULL; pdnmm = pdnmm->next) {
 
880
                pmmd = (PMMD) pdnmm->data.ptrvalue;
 
881
                ichn++;
 
882
 
 
883
                if ((pmmd->bWhat) & AM_PROT) {
 
884
                        if ((pmmd->iResCount <= 1) || (pmmd->iGi <= 0))
 
885
                                continue;
 
886
 
 
887
                        chndom0 = 10000*uid + 100*(Int4) ichn;
 
888
 
 
889
                        if (cnt++ == 0) {
 
890
                                fprintf(gdf, "<TR>\n<TD VALIGN=TOP NOWRAP>");
 
891
                                fprintf(gdf, "<strong>Structure Neighbors:</strong></TD>\n<TD>");
 
892
 
 
893
                                if (pmmd->pcMolName[0] == ' ')
 
894
                                        fprintf(gdf, VastLink1, VASTurl, (long) uid, (long) chndom0,
 
895
                                                "(single chain)");
 
896
                                else
 
897
                                        fprintf(gdf, VastLink1, VASTurl, (long) uid, (long) chndom0,
 
898
                                                pmmd->pcMolName);
 
899
                        }
 
900
                        else {
 
901
                                fprintf(gdf, ", ");
 
902
                                fprintf(gdf, VastLink1, VASTurl, (long) uid, (long) chndom0,
 
903
                                        pmmd->pcMolName);
 
904
                        }
 
905
 
 
906
                        /* now loop over domains */
 
907
                        idom = 0;
 
908
 
 
909
                        for (pdnmg = pmmd->pdnmgHead; pdnmg != NULL; pdnmg = pdnmg->next) {
 
910
                                pmgd = (PMGD) pdnmg->data.ptrvalue;
 
911
 
 
912
                                if (pmgd->iDomain > idom) {
 
913
                                        idom = (Int4) pmgd->iDomain;
 
914
                                        chndom = chndom0 + idom;
 
915
                                        fprintf(gdf, ", ");
 
916
 
 
917
                                        if (pmmd->pcMolName[0] == ' ')
 
918
                                                fprintf(gdf, VastLink3, VASTurl, (long) uid,
 
919
                                                        (long) chndom, (int) idom);
 
920
                                        else
 
921
                                                fprintf(gdf, VastLink2, VASTurl, (long) uid,
 
922
                                                        (long) chndom, pmmd->pcMolName, (int) idom);
 
923
                                }
 
924
                        }
 
925
                }
 
926
        }
 
927
 
 
928
        /* end the table */
 
929
        fprintf(gdf, "</TD>\n</TR>\n</TABLE>\n");
 
930
 
 
931
} /* end PrintStructureLinks */
 
932
 
 
933
 
 
934
 
 
935
/* Utility routine needed below.  Return a vector marking the distinct integers
 
936
 * appearing in the input array.  A naive algorithm is used since we never expect
 
937
 * the array size 'n' to be large.  If an entry vec[i] is 0, this means it is
 
938
 * a duplicate.  If vec[i] > 0 then this is the first occurrence of the number,
 
939
 * and the value vec[i] is the number of times the corresponding number nums[i]
 
940
 * appears.
 
941
 */
 
942
 
 
943
static void
 
944
mark_distinct(Int4Ptr nums, Int2Ptr vec, Int2 n)
 
945
{
 
946
        Int2 i, j, found;
 
947
 
 
948
        if (n == 0)
 
949
                return;
 
950
 
 
951
        /* initialize the vector */
 
952
        for (i = 0; i < n; i++)
 
953
                vec[i] = 1;
 
954
 
 
955
        for (i = 1; i < n; i++) {
 
956
                for (j = 0; j < i; j++) {
 
957
                        if (nums[i] == nums[j]) {
 
958
                                vec[i] = 0;
 
959
                                vec[j]++;
 
960
                                break;
 
961
                        }
 
962
                }
 
963
        }
 
964
 
 
965
} /* end mark_distinct */
 
966
 
 
967
 
 
968
 
 
969
/* Output the taxonomy data, per chain, and also links to the taxonomy database.
 
970
 * Return '1' in case there was a non-trivial taxonomy assignment, '0' if there
 
971
 * was no taxonomy data found.  The success or failure is passed ultimately to
 
972
 * PrintStructureInfo, where the PDB source is displayed in case no taxonomy data
 
973
 * was found.
 
974
 */
 
975
 
 
976
static Int2
 
977
SaveTaxonomyInfo(BiostrucPtr bsp, CharPtr save, Int2 n, CharPtr pname)
 
978
{
 
979
        MoleculeGraphPtr mgp;
 
980
        Int4 i, j, k, cnt, txcnt, uacnt;
 
981
        ValNodePtr descr, db;
 
982
        CharPtr chainp, name, names[MAX_CHAINS];
 
983
        Char chn, chns[MAX_CHAINS], org_names[MAX_TBUFF];
 
984
        Int2 flg, name_here, cnt_vec[MAX_CHAINS];
 
985
        BioSourcePtr bios;
 
986
        OrgRefPtr org;
 
987
        Int4 molecule_type, tax_id, tax_ids[MAX_CHAINS];
 
988
 
 
989
 
 
990
 
 
991
        if ((bsp == NULL) || (save == NULL))
 
992
                return 0;
 
993
 
 
994
        for (i = 0; i < MAX_TBUFF; i++)
 
995
                org_names[i] = '\0';
 
996
 
 
997
        name_here = 0;
 
998
        mgp = bsp->chemical_graph->molecule_graphs;
 
999
 
 
1000
        for (cnt = uacnt = 0; (mgp != NULL) && (cnt < MAX_CHAINS); mgp = mgp->next) {
 
1001
                /* zap the OrgRefPtr, there may be no taxonomy for this chain! */
 
1002
                org = NULL;
 
1003
 
 
1004
                for (descr = mgp->descr; descr != NULL; descr = descr->next) {
 
1005
                        if (descr->choice == BiomolDescr_molecule_type)
 
1006
                                molecule_type = descr->data.intvalue;
 
1007
 
 
1008
                        if (descr->choice == BiomolDescr_name) {
 
1009
                                chainp = descr->data.ptrvalue;
 
1010
                                chn = *chainp;
 
1011
                        }
 
1012
 
 
1013
                        if (descr->choice == BiomolDescr_organism) {
 
1014
                                bios = descr->data.ptrvalue;
 
1015
                                org = (OrgRefPtr) bios->org;
 
1016
                        }
 
1017
                }
 
1018
 
 
1019
                if ((molecule_type == Molecule_type_protein) ||
 
1020
                        (molecule_type == Molecule_type_dna) ||
 
1021
                        (molecule_type == Molecule_type_rna)) {
 
1022
 
 
1023
                        /* save the chain and get the taxonomy data */
 
1024
                        chns[cnt] = (chn == ' ') ? '\n' : chn;
 
1025
 
 
1026
                        if (org != NULL) {
 
1027
                                DbtagPtr dbtag;
 
1028
                                ObjectIdPtr object_id;
 
1029
 
 
1030
                                for (db = org->db, tax_id = -1; db != NULL; db = db->next) {
 
1031
                                        if (db->choice == TAX_ID_CHOICE_NUM) {
 
1032
                                                dbtag = db->data.ptrvalue;
 
1033
                                                tax_id = dbtag->tag->id;
 
1034
                                                break;
 
1035
                                        }
 
1036
                                }
 
1037
 
 
1038
                                names[cnt] = &org_names[name_here];
 
1039
 
 
1040
                                if (org->taxname != NULL) {
 
1041
                                        StringCat(names[cnt], org->taxname);
 
1042
                                        name_here += StrLen(org->taxname) + 1;
 
1043
                                }
 
1044
                                else if (org->common != NULL) {
 
1045
                                        StringCat(names[cnt], org->common);
 
1046
                                        name_here += StrLen(org->common) + 1;
 
1047
                                }
 
1048
                                else {
 
1049
                                        StringCat(names[cnt], "(unassigned)");
 
1050
                                        name_here += 13;
 
1051
                                }
 
1052
 
 
1053
                                tax_ids[cnt++] = tax_id;
 
1054
                        }
 
1055
                        else {
 
1056
                                /* track the number of unassigned chains */
 
1057
                                uacnt++;
 
1058
                                names[cnt] = &org_names[name_here];
 
1059
                                StringCat(names[cnt], "(unassigned)");
 
1060
                                name_here += 13;
 
1061
                                tax_ids[cnt++] = -1;
 
1062
                        }
 
1063
                }
 
1064
        }
 
1065
 
 
1066
        /* zap the save area */
 
1067
        for (i = 0; i < n; i++)
 
1068
                save[i] = '\0';
 
1069
 
 
1070
        /* flag the taxonomy section */
 
1071
        sprintf(save, "<TR>\n<TD VALIGN=TOP NOWRAP><strong>Taxonomy</strong>:</TD>\n");
 
1072
 
 
1073
        if (cnt == 0) {
 
1074
                k = StrLen(save);
 
1075
                sprintf(&save[k], "<TD>(unassigned)</TD>\n");
 
1076
                return 1;
 
1077
        }
 
1078
 
 
1079
        /* group chains by organism type, i.e. tax id */
 
1080
        mark_distinct(tax_ids, cnt_vec, cnt);
 
1081
 
 
1082
        for (i = flg = 0; i < cnt; i++) {
 
1083
                if (cnt_vec[i] == 0)
 
1084
                        continue;
 
1085
 
 
1086
                tax_id = tax_ids[i];
 
1087
                k = StrLen(save);
 
1088
 
 
1089
                if (flg)
 
1090
                        sprintf(&save[k], "; ");
 
1091
                else {
 
1092
                        sprintf(&save[k], "<TD>");
 
1093
                        flg = 1;
 
1094
                }
 
1095
 
 
1096
                /* collect chains with the same tax id */
 
1097
                txcnt = cnt_vec[i];
 
1098
                k = StrLen(save);
 
1099
 
 
1100
                /*****
 
1101
                if (txcnt > 1)
 
1102
                        sprintf(&save[k], "chains ");
 
1103
                else if (chns[i] != '\n')
 
1104
                        sprintf(&save[k], "chain ");
 
1105
                *****/
 
1106
 
 
1107
                for (j = i; (j < cnt) && (txcnt > 0); j++) {
 
1108
                        if (tax_ids[j] != tax_id)
 
1109
                                continue;
 
1110
 
 
1111
                        k = StrLen(save);
 
1112
 
 
1113
                        if (txcnt-- > 1)
 
1114
                                sprintf(&save[k], "%c, ", chns[j]);
 
1115
                        else
 
1116
                                sprintf(&save[k], "%c", chns[j]);
 
1117
                }
 
1118
 
 
1119
                k = StrLen(save);
 
1120
 
 
1121
                if (tax_id > 0) {
 
1122
                        if (chns[i] != '\n')
 
1123
                                sprintf(&save[k], "&nbsp <A HREF=\"");
 
1124
                        else
 
1125
                                sprintf(&save[k], "<A HREF=\"");
 
1126
 
 
1127
                        k = StrLen(save);
 
1128
                        sprintf(&save[k], TAXurl, tax_id);
 
1129
                        k = StrLen(save);
 
1130
                        sprintf(&save[k], "\">%s</A> ", names[i]);
 
1131
                }
 
1132
                else
 
1133
                        sprintf(&save[k], " (unassigned)");
 
1134
        }
 
1135
 
 
1136
        k = StrLen(save);
 
1137
        sprintf(&save[k], "</TD>\n</TR>\n");
 
1138
        return 1;
 
1139
 
 
1140
} /* end SaveTaxonomyInfo */
 
1141
 
 
1142
 
 
1143
 
 
1144
/* Old version of DumpMime, preserved for compatibility with Cn3D v1.0. */
 
1145
 
 
1146
static void DumpMime_v1(AsnIoPtr aip, CharPtr title, Int2 style, Int2 choice, VoidPtr datum)
 
1147
{
 
1148
  NcbiMimeAsn1Ptr mime;
 
1149
  EntrezGeneralPtr egp;
 
1150
 
 
1151
  mime = (NcbiMimeAsn1Ptr) ValNodeNew(NULL);
 
1152
  mime->choice = NcbiMimeAsn1_entrez;
 
1153
  egp = EntrezGeneralNew();
 
1154
  mime->data.ptrvalue = (VoidPtr) egp;
 
1155
  egp->Data_data = ValNodeNew(NULL);
 
1156
  egp->title = StringSave(title);
 
1157
  egp->style = style;
 
1158
  egp->Data_data->choice = choice;
 
1159
  egp->Data_data->data.ptrvalue = datum;
 
1160
 
 
1161
  NcbiMimeAsn1AsnWrite(mime, aip, NULL);
 
1162
  AsnIoFlush(aip);
 
1163
 
 
1164
  egp->Data_data->data.ptrvalue = NULL; /* for clean free */
 
1165
  NcbiMimeAsn1Free(mime);
 
1166
 
 
1167
} /* end DumpMime_v1 */
 
1168
 
 
1169
 
 
1170
 
 
1171
                                   /* yanli */
 
1172
static void DumpMime(AsnIoPtr aip, CharPtr title, VoidPtr datum)
 
1173
{
 
1174
  NcbiMimeAsn1Ptr mime;
 
1175
  EntrezGeneralPtr egp;
 
1176
 
 
1177
  mime = (NcbiMimeAsn1Ptr) ValNodeNew(NULL);
 
1178
  mime->choice = NcbiMimeAsn1_strucseq;
 
1179
  mime->data.ptrvalue= datum;
 
1180
 
 
1181
  NcbiMimeAsn1AsnWrite(mime, aip, NULL);
 
1182
  AsnIoFlush(aip);
 
1183
 
 
1184
  mime->data.ptrvalue=NULL ; /* for clean free */
 
1185
  NcbiMimeAsn1Free(mime);
 
1186
}
 
1187
 
 
1188
 
 
1189
 
 
1190
#define LAUNCH_VIEWER   0
 
1191
#define SEE_FILE        1
 
1192
#define SAVE_FILE       2
 
1193
#define SEND_FILE   4
 
1194
 
 
1195
/* Handle the various View/See/Save file options. */
 
1196
 
 
1197
static Int4
 
1198
SendStructureMIME(Char Filetype, Int4 uid, Int4 Mime, Int4 Complexity,
 
1199
                  Int4 Models, Int4 Color, Int4 Render)
 
1200
{
 
1201
  BiostrucPtr bsp;
 
1202
  PDNMS ModelStruc;
 
1203
  Int4 count, i4Sent = 0, error_flag = 0;
 
1204
  AsnIoPtr aip;
 
1205
        
 
1206
  /* yanli start */
 
1207
  NcbiMimeAsn1Ptr mime;
 
1208
  BiostrucSeqPtr bssp;
 
1209
  BiostrucGraphPtr bsgp;
 
1210
  MoleculeGraphPtr mgp;
 
1211
  SeqIdPtr sip;
 
1212
  SeqEntryPtr sep;
 
1213
  Int2 retcode = 3;
 
1214
  Int4 This_uid = 0;
 
1215
 
 
1216
  if ((bsp = MMDBBiostrucGet((DocUid) uid, Complexity, Models)) == NULL) {
 
1217
    printf("Content-type: text/html\r\n\r\n");
 
1218
    printf("<h2>Error</h2>\n");
 
1219
    printf("Structure with UID = %ld failed to fetch on Server<p>\n", (long) uid);
 
1220
    printf("No such structure available on this MMDB server.\n");
 
1221
    return 0;
 
1222
  }
 
1223
 
 
1224
  /*yanli*/
 
1225
  bsgp = bsp->chemical_graph;
 
1226
  mgp = bsgp->molecule_graphs;
 
1227
  sip = mgp->seq_id;
 
1228
  /*  This_uid =  EntrezFindSeqId(sip);    */
 
1229
  while(sip != NULL){
 
1230
    if(sip->choice == 12) This_uid = sip->data.intvalue;
 
1231
    sip = sip->next;
 
1232
  }
 
1233
  sep = EntrezSeqEntryGet(This_uid, retcode);
 
1234
     
 
1235
  bssp = BiostrucSeqNew();
 
1236
  bssp->structure = bsp;
 
1237
  ValNodeLink(&(bssp->sequences), sep);     
 
1238
        
 
1239
  mime = (NcbiMimeAsn1Ptr) ValNodeNew(NULL);    /* yanli */
 
1240
  mime->choice = NcbiMimeAsn1_strucseq;
 
1241
  mime->data.ptrvalue = bssp;
 
1242
  /*---yanli*/
 
1243
        
 
1244
  /* the following headers are format-independent */
 
1245
  switch (Mime) {
 
1246
  case SAVE_FILE:
 
1247
    printf("Content-type: application/octet-stream\r\n\r\n");
 
1248
    break;
 
1249
  case SEE_FILE:
 
1250
    printf ("Content-type: text/html\r\n\r\n");
 
1251
    printf ("<HTML><PRE>\r\n");
 
1252
    break;
 
1253
  case SEND_FILE:
 
1254
    printf ("Content-type: text/html\r\n\r\n");
 
1255
    break;
 
1256
  }
 
1257
 
 
1258
  if (Filetype == 'j') {
 
1259
    /* Cn3D v2.0 asn.1 format */
 
1260
    switch (Mime) {
 
1261
    case LAUNCH_VIEWER:
 
1262
      printf ("Content-type: chemical/ncbi-asn1-binary\r\n\r\n");
 
1263
      aip = AsnIoNew(ASNIO_BIN_OUT, stdout, NULL, NULL, NULL);
 
1264
      DumpMime(aip, "MIME", bssp);    
 
1265
      break;
 
1266
    case SAVE_FILE:
 
1267
      aip = AsnIoNew(ASNIO_BIN_OUT, stdout, NULL, NULL, NULL);   
 
1268
      NcbiMimeAsn1AsnWrite(mime, aip, NULL);
 
1269
      break;
 
1270
    case SEE_FILE:
 
1271
      aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
 
1272
      NcbiMimeAsn1AsnWrite(mime, aip, NULL);
 
1273
      break;
 
1274
    case SEND_FILE:
 
1275
      aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
 
1276
      NcbiMimeAsn1AsnWrite(mime, aip, NULL);
 
1277
      break;
 
1278
    }
 
1279
 
 
1280
    AsnIoReset(aip);
 
1281
    AsnIoClose(aip);
 
1282
  }
 
1283
  else if (Filetype == 'i') {
 
1284
    /* Cn3D v1.0 asn.1 format.  Included for backwards compatibility, in case users
 
1285
     * want to stick with their old Cn3D for some reason.
 
1286
     */
 
1287
    switch (Mime) {
 
1288
    case LAUNCH_VIEWER:
 
1289
      printf ("Content-type: chemical/ncbi-asn1-binary\r\n\r\n");
 
1290
      /* printf ("Content-disposition: filename=\"%d.val\"\r\n\r\n", uid);*/
 
1291
      aip = AsnIoNew(ASNIO_BIN_OUT, stdout, NULL, NULL, NULL);
 
1292
      DumpMime_v1(aip, "MIME", Entrez_style_report, Data_data_structure, bsp);
 
1293
      break;
 
1294
    case SAVE_FILE:
 
1295
      aip = AsnIoNew(ASNIO_BIN_OUT, stdout, NULL, NULL, NULL);
 
1296
      BiostrucAsnWrite(bsp, aip, NULL);
 
1297
      break;
 
1298
    case SEE_FILE:
 
1299
      aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
 
1300
      BiostrucAsnWrite(bsp, aip, NULL);
 
1301
      break;
 
1302
    case SEND_FILE:
 
1303
      aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
 
1304
      BiostrucAsnWrite(bsp, aip, NULL);
 
1305
      break;
 
1306
    }
 
1307
    
 
1308
    AsnIoReset(aip);
 
1309
    AsnIoClose(aip);
 
1310
  }
 
1311
  else if (Filetype == 'r') {
 
1312
    /* RasMol format */
 
1313
    if (Mime == LAUNCH_VIEWER)
 
1314
      printf ("Content-type: chemical/x-pdb\r\n\r\n");
 
1315
    /*      printf ("Content-disposition: filename=\"%d.pdb\"\r\n\r\n", uid);*/
 
1316
    
 
1317
    ModelStruc = MakeAModelstruc(bsp);
 
1318
    bssp->structure = NULL;  /* already linked into modelstruc */
 
1319
    WritePDBAllModel(ModelStruc, stdout);
 
1320
    ClearStructures();
 
1321
  }
 
1322
  else if (Filetype == 'k') {
 
1323
    /* Mage format */
 
1324
    if (Mime == LAUNCH_VIEWER)
 
1325
      printf ("Content-type: chemical/x-kinemage\r\n\r\n");
 
1326
    /* printf ("Content-disposition: filename=\"%d.kin\"\r\n\r\n", uid);*/
 
1327
    
 
1328
    ModelStruc = MakeAModelstruc(bsp);
 
1329
    bssp->structure = NULL;  /* already linked into modelstruc */
 
1330
    WriteKinAllModel(ModelStruc, stdout, Color, Render);
 
1331
    ClearStructures();
 
1332
  }
 
1333
  else {
 
1334
    error_flag = 1;
 
1335
    printf("Content-type: text/html\r\n\r\n");
 
1336
    printf("<h2>Error</h2>\n");
 
1337
    printf("Unknown file type on structure fetch.<p>\n");
 
1338
    printf("Contact %s\n", MAILTO);
 
1339
  }
 
1340
  
 
1341
  if ((Mime == SEE_FILE) && (!error_flag))
 
1342
    printf ("</PRE></HTML>\r\n");
 
1343
  
 
1344
  NcbiMimeAsn1Free(mime);           /* yanli */
 
1345
  return 1;
 
1346
  
 
1347
} /* end SendStructureMIME */
 
1348
 
 
1349
 
 
1350
static Int4  SendSummaryPage(Int4Ptr Uids, Int4 NumToDisplay, Int4 Save)
 
1351
{
 
1352
  BiostrucPtr bsp;
 
1353
  ValNodePtr descr;
 
1354
  PDNMS ModelStruc;
 
1355
  Int4 uid, count, i4Sent = 0;
 
1356
  Int2 taxret;
 
1357
  Char tax_save[MAX_TBUFF];
 
1358
  FILE *sendfile;
 
1359
   
 
1360
  if (Save == 2)
 
1361
    printf ("Content-type: application/octet-stream\n\n");  
 
1362
  else
 
1363
    printf ("Content-type: text/html\n\n");  
 
1364
     
 
1365
  if (!GlobalNonHtmlOutput) {
 
1366
    if (GlobalTitles) 
 
1367
      WWWPrintFileData(HEADFILE,  stdout);
 
1368
    else
 
1369
      printf ( "<html>\n<title>MMDB Structure Summary</title><body>"); 
 
1370
  }
 
1371
  for (count = 0; count < NumToDisplay; count++)
 
1372
    {
 
1373
      uid = Uids[count];
 
1374
      bsp = MMDBBiostrucGet((DocUid) uid,3,0);
 
1375
      if (bsp == NULL)
 
1376
        {
 
1377
          printf ("%sNo structure found for UID %ld.%s\n",
 
1378
                  GlobalNonHtmlOutput ? "\n" : "<P><h4>",uid,
 
1379
                  GlobalNonHtmlOutput ? "\n" : "</h4><P>");
 
1380
          if (!GlobalNonHtmlOutput) 
 
1381
            printf ("<HR SIZE=5 NOSHADE><BR>\n");
 
1382
          continue;
 
1383
        }
 
1384
 
 
1385
      if (!GlobalNonHtmlOutput) {
 
1386
        CharPtr name;
 
1387
 
 
1388
        for (descr = bsp->descr, name = NULL; descr != NULL; descr = descr->next) {
 
1389
          if (descr->choice == BiomolDescr_name) {
 
1390
            name = descr->data.ptrvalue;
 
1391
            break;
 
1392
          }
 
1393
        }
 
1394
        taxret = SaveTaxonomyInfo(bsp, tax_save, MAX_TBUFF, name);
 
1395
      }
 
1396
 
 
1397
      ModelStruc = MakeAModelstruc(bsp);  
 
1398
      PrintStructureInfo(ModelStruc, stdout, (taxret) ? tax_save : NULL);
 
1399
 
 
1400
      if (!GlobalNonHtmlOutput) /* Rest of View/Save stuff */
 
1401
        {
 
1402
          PrintStructureLinks(ModelStruc, stdout);
 
1403
          PrintStructureView(ModelStruc,  stdout);
 
1404
          printf ("<HR SIZE=5 NOSHADE><BR>\n");
 
1405
        }
 
1406
         
 
1407
      ClearStructures();
 
1408
    }
 
1409
  if (!GlobalNonHtmlOutput)
 
1410
    if (GlobalTitles)
 
1411
      WWWPrintFileData(TAILFILE, stdout);
 
1412
    else
 
1413
      printf ("</html>");
 
1414
  
 
1415
  return 1;
 
1416
} /* SendSummaryPage() */
 
1417
 
 
1418
 
 
1419
static void SendGetNetscape(void) {
 
1420
 
 
1421
  printf("HTTP/1.0 200 OK\r\n");
 
1422
  printf("MIME-Version: 1.0\r\n");
 
1423
  printf("Content-type: text/html\r\n\r\n"); 
 
1424
  printf("<BR><BR><B>Sorry, you need a <a href=\"http://home.netscape.com/\">",
 
1425
         "Netscape</a> version 2 or higher WWW-browser "
 
1426
         "to be able to enter this page</B><BR>\n\n");
 
1427
}  /* SendGetNetscape */
 
1428
 
 
1429
 
 
1430
 
 
1431
 
 
1432
static Boolean InitMMDBSrv(void)
 
1433
{
 
1434
  Int2 i;
 
1435
 
 
1436
  HTMLPath[0] = '\0';
 
1437
  URLBase[0] = '\0';
 
1438
  URLHelp[0] = '\0';
 
1439
  CGIPath[0] = '\0';
 
1440
  URLcgi[0] = '\0';
 
1441
  CGIName[0] = '\0';
 
1442
  DATApath[0] = '\0';
 
1443
  LOGpath[0] = '\0';
 
1444
  ENTREZurl[0] = '\0';
 
1445
  VASTurl[0] = '\0';
 
1446
  MMDBurl[0] = '\0';
 
1447
  PDBurl[0] = '\0';
 
1448
  TDBurl[0] = '\0';
 
1449
  MAILto[0] = '\0';
 
1450
 
 
1451
  for (i = 0; i < PATH_MAX; i++)
 
1452
    TAXurl[i] = '\0';
 
1453
 
 
1454
  FindPath("mmdb", "MMDBSRV", "HTMLPath", HTMLPath, PATH_MAX);
 
1455
  if (HTMLPath[0] == '\0')
 
1456
    {
 
1457
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no HTMLPath...\n");
 
1458
        return FALSE;
 
1459
    } 
 
1460
  GetAppParam("mmdb", "MMDBSRV", "URLBase", "", URLBase, PATH_MAX);
 
1461
  if (URLBase[0] == '\0')
 
1462
    {
 
1463
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no URLBase...\n");
 
1464
        return FALSE;
 
1465
    }
 
1466
  GetAppParam("mmdb", "MMDBSRV", "URLHelp", "", URLHelp, PATH_MAX);
 
1467
  if (URLHelp[0] == '\0')
 
1468
    {
 
1469
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no URLHelp...\n");
 
1470
        return FALSE;
 
1471
    }
 
1472
  FindPath("mmdb", "MMDBSRV", "CGIPath", CGIPath, PATH_MAX);
 
1473
  if (CGIPath[0] == '\0')
 
1474
    {
 
1475
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no CGIPath...\n");
 
1476
        return FALSE;
 
1477
    }
 
1478
    
 
1479
  GetAppParam("mmdb", "MMDBSRV", "URLcgi", "", URLcgi, PATH_MAX);
 
1480
  if (URLcgi[0] == '\0')
 
1481
    {
 
1482
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no URLcgi...\n");
 
1483
        return FALSE;
 
1484
    }
 
1485
  GetAppParam("mmdb", "MMDBSRV", "CGIName", "", CGIName, (size_t) 256 *  sizeof(char));
 
1486
  if (CGIName[0] == '\0')
 
1487
    {
 
1488
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no CGIName...\n");
 
1489
        return FALSE;
 
1490
    }
 
1491
  FindPath("mmdb", "MMDBSRV", "DATApath", DATApath, PATH_MAX);
 
1492
  if (DATApath[0] == '\0')
 
1493
    {
 
1494
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no DATApath...\n");
 
1495
        return FALSE;
 
1496
    }
 
1497
  ARROW[0] = '\0';
 
1498
  sprintf(ARROW, "<IMG alt=\"o\" SRC=\"%sarrow.gif\" ALT=\"o\" BORDER=0>\n", URLBase);
 
1499
 
 
1500
  FindPath("mmdb", "MMDBSRV", "LOGpath", LOGpath, PATH_MAX);
 
1501
  if (LOGpath[0] == '\0')
 
1502
    {
 
1503
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no LOGpath...\n");
 
1504
        return FALSE;
 
1505
    }
 
1506
  GetAppParam("mmdb", "MMDBSRV", "ENTREZurl", "", ENTREZurl, PATH_MAX);
 
1507
  if (ENTREZurl[0] == '\0')
 
1508
    {
 
1509
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no ENTREZurl...\n");
 
1510
        return FALSE;
 
1511
    } 
 
1512
  GetAppParam("mmdb", "MMDBSRV", "VASTurl", "", VASTurl, PATH_MAX);
 
1513
  if (VASTurl[0] == '\0')
 
1514
    {
 
1515
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no VASTurl...\n");
 
1516
        return FALSE;
 
1517
    }
 
1518
  GetAppParam("mmdb", "MMDBSRV", "MMDBurl", "", MMDBurl, PATH_MAX);
 
1519
  if (MMDBurl[0] == '\0')
 
1520
    {
 
1521
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no MMDBurl...\n");
 
1522
        return FALSE;
 
1523
    }
 
1524
  GetAppParam("mmdb", "MMDBSRV", "PDBurl", "", PDBurl, PATH_MAX);
 
1525
  if (PDBurl[0] == '\0')
 
1526
    {
 
1527
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no PDBurl...\n");
 
1528
        return FALSE;
 
1529
    }
 
1530
  GetAppParam("mmdb", "MMDBSRV", "TDBurl", "", TDBurl, PATH_MAX);
 
1531
  if (TDBurl[0] == '\0')
 
1532
    {
 
1533
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no TDBurl...\n");
 
1534
        return FALSE;
 
1535
    }  
 
1536
  GetAppParam("mmdb", "TAXLINK", "TAXurl", "", TAXurl, PATH_MAX);
 
1537
  if (TAXurl[0] == '\0')
 
1538
    {
 
1539
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nTAXLINK section no TAXurl...\n");
 
1540
        return FALSE;
 
1541
    }
 
1542
  StringCat(TAXurl, "?id=%d&lvl=0");
 
1543
  GetAppParam("mmdb", "MMDBSRV", "MAILto", "", MAILto, PATH_MAX);
 
1544
  if (MAILto[0] == '\0')
 
1545
    {
 
1546
        ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDBSRV section no Mailto...\n");
 
1547
        return FALSE;
 
1548
    }
 
1549
 
 
1550
  MAILTO[0] = '\0';
 
1551
  sprintf(MAILTO, "&lt <a href=\"%s\">%s</a> &gt",  MAILto,  MAILto);
 
1552
 
 
1553
  return TRUE;
 
1554
}
 
1555
 
 
1556
   
 
1557
 
 
1558
/************************************************************************
 
1559
 *
 
1560
 *  Int2 Main() - main function     
 
1561
 *
 
1562
 ***********************************************************************/
 
1563
 
 
1564
 
 
1565
 
 
1566
Int2 Main ()
 
1567
{
 
1568
  Boolean               Netscape2_OK = FALSE;
 
1569
  time_t                time_now;
 
1570
  CharPtr               TimeNowStr = NULL;
 
1571
  WWWErrorCode          error;
 
1572
  Int4                  IndexArgs = -1;
 
1573
  Int4                  Complex = 0;  
 
1574
  Int4                  MaxModels = 1;
 
1575
  Int4                  KinColor = 0;
 
1576
  Int4                  KinRender = 0;
 
1577
  Int4                  Save = 0;
 
1578
  Char                  DispOpt = '\0';
 
1579
  Int4                  Form = 0;
 
1580
  CharPtr               pcThis = NULL,  pcName = NULL;
 
1581
  CharPtr               Uids;  /* (list of MMDB ids or PDB ids) */
 
1582
  Int4Ptr               MMDBids = NULL; /* (list of MMDB ids ) */
 
1583
  Int4                  iMMDBids = 0;
 
1584
  Int4                  iCount = 0;
 
1585
  Int4                  iBytes = 0;
 
1586
  Int4                  NumEntries = 0;
 
1587
  ValNodePtr            pvnUIDS = NULL, pvnThis = NULL;
 
1588
  
 
1589
/* this sets up the unix time limit */  
 
1590
 
 
1591
#ifdef OS_UNIX
 
1592
 {
 
1593
  struct rlimit         rl;
 
1594
  getrlimit(RLIMIT_CPU, &rl);
 
1595
  rl.rlim_max = rl.rlim_cur = CPUTIME_MAX;
 
1596
  setrlimit(RLIMIT_CPU, &rl);
 
1597
 }
 
1598
#endif 
 
1599
 
 
1600
  if (!InitMMDBSrv()) {
 
1601
       printf("Content-type: text/html\r\n\r\n");
 
1602
       printf("<H2>MMDBSRV Error</H2>\n - Unable to Open Server Configuration File.<p>\n");
 
1603
       printf("Contact the MMDBSRV administrator for this site.<p>\n");   
 
1604
        exit(1);
 
1605
  }
 
1606
 
 
1607
  StringCat(LOGpath, "mmdbsrv.log");
 
1608
  if( !ErrSetLogfile (LOGpath, ELOG_APPEND) ) {  
 
1609
      printf("Content-type: text/html\r\n\r\n");
 
1610
      printf("<H2>MMDBSRV Error</H2>\n - Unable to Open Log.<p>\n");
 
1611
      printf("Contact MMDBSRV administrator for this site: %s<p>\n",  MAILTO);   
 
1612
        exit(1);
 
1613
  }
 
1614
  
 
1615
  ErrSetLogLevel(SEV_MAX); 
 
1616
  ErrSetOpts (ERR_CONTINUE, ERR_LOG_ON);
 
1617
 
 
1618
 
 
1619
 
 
1620
  if((error = WWWGetArgs(&info)) != WWWErrOk) {
 
1621
      printf("Content-type: text/html\r\n\r\n");
 
1622
      printf("<H2>MMDBSRV Error</H2>\n Failed to Process Posting - Check your GET/POST syntax\n");   
 
1623
    goto earlyout;
 
1624
  }
 
1625
 
 
1626
     
 
1627
 /* if(WWWGetBrowser(info) != NETSCAPE) {   
 
1628
       SendGetNetscape();    
 
1629
       exit(1); 
 
1630
  } */
 
1631
 
 
1632
  if ((!OpenMMDBAPI(0, NULL)) || (!MMDBInit()))
 
1633
    {
 
1634
       ErrLogPrintf("#MMDB Database Access Failed to Initialize.\n"); 
 
1635
       printf("Content-type: text/html\r\n\r\n");
 
1636
       printf("<H2>MMDBSRV Error</H2>\n - Unable to Open MMDB Database on Server.<p>");
 
1637
       printf("Contact MMDBSRV administrator for this site: %s<p>\n",  MAILTO);   
 
1638
        exit(1);
 
1639
    }
 
1640
                                                   /* yanli */
 
1641
   if (EntrezInit("mmdbsrv", NULL, FALSE) == FALSE)    
 
1642
     {
 
1643
       printf("Content-type: text/html\n\n");
 
1644
       printf("<h2>Error</h2>\n");
 
1645
       printf("MMDBSERV: EntrezInit Failed.<p>\n");
 
1646
       return;
 
1647
     }       
 
1648
                                                   /* yanli */
 
1649
/*if( !BioseqFetchInit(TRUE))                    
 
1650
    {
 
1651
      ErrLogPrintf("BioseqFetchInit failed! \n");
 
1652
      printf("Content-type: text/html\r\n\r\n");
 
1653
      printf("<H2>MMDBSRV Error</H2>\n - BioseqFetchInit failed! <p>"); 
 
1654
      printf("Contact MMDBSRV administrator for this site: %s<p>\n",  MAILTO);
 
1655
    exit(1);
 
1656
    }   */
 
1657
 
 
1658
    SeqAsnLoad();
 
1659
    objmimeAsnLoad();
 
1660
 
 
1661
  IndexArgs = -1;
 
1662
  if ((IndexArgs = WWWFindName(info,"db")) >= 0)
 
1663
    {
 
1664
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1665
      DispOpt = pcThis[0];
 
1666
      if ((DispOpt != 't') && (DispOpt != 'T'))
 
1667
        {
 
1668
          printf("Content-type: text/html\r\n\r\n");
 
1669
          printf("<H2>MMDBSRV Error</H2>\n - Wrong Database Type db=%c.<p>\n",  DispOpt );
 
1670
          printf("Try <a href=\"%s\">WWW-Entrez</a> for this<p>\n",  URLHelp);   
 
1671
          goto cleanout;
 
1672
        } 
 
1673
    }
 
1674
 
 
1675
  IndexArgs = -1;
 
1676
  if ((IndexArgs = WWWFindName(info, "form")) >= 0)
 
1677
    {
 
1678
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1679
      if ((!StrToInt4(pcThis,&Form)) || (Form != 6))
 
1680
        {
 
1681
          printf("Content-type: text/html\r\n\r\n");
 
1682
          printf("<H2>MMDBSRV Error</H2>\n - Wrong Form Type form=%s.<p>\n",  pcThis );
 
1683
          printf("<pre>Only supports form=6</pre><p>\n"); 
 
1684
          printf("Try <a href=\"%s\">WWW-Entrez</a> for this<p>\n",  URLHelp);
 
1685
          goto cleanout;
 
1686
        } 
 
1687
    }
 
1688
 
 
1689
 
 
1690
 
 
1691
 
 
1692
 IndexArgs = -1;
 
1693
 if ((IndexArgs = WWWFindName(info, "uid")) < 0)
 
1694
   {
 
1695
       printf("Content-type: text/html\r\n\r\n");
 
1696
       printf("<H2>MMDBSRV Error</H2>\n - No Identifier specified:<p>\n<pre>Missing uid=</pre><p>\n");   
 
1697
       goto cleanout;
 
1698
   }
 
1699
 else
 
1700
   {
 
1701
     /* loop over entire set of tag=value entries and add all the UID's found together */
 
1702
     /* must handle uid=1234&uid=1OMD&uid=1234,2345,2134&dopt=s&uid=3INS              */   
 
1703
      NumEntries = WWWGetNumEntries(info);
 
1704
      for (IndexArgs = 0; IndexArgs < NumEntries; IndexArgs++)
 
1705
        {
 
1706
            pcName = WWWGetNameByIndex(info,  IndexArgs);
 
1707
            pcThis = WWWGetValueByIndex(info,  IndexArgs);
 
1708
 
 
1709
            if (StrICmp(pcName, "uid") == 0)
 
1710
              {
 
1711
         /*         printf("[%s][%s]\n", pcThis, pcName);  */
 
1712
                  MakeUIDList(pcThis, &pvnUIDS);
 
1713
              }
 
1714
        }                
 
1715
     if (pvnUIDS == NULL)
 
1716
         {
 
1717
            printf("Content-type: text/html\r\n\r\n");
 
1718
            printf("<H2>MMDBSRV Error</H2>\n - UID specified does not match an MMDB-ID or PDB-Code:<p>\n");   
 
1719
            goto cleanout;
 
1720
         }
 
1721
     pvnThis = pvnUIDS;
 
1722
     while (pvnThis)
 
1723
       {
 
1724
          iMMDBids++;
 
1725
          pvnThis = pvnThis->next;
 
1726
       }
 
1727
     MMDBids = (Int4Ptr) MemNew((size_t) (sizeof(Int4) * iMMDBids));
 
1728
     pvnThis = pvnUIDS;
 
1729
     iCount = 0;
 
1730
     while (pvnThis)
 
1731
     {
 
1732
          MMDBids[iCount] =  (Int4) pvnThis->data.intvalue;
 
1733
/* printf("%ld \n",(long) MMDBids[iCount]); */
 
1734
          pvnThis = pvnThis->next;
 
1735
          iCount++;
 
1736
     }           
 
1737
   }
 
1738
   
 
1739
  DispOpt = ' '; 
 
1740
  IndexArgs = -1;
 
1741
  if ((IndexArgs = WWWFindName(info, "dopt")) >= 0)
 
1742
    {
 
1743
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1744
      switch (pcThis[0])
 
1745
      {
 
1746
         case 's':
 
1747
         case 'S':
 
1748
            DispOpt = 's';
 
1749
           break;
 
1750
         case 'r':
 
1751
         case 'R':
 
1752
            DispOpt = 'r';
 
1753
           break;
 
1754
         case 'i':
 
1755
         case 'I':
 
1756
            DispOpt = 'i';
 
1757
           break;
 
1758
         case 'j':
 
1759
         case 'J':
 
1760
                DispOpt = 'j';
 
1761
                break;
 
1762
         case 'k':
 
1763
         case 'K':
 
1764
             DispOpt = 'k';
 
1765
           break;
 
1766
         case 'a':
 
1767
         case 'A':
 
1768
             DispOpt = 'a';
 
1769
             Save = 1;
 
1770
          break;
 
1771
         default:
 
1772
            {
 
1773
            printf("Content-type: text/html\r\n\r\n");
 
1774
            printf("<H2>MMDBSRV Error</H2>\n - Wrong Display Option Type: dopt=%s.<p>\n",  pcThis);
 
1775
            printf("<pre>dopt supports:\n's' Structure summary\n");
 
1776
            printf("'r' RasMol(PDB) format\n'k' Kinemage format\n'i', 'j', or 'a' ASN.1</pre><p>\n"); 
 
1777
            goto cleanout2;
 
1778
            }
 
1779
      } 
 
1780
    }
 
1781
  else
 
1782
    {
 
1783
        DispOpt = 's';  /* structure summary is default when no dopt supplied */
 
1784
    }
 
1785
 
 
1786
 
 
1787
  IndexArgs = -1;
 
1788
  if ((IndexArgs = WWWFindName(info, "Complexity")) >= 0)
 
1789
    {
 
1790
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1791
 
 
1792
 
 
1793
          for (iCount = 0; iCount < NUM_COMPLEXITY_TYPES; iCount++)
 
1794
            if (StrICmp(pcThis,ComplexityDescriptions[iCount]) == 0)
 
1795
              break;
 
1796
 
 
1797
          Complex = ALLMDL;
 
1798
          MaxModels = 1;
 
1799
 
 
1800
          switch (iCount)
 
1801
            {
 
1802
            case 0 :
 
1803
             Complex = ONECOORDRES; 
 
1804
             break;
 
1805
            case 1:
 
1806
             Complex = ONECOORDATOM; 
 
1807
             break;
 
1808
            case 2:
 
1809
             MaxModels = 5; 
 
1810
             break;
 
1811
            case 3 :
 
1812
             MaxModels = 10; 
 
1813
             break;
 
1814
            case 4 :
 
1815
             MaxModels = INT2_MAX; 
 
1816
             break;
 
1817
            case 5:
 
1818
             Complex = ONECOORDATOM; 
 
1819
              break;
 
1820
            default:
 
1821
               {
 
1822
                printf("Content-type: text/html\r\n\r\n");
 
1823
                printf("<H2>MMDBSRV Error</H2>\n - Wrong Complexity Type: Complexity=%s.<p>\n", pcThis );
 
1824
                goto cleanout2;
 
1825
               } 
 
1826
            }    
 
1827
    }  
 
1828
 
 
1829
  IndexArgs = -1;
 
1830
  if ((IndexArgs = WWWFindName(info, "mdlLvl")) >= 0)
 
1831
    {
 
1832
      pcThis = WWWGetValueByIndex(info, IndexArgs);
 
1833
      Complex = atoi(pcThis);
 
1834
    }
 
1835
 
 
1836
  IndexArgs = -1;
 
1837
  if ((IndexArgs = WWWFindName(info, "maxModels")) >= 0)
 
1838
    {
 
1839
      pcThis = WWWGetValueByIndex(info, IndexArgs);
 
1840
      MaxModels = atoi(pcThis);
 
1841
    }
 
1842
  
 
1843
  IndexArgs = -1;
 
1844
  if ((IndexArgs = WWWFindName(info, "KinemageColor")) >= 0)
 
1845
    {
 
1846
      pcThis = WWWGetValueByIndex(info, IndexArgs);
 
1847
      KinColor = GetOptionValue(pcThis, NUM_COLOR_TYPES, ColorDescriptions);
 
1848
    }
 
1849
  
 
1850
         
 
1851
         
 
1852
  IndexArgs = -1;
 
1853
  if ((IndexArgs = WWWFindName(info, "KinemageRender")) >= 0)
 
1854
    {
 
1855
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1856
      StrToInt4(pcThis, &KinRender); 
 
1857
    }    
 
1858
  
 
1859
  IndexArgs = -1;
 
1860
  if ((IndexArgs = WWWFindName(info, "save")) >= 0)
 
1861
    {  /* these don't affect structure summaries */
 
1862
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1863
      if (StrICmp(pcThis, "See") == 0)
 
1864
        {
 
1865
            Save = 1;  /* sends MIMED-type files to view in ascii-readable form */
 
1866
        }
 
1867
      else if (StrICmp(pcThis, "Save") == 0)
 
1868
        {
 
1869
            Save = 2;  /* sends MIMED-type files in raw save form */
 
1870
        }       
 
1871
      else if (StrICmp(pcThis, "M") == 0)
 
1872
        {
 
1873
            Save = 3;  /* MacSave - not implemented */
 
1874
        }       
 
1875
      else if (StrICmp(pcThis, "asntext") == 0)
 
1876
        {
 
1877
            Save = SEND_FILE;  /* writes out asn text without the html header */
 
1878
        }       
 
1879
    }    
 
1880
  
 
1881
  IndexArgs = -1;
 
1882
  if ((IndexArgs = WWWFindName(info, "header")) >= 0)
 
1883
    {
 
1884
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1885
      if (StrICmp(pcThis, "no") == 0)
 
1886
        {
 
1887
            Save = 2;
 
1888
        }
 
1889
    }   
 
1890
    
 
1891
    
 
1892
  IndexArgs = -1;
 
1893
  if ((IndexArgs = WWWFindName(info, "title")) >= 0)
 
1894
    {
 
1895
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1896
      if (StrICmp(pcThis, "no") == 0)
 
1897
        {
 
1898
          
 
1899
          GlobalTitles = FALSE;
 
1900
        }
 
1901
    }     
 
1902
   
 
1903
  IndexArgs = -1;
 
1904
  if ((IndexArgs = WWWFindName(info, "html")) >= 0)
 
1905
    {
 
1906
      pcThis = WWWGetValueByIndex(info,IndexArgs);
 
1907
      if (StrICmp(pcThis, "no") == 0)
 
1908
        {
 
1909
          GlobalNonHtmlOutput = TRUE;
 
1910
          GlobalTitles = FALSE;
 
1911
        }
 
1912
    }   
 
1913
    
 
1914
    
 
1915
    
 
1916
/* call the pertinent Page Sender */
 
1917
 
 
1918
 if  (DispOpt == 's')
 
1919
   {
 
1920
       iBytes = SendSummaryPage(MMDBids, iMMDBids,  Save);
 
1921
   }
 
1922
 else 
 
1923
   {   
 
1924
        iBytes = SendStructureMIME(DispOpt,  MMDBids[0], Save  , 
 
1925
                         Complex, MaxModels,  KinColor, KinRender);
 
1926
   }
 
1927
 
 
1928
   
 
1929
   MemFree(MMDBids);
 
1930
 
 
1931
   /* log first MMDB-id, number of id's asked for, number of bytes sent */
 
1932
   time_now = GetSecs();
 
1933
   TimeNowStr = ctime(&time_now);
 
1934
   TimeNowStr[24] = '\0'; 
 
1935
   ErrLogPrintf("\n%s|%s|%s|%s|Do %c|Id %ld|No %ld|By %ld|Sv %ld|Cp %ld|Md %ld|Kc %ld|Kr %ld",
 
1936
                WWWGetAddress(info), WWWGetHost(info), WWWGetAgent(info), TimeNowStr, 
 
1937
                (char) DispOpt,
 
1938
                (long) MMDBids[0], (long) iMMDBids, (long)iBytes, (long) Save,
 
1939
                (long) Complex, (long) MaxModels, (long) KinColor, (long) KinRender); 
 
1940
  
 
1941
  
 
1942
  CloseMMDBAPI();
 
1943
  MMDBFini();  
 
1944
  fflush(stdout);  
 
1945
  exit(0);
 
1946
  
 
1947
cleanout2:
 
1948
  MemFree(MMDBids);  
 
1949
  CloseMMDBAPI();
 
1950
  MMDBFini();
 
1951
  /* log first MMDB-id, number of id's asked for, number of bytes sent */
 
1952
   time_now = GetSecs();
 
1953
   TimeNowStr = ctime(&time_now);
 
1954
   TimeNowStr[24] = '\0'; 
 
1955
   ErrLogPrintf("\n#Syntax Error %s|%s|%s|%s|%ld",
 
1956
                WWWGetAddress(info), WWWGetHost(info), WWWGetAgent(info), TimeNowStr, 
 
1957
                (long)iBytes); 
 
1958
   fflush(stdout);
 
1959
   exit(0);
 
1960
 
 
1961
cleanout:
 
1962
  CloseMMDBAPI();
 
1963
  MMDBFini();
 
1964
  /* log first MMDB-id, number of id's asked for, number of bytes sent */
 
1965
   time_now = GetSecs();
 
1966
   TimeNowStr = ctime(&time_now);
 
1967
   TimeNowStr[24] = '\0'; 
 
1968
   ErrLogPrintf("\n#Query Not Supported %s|%s|%s|%s|%ld",
 
1969
                WWWGetAddress(info), WWWGetHost(info), WWWGetAgent(info), TimeNowStr, 
 
1970
                (long)iBytes); 
 
1971
   fflush(stdout);
 
1972
   exit(0);
 
1973
 
 
1974
earlyout:
 
1975
   time_now = GetSecs();
 
1976
   TimeNowStr = ctime(&time_now);
 
1977
   TimeNowStr[24] = '\0'; 
 
1978
   ErrLogPrintf("\n#Get/Post Error %s|%s|%s|%s|%ld",
 
1979
                WWWGetAddress(info), WWWGetHost(info), WWWGetAgent(info), TimeNowStr, 
 
1980
                 (long)iBytes); 
 
1981
  
 
1982
  
 
1983
  fflush(stdout);
 
1984
  exit(0);
 
1985
}
 
1986