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