File: tkTraceComp.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (209 lines) | stat: -rw-r--r-- 5,054 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
/*
 * Title:  opp.c
 *
 * File:   opp.c
 * Purpose: code for complementing sequences
 *
 * Last update: Mon Oct 31, 1994
 *
 * 15.01.90 SD  Taken from seqIOEdit.c
 * 31.10.94 JKB Convert to use Read structure
*/


#include <os.h>
#include <io_lib/Read.h>

#include "tkTrace.h"
#include "tkTraceIO.h"

static char opp[256]; /* complement of any given base */

static void oppInitialize(void)
{
    int i;

    for (i = 0; i<256; i++) opp[i]='-';

    /* RMD 31/12/90 'N' -> '-' above.
     * removed 'N' and 'n' entries below and added reciprocal
     * 'K' and 'N' entries as for full Staden table
     */

    opp['A'] = 'T';
    opp['G'] = 'C';
    opp['T'] = 'A';
    opp['C'] = 'G';
    opp['a'] = 't';
    opp['g'] = 'c';
    opp['t'] = 'a';
    opp['c'] = 'g';
    opp['D'] = 'H';
    opp['H'] = 'D';
    opp['V'] = 'B';
    opp['B'] = 'V';
    opp['K'] = 'N';
    opp['N'] = 'K';
    opp['L'] = 'M';
    opp['M'] = 'L';
    opp['5'] = '6';
    opp['6'] = '5';
    opp['R'] = 'Y';
    opp['Y'] = 'R';
    opp['7'] = '7';
    opp['8'] = '8';
}

/*
 * An uncomplemented pyrosequencing trace has blank trace samples after a
 * tall peak in order to fit the subsequent base calls in.
 * When complemented the blank samples will be on the left of a run and the
 * peak on the right, so we shuffle the peaks back to maintain their order.
 */
static void shift_pyro_peaks(Read *r) {
    int i;

    for (i = r->NBases - 2; i >= 0; i--) {
	int j1;
	int j2;

	if (r->base[i] != r->base[i+1])
	    continue;

	j1 = r->basePos[i];
	j2 = r->basePos[i+1];

	if (r->traceA[j1] == 0 &&
	    r->traceC[j1] == 0 &&
	    r->traceG[j1] == 0 &&
	    r->traceT[j1] == 0) {
	    TRACE t;
	    t=r->traceA[j1]; r->traceA[j1]=r->traceA[j2]; r->traceA[j2]=t;
	    t=r->traceC[j1]; r->traceC[j1]=r->traceC[j2]; r->traceC[j2]=t;
	    t=r->traceG[j1]; r->traceG[j1]=r->traceG[j2]; r->traceG[j2]=t;
	    t=r->traceT[j1]; r->traceT[j1]=r->traceT[j2]; r->traceT[j2]=t;
	}
    }
}


/* ---- Exports ---- */

/*
 * Complement and reverse bases and traces
 */
void complement_read(Read *read, int len)
{
    static int initialised = 0;
    int_2 temp_int2;
    TRACE *temp_TRACEptr;
    char temp_char;
    int i;

    if (!initialised) {
	oppInitialize();
	initialised = 1;
    }

    /* swap */
#define swap(A,B,I) ( (I)=(A), (A)=(B), (B)=(I) )

    /* complement and reverse traces */
    /* swap traces A<->T and C<->G */
    swap(read->traceA,read->traceT,temp_TRACEptr);
    swap(read->traceC,read->traceG,temp_TRACEptr);

    /* reverse points in traces */
    if (read->traceA) {
	for (i=0;i<read->NPoints/2;i++) {
	    swap(read->traceA[i],read->traceA[read->NPoints-i-1],temp_int2);
	    swap(read->traceC[i],read->traceC[read->NPoints-i-1],temp_int2);
	    swap(read->traceG[i],read->traceG[read->NPoints-i-1],temp_int2);
	    swap(read->traceT[i],read->traceT[read->NPoints-i-1],temp_int2);
	}
    }

    /* complement the sequence */
    for (i=0;i<read->NBases;i++) {
	signed int tpos;

	read->base[i] = opp[(unsigned)read->base[i]];
	tpos = read->NPoints - read->basePos[i] - 1;
	read->basePos[i] = tpos > 0 ? tpos : 0;

	swap(read->prob_A[i], read->prob_T[i], temp_char);
	swap(read->prob_C[i], read->prob_G[i], temp_char);
    }

    /* reverse sequence */
    for (i=0;i<read->NBases/2;i++) {
	swap(read->base[i],read->base[read->NBases-i-1],temp_char);
	swap(read->basePos[i],read->basePos[read->NBases-i-1],temp_int2);
	swap(read->prob_A[i], read->prob_A[read->NBases-i-1], temp_char);
	swap(read->prob_C[i], read->prob_C[read->NBases-i-1], temp_char);
	swap(read->prob_G[i], read->prob_G[read->NBases-i-1], temp_char);
	swap(read->prob_T[i], read->prob_T[read->NBases-i-1], temp_char);
    }

    /* Always keep peaks in pyrosequencing to the "left" */
    if (read->traceA && read->flow) {
	shift_pyro_peaks(read);
    }

    /* swap cutoffs */
    i = read->leftCutoff;
    if (read->rightCutoff)
        read->leftCutoff = len - read->rightCutoff + 1;
    else
        read->leftCutoff = 0;

    if (i)
        read->rightCutoff = len - i + 1;
    else
        read->rightCutoff = 0;
}

/*
 * Complement and reverse a ted trace.
 */
void complement_trace(DNATrace *t) {
    int i;
    char temp_char;
    int_2 temp_int2;

    if (!t->read)
	return;

    complement_read(t->read, t->Ned);

    i = t->leftVector;
    if (t->rightVector != -1)
        t->leftVector = t->Ned - t->rightVector + 1;
    else
        t->leftVector = -1;

    if (i != -1)
        t->rightVector = t->Ned - i + 1;
    else
        t->rightVector = -1;

    /* complement the edited sequence */
    for (i=0;i<t->Ned;i++) {
	t->edBases[i] = opp[(unsigned)t->edBases[i]];
    }

    /* reverse sequence */
    for (i=0;i<t->Ned/2;i++) {
	swap(t->edBases[i],t->edBases[t->Ned-i-1],temp_char);
	swap(t->edPos[i],t->edPos[t->Ned-i-1],temp_int2);
	swap(t->edConf[i],t->edConf[t->Ned-i-1],temp_char);
    }

    /* screen position */
    t->disp_offset = t->read->NPoints - t->disp_offset - t->disp_width;

    t->comp ^= 1;

    /* Calculate trace positions */
    trace_init_pos(t);
}