summaryrefslogtreecommitdiff
path: root/api/viewmgr.c
blob: e574c9667d51dee41bba20d9848f7b477b08a949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
/*   $Id: viewmgr.c,v 1.37 2006/07/13 17:06:39 bollin Exp $
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
* ===========================================================================
*
* File Name:  $Id: viewmgr.c,v 1.37 2006/07/13 17:06:39 bollin Exp $
*
* Author:  Lewis Geer
*
* Version Creation Date:   2/1/00
*
* $Revision: 1.37 $
*
* File Description: The ViewMgr is the part of the alignment management
*                   system that creates a viewable seqalign from an original
*                   seqalign (called a target) by applying a set of
*                   transforms to the target, such as show/hide rows.  The
*                   ViewMgr also deals with allocation/deallocation of
*                   the target and view seqaligns.
*
* Modifications:
* --------------------------------------------------------------------------
* $Log: viewmgr.c,v $
* Revision 1.37  2006/07/13 17:06:39  bollin
* use Uint4 instead of Uint2 for itemID values
* removed unused variables
* resolved compiler warnings
*
* Revision 1.36  2002/05/09 15:34:51  kans
* commented out unneeded headers in tools library
*
* Revision 1.35  2001/01/18 22:28:06  hurwitz
* added fast option for ViewMgr_Update
*
* Revision 1.34  2001/01/07 08:11:13  vakatov
* Fixed C++ style comments to the C-style ones
*
* Revision 1.33  2001/01/06 01:09:00  lewisg
* get indexed begin in make multiple, copy seqaligns correctly
*
* Revision 1.32  2001/01/02 23:38:20  lewisg
* null target and begin pointers
*
* Revision 1.31  2000/12/29 21:10:43  lewisg
* bug fixes
*
* Revision 1.30  2000/08/30 13:43:11  lewisg
* change seqalign state when made into multiple
*
* Revision 1.29  2000/08/01 12:54:29  lewisg
* *** empty log message ***
*
* Revision 1.28  2000/06/20 19:35:11  hurwitz
* use indexed seqAlign when necessary, make multiple when redrawing
*
* Revision 1.27  2000/06/16 13:44:10  lewisg
* fix Int4->int fcn declaration
*
* Revision 1.26  2000/06/13 18:23:54  hurwitz
* made ViewMgr_MakeMultiple routine, call this on each launch of DDE rather than launch of DDV
*
* Revision 1.25  2000/06/08 20:52:42  hurwitz
* fixed a bug with show/hide rows
*
* Revision 1.24  2000/06/07 23:08:48  lewisg
* fix show/hide for large number of rows
*
* Revision 1.23  2000/05/12 16:15:26  hurwitz
* reverted to not doing IntersectOnMaster for DDE, now determined by call to ViewMgr_Attach
*
* Revision 1.22  2000/05/10 19:02:36  hurwitz
* for dde, always do IntersectOnMaster
*
* Revision 1.21  2000/04/17 21:46:55  lewisg
* do not do double index on viewmgr update, rename menus
*
* Revision 1.20  2000/04/10 20:33:40  lewisg
* fix show/hide for blast multiple, make blast multiple API generic
*
* Revision 1.19  2000/04/07 18:57:17  thiessen
* fix VRow2TRow
*
* Revision 1.18  2000/04/04 22:18:42  lewisg
* add defline to ddv, fix seq import bugs, set boundbox
*
* Revision 1.17  2000/04/04 17:51:54  lewisg
* fix various seq import bugs
*
* Revision 1.16  2000/03/29 23:38:06  lewisg
* hide/show, fixes to saving and opening
*
* Revision 1.15  2000/03/27 22:15:04  lewisg
* add show/hide row dialog
*
* Revision 1.14  2000/03/15 19:32:20  lewisg
* launch only single udv window
*
* Revision 1.13  2000/03/14 14:20:15  lewisg
* add extern row conversion functions
*
* Revision 1.12  2000/03/10 18:47:00  lewisg
* add show/hide
*
* Revision 1.11  2000/03/08 21:46:13  lewisg
* cn3d saves viewport, misc bugs
*
* Revision 1.10  2000/03/02 21:11:05  lewisg
* use bandalign for import sequence, make standalone ddv use viewmgr, make dialogs modal, send color update
*
* Revision 1.9  2000/03/01 22:53:40  lewisg
* import bioseq, neatlyindex, get rid of dead code
*
* Revision 1.8  2000/02/22 17:22:33  lewisg
* fix mac error
*
* Revision 1.7  2000/02/19 01:23:58  lewisg
* use ibm, add row tracking code, various bug fixes
*
* Revision 1.6  2000/02/16 16:17:34  lewisg
* add extra argument to AlnMgrMakeMultByIntersectOnMaster
*
* Revision 1.5  2000/02/15 23:02:46  kans
* return 0 instead of NULL for Int4 return value - too bad only the Mac compiler can catch this
*
* Revision 1.4  2000/02/15 22:40:57  lewisg
* add ability to launch udv so that it colors by row, fixes to colormgr, track rows from viewmgr, fix visual c projects
*
* Revision 1.3  2000/02/10 15:51:58  lewisg
* cn3d responds and send correct update messages.  many coloring bug fixes
*
* Revision 1.2  2000/02/05 19:37:45  kans
* fixed almost certain unintended assignment in if statement - caught by Mac compiler
*
* Revision 1.1  2000/02/05 01:32:23  lewisg
* add viewmgr, move place freeing is done in ddv, modify visual c++ projects
*
*
*
* ==========================================================================
*/

