1 /*
2  * $Id: vast2cn3dDB_comb.cpp,v 1.1 2005/07/26 17:12:50 chenj Exp $
3  *
4  *
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *            National Center for Biotechnology Information (NCBI)
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 do not place any restriction on its use or reproduction.
16  *  We would, however, appreciate having the NCBI and the author cited in
17  *  any work or product based on this material
18  *
19  *  Although all reasonable efforts have been taken to ensure the accuracy
20  *  and reliability of the software and data, the NLM and the U.S.
21  *  Government do not and cannot warrant the performance or results that
22  *  may be obtained by using this software or data. The NLM and the U.S.
23  *  Government disclaim all warranties, express or implied, including
24  *  warranties of performance, merchantability or fitness for any particular
25  *  purpose.
26  *
27  * ===========================================================================
28  *
29  *
30  * Author: Jie Chen, Lewis Geer, Chris Hogue, Siqian He
31  *
32  *
33  * $Log: vast2cn3dDB_comb.cpp,v $
34  * Revision 1.1  2005/07/26 17:12:50  chenj
35  * Making linux VSNbr.cgi
36  *
37  * Revision 1.3  2003/01/15 16:21:47  chenj
38  * add construcMaxSdi()
39  *
40  * Revision 1.2  2003/01/14 20:42:32  chenj
41  * change VastToCn3D() to VastToCn3DAndAli()
42  *
43  * Revision 1.1.1.1  2002/12/06 20:17:21  chenj
44  * Imported Scouces
45  *
46  *
47  *
48  * This file fetches data from pubvast database, but for Vast Search job, it
49  * still reads data from files.
50  *
51  * =========================================================================
52  *
53  */
54 
55 
56 #define NLM_GENERATED_CODE_PROTO
57 #undef  DOWNLOAD_DEBUG_MODE
58 
59 #include <corelib/ncbiexec.hpp>
60 #include <corelib/ncbifile.hpp>
61 #include <ctools/asn_converter.hpp>
62 #include "hUtilib.hpp"
63 #include "VastSrchUti.hpp"
64 #include "vastuti.hpp"
65 #include <accutils.h>
66 #include <mmdbapi1.h>
67 #include <objmime.h>
68 #include "cddalignview.h"
69 #include <objalign.h>
70 #include "vast2mage.h"
71 #include "mkbioseq.h"
72 
73 #define CPUTIME_MAX 120
74 
75 #define VIEW_STR_SUBBUT		0
76 #define VIEW_ALI_SUBBUT		1
77 #define VIEW_IN_Cn3DCACHE	'a'
78 #define VIEW_IN_Cn3D_GEN	'c'
79 #define VIEW_IN_HTML  		'h'
80 #define VIEW_IN_FASTA 		'f'
81 #define VIEW_IN_TEXT 		't'
82 #define VIEW_IN_HTML_PAGE	'H'
83 #define VIEW_IN_FASTA_PAGE	'F'
84 #define VIEW_IN_TEXT_PAGE	'T'
85 
86 
87 #define ALL_NBR 	0
88 #define CHECKED_NBR  	1
89 
90 extern  void    WWWPrintFileData(CharPtr FName,  FILE *pFile);
91 extern 	string	MAILto, JobID, Passwd;
92 extern 	Char 	Database[PATH_MAX], VSPATH[PATH_MAX];
93 extern 	unsigned	aSdi, aMmdbId, aChnNo;
94 extern 	long long 	ReqId;
95 
96 using namespace ncbi;
97 
AddMMDBIdToBioseq(BioseqPtr bsp,unsigned mmdbid)98 static void AddMMDBIdToBioseq(BioseqPtr bsp, unsigned mmdbid)
99 {
100     DbtagPtr    dbtp;
101     SeqIdPtr    sip_new = NULL;
102     SeqAnnotPtr sap_new = NULL;
103 
104     if (bsp == NULL) return;
105 
106     sap_new= SeqAnnotNew();
107     sap_new->type = 4;
108     sip_new = (SeqIdPtr)ValNodeNew(NULL);
109     sip_new->choice = SEQID_GENERAL;
110     dbtp = (DbtagPtr)DbtagNew();
111     dbtp->db = new char [10];
112     sprintf(dbtp->db, "mmdb");
113     dbtp->tag = (ObjectIdPtr) ObjectIdNew();
114     dbtp->tag->id = mmdbid;
115     sip_new->data.ptrvalue = dbtp;
116     sap_new->data = sip_new;
117     if (bsp->annot == NULL) bsp->annot = sap_new;
118     else {
119             sap_new->next = bsp->annot;
120             bsp->annot = sap_new;
121     }
122 
123     return;
124 
125 }	/* AddMMDBIdToBioseq */
126 
127 
128 
129 
RemoveGi(BioseqPtr bsp)130 static void RemoveGi(BioseqPtr bsp)
131 {
132     SeqIdPtr preSid=NULL, thisSid = (SeqIdPtr)bsp->id;
133 
134     while (thisSid != NULL) {
135 
136  	if (thisSid->choice == SEQID_GI) {
137             if (preSid) preSid->next = thisSid->next;
138 	    else bsp->id = thisSid->next;
139 	}
140 
141 	preSid = thisSid;
142         thisSid = thisSid->next;
143     }
144 }	/* RemoveGi */
145 
146 
147 
148 
149 /* Display a structural alignment in Cn3D and in HTML*/
150 
VastToCn3DAndAli(WWWInfoPtr www_info)151 void LIBCALL VastToCn3DAndAli(WWWInfoPtr www_info)
152 {
153   char 		szName[5], AsnPath[PATH_MAX], AsnName[10];
154   char		cViewType, pdbname_m[5], chain_m=' ';
155   char 		*Name, *www_arg, *szTemp;
156   short		iPDB = 0, complexity, indx;
157   short		nbr_complexity;
158   unsigned 	iFidCount=0, domNo_m, i, hits_num=0, iSubBut, *gi;
159   unsigned 	aGi=0, Fid, Fsid, NumLabels, *BsfId;
160   /* Fsid = aMmdbId * 10000 + aChnNo * 100 + aDomCumulNo , only used in VS job*/
161   /* Fid = bMmdbId * 100000 + bChnNo * 1000 + bDomCumulNo * 10 + bAlignId */
162   AsnIoPtr 		paiFile, aipr;
163   BioseqPtr		biosp;
164   BiostrucAnnotSetPtr 	pbsa = NULL, pbsaShort = NULL;
165   BiostrucAlignSeqPtr	basp = NULL;
166   BiostrucFeatureSetPtr pbsfs = NULL;
167   BiostrucFeaturePtr 	pbsf = NULL;
168   BiostrucResidueGraphSetPtr 	stdDictionary;
169   BiostrucPtr 		pbsMaster=NULL, pbsSlave=NULL, pbsSlaveHead = NULL;
170   BiostrucPtr		pbsSlaveTail;
171   BiostrucAlignPtr 	pbsaStruct;
172   BundleSeqsAlignsPtr 	bsap = NULL;
173   NcbiMimeAsn1Ptr 	pvnNcbi;
174   ObjectIdPtr		objid;
175   SeqAnnotPtr 		psaAlignHead = NULL, psaAlignTail;
176   SeqAlignPtr 		salpHead, salpTail;
177   SeqEntryPtr 		sep;
178   SeqIdPtr		sid;
179   ValNodePtr 		pvnFids = NULL;
180   VastPageDataPtr	vpp;
181 
182   /* action == 0 indicates MIME; action == 1 is text; action == 2 is save */
183   if ((indx = WWWFindName(www_info, (char *)"action")) < 0) iPDB = 0;
184   else {
185     www_arg = WWWGetValueByIndex(www_info, indx);
186 
187     if (isInt(www_arg)) iPDB = (Int4) atoi(www_arg);
188     else iPDB = 0;
189   }
190 
191 
192   if ( (indx=WWWFindName(www_info, (char *)"viewstr.x")) >=0
193   		|| (indx=WWWFindName(www_info, (char *)"viewstr")) >=0 )
194 		iSubBut=VIEW_STR_SUBBUT;
195   else if ((indx=WWWFindName(www_info, (char *)"viewali.x")) >=0
196   		|| (indx=WWWFindName(www_info, (char *)"viewali")) >=0 )
197 		iSubBut=VIEW_ALI_SUBBUT;
198   else {
199 	PrtMesC("", "VASTSRV (Vast2Cn3D)", "Please click either \"View 3D Alignment\" or \"View Sequence Alignment\".", "", false);
200   }
201 
202 
203   if (iSubBut == VIEW_STR_SUBBUT) {
204   	if ((indx = WWWFindName(www_info, (char *)"calltype")) >=0) {
205 		www_arg = WWWGetValueByIndex(www_info, indx);
206 		cViewType = www_arg[0];
207   	}
208 	else
209 	   PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)", "No calltype code -- Please choose either Cn3D or Cn3D/Cache.\n", "", false);
210 
211   }
212   else if ((indx = WWWFindName(www_info, (char *)"alitype")) >=0) {
213 	CharPtr www_arg2;
214 
215 	www_arg = WWWGetValueByIndex(www_info, indx);
216 
217   	if ((indx = WWWFindName(www_info, (char *)"nbr_complexity")) >=0) {
218 	    www_arg2 = WWWGetValueByIndex(www_info, indx);
219 	    if (isInt(www_arg2)) nbr_complexity = (Int2)atoi(www_arg2);
220             else nbr_complexity = ALL_NBR;
221 	}
222 	else nbr_complexity = ALL_NBR;
223 
224    	cViewType = www_arg[0];
225 	if (nbr_complexity == ALL_NBR) {
226 	    if (cViewType == VIEW_IN_HTML) cViewType = VIEW_IN_HTML_PAGE;
227 	    else if (cViewType == VIEW_IN_FASTA)
228 			cViewType = VIEW_IN_FASTA_PAGE;
229 	    else if (cViewType == VIEW_IN_TEXT)
230 			cViewType = VIEW_IN_TEXT_PAGE;
231 	}
232   }
233   else
234     PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)", "No alitype code -- Please choose Hypertext, Plain text or mFASTA.", "", true);
235 
236 
237   if ((indx = WWWFindName(www_info, (char *)"atm_complexity")) < 0)
238     	complexity = ONECOORDRES; /* select alpha Carbons only by default */
239   else {
240     www_arg = WWWGetValueByIndex(www_info, indx);
241     if (isInt(www_arg)) complexity = (Int2) atoi(www_arg);
242     else complexity = ONECOORDRES;
243   }
244   if ((complexity != ONECOORDRES) && (complexity != ONECOORDATOM))
245     complexity = ONECOORDRES;
246 		/* bizarre value, but default to alpha-Carbons only */
247 
248   if (cViewType == 'H' || cViewType == 'F' || cViewType == 'T') {
249 
250 	nbr_complexity = ALL_NBR;
251         if ((indx = WWWFindName(www_info, (char *)"allbfid")) < 0)  {
252               PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)", "No \"allbfid\" was submitted!", "", true);
253         }
254 
255         www_arg = WWWGetValueByIndex(www_info, indx);
256         MakeNbrList(www_arg, NULL, &BsfId,  &hits_num, 0);
257         gi = new unsigned [hits_num];
258         for (i=0; i< hits_num; i++) {
259 	    Fid = BsfId[i];
260 	    if (JobID != "") ValNodeAddInt(&pvnFids, 0, Fid);
261 	}
262 	iFidCount = i-1;
263   }
264   else {
265      nbr_complexity = CHECKED_NBR;
266 
267      if ((indx = WWWFindName(www_info, (char *)"hit")) <0) {
268 	if ((indx = WWWFindName(www_info, (char *)"defhit")) <0) {
269 	    PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)", "No defhit was submitted!", "", false);
270 	}
271 	else {
272 	   www_arg = WWWGetValueByIndex(www_info, indx);
273 
274 	   hits_num = 1;
275            gi = new unsigned;
276 	   BsfId = new unsigned;
277 
278 	   if (isInt(www_arg))
279 		BsfId[0] = Fid = atol(www_arg);
280            else {
281                    PrtMesC(MAILto, (char *)"VASTSRV (Vast2Cn3D)",
282 			(char *)"Non-numeric defhit: defhit = ", www_arg, TRUE);
283            }
284 
285            if (cViewType == VIEW_IN_Cn3D_GEN || JobID != "") {
286                  	ValNodeAddInt(&pvnFids, 0, Fid);
287 			iFidCount=1;
288 	   }
289 	}
290      }
291      else {	 /* loop over all the "hit" values in the list */
292 
293 	 NumLabels = WWWGetNumEntries(www_info);
294 
295 	 for (indx = 0; indx < NumLabels; indx++) {
296 		Name = WWWGetNameByIndex(www_info, indx);
297 		if (!StrICmp(Name, "hit")) hits_num++;
298 	 }
299 
300 	 gi = new unsigned [hits_num];
301 	 BsfId = new unsigned [hits_num];
302 
303 	 for (indx = 0; indx < NumLabels; indx++) {
304 
305 	     Name = WWWGetNameByIndex(www_info, indx);
306 
307 	     if (!StrICmp(Name, "hit")) {
308 		 www_arg = WWWGetValueByIndex(www_info, indx);
309 
310 		 if (isInt(www_arg)) Fid =atoi(www_arg);
311 		 else {
312 		   PrtMesC(MAILto,  (char *)"VASTSRV (Vast2Cn3D)",
313 			(char *)"Non-numeric hit: hit = .", www_arg, TRUE);
314 		 }
315 
316 		 if (++iFidCount > 10 && cViewType == VIEW_IN_Cn3D_GEN)
317 		 {
318 		     PrtMesC("", "VASTSrv (Vast2Cn3D)", "With general Cn3D, at most only 10 alignments can be displayed simultaneously. You may reselect neighbors or use Cn3D/Cache.", "", false);
319 			/* up to 10 boxes, ignore the rest. */
320 		 }
321 		 ValNodeAddInt(&pvnFids, 0, Fid);
322 		 BsfId[iFidCount-1] = Fid;
323 	     }
324 	 }
325      }
326   }
327 
328 
329   /* Get BiostrucAnnotSet and SeqAnnot */
330 
331 /*
332   if (JobID != "" && !ReqId) {  // old VastSearch
333     OpenMMDBAPI((POWER_VIEW  // ^ FETCH_ENTREZ ), NULL);
334     pbsa = LocalGetFeatureSet(aMmdbId, Fsid);
335   }
336   else
337 */
338 
339     VastPageDataPtr vpptmp;
340 
341     if (hits_num > 1)
342 	vpptmp = new VastPageData [hits_num];
343     vpp = new VastPageData [hits_num];
344     if (JobID == "") {
345     	if (hits_num ==1)
346     	    i=constructVastPagesByNbrs(vpp,BsfId,hits_num,aSdi, (SortBy)(-1));
347     	else
348     	    i=constructVastPagesByNbrs(vpptmp,BsfId,hits_num,aSdi,(SortBy)(-1));
349     }
350     else {
351 	if (hits_num ==1)
352 	    i=GetVSVppByNbrs(aSdi, vpp, BsfId, hits_num, (SortBy)(-1));
353 	else i=GetVSVppByNbrs(aSdi, vpptmp, BsfId, hits_num, (SortBy)(-1));
354 
355     }
356     if (i != hits_num)
357 	    PrtMesC(MAILto,"VASTSRV (Vast2Cn3D)","Error in getting VAST data.",
358 								 "", false);
359     if (hits_num > 1) {
360       OrderCopyVpp(vpptmp, vpp, hits_num, BsfId);
361       delete [] vpptmp;
362     }
363 
364     if (JobID == "") pbsa = constructBASPFromVastPagePtr(vpp, hits_num, 0);
365     else pbsa = constructBASPFromVastPagePtr(vpp, hits_num, 1);
366 
367     delete [] vpp;
368 
369 
370   if (pbsa == NULL) {
371     printf("Content-type: text/html\n\n");
372     printf("<br><center><h2>VASTSrv Error (VastToCn3D):</h2><p>\n");
373     printf("No alignment record exists for master mmdb_id = %d.</h3>\n",
374                         aMmdbId);
375     printf("Please alert \"%s\" of this problem.</center>\n",MAILto.c_str());
376   }
377 
378    pbsaShort = pbsa;
379 
380 {AsnIoPtr aipr;
381 aipr=AsnIoOpen("pbsa2.out", "w");
382 BiostrucAnnotSetAsnWrite(pbsaShort, aipr, NULL);
383 AsnIoClose(aipr);
384 }
385 
386 
387   if (pbsaShort == NULL)
388      PrtMesC("","VASTSRV (Vast2Cn3D)","Can't find alignment record.","",false);
389   pbsfs = pbsaShort->features;
390   if (pbsfs) {
391       pbsf = pbsfs->features;
392       szTemp = pbsf->name;
393       StrCut(szName, pbsf->name, 1, 4);
394       sprintf(pdbname_m, szName);
395       chain_m = szTemp[4];
396       domNo_m = (Int4) atol (szTemp+5);
397   }
398 
399   if (cViewType == VIEW_IN_Cn3DCACHE) bsap = BundleSeqsAlignsNew();
400   else if (cViewType != VIEW_IN_Cn3D_GEN) basp = BiostrucAlignSeqNew();
401 
402   for (i=0; i< hits_num; i++) {
403       unsigned bsdi;
404       bsdi = BsfId[i]/100;
405       gi[i] = constructGi(SdiToMmdbId(bsdi), SdiToChainNo(bsdi));
406   }
407 
408   if (JobID == "") aGi = constructGi(aMmdbId, aChnNo);
409   else aGi = GetVSGi(atoi(JobID.c_str()+2), aChnNo); // new VS
410 
411   /* Get Biostruc for Cn3d, and SeqEntry (Bioseq)  */
412 
413   if (cViewType==VIEW_IN_Cn3D_GEN || JobID!="") {
414 
415     pbsaStruct = BiostrucAlignNew();
416 
417     if (JobID == "") {     /* not a VS job */
418 	bool psok;
419 	pbsMaster = OpenBSP(aMmdbId, complexity, 1, TRUE, FALSE, FALSE, psok, Database);
420     }
421     else {
422 	string dir = "/tmp/" + string(JobID) + "_w/";
423 	string bFile = dir + "biostr.txt";
424 	if (!CDir(dir).Exists()) {
425 	    CExec::System((string("mkdir ") + dir).c_str());
426       	    CExec::System((string("chmod 777 ") + dir).c_str());  // temp.!!!!
427   	}
428 
429 	DownloadBiostrFromDB(JobID, bFile);
430 	pbsMaster = FetchBS((char*)bFile.c_str(),1, complexity, 1,POWER_VIEW);
431   	CExec::System((string("rm -r ") + dir).c_str());
432     }
433 
434     if (pbsMaster == NULL)
435       PrtMesC(NULL, "VASTSrv (Vast2Cn3D)", "Unable to load master structure from a Vast Search result.", "", false);
436 
437     if (JobID == "") {
438 	sep = (SeqEntryPtr) constructSeqEntryForGi(aGi, TRUE, Database);
439     }
440     else if (ReqId) { 	/* new VS job */
441 
442 	sep = (SeqEntryPtr) GetVSSeqEntryForGi(aGi, false, NULL);
443 	if (sep->choice == 1) { /* Bioseq */
444 	    RemoveGi((BioseqPtr)sep->data.ptrvalue);
445 	}
446 	else {
447 
448 	     fprintf(stderr, "sep->choice != 1 sth wrong\n");
449 	     exit(1);
450 	}
451     }
452 
453     if (sep == NULL)
454       PrtMesC(MAILto, "VASTSERV (Vast2Cn3D)", "Unable to get SeqEntry.", "", false);
455 
456     if (cViewType == VIEW_IN_Cn3D_GEN) {
457 	ValNodeLink(&(pbsaStruct->sequences), sep);
458 	if (JobID != "") {
459 		biosp = (BioseqPtr)sep->data.ptrvalue;
460 		sid = biosp->id;
461 		objid = (ObjectIdPtr) sid->data.ptrvalue;
462  	}
463 
464     }
465     else if (cViewType != VIEW_IN_Cn3DCACHE)
466     		ValNodeLink(&(basp->sequences), sep);
467     else
468 	PrtMesC("", "VASTSRV (Vast2Cn3D)", "Can't work in Cn3D/Cache mode -- please try Cn3D mode.", "", false);
469 
470 
471   /* Make a linked list of Biostrucs of the slave structures */
472 
473     for (i=0; i< hits_num; i++) {
474       Int4 bMmdbId;
475 
476       bMmdbId = SdiToMmdbId(BsfId[i]/100);
477       if (!pbsSlaveHead) {
478 	bool psok;
479 
480 	pbsSlaveHead = OpenBSP(bMmdbId, complexity, 1, TRUE, FALSE, FALSE, psok, Database);
481 	if (!pbsSlaveHead)
482 	  PrtMesC("", "VASTSRV (Vast2Cn3D)", "Unable to load slave structure.", "", false);
483 
484       /* Make Bioseq for Slaves */
485 	sep = (SeqEntryPtr) constructSeqEntryForGi(gi[i], TRUE, Database);
486 	if (sep == NULL)
487 	  PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)","Unable to get SeqEntry.","",false);
488 
489         if (cViewType == VIEW_IN_Cn3D_GEN)
490                 ValNodeLink(&(pbsaStruct->sequences), sep);
491     	else if (cViewType != VIEW_IN_Cn3DCACHE)
492                 ValNodeLink(&(basp->sequences), sep);
493     	else
494            PrtMesC("", "VASTSRV (Vast2Cn3D)", "Can't work in Cn3D/Cache mode -- please try Cn3D mode.", "", false);
495 
496 	pbsSlaveTail = pbsSlaveHead;
497       }
498       else {
499 	bool psok;
500 
501 	pbsSlave =OpenBSP(bMmdbId, complexity, 1, TRUE, FALSE, FALSE, psok, Database);
502 	if (!pbsSlave)
503 		PrtMesC("", "VASTSERV (Vast2Cn3D)", "Unable to load slave structure.", "", false);
504 
505 	sep = (SeqEntryPtr) constructSeqEntryForGi(gi[i], TRUE, Database);
506 	if (sep == NULL)
507 		PrtMesC(MAILto, "VASTSRV (Vast2Cn3D)", "Unable to get SeqEntry.", "", false);
508 
509         if (cViewType == VIEW_IN_Cn3D_GEN)
510                 ValNodeLink(&(pbsaStruct->sequences), sep);
511         else if (cViewType != VIEW_IN_Cn3DCACHE)
512                 ValNodeLink(&(basp->sequences), sep);
513         else
514            PrtMesC("", "VASTSRV (Vast2Cn3D)", "Can't work in Cn3d/Cache mode -- please try Cn3D mode.", "", false);
515 
516 	pbsSlaveTail->next = pbsSlave;
517 	pbsSlaveTail = pbsSlaveTail->next;
518 	pbsSlaveTail->next = NULL;
519       }
520     }
521   }
522   else {     /* cViewType != Cn3D_IN_GEN */
523 
524     BioseqPtr 		bsp;
525 
526 /* Get SeqEntryPtr */
527 
528     if (JobID == "")  	/* not a VS job */
529     	sep = constructSeqEntryForGi(aGi, TRUE, Database);
530     else sep = GetVSSeqEntryForGi(aGi, false, NULL);
531     if (sep == NULL)
532        PrtMesC(MAILto,"VASTSRV (Vast2Cn3D)","Unable to get Bioseq.", "", false);
533 
534     bsp = (BioseqPtr)sep->data.ptrvalue;
535     if (JobID == "") AddMMDBIdToBioseq(bsp, aMmdbId);
536 
537     if (cViewType == VIEW_IN_Cn3DCACHE) ValNodeLink(&(bsap->sequences), sep);
538     else ValNodeLink(&(basp->sequences), sep);
539 
540     for (i=0; i< hits_num; i++) {
541 
542       sep = constructSeqEntryForGi(gi[i], TRUE, Database);
543 
544       if (sep == NULL)
545          PrtMesC(MAILto,"VASTSRV (Vast2Cn3D)","Unable to get Bioseq.","",false);
546 
547       bsp = (BioseqPtr)sep->data.ptrvalue;
548       AddMMDBIdToBioseq(bsp, SdiToMmdbId(BsfId[i]/100));
549 
550       if (cViewType == VIEW_IN_Cn3DCACHE) ValNodeLink(&(bsap->sequences), sep);
551       else ValNodeLink(&(basp->sequences), sep);
552     }
553   }
554 
555   delete [] gi;
556   delete [] BsfId;
557 
558   /* Make a linked list of sequence alignments of master and slaves */
559   pbsf=pbsfs->features;
560 
561   while (pbsf) {
562     if (!psaAlignHead) {
563       psaAlignHead = fnPBSFtoPSA (pbsf); /* get the sequence alignments */
564 
565       if (psaAlignHead == NULL || psaAlignHead->data == NULL)
566 	PrtMesC(MAILto, "VASTSERV (Vast2Cn3D)", "Unable to create SeqAnnot.", "", false);
567 
568       salpHead = (SeqAlignPtr)(psaAlignHead->data);
569       salpTail = salpHead;
570 
571       if (JobID!="" && cViewType == VIEW_IN_Cn3D_GEN) {
572        	DenseDiagPtr dendiag;
573 
574       	for (dendiag=(DenseDiagPtr)salpHead->segs;dendiag;dendiag=dendiag->next)
575  	{
576       		sid = dendiag->id;
577       		sid->choice = 1;
578       		sid->data.ptrvalue = (ObjectIdPtr)objid;
579         }
580       }
581     }
582     else {
583       psaAlignTail = fnPBSFtoPSA (pbsf);
584 
585       salpTail->next = (SeqAlignPtr)(psaAlignTail->data);
586       if (psaAlignTail == NULL || psaAlignTail->data == NULL)
587 	PrtMesC(NULL, "VASTSRV (Vast2Cn3D)", "Unable to create SeqAnnot.","",false);
588 
589       salpTail = salpTail->next;
590       if (JobID != "" && cViewType == VIEW_IN_Cn3D_GEN) {
591 	DenseDiagPtr dendiag;
592 
593 	for (dendiag=(DenseDiagPtr)salpTail->segs;dendiag;dendiag=dendiag->next)
594 	{
595         	sid = dendiag->id;
596         	sid->choice = 1;
597         	sid->data.ptrvalue = (ObjectIdPtr)objid;
598 	}
599      }
600       salpTail->next = NULL;
601     }
602     pbsf = pbsf->next;
603   }
604 
605   /* assemble pvnNcbi */
606 
607   pvnNcbi = ValNodeNew(NULL);
608   if (cViewType == VIEW_IN_Cn3D_GEN) {
609 
610     pbsaStruct->master = pbsMaster;
611     pbsaStruct->slaves = pbsSlaveHead;
612     pbsaStruct->alignments = pbsaShort;
613     pbsaStruct->seqalign = psaAlignHead;
614 
615     pvnNcbi->choice =  NcbiMimeAsn1_alignstruc;
616     pvnNcbi->data.ptrvalue = pbsaStruct;
617 /*    pvnNcbi = (NcbiMimeAsn1Ptr) CheckId(pvnNcbi, JobID); */
618   	/* to check identity, yanli  */
619   }
620   else if (cViewType == VIEW_IN_HTML || cViewType == VIEW_IN_FASTA
621 	|| cViewType == VIEW_IN_HTML_PAGE || cViewType == VIEW_IN_FASTA_PAGE
622         || cViewType == VIEW_IN_TEXT || cViewType == VIEW_IN_TEXT_PAGE) {
623 
624 
625 
626 
627     if (JobID != "") { 		// VS job
628 
629 // first modification, could be done when uploading SeqEntry.
630 	sep=NULL;
631 	BioseqPtr   bsp=NULL;
632 	SeqIdPtr    sip=NULL, sip_next=NULL;
633 	ObjectIdPtr oip=NULL;
634 	PDBSeqIdPtr psip=NULL;
635 
636         sep=basp->sequences;
637 	bsp = (BioseqPtr)sep->data.ptrvalue;
638 	sip = bsp->id;
639         if (sip->choice == SEQID_LOCAL) {
640 	    sip_next = sip->next;
641 	    oip = (ObjectIdPtr)sip->data.ptrvalue;
642 	    psip = PDBSeqIdNew();
643 	    psip->mol= (char *) MemNew (6);
644 	    StrCut(psip->mol, oip->str, 1, 4);
645 //strcpy(psip->mol, "Query");
646 	    psip->chain = (Uint1)(oip->str)[5];
647             oip = ObjectIdFree(oip);
648 	    sip->choice = SEQID_PDB;
649 	    sip->next = sip_next;
650 	    sip->data.ptrvalue = psip;
651 	}
652 	else
653 	    PrtMesC(MAILto, "VastSrv(Vast2Cn3D)",
654 		"Can't find local id in JobId = ",(char *)JobID.c_str(), false);
655 
656 
657 /*
658 // second modification: could be done by changing vpp.aDomName
659  	salpHead = (SeqAlignPtr) psaAlignHead->data.ptrvalue;
660 	DenseDiagPtr  dendiag;
661 	for (dendiag = (DenseDiagPtr)salpHead->segs;dendiag;dendiag=dendiag->next)
662 	{
663 	   sid = dendiag->id;
664 	   psip = (PDBSeqIdPtr) sip->data.ptrvalue;
665 	   delete [] psip->mol;
666 	   psip->mol = new char [6];
667 	   strcpy(psip->mol, "Query");
668 	}
669 
670 */
671     }
672 
673     basp->seqalign = psaAlignHead;
674 
675     pvnNcbi->choice = NcbiMimeAsn1_alignseq;
676     pvnNcbi->data.ptrvalue = basp;
677 
678   }
679   else { 	/*if (cViewType == VIEW_IN_Cn3dCache) */
680 
681     BiostrucSeqsAlignsCddPtr bsacp;
682     SeqAlignData_seq_align_dataPtr sadp;
683 
684     bsap->seqaligns = psaAlignHead;
685     bsap->strucaligns = pbsaShort;
686 
687     sadp = ValNodeNew(NULL);
688     sadp->data.ptrvalue = bsap;
689     sadp->choice = SeqAlignData_seq_align_data_bundle;
690     bsacp = BiostrucSeqsAlignsCddNew();
691     bsacp->SeqAlignData_seq_align_data = sadp;
692     if (complexity == ONECOORDRES)
693 		bsacp->structure_type = 2;	/* ncbi_backbone */
694     else bsacp->structure_type = 3;		/* ncbi_all_atoms */
695 
696     pvnNcbi->choice = NcbiMimeAsn1_general;
697     pvnNcbi->data.ptrvalue = bsacp;
698 
699   }
700 
701 {AsnIoPtr aipr;
702 aipr=AsnIoOpen("pvnNcbi.out", "w");
703 NcbiMimeAsn1AsnWrite(pvnNcbi, aipr, NULL);
704 AsnIoClose(aipr);
705 }
706 
707   if (cViewType == VIEW_IN_Cn3D_GEN || cViewType == VIEW_IN_Cn3DCACHE) {
708     if (iPDB == 0)    /* cn3d MIME */
709       printf ("Content-type: chemical/ncbi-asn1-binary\n\n");
710 
711     else if (iPDB == 1) {    /* "See File" */
712       printf ("Content-type: text/html\n\n");
713       printf ("<HTML><body><pre>\n");
714     }
715     else        /* "Save File" */
716       printf ("Content-type: application/octet-stream\n\n");
717 
718     if (iPDB != 1) paiFile = AsnIoNew(ASNIO_BIN_OUT, stdout, NULL, NULL, NULL);
719     else paiFile = AsnIoNew(ASNIO_TEXT_OUT, stdout, NULL, NULL, NULL);
720 
721     NcbiMimeAsn1AsnWrite(pvnNcbi, paiFile, NULL);
722     AsnIoFlush(paiFile);
723     AsnIoClose(paiFile);
724   }
725   else {
726 
727     CNcbi_mime_asn1 *ptr = new CNcbi_mime_asn1;
728     CAsnConverter < CNcbi_mime_asn1, NcbiMimeAsn1>
729                 CtoCpp((AsnWriteFunc)NcbiMimeAsn1AsnWrite,
730                                         (AsnReadFunc)NcbiMimeAsn1AsnRead);
731 
732     if ( CtoCpp.FromC(pvnNcbi, ptr) ) {
733 
734     	printf("Content-type: text/html\n\n");
735     	WWWPrintFileData((char *)"sshead.txt", stdout);
736     	printf("<TABLE width=800 BORDER=0 CELLPADDING=3 CELLSPACING=0 bgcolor=#FFFFCC>\n\n");
737     	PrintQueryInfo(stdout, pdbname_m, chain_m, domNo_m);
738     	printf("</TABLE>\n");
739     	printf("<br>\n");
740     	if (cViewType == VIEW_IN_HTML || cViewType == VIEW_IN_HTML_PAGE)
741       	    CAV_DisplayMultiple(*ptr, CAV_HTML|CAV_SHOW_IDENTITY, 60, 2.0,
742 						NULL, 0, NULL, NULL, NULL);
743         else if (cViewType == VIEW_IN_FASTA || cViewType == VIEW_IN_FASTA_PAGE)
744 	{
745       	    printf("<pre>\n");
746             CAV_DisplayMultiple(*ptr,
747 		CAV_FASTA|CAV_LEFTTAILS|CAV_RIGHTTAILS|CAV_FASTA_LOWERCASE,
748 		60, 2.0, NULL, 0, NULL, NULL, NULL);
749       	    printf("</pre>\n");
750      	}
751     	else if (cViewType == VIEW_IN_TEXT || cViewType == VIEW_IN_TEXT_PAGE) {
752 		printf("<pre>\n");
753 		CAV_DisplayMultiple(*ptr, CAV_TEXT, 60, 2.0, NULL, 0, NULL,NULL, NULL);
754 		printf("</pre>\n");
755     	}
756     }
757     else {
758 	printf("Content-type: text/html\n\n");
759         WWWPrintFileData((char *)"sshead.txt", stdout);
760         printf("CtoCpp.FromC failed\n");
761 
762     }
763 
764     printf("<HR SIZE=5 NOSHADE>\n");
765     printf("</body></html>\n");
766 
767     delete ptr;		// forgot 6/23/65
768   }
769 
770   if (cViewType==VIEW_IN_Cn3D_GEN) {
771   	CloseMMDBAPI();
772   	MMDBFini();
773   }
774 
775   VastSrchFinish();
776 
777   NcbiMimeAsn1Free(pvnNcbi);
778 
779 } 	/* end of VastToCn3DAndAli */
780