File: dotmatrx.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (567 lines) | stat: -rw-r--r-- 13,747 bytes parent folder | download | duplicates (13)
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
/* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  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 have not placed any restriction on its use or reproduction.
*
*  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.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================*/

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

File name: dotmatrx.c

Author: Tom Madden

*****************************************************************************/
#include <dotmatrx.h>
#include <objmgr.h>
#include <dlogutil.h>
#include <vibrant.h>
#include <picture.h>
#include <viewer.h>
#include <objseq.h>
#include <objsset.h>
#include <salsap.h>
#include <blast.h>
#include <salpedit.h>

typedef struct dotmatrixform {
  FEATURE_FORM_BLOCK
  PopuP        scale;
  GrouP        showLabels;
  VieweR       vwr;
  SegmenT      pict;
  DotMatrixAlignmentPtr	dmap;
  int (LIBCALLBACK *user_callback)PROTO((SeqAlignPtr seqalign));
  Boolean      scaleNotCalculated;
} DotMatForm, PNTR DotMatFormPtr;

#define MAXZOOMSCALEVAL 22

static Int4  zoomScaleVal [MAXZOOMSCALEVAL] = {
  1L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 20L,
  30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 200L, 500L
};


static Boolean DeselectSegment (SegmenT seg, PrimitivE prim, Uint2 segID,
                                Uint2 primID, Uint2 primCt, VoidPtr userdata)

{
  DotMatFormPtr  dfp;

  dfp = (DotMatFormPtr) userdata;
  if (dfp == NULL) 
	return FALSE;
  HighlightSegment (dfp->vwr, seg, PLAIN_SEGMENT);
  return TRUE;
}

static SeqAlignPtr FindSeqAlign(DotMatFormPtr dfp, Int4 segID)

{
	DenseDiagPtr ddp, ddp_new;
	DotMatrixAlignmentPtr dmap;
	SeqAlignPtr seqalign, new_seqalign=NULL;
	Int4 index=0;

	dmap = dfp->dmap;
	while (dmap)
	{
		if (dmap->sap->segtype == 2)
		{
			seqalign = dmap->sap;
		}
		else
		{	/* discontinuous */
			seqalign = dmap->sap->segs;
		}
		
	  	while (seqalign != NULL) 
	  	{
	    		index++;
			if (index == segID)
			{
				break;
			}
	    		seqalign = seqalign->next;
		}
		
		if (seqalign)
		{
			if (seqalign->segtype == 1)
			{
				new_seqalign = SeqAlignNew();
				new_seqalign->type = 2;
				new_seqalign->segtype = 1;
				ddp = seqalign->segs;
				new_seqalign->segs = ddp_new = DenseDiagDup(ddp);	
				while (ddp->next)
				{
					ddp_new->next = DenseDiagDup(ddp->next);
					ddp = ddp->next;
					ddp_new = ddp_new->next;
				}
			}
			else if (seqalign->segtype == 2)
			{
				new_seqalign = SeqAlignDup(seqalign);
			}
		}
		dmap = dmap->next;
	}

	return new_seqalign;
}

static void ClickDotMatrix (VieweR vwr, SegmenT seg, PoinT pt)

{
  DotMatFormPtr  dfp;
  Int1           highlight;
  SegmenT        new_seg;
  PrimitivE      prim;
  SeqAlignPtr	 seqalign;
  Uint2          primCT;
  Uint2          primID;
  Uint2          segID;

	dfp = (DotMatFormPtr) GetObjectExtra (vwr);
	new_seg = FindSegment (vwr, pt, &segID, &primID, &primCT);
	if (new_seg == NULL) 
	{
		ExploreSegment(seg, (Pointer) dfp, DeselectSegment);
	} 
	else 
	{
		prim = GetPrimitive (new_seg, primCT);
		GetPrimDrawAttribute (prim, NULL, NULL, NULL, NULL, NULL, &highlight);
		if (highlight != PLAIN_SEGMENT) 
		{
			HighlightSegment (vwr, new_seg, PLAIN_SEGMENT);
		} 
		else 
		{
			HighlightSegment (vwr, new_seg, FILL_CONTENTS);
			seqalign = FindSeqAlign(dfp, segID);
			if (dblClick && dfp->user_callback)
				dfp->user_callback(seqalign);
		}
 	}
}
static SegmenT 
CreateDotMatrixPic (DotMatrixAlignmentPtr dmap, Boolean showLabels)

