-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalncode.c
284 lines (239 loc) · 8.06 KB
/
alncode.c
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
/* File: alncode.c
* Author: Richard Durbin ([email protected])
* Copyright (C) Richard Durbin, Cambridge University, 2024
*-------------------------------------------------------------------
* Description: IO for ONEcode .1aln files for Myers FASTGA package
* Exported functions:
* HISTORY:
* Last edited: Jul 15 13:10 2024 (rd109)
* Created: Sat Feb 24 12:19:16 2024 (rd109)
*-------------------------------------------------------------------
*/
#include <string.h>
#include <stdlib.h>
#include "alncode.h"
static char *alnSchemaText =
"1 3 def 2 1 schema for aln and FastGA\n"
".\n"
"P 3 seq SEQUENCE\n"
"O s 2 3 INT 6 STRING length and id for group of sequences = a scaffold\n"
"G S scaffolds (s) group sequence objects (S)\n"
"D n 2 4 CHAR 3 INT non-acgt chars outside (between) sequences within scaffold\n"
"O S 1 3 DNA sequence\n"
"D I 1 6 STRING identifier of sequence\n"
".\n"
"P 3 aln ALIGNMENTS\n"
"D t 1 3 INT trace point spacing in a - global\n"
"O a 0 groups A's into a colinear chain\n"
"G A chains (a) group alignment objects (A)\n"
"D p 2 3 INT 3 INT spacing in a,b between end of previous alignment and start of next\n"
". alignment: a_read[beg..end] b_read[beg..end], 0-indexing\n"
"O A 6 3 INT 3 INT 3 INT 3 INT 3 INT 3 INT\n"
"D L 2 3 INT 3 INT lengths of sequences a and b\n"
"D R 0 flag: reverse-complement sequence b\n"
"D Q 1 3 INT quality: alignment confidence in phred units\n"
"D M 1 3 INT match: number of matching bases\n"
"D D 1 3 INT differences: number of diffs = substitions + indels\n"
"D C 1 6 STRING cigar string: encodes precise alignment\n"
"D T 1 8 INT_LIST trace points in b\n"
"D X 1 8 INT_LIST number of differences in alignment per trace interval\n"
;
OneSchema *make_Aln_Schema ()
{ return (oneSchemaCreateFromText(alnSchemaText)); }
// Open the .1aln file for reading and read the header
OneFile *open_Aln_Read (char *filename, int nThreads,
int64 *nOverlaps, int *tspace,
char **db1_name, char **db2_name, char **cpath)
{ OneSchema *schema;
OneFile *of;
OneInfo *refInfo;
int i;
schema = oneSchemaCreateFromText(alnSchemaText);
if (schema == NULL)
{ fprintf (stderr, "failed to create 1aln schema\n");
return (NULL);
}
of = oneFileOpenRead(filename,schema,"aln",nThreads);
if (of == NULL)
{ fprintf (stderr,"%s: Failed to open .1aln file %s\n",Prog_Name,filename);
oneSchemaDestroy(schema);
return (NULL);
}
if (of->info['A'] == NULL)
{ fprintf(stderr,"%s: No alignments found in aln file\n",Prog_Name);
goto clean_up;
}
*nOverlaps = of->info['A']->given.count;
*db1_name = NULL;
*db2_name = NULL;
*cpath = NULL;
refInfo = of->info['<'];
if (refInfo == NULL)
{ fprintf(stderr,"%s: No references in aln file",Prog_Name);
goto clean_up;
}
for (i = 0; i < refInfo->accum.count; ++i)
if (of->reference[i].count == 1)
{ if (*db1_name != NULL)
free(*db1_name);
*db1_name = strdup(of->reference[i].filename);
}
else if (of->reference[i].count == 2)
{ if (*db2_name != NULL)
free(*db2_name);
*db2_name = strdup(of->reference[i].filename);
}
else if (of->reference[i].count == 3)
{ if (*cpath != NULL)
free(*cpath);
*cpath = strdup(of->reference[i].filename);
}
if (cpath == NULL)
*cpath = "";
*tspace = 0;
while (oneReadLine(of)) // advance to first alignment record
if (of->lineType == 'A')
break;
else if (of->lineType == 't')
*tspace = oneInt(of,0);
if (*tspace == 0)
{ fprintf(stderr,"%s: Did not find a t-line before first alignment\n",Prog_Name);
goto clean_up;
}
oneSchemaDestroy(schema);
return (of);
clean_up:
oneFileClose(of);
oneSchemaDestroy(schema);
return (NULL);
}
// Next two routines read the records from the file
void Read_Aln_Overlap(OneFile *of, Overlap *ovl)
{ if (of->lineType != 'A')
{ fprintf(stderr,"%s: Failed to be at start of alignment in Read_Aln_Overlap()\n",Prog_Name);
exit (1);
}
ovl->flags = 0;
ovl->aread = oneInt(of,0);
ovl->path.abpos = oneInt(of,1);
ovl->path.aepos = oneInt(of,2);
ovl->bread = oneInt(of,3);
ovl->path.bbpos = oneInt(of,4);
ovl->path.bepos = oneInt(of,5);
while (oneReadLine(of))
if (of->lineType == 'T')
break;
else if (of->lineType == 'R')
ovl->flags |= COMP_FLAG;
else if (of->lineType == 'D')
ovl->path.diffs = oneInt(of,0);
else if (of->lineType == 'A')
break;
if (of->lineType != 'T')
{ fprintf(stderr,"%s: Failed to find trace record in .1aln object %lld\n",
Prog_Name,of->info['A']->accum.count);
exit (1);
}
}
int Read_Aln_Trace(OneFile *of, uint8 *trace)
{ int64 *trace64;
int tlen;
int j, x;
if (of->lineType != 'T')
{ fprintf(stderr,"%s: Failed to be at start of trace in Read_Aln_Trace()\n",Prog_Name);
exit (1);
}
tlen = 2*oneLen(of);
trace64 = oneIntList(of);
j = 0;
for (x = 1; x < tlen; x += 2)
trace[x] = trace64[j++];
oneReadLine(of);
if (of->lineType != 'X')
{ fprintf(stderr,"%s: No X-line following a T-line in 1aln file\n",Prog_Name);
exit (1);
}
if (2*oneLen(of) != tlen)
{ fprintf(stderr,"%s: X-line and T-lines should have the same length\n",Prog_Name);
exit (1);
}
trace64 = oneIntList(of);
j = 0;
for (x = 0; x < tlen; x += 2)
trace[x] = trace64[j++];
while (oneReadLine(of)) // move to start of next alignment
if (of->lineType == 'A')
break;
return (tlen);
}
void Skip_Aln_Trace(OneFile *of)
{ int tlen;
if (of->lineType != 'T')
{ fprintf(stderr,"%s: Failed to be at start of trace in Read_Aln_Trace()\n",Prog_Name);
exit (1);
}
tlen = oneLen(of);
oneReadLine(of);
if (of->lineType != 'X')
{ fprintf(stderr,"%s: No X-line following a T-line in 1aln file\n",Prog_Name);
exit (1);
}
if (oneLen(of) != tlen)
{ fprintf(stderr,"%s: X-line and T-lines should have the same length\n",Prog_Name);
exit (1);
}
while (oneReadLine(of)) // move to start of next alignment
if (of->lineType == 'A')
break;
}
// And these routines write an alignment
OneFile *open_Aln_Write (char *filename, int nThreads,
char *progname, char *version, char *commandLine,
int tspace, char *db1_name, char *db2_name, char *cpath)
{ OneSchema *schema;
OneFile *of;
schema = oneSchemaCreateFromText(alnSchemaText);
if (schema == NULL)
{ fprintf(stderr,"%s: Failed to create 1aln schema\n",Prog_Name);
return 0;
}
of = oneFileOpenWriteNew(filename,schema,"aln",true,nThreads);
if (of == NULL)
{ fprintf(stderr,"%s: Failed to open .1aln file %s\n",Prog_Name,filename);
return 0;
}
oneAddProvenance(of,progname,version,commandLine);
oneAddReference(of,db1_name,1);
if (db2_name != NULL)
oneAddReference(of,db2_name,2);
if (cpath)
oneAddReference(of,cpath,3);
oneInt(of,0) = tspace;
oneWriteLine (of,'t',0,0);
oneSchemaDestroy (schema);
return of;
}
void Write_Aln_Overlap (OneFile *of, Overlap *ovl)
{ oneInt(of,0) = ovl->aread;
oneInt(of,1) = ovl->path.abpos;
oneInt(of,2) = ovl->path.aepos;
oneInt(of,3) = ovl->bread;
oneInt(of,4) = ovl->path.bbpos;
oneInt(of,5) = ovl->path.bepos;
oneWriteLine (of, 'A', 0, 0);
if (COMP(ovl->flags))
oneWriteLine(of,'R',0,0);
oneInt(of,0) = ovl->path.diffs;
oneWriteLine (of,'D',0,0);
}
void Write_Aln_Trace (OneFile *of, uint8 *trace, int tlen, int64 *trace64)
{ int j, x;
j = 0;
for (x = 1; x < tlen; x += 2)
trace64[j++] = trace[x];
oneWriteLine (of,'T',j,trace64);
j = 0;
for (x = 0; x < tlen; x += 2)
trace64[j++] = trace[x];
oneWriteLine(of,'X',j,trace64);
}