File: edExtend.c

package info (click to toggle)
staden 2.0.0%2Bb11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 21,556 kB
  • sloc: ansic: 240,603; tcl: 65,360; cpp: 12,854; makefile: 11,201; sh: 2,952; fortran: 2,033; perl: 63; awk: 46
file content (503 lines) | stat: -rw-r--r-- 10,672 bytes parent folder | download | duplicates (5)
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
/*
 * File: extend.c
 *
 * Author: Simon Dear
 *         MRC Laboratory of Molecular Biology
 *	   Hills Road
 *	   Cambridge CB2 2QH
 *	   United Kingdom
 *
 * Description: For moving cutoffs
 *
 * Created: ?
 * Updated: 16 April 1993
 *
 */

/*
 * Change log:
 *
 *   1/10/91 SD  Added calculateConsensusLength to extend, unextend, undo_unextend
 *   29/4/92 SD  Changes related to general speed up in edUtils.c
 *   18/5/92 SD  Construct (*nc=*++nc) not liked by dec
 */

#include <stdio.h>
#include <stdlib.h>

#include "edUtils.h"
#include "fortran.h"
#include "tagUtils.h"
#include "contigEditor.h"
#include "undo.h"
#include "select.h"
#include "extend.h"

#define OTHER_END(E) ((E)==EXTEND_LEFT ? EXTEND_RIGHT : EXTEND_LEFT)

int adjustMark(EdStruct *xx, int seq, int num_bases, int direction, int end);

/*
 * Handle cut-off adjust
 */
int meta_arrow (EdStruct *xx, int key)
{
    
    int seq,pos;
    int seq_length;
    int end;
    
    /* check in valid edit mode */
    if (!(DBI_flags(xx) & DB_ACCESS_UPDATE)) {
	bell();
	return 1;
    }
    
    seq = xx->cursorSeq;
    pos = xx->cursorPos;
    seq_length = DB_Length(xx,seq);
    
    /*
     * determine which operation is to take place
     */
    if (!seq) {
	/* no operation on consensus */
	if (key==EXTEND_LEFT)
	    edCursorLeft(xx);
	else
	    edCursorRight(xx);
    } else {
	/*
	 * determine which end we are operating on
	 */
	if (seq_length == 0)
	    /*
	     * a special case to make things work nicely
	     */
	    end = key;
	else if (pos == 1)
	    end = EXTEND_LEFT;
	else if (pos == seq_length+1) 
	    end = EXTEND_RIGHT;
	else
	    end = 0;

	/*
	 * May need to redraw numbering even if consensus length doesn't
	 * change
	 */
	if (seq == DBI(xx)->reference_seq)
	    xx->refresh_flags |= ED_DISP_RULER;

	if (end) {
	    if (! adjustMark(xx,seq,1,key,end)) {
		redisplayWithCursor(xx);
	    } else
		bell();
	} else {
	    if (key==EXTEND_LEFT)
		edCursorLeft(xx);
	    else
		edCursorRight(xx);
	}
	
    }

    getExtents(xx);
    return 0;
}




/*
 * Zap (unextend) to right end
 */
void zap_Right (EdStruct *xx)
{
    int seq,pos;
    int seq_length;
    int num_bases;
    
    /* check in valid edit mode */
    if (!(DBI_flags(xx) & DB_ACCESS_UPDATE) || xx->cursorSeq == 0) {
	bell();
	return;
    }
    
    seq = xx->cursorSeq;
    pos = xx->cursorPos;
    seq_length = DB_Length(xx,seq);

    /* Disallow in left cutoff region */
    if (xx->cursorPos <= 0) {
	bell();
	return;
    }

    num_bases = seq_length - pos + 1;
    setCursorPos(xx, seq_length+1);

    /*
     * May need to redraw numbering even if consensus length doesn't
     * change
     */
    if (seq == DBI(xx)->reference_seq)
	xx->refresh_flags |= ED_DISP_RULER;
    
    if (num_bases>0) {
	if (! adjustMark(xx,seq,num_bases,EXTEND_LEFT,EXTEND_RIGHT)) {
	    redisplayWithCursor(xx);
	} else
	    bell();
    } else {
	if (! adjustMark(xx,seq,-num_bases,EXTEND_RIGHT,EXTEND_RIGHT)) {
	    redisplayWithCursor(xx);
	} else
	    bell();
    }

    getExtents(xx);
}




/*
 * Zap (unextend) to left end
 */
