1 /*
2  * $Id: vastsrvDB.c,v 1.8 2003/01/31 16:11:44 chenj Exp $
3  *
4  * ===========================================================================
5  *
6  *                            PUBLIC DOMAIN NOTICE
7  *               National Center for Biotechnology Information
8  *
9  *  This software/database is a "United States Government Work" under the
10  *  terms of the United States Copyright Act.  It was written as part of
11  *  the author's official duties as a United States Government employee and
12  *  thus cannot be copyrighted.  This software/database is freely available
13  *  to the public for use. The National Library of Medicine and the U.S.
14  *  Government have not placed any restriction on its use or reproduction.
15  *
16  *  Although all reasonable efforts have been taken to ensure the accuracy
17  *  and reliability of the software and data, the NLM and the U.S.
18  *  Government do not and cannot warrant the performance or results that
19  *  may be obtained by using this software or data. The NLM and the U.S.
20  *  Government disclaim all warranties, express or implied, including
21  *  warranties of performance, merchantability or fitness for any particular
22  *  purpose.
23  *
24  *  Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * File Name: vastsrvDB.c --- The database version of vastsrv.c
29  *
30  * Author: Christopher Hogue, Tom Madej, Siqian He, Jie Chen
31  *
32  *
33  * $Log: vastsrvDB.c,v $
34  * Revision 1.8  2003/01/31 16:11:44  chenj
35  * set ODBCINI correctly
36  *
37  * Revision 1.7  2003/01/15 17:15:39  chenj
38  * delete all SelfNbr stuff
39  *
40  * Revision 1.6  2003/01/15 16:24:52  chenj
41  * Change some posting messages
42  *
43  * Revision 1.5  2003/01/14 21:15:19  chenj
44  * change VastToCn3d() to VastToCn3DAndAli()
45  *
46  *
47  *
48  * This file with others together produce graphical display of structure
49  * alignment.
50  *
51  *
52  * ===========================================================================
53  *
54  */
55 
56 /* Main program for VAST structure neighbor server. */
57 
58 /* Use vastlocl.c instead of vastdtbs.c because it's faster to read in the
59  * existing BiostrucAnnotSet file than to load it from database.
60  */
61 
62 /* define DEBUG_1 1 */
63 
64 #include "vastuti.h"
65 
66 #define CPUTIME_MAX		120
67 #define DEFAULT_SUBSET_NUM    	NRBlast10e_40 	/* the NR set BLAST 10e-40 */
68 #define DEFAULT_SORT_BY		NRes 	/* Number of Aligned Residues */
69 #define NUM_HITS_PER_PAGE	60
70 #define DEFAULT_PAGE            1
71 #define DEF_ALL_NBR		1
72 #define SELECTED_NBR		0
73 #define VIEW_STR_SUBMIT		0
74 #define VIEW_ALI_SUBMIT		1
75 #define DEF_LIST_SUBMIT		2
76 #define FIND_SUBMIT		3
77 #define MAX_KEPT_NBR		30
78 #define DOMID_SIZE              7       /* used to be 6, as length of domId */
79 #define MaxVPP			150
80 
81 #define HEADFILE   "sshead.txt"
82 
83 Char 		VSPATH[PATH_MAX];
84 static Char 	URLBase[PATH_MAX];
85 Char 		URLcgi[PATH_MAX];
86 Char 		DATApath[PATH_MAX];
87 static Char 	VASTpath[PATH_MAX];
88 Char 		ENTREZurl[PATH_MAX];
89 static Char 	DOCSUMurl[PATH_MAX];
90 Char 		MAILto[PATH_MAX];
91 Char 		CGIname[PATH_MAX];
92 Char 		MMDBCGIname[PATH_MAX];
93 static Char 	HELPname[PATH_MAX];
94 Char		Database[PATH_MAX];
95 
96 static Char 	DART[PATH_MAX];
97 static Char 	LOGIN[PATH_MAX];
98 static Char 	PASSWD[PATH_MAX];
99 static Char 	INITpath[PATH_MAX];
100 static Char 	LIBpath[PATH_MAX];
101 Char		CDDurl[PATH_MAX];
102 Int4		aSdi = 0;
103 
104 Uint1 			numSubsets = 0;
105 SubsetNameData 	subsetNames[6];
106 Boolean SubsetInfoLoaded = FALSE;
107 Dart_Connect	*dartcon;
108 extern BiostrucAnnotSetPtr LocalGetBiostrucAnnotSet(Int4 mmdbid, CharPtr JobID);
109 Boolean		psok;
110 
111 
getSubsetNbr(Char * subname)112 static Uint1 getSubsetNbr(Char *subname)
113 {
114   Uint1 i = 0;
115   if(! SubsetInfoLoaded) loadSubsetInfo();
116   for(i = 0; i < numSubsets; i++)
117     if(strcmp(subsetNames[i].subName,subname)==0)
118       return subsetNames[i].subId;
119 
120   PrtMes(MAILto, "VASTSRV", "GetSubsetNumber_DB() failed.", NULL, FALSE);
121 
122 } /* getSubsetNbr() */
123 
124 
125 
126 
127 static void
VastTableBegin(FILE * table,Int4 iMMDBid,Int4 iFSID,SortBy sortby,SubSetID subsetnum,Int4 iKept,CharPtr NonNbr,VastPageDataPtr vpp,Int4 numhitsdisplayed,Int4 numhits,Int4 numpages,Int4 pagenum,char cTable,Int4 NbrFlag,CharPtr JobID,CharPtr pcPass)128 VastTableBegin (FILE *table, Int4 iMMDBid, Int4 iFSID, SortBy sortby,
129 	SubSetID subsetnum, Int4 iKept,  CharPtr NonNbr, VastPageDataPtr vpp,
130 	Int4 numhitsdisplayed, Int4 numhits, Int4 numpages,
131 	Int4 pagenum, char cTable, Int4 NbrFlag, CharPtr JobID, CharPtr pcPass)
132 {
133 	 Int4 domNo;
134 	 Char pdbId[5], chainLett;
135 
136 
137 /* J. Chen */
138 
139 	StrCut(pdbId, vpp[0].aDomName, 1, 4);
140 	chainLett = vpp[0].aDomName[4];
141 	domNo = (Int4) atol(vpp[0].aDomName+5);
142 
143         fprintf(table, "Content-type: text/html\n\n");
144         WWWPrintFileData(HEADFILE,  stdout);
145 
146         fprintf(table, "<TABLE width=800 BORDER=0 CELLPADDING=3 CELLSPACING=0 bgcolor=#FFFFCC>\n\n");
147         PrintQueryInfo(table, pdbId, chainLett, domNo, iMMDBid, JobID);
148 
149   /* insert a blank row for spacing */
150  /*       fprintf(table, "<TR>\n<TD VALIGN=TOP NOWRAP></TD>\n</TR>\n\n"); */
151 
152         PrintAlignViewBanner(table, iFSID, vpp, numhitsdisplayed,JobID,pcPass);
153 
154         PrintHitsSortBanner(table, sortby, subsetnum, pagenum, numpages,cTable);
155 
156 	PrintSearchNbr(table);
157 
158         fprintf(table, "</TABLE>\n\n");
159         fflush(table);
160 
161 /* J. Chen */
162 
163     fprintf(table, "<br>\n");
164     fprintf(table, "<font class=SMALL1><strong>");
165     if (NbrFlag == DEF_ALL_NBR) {
166 	if (numhitsdisplayed > iKept) {
167             int numtmp;
168 
169             numtmp  = numhits - numhitsdisplayed + iKept;
170             if (numtmp)
171                 fprintf(table, "%d out of %d ", numhitsdisplayed-iKept,numhits);
172             else fprintf(table, "%d ", numhits);
173             if (iKept)
174                 fprintf(table, "with %d additional selected ", iKept);
175             if (numhitsdisplayed > 1)
176                 fprintf(table, "structure neighbors displayed");
177             else fprintf(table, "structure neighbor displayed");
178 
179         }
180         else if (iKept > 1)
181                 fprintf(table,"%d selected neighbors displayed",iKept);
182         else fprintf(table, "1 selected neighbor displayed");
183     }
184     else {
185 	if (numhitsdisplayed > iKept)  {
186             int numtmp;
187 
188             numtmp = numhitsdisplayed - iKept;
189 
190             if (numtmp>1)
191                 fprintf(table, "%d neighbors found", numtmp);
192             else fprintf(table, "1 neighbor found");
193             if (iKept>1)
194                 fprintf(table, ", and displayed with %d additional selected ones",iKept);
195             else if (iKept ==1)
196                 fprintf(table, ", and displayed with 1 additional selected neighbor");
197 
198 	}
199         else if (iKept >1)
200                 fprintf(table, "%d selected neighbors displayed", iKept);
201         else fprintf(table, "1 selected neighbor displayed");
202 
203         if (NonNbr && NonNbr[0] != NULLB) {
204 	   Char str[MAX_TBUFF];
205 
206 	   if (strchr(NonNbr, ','))
207 		sprintf(str, " are not structure neighbors");
208 	   else sprintf(str, " is not a structure neighbor");
209 	   if (StrLen(NonNbr) <= 41)
210 	      fprintf(table,", but <font color=#CC6600>%s</font>%s",NonNbr,str);
211 	   else fprintf(table, ", but others are not structure neighbors");
212  	}
213     }
214     fprintf(table, ".</strong></font>\n");
215 
216 } /* end of VastTableBegin */
217 
218 
219 
220 
CalCellHForCDs(Int4 Fsid)221 static Int2 CalCellHForCDs(Int4 Fsid)
222 {
223   CharPtr	*CddName;
224   Int4 		i, j, k, gi, seqlen, numseg, from, to, y, maxy=0, NumRows;
225   Int2		CdNum, *iClus;
226   Int4Ptr	starts, lens;
227   OverLoc       *head, *end;
228   DenseSegPtr   *dsp;
229   SeqAnnotPtr   sap = NULL;
230   SeqAlignPtr   salp= NULL;
231   unsigned	*PssmId, *Pssmlist, count;
232 
233   gi = constructGi(Fsid/10000, (Fsid % 10000)/100);
234   seqlen = constructChainLength(Fsid/10000, (Fsid % 10000)/100);
235 
236   if (Dart_Gi2Sap(dartcon, gi, &sap, NULL) && (sap != NULL)) {
237 
238     if (!Dart_CdNum(dartcon, &count))
239             PrtMes("chenj@ncbi.nlm.nih.gov",
240                         "MmdbSrv", "Can't do Dart_CdNum()", NULL, FALSE);
241     Pssmlist = (unsigned *) MemNew (count * sizeof (unsigned));
242 
243     end=head = NewOverLoc(seqlen);
244     head->y = 90+FontBH;
245     head->next = NULL;
246 
247     for (salp = (SeqAlignPtr)sap->data, CdNum=0; salp!=NULL;
248 		salp = salp->next, CdNum++);
249 
250     dsp = (DenseSegPtr *) MemNew (CdNum * sizeof(DenseSegPtr));
251     PssmId = (unsigned *) MemNew (CdNum * sizeof(unsigned));
252     iClus = (Int2Ptr) MemNew (CdNum * sizeof(Int2));
253     CddName = (CharPtr *) MemNew (CdNum * sizeof(CharPtr));
254     for (j=0; j< CdNum; j++) CddName[j] = (CharPtr) MemNew (30);
255 
256     for (salp = (SeqAlignPtr)sap->data, i=0; salp!=NULL; salp=salp->next,i++) {
257       dsp[i] = (DenseSegPtr)salp->segs;
258       PssmId[i] = GetPSSMID(dsp[i]);
259       iClus[i] = -1;
260 
261       if (PssmId[i]) Dart_CDGi2Acc(dartcon, PssmId[i], CddName[i], 30);
262       else {
263       	CharPtr strtmp;
264 
265         strtmp = GetCDDName(dsp[i]);
266         sprintf(CddName[i], strtmp);
267       }
268     }
269 
270     for (i=0; i< CdNum; i++) {
271 
272         if (iClus[i] >= 0) continue;
273         iClus[i] = i;
274 
275         if (Dart_Related(dartcon, CddName[i],Pssmlist,count,&NumRows, NULL)) {
276 
277 	  if (NumRows > count)
278 	     PrtMes("chenj@ncbi.nlm.nih.gov", "VASTSRV",
279 		"Dart_Related(): NumRows>MaxPssm", NULL, FALSE);
280 
281           for (j=0; j< NumRows; j++)
282             for (k=i+1; k< CdNum; k++)
283               if (PssmId[k] == Pssmlist[j]) iClus[k] = i;
284 
285         }
286     }
287 
288     for (i=0; i< CdNum; i++) {
289       numseg = dsp[i]->numseg;
290       starts = dsp[i]->starts;
291       lens = dsp[i]->lens;
292       from = starts[0]+1;
293       to = starts[(numseg-1)*2] + lens[numseg-1];
294       y = GetY1(head, &end, from , to, seqlen, iClus[i], 5);
295       if (y >=0) y += FontBH+2;
296       else y = maxy;
297       maxy = MAX(maxy, y);
298     }
299 
300     FreeOverLoc(head);
301     MemFree(dsp);
302     MemFree(PssmId);
303     MemFree(iClus);
304     for (i=0; i< CdNum; i++) MemFree(CddName[i]);
305     MemFree(CddName);
306     MemFree(Pssmlist);
307 
308   }
309 
310   if (!maxy) maxy = 90;
311   return(maxy);
312 
313 }	/* end of CalCellHForCDs */
314 
315 
316 
317 static void
VastInfoRows(FILE * table,Int4 iFSID,CharPtr JobID,CharPtr pcPass,VastPageDataPtr vpp,Int4 numhitsdisplayed,Int4 iKept,CharPtr selnbrstring,CharPtr selsdidstring,SortBy sortby,SubSetID subsetnum,Int4 pagenum)318 VastInfoRows(FILE *table, Int4 iFSID, CharPtr JobID, CharPtr pcPass,
319 	VastPageDataPtr vpp, Int4 numhitsdisplayed, Int4 iKept,
320 	CharPtr selnbrstring, CharPtr selsdidstring, SortBy sortby,
321 	SubSetID subsetnum, Int4 pagenum)
322 {
323   Int4 	i, cellh;
324   Char 	str[100];
325 
326    fprintf(table, "<TABLE border=0 CELLPADDING=0 CELLSPACING=0 width=800>\n");
327    fprintf(table, "                     <!-- table for checkbox -->\n");
328 
329    if (JobID) cellh = 100;
330    else cellh = CalCellHForCDs(iFSID);
331    fprintf(table, "<tr>\n");
332    fprintf(table, "<td height=%d valign=middle><br></td>\n", cellh);
333    fprintf(table, "<td width=770 rowspan=%d>\n", numhitsdisplayed+1);
334    fprintf(table, "<img src=");
335    if (JobID) fprintf(table, VSParURL, URLcgi, CGIname, iFSID, sortby);
336    else fprintf(table, ParURL, URLcgi, CGIname, aSdi, sortby);
337    if (selnbrstring == NULL && selsdidstring == NULL) {
338 	if (pagenum)
339 	    fprintf(table, PageSubsetURL, pagenum, subsetnum, subsetnum);
340 	else fprintf(table, "schsub=Find");
341    }
342    else {
343      fprintf(table, "schsub=Find");
344      if (selnbrstring) fprintf(table, "&selnbr=%s", selnbrstring);
345      if (selsdidstring) fprintf(table, "&selsdid=%s", selsdidstring);
346    }
347 
348    if (iKept)
349 	for (i=0; i< iKept; i++)
350 	    fprintf(table, "&hit=%d", vpp[i].bBsfId);
351    if (JobID) fprintf(table, "&vsid=%s&pass=%s", JobID, pcPass);
352 
353    fprintf(table,"&cmd=graph&imgsize=%d\" usemap=#chain_map border=0 ismap>",
354 		(numhitsdisplayed*30 + cellh));
355    fprintf(table, "\n</td>\n</tr>\n\n");
356 
357    fprintf(table, "<map name=chain_map>\n");
358 
359    ImgMapOrDraw(TRUE, iFSID, vpp, numhitsdisplayed, iKept, selnbrstring,
360 	selsdidstring,	sortby, subsetnum, pagenum, cellh, JobID, pcPass,table);
361    fprintf(table, "</map>\n");
362 
363   for (i = 0; i < numhitsdisplayed; i++) {
364 
365     fprintf(table, "<tr>\n");
366     fprintf(table, "<td VALIGN=TOP height=30 width=30 class=SMALL1>\n");
367     fprintf(table, "<INPUT TYPE=checkbox NAME=hit VALUE=%d", vpp[i].bBsfId);
368     if (i < iKept) fprintf(table, " checked");
369     fprintf(table, "></td>\n");
370     fprintf(table, "</tr>\n\n");
371 
372     fflush(table);
373 
374   } /* end of for */
375 
376   fprintf(table, "</TABLE>\n\n");
377   fprintf(table, "</FORM>\n"); 		/* close Form opened in PrintAlignView */
378 
379 } /* end of VastInfoRows */
380 
381 
382 
383 #define SetTd	"<td VALIGN=top ALIGN=center>"
384 #define SetFont "<font color=#CC6600>"
385 
386 static void
VastTableRows(FILE * table,CharPtr JobID,VastPageDataPtr vpp,Int4 numhitsdisplayed,Int4 iKept,SortBy sortby)387 VastTableRows(FILE *table, CharPtr JobID, VastPageDataPtr vpp,
388 	Int4 numhitsdisplayed, Int4 iKept, SortBy sortby)
389 {
390   Int4 	bMMDBid;
391   Uint1 domNo;
392   Char 	pcSlaveName[2 * DBStrSize + 4];
393   float f;
394   int i;
395 
396    fprintf(table, "<br>\n");
397    fprintf(table, "<TABLE cellspacing=3 cellpadding=2 width=800 border=1>\n");
398    fprintf(table,"<tr valign=middle>\n");
399    fprintf(table,"<th>&nbsp;</th>\n");
400    fprintf(table,"<th align=left><pre> <a href=\"%s/vasthelp.html#Structure\">PDB</a>", VASTpath);
401    fprintf(table," <a href=\"%s/vasthelp.html#C\">C</a>", VASTpath);
402    fprintf(table," <a href=\"%s/vasthelp.html#D\">D</a></pre></th>\n",VASTpath);
403 
404    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#NRES\">",
405 		VASTpath);
406    if (sortby == NRes)
407 	fprintf(table, "%sAli. Len.</font></a></pre></th>\n", SetFont);
408    else fprintf(table, "Ali. Len.</a></pre></th>\n");
409 
410    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#SCORE\">",
411 		VASTpath);
412    if (sortby == VScore)
413 	fprintf(table, "%sSCORE</font></a></pre></th>\n", SetFont);
414    else fprintf(table, "SCORE</a></pre></th>\n");
415 
416    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#P-VAL\">",
417 		VASTpath);
418    if (sortby == PValue)
419 	fprintf(table, "%sP-VAL</font></a></pre></th>\n", SetFont);
420    else fprintf(table, "P-VAL</a></pre></th>\n");
421 
422    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#RMSD\">",
423 		VASTpath);
424    if (sortby == Rmsd)
425 	fprintf(table, "%sRMSD</font></a></pre></th>\n", SetFont);
426    else fprintf(table, "RMSD</a></pre></th>\n");
427 
428    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#Id\">",
429 		VASTpath);
430    if (sortby == PcntId)
431 	fprintf(table, "%s%%Id</font></a></pre></th>\n", SetFont);
432    else fprintf(table, "%%Id</a></pre></th>\n");
433 
434    fprintf(table,"<th><pre><a href=\"%s/vasthelp.html#Contents\">Description</pre></th>\n",VASTpath);
435    fprintf(table,"</tr><br>\n");
436    fflush(table);
437 
438 
439   for (i = 0; i < numhitsdisplayed; i++) {
440 
441     Char	pdbId[5], strtmp[100];
442 
443     bMMDBid = vpp[i].bBsfId / 100000;
444     domNo = (Int4) atoi(vpp[i].bDomName+5);
445 
446     pcSlaveName[0] = NULLB;
447     if(vpp[i].name[0] != NULLB || vpp[i].name2[0] != NULLB) {
448       if(vpp[i].name[0] != NULLB) strcpy(pcSlaveName,vpp[i].name);
449       if(vpp[i].name2[0] != NULLB) strcat(pcSlaveName,vpp[i].name2);
450     }
451     else if (JobID)
452 	constructPdbDescr(bMMDBid, pcSlaveName, 2 * DBStrSize + 4);
453 
454     fprintf(table, "<tr>\n");
455     fprintf(table, "<td VALIGN=TOP>");
456 
457     fprintf(table, "<INPUT TYPE=checkbox NAME=hit VALUE=%d", vpp[i].bBsfId);
458     if (i < iKept) fprintf(table, " checked");
459     fprintf(table, "></td>\n");
460     fprintf(table, "<td VALIGN=TOP><pre>");
461     StrCut(pdbId, vpp[i].bDomName, 1, 4);
462     fprintf(table, "<a href=\"%s%s?uid=%ld&form=6&db=t&Dopt=s\">%s</a>",
463 		URLcgi, MMDBCGIname, (long) bMMDBid, pdbId);
464 
465     if (vpp[i].bDomName[4] == ' ') fprintf(table,"&nbsp;");
466     else
467       fprintf(table," <a href=\"%s%s?uid=%ld&form=6&db=t&Dopt=s\">%c</a>",
468 		URLcgi, MMDBCGIname, (long) bMMDBid, vpp[i].bDomName[4]);
469 
470     if (domNo > 0)
471       fprintf(table," <a href=\"%s%s?uid=%ld&form=6&db=t&Dopt=s\">%d</a></pre></td>\n",
472 		URLcgi, MMDBCGIname, (long) bMMDBid, domNo);
473     else
474       fprintf(table," </pre></td>\n");
475 
476     if (vpp[i].nres > 0) {
477       	fprintf(table,"%s", SetTd);
478 	if (sortby == NRes)
479 	    fprintf(table, "%s%d</font></td>\n", SetFont, vpp[i].nres);
480 	else fprintf(table, "%d</td>\n", vpp[i].nres);
481     }
482     else fprintf(table,"<td> </td>\n");
483 
484     if (vpp[i].vScore > 0) {
485         f = (FloatLo) (vpp[i].vScore);
486         f = f/(FloatLo) ASP_SCALE_FACTOR;
487         fprintf(table, "%s", SetTd);
488 	if (sortby == VScore)
489 	    fprintf(table, "%s%.1f</font></td>\n",SetFont, f);
490 	else fprintf(table, "%0.1f</td>\n", f);
491     }
492     else fprintf(table, "<td> </td>\n");
493 
494     if (vpp[i].mlogp > 0) {
495         f = (float) (vpp[i].mlogp);
496         f = f/(float) ASP_SCALE_FACTOR;
497 
498         /* adjust for database size */
499         f -= LOG10_500;
500 
501         if (f <= 4.0) {
502           f = (float) exp(-LOG_10*f);
503           fprintf(table, "%s", SetTd);
504 	  if (sortby == PValue)
505 	      fprintf(table, "%s%.4f</font></td>\n", SetFont, f);
506 	  else fprintf(table, "%.4f</td>\n", f);
507         }
508         else {
509           fprintf(table, "%s", SetTd);
510 	  if (sortby == PValue)
511 	      fprintf(table, "%s10e-%.1f</font></td>\n", SetFont, f);
512 	  else fprintf(table, "10e-%.1f</td>\n", f);
513 	}
514     }
515     else fprintf(table,"<td> </td>\n");
516 
517     if (vpp[i].rmsd > 0) {
518       f = (FloatLo) (vpp[i].rmsd);
519       f = f/(FloatLo) ASP_SCALE_FACTOR;
520       fprintf(table, "%s", SetTd);
521       if (sortby == Rmsd)
522 	fprintf(table, "%s%.1f</font></td>\n", SetFont, f);
523       else fprintf(table,"%.1f</td>\n", f);
524     }
525     else fprintf(table,"<td> </td>\n");
526 
527     if (vpp[i].pcntId > 0) {
528       f = (FloatLo) (vpp[i].pcntId);
529       f = f/(FloatLo) ASP_SCALE_FACTOR;
530       fprintf(table, "%s", SetTd);
531       if (sortby == PcntId)
532       	  fprintf(table, "%s%.1f</font></td>\n", SetFont, f * 100.0);
533       else fprintf(table, "%.1f</td>\n", f*100.0);
534     }
535     else
536       fprintf(table,"<td VALIGN=top ALIGN=center>0.0</td>\n");
537 
538     fprintf(table,"<td VALIGN=top>\n");
539     fprintf(table,"%s" , pcSlaveName);
540     fprintf(table,"<BR>\n");
541     fprintf(table,"</td>\n</tr>\n");
542     fflush(table);
543   } /* end of for */
544 
545 
546   fprintf(table, "</TABLE>\n\n");
547   fprintf(table, "</FORM>\n"); 		/* close Form opened in PrintAlignView */
548 } /* end of VastTableRows */
549 
550 
551 
552 static void
SaveBioAnnotSetInFile(Int4 Fsid,SubSetID subsetnum,SortBy sortby,CharPtr JobID)553 SaveBioAnnotSetInFile(Int4 Fsid, SubSetID subsetnum, SortBy sortby, CharPtr JobID)
554 {
555         Int4                  	numhitsdisplayed, alloc_size;
556         BiostrucAnnotSetPtr     pbsa = NULL;
557         AsnIoPtr                aip;
558 	VastPageDataPtr 	vpp=NULL;
559 
560   	if (JobID) {
561 	    OpenMMDBAPI((POWER_VIEW), NULL);
562 	    pbsa = LocalGetFeatureSet(Fsid/10000, Fsid, JobID);
563 	    CloseMMDBAPI();
564 	}
565 	else {
566         	vpp = constructVastPagesForAllNbrs(aSdi, subsetnum, sortby,
567 			(unsigned *)&alloc_size, (unsigned *)&numhitsdisplayed);
568         	pbsa = constructBASPFromVastPagePtr(vpp, numhitsdisplayed);
569 	}
570 
571         printf("Content-type: application/octet-stream\r\n\r\n");
572 /*
573         printf("Content-type: text/html\r\n\r\n");
574         printf("<HTML><PRE>\r\n");
575 */
576         aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
577         BiostrucAnnotSetAsnWrite(pbsa, aip, NULL);
578         AsnIoFlush(aip);
579         AsnIoClose(aip);
580 	free(vpp);
581 }
582 
583 
584 
585 
586 
587 
588 static void
MakeVastTableGraph(VastPageDataPtr vpp,Int4 numhitsdisplayed,Int4 FSID,Int4 iKept,CharPtr selnbrstring,CharPtr selsdidstring,CharPtr NonNbr,SortBy sortby,SubSetID subsetnum,Int4 pagenum,Int4 numhits,char cTable,Int4 NbrFlag,CharPtr JobID,CharPtr pcPass,Int2 ImgSize,Int2 GraphFlag)589 MakeVastTableGraph(VastPageDataPtr vpp, Int4 numhitsdisplayed, Int4 FSID,
590 	Int4 iKept, CharPtr selnbrstring, CharPtr selsdidstring,
591 	CharPtr NonNbr, SortBy sortby, SubSetID subsetnum,
592 	Int4 pagenum, Int4 numhits, char cTable, Int4 NbrFlag, CharPtr JobID,
593 	CharPtr pcPass, Int2 ImgSize, Int2 GraphFlag)
594 {
595   Int4 iMMDBid = 0, numpages;
596 
597   if (FSID <= 0) return;
598 
599   iMMDBid = (Int4) (FSID / 10000);
600 
601   numpages = numhits / NUM_HITS_PER_PAGE;
602   numpages = numhits % NUM_HITS_PER_PAGE == 0 ? numpages : (numpages+1);
603 
604 /*
605   if(numhits<=0) {
606       fprintf(stdout, "<h1><a href=\"%s%s#VASTNonbr\">%s</a></h1>\n",
607 							URLBase, HELPname);
608       fprintf(stdout, "VAST did not find any structure neighbors.");
609       fprintf(stdout, "<HR SIZE=5 NOSHADE>\n");
610       fprintf(stdout, "</body></html>\n");
611       return;
612   }
613 */
614 
615   if (NbrFlag == DEF_ALL_NBR) {
616      if (numhits < NUM_HITS_PER_PAGE) pagenum = DEFAULT_PAGE;
617   }
618   else pagenum = 0;
619 
620   if (GraphFlag)
621       	DrawStrucNbr(FSID, vpp, numhitsdisplayed, iKept, selnbrstring,
622 	     selsdidstring, sortby, subsetnum, pagenum, ImgSize, JobID, pcPass);
623   else {
624   	VastTableBegin(stdout, iMMDBid, FSID, sortby, subsetnum, iKept,NonNbr,
625 		vpp, numhitsdisplayed, numhits, numpages,
626 		pagenum, cTable, NbrFlag, JobID, pcPass);
627 	switch (cTable) {
628 	    case 'n':
629 		     VastInfoRows(stdout, FSID, JobID, pcPass, vpp,
630 			numhitsdisplayed, iKept, selnbrstring, selsdidstring,
631 			sortby, subsetnum, pagenum);
632 		     break;
633 	    case 'y':
634 		     VastTableRows(stdout, JobID, vpp, numhitsdisplayed, iKept,
635 				sortby);
636 		     break;
637 	    default:
638 		     PrtMes(NULL, "VASTSERV",
639                         "Please select \"Graph\" or \"Tabler\" for the display on screen or \"File\" for a local copy.",
640 			NULL, FALSE);
641 	}
642 
643 	fprintf(stdout, "\n<BR><HR SIZE=5 NOSHADE><BR>\n");
644         fprintf(stdout, "</body></html>\n");
645 
646   }
647 
648   RemoveTempFiles();
649 
650   return;
651 
652 } /* MakeVastTableGraph() */
653 
654 
655 
656 
657 /* Extract vastsrv parameters from the config file. */
GetVastParams()658 static Boolean GetVastParams()
659 {
660   URLBase[0] = URLcgi[0] = ENTREZurl[0] = DOCSUMurl[0] = HELPname[0]
661   = MAILto[0] = CGIname[0] = MMDBCGIname[0] = DART[0] = LOGIN[0] = PASSWD[0]
662   = INITpath[0] = LIBpath[0] = CDDurl[0] = Database[0] = NULLB;
663 
664   GetAppParam("vast", "VASTSRV", "URLBase", "", URLBase, PATH_MAX);
665   if (URLBase[0] == NULLB) {
666     ErrPostEx(SEV_FATAL,0,0,
667 		"VAST config file\nVASTSRV section has no URLBase...\n");
668     return FALSE;
669   }
670 
671   GetAppParam("vast", "VASTSRV", "URLcgi", "", URLcgi, PATH_MAX);
672   if (URLcgi[0] == NULLB) {
673     ErrPostEx(SEV_FATAL,0,0,
674 			"VAST config file\nVASTSRV section has no URLcgi...\n");
675     return FALSE;
676   }
677 
678   GetAppParam("vast", "VASTSRV", "ENTREZurl", "", ENTREZurl, PATH_MAX);
679   if (ENTREZurl[0] == NULLB) {
680     ErrPostEx(SEV_FATAL,0,0,
681 		"VAST config file\nVASTSRV section has no ENTREZurl...\n");
682     return FALSE;
683   }
684 
685   GetAppParam("vast", "VASTSRV", "DOCSUMurl", "", DOCSUMurl, PATH_MAX);
686   if (DOCSUMurl[0] == NULLB) {
687     ErrPostEx(SEV_FATAL,0,0,
688 		"VAST config file\nVASTSRV section has no DOCSUMurl...\n");
689     return FALSE;
690   }
691 
692   GetAppParam("vast", "VASTSRV", "CGIname", "", CGIname, PATH_MAX);
693   if (CGIname[0] == NULLB) {
694     ErrPostEx(SEV_FATAL,0,0,
695 		"VAST config file\nVASTSRV section has no CGIname...\n");
696     return FALSE;
697   }
698 
699   GetAppParam("vast", "VASTSRV", "MMDBCGIname", "", MMDBCGIname, PATH_MAX);
700   if (MMDBCGIname[0] == NULLB) {
701     ErrPostEx(SEV_FATAL,0,0,
702 		"VAST config file\nVASTSRV section has no MMDBCGIname...\n");
703     return FALSE;
704   }
705 
706   GetAppParam("vast", "VASTSRV", "HELPname", "", HELPname, PATH_MAX);
707   if (HELPname[0] == NULLB) {
708     ErrPostEx(SEV_FATAL,0,0,
709 		"VAST config file\nVASTSRV section has no HELPname...\n");
710     return FALSE;
711   }
712 
713   GetAppParam("vast", "VASTSRV", "MAILto", "", MAILto, PATH_MAX);
714   if (MAILto[0] == NULLB) {
715     ErrPostEx(SEV_FATAL,0,0,
716 		"VAST config file\nVASTSRV section has no MAILto...\n");
717     return FALSE;
718   }
719 
720   GetAppParam("vast", "VASTSRV", "VSPATH", "", VSPATH, PATH_MAX);
721   if (VSPATH[0] == NULLB) {
722     ErrPostEx(SEV_FATAL,0,0,
723 	"VAST config file\nVASTSRV section has no VSPATH...\n");
724     return FALSE;
725   }
726 
727   GetAppParam("vast", "VASTSRV", "DATApath", "", DATApath, PATH_MAX);
728   if (DATApath[0] == NULLB) {
729     ErrPostEx(SEV_FATAL, 0, 0,
730 		"VAST config file\nVASTSRV section has no VAST DATApath...\n");
731     return FALSE;
732   }
733 
734   GetAppParam("vast", "VASTSRV", "VASTpath", "", VASTpath, PATH_MAX);
735   if (VASTpath[0] == NULLB) {
736     ErrPostEx(SEV_FATAL, 0, 0,
737 		"VAST config file\nVASTSRV section has no VASTpath...\n");
738     return FALSE;
739   }
740 
741   GetAppParam("mmdb", "MMDB", "Database", "", Database, PATH_MAX);
742   if (Database[0] == NULLB) {
743       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nMMDB section no Database\n");      return FALSE;
744   }
745 
746   GetAppParam("mmdb", "CD", "DARTdb", "", DART, PATH_MAX);
747   if (DART[0] == NULLB) {
748       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no DARTdb\n");
749       return FALSE;
750   }
751 
752   GetAppParam("mmdb", "CD", "LOGINname", "", LOGIN, PATH_MAX);
753   if (LOGIN[0] == NULLB) {
754       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no LOGINname\n");
755       return FALSE;
756   }
757 
758   GetAppParam("mmdb", "CD", "PASSwd", "", PASSWD, PATH_MAX);
759   if (PASSWD[0] == NULLB) {
760       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no PASSwd\n");
761       return FALSE;
762   }
763 
764   GetAppParam("mmdb", "CD", "INITpath", "", INITpath, PATH_MAX);
765   if (INITpath[0] == NULLB) {
766       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no INITpath\n");
767       return FALSE;
768   }
769 
770   GetAppParam("mmdb", "CD", "LIBpath", "", LIBpath, PATH_MAX);
771   if (LIBpath[0] == NULLB) {
772       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no LIBpath\n");
773       return FALSE;
774   }
775 
776   GetAppParam("mmdb", "CD", "CDDurl", "", CDDurl, PATH_MAX);
777   if (CDDurl[0] == NULLB) {
778       ErrPostEx(SEV_FATAL,0,0, "MMDB config file \nCD section no CDDurl\n");
779       return FALSE;
780   }
781 
782   return TRUE;
783 } /* GetVastParams() */
784 
785 
786 
787 
788 /* The following ValidateMMDBID() was changed by J. Chen, May, 2002) */
789 
ValidateMMDBID(CharPtr pdbcode,Int4 iMMDBid)790 static Boolean ValidateMMDBID(CharPtr pdbcode, Int4 iMMDBid)
791 {
792   DocUid 	uid;
793   Uint1		i;
794 
795   for (i=0; i< StrLen(pdbcode); i++) pdbcode[i] = toupper(pdbcode[i]);
796   uid = constructLiveOrDeadMmdbIdForPdbId(pdbcode, &psok, Database);
797 
798   if ((Int4)uid == iMMDBid) return TRUE;
799   else return FALSE;
800 
801 }
802 
803 
804 
FilterHitsByDomainSubset(BiostrucFeaturePtr pbsf,Int4 subsetnum,Int4 * KepId,Int4 iKept)805 static BiostrucFeaturePtr FilterHitsByDomainSubset(BiostrucFeaturePtr pbsf,
806 	Int4 subsetnum, Int4 *KepId, Int4 iKept)
807 {
808   BiostrucFeaturePtr current, pbsfHead = NULL, pbsfTail;
809   Int4 i, n, dombsfid;
810   Int4 gn, gr, hcnt, *min_ranks, *group_num, *group_rank;
811   Char domid[DOMID_SIZE + 1], pdbcode[4+1];
812 
813 /* The next bit of code is used for filtering the hit lists.  When we go through a hit
814  * list we skip domains that do not belong to the subset of interest, or which belong to
815  * the subset but for which a group representative has already been encountered.
816 */
817   for (i = 0; i <= DOMID_SIZE; i++)
818     domid[i] = NULLB;
819 
820   n = GetNumberOfDomains();
821   min_ranks = (Int4 *) MemNew(n*sizeof(Int4));
822   group_num = (Int4 *) MemNew(n*sizeof(Int4));
823   group_rank = (Int4 *) MemNew(n*sizeof(Int4));
824 
825   /* use a first pass to "flag" the selected hits in the specified subset
826      If group_num[i] = 0 or group_rank[i] is larger than min_ranks[i],
827      then the ith neighbor should not be included in summary page */
828 
829   if (group_rank != NULL) {	      /* check mem alloc */
830 
831     for (i = 0; i < n; i++) {	      /* initializations */
832          group_num[i] = 0;
833          min_ranks[i] = n + 1;
834          group_rank[i] = n + 1;
835     }
836 
837     for (current = pbsf, hcnt = 0; current != NULL; current = current->next, hcnt++) {
838        /* copy domain identifier into domid[] */
839       if (current->name[6] == ' '){
840 	 StrCut(domid, current->name, 8, 13);
841 	 if (current->name[13] != ' ') domid[6] = current->name[13];
842       }
843       else {
844 	StrCut(domid, current->name, 9, 14);
845 	if (current->name[14] != ' ') domid[6] = current->name[14];
846       }
847 
848       if (domid[5] == '0') domid[5] = ' ';
849 
850        /* if not in subset then skip over this domain */
851       if (BelongsToSubset(domid, subsetnum, &gn, &gr) <= 0) {
852 	continue;
853       }
854 
855       /* otherwise record group data for this hit */
856       group_num[hcnt] = gn;
857       group_rank[hcnt] = gr;
858 
859       /* and reset minimum rank for this group */
860       if (gr < min_ranks[gn - 1])
861 	min_ranks[gn - 1] = gr;
862     }
863   }
864 
865   /* Now use the values just set in group_num, group_rank, and min_ranks
866      to decide whether or not the current neighbor should be linked
867      into the new feature list */
868 
869   current = pbsf;
870   hcnt = 0;
871   while (current) {
872     dombsfid = current->id;
873 /*
874     if (current->name[6] == ' '){
875 	 StrCut(domid, current->name, 8, 13);
876 	 if (current->name[13] != ' ') domid[6] = current->name[13];
877     }
878     else {
879 	StrCut(domid, current->name, 9, 14);
880 	if (current->name[14] != ' ') domid[6] = current->name[14];
881     }
882 */
883 
884     if (group_rank != NULL)		      /* check mem alloc */
885     {
886       if (group_num[hcnt] == 0)	     /* group_num not set so NOT in subset */
887       {					      /* incr hcnt and do NOT link */
888         hcnt++;
889 	current = current->next;
890         continue;
891       }
892 
893       gn = group_num[hcnt];
894 
895       if (group_rank[hcnt] != min_ranks[gn - 1])
896 			/* neighbor is in subset but of lower rank */
897       {
898         hcnt++;			         /* incr hcnt and do NOT link */
899 	current = current->next;
900         continue;
901       }
902     }
903 
904     /* With the new rcsb depositions we now need to validate mmdbids
905        in the .bas files. Extract pdbcode and use it with mmdbID to validate */
906 
907     StrCut(pdbcode, current->name, 8, 11);
908     pdbcode[4]= NULLB;
909 
910     if (!ValidateMMDBID(pdbcode, (current->id)/100000)) {
911       hcnt++;
912       current = current->next;	         /* incr hcnt and do NOT link */
913       continue;
914     }
915 
916 /* remove duplicated lists, J. Chen */
917     i=0;
918     while (i< iKept && dombsfid != KepId[i]) i++;
919     if (i < iKept) {
920 	hcnt++;
921 	current = current->next;
922 	continue;
923     }
924 
925     if (pbsfHead == NULL)    /* neighbor has passed all tests - include it!*/
926     {
927       pbsfHead = current;
928       pbsfTail = pbsfHead;
929       hcnt++;
930       current = current->next;
931       pbsfTail->next = NULL;
932     }
933     else
934     {
935       pbsfTail->next = current;
936       pbsfTail = current;
937       hcnt++;
938       current = current->next;
939       pbsfTail->next = NULL;
940     }
941   }
942 
943   MemFree(min_ranks);
944   MemFree(group_num);
945   MemFree(group_rank);
946   return pbsfHead;
947 
948 }	/* FilterHitsByDomainSubset */
949 
950 
951 
FilterHitsByPageNew(BiostrucFeatureSetPtr pbsfs,Int4 PageNum,Int4 HitsPerPage,Int4 * numhits,Int4 * numpages)952 static void FilterHitsByPageNew(BiostrucFeatureSetPtr pbsfs, Int4 PageNum,
953 	Int4 HitsPerPage, Int4 *numhits, Int4 *numpages)
954 {
955 
956   BiostrucFeaturePtr pbsf, pbfHead=NULL, pbfTail=NULL;
957   Int4 index, FidCount, RemainFids, CompleteFidSet;
958   Int4 UpperLimit, LowerLimit;
959 
960   pbsf = pbsfs->features;
961   if (pbsf == NULL) {
962     *numpages = 1;
963     *numhits = 0;
964   }
965 
966   FidCount = 0;
967 
968   while (pbsf)
969   {
970     FidCount++;
971     pbsf = pbsf->next;
972   }
973   *numhits = FidCount;
974 
975   if (FidCount <= HitsPerPage) *numpages = 1;
976   else
977   {
978     RemainFids = FidCount % HitsPerPage;
979 
980     if (RemainFids)
981     {
982      CompleteFidSet = FidCount - RemainFids;
983      *numpages = (CompleteFidSet/HitsPerPage) + 1;
984     }
985     else *numpages = FidCount/HitsPerPage;
986   }
987 
988   UpperLimit = HitsPerPage * PageNum;
989   LowerLimit = UpperLimit - HitsPerPage + 1;
990 
991   if ((FidCount < HitsPerPage ) || (LowerLimit > FidCount)) {
992     UpperLimit = FidCount;
993     LowerLimit = 1;
994   }
995 
996   if (UpperLimit > FidCount) UpperLimit = FidCount;
997 
998   for (index = 1, pbsf = pbsfs->features; index <= FidCount;
999 	index++, pbsf=pbsf->next)
1000   {
1001     if ((index >= LowerLimit) && (index <= UpperLimit)) {
1002 	if (!pbfHead) pbfHead = pbfTail = pbsf;
1003 	else {
1004 		pbfTail->next = pbsf;
1005 		pbfTail = pbsf;
1006 	}
1007     }
1008   }
1009 
1010   if (pbfTail) pbfTail->next = NULL;
1011   pbsfs->features = pbfHead;
1012 
1013 }	/* FilterHitsByPageNew */
1014 
1015 
1016 static
GetValidBiostrucAnnotSet(BiostrucAnnotSetPtr basp,Int4 Fsid)1017 BiostrucAnnotSetPtr GetValidBiostrucAnnotSet(BiostrucAnnotSetPtr basp,Int4 Fsid)
1018 {
1019     BiostrucAnnotSetPtr basp2 = NULL;
1020     BiostrucFeatureSetPtr pbsfs = NULL, pbsfsLast= NULL;
1021     BiostrucIdPtr               pbsidThis = NULL;
1022 
1023     if (basp == NULL) return NULL;
1024 
1025     pbsfs = basp->features;
1026     while (pbsfs) {
1027        if (pbsfs->id == Fsid)
1028         {
1029           basp2 = BiostrucAnnotSetNew();
1030           basp2->id = basp->id;
1031           basp->id = NULL; /* unlink the id valnode from basp object */
1032           basp2->descr = basp->descr;
1033           basp->descr = NULL;  /* unlink the descr from basp object */
1034           basp2->features = pbsfs;
1035           if (pbsfsLast) /* relink next to prev */
1036             pbsfsLast->next = pbsfs->next;
1037           else
1038             basp->features = pbsfs->next;
1039           basp2->features->next = NULL;
1040 	  break;
1041 	}
1042        pbsfsLast = pbsfs;
1043        pbsfs = pbsfs->next;
1044     }
1045     BiostrucAnnotSetFree(basp);
1046 
1047     if (basp2 == NULL) {
1048 	char    str[15];
1049 
1050         sprintf(str, "%d", Fsid);
1051 	PrtMes(NULL, "VASTSRV", "Incorrect sdid or chaindom: chaindom = ", str, FALSE);
1052     }
1053 
1054     pbsidThis = ValNodeFindNext(basp2->id, NULL, BiostrucId_mmdb_id);
1055     if (!pbsidThis) {
1056 	PrtMes(MAILto, "VASTSRV", "No MMDB-ID in Data on Server.", NULL, TRUE);
1057 	BiostrucAnnotSetFree(basp2);
1058 	return NULL;
1059     }
1060 
1061     return(basp2);
1062 
1063 } /* GetValidBiostrucAnnotSet */
1064 
1065 
1066 static Int4
MakeVppByVS(Int4 Fsid,CharPtr JobID,VastPageDataPtr vpp,SubSetID subsetnum,SortBy sortby,Int4 pagenum,Int4 * KepBsfId,Int4 iKept)1067 MakeVppByVS(Int4 Fsid, CharPtr JobID, VastPageDataPtr vpp, SubSetID subsetnum,
1068  	SortBy sortby, Int4 pagenum, Int4 *KepBsfId, Int4 iKept)
1069 {
1070 
1071     BiostrucAnnotSetPtr 	basp = NULL, basp2 = NULL;
1072     BiostrucFeatureSetPtr 	pbsfs = NULL, pbsfs2 = NULL;
1073     BiostrucIdPtr 	  	pbsidThis = NULL;
1074     Int4			numhits, numpages, numhitsdisplayed;
1075     Char                        str[15];
1076 
1077     basp = LocalGetBiostrucAnnotSet(Fsid/10000, JobID);
1078     if (!basp) {
1079 	sprintf(str, "%d", Fsid);
1080         PrtMes(NULL, "VASTSRV", "Incorrect sdid or chaindom: chaindom = ", str, FALSE);
1081     }
1082 
1083     basp2 = GetValidBiostrucAnnotSet(basp, Fsid);
1084     if (!basp2) return 0;
1085 
1086     pbsfs = basp2->features;
1087     while (pbsfs)
1088     {
1089      if (pbsfs->id == Fsid)
1090       {
1091 
1092 	pbsidThis = ValNodeFindNext(pbsfs->descr, NULL,
1093 					BiostrucFeatureSetDescr_name);
1094         if (pbsfs->features != NULL) {
1095           pbsfs2 = BiostrucFeatureSetNew();
1096           pbsfs2->id = pbsfs->id;
1097           pbsfs->id = NULL;
1098           pbsfs2->descr = pbsfs->descr;
1099           pbsfs->descr = NULL;
1100 
1101           pbsfs2->features =
1102 		FilterHitsByDomainSubset(pbsfs->features, subsetnum,
1103 			KepBsfId, iKept);
1104           pbsfs->features = NULL;
1105           if (pbsfs2->features != NULL) VastTableSort(pbsfs2, sortby);
1106           FilterHitsByPageNew(pbsfs2, pagenum, NUM_HITS_PER_PAGE,
1107 					&numhits, &numpages);
1108 /* numhits=60; */
1109           if (numhits < NUM_HITS_PER_PAGE) pagenum = DEFAULT_PAGE;
1110 
1111 
1112 	  numhitsdisplayed = BFSP2VastPageDataPtr(pbsfs2, vpp, numhits);
1113         }
1114         else numhitsdisplayed = 0;
1115 
1116 	BiostrucAnnotSetFree(basp2);
1117 	return (numhitsdisplayed);
1118       }
1119      pbsfs = pbsfs->next;
1120    }
1121 
1122   BiostrucAnnotSetFree(basp2);
1123   sprintf(str, "%d", Fsid);
1124   PrtMes(NULL, "VASTSRV", "Incorrect sdid or chaindom: chaindom = ", str,FALSE);
1125 
1126 }	/* MakeVppByVS */
1127 
1128 
1129 
1130 
1131 static BiostrucFeaturePtr
FilterHitsByNbrs(BiostrucFeaturePtr bfp,CharPtr * SelNames,Int4 * SelId,Int4 issdid,Int4 iSel,Int4 * KepId,Int4 iKept)1132 FilterHitsByNbrs(BiostrucFeaturePtr bfp, CharPtr *SelNames, Int4 *SelId,
1133 	Int4 issdid, Int4 iSel, Int4 *KepId, Int4 iKept)
1134 {
1135    BiostrucFeaturePtr pbfHead = NULL, pbfTail=NULL;
1136    Char		*str, sName[10];
1137    Int4 	i, j, id, sdi;
1138 
1139    if (bfp == NULL) return NULL;
1140 
1141    while (bfp) {
1142      if (SelNames != NULL) {
1143 	str = bfp->name;
1144      	if (str[6] == ' ') {
1145 	    StrCut(sName, str, 8, 13);
1146 	    if (StrLen(str) >= 14 && str[13] != ' ') StringCat(sName, str+13);
1147         }
1148      	else {
1149 	    StrCut(sName, str, 9, 14);
1150 	    if (StrLen(str) >=15 && str[14] != ' ') StringCat(sName, str+14);
1151      	}
1152      }
1153      id = bfp->id;
1154      sdi = LongDomIdToSdi(id/10);
1155 
1156      for (i=0; i< iSel; i++) {
1157 	if (SelNames != NULL) {
1158      	    if (StringNCmp(SelNames[i], sName, StrLen(SelNames[i]))) continue;
1159 	}
1160 	else {
1161 	     if (issdid) {
1162 		if (sdi != SelId[i]) continue;
1163 	     }
1164 	     else if (id != SelId[i]) continue;
1165   	}
1166 
1167 	j=0;
1168 	while (j< iKept && id != KepId[j]) j++;
1169         if (j < iKept) continue;
1170         if (pbfHead == NULL)  {
1171 		pbfHead = pbfTail = bfp;
1172 	}
1173         else {
1174 		pbfTail->next = bfp;
1175 		pbfTail = bfp;
1176 	}
1177 	break;
1178     }
1179     bfp = bfp->next;
1180 
1181   }
1182 
1183   if (pbfTail) pbfTail->next = NULL;
1184 
1185   return pbfHead;
1186 } 	/* end of FilterHitsByNbrs */
1187 
1188 
1189 
1190 
MakeVppByVSNbr(Int4 Fsid,CharPtr JobID,VastPageDataPtr vpp,CharPtr * SelNames,Int4 * SelId,Int4 issdid,Int4 iSel,Int4 * KepId,Int4 iKept,SortBy sortby)1191 static Int4 MakeVppByVSNbr(Int4 Fsid, CharPtr JobID, VastPageDataPtr vpp,
1192   	CharPtr *SelNames, Int4 *SelId, Int4 issdid, Int4 iSel, Int4 *KepId, 		Int4 iKept, SortBy sortby)
1193 {
1194     BiostrucAnnotSetPtr 	basp = NULL, basp2=NULL;
1195     BiostrucFeatureSetPtr 	pbsfs = NULL, pbsfs2 = NULL;
1196     Int4 			numhitsdisplayed;
1197     Char                        str[15];
1198 
1199     basp = LocalGetBiostrucAnnotSet(Fsid/10000, JobID);
1200     if (!basp) {
1201         sprintf(str, "%d", Fsid);
1202         PrtMes(NULL, "VASTSRV", "Incorrect sdid or chaindom. chaindom  = ", str, FALSE);
1203     }
1204 
1205     basp2 = GetValidBiostrucAnnotSet(basp, Fsid);
1206     pbsfs = basp2->features;
1207     while (pbsfs) {
1208 	if (pbsfs->id == Fsid) {
1209 	    if (pbsfs->features != NULL) {
1210 		pbsfs2 = BiostrucFeatureSetNew();
1211 		pbsfs2->id = pbsfs->id;
1212 		pbsfs->id = NULL;
1213 		pbsfs2->descr = pbsfs->descr;
1214 		pbsfs->descr = NULL;
1215 
1216 		pbsfs2->features =
1217 		   FilterHitsByNbrs(pbsfs->features, SelNames, SelId, issdid,
1218 				iSel, KepId, iKept);
1219 		if (pbsfs2->features) {
1220 		   if (sortby) VastTableSort(pbsfs2, sortby);
1221 		   pbsfs->features = NULL;
1222 
1223 		   numhitsdisplayed=
1224 			BFSP2VastPageDataPtr(pbsfs2, vpp, MaxVPP);
1225 	       } else numhitsdisplayed = 0;
1226 	    }
1227 	    else numhitsdisplayed = 0;
1228 
1229 	    BiostrucAnnotSetFree(basp2);
1230 	    return (numhitsdisplayed);
1231 	}
1232 	pbsfs = pbsfs->next;
1233     }
1234 
1235     BiostrucAnnotSetFree(basp2);
1236     sprintf(str, "%d", Fsid);
1237     PrtMes(NULL, "VASTSRV", "Incorrect sdid or chaindom: chaindom = ", str, FALSE);
1238 
1239 } /* MakeVppByVSNbrs */
1240 
1241 
1242 
1243 
CheckNbrs(CharPtr arg,CharPtr * SelNames,Int4 iSel,VastPageDataPtr vpp,Int4 vppnum)1244 static Int4 CheckNbrs(CharPtr arg, CharPtr *SelNames, Int4 iSel, VastPageDataPtr vpp, Int4 vppnum)
1245 {
1246    Char		savedchar, name[10];
1247    CharPtr	str, ptr0=NULL, ptr = NULL, ptr2 = NULL;
1248    Int4		i, j, count=0;
1249 
1250    arg[0] = NULLB;
1251    if (!vppnum) {
1252 	for (i=0; i< iSel; i++) {
1253 	   char tmp[10];
1254 
1255            sprintf(tmp, "%s, ", SelNames[i]);
1256 	   StringCat(arg, tmp);
1257  	}
1258 	return;
1259    }
1260 
1261    str  = (CharPtr) MemNew (vppnum*8+1);
1262    ptr0 = (CharPtr) MemNew (vppnum*8+1);
1263 
1264    str[0] = NULLB;
1265    for (i=0; i< vppnum; i++) {
1266 	StringCat(str, vpp[i].bDomName);
1267 	StringCat(str, ",");
1268    }
1269 
1270    for (i=0; i< iSel; i++) {
1271      sprintf(ptr0, str);
1272      ptr = ptr0;
1273      while (*ptr) {
1274 	sprintf(name, SelNames[i]);
1275 	if (IsMmdbId(name)) {
1276 	    j = (Int4) atol (name);
1277 	    constructPdbIdForMmdbId(j, name);
1278 	    name[4] = NULLB;
1279 	}
1280 	ptr = SkipToSet(ptr, name);
1281 	ptr2 = SkipToSet(ptr, ",");
1282 	savedchar = *ptr2;
1283 	*ptr2 = NULLB;
1284  	if (!strncmp(name, ptr, StrLen(name))) break;
1285 	*ptr2 = savedchar;
1286 	ptr = SkipSet(ptr2, ",");
1287      }
1288      if (ptr[0] == NULLB) {
1289 	char	tmp[10];
1290 
1291 	sprintf(tmp, "%s, ", SelNames[i]);
1292 	StringCat(arg, tmp);
1293 	count++;
1294      }
1295   }
1296 
1297   MemFree(str); MemFree(ptr0);
1298   return (count);
1299 }
1300 
1301 
1302 
1303 
CheckNbrsbysdid(CharPtr NonNbr,Int4Ptr SelSds,Int4 iSds,VastPageDataPtr vpp,Int4 numhits)1304 CheckNbrsbysdid(CharPtr NonNbr, Int4Ptr SelSds, Int4 iSds,
1305         VastPageDataPtr vpp, Int4 numhits)
1306 {
1307    Int4  i, j;
1308    Char  str[10];
1309 
1310    NonNbr[0] = NULLB;
1311 
1312    for (i=0; i< iSds; i++) {
1313         j=0;
1314         while (j < numhits && SelSds[i] != vpp[j].bSdi) j++;
1315         if (j == numhits) {
1316             sprintf(str, "%d, ", SelSds[i]);
1317             StringCat(NonNbr, str);
1318         }
1319    }
1320 
1321 
1322 }       /* CheckNbrsbysdid */
1323 
1324 
1325 
1326 
Main()1327 Int2 Main()
1328 {
1329   Char 		cTable='n', ODBCInitStr[100], LIBPathStr[100], str[15];
1330   Char          NonNbr[MAX_TBUFF];
1331   CharPtr 	JobID = NULL, pcPass, www_arg, Cmd=NULL, *SelNames=NULL;
1332   CharPtr	selnbrstring = NULL, selsdidstring=NULL;
1333   Int2 		ret, ImgSize;
1334   Int4 		aDomId, indx, pagenum, numhitsdisplayed, totalnumhits;
1335   Int4 		maxAsdi = 0, iSel=0, iKept=0, i, *KepBsfId;
1336   Int4		NbrFlag, iSubBut=DEF_LIST_SUBMIT, *SelSds, iSds;
1337   struct rlimit rl;
1338   SubSetID 	pre_subsetnum=DEFAULT_SUBSET_NUM;
1339   SubSetID	subsetnum=DEFAULT_SUBSET_NUM;
1340   SortBy 	sortby = DEFAULT_SORT_BY;
1341   WWWInfoPtr 	www_info;
1342   VastPageData 	vpp[MaxVPP];
1343 
1344 
1345   /* this sets up the unix time limit */
1346   getrlimit(RLIMIT_CPU, &rl);
1347   rl.rlim_max = rl.rlim_cur = CPUTIME_MAX;
1348   setrlimit(RLIMIT_CPU, &rl);
1349 
1350 /* Begin processing www information block */
1351   if (WWWGetArgs(&www_info) != WWWErrOk)
1352 	PrtMes(NULL, "VASTSRV",
1353 		"Failed to process posting - check your get/post syntax.",
1354 		 NULL, FALSE);
1355 
1356   if (WWWGetNumEntries(www_info) == 0)
1357 	PrtMes(NULL, "VASTSERV", "No input - nothing to report.", NULL, FALSE);
1358 
1359 
1360   if (! GetVastParams())
1361         PrtMes(MAILto, "VASTSRV",
1362                 "Couldn't read the config file \".vastrc\"...", NULL, FALSE);
1363 
1364   sprintf(ODBCInitStr, "ODBCINI=%s", INITpath);
1365   putenv(ODBCInitStr);
1366 
1367   VastSrvInitialize();
1368   MmdbSrvInitialize();
1369 
1370   if ((indx= WWWFindName(www_info, "dispsub")) >=0)
1371 		iSubBut = DEF_LIST_SUBMIT;
1372   else if ((indx=WWWFindName(www_info,"viewstr")) >=0)
1373 		iSubBut=VIEW_STR_SUBMIT;
1374   else if ((indx=WWWFindName(www_info,"viewali")) >=0)
1375 		iSubBut=VIEW_ALI_SUBMIT;
1376   else if ((indx= WWWFindName(www_info,"schsub")) >=0)
1377 		iSubBut=FIND_SUBMIT;
1378 /*
1379    else if ((indx= WWWFindName(www_info, "selnbr")) >=0) {
1380        www_arg = WWWGetValueByIndex(www_info, indx);
1381        if (www_arg[0] != NULLB) iSubBut = FIND_SUBMIT;
1382        else if ((indx= WWWFindName(www_info, "selsdid")) >=0) {
1383                www_arg = WWWGetValueByIndex(www_info, indx);
1384                if (www_arg[0] != NULLB) iSubBut = FIND_SUBMIT;
1385          }
1386    }
1387    else if ((indx= WWWFindName(www_info, "selsdid")) >=0) {
1388                  www_arg = WWWGetValueByIndex(www_info, indx);
1389                  if (www_arg[0] != NULLB) iSubBut = FIND_SUBMIT;
1390    }
1391 */
1392 
1393   if (iSubBut == VIEW_STR_SUBMIT) {
1394 				/* check whether or not to launch a viewer */
1395     if ((indx = WWWFindName(www_info, "calltype")) >= 0) {
1396       www_arg = WWWGetValueByIndex(www_info, indx);
1397 
1398       switch (www_arg[0]) {
1399       	case 'a':			/* Cn3d 4.0 */
1400       	case 'c':			/* Cn3d 3.0 */
1401         	  (Boolean) VastToCn3DAndAli(www_info);
1402         	  exit(0);
1403       	default:
1404 	   PrtMes(NULL, "VASTSRV",
1405 		"Bad calltype -- check you viewer selection.", NULL, FALSE);
1406       }
1407     }
1408     else PrtMes(NULL, "VASTSrv",
1409 		"No calltype -- please select a viewer.", NULL, FALSE);
1410   }
1411   else if (iSubBut == VIEW_ALI_SUBMIT) {
1412     if ((indx = WWWFindName(www_info, "alitype")) >= 0) {
1413       www_arg = WWWGetValueByIndex(www_info, indx);
1414 
1415       switch (www_arg[0]) {
1416 
1417       	case 'h':			/* HTML */
1418       	case 'f':			/* FASTA */
1419       	case 't':			/* TEXT */
1420 		  (Boolean) VastToCn3DAndAli(www_info);
1421                   exit(0);
1422         default:
1423 	   PrtMes(NULL, "VASTSRV",
1424 	     "Bad alitype -- please select a correct alignment display format",
1425 	      NULL, FALSE);
1426 
1427      }
1428    }
1429    else PrtMes(NULL, "VASTSRV",
1430 	   	"No alitype -- please select a alignment display format.",
1431 		 NULL, FALSE);
1432  }
1433 
1434 
1435 /* Jie Chen: Initialization of dart database */
1436 
1437   sprintf(LIBPathStr, "LD_LIBRARY_PATH=%s", LIBpath);
1438   putenv(LIBPathStr);
1439 
1440   dartcon = Dart_Init2(DART, LOGIN, PASSWD);
1441 
1442   if (dartcon == NULL )
1443 	PrtMes(MAILto, "VASTSRV", "Unable to open dartcon", NULL, FALSE);
1444 
1445   /* load in the chaindom into memory */
1446 
1447   objmmdb1AsnLoad();
1448   objmmdb2AsnLoad();
1449   objmmdb3AsnLoad();
1450 
1451   if ((indx = WWWFindName(www_info, "chaindom")) >= 0) {
1452   	www_arg = WWWGetValueByIndex(www_info, indx);
1453   	if (isdigit(www_arg[0])) aDomId = (long) atol(www_arg);
1454 	else
1455 	   PrtMes(MAILto, "VASTSRV",
1456 		"Invalid Biostruc-feature-set-id: chaindom = ", www_arg, FALSE);
1457   }
1458   else if ((indx = WWWFindName(www_info, "sdid")) >=0) {
1459 	int maxsdi;
1460 
1461 	www_arg = WWWGetValueByIndex(www_info, indx);
1462 	if (isdigit(www_arg[0])) aSdi = (long) atol(www_arg);
1463 	else if (www_arg[0] == '-') aSdi = atol(www_arg);
1464 	else
1465 	   PrtMes(MAILto, "VASTSRV",
1466                 "Non-numeric Structure Domain Identifier (sdid) = ",
1467                  www_arg,FALSE);
1468 
1469 	maxsdi = constructMaxSdi();
1470 	if (!aSdi || aSdi > maxsdi)
1471            PrtMes(NULL, "VASTSRV",
1472                "Vast neighbor data for this domain is not yet available. Please try later again.", NULL, FALSE);
1473 
1474 	if (aSdi < 0)
1475 	   PrtMes(NULL, "VASTSRV",
1476 	     "This is an obsolete domain, no Vast information.", NULL, FALSE);
1477 
1478 	aDomId = SdiToLongDomId(aSdi);
1479         if (!aDomId) {
1480 
1481            sprintf(str, "%d", aSdi);
1482            PrtMes(MAILto, "VASTSRV",
1483 	    "Error: No domain exists for this identifier: sdid  = ",str,FALSE);
1484         }
1485 
1486   }
1487   else PrtMes(NULL, "VASTSRV",
1488 		"No chaindom or sdid -- Please input chaindom or Structure Domain Identifier.", NULL, FALSE);
1489 
1490 
1491   if ((indx = WWWFindName(www_info, "vsid")) >= 0) { /* a VAST Search job */
1492       www_arg = WWWGetValueByIndex(www_info, indx);
1493       JobID = StringSave(www_arg);
1494 
1495       if ((indx = WWWFindName(www_info, "pass")) < 0)
1496 	PrtMes(NULL, "VASTSRV",
1497 	  "Password required -- input password of your Vast Search job",
1498 	   NULL, FALSE);
1499 
1500       www_arg = WWWGetValueByIndex(www_info, indx);
1501       pcPass = StringSave(www_arg);
1502       if ((ret = Check_VastSearch_Password(pcPass, JobID)) != 1) {
1503 	  if (ret == 2) exit(0);
1504 	  PrtMes(NULL, "VASTSRV",
1505 		"Incorrect password of a Vast Search result. It could be a wrong Vast Search identifier or a wrong password.", NULL, FALSE);
1506       }
1507 
1508   }
1509   else {
1510       if (!aSdi) aSdi = LongDomIdToSdi(aDomId);
1511       if(aSdi <= 0)  {
1512         sprintf(str, "%d", aDomId);
1513 
1514 	PrtMes(NULL, "VASTSRV",
1515 	 "VAST structure neighbor calculations for this entry are in progress or an incorrect chaindom = ", str, FALSE);
1516       }
1517 
1518       maxAsdi = constructMaxASdi();
1519 
1520       if(aSdi > maxAsdi)
1521 	PrtMes(NULL, "VASTSRV",
1522 	 "VAST structure neighbor calculations for this entry are in progress.",
1523 	  NULL, FALSE);
1524   }
1525 
1526   if (!VASTInit(JobID))
1527       PrtMes(MAILto, "VASTSRV",
1528 	"Cannot find VAST data on server or in database.", NULL, FALSE);
1529 
1530   /* subset filtering; identify which subset we're working with */
1531   if ((indx = WWWFindName(www_info, "subset")) >= 0) {
1532     www_arg = WWWGetValueByIndex(www_info, indx);
1533     if (isdigit(www_arg[0])) subsetnum = atoi(www_arg);
1534     else subsetnum = getSubsetNbr(www_arg);
1535 					/* www_arg is the subsetname now */
1536   }
1537 /*  else if (JobID) subsetnum = 1; */
1538   if (iSubBut == FIND_SUBMIT) subsetnum = 1;
1539 
1540   if ((indx = WWWFindName(www_info, "presubset")) >=0) {
1541 	www_arg = WWWGetValueByIndex(www_info, indx);
1542 	if (isdigit(www_arg[0])) pre_subsetnum = atoi(www_arg);
1543 	else pre_subsetnum = getSubsetNbr(www_arg);
1544   }
1545 
1546 
1547   if (pre_subsetnum != subsetnum) pagenum = DEFAULT_PAGE;
1548   else {
1549      if ((indx = WWWFindName(www_info, "doclistpage")) < 0)
1550      	pagenum = DEFAULT_PAGE;
1551      else {
1552     	www_arg = WWWGetValueByIndex(www_info, indx);
1553     	if (isdigit(www_arg[0])) pagenum = (Int4) atoi(www_arg);
1554     	else pagenum = DEFAULT_PAGE;
1555 	if (!pagenum) pagenum = DEFAULT_PAGE;
1556      }
1557   }
1558 
1559   if ((indx = WWWFindName(www_info, "sort")) < 0)
1560     sortby = DEFAULT_SORT_BY;
1561   else {
1562     www_arg = WWWGetValueByIndex(www_info, indx);
1563     if (isdigit(www_arg[0]))
1564       sortby = (Int2) atoi(www_arg);
1565     else
1566       sortby = DEFAULT_SORT_BY;
1567   }
1568 
1569 
1570   if ((indx = WWWFindName(www_info, "table")) >= 0) {
1571 	www_arg = WWWGetValueByIndex(www_info, indx);
1572 	cTable = www_arg[0];
1573 
1574         if (cTable == 's' && (indx=WWWFindName(www_info, "hit")) <0 ) {
1575                 SaveBioAnnotSetInFile(aDomId, subsetnum, sortby, JobID);
1576                 return;
1577         }
1578   }
1579 
1580 
1581   iKept = 0;
1582   KepBsfId = NULL;
1583   if ((iSubBut==DEF_LIST_SUBMIT || iSubBut==FIND_SUBMIT)
1584 		&& (indx = WWWFindName(www_info, "hit")) >=0) {
1585 
1586     Int4 	NumLabels;
1587     CharPtr 	Name;
1588     VastPageDataPtr vpptmp;
1589 
1590     NumLabels = WWWGetNumEntries(www_info);
1591 
1592     for (indx=0; indx < NumLabels; indx++) {
1593 	Name = WWWGetNameByIndex(www_info, indx);
1594 	if (!StrICmp(Name, "hit")) iKept++;
1595     }
1596 
1597     if (iKept > MAX_KEPT_NBR) {
1598 	Char   str[100];
1599 
1600 	sprintf(str, "%d", MAX_KEPT_NBR);
1601 	PrtMes(NULL, "VASTSRV",
1602 		"Too many selected neighbors. The maximum allowed is ",
1603 		 str, FALSE);
1604     }
1605 
1606     KepBsfId = (Int4Ptr) MemNew (iKept * sizeof(Int4));
1607     vpptmp = (VastPageDataPtr) MemNew (iKept*5 * sizeof(VastPageData));
1608 
1609     iKept = 0;
1610     for (indx = 0; indx< NumLabels; indx++) {
1611 	Name = WWWGetNameByIndex(www_info, indx);
1612 	if (!StrICmp(Name, "hit")) {
1613 
1614 	   CharPtr ptr= NULL;
1615 
1616 	   www_arg = WWWGetValueByIndex(www_info, indx);
1617 	   if (isdigit(www_arg[0]))
1618 		KepBsfId[iKept] = vpptmp[iKept++].bBsfId = atoi(www_arg);
1619 	   else {
1620                PrtMes(NULL, "VASTSRV", "The hit format is not correct.",
1621                         NULL, TRUE);
1622                return 0;
1623            }
1624 	}
1625     }
1626 
1627     if (JobID)
1628 	numhitsdisplayed =
1629 	    MakeVppByVSNbr(aDomId, JobID, vpp, NULL,KepBsfId,0, iKept,NULL,0,0);
1630     else {
1631 	numhitsdisplayed =
1632 	    constructVastPagesByNbrs(vpptmp, (unsigned *)KepBsfId, iKept, aSdi);
1633     	OrderCopyVpp(vpptmp, vpp, iKept, KepBsfId);
1634     }
1635 
1636     MemFree(vpptmp);
1637 
1638   }
1639 
1640   NbrFlag = DEF_ALL_NBR;
1641   if (iSubBut==FIND_SUBMIT) {
1642 
1643     Int4        i, numhitsbyuid = 0, numhitsbysdid = 0;
1644     CharPtr 	NonNbrbyuid=NULL, NonNbrbysdid=NULL;
1645 
1646     if ((indx=WWWFindName(www_info, "selnbr")) >= 0) {
1647 
1648       www_arg = WWWGetValueByIndex(www_info, indx);
1649 
1650 
1651     for (i=0; i< strlen(www_arg); i++)
1652 	if (!isdigit(www_arg[i]) && (!isalpha(www_arg[i])) && (www_arg[i] != ','))
1653            {
1654 		if (i && www_arg[i-1] == ',') www_arg[i] = ' ';
1655 		else www_arg[i] = ',';
1656 	   }
1657 
1658 
1659       if (www_arg[0] != NULLB) {
1660 	MakeNbrList(www_arg, &SelNames, NULL, &iSel, 1);
1661 
1662         if (iSel) {
1663 	   if (JobID)
1664 		numhitsbyuid =
1665 		   MakeVppByVSNbr(aDomId, JobID, vpp+iKept, SelNames, NULL, 0,
1666 				iSel, KepBsfId, iKept, sortby);
1667   	   else
1668 		numhitsbyuid =
1669 	    	   constructVastPagesByNbrsWithLikeUnlikeBsfId(vpp+iKept,
1670 			MaxVPP, aSdi, SelNames, iSel,
1671 				(unsigned *)KepBsfId, iKept, sortby);
1672 
1673            numhitsdisplayed += numhitsbyuid;
1674 	   NonNbrbyuid = (CharPtr) MemNew (iSel * 10);
1675 	   CheckNbrs(NonNbrbyuid,SelNames, iSel, vpp, numhitsdisplayed);
1676 	   sprintf(NonNbr, NonNbrbyuid);
1677 
1678 	   selnbrstring =(CharPtr)MemNew((size_t)(StrLen(www_arg) +3*iSel));
1679            for (i=0; i< iSel; i++) {
1680 		if (SelNames[i][4] == ' ') SelNames[i][4] = '.';
1681 		StringCat(selnbrstring, SelNames[i]);
1682 		StringCat(selnbrstring, "%2C");
1683            }
1684 
1685 	}	/* if (iSel) */
1686       }		/* if (www_arg[0]) */
1687     }		/* if (selnbr) */
1688 
1689     if ((indx=WWWFindName(www_info, "selsdid")) >=0) {
1690 	int i;
1691 
1692         www_arg = WWWGetValueByIndex(www_info, indx);
1693 
1694 /* change return carriage. */
1695 
1696 	for (i=0; i< strlen(www_arg); i++)
1697            if (!isdigit(www_arg[i]) && (!isalpha(www_arg[i])) &&
1698 						(www_arg[i] != ','))
1699            {
1700                 if (i && www_arg[i-1] == ',') www_arg[i] = ' ';
1701                 else www_arg[i] = ',';
1702            }
1703 
1704         if (www_arg[0] != NULLB) {
1705             MakeNbrList(www_arg, NULL, &SelSds, &iSds, 0);
1706         }
1707 
1708         if (iSds) {
1709 	    char str[10];
1710 	    sprintf(www_arg, "%d", SelSds[0]);
1711 	    for (i=1; i< iSds-1; i++) {
1712 
1713 		sprintf(str, ",%d", SelSds[i]);
1714 		StringCat(www_arg, str);
1715 	    }
1716 	    if (iSds >1) {
1717 		sprintf(str, ",%d", SelSds[iSds-1]);
1718 	    	StringCat(www_arg, str);
1719 	    }
1720 
1721             if (JobID) {        /* Vast Search */
1722 		numhitsbysdid =
1723 		  MakeVppByVSNbr(aDomId, JobID, vpp+numhitsdisplayed, NULL,
1724 			SelSds, 1, iSds, KepBsfId,iKept,0);
1725             }
1726             else
1727                 numhitsbysdid =
1728                    constructVPPBySdi(vpp+numhitsdisplayed, MaxVPP, aSdi,
1729 				www_arg, (unsigned *)KepBsfId,iKept,sortby);
1730 
1731             numhitsdisplayed += numhitsbysdid;
1732             NonNbrbysdid = (CharPtr) MemNew (iSds *10);
1733             CheckNbrsbysdid(NonNbrbysdid, SelSds, iSds, vpp, numhitsdisplayed);
1734 
1735             selsdidstring = (CharPtr) MemNew (StrLen(www_arg)+3*iSds);
1736             for (i=0; i< iSds; i++) {
1737                 Char str[20];
1738 
1739                 sprintf(str, "%d%%2C", SelSds[i]);
1740                 StringCat(selsdidstring, str);
1741             }
1742         }
1743 
1744     }
1745 
1746     if (NonNbrbyuid && NonNbrbyuid[0] != NULLB) {
1747         sprintf(NonNbr, NonNbrbyuid);
1748         MemFree(NonNbrbyuid);
1749 
1750         if (NonNbrbysdid && NonNbrbysdid[0] != NULLB) {
1751                 StringCat(NonNbr, NonNbrbysdid);
1752                 MemFree(NonNbrbysdid);
1753         }
1754     }
1755     else if (NonNbrbysdid && NonNbrbysdid[0] != NULLB) {
1756         sprintf(NonNbr, NonNbrbysdid);
1757         MemFree(NonNbrbysdid);
1758     }
1759 
1760     if (NonNbr[0] != NULLB) NonNbr[StrLen(NonNbr)-2] = NULLB;
1761 
1762     if (!numhitsdisplayed && NonNbr[0] == NULLB)
1763         PrtMes(NULL, "VASTSRV",
1764           "Please select a neighbor by using checkbox or input a name in the test box following \"Find\", then click \"Find\" again.",
1765           NULL, FALSE);
1766 
1767     NbrFlag = SELECTED_NBR;
1768     vpp[0].totalNumHits = numhitsdisplayed;
1769 
1770   }     /* if (Find) */
1771 
1772   if (NbrFlag == DEF_ALL_NBR) {
1773 	if (JobID)
1774 	    numhitsdisplayed = MakeVppByVS(aDomId, JobID, vpp+iKept, subsetnum,
1775 				      sortby, pagenum, KepBsfId, iKept);
1776 	else if (subsetnum < 6)
1777     	    numhitsdisplayed=
1778 		constructVastPages(vpp+iKept,NUM_HITS_PER_PAGE,
1779 			aDomId, (unsigned *)KepBsfId, iKept, subsetnum, sortby,
1780 			NUM_HITS_PER_PAGE, pagenum);
1781 
1782     	if (subsetnum < 6) {
1783 		vpp[0].totalNumHits = vpp[iKept].totalNumHits;
1784     		numhitsdisplayed += iKept;
1785 	}
1786 	else vpp[0].totalNumHits = iKept;
1787   }
1788 
1789   if (numhitsdisplayed <= 0) {
1790       if (subsetnum == 6)
1791 	PrtMes(NULL, "VASTSRV",
1792 		"Please use checkbox(es) to select neighbor(s).", NULL, FALSE);
1793       if (NbrFlag == DEF_ALL_NBR) {
1794      	printf("Content-type: text/html\n\n");
1795      	printf("<body bgcolor = \"#f0f0f0\">\n");
1796      	printf("<br>\n");
1797      	printf("<h2><a href=\"%s%s#VASTNonbr\">", URLBase, HELPname);
1798 	printf("VAST did not find any structure neighbor.</a></h2>");
1799       }
1800       else {
1801 	Char str[MAX_TBUFF];
1802 
1803 	printf("Content-type: text/html\n\n");
1804         printf("<body bgcolor = \"#f0f0f0\">\n");
1805 	printf("<br><h2>VASTSRV:<p>\n");
1806 	if (NonNbr && NonNbr[0] != NULLB) {
1807 	   if (strchr(NonNbr, ','))
1808 	      sprintf(str," are not structure neighbors. ");
1809 	   else sprintf(str," is not a structure neighbor. ");
1810  	   printf("<font color=#CC6600>%s</font>%s\n", NonNbr, str);
1811 	}
1812       }
1813 
1814       exit(0);
1815   }
1816 
1817 
1818   if (iSubBut == DEF_LIST_SUBMIT || iSubBut == FIND_SUBMIT) {
1819 
1820     totalnumhits = vpp[0].totalNumHits;
1821     if (NbrFlag == DEF_ALL_NBR && totalnumhits <=0)
1822 	PrtMes(MAILto, "VASTSRV",
1823 	   "TotalNumberOfHits == 0, yet TotalNumberOfHitsDisplayed > 0.",
1824 	   NULL, FALSE);
1825     if (!JobID) {
1826     	aSdi = vpp[0].aSdi;
1827     	if (aSdi <= 0 )
1828 	    PrtMes(MAILto, "VASTSRV",
1829 		"TotalNumberOfHits > 0, yet aSdi == 0.", NULL, FALSE);
1830     }
1831 
1832     if ((indx = WWWFindName(www_info, "cmd")) >=0) {
1833 	Cmd = WWWGetValueByIndex(www_info, indx);
1834 	if (!StrICmp(Cmd, "graph")) {
1835 	    if ((indx = WWWFindName(www_info, "imgsize")) <0)
1836 		PrtMes(NULL, "VASTSRV",
1837 			"Missing imgsize -- please use \"imgsize = number\".",
1838 			 NULL, FALSE);
1839 	    else {
1840 		www_arg = WWWGetValueByIndex(www_info, indx);
1841 		if (isdigit(www_arg[0])) ImgSize = atoi(www_arg);
1842 		else {
1843 		    PrtMes(MAILto, "VASTSRV",
1844                  	"Missing imgsize: imgsize = ", www_arg, FALSE);
1845 		}  /* sth is wrong */
1846 		MakeVastTableGraph(vpp, numhitsdisplayed, aDomId, iKept,
1847 			selnbrstring, selsdidstring, NonNbr, sortby,
1848 			subsetnum, pagenum, totalnumhits, cTable, NbrFlag,
1849 			JobID, pcPass, ImgSize,1);
1850 	   }
1851 	}
1852     }
1853     else if (cTable == 's') {
1854 	BiostrucAnnotSetPtr     pbsa = NULL;
1855 	AsnIoPtr                aip;
1856 
1857 	pbsa = constructBASPFromVastPagePtr(vpp, numhitsdisplayed);
1858 	printf("Content-type: application/octet-stream\r\n\r\n");
1859  	        aip = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
1860         BiostrucAnnotSetAsnWrite(pbsa, aip, NULL);
1861         AsnIoFlush(aip);
1862         AsnIoClose(aip);
1863     }
1864     else
1865     	MakeVastTableGraph(vpp,numhitsdisplayed,aDomId, iKept, selnbrstring,
1866 		selsdidstring, NonNbr, sortby, subsetnum, pagenum,
1867 		totalnumhits, cTable, NbrFlag, JobID, pcPass, 0, 0);
1868   }
1869 
1870 
1871   MMDBFini();
1872   VASTFini();
1873   VastSrvFinish();
1874   MmdbSrvFinish();
1875 
1876   if (selnbrstring != NULL) {
1877  	MemFree(selnbrstring);
1878   	for (i=0; i< iSel; i++) MemFree(SelNames[i]);
1879   	MemFree(SelNames);
1880   }
1881 
1882   if (selsdidstring != NULL) {
1883        MemFree(selsdidstring);
1884        MemFree(SelSds);
1885   }
1886 
1887   Dart_Fini(dartcon);
1888 
1889 } /* end Main */
1890 
1891