#include <viewmgr.h>
#include <objmgr.h>
#include <alignmgr.h>
#include <sqnutils.h>
/*
#include <ddvcolor.h>
#include <actutils.h>
*/


/*****************************************************************************

Function: ViewMgr_Find

Purpose: Finds a seqalign by pointer inside of the ViewMgr_Global
  
Parameters: salp, the seqalign
            pGlobal, the ViewMgr_Global

Returns: a pointer to info about the alignment

*****************************************************************************/

static ViewMgr_AlignInfo * ViewMgr_Find(ViewMgr_Global *pGlobal,
                                        SeqAlign *salp)
{
    ValNode *pvn;
    ViewMgr_AlignInfo *pInfo;


    if(pGlobal == NULL || salp == NULL) return NULL;

    for(pvn = pGlobal->pAlignList; pvn != NULL; pvn = pvn->next) {
        pInfo = (ViewMgr_AlignInfo *)pvn->data.ptrvalue;
        if(pInfo == NULL) continue;
        if(pInfo->pView == salp) return pInfo;
    }
    return NULL;
}

static ViewMgr_AlignInfo * ViewMgr_GetInfo(SeqAlign *salp)
{
    ViewMgr_Global *pGlobal;

    pGlobal = GetAppProperty("ViewMgr");
    if(pGlobal == NULL || salp == NULL) return NULL;
    return ViewMgr_Find(pGlobal, salp);
}

static Int4 ViewMgr_xVRow2TRow(ViewMgr_Global *pGlobal, SeqAlign *salp,
                               Int4 VRow)
{
    ViewMgr_AlignInfo * pInfo;
    ValNode *pvn;
    Int4 TRow, nVRow;
    Boolean isHidden;

    if(pGlobal == NULL || salp == NULL || VRow <= 0) return -1;
    pInfo = ViewMgr_Find(pGlobal, salp);
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_xVRow2TRow", -1);

    for (TRow = 1, nVRow = 0; ; TRow++) {
        isHidden = FALSE;
        for (pvn = pInfo->pHiddenRows; pvn != NULL; pvn = pvn->next) {
            if (pvn->data.intvalue == TRow) {
                isHidden = TRUE;
                break;
            }
        }
        if (!isHidden) nVRow++;
        if (nVRow == VRow)
            break;
    }
    return TRow;
}