void zap_Left (EdStruct *xx)
{
    int seq,pos;
    int seq_length;
    int num_bases;
    
    /* check in valid edit mode */
    if (!(DBI_flags(xx) & DB_ACCESS_UPDATE) || xx->cursorSeq == 0) {
	bell();
	return;
    }
    
    seq = xx->cursorSeq;
    pos = xx->cursorPos;
    seq_length = DB_Length(xx,seq);

    /* Disallow in right cutoff region */
    if (xx->cursorPos > seq_length+1) {
	bell();
	return;
    }

    num_bases = pos - 1;
    setCursorPos(xx, 1);

    if (num_bases>0) {
	if (! adjustMark(xx,seq,num_bases,EXTEND_RIGHT,EXTEND_LEFT)) {
	    redisplayWithCursor(xx);
	} else
	    bell();
    } else {
	if (! adjustMark(xx,seq,-num_bases,EXTEND_LEFT,EXTEND_LEFT)) {
	    redisplayWithCursor(xx);
	} else
	    bell();
    }

    getExtents(xx);
}




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


/*************************************************************
 ** Low level
 *************************************************************/


int _adjust_ends(DBInfo *db, int seq, int start_bases, int end_bases,
		 int seq_flags)
{
    _DBsetStart(db,seq,_DB_Start(db,seq)+start_bases);
    _DBsetEnd(db,seq,_DB_End(db,seq)+end_bases);
    _DBsetLength(db,seq,_DB_Length(db,seq)-start_bases+end_bases);
    _DBsetFlags(db,seq,seq_flags);

    return 0;
}




/*************************************************************
 ** Undo level
 *************************************************************/

int U_adjust_ends(EdStruct *xx, int seq, int start_bases, int end_bases)
{
    UndoStruct *u;
    int seq_flags, old_seq_flags;
    
    old_seq_flags = DB_Flags(xx,seq);
    /* UNDO - _left_cutoff_add_bases(xx,seq,tag,num_bases,bases) */
    if ( (u = newUndoStruct(DBI(xx))) != NULL ) {
	u->db = DBI(xx);
	u->command = UndoAdjustEnds;
	u->sequence = seq;
	u->info.adjust_ends.start_bases = -start_bases;
	u->info.adjust_ends.end_bases   = -end_bases;
	u->info.adjust_ends.seq_flags   = old_seq_flags;
	recordUndo(DBI(xx),u);
    }
    
    seq_flags = old_seq_flags | DB_FLAG_SEQ_MODIFIED | DB_FLAG_REL_MODIFIED;
    return _adjust_ends(DBI(xx), seq, start_bases, end_bases, seq_flags);
}


void U_adjust_display(EdStruct *xx, int n)
{
    UndoStruct *u;
    
    /* UNDO - xx->displayPos += n; */
    if ( (u = newUndoStruct(DBI(xx))) != NULL ) {
	u->db = DBI(xx);
	u->command = UndoAdjustDisplay;
	u->info.adjust_display.xx = xx;
	u->info.adjust_display.position = -n;
	u->info.adjust_display.editor_id = xx->editor_id;
	recordUndo(DBI(xx),u);
    }
    
    xx->displayPos += n;
    xx->refresh_flags |= ED_DISP_RULER | ED_DISP_SCROLL;
}






/*************************************************************
 ** Functional level
 *************************************************************/












/*
 * Extend a reading by num_bases in a particular direction
 */