{
  Char          buffer [50];
  DenseDiagPtr  ddp;
  DenseSegPtr	dsp;
  SeqAlignPtr   sap;
  Int4          index, index1;
  SegmenT       seg;
  Int4          x;
  Int4          y;
  SegmenT 		pict;

  if (dmap == NULL || dmap->sap == NULL) 
	return NULL;
  pict = CreatePicture ();

  while (dmap)
  {
	  sap = dmap->sap;
	  if (sap == NULL)
		break;
	  if (sap->segtype != 2)
	  {	/* discontinuous */
	  	sap = sap->segs;
	  }
	  index = 0;
	  while (sap != NULL) 
	  {
	    seg = CreateSegment (pict, index + 1, 0);
	
	    AddAttribute (seg, COLOR_ATT, dmap->color, 0, 0, 0, 0);
	
	    if (sap->segtype == 1)
	    {
	    	ddp = sap->segs;
	    	while (ddp != NULL) 
	    	{
	    		AddLine (seg, ddp->starts [0], ddp->starts [1], ddp->starts [0] + ddp->len, ddp->starts [1] + ddp->len, FALSE, 0);
		
	      		if (showLabels) 
	      		{
	       	 		x = ddp->starts [0];
	        		y = ddp->starts [1];
				sprintf (buffer, "(%ld, %ld)", (long) x + 1, (long) y + 1);
	       			AddLabel (seg, x, y, buffer, SMALL_TEXT, 0, LOWER_RIGHT, 0);
	
	        		x = ddp->starts [0] + ddp->len;
	        		y = ddp->starts [1] + ddp->len;
	        		sprintf (buffer, "(%ld, %ld)", (long) x, (long) y);
	        		AddLabel (seg, x, y, buffer, SMALL_TEXT, 0, UPPER_RIGHT, 0);
	      		}
	
	      		ddp = ddp->next;
	    	}
	    }
	    else if (sap->segtype == 2)
	    {
		dsp = sap->segs;
		for (index1=0; index1<dsp->numseg; index1++)
		{
			if (dsp->starts[2*index1] != -1 && dsp->starts[2*index1+1] != -1)
			{
	    			AddLine (seg, dsp->starts[2*index1], dsp->starts[2*index1+1], dsp->starts[2*index1] + dsp->lens[index1], dsp->starts[2*index1+1] + dsp->lens[index1], FALSE, 0);
	      			if (showLabels) 
	      			{
					x = dsp->starts[2*index1];
					y = dsp->starts[2*index1+1];
					sprintf (buffer, "(%ld, %ld)", (long) x + 1, (long) y + 1);
	       				AddLabel (seg, x, y, buffer, SMALL_TEXT, 0, LOWER_RIGHT, 0);
					x = dsp->starts[2*index1] + dsp->lens[index1];
					y = dsp->starts[2*index1+1] + dsp->lens[index1];
					sprintf (buffer, "(%ld, %ld)", (long) x + 1, (long) y + 1);
	       				AddLabel (seg, x, y, buffer, SMALL_TEXT, 0, UPPER_RIGHT, 0);
				}
			}
		}
	    }
	    index++;
	    sap = sap->next;
  	}
	dmap = dmap->next;
  }
  return pict;
}

static void MakeDotMatrixPicture (DotMatFormPtr dfp)

{
  Boolean       showLabels;

  if (dfp == NULL || dfp->dmap == NULL) 
	return;

  showLabels = (Boolean) (GetValue (dfp->showLabels) == 1);
  dfp->pict = CreateDotMatrixPic (dfp->dmap, showLabels);

}
static void RepopulateDotMatrixViewer (DotMatFormPtr dfp)

{
  Int2  index;
  Int4  scaleX;
  Int4  scaleY;
  Char  str [16];

  if (dfp == NULL) 
	return;

  Reset (dfp->vwr);
  dfp->pict = DeletePicture (dfp->pict);
  Update ();
  MakeDotMatrixPicture (dfp);
  if (dfp->scaleNotCalculated) 
  {
    SafeHide (dfp->scale);
    Reset (dfp->scale);
    for (index=1; index<=20; index++) 
    {
      sprintf (str, "%ld", (long) (zoomScaleVal [index]));
      PopupItem (dfp->scale, str);
    }
    SetValue (dfp->scale, 5);
    dfp->scaleNotCalculated = FALSE;
  }
  SafeShow (dfp->scale);
  index = GetValue (dfp->scale);
  if (index < MAXZOOMSCALEVAL && index > 0) 
  {
    scaleX = zoomScaleVal [index];
  } 
  else 
  {
    scaleX = 1;
  }
  scaleY = scaleX;
  AttachPicture (dfp->vwr, dfp->pict, INT4_MIN, INT4_MAX, UPPER_LEFT, scaleX, scaleY, NULL);
  SetViewerProcs (dfp->vwr, ClickDotMatrix, NULL, NULL, NULL);
  Update ();
}

