NCBI C Toolkit Cross Reference

C/ddv/ddvopen.c


  1 /*  $Id: ddvopen.c,v 1.98 2006/07/13 17:11:42 bollin Exp $
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *            National Center for Biotechnology Information (NCBI)
  6 *
  7 *  This software/database is a "United States Government Work" under the
  8 *  terms of the United States Copyright Act.  It was written as part of
  9 *  the author's official duties as a United States Government employee and
 10 *  thus cannot be copyrighted.  This software/database is freely available
 11 *  to the public for use. The National Library of Medicine and the U.S.
 12 *  Government do not place any restriction on its use or reproduction.
 13 *  We would, however, appreciate having the NCBI and the author cited in
 14 *  any work or product based on this material
 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 * ===========================================================================
 25 *
 26 * File Name:  ddvopen.c
 27 *
 28 * Author:  Patrick Durand
 29 *
 30 * Version Creation Date:   06/19/99
 31 *
 32 * $Revision: 1.98 $
 33 *
 34 * File Description: code to open a SeqAlign (file & Net) and code of the
 35 * message callback for DeuxD-Viewer (DDV).
 36 *
 37 * Modifications:
 38 * --------------------------------------------------------------------------
 39 * $Log: ddvopen.c,v $
 40 * Revision 1.98  2006/07/13 17:11:42  bollin
 41 * use Uint4 instead of Uint2 for itemID values
 42 *
 43 * Revision 1.97  2004/04/01 13:43:06  lavr
 44 * Spell "occurred", "occurrence", and "occurring"
 45 *
 46 * Revision 1.96  2001/04/26 16:41:20  juran
 47 * Squelch warnings.
 48 *
 49 * Revision 1.95  2000/07/17 17:46:48  hurwitz
 50 * made it so show/hide dialog only updates view when ok is clicked
 51 *
 52 * Revision 1.94  2000/07/14 22:24:56  lewisg
 53 * fix scroll, click, visual c++ build bugs.  add command line arg parse to ddv
 54 *
 55 * Revision 1.92  2000/07/05 19:23:13  lewisg
 56 * add two panes to ddv, update msvc project files
 57 *
 58 * Revision 1.91  2000/06/16 14:57:03  lewisg
 59 * move entrez calls out of desktop
 60 *
 61 * Revision 1.90  2000/06/15 17:33:18  hurwitz
 62 * used view seqAlignPtr to get original (bug fix), and started working on left/right/center justify for DDE
 63 *
 64 * Revision 1.89  2000/06/15 16:01:07  hurwitz
 65 * rewrote section that calls ViewMgr_Attach when SeqAlign is not editable but IBMable
 66 *
 67 * Revision 1.88  2000/06/13 18:23:55  hurwitz
 68 * made ViewMgr_MakeMultiple routine, call this on each launch of DDE rather than launch of DDV
 69 *
 70 * Revision 1.87  2000/06/12 23:02:40  hurwitz
 71 * enable launch of DDE from Cn3D, swith from DDV_ComputeRuler to DDE_ReMakeRuler, get rid of styles option for DDE
 72 *
 73 * Revision 1.86  2000/06/08 20:04:38  hurwitz
 74 * made warning about converting to true multiple alignment into a Message window, and other small fixes
 75 *
 76 * Revision 1.85  2000/06/07 19:09:36  hurwitz
 77 * made DDE_ReMakeRuler work with linked list of ParaGs
 78 *
 79 * Revision 1.84  2000/06/06 14:44:16  hurwitz
 80 * fixed bug that SetAppProperty(dde_nogaps)
 81 *
 82 * Revision 1.83  2000/06/02 19:57:03  hurwitz
 83 * added a query dialog when converting from pairwise multiple to true multiple, bug fix of double freeing of a seqAlignPtr
 84 *
 85 * Revision 1.82  2000/06/01 14:40:08  hurwitz
 86 * temp fix: when DDV is launched as slave, don't allow aligned gaps
 87 *
 88 * Revision 1.81  2000/05/31 23:07:26  hurwitz
 89 * made NoGaps a runtime parameter, fixed bug with vertical scroll of show/hide list, save edits query is not performed if nothing to save
 90 *
 91 * Revision 1.80  2000/05/24 21:43:00  hurwitz
 92 * getting hide/show rows to work with DDV and DDE together
 93 *
 94 * Revision 1.79  2000/05/23 22:00:14  hurwitz
 95 * working on launch of DDE from DDV
 96 *
 97 * Revision 1.78  2000/05/15 23:39:34  lewisg
 98 * shred cblast, add menu items for gapped/ungapped, fix pdbheaders
 99 *
100 * Revision 1.77  2000/05/15 22:00:36  hurwitz
101 * put save-edits query into DDV_SlaveQuit where it belongs
102 *
103 * Revision 1.76  2000/05/12 21:18:13  hurwitz
104 * added window asking if user wants to save unsaved edits for dde
105 *
106 * Revision 1.75  2000/05/12 16:15:25  hurwitz
107 * reverted to not doing IntersectOnMaster for DDE, now determined by call to ViewMgr_Attach
108 *
109 * Revision 1.74  2000/05/11 16:28:44  wheelan
110 * made DDV_ShredAln NLM_EXTERN
111 *
112 * Revision 1.73  2000/05/10 22:03:29  thiessen
113 * temporary fix for CN3D_[TRIM,SHRED] not defined
114 *
115 * Revision 1.72  2000/05/10 16:47:25  wheelan
116 * added CN3D_SHRED and CN3D_TRIM options; added ddv_shred_aln
117 *
118 * Revision 1.71  2000/05/09 14:21:54  wheelan
119 * added ddv_truncate_overlaps
120 *
121 * Revision 1.70  2000/05/08 16:29:27  wheelan
122 * change to ungapped BLAST
123 *
124 * Revision 1.69  2000/05/04 22:43:38  hurwitz
125 * don't launch DDE on top of DDV, change some wording, redraw DDE after save to AlnMgr
126 *
127 * Revision 1.68  2000/05/03 17:48:30  hurwitz
128 * keep DDV in same column when DDE does a save
129 *
130 * Revision 1.67  2000/05/03 16:16:51  hurwitz
131 * fixed bug fix from yesterday
132 *
133 * Revision 1.66  2000/05/02 19:50:38  hurwitz
134 * fixed some bugs with launching DDE from DDV, added new alnMgr fn for positioning DDE on proper column
135 *
136 * Revision 1.65  2000/04/28 14:43:37  hurwitz
137 * send message to DDV after edits in DDE are accepted by AlnMgr
138 *
139 * Revision 1.64  2000/04/27 22:21:58  lewisg
140 * misc bugs/features
141 *
142 * Revision 1.63  2000/04/27 19:57:59  hurwitz
143 * scroll to aligned block on launch of DDE
144 *
145 * Revision 1.62  2000/04/27 13:33:41  durand
146 * SpacerSize min value fixed to 1; update view when SpacerSize is changed
147 *
148 * Revision 1.61  2000/04/21 23:00:50  hurwitz
149 * can launch DDE from DDV
150 *
151 * Revision 1.60  2000/04/20 23:27:43  lewisg
152 * misc bug fixes
153 *
154 * Revision 1.59  2000/04/19 15:45:50  hurwitz
155 * can create display for a block
156 *
157 * Revision 1.58  2000/04/17 13:30:43  durand
158 * removed g_hParent and unused functions DDV_LaunchAlignViewer and DDV_LaunchAlignEditor
159 *
160 * Revision 1.57  2000/04/11 14:08:33  durand
161 * removed a debug line
162 *
163 * Revision 1.56  2000/04/10 20:33:40  lewisg
164 * fix show/hide for blast multiple, make blast multiple API generic
165 *
166 * Revision 1.55  2000/04/10 19:53:14  durand
167 * allowed ddv to display NULL SeqAlign
168 *
169 * Revision 1.54  2000/04/08 00:37:32  lewisg
170 * multiple seqentries, NEWSEQ message, etc.
171 *
172 * Revision 1.53  2000/04/07 15:03:08  durand
173 * added use of UPDATE_TYPE_NEWSEQ; DDV now rebuilt EntitiesTable after an update msg
174 *
175 * Revision 1.52  2000/04/04 17:51:54  lewisg
176 * fix various seq import bugs
177 *
178 * Revision 1.51  2000/03/29 23:38:06  lewisg
179 * hide/show, fixes to saving and opening
180 *
181 * Revision 1.50  2000/03/29 19:26:10  lewisg
182 * use blast2seqs instead of bandalign
183 *
184 * Revision 1.49  2000/03/27 22:15:06  lewisg
185 * add show/hide row dialog
186 *
187 * Revision 1.48  2000/03/27 18:07:48  hurwitz
188 * page up and page down working, give panel focus on launch
189 *
190 * Revision 1.47  2000/03/24 20:34:58  lewisg
191 * add blast from file, bug fixes, get rid of redundant code, etc.
192 *
193 * Revision 1.46  2000/03/22 20:31:34  durand
194 * added system menus for slave DDV
195 *
196 * Revision 1.45  2000/03/21 14:21:46  durand
197 * fixed a problem with menus setup
198 *
199 * Revision 1.44  2000/03/20 19:26:53  kans
200 * need to create menus before other window contents - Mac menu bar for windows is actually in content area
201 *
202 * Revision 1.43  2000/03/16 14:11:59  durand
203 * set corretly mouse mode menu
204 *
205 * Revision 1.42  2000/03/02 21:11:06  lewisg
206 * use bandalign for import sequence, make standalone ddv use viewmgr, make dialogs modal, send color update
207 *
208 * Revision 1.41  2000/03/02 15:43:10  durand
209 * use MovableModalWindow for dialog boxes
210 *
211 * Revision 1.40  2000/03/01 22:49:41  lewisg
212 * import bioseq, neatlyindex, get rid of dead code
213 *
214 * Revision 1.39  2000/02/28 20:15:32  durand
215 * if DDV is started from Cn3D, DDV doesn't set up anymore letters layout
216 *
217 * Revision 1.38  2000/02/15 22:40:57  lewisg
218 * add ability to launch udv so that it colors by row, fixes to colormgr, track rows from viewmgr, fix visual c projects
219 *
220 * Revision 1.37  2000/02/07 21:14:08  durand
221 * added a generic answer to Update messages
222 *
223 * Revision 1.36  2000/02/07 14:03:35  durand
224 * replace BioseqUnlockById by BioseqUnlock
225 *
226 * Revision 1.35  2000/02/05 01:32:22  lewisg
227 * add viewmgr, move place freeing is done in ddv, modify visual c++ projects
228 *
229 * Revision 1.34  2000/02/04 16:05:40  durand
230 * add click action to select a row
231 *
232 * Revision 1.33  2000/02/02 14:44:32  durand
233 * added function to create data structure for block editor, fixed some bugs
234 *
235 * Revision 1.32  2000/01/26 13:38:53  durand
236 * update the GUI for the editor. Add functions to create the data to be used by the editor
237 *
238 * Revision 1.31  2000/01/18 22:49:16  lewisg
239 * send OM_MSG_FLUSH to ddv/udv, tweak CPK coloration, misc bugs
240 *
241 * Revision 1.30  2000/01/12 21:52:16  durand
242 * add import function; update menus when DDV is loaded from Cn3D
243 *
244 * Revision 1.29  2000/01/11 15:05:23  durand
245 * remove network stuff
246 *
247 * Revision 1.28  2000/01/10 15:09:46  durand
248 * Use Entrez instead of ID1
249 *
250 * Revision 1.27  2000/01/05 21:11:14  durand
251 * update mouse click actions and DrawSequence function for a better use from ddv and cn3d
252 *
253 * Revision 1.26  1999/12/30 21:08:45  lewisg
254 * bioseq import dialog
255 *
256 * Revision 1.25  1999/12/30 13:45:50  beloslyu
257 * fix comment
258 *
259 * Revision 1.24  1999/12/29 22:55:03  lewisg
260 * get rid of seqalign id
261 *
262 * Revision 1.23  1999/12/23 19:22:06  durand
263 * modify default options for DDV when loaded from Cn3D
264 *
265 * Revision 1.22  1999/12/21 15:27:24  durand
266 * avoid to quit Cn3D when closing DDV
267 *
268 * Revision 1.21  1999/12/20 20:20:41  lewisg
269 * allow cn3d to do color and ddv to do case when both are running
270 *
271 * Revision 1.20  1999/12/07 21:40:13  durand
272 * add mouse modes menu and caret facility for the editor
273 *
274 * Revision 1.19  1999/12/06 16:19:19  durand
275 * add GoTo facility to DDV
276 *
277 * Revision 1.18  1999/12/03 23:17:23  lewisg
278 * Patrick's new global update msg, argument passing when launching ddv, experimental editing
279 *
280 * Revision 1.17  1999/11/30 18:19:47  durand
281 * fix a problem with function declaration DDV_CloseData
282 *
283 * Revision 1.16  1999/11/29 15:26:26  durand
284 * designed a new GUI to fix problems under MacOS, Linux and SGI
285 *
286 * Revision 1.15  1999/11/17 22:43:58  durand
287 * speed up the selection manager for large SeqAlign
288 *
289 * Revision 1.14  1999/11/09 17:09:00  durand
290 * transfer some functions from ddvgraph to ddvcreate, so that ddvcreate remains Vibrant free and can be compiled with BLAST
291 *
292 * Revision 1.13  1999/11/04 22:11:38  durand
293 * add the Desktop to DDV. Add a better set of cleanup functions when closing DDV. Before creating color tables, try to get them from the SeqAlign
294 *
295 * Revision 1.12  1999/11/03 21:29:47  durand
296 * add CTRL and SHFT keys for mouse selection. redesign the loader functions of DDV to properly register the message callbacks
297 *
298 * Revision 1.11  1999/10/29 19:04:21  durand
299 * move DDVUpdateMSG in objmgr.h
300 *
301 * Revision 1.10  1999/10/29 14:15:39  durand
302 * add simple mouse selection functions
303 *
304 * Revision 1.9  1999/10/23 21:20:45  lewisg
305 * move g_hParent to ddvopen.c
306 *
307 * Revision 1.8  1999/10/23 14:54:34  durand
308 * resolve external symbol g_hParent
309 *
310 * Revision 1.7  1999/10/22 14:19:43  durand
311 * update the code for the startup functions of DDV drawing panel
312 *
313 * Revision 1.6  1999/10/20 18:37:31  lewisg
314 * add messagefunc for slave mode
315 *
316 * Revision 1.5  1999/10/20 13:17:18  durand
317 * add display for disc. SeqAlign tails
318 *
319 * Revision 1.4  1999/10/16 15:02:25  durand
320 * fixes due to toolkit build failed
321 *
322 * Revision 1.3  1999/10/15 21:57:36  durand
323 * add a UI for display options
324 *
325 * Revision 1.2  1999/10/12 15:01:29  lewisg
326 * resolve confict with internal/ddv
327 *
328 * Revision 1.1  1999/09/30 14:10:28  durand
329 * add ddv to toolkit
330 *
331 * Revision 1.14  1999/09/30 13:38:10  durand
332 * DDV_CreateDisplayFromIndex takes ParaG_Size as an argument
333 *
334 * Revision 1.13  1999/09/16 13:07:52  durand
335 * add File|Close and File|Open|Network commands
336 *
337 * Revision 1.12  1999/09/09 21:55:06  durand
338 * instantiate the Fle|Close command of DDV
339 *
340 * Revision 1.11  1999/09/02 17:36:07  durand
341 * reconcile ddvopen.c
342 *
343 * Revision 1.10  1999/08/19 17:15:27  wheelan
344 * added messages to indicate timing of index building vs color tables
345 *
346 * Revision 1.9  1999/08/04 18:02:12  wheelan
347 * changes to support new seqalign indexing
348 *
349 * Revision 1.8  1999/07/29 12:43:07  durand
350 * update DDV_GetAndCheckSeqAlign
351 *
352 * Revision 1.7  1999/07/20 17:18:23  durand
353 * update DDV_GetAndCheckSeqAlign for PopSet Viewer
354 *
355 * Revision 1.6  1999/07/20 14:58:01  durand
356 * use the Color Manager to display colored MSA
357 *
358 * Revision 1.5  1999/07/01 15:28:29  durand
359 * validate function loaders of DDV
360 *
361 * Revision 1.2  1999/06/28 22:07:19  durand
362 * add loader functions and clean the code with Lint and Purify
363 *
364 * Revision 1.1  1999/06/19 17:21:05  durand
365 * add Vibrant DDV code
366 *
367 *
368 *
369 * ==========================================================================
370 */
371 
372 #include <ncbi.h>
373 #include <vibrant.h>
374 #include <salfiles.h>
375 #include <pgppop.h>
376 #include <udviewer.h>
377 #include <ddvopen.h>
378 #include <ddvpanel.h>
379 #include <ddvmain.h>
380 #include <ddvgraph.h>
381 #include <ddvcreate.h>
382 #include <objmgr.h>
383 #include <gather.h>
384 #include <ddvcolor.h>
385 #include <samutil.h>
386 #include <blast.h>
387 #include <viewmgr.h>
388 #include <actutils.h>
389 
390 /*extern WindoW g_hParent = NULL;*/
391 static void DDV_RegisterMsgFuncOnBsp(Uint2 bsp_eID,DdvMainPtr dmp);
392 
393 static void DDV_Convert(ButtoN g);
394 static void DDV_NoConvert(ButtoN g);
395 
396 /*local struct used only by the Download sequence dialog box*/
397         typedef struct ddvnetopen {
398                 WindoW                  hWndMain;       /*main window*/
399                 TexT                    Entry;          /*Entrez entry*/
400                 PopuP                   AccessType;     /*database type*/
401                 ButtoN                  ok;                     /*ok button*/
402                 UDVMainMenuPtr  mmp;            /*main menu*/
403         } DDVNetOpen, PNTR DDVNetOpenPtr;
404 
405 
406 /*******************************************************************************
407 
408   Function : DDV_GetSelectedRegions()
409   
410   Purpose :  get the selected region(s) of one bioseq.
411   
412   Parameters :  om_ssp;list of selected regions (usually this field points to
413                   ObjMgr data)
414                                 bsp_eID,bsp_iID; bioseq identifiers.
415                                     
416   Return value : list of selected regions on the bioseq bsp_eID,bsp_iID
417 
418 *******************************************************************************/
419 extern ValNodePtr DDV_GetSelectedRegions(SelStructPtr om_ssp, Uint2 bsp_eID,
420         Uint2 bsp_iID)
421 {
422 SelStructPtr ssp;
423 SeqLocPtr    slp,slp2;
424 ValNodePtr   bsp_vnp=NULL,vnp;
425 
426         if (om_ssp==NULL || bsp_eID==0 || bsp_iID==0) return(NULL);
427 
428         ssp=om_ssp;
429         
430         while(ssp){
431                 if (ssp->entityID==bsp_eID && ssp->itemID==bsp_iID && 
432                         ssp->itemtype==OBJ_BIOSEQ && ssp->regiontype==OM_REGION_SEQLOC){
433                         slp=(SeqLocPtr)ssp->region;
434                         while(slp){
435                                 slp2=slp->next;
436                                 slp->next=NULL;
437                                 if (!bsp_vnp){
438                                         vnp=ValNodeAddPointer(NULL,0,(Pointer)slp);
439                                         if (!vnp) return(NULL);
440                                         bsp_vnp=vnp;
441                                 }
442                                 else{
443                                         vnp=ValNodeAddPointer(&vnp,0,(Pointer)slp);
444                                         if (!vnp){
445                                                 if (bsp_vnp) ValNodeFree(bsp_vnp);
446                                                 return(NULL);
447                                         }
448                                 }
449                                 slp->next=slp2;
450                                 slp=slp->next;
451                         }
452                 }
453                 ssp=ssp->next;
454         }
455         return(bsp_vnp);
456 }
457 
458 /*******************************************************************************
459 
460   Function : DDV_IsLetterSelected()
461   
462   Purpose : check if a bsp_pos is selected (vnp_bsp is usually built with 
463       DDV_GetSelectedRegions() function)
464   
465   Return value : TRUE if bsp_pos is selected
466 
467 *******************************************************************************/
468 extern Boolean DDV_IsLetterSelected(ValNodePtr vnp_bsp, Int4 bsp_pos)
469 {
470 Boolean    bSelected=FALSE;
471 ValNodePtr vnp;
472 SeqLocPtr  slp;
473 Int4 bsp_start,bsp_stop;
474         if (vnp_bsp==NULL || bsp_pos==(Int4)-1) return(FALSE);
475 
476         vnp=vnp_bsp;
477 
478         while(vnp){
479                 slp=(SeqLocPtr)vnp->data.ptrvalue;
480                 bsp_start=SeqLocStart(slp);
481                 bsp_stop=SeqLocStop(slp);
482                 if (bsp_pos>=bsp_start && bsp_pos<=bsp_stop){
483                         bSelected=TRUE;
484                         break;
485                 }
486                 vnp=vnp->next;
487         }
488         
489         return(bSelected);
490 }
491 
492 /*******************************************************************************
493 
494   Function : DDV_CheckBSPEntitiesInPgp()
495   
496   Purpose : check if entityID,itemID Object is in the current paragraph
497   
498   Return value : TRUE if success
499 
500 *******************************************************************************/
501 /*static Boolean DDV_CheckBSPEntities(Uint4Ptr entitiesTbl,Int4 nRowBsp,
502         Int4 nBsp,Uint2 eID,Uint2 iID)
503 {
504 Uint2     bsp_eID,bsp_iID;
505 Int4      i;
506 Boolean   bRet=FALSE;
507 
508         for (i=0;i<nBsp;i++){
509                 UDV_DecodeIdxFeat (entitiesTbl[i], &bsp_eID,&bsp_iID);
510         }
511         bsp=BioseqLockById(pgp->sip);
512         if (bsp){
513                 bsp_eID=ObjMgrGetEntityIDForPointer((Pointer)bsp);
514                 bsp_iID = GetItemIDGivenPointer (bsp_eID, 
515                                 OBJ_BIOSEQ, (Pointer) bsp);     
516         
517                 if (bsp_eID==eID && bsp_iID==iID) bRet=TRUE;
518                 BioseqUnlock(bsp);
519         }
520         return(bRet);
521 }
522 */
523 /*******************************************************************************
524 
525   Function : DDV_CheckBSPEntitiesInDisplay()
526   
527   Purpose : check if entityID,itemID Object is in the current DDV display
528   
529   Return value : TRUE if object found
530 
531 *******************************************************************************/
532 static Boolean DDV_CheckBSPEntitiesInDisplay(MsaParaGPopListPtr mpplp,
533                 Uint2 entityID,Uint4 itemID)
534 {
535 Uint2     bsp_eID;
536 Uint4     bsp_iID;
537 Int4      i;
538 Boolean   bRet=FALSE;
539 
540         /*scan the ParaG list to find the Bioseq*/
541         for (i=0;i<mpplp->nBsp;i++){
542                 UDV_DecodeIdxFeat (mpplp->entitiesTbl[i], &bsp_eID,&bsp_iID);
543                 if (bsp_eID==entityID && bsp_iID==itemID) {
544                         bRet=TRUE;
545                         break;
546                 }
547         }
548         return(bRet);
549 }
550 
551 /*******************************************************************************
552 
553   Function : DDV_GetBspListGivenIDs()
554   
555   Purpose : analyse the ParaG struct and get a list of row(s) containing the
556          BSP identified by eID and iID
557   
558   Return value : list of row number (one-based values)
559 
560 *******************************************************************************/
561 static Int4Ptr DDV_GetBspListGivenIDs(MsaParaGPopListPtr mpplp,
562                 Uint2 entityID,Uint4 itemID,Int4Ptr nFound)
563 {
564 Int4       i;
565 Int4Ptr    pList=NULL;
566 Uint2     bsp_eID;
567 Uint4     bsp_iID;
568 
569         *nFound=0;
570         
571         /*scan the ParaG list to find all the ref. of the Bioseq
572         (entityID,itemID) in the ParaG list*/
573         for (i=0;i<mpplp->nBsp;i++){
574                 UDV_DecodeIdxFeat (mpplp->entitiesTbl[i], &bsp_eID,&bsp_iID);
575                 if (bsp_eID==entityID && bsp_iID==itemID) {
576                         if (!pList){
577                                 pList=(Int4Ptr)MemNew(sizeof(Int4));
578                         }
579                         else{
580                                 pList=(Int4Ptr)MemExtend((void *)pList, 
581                                         ((*nFound)+1)*sizeof(Int4), (*nFound)*sizeof(Int4));
582                         }
583                         if (!pList) {*nFound=0;return(NULL);}
584 
585 
586                         pList[*nFound]=i+1;
587                         (*nFound)++;
588                 }
589 
590         }
591         return(pList);
592 }
593 
594 /*******************************************************************************
595 
596   Function : DDV_MSG_SELECT()
597   
598   Purpose : manage OM_MSG_SELECT & OM_MSG_DESELECT message
599   
600   Return value : OM_MSG_RET_OK if success
601 
602 *******************************************************************************/
603 static Int2 DDV_MSG_SELECT(OMMsgStructPtr ommsp,Boolean IsSelectMsg)
604 {
605 DdvMainPtr    dmp;  /*DDV panel data*/
606 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
607 Int4Ptr       bsp_List=NULL;
608 ParaGPtr      pgp;
609 
610 Boolean       bRet;
611 SeqLocPtr     slp;
612 Int4          from_col,to_col,from_row,to_row,bsp_start,bsp_stop,nBspInstances=0,i,
613               row_num;
614 
615         omudp = (OMUserDataPtr)(ommsp->omuserdata);
616         if (omudp == NULL) return(OM_MSG_RET_ERROR);
617 
618         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
619         if (dmp == NULL) return(OM_MSG_RET_ERROR);
620 
621         if (ommsp->itemtype != OBJ_BIOSEQ) return(OM_MSG_RET_OK);
622         if (ommsp->regiontype!=OM_REGION_SEQLOC) return(OM_MSG_RET_OK);
623 
624         /*Am I concern by that Bioseq ?*/
625         bRet=DDV_CheckBSPEntitiesInDisplay(&(dmp->MSA_d.pgp_l),ommsp->entityID,
626                 ommsp->itemID);
627         
628         if (bRet==FALSE) return(OM_MSG_RET_OK);  /* some other viewer */
629         
630         /*get the disp coord range*/
631         DDV_GetCurrentDispRange(dmp->hWndDDV,&(dmp->GrData),dmp->MSA_d.pgp_l.LengthAli,
632                         &from_col,&to_col,&from_row,&to_row);
633 
634         /*get the occurrence(s) (row(s) #) for that bioseq*/
635         bsp_List=DDV_GetBspListGivenIDs(&(dmp->MSA_d.pgp_l),ommsp->entityID,
636                 ommsp->itemID,&nBspInstances);
637 
638         if (bsp_List==NULL || nBspInstances==0) return(OM_MSG_RET_ERROR);
639 
640         /*scan the SeqLoc list to figure out if it's currently on the screen*/
641         slp=(SeqLocPtr)ommsp->region;
642         while(slp){/*for each SeqLoc, I try to see what region(s) has (have) to be
643                 uptdated*/
644                 for(i=0;i<nBspInstances;i++){
645                         row_num=bsp_List[i]-1;
646                         bsp_start=DDV_GetDispCoordGivenBspCoord(dmp->MSA_d.pgp_l.TableHead[row_num],
647                                 SeqLocStart(slp));
648                         bsp_stop=DDV_GetDispCoordGivenBspCoord(dmp->MSA_d.pgp_l.TableHead[row_num],
649                                 SeqLocStop(slp));
650                         if (bsp_start!=(Int4)-1 && bsp_stop!=(Int4)-1){
651                                 pgp=(ParaGPtr)(dmp->MSA_d.pgp_l.TableHead[row_num]->data.ptrvalue);
652 
653                                 row_num=pgp->StartLine+
654 
655                                                 (pgp->ScaleStyle==SCALE_POS_NONE ? (Int4)0 : (Int4)1);
656 
657                                 DDV_InvalRegion(dmp->hWndDDV,&(dmp->GrData),
658                                         _max_(bsp_start,from_col),_min_(bsp_stop,to_col),
659                                         row_num,IsSelectMsg);
660 
661                         }
662                 }
663                 slp=slp->next;
664         }
665         return(OM_MSG_RET_OK);
666 }
667 
668 /*******************************************************************************
669 
670   Function : DDV_MSG_UPDATE_CaretPos()
671   
672   Purpose : update the position of the caret (edit mode only)
673   
674   Return value : OM_MSG_RET_OK if success
675 
676 *******************************************************************************/
677 static Int2 DDV_MSG_UPDATE_CaretPos(OMMsgStructPtr ommsp)
678 {
679 DdvMainPtr    dmp;  /*DDV panel data*/
680 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
681 Boolean       bRet;
682 Int4          from_col,to_col,from_row,to_row;
683 
684         omudp = (OMUserDataPtr)(ommsp->omuserdata);
685         if (omudp == NULL) return(OM_MSG_RET_ERROR);
686 
687         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
688         if (dmp == NULL) return(OM_MSG_RET_ERROR);
689         
690         /*Am I concern by that Bioseq ?*/
691         bRet=DDV_CheckBSPEntitiesInDisplay(&(dmp->MSA_d.pgp_l),ommsp->entityID,
692                 ommsp->itemID);
693         
694         if (bRet==FALSE) return(OM_MSG_RET_ERROR);
695         
696         /*get the disp coord range*/
697         DDV_GetCurrentDispRange(dmp->hWndDDV,&(dmp->GrData),dmp->MSA_d.pgp_l.LengthAli,
698                         &from_col,&to_col,&from_row,&to_row);
699         
700         /*switch to one-bsed value because dmp->dci values are one-based values*/
701         from_row++;
702         to_row++;
703         /*hide the caret from old coordinates*/
704         if (dmp->dci.old_col>=from_col && dmp->dci.old_col<=to_col &&
705                 dmp->dci.old_row>=from_row && dmp->dci.old_row<=to_row){
706                 DDV_InvalRegion(dmp->hWndDDV,&(dmp->GrData),
707                         dmp->dci.old_col,dmp->dci.old_col+1,
708                         dmp->dci.old_row,FALSE);
709         }
710         /*show caret on new coordinates*/
711         if (dmp->dci.new_col>=from_col && dmp->dci.new_col<=to_col &&
712                 dmp->dci.new_row>=from_row && dmp->dci.new_row<=to_row){
713                 DDV_InvalRegion(dmp->hWndDDV,&(dmp->GrData),
714                         dmp->dci.new_col,dmp->dci.new_col+1,
715                         dmp->dci.new_row,FALSE);
716         }
717     return(OM_MSG_RET_OK);
718 }
719 
720 /*******************************************************************************
721 
722   Function : DDV_MSG_UPDATE_DelBSP()
723   
724   Purpose : rebuilt a display after deletion of one BSP
725   
726   Return value : OM_MSG_RET_OK if success
727 
728 *******************************************************************************/
729 static Int2 DDV_MSG_UPDATE_DelBSP(OMMsgStructPtr ommsp)
730 {
731 DdvMainPtr    dmp;  /*DDV panel data*/
732 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
733 
734         omudp = (OMUserDataPtr)(ommsp->omuserdata);
735         if (omudp == NULL) return(OM_MSG_RET_ERROR);
736 
737         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
738         if (dmp == NULL) return(OM_MSG_RET_ERROR);
739         
740     /*delete the current display*/
741         if (dmp->MSA_d.pgp_l.TableHead) 
742                 DDV_DeleteDisplayList(&dmp->MSA_d.pgp_l);
743         if (dmp->MSA_d.pgp_l.RulerDescr) 
744                 ValNodeFreeData(dmp->MSA_d.pgp_l.RulerDescr);
745         if (dmp->MSA_d.pgp_l.entitiesTbl) 
746                 MemFree(dmp->MSA_d.pgp_l.entitiesTbl);
747         dmp->MSA_d.pgp_l.TableHead=NULL;
748         dmp->MSA_d.pgp_l.RulerDescr=NULL;
749         /*rebuild a new one*/
750         if (!DDV_CreateDisplayFromIndex(dmp->MSA_d.pgp_l.sap,
751                 &(dmp->MSA_d.pgp_l),ParaG_Size,&(dmp->ddo))) 
752                 return(OM_MSG_RET_ERROR);
753 
754         dmp->MSA_d.pgp_l.entitiesTbl=
755                 DDV_BuildBspEntitiesTbl(dmp->MSA_d.pgp_l.TableHead,
756                         dmp->MSA_d.pgp_l.nBsp);
757         if (!dmp->MSA_d.pgp_l.entitiesTbl)
758                 return(OM_MSG_RET_ERROR);       
759         /*build the Master Ruler descriptor*/
760 /*      dmp->MSA_d.pgp_l.RulerDescr=DDV_ComputeRuler(dmp->MSA_d.pgp_l.sap,&(dmp->ddo)); */
761   dmp->MSA_d.pgp_l.RulerDescr = DDE_ReMakeRuler(&(dmp->MSA_d.pgp_l), FALSE, 0);
762 
763         /*delete old tables*/
764     if(dmp->MasterViewer != SAMVIEWCN3D) {
765         DDV_ClearColor(dmp->Globals.colorp);
766         /*build new tables*/
767         if (!DDV_InitColour_When_Start(dmp->MSA_d.pgp_l.sap,
768             &(dmp->MSA_d.pgp_l),&(dmp->Globals.colorp), FALSE)){
769             dmp->ddo.bUseColors=FALSE;
770         }
771     }
772     
773     DDV_SortPGPLineNum(dmp->MSA_d.pgp_l.TableHead,dmp->MSA_d.pgp_l.nBsp);
774         DDV_WhatSize(dmp);
775         /* DDV_SetupWin(dmp->hWndDDV,TRUE,NULL); */
776     DDV_Resize_DDV(dmp->hWndDDV,TRUE); 
777         Update();
778 
779     return(OM_MSG_RET_OK);
780 }
781 /*******************************************************************************
782 
783   Function : DDV_MSG_UPDATE_ViewMgr()
784   
785   Purpose : rebuild a display after update message from the ViewMgr
786   
787   Return value : OM_MSG_RET_OK if success
788 
789 *******************************************************************************/
790 static Int2 DDV_MSG_UPDATE_ViewMgr(OMMsgStructPtr ommsp)
791 {
792 DdvMainPtr    dmp;  /*DDV panel data*/
793 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
794 BaR           hsb;
795 Int4          HPos;
796 
797         omudp = (OMUserDataPtr)(ommsp->omuserdata);
798         if (omudp == NULL) return(OM_MSG_RET_ERROR);
799 
800         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
801         if (dmp == NULL) return(OM_MSG_RET_ERROR);
802 
803   if (dmp->bEditor) return(OM_MSG_RET_OK);
804         
805   /* get the current column */
806   hsb = GetSlateHScrollBar((SlatE) dmp->hWndDDV);
807   HPos = GetBarValue(hsb);
808   
809   /*delete the current display*/
810         if (dmp->MSA_d.pgp_l.TableHead) 
811                 DDV_DeleteDisplayList(&dmp->MSA_d.pgp_l);
812         if (dmp->MSA_d.pgp_l.RulerDescr) 
813                 ValNodeFreeData(dmp->MSA_d.pgp_l.RulerDescr);
814         if (dmp->MSA_d.pgp_l.entitiesTbl) 
815                 MemFree(dmp->MSA_d.pgp_l.entitiesTbl);
816         dmp->MSA_d.pgp_l.TableHead=NULL;
817         dmp->MSA_d.pgp_l.RulerDescr=NULL;
818         /*rebuild a new one*/
819         if (!DDV_CreateDisplayFromIndex(dmp->MSA_d.pgp_l.sap,
820                 &(dmp->MSA_d.pgp_l),ParaG_Size,&(dmp->ddo))) 
821                 return(OM_MSG_RET_ERROR);
822 
823         dmp->MSA_d.pgp_l.entitiesTbl=
824                 DDV_BuildBspEntitiesTbl(dmp->MSA_d.pgp_l.TableHead,
825                         dmp->MSA_d.pgp_l.nBsp);
826         if (!dmp->MSA_d.pgp_l.entitiesTbl)
827                 return(OM_MSG_RET_ERROR);
828 
829         /* build the Master Ruler descriptor */
830   /* Patrick's ComputeRuler isn't working right, so I substituted mine -- Dave H. */
831   /* dmp->MSA_d.pgp_l.RulerDescr=DDV_ComputeRuler(dmp->MSA_d.pgp_l.sap,&(dmp->ddo)); */
832   dmp->MSA_d.pgp_l.RulerDescr = DDE_ReMakeRuler(&(dmp->MSA_d.pgp_l), FALSE, 0);
833 
834         /*delete old tables*/
835     if(dmp->MasterViewer != SAMVIEWCN3D) {
836         DDV_ClearColor(dmp->Globals.colorp);
837         /*build new tables*/
838         if (!DDV_InitColour_When_Start(dmp->MSA_d.pgp_l.sap,
839             &(dmp->MSA_d.pgp_l),&(dmp->Globals.colorp), FALSE)){
840             dmp->ddo.bUseColors=FALSE;
841         }
842     }
843     
844   DDV_SortPGPLineNum(dmp->MSA_d.pgp_l.TableHead,dmp->MSA_d.pgp_l.nBsp);
845         DDV_WhatSize(dmp);
846         DDV_Resize_DDV(dmp->hWndDDV,TRUE); /* DDV_SetupWin(dmp->hWndDDV,TRUE,NULL); */
847 
848   /* put col back to where it was */
849   SetValue(hsb, HPos);
850 
851         Update();
852 
853     return(OM_MSG_RET_OK);
854 }
855 
856 /*******************************************************************************
857 
858   Function : DDV_MSG_UPDATE_Layout()
859   
860   Purpose : manage the OM_MSG_UPDATE message
861   
862   Return value : OM_MSG_RET_OK if success
863 
864 *******************************************************************************/
865 static Int2 DDV_MSG_UPDATE_Layout(PaneL hWndDDV, DDVUpdateLayoutDataPtr dumdp)
866 {
867 DdvMainPtr  dmp;
868 Int4        j;
869 Boolean     bResetScrolls=FALSE,
870                         bRebuildDisplay=FALSE,
871                         bSwitchColors=FALSE,
872                         bUpdateColors=FALSE;
873 Uint1       oldSpacerSize;
874 
875         if (!hWndDDV || !dumdp)
876                 return(OM_MSG_RET_OK);
877         
878         dmp = (DdvMainPtr) GetObjectExtra(hWndDDV);
879         
880         if (!dmp)
881                 return(OM_MSG_RET_OK);
882 
883         if (!Visible(hWndDDV))
884                 return(OM_MSG_RET_OK);
885         
886         oldSpacerSize=dmp->ddo.SpacerSize;
887         dmp->ddo.SpacerSize=dumdp->SpacerSize;
888 
889         /*rebuild the display, if needed*/      
890         if ((dmp->ddo.DispDiscStyle!=dumdp->DispDiscStyle) ||
891                 (oldSpacerSize!=dumdp->SpacerSize) ||
892                 (dmp->ddo.DiscJustification!=dumdp->DiscJustification) ||
893                 (dmp->ddo.ShowLeftTail!=dumdp->ShowLeftTail)||
894                 (dmp->ddo.ShowRightTail!=dumdp->ShowRightTail)){
895                 
896                 if((dmp->ddo.ShowLeftTail!=dumdp->ShowLeftTail)||
897                         (dmp->ddo.ShowRightTail!=dumdp->ShowRightTail)||
898                         (dmp->ddo.DispDiscStyle!=dumdp->DispDiscStyle)){
899                         bUpdateColors=TRUE;
900                 }
901                 /*update the styles*/
902                 dmp->ddo.DispDiscStyle=dumdp->DispDiscStyle;
903                 dmp->ddo.DiscJustification=dumdp->DiscJustification;
904                 dmp->ddo.ShowLeftTail=dumdp->ShowLeftTail;
905                 dmp->ddo.ShowRightTail=dumdp->ShowRightTail;
906                 
907                 /*delete the current display*/
908                 if (dmp->MSA_d.pgp_l.TableHead) 
909                         DDV_DeleteDisplayList(&dmp->MSA_d.pgp_l);
910                 if (dmp->MSA_d.pgp_l.RulerDescr) 
911                         ValNodeFreeData(dmp->MSA_d.pgp_l.RulerDescr);
912                 if (dmp->MSA_d.pgp_l.entitiesTbl) 
913                         MemFree(dmp->MSA_d.pgp_l.entitiesTbl);
914 
915                 dmp->MSA_d.pgp_l.TableHead=NULL;
916                 dmp->MSA_d.pgp_l.RulerDescr=NULL;
917                 /*rebuild a new one*/
918                 if (!DDV_CreateDisplayFromIndex(dmp->MSA_d.pgp_l.sap,
919                         &(dmp->MSA_d.pgp_l),ParaG_Size,&(dmp->ddo))) 
920                         return(OM_MSG_RET_ERROR);
921 
922                 dmp->MSA_d.pgp_l.entitiesTbl=
923                         DDV_BuildBspEntitiesTbl(dmp->MSA_d.pgp_l.TableHead,
924                                 dmp->MSA_d.pgp_l.nBsp);
925                 if (!dmp->MSA_d.pgp_l.entitiesTbl)
926                         return(OM_MSG_RET_ERROR);       
927                 /*build the Master Ruler descriptor*/
928 /*              dmp->MSA_d.pgp_l.RulerDescr=DDV_ComputeRuler(dmp->MSA_d.pgp_l.sap,&(dmp->ddo));  */
929     dmp->MSA_d.pgp_l.RulerDescr = DDE_ReMakeRuler(&(dmp->MSA_d.pgp_l), FALSE, 0);
930 
931                 /*init the colours*/
932                 if(bUpdateColors){
933             if(dmp->MasterViewer != SAMVIEWCN3D ) {
934                 /*delete old tables*/
935                 DDV_ClearColor(dmp->Globals.colorp);
936                 /*build new tables*/
937                 if (!DDV_InitColour_When_Start(dmp->MSA_d.pgp_l.sap,
938                     &(dmp->MSA_d.pgp_l),&(dmp->Globals.colorp),FALSE)){
939                     dmp->ddo.bUseColors=FALSE;
940                 }
941             }
942         }
943         bResetScrolls=TRUE;
944                 bRebuildDisplay=TRUE;
945         }
946 
947         /*set the ruler styles*/
948         if(dumdp->nSeq && dumdp->SeqList){
949                 for(j=0;j<dumdp->nSeq;j++){
950                         switch(dumdp->RulerStyle){
951                                 case SCALE_POS_TOP:
952                                         DDV_SetRulerAttribInPGP(dmp->MSA_d.pgp_l.TableHead[dumdp->SeqList[j]-1], 
953                                                         SCALE_POS_TOP);
954                                         break;
955                                 case SCALE_POS_NONE:
956                                         DDV_SetRulerAttribInPGP(dmp->MSA_d.pgp_l.TableHead[dumdp->SeqList[j]-1], 
957                                                         SCALE_POS_NONE);
958                                         break;
959                                 default:
960                                         DDV_SetRulerAttribInPGP(dmp->MSA_d.pgp_l.TableHead[dumdp->SeqList[j]-1], 
961                                                         SCALE_POS_NONE);
962                                         break;
963                         }                       
964                 }
965                 bRebuildDisplay=TRUE;
966         }
967 
968         /*colors*/
969         if(dmp->ddo.bUseColors!=dumdp->bUseColors){
970                 dmp->ddo.bUseColors=dumdp->bUseColors;
971                 bSwitchColors=TRUE;
972         }
973 
974         /*redraw if needed*/
975         if (bRebuildDisplay){
976             DDV_SortPGPLineNum(dmp->MSA_d.pgp_l.TableHead,dmp->MSA_d.pgp_l.nBsp);
977             DDV_WhatSize(dmp);
978             DDV_Resize_DDV(hWndDDV,bResetScrolls); /* DDV_SetupWin(hWndDDV,bResetScrolls,NULL);*/
979             Update();
980         }
981         else if (bSwitchColors){
982                 RecT   rcP;
983                 WindoW temport;
984                 
985                 temport=SavePort(ParentWindow(hWndDDV));
986                 Select(hWndDDV);
987                 
988                 ObjectRect(hWndDDV,&rcP);
989                 InvalRect(&rcP);
990                 RestorePort(temport);
991                 /*Update();*/
992         }
993 
994 
995         return(OM_MSG_RET_OK);
996 }
997 
998 /*******************************************************************************
999 
1000   Function : DDV_MSG_FLUSH()
1001   
1002   Purpose : kill the viewer in response to a OM_MSG_FLUSH message
1003   
1004   Return value : OM_MSG_RET_OK if success
1005 
1006 *******************************************************************************/
1007 static Int2 DDV_MSG_FLUSH(OMMsgStructPtr ommsp)
1008 {
1009 DdvMainPtr    dmp;  /*DDV panel data*/
1010 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
1011 /*ParaGPtr pgp;
1012 BioseqPtr bsp;
1013 Int4 i;
1014 Uint2 bsp_eID;*/
1015 
1016 
1017         omudp = (OMUserDataPtr)(ommsp->omuserdata);
1018         if (omudp == NULL) return(OM_MSG_RET_ERROR);
1019 
1020         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
1021         if (dmp == NULL) return(OM_MSG_RET_ERROR);
1022         
1023     if (dmp->MSA_d.entityID==ommsp->entityID &&
1024         ((dmp->MSA_d.itemID==ommsp->itemID && ommsp->itemtype==OBJ_SEQALIGN)
1025         ||(ommsp->itemID == 0 && ommsp->itemtype==0))){
1026         Remove(dmp->hParent);
1027         DDV_CleanupDDVPdata_g(dmp);
1028        /* for(i=0;i<dmp->MSA_d.pgp_l.nBsp;i++){
1029             pgp=(ParaGPtr)(dmp->MSA_d.pgp_l.TableHead[i]->data.ptrvalue);
1030             bsp=BioseqLockById(pgp->sip);
1031             if (bsp != NULL){
1032                 bsp_eID=ObjMgrGetEntityIDForPointer((Pointer)bsp);
1033                 BioseqUnlock(bsp);
1034                 if (bsp_eID>0 && bsp_eID!=dmp->MSA_d.entityID){
1035                     ObjMgrFreeUserData(bsp_eID,dmp->procid,dmp->proctype,dmp->userkey);
1036                 }
1037             }
1038         }
1039         
1040         ObjMgrFreeUserData(dmp->MSA_d.entityID,dmp->procid,dmp->proctype,dmp->userkey);*/
1041     }
1042         return(OM_MSG_RET_OK);
1043 }
1044 
1045 /*******************************************************************************
1046 
1047   Function : DDV_SimpleUpdateAnswer()
1048   
1049   Purpose : simple answer to an Update Msg; just redraw the window
1050   
1051   Parameters : see Toolkit
1052   
1053   Return value : see Toolkit 
1054 
1055 *******************************************************************************/
1056 static void DDV_SimpleUpdateAnswer(OMMsgStructPtr ommsp)
1057 {                           
1058 RecT   rcP;
1059 WindoW temport;
1060 DdvMainPtr    dmp;  /*DDV panel data*/
1061 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
1062 
1063         omudp = (OMUserDataPtr)(ommsp->omuserdata);
1064         if (omudp == NULL) return;
1065 
1066         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
1067         if (dmp == NULL) return;
1068         
1069         temport=SavePort(ParentWindow(dmp->hWndDDV));
1070         Select(dmp->hWndDDV);
1071         
1072         ObjectRect(dmp->hWndDDV,&rcP);
1073         InvalRect(&rcP);
1074         RestorePort(temport);
1075 }
1076 
1077 /*******************************************************************************
1078 
1079   Function : DDV_UpdateRegMsgFunc()
1080   
1081   Purpose : register a MsgCallback in response to an OM_MSG_UPDATE, after 
1082           SeqImport
1083   
1084 *******************************************************************************/
1085 static void DDV_UpdateRegMsgFunc(OMMsgStructPtr ommsp,Uint2 bsp_eID)
1086 {
1087 DdvMainPtr    dmp;  /*DDV panel data*/
1088 OMUserDataPtr omudp;/*user data set when registering DDV panel*/
1089 
1090         omudp = (OMUserDataPtr)(ommsp->omuserdata);
1091         if (omudp == NULL) return;
1092 
1093         dmp=(DdvMainPtr)omudp->userdata.ptrvalue;
1094         if (dmp == NULL) return;
1095 
1096         DDV_RegisterMsgFuncOnBsp(bsp_eID,dmp);          
1097 }
1098 
1099 /*******************************************************************************
1100 
1101   Function : DDV_OM_MsgFunc()
1102   
1103   Purpose : ObjMgr message loop of the DDV viewer/editor
1104   
1105   Parameters : see Toolkit
1106   
1107   Return value : see Toolkit 
1108 
1109 *******************************************************************************/
1110 static Int2 LIBCALLBACK DDV_OM_MsgFunc (OMMsgStructPtr ommsp)
1111 {
1112 Int2 nRet=OM_MSG_RET_OK;
1113   
1114         switch (ommsp->message)
1115         {
1116                 case OM_MSG_DEL:
1117                         break;
1118                 case OM_MSG_CREATE:
1119                         break;
1120                 case OM_MSG_UPDATE:{
1121                         DDVUpdateMSGPtr        dump;
1122                         
1123                         dump = (DDVUpdateMSGPtr)(ommsp->procmsgdata);
1124                         
1125                         /*generic UPDATE msg*/
1126                         if (!dump){
1127                                 DDV_SimpleUpdateAnswer(ommsp);
1128                                 break;
1129                         }
1130 
1131                         /*specific UPDATE msg*/
1132             switch(dump->type){
1133                 case UPDATE_TYPE_LAYOUT:{
1134                     DDVUpdateLayoutDataPtr dumdp;
1135                 
1136                     dumdp=(DDVUpdateLayoutDataPtr)dump->data;
1137                 
1138                     if (!dumdp)
1139                         break;
1140                 
1141                     nRet=DDV_MSG_UPDATE_Layout(dumdp->ddv_panel,dumdp);
1142                     break;
1143                 }
1144                 case UPDATE_TYPE_EDIT_DELBSP:
1145                     nRet=DDV_MSG_UPDATE_DelBSP(ommsp);
1146                     break;
1147                 case UPDATE_TYPE_VIEWMGR:
1148                     nRet=DDV_MSG_UPDATE_ViewMgr(ommsp);
1149                     break;
1150                                 case UPDATE_TYPE_CARETPOS:
1151                                         nRet=DDV_MSG_UPDATE_CaretPos(ommsp);
1152                                         break;
1153                 case UPDATE_TYPE_NEWSEQ:
1154                                         if (dump->data)
1155                                                 DDV_UpdateRegMsgFunc(ommsp,*((Uint2 *)dump->data));
1156                                         break;
1157                                 default :
1158                                         DDV_SimpleUpdateAnswer(ommsp);
1159             }
1160                         
1161                         break;
1162                 }
1163                 case OM_MSG_SELECT:
1164                         nRet=DDV_MSG_SELECT(ommsp,TRUE);
1165                         break;
1166                 case OM_MSG_DESELECT:
1167                         nRet=DDV_MSG_SELECT(ommsp,FALSE);
1168                         break;
1169                 case OM_MSG_CACHED:
1170                         break;
1171                 case OM_MSG_UNCACHED:
1172                         break;
1173                 case OM_MSG_TO_CLIPBOARD:
1174                         break;
1175                 case OM_MSG_SETCOLOR:
1176                         break;
1177                 case OM_MSG_FLUSH:
1178                         DDV_MSG_FLUSH(ommsp);
1179                         break;
1180                 default:
1181                         break;
1182         }
1183 
1184         return (nRet);
1185 }
1186 
1187 /*******************************************************************************
1188 
1189   Function : DDV_RegisterMsgFuncOnBsp()
1190   
1191   Purpose : register a MsgFunc for a bsp. 
1192   
1193 *******************************************************************************/
1194 static void DDV_RegisterMsgFuncOnBsp(Uint2 bsp_eID,DdvMainPtr dmp)
1195 {
1196 OMUserDataPtr omudp;
1197 
1198         if (bsp_eID==0 || dmp==NULL) return;
1199         
1200     omudp = ObjMgrAddUserData (bsp_eID,dmp->procid,dmp->proctype,dmp->userkey);
1201     if (omudp != NULL) {
1202         omudp->messagefunc = DDV_OM_MsgFunc;
1203                 omudp->userdata.ptrvalue = (Pointer)dmp;
1204         }
1205 }
1206 
1207 /*******************************************************************************
1208 
1209   Function : DDV_RegMsgFuncForBsp()
1210   
1211   Purpose : given the display data structure (nBsp & TableHead), register a
1212             MsgFunc callback for each bioseq of a SeqAlign.
1213   
1214   Parameters : TableHead; entry point of the display data structure (ParaG list)
1215                            nBsp; number of sequences in the SeqAlign
1216                            master_eID; entityID of the object containing the SeqAlign
1217                            procID; identifier of the viewer (usually DDV)
1218                            userkey; userkey of the viewer (usually DDV)
1219                            
1220   Return value : -
1221 
1222 *******************************************************************************/
1223 static void DDV_RegMsgFuncForBsp(ValNodePtr PNTR TableHead,DdvMainPtr dmp,Int4 nBsp, 
1224         Uint2 master_eID, Uint2 procID, Uint2 procType, Uint2 userkey)
1225 {
1226 OMUserDataPtr omudp,omudp_tmp;
1227 ParaGPtr      pgp;
1228 BioseqPtr     bsp;
1229 Int4          i;
1230 Boolean       bFound;
1231 Uint2         bsp_eID;
1232 
1233         if (!TableHead || nBsp==0 || master_eID==0 || procID==0 
1234                 || procType==0 ||userkey==0) return;
1235                 
1236         for(i=0;i<nBsp;i++){
1237                 pgp=(ParaGPtr)(dmp->MSA_d.pgp_l.TableHead[i]->data.ptrvalue);
1238                 bsp=BioseqLockById(pgp->sip);
1239                 if (bsp){
1240                         bsp_eID=ObjMgrGetEntityIDForPointer((Pointer)bsp);
1241                         if (bsp_eID>0 && bsp_eID!=master_eID){
1242                                 omudp=ObjMgrGetUserData(bsp_eID,procID,procType,userkey);
1243                                 if (omudp){
1244                                         /*scan the user data associated with that bioseq and try to
1245                                         find if a Msg Func is already attached for DDV*/
1246                                         omudp_tmp=omudp;
1247                                         bFound=FALSE;
1248                                         while(omudp_tmp){
1249                                                 if (omudp_tmp->procid==procID && 
1250                                                         omudp_tmp->proctype==procType && 
1251                                                         omudp_tmp->userkey==userkey && 
1252                                                         omudp_tmp->messagefunc==DDV_OM_MsgFunc){
1253                                                         
1254                                                         bFound=TRUE;
1255                                                         break;
1256                                                 }
1257                                                 omudp_tmp=omudp_tmp->next;
1258                                         }
1259                                 }
1260                                 else{/*add the Msg Callaback and DDV main data block to the bioseq*/
1261                                         bFound=FALSE;
1262                                 }
1263                                 if (bFound==FALSE){
1264                                         DDV_RegisterMsgFuncOnBsp(bsp_eID,dmp);
1265                                 }
1266                         }
1267                         BioseqUnlock(bsp);
1268                 }
1269         }
1270 }
1271 
1272 static void DDV_Convert(ButtoN g) {
1273 /*----------------------------------------------------------------------------
1274 *  just need to quit the pop-up window
1275 *---------------------------------------------------------------------------*/
1276   WindoW  hDialog;
1277 
1278         hDialog = (WindoW)ParentWindow(g);
1279         if (!hDialog) return;
1280         Remove(hDialog);
1281   return;
1282 }
1283 
1284 
1285 static void DDV_NoConvert(ButtoN g) {
1286 /*----------------------------------------------------------------------------
1287 *  quit the pop-up window and close the file
1288 *---------------------------------------------------------------------------*/
1289   WindoW         hDialog;
1290   DdvMainPtr     dmp;
1291 
1292         hDialog = (WindoW)ParentWindow(g);
1293         if (!hDialog) return;
1294         Remove(hDialog);
1295 
1296   dmp = (DdvMainPtr) GetObjectExtra(hDialog);
1297   DDV_FileCloseIt(dmp->hParent);
1298   return;
1299 }
1300 
1301 
1302 /*******************************************************************************
1303 
1304   Function : DDV_StartPanel_Slave()
1305   
1306   Purpose : start DDV main window as a slave module: init 'dmp' data structure
1307                         (if needed), create the DDV panel, prepare a display from 'sap'.
1308   
1309   Parameters :  sap; pointer to a SeqAlign
1310                                 input_entityID,input_itemID; identity of the SeqAlign
1311                                 dmp; main data of DDV. May be NULL
1312                                 hParent; parent window for DDV. Must not be NULL
1313                                 bSetAppProp; if TRUE, use SetAppProperty().
1314                                 bInitGraph; if TRUE, initialize the graphical data for DDV
1315                                 bEditor; if TRUE, start the editor mode. Otherwise, a simple
1316                                                 viewer will ba started
1317                 rcPp; the RecT that defines the size of the panel.  can be NULL.
1318                                                 
1319   Note :  This function assumes that DDV will be placed inside an existing 
1320      parent window provided by the host application.
1321          
1322   Return value : handle to the DDV panel
1323 
1324 *******************************************************************************/
1325 static Boolean DDV_StartPanel_Slave(DdvMainPtr dmp, SeqAlignPtr sap,
1326                 Uint2* pInput_entityID, Uint4* pInput_itemID, Boolean bEditor)
1327 {
1328 DDV_ColorGlobal * dcgp;
1329 MsaParaGPopListPtr  mpplp;
1330 BaR  hsb;
1331 Int4  HPos, Col, IsEditable;
1332 DdvMainWinPtr  mWin_d;
1333 Boolean*   pNoGaps;
1334 Boolean    NoGaps;
1335 char       Str[1024];
1336 MsgAnswer  Answer;
1337 Boolean    Attached = FALSE;
1338   
1339   WatchCursor();
1340 
1341   if (!dmp->bEditor) {
1342     mWin_d = (DdvMainWinPtr) GetObjectExtra(dmp->hParent);
1343     Enable(mWin_d->MainMenu.LaunchEditor);
1344     IsEditable = AlnMgrIsEditable(sap);
1345     if (!IsEditable) {
1346       if (AlnMgrIsIBMable(sap)) {
1347         Str[0] = 0;
1348         StringCat(Str, "This file is a multiple pairwise alignment.\n");
1349         StringCat(Str, "To edit this file, it needs to be converted\n");
1350         StringCat(Str, "to a true multiple alignment.\n");
1351         StringCat(Str, "Should it be converted?");
1352         mWin_d->Show_logo=TRUE;
1353         Answer = Message(MSG_YN, "%s", Str);
1354         mWin_d->Show_logo=FALSE;
1355         if (Answer == ANS_YES) {
1356           if (ViewMgr_Attach(sap, TRUE, TRUE, *pInput_entityID, *pInput_itemID) < 1)
1357             return (FALSE);
1358           Attached = TRUE;
1359         }
1360         else {
1361           Disable(mWin_d->MainMenu.LaunchEditor);
1362         }
1363       }
1364     }
1365     else if (IsEditable == AM_EDITGAPS) {
1366       pNoGaps = (Boolean*) GetAppProperty("dde_nogaps");
1367       if (pNoGaps == NULL) NoGaps = TRUE;
1368       else NoGaps = *pNoGaps;
1369       if (NoGaps) {
1370         Str[0] = 0;
1371         StringCat(Str, "This alignment can't be edited\n");
1372         StringCat(Str, "because it has aligned gaps\n");
1373         StringCat(Str, "and DDV is running in NoGaps mode");
1374         Message(MSG_OK, "%s", Str);
1375         Disable(mWin_d->MainMenu.LaunchEditor);
1376       }
1377     }
1378   }
1379 
1380   if (!Attached) {
1381     if (ViewMgr_Attach(sap, FALSE, FALSE, *pInput_entityID, *pInput_itemID) < 1)
1382       return (FALSE);
1383   }
1384         
1385         /*if we have a NULL SeqAlign, switch the display style to show
1386         the sequences, not a spacer*/
1387         if (AlnMgrIsSAPNULL(sap)){
1388                 dmp->ddo.DispDiscStyle=MSA_TXT_STYLE_2;
1389                 dmp->ddo.SpacerSize=SPACER_TXT_BLANK;
1390         dmp->ddo.DiscJustification=DISP_JUST_LEFT;
1391         }
1392 
1393   if (bEditor) {
1394     dmp->dsp = (DDE_StackPtr) GetAppProperty("ddeinterndata");
1395     if (dmp->dsp == NULL) {return(FALSE);}
1396     mpplp = dmp->dsp->pEdit->pPopList;
1397     /* copy paragraphs from DDE_Stack into the ones that are displayed */
1398     MemCopy(&(dmp->MSA_d.pgp_l),mpplp,sizeof(MsaParaGPopList));
1399   }
1400   else {
1401           if (!DDV_CreateDisplayFromIndex(sap,&(dmp->MSA_d.pgp_l),ParaG_Size,
1402                   &(dmp->ddo))) return(FALSE);
1403           dmp->MSA_d.pgp_l.entitiesTbl=
1404                   DDV_BuildBspEntitiesTbl(dmp->MSA_d.pgp_l.TableHead,
1405                   dmp->MSA_d.pgp_l.nBsp);
1406           if (!dmp->MSA_d.pgp_l.entitiesTbl) return(FALSE);
1407           /*build the Master Ruler descriptor*/
1408 /*        dmp->MSA_d.pgp_l.RulerDescr=DDV_ComputeRuler(sap,&(dmp->ddo));  */
1409     dmp->MSA_d.pgp_l.RulerDescr = DDE_ReMakeRuler(&(dmp->MSA_d.pgp_l), FALSE, 0);
1410   }
1411 
1412         dmp->MSA_d.entityID=*pInput_entityID;
1413         dmp->MSA_d.itemID=*pInput_itemID;
1414 
1415 /*
1416 #if defined(_DEBUG_DDE)
1417 {
1418   MsaParaGPopListPtr  mpplp;
1419   mpplp = DDV_CreateDataForEditor(&(dmp->MSA_d.pgp_l), 0, dmp->MSA_d.pgp_l.LengthAli-1);
1420         MemCopy(&(dmp->MSA_d.pgp_l),mpplp,sizeof(MsaParaGPopList));
1421   dmp->dsp = DDE_NewStack(mpplp);
1422 }
1423 #endif
1424 */
1425 
1426         /*get the color stuff from the sap object. Usually I can do that
1427         when DDV is loaded from Cn3D*/
1428         dcgp=DDV_GetColorGlobal((Pointer) sap);
1429 
1430         /*init the colours*/
1431         if (dcgp==NULL){
1432                 if (!DDV_InitColour_When_Start(dmp->MSA_d.pgp_l.sap,&(dmp->MSA_d.pgp_l),
1433                         &(dmp->Globals.colorp),FALSE)){
1434                         dmp->ddo.bUseColors=FALSE;
1435                 }
1436         }
1437     else dmp->Globals.colorp = dcgp;        
1438 
1439         /*set the initial position of the caret (edit mode)*/
1440         dmp->dci.old_row=1;
1441         dmp->dci.new_row=1;
1442         dmp->dci.old_col=0;
1443         dmp->dci.new_col=0;
1444         
1445 
1446         /*relation between align size and display type; compute nTotLines for example*/
1447         DDV_WhatSize(dmp);
1448 
1449         ArrowCursor();
1450         DDV_Resize_DDV(dmp->hWndDDV,TRUE); /*DDV_SetupWin (dmp->hWndDDV,TRUE,NULL); */
1451         /*Update();*/
1452 
1453   CaptureSlateFocus((SlatE)dmp->hWndDDV);
1454 
1455   /* scroll display horizontally so aligned region is shown at position 5 */
1456   if (DDE_GetNumBlocks2(&(dmp->MSA_d.pgp_l))) {
1457     hsb = GetSlateHScrollBar((SlatE) dmp->hWndDDV);
1458     HPos = GetBarValue(hsb);
1459     if (bEditor) {
1460       Col = DDE_GetAlignStart(dmp->dsp->pEdit, 0);
1461     }
1462     else {
1463       Col = 0;
1464       if (dmp->ddo.ShowLeftTail) {
1465         Col = AlnMgrGetMaxTailLength(sap, LEFT_TAIL);
1466       }
1467     }
1468     SetValue(hsb, HPos+Col-5);
1469   }
1470 
1471   /* only enable left/right/center justify for an unaligned region in the editor */
1472   if (bEditor) {
1473     mWin_d = (DdvMainWinPtr) GetObjectExtra(dmp->hParent);
1474     if (dmp->dsp->IsUnAligned) {
1475       Enable(mWin_d->MainMenu.Justify);
1476     }
1477     else {
1478       Disable(mWin_d->MainMenu.Justify);
1479     }
1480   }
1481 
1482         return(TRUE);
1483 }
1484 
1485 
1486 /*******************************************************************************
1487 
1488   Function : DDV_ObjRegMasterDDV()
1489   
1490   Purpose : call by ObjMgr to start DDV panel
1491   
1492   Parameters : see Toolkit
1493   
1494   Note : this callback is only used by the standalone DDV. Never use this
1495          callback for other purpose.
1496   
1497   Return value : see Toolkit 
1498 
1499 *******************************************************************************/
1500 static Int2 DDV_ObjRegMasterDDV (Pointer data,Boolean bEditor)
1501 {
1502 OMProcControlPtr    ompcp;
1503 SeqAlignPtr                     sap=NULL;
1504 OMUserDataPtr       omudp;
1505 DdvMainPtr          dmp;
1506 
1507         /*retrieve data*/
1508         ompcp = (OMProcControlPtr) data;
1509 
1510         if (ompcp == NULL || ompcp->proc == NULL) {
1511                 ErrPostEx (SEV_ERROR, 0, 0, "Data NULL [1]");
1512                 return OM_MSG_RET_ERROR;
1513         }
1514         /*only accept a SeqALign*/
1515         switch (ompcp->input_itemtype) {
1516                 case OBJ_SEQALIGN :
1517                         sap = (SeqAlignPtr) ompcp->input_data;
1518                         break;
1519                 default :
1520                         return OM_MSG_RET_ERROR;
1521         }
1522         /*oups... nothing to deal with*/
1523         if (sap == NULL) {
1524                 ErrPostEx (SEV_ERROR, 0, 0, "Data NULL [2]");
1525                 return OM_MSG_RET_ERROR;
1526         }
1527         
1528         dmp = (DdvMainPtr) GetAppProperty("ddvinterndata");
1529         if (!dmp) goto error;
1530         if (!DDV_StartPanel_Slave(dmp, sap, &ompcp->input_entityID,
1531                 &ompcp->input_itemID, bEditor)) goto error;
1532         
1533         
1534         /*attach a Msg Func on the current seqalign*/
1535         dmp->userkey=OMGetNextUserKey();
1536         dmp->procid=ompcp->proc->procid;
1537         dmp->proctype=ompcp->proc->proctype;
1538     omudp = ObjMgrAddUserData (ompcp->input_entityID, ompcp->proc->procid, 
1539                 (bEditor==TRUE ? OMPROC_EDIT : OMPROC_VIEW),dmp->userkey);
1540     if (omudp == NULL) 
1541                 goto error;
1542     omudp->messagefunc =  DDV_OM_MsgFunc;
1543         omudp->userdata.ptrvalue = (Pointer)dmp;
1544 
1545         /*attach a Msg Func on each Bioseq of the current seqalign*/
1546         DDV_RegMsgFuncForBsp(dmp->MSA_d.pgp_l.TableHead,dmp,dmp->MSA_d.pgp_l.nBsp, 
1547                 ompcp->input_entityID, dmp->procid,dmp->proctype,dmp->userkey);
1548 
1549         DDV_EnableGotoTBItems(dmp->hParent,TRUE);
1550         return(OM_MSG_RET_DONE);
1551 
1552 error:
1553         /*show the logo window*/
1554         /*mWin_d->Show_logo=TRUE;*/
1555         Update ();
1556         ArrowCursor();
1557         return(OM_MSG_RET_ERROR);
1558 }
1559 
1560 /*******************************************************************************
1561 
1562   Function : DDV_ObjRegMasterEditDDV()
1563   
1564   Purpose : call by ObjMgr to start DDV as a standalone editor
1565   
1566   Parameters : see Toolkit
1567   
1568   Return value : see Toolkit 
1569 
1570 *******************************************************************************/
1571 extern Int2 LIBCALLBACK DDV_ObjRegMasterEditDDV (Pointer data)
1572 {
1573         return(DDV_ObjRegMasterDDV(data,TRUE));
1574 }
1575 
1576 /*******************************************************************************
1577 
1578   Function : DDV_ObjRegMasterViewDDV()
1579   
1580   Purpose : call by ObjMgr to start DDV as a standalone viewer
1581   
1582   Parameters : see Toolkit
1583   
1584   Return value : see Toolkit 
1585 
1586 *******************************************************************************/
1587 extern Int2 LIBCALLBACK DDV_ObjRegMasterViewDDV (Pointer data)
1588 {
1589         return(DDV_ObjRegMasterDDV(data,FALSE));
1590 }
1591 
1592 
1593 /*******************************************************************************
1594 
1595   Function : DDV_ObjRegSlaveDDV()
1596   
1597   Purpose : call by DDV_ObjRegSlaveEditDDV() & DDV_ObjRegSlaveViewDDV(), below
1598   
1599 *******************************************************************************/
1600 static Int2 DDV_ObjRegSlaveDDV(Pointer data,Boolean bEditor)
1601 {
1602 OMProcControlPtr    ompcp;
1603 SeqAlignPtr                     sap=NULL;
1604 WindoW              hParent=NULL;
1605 DdvMainWinPtr           mWin_d;
1606 OMUserDataPtr       omudp = NULL;
1607 DdvMainPtr          dmp;
1608 SAM_ViewGlobal      *vgp;
1609 
1610 
1611         /*retrieve data*/
1612         ompcp = (OMProcControlPtr) data;
1613     if (ompcp == NULL || ompcp->proc == NULL) {
1614                 ErrPostEx (SEV_ERROR, 0, 0, "Data NULL [1]");
1615                 return OM_MSG_RET_ERROR;
1616         }
1617 
1618         /*only accept a SeqALign*/
1619         switch (ompcp->input_itemtype) {
1620                 case OBJ_SEQALIGN :
1621                         sap = (SeqAlignPtr) ompcp->input_data;
1622                         break;
1623                 default :
1624                         return OM_MSG_RET_ERROR;
1625         }
1626 
1627         /*oups... nothing to deal with*/
1628         if (sap == NULL) {
1629                 ErrPostEx (SEV_ERROR, 0, 0, "Data NULL [2]");
1630                 return OM_MSG_RET_ERROR;
1631         }
1632         
1633         /*start the slave module*/
1634     /* get parameters from launching program */
1635     vgp = GetAppProperty(SAM_ViewString);
1636     if(vgp == NULL) hParent=DDV_StartMainWin_Slave(NULL,bEditor);
1637     else hParent=DDV_StartMainWin_Slave(vgp,bEditor);
1638 
1639         if (hParent){
1640                 RealizeWindow(hParent);
1641         Show(hParent);
1642         mWin_d=(DdvMainWinPtr)GetObjectExtra(hParent);
1643         dmp = (DdvMainPtr) GetObjectExtra(mWin_d->hWndDDV);
1644         if(vgp != NULL) dmp->MasterViewer = vgp->MasterViewer;
1645 
1646         if(dmp->MasterViewer == SAMVIEWCN3D) {
1647             mWin_d->UseNetwork = TRUE;
1648             mWin_d->NetStartProc = vgp->NetStartProc;
1649         }
1650         
1651         if (!DDV_StartPanel_Slave(dmp, sap, &ompcp->input_entityID,
1652             &ompcp->input_itemID, bEditor)) 
1653             goto error;
1654         
1655                 /*when the user will close DDV, DDV won't delete any data excepts
1656                 its internal data structure*/
1657                 dmp->dod.choice=DDV_OPENTYPE_NOTRESP;
1658                 
1659                 /*attach a Msg Func on the current seqalign*/
1660                 dmp->userkey=OMGetNextUserKey();
1661                 dmp->procid=ompcp->proc->procid;
1662                 dmp->proctype=ompcp->proc->proctype;
1663         omudp = ObjMgrAddUserData (ompcp->input_entityID, ompcp->proc->procid, 
1664                         (bEditor==TRUE ? OMPROC_EDIT : OMPROC_VIEW),dmp->userkey);
1665         if (omudp == NULL) 
1666                         goto error;
1667         omudp->messagefunc =  DDV_OM_MsgFunc;
1668                 omudp->userdata.ptrvalue = (Pointer)dmp;
1669                 /*attach a Msg Func on each Bioseq of the current seqalign*/
1670                 DDV_RegMsgFuncForBsp(dmp->MSA_d.pgp_l.TableHead,dmp,dmp->MSA_d.pgp_l.nBsp, 
1671                         ompcp->input_entityID, dmp->procid,dmp->proctype,dmp->userkey);
1672                 DDV_EnableGotoTBItems(hParent,TRUE);
1673         } 
1674         else {
1675                 goto error;
1676         }
1677         
1678         return(OM_MSG_RET_DONE);
1679 
1680 error:
1681         if (hParent) Remove(hParent);
1682         return(OM_MSG_RET_ERROR);
1683 }
1684 
1685 /*******************************************************************************
1686 
1687   Function : DDV_ObjRegSlaveEditDDV()
1688   
1689   Purpose : call by ObjMgr to start DDV as a slave, editor mode on a SeqAlign
1690   
1691   Parameters : see Toolkit
1692   
1693   Return value : see Toolkit 
1694 
1695 *******************************************************************************/
1696 extern Int2 LIBCALLBACK DDV_ObjRegSlaveEditDDV (Pointer data)
1697 {
1698         return(DDV_ObjRegSlaveDDV(data,TRUE));
1699 }
1700 
1701 /*******************************************************************************
1702 
1703   Function : DDV_ObjRegSlaveViewDDV()
1704   
1705   Purpose : call by ObjMgr to start DDV as a slave, editor mode on a SeqAlign
1706   
1707   Parameters : see Toolkit
1708   
1709   Return value : see Toolkit 
1710 
1711 *******************************************************************************/
1712 extern Int2 LIBCALLBACK DDV_ObjRegSlaveViewDDV (Pointer data)
1713 {
1714         return(DDV_ObjRegSlaveDDV(data,FALSE));
1715 }
1716 
1717 /*******************************************************************************
1718 
1719   Function : DDV_GetAndCheckSeqAlign()
1720   
1721   Purpose : given a file OR a GI OR a SeqEntry , retrieve SeqAlign(s) 
1722   
1723   Parameters :  fp; a handle of an open file
1724                                 gi; a GI numger
1725                                 sep2; a SeqEntry
1726                                 fetchSepProc; fetch procedure of the user (only if GI!=0)
1727                                 
1728   Return value : a list of nodes. For each node, data.intvalue is an entityID
1729            of a SeqAlign 
1730 
1731 *******************************************************************************/
1732 extern ValNodePtr DDV_GetAndCheckSeqAlign(FILE *fp,Int4 gi,SeqEntryPtr sep2,
1733         UdvFetchSeqEntryProc fetchSepProc,DdvOpenDataPtr dodp,Uint2Ptr entityID)
1734 {
1735 SeqEntryPtr sep=NULL;
1736 ValNodePtr  head = NULL,vnp_ali=NULL;
1737 Pointer     dataptr=NULL;
1738 Uint2       datatype=0;
1739 Uint2           nRet=DVV_MSG_M_OK;
1740 
1741         *entityID=0;
1742         if (fp!=NULL){/*open a file*/
1743                 /*Read the file (it can contain several objects)*/
1744                 while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, 
1745                         FALSE, TRUE, FALSE)) != NULL) {
1746                                 ValNodeAddPointer (&head, datatype, dataptr);
1747                 }
1748                 /*scan the first node of the head list*/
1749                 datatype = head->choice;
1750                 dataptr = head->data.ptrvalue;
1751         *entityID=ObjMgrRegister(datatype,dataptr);
1752                 AssignIDsInEntity (*entityID,0,NULL);
1753                 switch(datatype){
1754                         case OBJ_SEQENTRY:
1755                         case OBJ_SEQALIGN:
1756                         case OBJ_SEQANNOT:
1757                         case OBJ_BIOSEQ:
1758                         case OBJ_BIOSEQSET:
1759                                 DDV_GetSeqAlign(dataptr,datatype,&vnp_ali);
1760                                 break;
1761                         default:
1762                                 break;
1763                 }
1764                 if (dodp){
1765                         dodp->choice=DDV_OPENTYPE_FILE;
1766                         dodp->vnp=head;
1767                 }
1768         } else if (gi!=0){/*retrieve a Network Entry given a GI*/
1769                 if (fetchSepProc) {/*fetch the SeqEntry*/
1770                         sep=fetchSepProc(gi, 0);
1771                         if (!sep) {
1772                                 nRet=DVV_MSG_O_E_READGI;
1773                                 goto error;
1774                         }
1775                         *entityID=ObjMgrGetEntityIDForPointer((Pointer)sep);
1776                         AssignIDsInEntity (*entityID,0,NULL);
1777                         SeqEntryExplore(sep,(Pointer)&vnp_ali,DDV_SearchAli);
1778                         if (dodp){
1779                                 dodp->choice=DDV_OPENTYPE_GI;
1780                                 dodp->sep=sep;
1781                         }
1782                 } else {
1783                         nRet=DVV_MSG_O_E_NOFETCHFUNC;
1784                         goto error;
1785                 }
1786         } else if (sep2!=NULL){/*analyse a SeqEntry*/
1787                 *entityID=ObjMgrGetEntityIDForPointer((Pointer)sep2);
1788                 AssignIDsInEntity (*entityID,0,NULL);
1789                 SeqEntryExplore(sep2,(Pointer)&vnp_ali,DDV_SearchAli);
1790                 if (dodp){
1791                         dodp->choice=DDV_OPENTYPE_SEP;
1792                         dodp->sep=sep2;
1793                 }
1794         } else {
1795                 nRet=DVV_MSG_O_E_NOTHINGTODO;
1796                 goto error;
1797         }
1798         
1799         /*register for ObjMgr*/
1800         if (!vnp_ali) {
1801                 nRet=DVV_MSG_O_E_BADTYPE;
1802                 goto error;
1803         }
1804 
1805         
1806         return(vnp_ali);
1807 
1808 error:
1809         /*to add : Msg Handler with nRet*/
1810         return(NULL);
1811 }
1812 
1813 /*******************************************************************************
1814 
1815   Function : DDV_FOpenTextProc()
1816   
1817   Purpose : manage File name edit control of the FileOpen dialog box 
1818   
1819   Parameters : t; edit control
1820   
1821   Return value : none 
1822 
1823 *******************************************************************************/
1824 static void DDV_FOpenTextProc(TexT t)
1825 {
1826 Char                            szFName[PATH_MAX]={""};
1827 WindoW                          hOpenDlg;
1828 DlgFileOpenDataPtr  dfodp;
1829 
1830         hOpenDlg=(WindoW)ParentWindow(t);
1831 
1832         if (!hOpenDlg) return;
1833         
1834         dfodp = (DlgFileOpenDataPtr) GetObjectExtra (hOpenDlg);
1835 
1836         if (dfodp==NULL) return;
1837         
1838         GetTitle(t, szFName, sizeof(szFName)-1);
1839 
1840         if (StringLen(szFName) == 0)
1841                 Disable(dfodp->Ok);
1842         else Enable(dfodp->Ok);
1843 
1844         return;
1845 }
1846 
1847 /*******************************************************************************
1848 
1849   Function : DDV_FOpenBrowseProc()
1850   
1851   Purpose : manage browse button of the FileOpen dialog box 
1852   
1853   Parameters : g; button
1854   
1855   Return value : none 
1856 
1857 *******************************************************************************/
1858 static void DDV_FOpenBrowseProc(ButtoN g)
1859 {
1860 DlgFileOpenDataPtr  dfodp;
1861 WindoW                          hOpenDlg;
1862 Char                            path[PATH_MAX]={""};
1863 
1864         hOpenDlg=(WindoW)ParentWindow(g);
1865 
1866         if (!hOpenDlg) return;
1867 
1868         dfodp = (DlgFileOpenDataPtr) GetObjectExtra (hOpenDlg);
1869 
1870         if (dfodp==NULL) return;
1871         if (!dfodp->FNameEditCtrl) return;
1872 
1873         if (GetInputFileName (path, sizeof(path), NULL, NULL)){ 
1874                 SetTitle(dfodp->FNameEditCtrl, path);
1875                 DDV_FOpenTextProc(dfodp->FNameEditCtrl);
1876         }
1877 
1878         return;   
1879 }
1880 
1881 /*******************************************************************************
1882 
1883   Function : DDV_FOpenCancelProc()
1884   
1885   Purpose : manage cancel button of the FileOpen dialog box 
1886   
1887   Parameters : g; button
1888   
1889   Return value : none 
1890 
1891 *******************************************************************************/
1892 static void DDV_FOpenCancelProc(ButtoN g)
1893 {
1894 WindoW                          hOpenDlg;
1895 DlgFileOpenDataPtr  dfodp;
1896 DdvMainWinPtr           mWin_d;
1897 
1898         hOpenDlg=(WindoW)ParentWindow(g);
1899 
1900         if (!hOpenDlg) return;
1901         dfodp = (DlgFileOpenDataPtr) GetObjectExtra (hOpenDlg);
1902         
1903         if (dfodp){
1904                 mWin_d = (DdvMainWinPtr) GetObjectExtra (dfodp->parent);
1905                 if (mWin_d) Enable(mWin_d->MainMenu.FileOpen);
1906         }
1907         
1908         Remove(hOpenDlg);
1909 }
1910 
1911 /*******************************************************************************
1912 
1913   Function : DDV_FOpenAcceptProc()
1914   
1915   Purpose : manage ok button of the FileOpen dialog box 
1916   
1917   Parameters : g; button
1918   
1919   Return value : none 
1920 
1921 *******************************************************************************/
1922 static void DDV_FOpenAcceptProc(ButtoN g)
1923 {
1924 WindoW                          hOpenDlg;
1925 DlgFileOpenDataPtr  dfodp;
1926 DdvMainPtr          dmp;
1927 DdvMainWinPtr           mWin_d;
1928 Char                            szFName[PATH_MAX]={""};
1929 Boolean                         isBinary;
1930 FILE                            *fp;
1931 ValNodePtr                      vnp_ali=NULL;
1932 Uint2                           nRet=DVV_MSG_M_OK,The_entityID;
1933 Int2                            procval;
1934 
1935         hOpenDlg=(WindoW)ParentWindow(g);
1936 
1937         if (!hOpenDlg) return;
1938         dfodp = (DlgFileOpenDataPtr) GetObjectExtra (hOpenDlg);
1939         
1940         if (dfodp){
1941 
1942                 mWin_d = (DdvMainWinPtr) GetObjectExtra (dfodp->parent);
1943                 if (mWin_d!=NULL){
1944                         /*try to connect Entrez; if failure, still ok : the user can
1945                         use DDV with a local file. Otherwise he/she will be in trouble ;-)*/
1946                         if (mWin_d->AutonomeViewer && mWin_d->NetStartProc){
1947                                 mWin_d->NetStartProc(mWin_d->UseNetwork);
1948                         }
1949 
1950                         Enable(mWin_d->MainMenu.FileOpen);
1951                         /*file name*/
1952                         GetTitle(dfodp->FNameEditCtrl, szFName, sizeof(szFName));
1953 
1954                         /*file reading mode*/
1955                         if (GetValue(dfodp->ReadMode)==2) isBinary=TRUE;
1956                         else isBinary=FALSE;
1957         
1958                         Remove(hOpenDlg);
1959 
1960                         /* open the i/o files in the correct mode */
1961                         if ((fp = FileOpen (szFName, isBinary?"rb":"r")) == NULL){
1962                                 nRet=DVV_MSG_O_E_OPENFILEFAIL;
1963                                 goto error;
1964                         }
1965                         /*restrieve a list of registered seqalign(s)*/
1966                         WatchCursor();
1967                         /*delete old data, if needed*/
1968                         dmp=(DdvMainPtr)GetObjectExtra(mWin_d->hWndDDV);
1969                         DDV_CleanupDDVPdata_g(dmp);
1970                         DDV_CloseData(mWin_d,FALSE);
1971                         
1972                         vnp_ali=DDV_GetAndCheckSeqAlign(fp,0,NULL,mWin_d->fetchSepProc,&(dmp->dod),
1973                                 &The_entityID);
1974                         ArrowCursor();
1975                         if (vnp_ali && The_entityID!=0){
1976                                 Uint2 entityID;
1977                                 Uint4 itemID;
1978                                 SeqAlignPtr sap;
1979                                                                 
1980                                 /*get the first SeqAlign in the list and load the viewer panel*/
1981                                 dmp->vnp_ali=vnp_ali;
1982                                 
1983                                 if(mWin_d->Show_logo){
1984                                         mWin_d->Show_logo=FALSE;
1985                                 }
1986                                 sap=(SeqAlignPtr)vnp_ali->data.ptrvalue;
1987                                 entityID=sap->idx.entityID;
1988                                 itemID=sap->idx.itemID;
1989                                 SetAppProperty("ddvinterndata",(void *)dmp);
1990                                 procval=GatherProcLaunch ((dmp->bEditor==TRUE? OMPROC_EDIT : OMPROC_VIEW), FALSE, 
1991                                         entityID, itemID, OBJ_SEQALIGN, 0, 0, OBJ_SEQALIGN, 0);
1992                                 if ((procval = OM_MSG_RET_ERROR))
1993                                         mWin_d->Show_logo=TRUE;
1994                                 RemoveAppProperty("ddvinterndata");
1995                         }
1996                         FileClose(fp);
1997                 }
1998         }
1999         
2000 error:  
2001         return;
2002 }
2003 
2004 /*******************************************************************************
2005 
2006   Function : DDV_OpenFile()
2007   
2008   Purpose : callback of the File|Open command 
2009   
2010   Parameters : i; command item
2011   
2012   Return value : none 
2013 
2014 *******************************************************************************/
2015 extern void DDV_OpenFile(IteM i)
2016 {
2017 DlgFileOpenDataPtr  dfodp;
2018 DdvMainWinPtr           mWin_d;
2019 WindoW                          hWinMain,hOpenDlg;
2020 GrouP                           g,g4;
2021 ButtoN                          b1;
2022 TexT                            t1;    
2023 
2024         hWinMain=(WindoW)ParentWindow(i);
2025 
2026         if (!hWinMain) return;
2027         
2028         mWin_d = (DdvMainWinPtr) GetObjectExtra (hWinMain);
2029 
2030         if (mWin_d==NULL) return;
2031                 
2032         dfodp=(DlgFileOpenDataPtr)MemNew(sizeof(DlgFileOpenData));
2033         if (!dfodp) return;
2034         MemSet(dfodp,0,sizeof(DlgFileOpenData));
2035 
2036     hOpenDlg = MovableModalWindow(-30, -20,  -10,  -10, 
2037                                 "DDV - Open a local file",  NULL);
2038     g = NormalGroup(hOpenDlg, 2, 1, "File name:",  systemFont, NULL);
2039     SetGroupMargins(g, 10, 10);
2040     SetGroupSpacing(g, 10, 20);  
2041     t1 = DialogText(g,"",25, DDV_FOpenTextProc);
2042     PushButton(g, " browse...", DDV_FOpenBrowseProc);
2043    
2044     g = HiddenGroup(hOpenDlg, 3, 1, NULL);
2045     SetGroupMargins(g, 10, 10);
2046     SetGroupSpacing(g, 30, 30);
2047  
2048     g4 = NormalGroup(g, 2, 1, "File type", systemFont,  NULL);
2049     SetGroupMargins(g4, 10, 10);
2050     RadioButton(g4, "Ascii");
2051     RadioButton(g4, "Binary");
2052     SetValue(g4, 1);
2053 
2054     b1 = DefaultButton(g, "OK",  DDV_FOpenAcceptProc);
2055     PushButton(g, "Cancel",  DDV_FOpenCancelProc);
2056    
2057     Disable(b1);
2058 
2059         dfodp->parent=hWinMain; 
2060         dfodp->FNameEditCtrl=t1;
2061         dfodp->Ok=b1;
2062         dfodp->ReadMode=g4;
2063 
2064         SetObjectExtra (hOpenDlg, (Pointer) dfodp, StdCleanupExtraProc);
2065         
2066     Select(hOpenDlg);
2067     Show(hOpenDlg);
2068 
2069         /*allow only one FOpen dlg at a time*/
2070     /*UDV_set_PullMenus(&vmp->MainMenu,FALSE);*/
2071 }
2072 
2073 /*******************************************************************************
2074 
2075   Function : DDV_NetOpen_cancelProc()
2076   
2077   Purpose : manage ok button of the Network open dialog box 
2078   
2079   Parameters : g; button
2080   
2081   Return value : none 
2082 
2083 *******************************************************************************/
2084 static void DDV_NetOpen_cancelProc(ButtoN g)
2085 {
2086 WindoW                  hOpenDlg;
2087 DDVNetOpenPtr   dnop;
2088 
2089         hOpenDlg=(WindoW)ParentWindow(g);
2090 
2091         if (!hOpenDlg) return;
2092         
2093         dnop = (DDVNetOpenPtr) GetObjectExtra (hOpenDlg);
2094 
2095         if (dnop==NULL) return;
2096 
2097         Remove(hOpenDlg);
2098 }
2099 
2100 /*******************************************************************************
2101 
2102   Function : DDV_IsInt()
2103   
2104   Purpose : test if an entry access code is a GI number, i.e. an Int
2105   
2106   Parameters : t; edit control
2107   
2108   Return value : none 
2109 
2110 *******************************************************************************/
2111 static Boolean DDV_IsInt(CharPtr szAccess)
2112 {
2113 Int2 i=0;
2114 
2115         while(szAccess[i]){
2116                 if (!IS_DIGIT(szAccess[i])){
2117                         Message (MSG_OK, "Not a GI number.");
2118                         return(FALSE);
2119                 }
2120                 i++;
2121         }
2122         return(TRUE);
2123 }
2124 
2125 /*******************************************************************************
2126 
2127   Function : DDV_NetOpen_okProc()
2128   
2129   Purpose : manage ok button of the Network open dialog box 
2130   
2131   Parameters : g; button
2132   
2133   Return value : none 
2134 
2135 *******************************************************************************/
2136 static void DDV_NetOpen_okProc(ButtoN g)
2137 {
2138 WindoW                  hOpenDlg;
2139 DDVNetOpenPtr   dnop;
2140 Char                    szAccess[50]={""};
2141 Int2                    AccessType,procval;
2142 Int4                    uid=0;
2143 DdvMainWinPtr   mWin_d;
2144 ValNodePtr      vnp_ali;
2145 MonitorPtr      mon;
2146 DdvMainPtr      dmp;
2147 Uint2           The_entityID;
2148 
2149         hOpenDlg=(WindoW)ParentWindow(g);
2150 
2151         if (!hOpenDlg) return;
2152         
2153         dnop = (DDVNetOpenPtr) GetObjectExtra (hOpenDlg);       
2154 
2155         if (dnop==NULL) return;
2156 
2157         mWin_d = (DdvMainWinPtr) GetObjectExtra (dnop->hWndMain);
2158         if (mWin_d==NULL) return;
2159 
2160         dmp=(DdvMainPtr)GetObjectExtra(mWin_d->hWndDDV);        
2161         
2162         Hide(hOpenDlg);
2163         
2164         /*retrieve a Gi*/
2165         GetTitle(dnop->Entry, szAccess, sizeof(szAccess));
2166 
2167         if (StringHasNoText (szAccess)) {
2168                 Message (MSG_OK, "Please enter a GI number");
2169                 Show (hOpenDlg);
2170                 Select (hOpenDlg);
2171                 Select (dnop->Entry);
2172                 return;
2173         }
2174 
2175         AccessType=GetValue(dnop->AccessType);
2176         if (!StrToLong(szAccess,&uid)) 
2177                 uid=0;
2178         /*Connect ID1 with a valid UID*/
2179         if (uid==0){
2180                 ArrowCursor();
2181                 Message (MSG_OK, "Unable to find your record in the database.");
2182                 Show (hOpenDlg);
2183                 Select (hOpenDlg);
2184                 Select (dnop->Entry);
2185                 return;
2186         }
2187         else{
2188                 /*delete old data, if needed*/
2189                 DDV_CleanupDDVPdata_g(dmp);
2190                 DDV_CloseData(mWin_d,FALSE);
2191                 
2192                 WatchCursor();
2193 
2194                 mon = MonitorStrNewEx (szAppName, 30, FALSE);
2195                 MonitorStrValue (mon, "Retrieving your record...");
2196                 Update ();
2197 
2198                 vnp_ali=DDV_GetAndCheckSeqAlign(NULL,uid,NULL,mWin_d->fetchSepProc,
2199                         &(dmp->dod),&The_entityID);
2200 
2201                 ArrowCursor();
2202 
2203                 MonitorFree (mon);
2204 
2205                 if (vnp_ali && The_entityID!=0){
2206                         Uint2 entityID;
2207                         Uint4 itemID;
2208                         SeqAlignPtr sap;
2209                         
2210                         /*get the first SeqAlign in the list and load the viewer panel*/
2211                         dmp->vnp_ali=vnp_ali;
2212 
2213                         if(mWin_d->Show_logo){
2214                                 /*Hide(mWin_d->Logo_Panel);*/
2215                                 mWin_d->Show_logo=FALSE;
2216                         }
2217                         sap=(SeqAlignPtr)vnp_ali->data.ptrvalue;
2218                         entityID=sap->idx.entityID;
2219                         itemID=sap->idx.itemID;
2220                         SetAppProperty("ddvinterndata",(void *)dmp);
2221                         procval=GatherProcLaunch ((dmp->bEditor==TRUE? OMPROC_EDIT : OMPROC_VIEW), FALSE, 
2222                                 entityID, itemID, OBJ_SEQALIGN, 0, 0, OBJ_SEQALIGN, 0);
2223                         if ((procval = OM_MSG_RET_ERROR))
2224                                 mWin_d->Show_logo=TRUE;
2225                         RemoveAppProperty("ddvinterndata");
2226                 }
2227         }
2228 
2229         Remove(hOpenDlg);
2230 }
2231 
2232 /*******************************************************************************
2233 
2234   Function : DDV_NetOpen_access()
2235   
2236   Purpose : manage Accession edit control of the Download dialog box 
2237   
2238   Parameters : t; edit control
2239   
2240   Return value : none 
2241 
2242 *******************************************************************************/
2243 static void DDV_NetOpen_access(TexT t)
2244 {
2245 Char                    szAccess[50]={""};
2246 WindoW                  hOpenDlg;
2247 DDVNetOpenPtr   dnop;
2248 
2249         hOpenDlg=(WindoW)ParentWindow(t);
2250 
2251         if (!hOpenDlg) return;
2252         
2253         dnop = (DDVNetOpenPtr) GetObjectExtra (hOpenDlg);
2254 
2255         if (dnop==NULL) return;
2256         
2257         GetTitle(t, szAccess, sizeof(szAccess));
2258 
2259         if (StringLen(szAccess) == 0)
2260                 Disable(dnop->ok);
2261         else Enable(dnop->ok);
2262 
2263         return;
2264 }
2265 
2266 /*******************************************************************************
2267 
2268   Function : DDV_OpenNetwork()
2269   
2270   Purpose : callback of the File|Network command 
2271   
2272   Parameters : i; command item
2273   
2274   Return value : none 
2275 
2276 *******************************************************************************/
2277 extern void DDV_OpenNetwork(IteM i)
2278 {
2279 DdvMainWinPtr           mWin_d;
2280 WindoW                          hWinMain,hOpenDlg;
2281 GrouP                           g,c;
2282 DDVNetOpenPtr       dnopp;
2283 
2284         hWinMain=(WindoW)ParentWindow(i);
2285 
2286         if (!hWinMain) return;
2287         
2288         mWin_d = (DdvMainWinPtr) GetObjectExtra (hWinMain);
2289 
2290         if (mWin_d==NULL) return;
2291                 
2292         dnopp=(DDVNetOpenPtr)MemNew(sizeof(DDVNetOpen));
2293         if (!dnopp) return;
2294 
2295         /*init the Network, if needed*/
2296         if (mWin_d->AutonomeViewer && mWin_d->NetStartProc){
2297                 if (!mWin_d->NetStartProc(mWin_d->UseNetwork)) return;
2298         }
2299 
2300     hOpenDlg = MovableModalWindow(-30, -20,  -10,  -10, 
2301                                 "DDV - Fetch a network entry",  NULL);
2302         SetGroupSpacing (hOpenDlg, 10, 10);
2303         
2304         dnopp->hWndMain=hWinMain;
2305         
2306         /*accesion*/
2307         g = NormalGroup (hOpenDlg, 3, 0, "Entry", systemFont,NULL);
2308 
2309 #ifdef WIN_MAC
2310         dnopp->AccessType = PopupList (g, TRUE, NULL);
2311 #endif
2312 
2313 #ifndef WIN_MAC
2314         dnopp->AccessType = PopupList (g, FALSE, NULL);
2315 #endif
2316 
2317         PopupItem(dnopp->AccessType,"PopSet");
2318         
2319         SetValue (dnopp->AccessType, 1);
2320 
2321         dnopp->Entry = DialogText (g, "", 10, DDV_NetOpen_access);
2322 
2323         c = HiddenGroup (hOpenDlg, 4, 0, NULL);
2324         SetGroupSpacing (c, 10, 2);
2325         dnopp->ok=DefaultButton (c, "Retrieve", DDV_NetOpen_okProc);
2326         Disable (dnopp->ok);
2327         PushButton (c, "Cancel", DDV_NetOpen_cancelProc);
2328         
2329         SetObjectExtra (hOpenDlg, (Pointer) dnopp, StdCleanupExtraProc);
2330 
2331         /*display*/
2332         AlignObjects (ALIGN_CENTER, (HANDLE) g, (HANDLE) c, NULL);
2333         RealizeWindow (hOpenDlg);
2334         Show (hOpenDlg);
2335         Select (hOpenDlg);
2336         Update ();
2337 }
2338 
2339 NLM_EXTERN void DDV_SlaveQuit(WindoW hWinMain)
2340 {
2341   DdvMainWinPtr  mWin_d;
2342   DdvMainPtr     dmp;
2343   WindoW                                 hDialog;
2344   GrouP          g1, g2;
2345   Boolean        QuitElseWhere = FALSE;
2346 
2347         mWin_d = (DdvMainWinPtr) GetObjectExtra((Handle)hWinMain);
2348         if (mWin_d==NULL) return;
2349 
2350   /* if there are unsaved edits, ask the user if they'd like to save */
2351   dmp = (DdvMainPtr)GetObjectExtra(mWin_d->hWndDDV);
2352   if (dmp->bEditor) {
2353     if (dmp->dsp->SomethingToSave) {
2354       QuitElseWhere = TRUE;
2355       hDialog = MovableModalWindow(-30, -20,  -10,  -10, "DDE - Save Query",  NULL);
2356       g1 = HiddenGroup(hDialog, 1, 1, NULL);
2357       StaticPrompt(g1, "Save Changes?", 0, stdLineHeight, systemFont, 'c');
2358       g2 = HiddenGroup(hDialog, 3, 1, NULL);
2359       PushButton(g2, "Yes", DDV_Save);
2360       PushButton(g2, "No",  DDV_NoSave);
2361       PushButton(g2, "Cancel", DDV_Cancel);
2362       SetObjectExtra (hDialog, (Pointer)hWinMain, NULL);
2363       Select(hDialog);
2364       Show(hDialog);
2365     }
2366   }
2367   if (!QuitElseWhere) {
2368     if (mWin_d->AutonomeViewer)
2369       QuitProgram(); /*standalone DDV*/
2370     else
2371       Remove(hWinMain);/*slave viewer : just kill the main window*/
2372   }
2373 }
2374 
2375 /*******************************************************************************
2376 
2377   Function : DDV_StartMainWin_Slave()
2378   
2379   Purpose : start DDV main window as a slave module: init 'dmp' data structure,
2380                         set 'DDV_MAIN_DATA' property, start a dlg (the parent window of DDV
2381                         panel).
2382   
2383   Parameters : vgp; sets defaults for non-autonomous viewers
2384                                                          
2385   Return value : handle to the DDV main window
2386 
2387 *******************************************************************************/
2388 extern WindoW DDV_StartMainWin_Slave(SAM_ViewGlobal *vgp,Boolean bEditor)
2389 {
2390 Int2    Margins;
2391 DdvMainWinPtr mWin_d;
2392 WindoW  w; 
2393 Boolean bRet;
2394 Char AppName[40];
2395 
2396   /* use "DDE" for editor, "DDV" for viewer, for title */
2397   if (bEditor) {StringCpy(AppName, szAppName2);}
2398   else         {StringCpy(AppName, szAppName);}
2399         
2400         /*init data blocks*/
2401         mWin_d=(DdvMainWinPtr)MemNew(sizeof(DdvMainWin));
2402         if (!mWin_d) return(NULL);
2403         
2404         /*main window*/
2405         Margins=15*stdCharWidth;
2406     if(vgp == NULL)
2407         w=DocumentWindow(Margins,Margins ,-10, -10,
2408            AppName, 
2409            DDV_SlaveQuit,
2410            DDV_WinMainResize);
2411     else
2412         w=DocumentWindow(vgp->Rect.left,vgp->Rect.top ,-10, -10,
2413             AppName, 
2414             DDV_SlaveQuit,
2415             DDV_WinMainResize);
2416 
2417         if (!w) return(NULL);
2418 
2419         SetObjectExtra (w, (Pointer) mWin_d, (FreeProc)DDV_WinMainCleanup);
2420         mWin_d->hWndMain=w;
2421         /*this is a slave editor/viewer*/
2422         mWin_d->AutonomeViewer=FALSE;
2423 
2424         /*build GUI*/
2425         bRet=DDV_CreateViewerPanel(w,mWin_d, vgp, bEditor);
2426 
2427         if (!bRet) return(NULL);
2428 
2429         mWin_d->Show_logo=FALSE;
2430 
2431         return(w);
2432 }
2433 
2434 
2435 static Boolean DDV_DigitsOnly(Char* str)
2436 {
2437     while(*str) {
2438         if (!isdigit(*str)) 
2439             return FALSE;
2440         str++;
2441     }
2442     return TRUE;
2443 }
2444 
2445 /*******************************************************************************
2446 
2447   Function : DDV_ImportEnableProc
2448   
2449   Purpose : this function does the actual bioseq import.  note that it doesn't
2450             require a vibrant dialog to operate, only that idp is filled out
2451   
2452   Return value : none 
2453 
2454 *******************************************************************************/
2455 
2456 static void DDV_ImportEnableProc(TexT t)
2457 {
2458     Char str[32];
2459     WindoW hImportDlg;
2460     DDV_ImportDialog *idp;
2461 
2462         hImportDlg= (WindoW)ParentWindow(t);
2463         if(hImportDlg == NULL) return;  
2464         idp = (DDV_ImportDialog *) GetObjectExtra(hImportDlg);
2465         if(idp == NULL) return;
2466 
2467     GetTitle(idp->DDV_tAccession, str, sizeof(str));
2468     if (StringLen(str) == 0) {
2469         Disable(idp->DDV_bImportAccept);
2470     } else {
2471         Enable(idp->DDV_bImportAccept);
2472         if(DDV_DigitsOnly(str)) SetValue(idp->DDV_gAccType, 2);
2473         else SetValue(idp->DDV_gAccType, 1);
2474     }
2475     return;
2476 }
2477 
2478 static SeqAlignPtr ddv_truncate_overlaps (SeqAlignPtr sap)
2479 {
2480    AMAlignIndexPtr  amaip;
2481    DenseDiagPtr     ddp;
2482    DenseDiagPtr     ddp_head;
2483    DenseDiagPtr     ddp_prev;
2484    DenseSegPtr      dsp1;
2485    DenseSegPtr      dsp2;
2486    Int4             i;
2487    SeqAlignPtr      salp;
2488    Int4             start1;
2489    Int4             start2;
2490    Int4             stop1;
2491    Int4             stop2;
2492    Int4             tmp;
2493 
2494    if (sap == NULL) return(NULL);
2495    if (sap->saip == NULL) return(NULL);
2496    if (sap->saip->indextype != INDEX_PARENT) return(NULL);
2497 
2498    amaip = (AMAlignIndexPtr)(sap->saip);
2499    for (i=0; i<amaip->numsaps-1; i++)
2500    {
2501       AlnMgrGetNthSeqRangeInSA(amaip->saps[i], 1, &start1, &stop1);
2502       AlnMgrGetNthSeqRangeInSA(amaip->saps[i+1], 1, &start2, &stop2);
2503       if (start2 <= stop1)
2504       {
2505          dsp1 = (DenseSegPtr)(amaip->saps[i]->segs);
2506          dsp2 = (DenseSegPtr)(amaip->saps[i+1]->segs);
2507          dsp1->lens[0] = dsp1->lens[0] - (stop1-start2)/2;
2508          tmp = dsp1->starts[0]+dsp1->lens[0];
2509          tmp = tmp - dsp2->starts[0];
2510          dsp2->starts[1] = tmp + dsp2->starts[1];
2511          dsp2->starts[0] = dsp1->starts[0]+dsp1->lens[0];
2512          dsp2->lens[0] = stop2 - dsp2->starts[0] + 1;
2513          SAIndexFree(amaip->saps[i]->saip);
2514          SAIndexFree(amaip->saps[i+1]->saip);
2515          amaip->saps[i]->saip = amaip->saps[i+1]->saip = NULL;
2516          AlnMgrIndexSingleChildSeqAlign(amaip->saps[i]);
2517          AlnMgrIndexSingleChildSeqAlign(amaip->saps[i+1]);
2518       }
2519       AlnMgrGetNthSeqRangeInSA(amaip->saps[i], 2, &start1, &stop1);
2520       AlnMgrGetNthSeqRangeInSA(amaip->saps[i+1], 2, &start2, &stop2);
2521       if (start2 <= stop1)
2522       {
2523          dsp1 = (DenseSegPtr)(amaip->saps[i]->segs);
2524          dsp2 = (DenseSegPtr)(amaip->saps[i+1]->segs);
2525          dsp1->lens[0] = dsp1->lens[0] - (stop1-start2)/2;
2526          tmp = dsp1->starts[1]+dsp1->lens[0];
2527          tmp = tmp - dsp2->starts[1];
2528          dsp2->starts[0] = tmp + dsp2->starts[0];
2529          dsp2->starts[1] = dsp1->starts[1]+dsp1->lens[0];
2530          dsp2->lens[0] = stop2 - dsp2->starts[1] + 1;
2531          SAIndexFree(amaip->saps[i]->saip);
2532          SAIndexFree(amaip->saps[i+1]->saip);
2533          amaip->saps[i]->saip = amaip->saps[i+1]->saip = NULL;
2534          AlnMgrIndexSingleChildSeqAlign(amaip->saps[i]);
2535          AlnMgrIndexSingleChildSeqAlign(amaip->saps[i+1]);
2536       }
2537    }
2538    ddp_head = ddp_prev = NULL;
2539    for (i=0; i<amaip->numsaps; i++)
2540    {
2541       ddp = DenseDiagNew();
2542       AlnMgrGetNthSeqRangeInSA(amaip->saps[i], 1, &start1, &stop1);
2543       AlnMgrGetNthSeqRangeInSA(amaip->saps[i], 2, &start2, &stop2);
2544       dsp1 = (DenseSegPtr)(amaip->saps[i]->segs);
2545       ddp->starts = (Int4Ptr)MemNew(2*sizeof(Int4));
2546       ddp->dim = 2;
2547       ddp->id = dsp1->ids;
2548       dsp1->ids = NULL;
2549       ddp->starts[0] = start1;
2550       ddp->starts[1] = start2;
2551       ddp->len = stop1 - start1 + 1;
2552       if (ddp_head != NULL)
2553       {
2554          ddp_prev->next = ddp;
2555          ddp_prev = ddp;
2556       } else
2557          ddp_head = ddp_prev = ddp;
2558    }
2559    salp = SeqAlignNew();
2560    salp->dim = 2;
2561    salp->segtype = SAS_DENDIAG;
2562    salp->type = SAT_DIAGS;
2563    salp->segs = (Pointer)(ddp_head);
2564    SeqAlignFree(sap);
2565    return salp;
2566 }
2567 
2568 NLM_EXTERN SeqAlignPtr DDV_ShredAln(SeqAlignPtr sap)
2569 {
2570    AMAlignIndexPtr  amaip;
2571    AlnMsgPtr        amp1;
2572    AlnMsgPtr        amp2;
2573    DenseDiagPtr     ddp;
2574    DenseDiagPtr     ddp_head;
2575    DenseDiagPtr     ddp_prev;
2576    Int4             i;
2577    Boolean          more1;
2578    Boolean          more2;
2579    SeqAlignPtr      salp;
2580 
2581    if (sap == NULL || sap->saip == NULL || sap->saip->indextype != INDEX_PARENT)
2582       return NULL;
2583    amaip = (AMAlignIndexPtr)(sap->saip);
2584    if (amaip->numrows > 2)
2585       return NULL;
2586    amp1 = AlnMsgNew();
2587    amp2 = AlnMsgNew();
2588    ddp_head = ddp_prev = NULL;
2589    for (i=0; i<amaip->numsaps; i++)
2590    {
2591       amp1 = AlnMsgReNew(amp1);
2592       amp2 = AlnMsgReNew(amp2);
2593       amp1->from_m = amp2->from_m = 0;
2594       amp1->to_m = amp2->to_m = -1;
2595       amp1->row_num = 1;
2596       amp2->row_num = 2;
2597       while ((more1 = AlnMgrGetNextAlnBit(amaip->saps[i], amp1)) && (more2 = AlnMgrGetNextAlnBit(amaip->saps[i], amp2)))
2598       {
2599          if (amp1->gap == 0 && amp2->gap == 0)
2600          {
2601             ddp = DenseDiagNew();
2602             ddp->dim = 2;
2603             ddp->starts = (Int4Ptr)MemNew(2*sizeof(Int4));
2604             ddp->strands = (Uint1Ptr)MemNew(2*sizeof(Uint1));
2605             ddp->id = AlnMgrGetNthSeqIdPtr(sap, 1);
2606             ddp->id->next = AlnMgrGetNthSeqIdPtr(sap, 2);
2607             ddp->starts[0] = amp1->from_b;
2608             ddp->starts[1] = amp2->from_b;
2609             ddp->len = amp1->to_b - amp1->from_b + 1;
2610             ddp->strands[0] = AlnMgrGetNthStrand(sap, 1);
2611             ddp->strands[1] = AlnMgrGetNthStrand(sap, 2);
2612             if (ddp_head != NULL)
2613             {
2614                ddp_prev->next = ddp;
2615                ddp_prev = ddp;
2616             } else 
2617                ddp_head = ddp_prev = ddp;
2618          }
2619       }
2620    }
2621    salp = SeqAlignNew();
2622    salp->type = SAT_DIAGS;
2623    salp->segtype = SAS_DENDIAG;
2624    salp->dim = 2;
2625    salp->segs = (Pointer)(ddp_head);
2626    SeqAlignFree(sap);
2627    AlnMsgFree(amp1);
2628    AlnMsgFree(amp2);
2629    return salp;
2630 }
2631 
2632 /*******************************************************************************
2633 
2634   Function : DDV_DoAlign
2635   
2636   Purpose : this function does the actual bioseq import.  note that it doesn't
2637             require a vibrant dialog to operate, only that idp is filled out
2638   
2639   Return value : none 
2640 
2641 *******************************************************************************/
2642 
2643 NLM_EXTERN void DDV_DoAlign(DDV_ImportDialog *idp)
2644 {
2645     Char str[32];
2646     Bioseq *bsp1 = NULL, *bsp2 = NULL;
2647     SeqId si;
2648     SeqAlign *sap, *salp;
2649     Int4 value, i;
2650     ValNode *pvn;
2651     ErrSev sev;
2652     
2653     if(idp->mode & DDVIMP2SE) {
2654         value = GetValue(idp->DDV_lsip); /*one-base value*/
2655         for(pvn = idp->pvnSips, i = 1; i != value && pvn != NULL;
2656             i++, pvn = pvn->next) continue;
2657         if(pvn != NULL)
2658             bsp1 = BioseqLockById((SeqId *)pvn->data.ptrvalue);
2659     } else bsp1 = BioseqLockById(idp->sip);
2660     if (bsp1 == NULL) {
2661         Message (MSG_ERROR, "Unable to load sequence");
2662         goto out2;
2663     }
2664     
2665     if(idp->mode & DDVIMPNET) {
2666         si.choice = SEQID_GI;
2667         GetTitle(idp->DDV_tAccession, str, sizeof(str));
2668         if(GetValue(idp->DDV_gAccType) == 1 && idp->AccessionCB)
2669             si.data.intvalue = idp->AccessionCB(str, ISA_aa(bsp1->mol));
2670         else si.data.intvalue = atoi(str);
2671         if (si.data.intvalue <= 0) {
2672             Message (MSG_ERROR, "Unable to understand accession");
2673             goto out2;
2674         }
2675         bsp2 = BioseqLockById(&si);
2676     } else bsp2 = BioseqLockById((SeqId *)idp->sipslave->data.ptrvalue);
2677     if(bsp2 == NULL) {
2678         Message (MSG_ERROR, "Unable to load sequence");
2679         BioseqUnlock(bsp1);
2680         goto out2;
2681     }
2682     idp->bsp = bsp2;
2683     
2684     if(ISA_aa(bsp1->mol) != ISA_aa(bsp2->mol) ||
2685         ISA_na(bsp1->mol) != ISA_na(bsp1->mol)) {
2686         Message (MSG_ERROR, "Sequences to be aligned not of same type");
2687         BioseqUnlock(bsp1);
2688         BioseqUnlock(bsp2);
2689         goto out2;
2690     }
2691 
2692     sev = ErrSetMessageLevel(SEV_FATAL);
2693     
2694 /*    sap = ACT_GlobalAlignSimple(bsp1, bsp2, TRUE); */
2695     if(ISA_aa(bsp1->mol))
2696         sap = DDV_Blast2Seqs(bsp1, bsp2, idp->Gap, "blastp");
2697     else 
2698         sap = DDV_Blast2Seqs(bsp1, bsp2, idp->Gap, "blastn");
2699 
2700     ErrSetMessageLevel(sev);
2701     BioseqUnlock(bsp1);
2702     BioseqUnlock(bsp2);
2703     
2704     if(sap == NULL) {
2705         Message (MSG_OK, "Not able to align the sequence");
2706         goto out2;
2707     }
2708     
2709     SAM_ReplaceGI(sap);
2710     AlnMgrIndexSeqAlign(sap);
2711 
2712     if(idp->Gap) {
2713         AlnMgrMakeMultipleByScore(sap);
2714         AlnMgrDeleteHidden(sap, FALSE);
2715         salp = DDV_ShredAln(sap);
2716     } else {
2717         AlnMgrMakeMultipleByScoreEx(sap, 10);
2718         AlnMgrDeleteHidden(sap, FALSE);
2719         salp = ddv_truncate_overlaps(sap);
2720     }
2721 
2722     if(idp->callback) idp->callback(idp, idp->sap, salp);
2723     /*SeqAlignFree(sap);*/
2724     
2725 out2:
2726     MemFree(idp->userdata);
2727     ValNodeFree(idp->pvnSips); /* don't free the data */
2728     ValNodeFree(idp->sipslave);
2729 }
2730 
2731 /*******************************************************************************
2732 
2733   Function : DDV_ImportAcceptProc
2734   
2735   Purpose : fired by clicking OK in the bioseq import dialog
2736   
2737   Return value : none 
2738 
2739 *******************************************************************************/
2740 
2741 static void DDV_ImportAcceptProc(ButtoN b)
2742 {
2743     WindoW hImportDlg;
2744     DDV_ImportDialog *idp;
2745 
2746         hImportDlg= (WindoW)ParentWindow(b);
2747         if(hImportDlg == NULL) goto out;        
2748         idp = (DDV_ImportDialog *) GetObjectExtra(hImportDlg);
2749         if(idp == NULL) goto out;
2750 
2751     WatchCursor();
2752     DDV_DoAlign(idp);
2753     ArrowCursor();
2754 
2755 out:
2756     Remove(hImportDlg);
2757     return;
2758 }
2759 
2760 /*******************************************************************************
2761 
2762   Function : DDV_ImportCB
2763   
2764   Purpose : callback for import bioseq dialog.  used to clean up after the
2765             import
2766   
2767   Return value : none 
2768 
2769 *******************************************************************************/
2770 
2771 NLM_EXTERN void DDV_ImportCB(DDV_ImportDialog *idp, SeqAlign *salpdest,
2772                              SeqAlign *salp)
2773 {
2774     if (salpdest == NULL || salp == NULL) return;
2775     SeqAlignSetFree(salp->next);
2776     salp->next = NULL;
2777     ViewMgr_Add(salpdest, salp);
2778 }
2779 
2780 /*******************************************************************************
2781 
2782   Function : DDV_ImportCancelProc
2783   
2784   Purpose : cancel function for the import bioseq dialog
2785   
2786   Return value : none 
2787 
2788 *******************************************************************************/
2789 
2790 static void DDV_ImportCancelProc(ButtoN b)
2791 {
2792     WindoW hImportDlg;
2793     DDV_ImportDialog *idp;
2794 
2795         hImportDlg= (WindoW)ParentWindow(b);
2796         if(hImportDlg == NULL) return;  
2797         idp = (DDV_ImportDialog *) GetObjectExtra(hImportDlg);
2798         if(idp == NULL) return;
2799     MemFree(idp->userdata);
2800 
2801     Remove(idp->DDV_wImport);
2802     return;
2803 }
2804 
2805 /*******************************************************************************
2806 
2807   Function : DDV_ImportBioseqDlg
2808   
2809   Purpose : dialog to import a bioseq into a seqalign.  Note that it has
2810             several modes listed in ddvopen.h
2811   
2812   Return value : none 
2813 
2814 *******************************************************************************/
2815 
2816 NLM_EXTERN void DDV_ImportBioseqDlg(DDV_ImportDialog *idp)
2817 {
2818     GrouP g, hg;
2819     ButtoN b;
2820     DDVUpdateMSG  *dump;
2821     ValNode *pvn;
2822     Char szName[21]={""};
2823 
2824     if(idp == NULL) return;
2825     
2826     /* check if we have the right data for each mode */
2827     if(idp->mode == DDVIMPSE2SA 
2828         && (idp->sip == NULL || idp->sap == NULL || idp->sipslave == NULL)) return;
2829     else if (idp->mode == DDVIMPSE2SE 
2830         && (idp->pvnSips == NULL || idp->sipslave == NULL)) return;
2831     else if (idp->mode == DDVIMPNET2SA 
2832         && (idp->sap == NULL || idp->sip == NULL)) return;
2833     else if (idp->mode == DDVIMPNET2SE  
2834         && idp->pvnSips == NULL) return;
2835 
2836     idp->DDV_wImport = NULL;
2837     idp->DDV_gAccType = NULL;
2838     idp->DDV_bImportAccept = NULL;
2839     idp->DDV_tAccession = NULL;
2840     dump = MemNew(sizeof(DDVUpdateMSG));
2841     if(dump == NULL) {
2842         MemFree(idp);
2843         return;
2844     }
2845     idp->userdata = dump;
2846     dump->data = NULL;
2847         dump->type=UPDATE_TYPE_EDIT_DELBSP;
2848 
2849     idp->DDV_wImport = MovableModalWindow(-30, -20, -10, -10,
2850         "Import sequence using BLAST", NULL);
2851     Nlm_SetObjectExtra (idp->DDV_wImport, (Nlm_VoidPtr)idp, StdCleanupExtraProc);
2852 
2853     if(idp->mode & DDVIMP2SE) {
2854         hg = HiddenGroup(idp->DDV_wImport, 1, 4, NULL);
2855         g = NormalGroup(hg, 1, 2, "Choose sequence to align to:", systemFont, NULL);
2856         idp->DDV_lsip = SingleList(g,10,6,NULL);
2857 
2858         for(pvn = idp->pvnSips; pvn != NULL; pvn = pvn->next) {
2859             SeqIdWrite((SeqId *)pvn->data.ptrvalue,szName,
2860                 PRINTID_TEXTID_ACCESSION, 10);
2861             ListItem(idp->DDV_lsip, szName);
2862         }
2863         SetValue(idp->DDV_lsip, 1);
2864     } else hg = HiddenGroup(idp->DDV_wImport, 1, 3, NULL);
2865 
2866     if(idp->mode & DDVIMPNET) {
2867         g = NormalGroup(hg, 1, 2, " Enter sequence identifier:", systemFont, NULL);
2868         idp->DDV_tAccession = DialogText(g, "", 10, (TxtActnProc) DDV_ImportEnableProc);
2869         idp->DDV_gAccType =
2870             NormalGroup(hg, 1, 2, " accession type", systemFont, NULL);
2871         RadioButton(idp->DDV_gAccType, "Accession");
2872         RadioButton(idp->DDV_gAccType, "gi");
2873     }
2874 
2875     g = HiddenGroup(hg, 2, 1, NULL);
2876     SetGroupSpacing(g, 15, 15);
2877     idp->DDV_bImportAccept =
2878         DefaultButton(g, "OK", (BtnActnProc) DDV_ImportAcceptProc);
2879     b = PushButton(g, "Cancel", (BtnActnProc) DDV_ImportCancelProc);
2880 
2881     if(idp->mode & DDVIMPNET) {
2882         SetValue(idp->DDV_gAccType, 1);
2883         Disable(idp->DDV_bImportAccept);
2884         Select(idp->DDV_tAccession);
2885     }
2886     Show(idp->DDV_wImport);
2887     return;
2888 }
2889 
2890 /*******************************************************************************
2891 
2892   Function : DDV_ImportBioseq
2893   
2894   Purpose : sets up dialog to import a bioseq into a seqalign
2895   
2896   Return value : none 
2897 
2898 *******************************************************************************/
2899 
2900 NLM_EXTERN void DDV_ImportBioseq(IteM i)
2901 {
2902     DDV_ImportDialog *idp;
2903     DdvMainPtr    dmp;  /*DDV panel data*/
2904     DdvMainWinPtr mWin_d;
2905     WindoW                hWinMain;
2906 
2907     idp = MemNew(sizeof(DDV_ImportDialog));
2908     if(idp == NULL) return;
2909 
2910         hWinMain=(WindoW)ParentWindow(i);
2911         if (hWinMain==NULL) return;
2912         mWin_d = (DdvMainWinPtr) GetObjectExtra (hWinMain);
2913         if (mWin_d==NULL) return;
2914 
2915     if (mWin_d->NetStartProc) mWin_d->NetStartProc(mWin_d->UseNetwork);
2916 
2917         if (mWin_d->hWndDDV==NULL) return;
2918         dmp = (DdvMainPtr) GetObjectExtra(mWin_d->hWndDDV);
2919         if (dmp==NULL) return;
2920 
2921     if(dmp->MSA_d.pgp_l.sap == NULL) return;
2922 
2923     idp->sap = dmp->MSA_d.pgp_l.sap;
2924     idp->sip = AlnMgrFindMaster(idp->sap);
2925     if(idp->sip == NULL) {
2926         Message (MSG_ERROR, "Alignment must have master sequence");
2927         MemFree(idp);
2928         return;
2929     }
2930 
2931     idp->callback = DDV_ImportCB;
2932     idp->mode = DDVIMPNET2SA;
2933 
2934     DDV_ImportBioseqDlg(idp);
2935 }
2936 
2937 
2938 NLM_EXTERN SeqAlign *DDV_Blast2Seqs(Bioseq *bsp1, Bioseq *bsp2, Boolean gapped,
2939                          Char *progname)
2940 {
2941     BLAST_OptionsBlkPtr options;
2942     SeqAlign  *salp;
2943     
2944     if(bsp1 == NULL || bsp2 == NULL || progname == NULL) return NULL;
2945     options = BLASTOptionNew(progname, gapped);
2946     if(options == NULL) return NULL;
2947 /*    options->discontinuous = FALSE; */
2948     salp = BlastTwoSequences(bsp1, bsp2, progname, options);
2949     BLASTOptionDelete(options);
2950     return salp;
2951 }
2952 
2953 
2954 /*******************************************************************************
2955 
2956   Function : DDV_ImportSeqAlign(), DDV_ImportNucSeqAlign() and DDV_ImportProtSeqAlign()
2957   
2958   Purpose : import a sequence alignment of text type
2959   
2960   Return value : none 
2961 
2962 *******************************************************************************/
2963 static void DDV_ImportSeqAlign(Boolean is_prot,IteM i)
2964 {
2965 WindoW        hWinMain;
2966 DdvMainPtr    dmp;
2967 DdvMainWinPtr mWin_d;
2968 SeqEntryPtr   sep;
2969 ValNodePtr    vnp_ali;
2970 SeqAlignPtr   sap;
2971 Uint2             The_entityID,entityID;
2972 Uint4         itemID;
2973 Int2              procval;
2974 
2975         hWinMain=(WindoW)ParentWindow(i);
2976         
2977         mWin_d = (DdvMainWinPtr) GetObjectExtra (hWinMain);
2978 
2979         sep=ReadAnyAlignment(is_prot,NULL);
2980 
2981         if (sep!=NULL && mWin_d!=NULL){
2982                 /*try to connect Entrez; if failure, still ok : the user can
2983                 use DDV with a local file. Otherwise he/she will be in trouble ;-)*/
2984                 if (mWin_d->AutonomeViewer && mWin_d->NetStartProc){
2985                         mWin_d->NetStartProc(mWin_d->UseNetwork);
2986                 }
2987                 /*delete old data, if needed*/
2988                 dmp=(DdvMainPtr)GetObjectExtra(mWin_d->hWndDDV);
2989                 DDV_CleanupDDVPdata_g(dmp);
2990                 DDV_CloseData(mWin_d,FALSE);
2991 
2992                 vnp_ali=DDV_GetAndCheckSeqAlign(NULL,0,sep,mWin_d->fetchSepProc,&(dmp->dod),
2993                         &The_entityID);
2994                 if (vnp_ali && The_entityID!=0){
2995 
2996                         /*get the first SeqAlign in the list and load the viewer panel*/
2997                         dmp->vnp_ali=vnp_ali;
2998 
2999                         if(mWin_d->Show_logo){
3000                                 mWin_d->Show_logo=FALSE;
3001                         }
3002                         sap=(SeqAlignPtr)vnp_ali->data.ptrvalue;
3003                         entityID=sap->idx.entityID;
3004                         itemID=sap->idx.itemID;
3005                         SetAppProperty("ddvinterndata",(void *)dmp);
3006                         procval=GatherProcLaunch ((dmp->bEditor==TRUE? OMPROC_EDIT : OMPROC_VIEW), FALSE, entityID, itemID, 
3007                                 OBJ_SEQALIGN, 0, 0, OBJ_SEQALIGN, 0);
3008                         if ((procval = OM_MSG_RET_ERROR))
3009                                 mWin_d->Show_logo=TRUE;
3010                         RemoveAppProperty("ddvinterndata");
3011                 }
3012         }
3013         else{
3014                 ErrPostEx (SEV_ERROR, 0, 0, "We do not support this format yet");
3015         }
3016 }
3017 
3018 extern void DDV_ImportNucSeqAlign(IteM i)
3019 {
3020         DDV_ImportSeqAlign(FALSE,i);
3021 }
3022 
3023 extern void DDV_ImportProtSeqAlign(IteM i)
3024 {
3025         DDV_ImportSeqAlign(TRUE,i);
3026 }
3027 
3028 static Int4 DDV_GetLastRow(DDV_HideDialog *hdp)
3029 {
3030   int i, max=0;
3031 
3032   for (i = 1; i <= hdp->numrows; i++) {
3033     if (ViewMgr_TRow2VRow(hdp->salp, i) > 0) {
3034       max = i;
3035     }
3036   }
3037   return(max);
3038 }
3039 
3040 static void DDV_SetHideList(DDV_HideDialog *hdp)
3041 {
3042     int i;
3043     
3044     for(i = 1; i <= hdp->numrows; i++) {
3045         if(ViewMgr_TRow2VRow(hdp->salp, i) > 0) 
3046             SetItemStatus(hdp->DDV_lsip, i, TRUE);
3047         else SetItemStatus(hdp->DDV_lsip, i, FALSE);
3048     }
3049     SetItemStatus(hdp->DDV_lsip, hdp->amaip->master, TRUE);
3050 }
3051 
3052 static void DDV_HideList(Nlm_LisT l)
3053 /*******************************************************************************
3054 *  this function is called each time an item from the list is clicked.
3055 *  it sets the status for each row, making sure not to allow the
3056 *  master to be hidden, nor the number of rows to be less than 2.
3057 *******************************************************************************/
3058 {
3059     WindoW hHideDlg;
3060     DDV_HideDialog *hdp;
3061     Int4 i, LastRow, count = 0;
3062 
3063     hHideDlg = (WindoW)ParentWindow(l);
3064     if(hHideDlg == NULL) return;        
3065     hdp = (DDV_HideDialog *) GetObjectExtra(hHideDlg);
3066     if(hdp == NULL) return;
3067 
3068     /* get row count, and last displayed row */
3069     for(i = 1; i <= hdp->numrows; i++) {
3070       if (GetItemStatus(hdp->DDV_lsip, i)) {
3071         count++;
3072         LastRow = i;
3073       }
3074     }
3075 
3076     /* don't let number of rows fall below 2 */
3077     if (count < 2) {
3078       if (hdp->LastValidRow == -1) {
3079         SetItemStatus(hdp->DDV_lsip, DDV_GetLastRow(hdp), TRUE);
3080       }
3081       else {
3082         SetItemStatus(hdp->DDV_lsip, hdp->LastValidRow, TRUE);
3083       }
3084     }
3085     else {
3086       /* each time a row is clicked, update the last saved row */
3087       hdp->LastValidRow = LastRow;
3088     }
3089 
3090     /* don't let master be hidden */
3091     if (!GetItemStatus(hdp->DDV_lsip, 1)) {
3092       SetItemStatus(hdp->DDV_lsip, 1, TRUE);
3093     }
3094 }
3095 
3096 
3097 static void DDV_HideAcceptProc(ButtoN b)
3098 {
3099     WindoW hHideDlg;
3100     DDV_HideDialog *hdp;
3101     Int4  i;
3102     Boolean  status;
3103 
3104     hHideDlg = (WindoW)ParentWindow(b);
3105     if(hHideDlg == NULL) return;        
3106     hdp = (DDV_HideDialog *) GetObjectExtra(hHideDlg);
3107     if(hdp == NULL) return;
3108 
3109     for(i = 1; i <= hdp->numrows; i++) {
3110       status = GetItemStatus(hdp->DDV_lsip, i);
3111       ViewMgr_SetHidden(hdp->salp, !status, i);
3112     }
3113     ViewMgr_Update(hdp->salp);
3114     DDV_SetHideList(hdp);
3115 
3116     MemFree(hdp->userdata);
3117     Remove(hdp->DDV_wHide);
3118 }
3119 
3120 
3121 /*******************************************************************************
3122 
3123   Function : DDV_HideDlg
3124   
3125   Purpose : dialog to import a bioseq into a seqalign.  Note that it has
3126             several modes listed in ddvopen.h
3127   
3128   Return value : none 
3129 
3130 *******************************************************************************/
3131 
3132 NLM_EXTERN void DDV_HideDlg(DDV_HideDialog *hdp)
3133 {
3134     GrouP g, hg;
3135     ButtoN b;
3136     DDVUpdateMSG  *dump;
3137     Char szName[181], szName2[161];
3138     Int4 i;
3139     SeqId *sip;
3140     Bioseq *bsp;
3141 
3142     if(hdp == NULL) return;
3143     hdp->DDV_wHide = NULL;
3144     
3145     hdp->target = ViewMgr_GetBeginIndexed(hdp->salp);
3146 
3147     if(hdp->target == NULL) return;
3148     if( AlnMgrCheckAlignForParent(hdp->target) != AM_PARENT) return;
3149     hdp->amaip = (AMAlignIndexPtr)(hdp->target->saip);
3150     if (!hdp->amaip) return;
3151     
3152     dump = MemNew(sizeof(DDVUpdateMSG));
3153     if(dump == NULL) {
3154         MemFree(hdp);
3155         return;
3156     }
3157     hdp->userdata = dump;
3158     dump->data = NULL;
3159     dump->type=UPDATE_TYPE_EDIT_DELBSP;
3160 
3161     hdp->DDV_wHide = MovableModalWindow(-30, -20, -10, -10,
3162         "Hide/Show Sequences", NULL);
3163     /* need to do something more than std cleanup.  also for blast dialog */
3164     Nlm_SetObjectExtra (hdp->DDV_wHide, (void *)hdp, StdCleanupExtraProc);
3165     
3166     hg = HiddenGroup(hdp->DDV_wHide, 1, 2, NULL);
3167     g = NormalGroup(hg, 1, 2, "Choose sequences to show:", systemFont, NULL);
3168     hdp->DDV_lsip = MultiList(g,40, 6, DDV_HideList);
3169 
3170     hdp->numrows = AlnMgrGetNumRows(hdp->target);
3171 
3172     for(i = 1; i <= hdp->numrows; i++) {
3173         sip = AlnMgrGetNthSeqIdPtr(hdp->target, i);
3174         if(sip == NULL) continue;
3175         bsp=BioseqLockById(sip);
3176         if(bsp == NULL) continue;
3177         sip = SeqIdFindBestAccession(bsp->id);
3178         SeqIdWrite(sip, szName, PRINTID_TEXTID_ACCESSION, 10);
3179         CreateDefLine(NULL,bsp,szName2,157,0,NULL,NULL);
3180         BioseqUnlock(bsp);
3181         StrNCat(szName, " - ", 180);
3182         StrNCat(szName, szName2, 180);
3183         ListItem(hdp->DDV_lsip, szName);
3184     }   
3185     DDV_SetHideList(hdp);
3186 
3187     g = HiddenGroup(hg, 2, 1, NULL);
3188     SetGroupSpacing(g, 15, 15);
3189     b = DefaultButton(g, "OK", (BtnActnProc) DDV_HideAcceptProc);
3190 
3191     Show(hdp->DDV_wHide);
3192 
3193     /* set scroll to upper left */
3194     SetOffset(hdp->DDV_lsip, 0, 0);
3195 
3196     return;
3197 }
3198 
3199 NLM_EXTERN void DDV_HideDlgItem(IteM i)
3200 {
3201     DDV_HideDialog *hdp;
3202     DdvMainPtr    dmp;  /*DDV panel data*/
3203     DdvMainWinPtr mWin_d;
3204     WindoW                hWinMain;
3205 
3206     hdp = MemNew(sizeof(DDV_HideDialog));
3207     if(hdp == NULL) return;
3208 
3209     /* init LastValidRow to something illegal */
3210     hdp->LastValidRow = -1;
3211 
3212     hWinMain=(WindoW)ParentWindow(i);
3213     if (hWinMain==NULL) return;
3214     mWin_d = (DdvMainWinPtr) GetObjectExtra (hWinMain);
3215     if (mWin_d==NULL) return;
3216 
3217     if (mWin_d->hWndDDV==NULL) return;
3218     dmp = (DdvMainPtr) GetObjectExtra(mWin_d->hWndDDV);
3219     if (dmp==NULL) return;
3220 
3221     if(dmp->MSA_d.pgp_l.sap == NULL) return;
3222 
3223     hdp->salp = dmp->MSA_d.pgp_l.sap;
3224 
3225     DDV_HideDlg(hdp);
3226 }
3227 
3228 
3229 /*******************************************************************************
3230 
3231   Function : DDV_GetCharBufferForEditor()
3232   
3233   Purpose : get a char buffer from a row of a SeqAlign, given display coords
3234 
3235   Return value : an allocated char buffer ready for use by the editor. Null if
3236   failure
3237 
3238 *******************************************************************************/
3239 NLM_EXTERN void DDV_GetCharBufferForEditor(ParaGPtr pgp,CharPtr szEditSeq)
3240 {
3241 BioseqPtr  bsp;
3242 Int4       bspLength;
3243 Boolean    IsAA;
3244 
3245         /*get sequence size and type*/
3246         bsp=BioseqLockById(pgp->sip);
3247         if (!bsp) return;
3248         bspLength=BioseqGetLen(bsp);
3249         IsAA=ISA_aa(bsp->mol);
3250         BioseqUnlock(bsp);
3251 
3252         /*get the sequence*/
3253         DDV_GetSequenceFromParaG(pgp,&szEditSeq,bspLength,IsAA,NULL,NULL,NULL);
3254         
3255 }
3256 
3257 /*******************************************************************************
3258 
3259   Function : DDV_GetMtdpListForEditor()
3260   
3261   Purpose : get the descriptor for a row of a SeqAlign, given display coords
3262 
3263   Note : in plain english, this function creates the pTxtList ValNode for
3264      a ParaG designed to be used in the editor.
3265          
3266   Return value : a valnode ready for use by the editor. Null if
3267   failure. 
3268 
3269 *******************************************************************************/
3270 NLM_EXTERN ValNodePtr DDV_GetMtdpListForEditor(ValNodePtr row,Int4 from_disp,
3271                 Int4 to_disp)
3272 {
3273 MsaTxtDispPtr mtdp,new_mtdp,prev_mtdp;
3274 ValNodePtr vnp,vnp2,vnp3,vnpTxt;
3275 ParaGPtr   pgp;
3276 Int4       cumulMtdp,beginCopy,endCopy,mtdp_size,mtdp_from_disp,
3277            mtdp_to_disp,mtdp_from,mtdp_to;
3278 Boolean    bPop;
3279 
3280 
3281         
3282         prev_mtdp=NULL;
3283         vnpTxt=NULL;
3284         for (vnp=row;vnp!=NULL;vnp=vnp->next){
3285                 pgp=(ParaGPtr)vnp->data.ptrvalue;
3286                 /*not yet in the first ParaG ?*/
3287                 if (pgp->StopLetter<from_disp) {
3288                         continue;
3289                 }
3290                 /*get the mtdp nodes*/
3291                 cumulMtdp=0;
3292                 for(vnp2=pgp->ptxtList;vnp2!=NULL;vnp2=vnp2->next){
3293                         mtdp=(MsaTxtDispPtr)vnp2->data.ptrvalue;
3294                         mtdp_size=mtdp->to-mtdp->from+1;
3295                         /*convert mtdp coordinates (can Bioseq coord, gap coord, etc.)
3296                         to display coordinates*/
3297                         mtdp_from_disp=pgp->StartLetter+cumulMtdp;
3298                         mtdp_to_disp=mtdp_from_disp+mtdp_size-1;
3299                         if (mtdp_to_disp<from_disp) {
3300                                 cumulMtdp+=mtdp_size;
3301                                 continue;
3302                         }
3303                         beginCopy=_max_(from_disp,mtdp_from_disp);
3304                         endCopy=_min_(mtdp_to_disp,to_disp);
3305 
3306                         /*convert display coordinates back to mtdp coordinates*/
3307                         mtdp_from=mtdp->from+(beginCopy-mtdp_from_disp);
3308                         mtdp_to=mtdp->from+(endCopy-mtdp_from_disp);
3309 
3310                         /*in order to merge nodes of same type, i keep track of
3311                         the previous populated mtdp node. Same style==yes, then
3312                         just extend to the right the previous node*/
3313                         if (prev_mtdp){
3314                                 if (prev_mtdp->TextStyle==mtdp->TextStyle){
3315                                         prev_mtdp->to=mtdp_to;
3316                                         bPop=TRUE;
3317                                 }
3318                                 else{
3319                                         bPop=FALSE;
3320                                 }
3321                         }
3322                         else{
3323                                 bPop=FALSE;
3324                         }
3325                         if (!bPop){
3326                                 new_mtdp=(MsaTxtDispPtr)MemNew(sizeof(MsaTxtDisp));
3327                                 if (!new_mtdp) goto erreur;
3328                                 MemCopy(new_mtdp,mtdp,sizeof(MsaTxtDisp));
3329                                 new_mtdp->from=mtdp_from;
3330                                 new_mtdp->to=mtdp_to;
3331                                 if (!vnpTxt){
3332                                         vnp3=ValNodeAddPointer(&vnpTxt,0,(Pointer)new_mtdp);
3333                                 }
3334                                 else{
3335                                         vnp3=ValNodeAddPointer(&vnp3,0,(Pointer)new_mtdp);
3336                                 }
3337                                 prev_mtdp=new_mtdp;
3338                         }
3339                         if (mtdp_to_disp>=to_disp) break;
3340                         cumulMtdp+=mtdp_size;
3341                 }
3342                 /*done ?*/
3343                 if (pgp->StopLetter>=to_disp) break;
3344         }
3345         return(vnpTxt);
3346 
3347 erreur:
3348         if (vnpTxt){
3349                 DDV_DeleteTxtList(&vnpTxt);
3350         }
3351         return(NULL);
3352 }
3353 
3354 /*******************************************************************************
3355 
3356   Function : DDV_CreateDataForEditor()
3357   
3358   Purpose : create a MsaParaGPopListPtr intended to be used by the editor
3359 
3360   Return value : an allocated data block ready for use by the editor. Null if
3361   failure
3362 
3363 *******************************************************************************/
3364 NLM_EXTERN MsaParaGPopListPtr DDV_CreateDataForEditor(MsaParaGPopListPtr mpplp, 
3365         Int4 from_disp,Int4 to_disp)
3366 {
3367 MsaParaGPopListPtr new_mpplp;
3368 
3369 ParaGPtr           pgp,old_pgp;
3370 Int4               i,iNewAliSize,diff;
3371 
3372         if (from_disp<0 || from_disp>=mpplp->LengthAli || from_disp>to_disp ||
3373                 to_disp<0 || to_disp>=mpplp->LengthAli){
3374                 return(NULL);
3375         }
3376         
3377         diff=to_disp-from_disp;
3378         iNewAliSize = diff+50;
3379 
3380         if (iNewAliSize<1 || iNewAliSize>LIMIT_EDITOR_SIZE)
3381                 return(NULL);
3382 
3383         new_mpplp=(MsaParaGPopListPtr)MemNew(sizeof(MsaParaGPopList));
3384         if (!new_mpplp) return(NULL);
3385         
3386         /*table of ParaG list (each cell of that table corresponds to one row
3387         of the seqalign to display*/
3388         new_mpplp->TableHead=(ValNodePtr *)MemNew(mpplp->nBsp*sizeof(ValNodePtr));
3389         if (!new_mpplp->TableHead) {
3390                 MemFree(new_mpplp);
3391                 return(NULL);
3392         }
3393         /*allocate the new ParaG list; for the editor there is a single ParaG for
3394         each row (for the viewer there are usually n ParaGs for each row. For the
3395         editor, the ParaG size = to_disp-from_disp+50 (whereas in the viewer a
3396         ParaG size = ParaG_Size (usually 70; see pgppop.h)*/
3397         for (i=0;i<mpplp->nBsp;i++){
3398                 /*allocations*/
3399                 pgp=(ParaGPtr)MemNew(sizeof(ParaG));
3400                 if (!pgp) goto erreur;
3401                 pgp->szEditSeq=(CharPtr)MemNew(iNewAliSize*sizeof(Char));
3402                 if (pgp->szEditSeq==NULL) goto erreur;
3403                 /*get the ParaG line descriptor */
3404                 pgp->ptxtList=DDV_GetMtdpListForEditor(mpplp->TableHead[i],from_disp,
3405                                 to_disp);
3406                 if (!pgp->ptxtList) goto erreur;
3407                 /*get the char buffer*/
3408                 old_pgp=(ParaGPtr)mpplp->TableHead[i]->data.ptrvalue;
3409                 pgp->sip = old_pgp->sip;
3410                 DDV_GetCharBufferForEditor(pgp,pgp->szEditSeq);
3411                 if (*(pgp->szEditSeq)=='\0') goto erreur;
3412                 /*copy/init values*/
3413                 pgp->StartLetter = 0;
3414                 pgp->StopLetter = diff;
3415                 pgp->NumOrder = i;
3416                 pgp->StartLine = i;
3417                 pgp->nLines = 1;
3418                 pgp->ScaleStyle=SCALE_POS_NONE;
3419                 /*store pgp*/
3420                 new_mpplp->TableHead[i]=(ValNodePtr)MemNew(sizeof(ValNode));
3421                 if (!new_mpplp->TableHead[i]) goto erreur;
3422                 new_mpplp->TableHead[i]->data.ptrvalue=(Pointer)pgp;
3423         }
3424 
3425         /*seqali size and type*/
3426         new_mpplp->LengthAli=diff+1;
3427         new_mpplp->nBsp=mpplp->nBsp;
3428         new_mpplp->sap=mpplp->sap;
3429         new_mpplp->DisplayType=mpplp->DisplayType;
3430         
3431         /*entities table*/
3432         new_mpplp->entitiesTbl=(Uint4Ptr)MemNew(new_mpplp->nBsp*sizeof(Uint4));
3433         if (new_mpplp->entitiesTbl==NULL) goto erreur;
3434         MemCopy(new_mpplp->entitiesTbl,mpplp->entitiesTbl,
3435                 new_mpplp->nBsp*sizeof(Uint4));
3436         /*ruler descriptor*/
3437         new_mpplp->RulerDescr=DDV_GetRulerForEditor(mpplp->RulerDescr,from_disp,
3438                         to_disp);
3439         if (new_mpplp->RulerDescr==NULL) goto erreur;
3440         return(new_mpplp);      
3441         
3442 erreur : /*serious problem*/    
3443         if (new_mpplp->entitiesTbl) 
3444                 MemFree(new_mpplp->entitiesTbl);
3445         if (new_mpplp->RulerDescr)
3446                 ValNodeFreeData(new_mpplp->RulerDescr);
3447         DDV_DeleteDisplayList(new_mpplp);
3448         MemFree(new_mpplp);
3449         return(NULL);
3450 }
3451 
3452 
3453 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.