int adjustMark(EdStruct *xx, int seq, int num_bases, int direction, int end)
{
    int max_bases;
    int extend;
    int orig_end;
    int comp;
    int old_len, new_len;
    
    /*
     * Can't adjust consensus!
     */
    if (seq == 0) return 1;
    
    old_len = DB_Length(xx, 0);

    /*
     * Determine if we are extending or unextending
     * Determine whether sequence is in the original sense
     * Determine original end we're dealing with
     */
    extend   = (end==direction);
    comp     = (DB_Comp(xx,seq)==COMPLEMENTED);
    orig_end = comp?OTHER_END(end):end;



    /*
     * Don't take more than is available
     */
    if (extend) {
	/* rules for extending */
	if (end == EXTEND_LEFT)
	    max_bases = DB_Start(xx,seq);
	else
	    max_bases = DB_Length2(xx,seq) - DB_End(xx,seq) + 1;
    } else {
	/* rules for unextending */
	max_bases = DB_Length(xx,seq) - 1;
    }
    if (num_bases > max_bases) num_bases = max_bases;

    /* no more room */
    if (num_bases <= 0) return 1;
    
    /* START UNDO */
    openUndo(DBI(xx));


    /*
     * Phase 1 - adjust marks
     */
    if (extend) {
	/* extending */
	if (end == EXTEND_LEFT) {
	    U_adjust_ends(xx,seq,-num_bases,0);
	} else {
	    U_adjust_ends(xx,seq,0,num_bases);
	}
	    
    } else {
	/* unextending */
	if (end == EXTEND_LEFT) {
	    U_adjust_ends(xx,seq,num_bases,0);
	} else {
	    U_adjust_ends(xx,seq,0,-num_bases);
	}
    }
    

    /*
     * Phase 1b - when dealing with left end we need to adjust position in gel
     */
    if (end == EXTEND_LEFT) {
	if (extend)
	    shiftLeft(xx,seq,num_bases);
	else
	    shiftRight(xx,seq,num_bases);
    }
    

    /*
     * Tags are now in absolute positions
     */
#if 0
    /*
     * Phase 2 - adjust tag positions
     */
    /* complemented? current_gel_end key_direction */
    if (extend) {
	/* determine current position of left end */
	if (direction==EXTEND_LEFT)
	    pos = 1;			/* fwd left  left / rev left  left */
	else
	    pos = DB_Length(xx,seq) + 1;/* rev right right/ fwd right right */
	tagInsertBases(xx,seq,pos,num_bases);
    } else {
	if (orig_end == EXTEND_LEFT) {
	    if (direction==EXTEND_LEFT)
		pos = DB_Length(xx,seq) + 1 - num_bases; /* rev right left */
	    else
		pos = 1;				 /* fwd left  right */
	} else {
	    if (direction==EXTEND_LEFT)
		pos = DB_Length(xx,seq) + 2 - num_bases; /* fwd right left */
	    else
		pos = 0;				 /* rev left  right */
	}
	tagDeleteBases(xx,seq,pos,num_bases);
    }
#endif


    /*
     * Phase 3 - adjust cursor position
     */
    if (end == EXTEND_RIGHT) {
	if (extend)
	    U_adjust_cursor(xx,num_bases);
	else
	    U_adjust_cursor(xx,-num_bases);
    } else {
	/* YUK! force cursor position on undo */
	U_adjust_cursor(xx,0);
    }

    
    /*
     * Phase 4 - adjust consensus length = Lengths are a changing
     */

    if (end == EXTEND_LEFT && DB_Length(xx, 0) != old_len) {
	int move_by = DB_Length(xx, 0) - old_len;
	U_adjust_display(xx, move_by);
    }


    if (xx->link) {
	xx->link->lockOffset = xx->link->xx[1]->displayPos -
	    xx->link->xx[0]->displayPos;
	setCursorPos(xx, xx->cursorPos);
    }

#if 0
    if (end == EXTEND_LEFT && DB_Length(xx, 0) != old_len) {
	if (DB_Length(xx, 0) > old_len) {
	    xx->displayPos += num_bases;
	    /* decDisplayPos(xx, num_bases); */
	} else {
	    xx->displayPos -= num_bases;
	    /* incDisplayPos(xx, num_bases); */
	}
    }
#endif

    new_len = calculate_consensus_length(xx);

    /*
     * This bit doesn't appear to be needed as it's apparently done elsewhere.
     * But I certainly can't see where! - jkb
     */
    if (DB_Length(xx, 0) != new_len) {
	U_change_consensus_length(xx, new_len);
    }


    /*
     * Phase 5 - adjust consensus tags
     */
    if (DB_Length(xx, 0) != old_len) {
	if (end == EXTEND_LEFT) {
	    if (DB_Length(xx, 0) > old_len)
		tagInsertBases(xx, 0, 1, DB_Length(xx, 0) - old_len);
	    else
		tagDeleteBases(xx, 0, old_len - DB_Length(xx, 0),
			       old_len - DB_Length(xx, 0));
	} else {
	    if (DB_Length(xx, 0) < old_len)
		tagDeleteBases(xx, 0, old_len, old_len - DB_Length(xx, 0));
	}
    }
    
#if 0
    if (DB_Length(xx, 0) != old_len) {
	if (extend && end == EXTEND_LEFT) {
		tagInsertBases(xx, 0, 0, DB_Length(xx, 0) - old_len);
	} else if (end == EXTEND_LEFT) {
	    tagDeleteBases(xx, 0, 0,
			   old_len - DB_Length(xx, 0));
	}
    }
#endif

    closeUndo(xx, DBI(xx));
    invalidate_consensus(xx);

    return 0;
    
}