static void ChangeScale (PopuP p)

{
  DotMatFormPtr  dfp;

  dfp = (DotMatFormPtr) GetObjectExtra (p);
  if (dfp != NULL) 
  {
    RepopulateDotMatrixViewer (dfp);
  }
}

static void ChangeLabels (GrouP g)

{
  DotMatFormPtr  dfp;

  dfp = (DotMatFormPtr) GetObjectExtra (g);
  if (dfp != NULL) 
  {
    RepopulateDotMatrixViewer (dfp);
  }
}

static void ResizeDotMatrixForm (WindoW w)

{
  DotMatFormPtr  dfp;
  Int2           height;
  RecT           r;
  RecT           s;
  Int2           width;

  dfp = (DotMatFormPtr) GetObjectExtra (w);
  if (dfp != NULL) 
  {
    ObjectRect (w, &r);
    width = r.right - r.left;
    height = r.bottom - r.top;
    if (dfp->vwr != NULL) 
    {
      GetPosition (dfp->vwr, &s);
      s.right = width - s.left;
      s.bottom = height - s.left;
      SetPosition (dfp->vwr, &s);
      AdjustPrnt (dfp->vwr, &s, FALSE);
      if (Visible (dfp->vwr) && AllParentsVisible (dfp->vwr)) 
      {
      		ViewerWasResized (dfp->vwr);
      }
    }
    Update ();
  }
}

static void CleanupDotMatrixForm (GraphiC g, VoidPtr data)

{
  DotMatFormPtr  dfp;
  DotMatrixAlignmentPtr dmap;

  dfp = (DotMatFormPtr) data;
  if (dfp != NULL) 
  {
    dfp->pict = DeletePicture (dfp->pict);
    dmap = dfp->dmap;
    while (dmap)
    {
    	dmap->sap = SeqAlignFree (dmap->sap);
	dmap = dmap->next;
    }
  }
  StdCleanupFormProc (g, data);
}

static void HideDotMatrix (ButtoN i)

{
	WindoW w;

	w = (WindoW)ParentWindow (i);
	Remove(w);
	
}

DotMatrixAlignmentPtr LIBCALL
DotMatrixAlignmentNew (SeqAlignPtr sap, Uint1Ptr color, DotMatrixAlignmentPtr PNTR oldp)

{
	DotMatrixAlignmentPtr new, old;

	new = MemNew(sizeof(DotMatrixAlignment));

	if (new == NULL || oldp == NULL)
		return NULL;

	new->sap = sap;
	new->color = color;

	if (*oldp)
	{
		old = *oldp;
		while (old->next)
			old = old->next;

		old->next = new;
	}
	else
	{
		*oldp = new;
	}
	return new;
}

#define BLAST_DOTMATRIX_EXTENT 600	/* Size of screen. */

ForM LIBCALL CreateDotMatrixForm (SeqAlignPtr sap, int (LIBCALLBACK *callback)PROTO((SeqAlignPtr seqalign)))

{
	DotMatrixAlignmentPtr dmap, tmp;

	tmp = NULL;
	dmap = DotMatrixAlignmentNew(sap, NULL, &tmp);
	return CreateDotMatrixFormEx(dmap, callback);
}

ForM LIBCALL CreateDotMatrixFormEx (DotMatrixAlignmentPtr dmap, int (LIBCALLBACK *callback)PROTO((SeqAlignPtr seqalign)))