/*****************************************************************************

Function: ViewMgr_VRow2TRow

Purpose: Converts a viewed row to a target row
  
Parameters: salp, the seqalign
            VRow, the row number in the viewed SeqAlign

Returns: the row in the target SeqAlign, -1 on failure

*****************************************************************************/
NLM_EXTERN Int4 ViewMgr_VRow2TRow(SeqAlign *salp, Int4 VRow)
{
    ViewMgr_Global *pGlobal;

    pGlobal = GetAppProperty("ViewMgr");
    if (pGlobal == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_TRow2VRow", -1);
    return ViewMgr_xVRow2TRow(pGlobal, salp, VRow);
}


static Int4 ViewMgr_xTRow2VRow (ViewMgr_Global *pGlobal, SeqAlign *salp,
                               Int4 TRow)
{
    ViewMgr_AlignInfo * pInfo;
    ValNode *pvn;
    Int4 VRow = TRow;

    if(pGlobal == NULL || salp == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_xTRow2VRow", -1);
    pInfo = ViewMgr_Find(pGlobal, salp);
    if(pInfo == NULL) return -1;

    for(pvn = pInfo->pHiddenRows; pvn != NULL; pvn = pvn->next) {
        if(pvn->data.intvalue < TRow) VRow--;
        if(pvn->data.intvalue == TRow) return -1;
    }
    return VRow;
}

/*****************************************************************************

Function: ViewMgr_TRow2VRow

Purpose: Finds a seqalign by pointer inside of the ViewMgr_Global
  
Parameters: salp, the seqalign
            TRow, the row number in the target SeqAlign

Returns: the row in the viewed SeqAlign, -1 on failure

*****************************************************************************/
NLM_EXTERN Int4 ViewMgr_TRow2VRow (SeqAlign *salp, Int4 TRow)
{
    ViewMgr_Global *pGlobal;

    pGlobal = GetAppProperty("ViewMgr");
    if (pGlobal == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_TRow2VRow", -1);
    return ViewMgr_xTRow2VRow(pGlobal, salp, TRow);
}


/* frees a from/to valnode chain */
static void ViewMgr_ClearFromTo(ValNode *Transform)
{
    ValNode *pvn;

    if(Transform == NULL) return;
    for(pvn = (ValNode *)Transform->data.ptrvalue; pvn != NULL;
        pvn = pvn->next) MemFree(pvn->data.ptrvalue);
    ValNodeFree((ValNode *)Transform->data.ptrvalue);
}

Int4 ViewMgr_ClearTrans(SeqAlign *salp)
{
    ViewMgr_Global *pGlobal;
    ValNode *pvn;
    ViewMgr_AlignInfo *pInfo;

    pGlobal = GetAppProperty("ViewMgr");
    if (pGlobal == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_ClearTrans", -1);

    pInfo = ViewMgr_Find(pGlobal, salp);
    if (pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_ClearTrans", -1);

    for(pvn = pInfo->pTransforms; pvn != NULL; pvn = pvn->next)
        ViewMgr_ClearFromTo(pvn);

    ValNodeFree(pInfo->pTransforms);
    return 1;
}

static void ViewMgr_AddFromTo(ValNode *Transform, Int4 From, Int4 To)
{
    ValNode *pvn;
    ViewMgr_FromTo *pFromTo;
    
    if(Transform == NULL) ErrorReturnVoid(SEV_ERROR, "ViewMgr_AddFromTo");
    for(pvn = (ValNode *)Transform->data.ptrvalue; pvn != NULL;
    pvn = pvn->next) {
        pFromTo = pvn->data.ptrvalue;
        if(pFromTo == NULL) continue;
        if(pFromTo->From == From || pFromTo->To == To) return;
    }
    
    pFromTo = MemNew(sizeof(ViewMgr_FromTo));
    pFromTo->From = From;
    pFromTo->To = To;
    
    pvn = (ValNode *)Transform->data.ptrvalue;
    ValNodeAddPointer(&pvn, 0, pFromTo);
}


Int4 ViewMgr_AddTransform(SeqAlign * salp, Int4 Transform, Int4 From, Int4 To)
{
    ValNode *pvn;
    ViewMgr_AlignInfo *pInfo;

    pInfo = ViewMgr_GetInfo(salp);
    if (pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_AddTransform", -1);

    for(pvn = pInfo->pTransforms; pvn != NULL; pvn = pvn->next)
        if(Transform == pvn->choice) break;

    if(pvn == NULL) pvn = ValNodeAddPointer(&pInfo->pTransforms, (Nlm_Int2)Transform,
        NULL);
    ViewMgr_AddFromTo(pvn, From, To);
    return 1;
}


/*****************************************************************************

Function: ViewMgr_GetRow

Purpose: Returns the row that a bioseq viewer is looking at, given the
         viewer's unique UserKey
  
Parameters: pGlobal, the ViewMgr_Global
            UserKey, the userkey of the viewer

Returns: the row, -1 on failure

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_GetRow(DDV_ColorGlobal *pCGlobal, Uint2 UserKey)
{
    ViewMgr_Global *pVGlobal;
    ViewMgr_AlignInfo *pInfo;
    ValNode *pvn;

    pVGlobal = GetAppProperty("ViewMgr");
    if (pVGlobal == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_GetRow", -1);

    pInfo = ViewMgr_Find(pVGlobal, (SeqAlign *)pCGlobal->pObject);
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_GetRow", -1);

    for(pvn = pInfo->pUserKey2Row; pvn != NULL; pvn = pvn->next)
        if(UserKey == pvn->choice) return ViewMgr_xTRow2VRow(pVGlobal,
            (SeqAlign *)pCGlobal->pObject, pvn->data.intvalue);
    ErrorReturn(SEV_ERROR, "ViewMgr_GetRow", -1);
}

/*****************************************************************************

Function: ViewMgr_SetRow

Purpose: Sets the row that a bioseq viewer is looking at, given the
         viewer's unique UserKey
  
Parameters: pGlobal, the DDV_ColorGlobal
            UserKey, the userkey of the viewer
            Row, the row the viewer is looking at

Returns: 1 on success, 0 on failure

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_SetRow(DDV_ColorGlobal *pCGlobal, Uint2 UserKey,
                               Int4 Row)
{
    ViewMgr_Global *pVGlobal;
    ViewMgr_AlignInfo *pInfo;

    pVGlobal = GetAppProperty("ViewMgr");
    if (pVGlobal == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_SetRow", -1);

    pInfo = ViewMgr_Find(pVGlobal, (SeqAlign *)pCGlobal->pObject);
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_SetRow", -1);

    ValNodeAddInt(&pInfo->pUserKey2Row, UserKey, ViewMgr_xVRow2TRow(pVGlobal,
            (SeqAlign *)pCGlobal->pObject, Row));

    return 1;
}

/*****************************************************************************

Function: ViewMgr_FreeInfo

Purpose: Free a ViewMgr_AlignInfo
  
Parameters: pInfo, to be freed

Returns: 1 on success

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_FreeInfo(ViewMgr_AlignInfo *pInfo)
{
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_FreeInfo", -1);
    SeqAlignListFree(pInfo->pTarget);
    SeqAlignListFree(pInfo->pBegin);
    SeqAlignListFree(pInfo->pBeginIndexed);
    ValNodeFree(pInfo->pHiddenRows);
    ValNodeFree(pInfo->pDeleteRows);
    /*ViewMgr_ClearTrans(pInfo->pTransforms);*/
    MemFree(pInfo);
    return 1;
}


/*****************************************************************************

Function: ViewMgr_RemoveSA

Purpose: Takes the information about a seqalign out of the ViewMgr
  
Parameters: salp, the seqalign to remove

Returns: 1 on success

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_RemoveSA(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;
    ValNode *pvn, *pvnPrevious = NULL;
    ViewMgr_Global *pGlobal;

    pGlobal = GetAppProperty("ViewMgr");
    if(pGlobal == NULL) return -1;
    for(pvn = pGlobal->pAlignList; pvn != NULL; pvn = pvn->next) {
        pInfo = (ViewMgr_AlignInfo *)pvn->data.ptrvalue;
        if(pInfo == NULL) continue;
        if(pInfo->pView == salp) break;
        pvnPrevious = pvn;
    }
    
    if(pvn == NULL) return -1;
    if(pvn == pGlobal->pAlignList) {
        ViewMgr_FreeInfo(pInfo);
        if(pvn->next != NULL) pGlobal->pAlignList = pvn->next;
        else pGlobal->pAlignList = NULL;
        MemFree(pvn);
    }
    else {
        pvnPrevious->next = pvn->next;
        ViewMgr_FreeInfo(pInfo);
        MemFree(pvn);
    }
    return 1;
}


/*****************************************************************************

Function: ViewMgr_New

Purpose: Creates a new View Manager global for tracking SeqAligns
  
Returns: the new global

*****************************************************************************/

NLM_EXTERN ViewMgr_Global * ViewMgr_New(void)
{
    ViewMgr_Global *pGlobal;

    pGlobal = GetAppProperty("ViewMgr");
    if (pGlobal == NULL) {
        pGlobal = MemNew(sizeof(ViewMgr_Global));
        if (pGlobal == NULL) return NULL;
        SetAppProperty("ViewMgr", pGlobal);

        /* set up the registration function and get a userkey */
        ObjMgrProcLoad(OMPROC_VIEW, "ViewMgr Register", "Media", OBJ_SEQALIGN, 0,
            OBJ_SEQALIGN, 0, NULL, ViewMgr_RegisterFunc, 0);
        
        pGlobal->userkey = OMGetNextUserKey();
    }
    return pGlobal;
}

/*****************************************************************************

Function: ViewMgr_GetTarget

Purpose: return the original *indexed* SeqAlign given the view seqalign
  
*****************************************************************************/
NLM_EXTERN SeqAlign * ViewMgr_GetTarget(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return NULL;

    return pInfo->pTarget;
}

/*****************************************************************************

Function: ViewMgr_GetBegin

Purpose: return the original SeqAlign given the view seqalign
  
*****************************************************************************/
NLM_EXTERN SeqAlign * ViewMgr_GetBegin(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return NULL;

    return pInfo->pBegin;
}

/*****************************************************************************

Function: ViewMgr_SetBegin

Purpose: make pNewBegin the original SeqAlign
  
*****************************************************************************/
NLM_EXTERN SeqAlign * ViewMgr_SetBegin(SeqAlign *salp, SeqAlign *pNewBegin,
                                       Boolean Neat, Boolean Intersect)
{
    ViewMgr_AlignInfo *pInfo;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return NULL;
    SeqAlignListFree(pInfo->pBegin);
    pInfo->pBegin = pNewBegin;
    pInfo->Neat = Neat;
    pInfo->Intersect = Intersect;
    return pInfo->pBegin;
}

/*****************************************************************************

Function: ViewMgr_GetBeginIndexed

Purpose: return the original SeqAlign given the view seqalign.  This is
an indexed copy of the original SeqAlign.
  
*****************************************************************************/
NLM_EXTERN SeqAlign * ViewMgr_GetBeginIndexed(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return NULL;

    if(!pInfo->BeginIndexed) {
        if(pInfo->Intersect) {
            AlnMgrMakeMultByIntersectOnMaster(pInfo->pBeginIndexed, TRUE);
        }
        else {
            AlnMgrIndexSeqAlign(pInfo->pBeginIndexed);
        }
        pInfo->BeginIndexed = TRUE;
    }
    return pInfo->pBeginIndexed;
}


/*****************************************************************************

Function: ViewMgr_IsNeat

Purpose: is the seqalign neatly indexed?
  
*****************************************************************************/
NLM_EXTERN Boolean ViewMgr_IsNeat(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;
    pInfo = ViewMgr_GetInfo(salp);
    if(!pInfo) return FALSE;
    
    if(pInfo->Neat) return TRUE;
    return FALSE;
}


/*****************************************************************************

Function: ViewMgr_IsIBM

Purpose: is the seqalign IBMed?
  
*****************************************************************************/
NLM_EXTERN Boolean ViewMgr_IsIBM(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;
    pInfo = ViewMgr_GetInfo(salp);
    if(!pInfo) return FALSE;
    
    if(pInfo->Intersect) return TRUE;
    return FALSE;
}


/*****************************************************************************

Function: ViewMgr_Delete

Purpose: Creates a new View Manager global for tracking SeqAligns
  
Returns: the new global

*****************************************************************************/

NLM_EXTERN ViewMgr_Global * ViewMgr_Delete(ViewMgr_Global *pGlobal)
{
    ValNode * pvn;
    if (pGlobal != NULL) {
        for(pvn = pGlobal->pAlignList; pvn != NULL; pvn = pvn->next) {
            ViewMgr_FreeInfo((ViewMgr_AlignInfo *)pvn->data.ptrvalue);
        }

        ValNodeFree(pGlobal->pAlignList);
        pGlobal = MemFree(pGlobal);
    }
    return(pGlobal);
}

static void ViewMgr_Swap(SeqAlign *salp1, SeqAlign *salp2)
{
    void *pSwap;
    Int4 lSwap;
    GatherIndex gSwap;
    
    lSwap = (Int4)salp1->type;
    salp1->type = salp2->type;
    salp2->type = (Uint1)lSwap;
    
    lSwap = (Int4)salp1->segtype;
    salp1->segtype = salp2->segtype;
    salp2->segtype = (Uint1)lSwap;
    
    lSwap = (Int4)salp1->dim;
    salp1->dim = salp2->dim;
    salp2->dim = (Int2)lSwap;
    
    pSwap = (void *)salp1->score;
    salp1->score = salp2->score;
    salp2->score = (ScorePtr)pSwap;
    
    pSwap = (void *)salp1->segs;
    salp1->segs = salp2->segs;
    salp2->segs = pSwap;
    
    pSwap = (void *)salp1->next;
    salp1->next = salp2->next;
    salp2->next = (SeqAlign *)pSwap;
    
    pSwap = (void *)salp1->bounds;
    salp1->bounds = salp2->bounds;
    salp2->bounds = (SeqLocPtr)pSwap;
    
    pSwap = (void *)salp1->master;
    salp1->master = salp2->master;
    salp2->master = (SeqIdPtr)pSwap;
    
    pSwap = (void *)salp1->saip;
    salp1->saip = salp2->saip;
    salp2->saip = (SeqAlignIndexPtr)pSwap;
    
    MemCpy((void *)&gSwap, (void *)&salp1->idx, sizeof(GatherIndex));
    MemCpy((void *)&salp1->idx, (void *)&salp2->idx, sizeof(GatherIndex));
    MemCpy((void *)&salp2->idx, (void *)&gSwap, sizeof(GatherIndex));
    
    lSwap = (Int4)salp1->alignID;
    salp1->alignID = salp2->alignID;
    salp2->alignID = (Uint2)lSwap;
}

static int LIBCALLBACK ViewMgr_CmpInt(void *int1, void *int2)
{
    if(int1 && int2) return *(Int4 *)int2 - *(Int4 *)int1;
    else return 0;
}

static Int4 ViewMgr_Massage(ViewMgr_AlignInfo *pInfo)
{
    Int4 *throwaway, len, i;
    ValNode *pvn;
    
    SeqAlignListFree(pInfo->pTarget);
    pInfo->pTarget = NULL;
    SeqAlignListFree(pInfo->pBeginIndexed);
    pInfo->pBeginIndexed = NULL;
    pInfo->BeginIndexed = FALSE;

    if(pInfo->Neat) {
        if(!AlnMgrNeatlyIndex(pInfo->pBegin)) 
            ErrorReturn(SEV_ERROR, "ViewMgr_Massage", -1);
        /* delete neat rows */
        len = ValNodeLen(pInfo->pHiddenRows);
        pInfo->pBeginIndexed = SeqAlignListDup(pInfo->pBegin);
        AlnMgrNeatlyIndex(pInfo->pBeginIndexed);
        if(len > 0) {
            throwaway = MemNew(sizeof(Int4)*len);
            for(pvn = pInfo->pHiddenRows, i = 0; pvn != NULL; pvn = pvn->next, i++) 
                throwaway[i] = pvn->data.intvalue;
            pInfo->pTarget = AlnMgrTossNeatRows(pInfo->pBegin, throwaway, len);
            MemFree(throwaway);
        }
        else { 
            pInfo->pTarget = SeqAlignListDup(pInfo->pBegin);
            AlnMgrNeatlyIndex(pInfo->pTarget);
        }
    }
    else {
        pInfo->pTarget = SeqAlignListDup(pInfo->pBegin);
        pInfo->pBeginIndexed = SeqAlignListDup(pInfo->pBegin);
    }
    
    if(pInfo->Intersect) {
        AlnMgrMakeMultByIntersectOnMaster(pInfo->pTarget, TRUE);
    }
    else {
        AlnMgrIndexSeqAlign(pInfo->pTarget);

        /* delete unneat rows */
        if(!pInfo->Neat) {
            len = ValNodeLen(pInfo->pHiddenRows);
            if(len > 0) {
                throwaway = MemNew(sizeof(Int4)*len);
                for(pvn = pInfo->pHiddenRows, i = 0; pvn != NULL; pvn = pvn->next, i++) 
                    throwaway[i] = pvn->data.intvalue;
                HeapSort(throwaway, len, sizeof(Int4), ViewMgr_CmpInt);
                for(i = 0; i < len; i++) {
                    AlnMgrDeleteNthRow(pInfo->pTarget, throwaway[i]);
                    AlnMgrReIndexSeqAlign(pInfo->pTarget);
                }
                MemFree(throwaway);
            }
        }
    }
    return 1;
}

/*****************************************************************************

Function: ViewMgr_MakeMultiple

Purpose: Turns a pairwise seqalign into a multiple
  
Parameters: salp, the seqalign to be turned into a multiple

Returns: 1 on success, 0 otherwise

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_MakeMultiple(SeqAlign *salp)
{
    ViewMgr_AlignInfo *pInfo;
    SeqAlign *pSeqAlign;

    if(salp == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_MakeMultiple", -1);
    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_MakeMultiple", -1);

    pSeqAlign = AlnMgrGetSubAlign(ViewMgr_GetBeginIndexed(salp), NULL, 0, -1);

    if(pSeqAlign == NULL) return 0;
    pInfo->pBegin = pSeqAlign;
    /*    ViewMgr_Massage(pInfo); */

    /*    AlnMgrCopyIndexedParentIntoSap(pInfo->pTarget, salp); */
    
    return ViewMgr_Update(salp);
}


/*****************************************************************************

Function: ViewMgr_Attach

Purpose:  Adds the seqalign to the global and starts tracking it
  
Parameters: salp, a seqalign that *isn't* indexed.
            Intersect, true if intersect-on-master should be applied
            Neat, true if AlnMgrNeatlyIndex should be applied
            entityID, itemID of object passed in.  If entityID == 0,
                then salp is used to look up the values.

Returns: 0 on error, 1 otherwise

Notes: will index the seqalign

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_Attach(SeqAlign *salp, Boolean Neat,
                               Boolean Intersect, Uint2 entityID,
                               Uint4 itemID)
{
    ViewMgr_AlignInfo *pInfo;
    ViewMgr_Global *pGlobal;

    pGlobal = ViewMgr_New();
    if(pGlobal == NULL || salp == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_Attach", -1);

    /* don't bother attaching it again */
    if(ViewMgr_Find(pGlobal, salp) != NULL) return 1;

    if (entityID == 0) {
        entityID = ObjMgrGetEntityIDForPointer(salp);
        itemID =
            GetItemIDGivenPointer(entityID, OBJ_SEQALIGN, (void *) salp);
    }

    /* check to see if indexed, if it is, return 
    if(AlnMgrCheckAlignForParent(salp) > 0) 
        ErrorReturn(SEV_ERROR, "ViewMgr_Attach", -1);*/
    
    /* add a message func.  use AppProperty hack to pass values */
    GatherSpecificProcLaunch(0, "ViewMgr Register", OMPROC_VIEW,
        FALSE, entityID, itemID, OBJ_SEQALIGN);

    pInfo = MemNew(sizeof(ViewMgr_AlignInfo));
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_Attach", -1);

    pInfo->Neat = Neat;
    pInfo->Intersect = Intersect;
    pInfo->pBegin = SeqAlignListDup(salp);
    ViewMgr_Massage(pInfo);

    AlnMgrCopyIndexedParentIntoSap(pInfo->pTarget, salp);
    
    pInfo->pView = salp;
    pInfo->entityID = entityID;
    
    ValNodeAddPointer(&pGlobal->pAlignList, 0, pInfo);

    return 1;
}


/*****************************************************************************

Function: ViewMgr_Add

Purpose: Add a seqalign to a seqalign already in the viewmgr
  
Parameters: salp, the seqalign to be added to.  Must be attached to viewmgr
            salpAdd, the seqalign to be added.  should not be indexed.

Returns: 1 on success

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_Add(SeqAlign *salp, SeqAlign *salpAdd)
{
    ViewMgr_AlignInfo *pInfo;
    SeqAlign *pSeqAlign;

    if(salp == NULL || salpAdd == NULL) 
        ErrorReturn(SEV_ERROR, "ViewMgr_Add", -1);
    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) ErrorReturn(SEV_ERROR, "ViewMgr_Add", -1);

    /* check indexed state */
/*    if (AlnMgrCheckAlignForParent(salp) < 0 ||
        AlnMgrCheckAlignForParent(salpAdd) >= 0)
        ErrorReturn(SEV_ERROR, "ViewMgr_Add", -1);*/

    for(pSeqAlign = pInfo->pBegin; pSeqAlign->next != NULL;
        pSeqAlign = pSeqAlign->next);

    pSeqAlign->next = salpAdd;
    
    return ViewMgr_Update(salp);
}


/*****************************************************************************

Function: ViewMgr_SetHidden

Purpose: Set a row to contribute / not contribute to the seqalign
  
Parameters: salp, the seqalign to be shown/hid
            Row, the row to show or hide
            Hidden, 

Returns: 1 on success, 0 on setup failure, -1 if the requested action can't
         be done.

*****************************************************************************/

NLM_EXTERN Int4 ViewMgr_SetHidden(SeqAlign *salp, Boolean Hidden, Int4 Row)
{
    ViewMgr_AlignInfo *pInfo;
    ValNode *pvn, *pvnPrev;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return 0;
    /* check to see if indexed */
    if (AlnMgrCheckAlignForParent(salp) < 0) return 0;
    if(pInfo->Intersect && !pInfo->Neat) return 0;  /* can't handle this case */
    if (Hidden) {
        /*	if(AlnMgrGetNumRows(pInfo->pBegin) - ValNodeLen(pInfo->pHiddenRows) < 3) return -1;  not indexed */
        for(pvn = pInfo->pHiddenRows; pvn != NULL; pvn = pvn->next)
            if(pvn->data.intvalue == Row) break;
        if(pvn == NULL) {
            ValNodeAddInt(&pInfo->pHiddenRows, (Nlm_Int2)Row, Row);
            return 1;
            }
        else return -1;
    }
    else {
        for(pvnPrev = pvn = pInfo->pHiddenRows; pvn != NULL; pvn = pvn->next) {
            if(pvn->data.intvalue == Row) {
                if(pvnPrev == pvn) pInfo->pHiddenRows = pvn->next;
                else pvnPrev->next = pvn->next;
                MemFree(pvn);
                return 1;
            }
            else pvnPrev = pvn;
        }
        return -1;
    }
    
    return 0;
}


NLM_EXTERN Int4 ViewMgr_Update(SeqAlign *salp) {
  ViewMgr_Update2(salp, FALSE);
}


/*****************************************************************************

Function: ViewMgr_Update

Purpose: Copies the target into the viewed seqalign, performs all the 
        transformations, then sends an update message
  
Parameters: salp, the seqalign to work on

Returns: 1 on success

*****************************************************************************/
NLM_EXTERN Int4 ViewMgr_Update2(SeqAlign *salp, Boolean Fast)
{
    ViewMgr_AlignInfo *pInfo;
    Uint4 itemID;
    DDVUpdateMSG dum;

    pInfo = ViewMgr_GetInfo(salp);
    if(pInfo == NULL) return 0;

    ViewMgr_Massage(pInfo);

    AlnMgrCopyIndexedParentIntoSap(pInfo->pTarget, salp);

    if (!Fast) {
      itemID =
          GetItemIDGivenPointer(pInfo->entityID, OBJ_SEQALIGN, (void *) salp);

      /* first tell everyone to recolor the entity */
      MemSet(&dum, 0, sizeof(DDVUpdateMSG));
      dum.data = NULL;
      dum.type = UPDATE_TYPE_RECOLOR;

      ObjMgrSendProcMsg(OM_MSG_UPDATE, pInfo->entityID, itemID, OBJ_SEQALIGN,
				  0, 0, (Pointer)&dum);

      /* then redraw the entity */
      MemSet(&dum, 0, sizeof(DDVUpdateMSG));
      dum.data = NULL;
      dum.type = UPDATE_TYPE_VIEWMGR;

      ObjMgrSendProcMsg(OM_MSG_UPDATE, pInfo->entityID, itemID, OBJ_SEQALIGN,
				  0, 0, (Pointer)&dum);
    }

    return 1;
}


/*****************************************************************************

Function: ViewMgr_MsgFunc

Purpose: this listens to the entity the seqalign sits in.  If there is a
         request that the object be deleted and the viewmgr has the last
         messagefunc, the object is deleted.
  
*****************************************************************************/

static Int2 LIBCALLBACK ViewMgr_MsgFunc(OMMsgStructPtr ommsp)
{
    OMUserDataPtr omudp = NULL;
    Int4 Count = 0;
    OMMessageFunc messagefunc = NULL;
    ViewMgr_Global *pGlobal;
    ValNode *pvn;
    ViewMgr_AlignInfo *pInfo;

    omudp = (OMUserDataPtr) (ommsp->omuserdata);

    switch (ommsp->message) {
    case OM_MSG_UPDATE:
        break;

    case OM_MSG_DEL:
        /* kill the object if there are no more message funcs */
        for(;omudp != NULL; omudp = omudp->next) {
            if(omudp->messagefunc != NULL) {
                Count++;
                messagefunc = omudp->messagefunc;
            }
        }
        if(Count == 1 && messagefunc == ViewMgr_MsgFunc) {
            ObjMgrFreeUserData(ommsp->entityID, 0, 0, 0);  /* deadlock? */
            pGlobal = GetAppProperty("ViewMgr");
            if(pGlobal != NULL) {
                for(pvn = pGlobal->pAlignList; pvn != NULL; pvn = pvn->next) {
                    pInfo = (ViewMgr_AlignInfo *)pvn->data.ptrvalue;
                    if(pInfo == NULL) continue;
                    if(pInfo->entityID == ommsp->entityID) {
                        ViewMgr_RemoveSA(pInfo->pView);
                        break;
                    }
                }
                if(pGlobal->pAlignList == NULL) {
                    ViewMgr_Delete(pGlobal);
                    RemoveAppProperty("ViewMgr");
                }
            }
        }
        return OM_MSG_RET_OK;
        break;

    default:
        break;      

    }
return OM_MSG_RET_OK;
}


/*****************************************************************************

Function: ViewMgr_RegisterFunc

Purpose: Adds the ViewMgr message function to the seqalign
  
*****************************************************************************/

NLM_EXTERN Int2 LIBCALLBACK ViewMgr_RegisterFunc(Pointer data)
{
    OMProcControlPtr ompcp = NULL;
    OMUserDataPtr omudp = NULL;
    ViewMgr_Global *pGlobal;

    ompcp = (OMProcControlPtr) data;
    if (ompcp == NULL) {
        Message(MSG_ERROR, "ViewMgr: Data Null 1");
        return OM_MSG_RET_ERROR;
    }
    
    if(ompcp->proc == NULL) {
        Message(MSG_ERROR, "ViewMgr: Data Null 2");
        return OM_MSG_RET_ERROR;
    }

    switch (ompcp->input_itemtype) {
    case OBJ_SEQALIGN:
        break;
    default:
        return OM_MSG_RET_ERROR;
    }

    if (ompcp->input_data == NULL) {
        Message(MSG_ERROR, "ViewMgr: Null Data 3");
        return OM_MSG_RET_ERROR;
    }
    pGlobal = GetAppProperty("ViewMgr");
    if(pGlobal == NULL) return OM_MSG_RET_ERROR;

    pGlobal->procid = ompcp->proc->procid;
    omudp = ObjMgrAddUserData(ompcp->input_entityID, ompcp->proc->procid,
                              OMPROC_EDIT, pGlobal->userkey);
    if (omudp != NULL) {
        omudp->messagefunc = ViewMgr_MsgFunc;
    }

    return OM_MSG_RET_OK;
}