{
  DotMatFormPtr  dfp;
  GrouP          g;
  PrompT         p1;
  PrompT         p2;
  GrouP          s, x;
  WindoW         w;

  w = NULL;
  dfp = (DotMatFormPtr) MemNew (sizeof (DotMatForm));
  if (dfp != NULL) 
  {
    dfp->dmap = dmap;
    dfp->user_callback = callback;

    w = DocumentWindow (-40, -33, -10, -10, "Dot Matrix Display",
                        StdCloseWindowProc, ResizeDotMatrixForm);
    SetObjectExtra (w, dfp, CleanupDotMatrixForm);
    dfp->form = (ForM) w;

    x = HiddenGroup (w, 0, 1, NULL);
    StaticPrompt (x, "Dot Matrix showing the BLAST hits between 2 sequences", 0, 0, programFont, 'l');

    s = HiddenGroup (w, 5, 0, NULL);
    SetGroupSpacing (s, 7, 2);

    p1 = StaticPrompt (s, "Labels", 0, 0, programFont, 'l');
    dfp->showLabels = HiddenGroup (s, 3, 0, ChangeLabels);
    SetObjectExtra (dfp->showLabels, dfp, NULL);
    RadioButton (dfp->showLabels, "Show");
    RadioButton (dfp->showLabels, "Hide");
    SetValue (dfp->showLabels, TRUE);

    p2 = StaticPrompt (s, "Scale", 0, popupMenuHeight, programFont, 'l');
    dfp->scale = PopupList (s, TRUE, ChangeScale);
    SetObjectExtra (dfp->scale, dfp, NULL);

    AlignObjects (ALIGN_MIDDLE, (HANDLE) p1, (HANDLE) dfp->showLabels, (HANDLE) p2, (HANDLE) dfp->scale, NULL);

    x = HiddenGroup (w, 0, 2, NULL);
    StaticPrompt (x, "Decrease scale to zoom in", 0, 0, programFont, 'l');
    StaticPrompt (x, "Click on diagonals to launch the alignment editor", 0, 0, programFont, 'l');

    g = HiddenGroup (w, 2, 0, NULL);
    dfp->vwr = CreateViewer (g, BLAST_DOTMATRIX_EXTENT, BLAST_DOTMATRIX_EXTENT, TRUE, TRUE);
    SetObjectExtra (dfp->vwr, dfp, NULL);
    dfp->pict = NULL;
    dfp->scaleNotCalculated = TRUE;

    PushButton (w, "Dismiss", HideDotMatrix );

    RealizeWindow (w);
    RepopulateDotMatrixViewer (dfp);
  }
  return (ForM) w;
}

Int2 LIBCALL DotMatrixSearch(SeqIdPtr sip1, SeqIdPtr sip2, int (LIBCALLBACK *callback)PROTO((SeqAlignPtr seqalign)))

{
	BioseqPtr query_bsp, subject_bsp;
	BLAST_OptionsBlkPtr options;
	Boolean is_na;
	SeqAlignPtr seqalign;
	WindoW  window;


	query_bsp = BioseqLockById(sip1);
	subject_bsp = BioseqLockById(sip2);
	if (query_bsp == NULL || subject_bsp == NULL)
	{
                ErrPostEx(SEV_WARNING, 0, 0, "Unable to obtain sequences");
                return 1;
	}

	if (ISA_na(query_bsp->mol) != ISA_na(subject_bsp->mol))
	{
                ErrPostEx(SEV_WARNING, 0, 0, "Sequences are of different types");
                return 1;
	}

	is_na = ISA_na(query_bsp->mol);

	options = BLASTOptionNew((is_na == TRUE) ? "blastn":"blastp", TRUE);

        seqalign = BlastTwoSequences(query_bsp, subject_bsp, NULL, options);

        if (seqalign == NULL)
        {
                ErrPostEx(SEV_WARNING, 0, 0, "No hits found");
                return 0;
        }

        window = (WindoW) CreateDotMatrixForm(seqalign, callback);

        Show(window);

	BioseqUnlockById(sip1);
	BioseqUnlockById(sip2);

	return 0;
}

Int2 LIBCALLBACK DotMatrixGenFunc (Pointer data)

{
  OMProcControlPtr  ompcp;
  SeqAlignPtr       sap;
  WindoW            w;

  ompcp = (OMProcControlPtr) data;
  if (ompcp == NULL || ompcp->proc == NULL) 
	return OM_MSG_RET_ERROR;

  sap = NULL;
  switch (ompcp->input_itemtype) 
  {
    case OBJ_SEQALIGN :
      sap = (SeqAlignPtr) ompcp->input_data;
      break;
    case 0 :
      return OM_MSG_RET_ERROR;
    default :
      return OM_MSG_RET_ERROR;
  }
  w = (WindoW) CreateDotMatrixForm (sap, NULL);
  Show (w);
  Select (w);
  return OM_MSG_RET_DONE;
}